Linear.Quaternion:$csin from linear-1.19.1.3

Percentage Accurate: 100.0% → 100.0%
Time: 9.2s
Alternatives: 8
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \cos x \cdot \frac{\sinh y}{y} \end{array} \]
(FPCore (x y) :precision binary64 (* (cos x) (/ (sinh y) y)))
double code(double x, double y) {
	return cos(x) * (sinh(y) / y);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = cos(x) * (sinh(y) / y)
end function
public static double code(double x, double y) {
	return Math.cos(x) * (Math.sinh(y) / y);
}
def code(x, y):
	return math.cos(x) * (math.sinh(y) / y)
function code(x, y)
	return Float64(cos(x) * Float64(sinh(y) / y))
end
function tmp = code(x, y)
	tmp = cos(x) * (sinh(y) / y);
end
code[x_, y_] := N[(N[Cos[x], $MachinePrecision] * N[(N[Sinh[y], $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos x \cdot \frac{\sinh y}{y}
\end{array}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 8 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos x \cdot \frac{\sinh y}{y} \end{array} \]
(FPCore (x y) :precision binary64 (* (cos x) (/ (sinh y) y)))
double code(double x, double y) {
	return cos(x) * (sinh(y) / y);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = cos(x) * (sinh(y) / y)
end function
public static double code(double x, double y) {
	return Math.cos(x) * (Math.sinh(y) / y);
}
def code(x, y):
	return math.cos(x) * (math.sinh(y) / y)
function code(x, y)
	return Float64(cos(x) * Float64(sinh(y) / y))
end
function tmp = code(x, y)
	tmp = cos(x) * (sinh(y) / y);
end
code[x_, y_] := N[(N[Cos[x], $MachinePrecision] * N[(N[Sinh[y], $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos x \cdot \frac{\sinh y}{y}
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos x \cdot \frac{\sinh y}{y} \end{array} \]
(FPCore (x y) :precision binary64 (* (cos x) (/ (sinh y) y)))
double code(double x, double y) {
	return cos(x) * (sinh(y) / y);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = cos(x) * (sinh(y) / y)
end function
public static double code(double x, double y) {
	return Math.cos(x) * (Math.sinh(y) / y);
}
def code(x, y):
	return math.cos(x) * (math.sinh(y) / y)
function code(x, y)
	return Float64(cos(x) * Float64(sinh(y) / y))
end
function tmp = code(x, y)
	tmp = cos(x) * (sinh(y) / y);
end
code[x_, y_] := N[(N[Cos[x], $MachinePrecision] * N[(N[Sinh[y], $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos x \cdot \frac{\sinh y}{y}
\end{array}
Derivation
  1. Initial program 100.0%

    \[\cos x \cdot \frac{\sinh y}{y} \]
  2. Add Preprocessing

Alternative 2: 93.3% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\sinh y}{y}\\ t_1 := \cos x \cdot t\_0\\ \mathbf{if}\;t\_1 \leq -\infty:\\ \;\;\;\;0.5 \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \left(1 - y\right)\right)\\ \mathbf{elif}\;t\_1 \leq 0.9999996849910348:\\ \;\;\;\;\cos x \cdot \frac{y}{y}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ (sinh y) y)) (t_1 (* (cos x) t_0)))
   (if (<= t_1 (- INFINITY))
     (* 0.5 (* (/ (expm1 (+ y y)) y) (- 1.0 y)))
     (if (<= t_1 0.9999996849910348) (* (cos x) (/ y y)) (* 1.0 t_0)))))
double code(double x, double y) {
	double t_0 = sinh(y) / y;
	double t_1 = cos(x) * t_0;
	double tmp;
	if (t_1 <= -((double) INFINITY)) {
		tmp = 0.5 * ((expm1((y + y)) / y) * (1.0 - y));
	} else if (t_1 <= 0.9999996849910348) {
		tmp = cos(x) * (y / y);
	} else {
		tmp = 1.0 * t_0;
	}
	return tmp;
}
public static double code(double x, double y) {
	double t_0 = Math.sinh(y) / y;
	double t_1 = Math.cos(x) * t_0;
	double tmp;
	if (t_1 <= -Double.POSITIVE_INFINITY) {
		tmp = 0.5 * ((Math.expm1((y + y)) / y) * (1.0 - y));
	} else if (t_1 <= 0.9999996849910348) {
		tmp = Math.cos(x) * (y / y);
	} else {
		tmp = 1.0 * t_0;
	}
	return tmp;
}
def code(x, y):
	t_0 = math.sinh(y) / y
	t_1 = math.cos(x) * t_0
	tmp = 0
	if t_1 <= -math.inf:
		tmp = 0.5 * ((math.expm1((y + y)) / y) * (1.0 - y))
	elif t_1 <= 0.9999996849910348:
		tmp = math.cos(x) * (y / y)
	else:
		tmp = 1.0 * t_0
	return tmp
function code(x, y)
	t_0 = Float64(sinh(y) / y)
	t_1 = Float64(cos(x) * t_0)
	tmp = 0.0
	if (t_1 <= Float64(-Inf))
		tmp = Float64(0.5 * Float64(Float64(expm1(Float64(y + y)) / y) * Float64(1.0 - y)));
	elseif (t_1 <= 0.9999996849910348)
		tmp = Float64(cos(x) * Float64(y / y));
	else
		tmp = Float64(1.0 * t_0);
	end
	return tmp
end
code[x_, y_] := Block[{t$95$0 = N[(N[Sinh[y], $MachinePrecision] / y), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[x], $MachinePrecision] * t$95$0), $MachinePrecision]}, If[LessEqual[t$95$1, (-Infinity)], N[(0.5 * N[(N[(N[(Exp[N[(y + y), $MachinePrecision]] - 1), $MachinePrecision] / y), $MachinePrecision] * N[(1.0 - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 0.9999996849910348], N[(N[Cos[x], $MachinePrecision] * N[(y / y), $MachinePrecision]), $MachinePrecision], N[(1.0 * t$95$0), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\sinh y}{y}\\
t_1 := \cos x \cdot t\_0\\
\mathbf{if}\;t\_1 \leq -\infty:\\
\;\;\;\;0.5 \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \left(1 - y\right)\right)\\

\mathbf{elif}\;t\_1 \leq 0.9999996849910348:\\
\;\;\;\;\cos x \cdot \frac{y}{y}\\

\mathbf{else}:\\
\;\;\;\;1 \cdot t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 (cos.f64 x) (/.f64 (sinh.f64 y) y)) < -inf.0

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{e^{y} - \frac{1}{e^{y}}}{y}} \]
    3. Applied rewrites39.6%

      \[\leadsto \color{blue}{0.5 \cdot \frac{e^{y} - \frac{1}{e^{y}}}{y}} \]
    4. Applied rewrites46.3%

      \[\leadsto 0.5 \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \color{blue}{e^{-y}}\right) \]
    5. Taylor expanded in y around 0

      \[\leadsto \frac{1}{2} \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \left(1 + \color{blue}{-1 \cdot y}\right)\right) \]
    6. Applied rewrites34.5%

      \[\leadsto 0.5 \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \left(1 + \color{blue}{-1 \cdot y}\right)\right) \]
    7. Applied rewrites34.5%

      \[\leadsto \color{blue}{0.5 \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \left(1 - y\right)\right)} \]

    if -inf.0 < (*.f64 (cos.f64 x) (/.f64 (sinh.f64 y) y)) < 0.999999684991034754

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in y around 0

      \[\leadsto \cos x \cdot \frac{\color{blue}{y}}{y} \]
    3. Applied rewrites51.4%

      \[\leadsto \cos x \cdot \frac{\color{blue}{y}}{y} \]

    if 0.999999684991034754 < (*.f64 (cos.f64 x) (/.f64 (sinh.f64 y) y))

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{1} \cdot \frac{\sinh y}{y} \]
    3. Applied rewrites65.1%

      \[\leadsto \color{blue}{1} \cdot \frac{\sinh y}{y} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 3: 77.8% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\sinh y}{y}\\ \mathbf{if}\;\cos x \cdot t\_0 \leq -0.04:\\ \;\;\;\;\mathsf{fma}\left(x \cdot x, -0.5, 1\right) \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;1 \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ (sinh y) y)))
   (if (<= (* (cos x) t_0) -0.04) (* (fma (* x x) -0.5 1.0) t_0) (* 1.0 t_0))))
double code(double x, double y) {
	double t_0 = sinh(y) / y;
	double tmp;
	if ((cos(x) * t_0) <= -0.04) {
		tmp = fma((x * x), -0.5, 1.0) * t_0;
	} else {
		tmp = 1.0 * t_0;
	}
	return tmp;
}
function code(x, y)
	t_0 = Float64(sinh(y) / y)
	tmp = 0.0
	if (Float64(cos(x) * t_0) <= -0.04)
		tmp = Float64(fma(Float64(x * x), -0.5, 1.0) * t_0);
	else
		tmp = Float64(1.0 * t_0);
	end
	return tmp
end
code[x_, y_] := Block[{t$95$0 = N[(N[Sinh[y], $MachinePrecision] / y), $MachinePrecision]}, If[LessEqual[N[(N[Cos[x], $MachinePrecision] * t$95$0), $MachinePrecision], -0.04], N[(N[(N[(x * x), $MachinePrecision] * -0.5 + 1.0), $MachinePrecision] * t$95$0), $MachinePrecision], N[(1.0 * t$95$0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\sinh y}{y}\\
\mathbf{if}\;\cos x \cdot t\_0 \leq -0.04:\\
\;\;\;\;\mathsf{fma}\left(x \cdot x, -0.5, 1\right) \cdot t\_0\\

\mathbf{else}:\\
\;\;\;\;1 \cdot t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (cos.f64 x) (/.f64 (sinh.f64 y) y)) < -0.0400000000000000008

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\left(1 + \frac{-1}{2} \cdot {x}^{2}\right)} \cdot \frac{\sinh y}{y} \]
    3. Applied rewrites63.3%

      \[\leadsto \color{blue}{\left(1 + -0.5 \cdot {x}^{2}\right)} \cdot \frac{\sinh y}{y} \]
    4. Applied rewrites63.3%

      \[\leadsto \mathsf{fma}\left(x \cdot x, \color{blue}{-0.5}, 1\right) \cdot \frac{\sinh y}{y} \]

    if -0.0400000000000000008 < (*.f64 (cos.f64 x) (/.f64 (sinh.f64 y) y))

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{1} \cdot \frac{\sinh y}{y} \]
    3. Applied rewrites65.1%

      \[\leadsto \color{blue}{1} \cdot \frac{\sinh y}{y} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 72.0% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\sinh y}{y}\\ \mathbf{if}\;\cos x \cdot t\_0 \leq 0.65:\\ \;\;\;\;0.5 \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \left(1 - y\right)\right)\\ \mathbf{else}:\\ \;\;\;\;1 \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ (sinh y) y)))
   (if (<= (* (cos x) t_0) 0.65)
     (* 0.5 (* (/ (expm1 (+ y y)) y) (- 1.0 y)))
     (* 1.0 t_0))))
double code(double x, double y) {
	double t_0 = sinh(y) / y;
	double tmp;
	if ((cos(x) * t_0) <= 0.65) {
		tmp = 0.5 * ((expm1((y + y)) / y) * (1.0 - y));
	} else {
		tmp = 1.0 * t_0;
	}
	return tmp;
}
public static double code(double x, double y) {
	double t_0 = Math.sinh(y) / y;
	double tmp;
	if ((Math.cos(x) * t_0) <= 0.65) {
		tmp = 0.5 * ((Math.expm1((y + y)) / y) * (1.0 - y));
	} else {
		tmp = 1.0 * t_0;
	}
	return tmp;
}
def code(x, y):
	t_0 = math.sinh(y) / y
	tmp = 0
	if (math.cos(x) * t_0) <= 0.65:
		tmp = 0.5 * ((math.expm1((y + y)) / y) * (1.0 - y))
	else:
		tmp = 1.0 * t_0
	return tmp
function code(x, y)
	t_0 = Float64(sinh(y) / y)
	tmp = 0.0
	if (Float64(cos(x) * t_0) <= 0.65)
		tmp = Float64(0.5 * Float64(Float64(expm1(Float64(y + y)) / y) * Float64(1.0 - y)));
	else
		tmp = Float64(1.0 * t_0);
	end
	return tmp
end
code[x_, y_] := Block[{t$95$0 = N[(N[Sinh[y], $MachinePrecision] / y), $MachinePrecision]}, If[LessEqual[N[(N[Cos[x], $MachinePrecision] * t$95$0), $MachinePrecision], 0.65], N[(0.5 * N[(N[(N[(Exp[N[(y + y), $MachinePrecision]] - 1), $MachinePrecision] / y), $MachinePrecision] * N[(1.0 - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 * t$95$0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\sinh y}{y}\\
\mathbf{if}\;\cos x \cdot t\_0 \leq 0.65:\\
\;\;\;\;0.5 \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \left(1 - y\right)\right)\\

\mathbf{else}:\\
\;\;\;\;1 \cdot t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (cos.f64 x) (/.f64 (sinh.f64 y) y)) < 0.650000000000000022

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{e^{y} - \frac{1}{e^{y}}}{y}} \]
    3. Applied rewrites39.6%

      \[\leadsto \color{blue}{0.5 \cdot \frac{e^{y} - \frac{1}{e^{y}}}{y}} \]
    4. Applied rewrites46.3%

      \[\leadsto 0.5 \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \color{blue}{e^{-y}}\right) \]
    5. Taylor expanded in y around 0

      \[\leadsto \frac{1}{2} \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \left(1 + \color{blue}{-1 \cdot y}\right)\right) \]
    6. Applied rewrites34.5%

      \[\leadsto 0.5 \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \left(1 + \color{blue}{-1 \cdot y}\right)\right) \]
    7. Applied rewrites34.5%

      \[\leadsto \color{blue}{0.5 \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \left(1 - y\right)\right)} \]

    if 0.650000000000000022 < (*.f64 (cos.f64 x) (/.f64 (sinh.f64 y) y))

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{1} \cdot \frac{\sinh y}{y} \]
    3. Applied rewrites65.1%

      \[\leadsto \color{blue}{1} \cdot \frac{\sinh y}{y} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 71.3% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\sinh y}{y}\\ \mathbf{if}\;\cos x \cdot t\_0 \leq -0.04:\\ \;\;\;\;\mathsf{fma}\left(x \cdot x, -0.5, 1\right) \cdot \frac{y}{y}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ (sinh y) y)))
   (if (<= (* (cos x) t_0) -0.04)
     (* (fma (* x x) -0.5 1.0) (/ y y))
     (* 1.0 t_0))))
double code(double x, double y) {
	double t_0 = sinh(y) / y;
	double tmp;
	if ((cos(x) * t_0) <= -0.04) {
		tmp = fma((x * x), -0.5, 1.0) * (y / y);
	} else {
		tmp = 1.0 * t_0;
	}
	return tmp;
}
function code(x, y)
	t_0 = Float64(sinh(y) / y)
	tmp = 0.0
	if (Float64(cos(x) * t_0) <= -0.04)
		tmp = Float64(fma(Float64(x * x), -0.5, 1.0) * Float64(y / y));
	else
		tmp = Float64(1.0 * t_0);
	end
	return tmp
end
code[x_, y_] := Block[{t$95$0 = N[(N[Sinh[y], $MachinePrecision] / y), $MachinePrecision]}, If[LessEqual[N[(N[Cos[x], $MachinePrecision] * t$95$0), $MachinePrecision], -0.04], N[(N[(N[(x * x), $MachinePrecision] * -0.5 + 1.0), $MachinePrecision] * N[(y / y), $MachinePrecision]), $MachinePrecision], N[(1.0 * t$95$0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\sinh y}{y}\\
\mathbf{if}\;\cos x \cdot t\_0 \leq -0.04:\\
\;\;\;\;\mathsf{fma}\left(x \cdot x, -0.5, 1\right) \cdot \frac{y}{y}\\

\mathbf{else}:\\
\;\;\;\;1 \cdot t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (cos.f64 x) (/.f64 (sinh.f64 y) y)) < -0.0400000000000000008

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in y around 0

      \[\leadsto \cos x \cdot \frac{\color{blue}{y}}{y} \]
    3. Applied rewrites51.4%

      \[\leadsto \cos x \cdot \frac{\color{blue}{y}}{y} \]
    4. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\left(1 + \frac{-1}{2} \cdot {x}^{2}\right)} \cdot \frac{y}{y} \]
    5. Applied rewrites33.2%

      \[\leadsto \color{blue}{\left(1 + -0.5 \cdot {x}^{2}\right)} \cdot \frac{y}{y} \]
    6. Applied rewrites33.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, -0.5, 1\right)} \cdot \frac{y}{y} \]

    if -0.0400000000000000008 < (*.f64 (cos.f64 x) (/.f64 (sinh.f64 y) y))

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{1} \cdot \frac{\sinh y}{y} \]
    3. Applied rewrites65.1%

      \[\leadsto \color{blue}{1} \cdot \frac{\sinh y}{y} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 35.8% accurate, 2.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 2.5 \cdot 10^{+188}:\\ \;\;\;\;0.5 \cdot \left(\left(2 + 2 \cdot y\right) \cdot \left(1 + -1 \cdot y\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x \cdot x, -0.5, 1\right) \cdot \frac{y}{y}\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= x 2.5e+188)
   (* 0.5 (* (+ 2.0 (* 2.0 y)) (+ 1.0 (* -1.0 y))))
   (* (fma (* x x) -0.5 1.0) (/ y y))))
double code(double x, double y) {
	double tmp;
	if (x <= 2.5e+188) {
		tmp = 0.5 * ((2.0 + (2.0 * y)) * (1.0 + (-1.0 * y)));
	} else {
		tmp = fma((x * x), -0.5, 1.0) * (y / y);
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if (x <= 2.5e+188)
		tmp = Float64(0.5 * Float64(Float64(2.0 + Float64(2.0 * y)) * Float64(1.0 + Float64(-1.0 * y))));
	else
		tmp = Float64(fma(Float64(x * x), -0.5, 1.0) * Float64(y / y));
	end
	return tmp
end
code[x_, y_] := If[LessEqual[x, 2.5e+188], N[(0.5 * N[(N[(2.0 + N[(2.0 * y), $MachinePrecision]), $MachinePrecision] * N[(1.0 + N[(-1.0 * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(x * x), $MachinePrecision] * -0.5 + 1.0), $MachinePrecision] * N[(y / y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.5 \cdot 10^{+188}:\\
\;\;\;\;0.5 \cdot \left(\left(2 + 2 \cdot y\right) \cdot \left(1 + -1 \cdot y\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x \cdot x, -0.5, 1\right) \cdot \frac{y}{y}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 2.5000000000000001e188

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{e^{y} - \frac{1}{e^{y}}}{y}} \]
    3. Applied rewrites39.6%

      \[\leadsto \color{blue}{0.5 \cdot \frac{e^{y} - \frac{1}{e^{y}}}{y}} \]
    4. Applied rewrites46.3%

      \[\leadsto 0.5 \cdot \left(\frac{\mathsf{expm1}\left(y + y\right)}{y} \cdot \color{blue}{e^{-y}}\right) \]
    5. Taylor expanded in y around 0

      \[\leadsto \frac{1}{2} \cdot \left(\left(2 + 2 \cdot y\right) \cdot e^{\color{blue}{-y}}\right) \]
    6. Applied rewrites34.3%

      \[\leadsto 0.5 \cdot \left(\left(2 + 2 \cdot y\right) \cdot e^{\color{blue}{-y}}\right) \]
    7. Taylor expanded in y around 0

      \[\leadsto \frac{1}{2} \cdot \left(\left(2 + 2 \cdot y\right) \cdot \left(1 + \color{blue}{-1 \cdot y}\right)\right) \]
    8. Applied rewrites34.5%

      \[\leadsto 0.5 \cdot \left(\left(2 + 2 \cdot y\right) \cdot \left(1 + \color{blue}{-1 \cdot y}\right)\right) \]

    if 2.5000000000000001e188 < x

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in y around 0

      \[\leadsto \cos x \cdot \frac{\color{blue}{y}}{y} \]
    3. Applied rewrites51.4%

      \[\leadsto \cos x \cdot \frac{\color{blue}{y}}{y} \]
    4. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\left(1 + \frac{-1}{2} \cdot {x}^{2}\right)} \cdot \frac{y}{y} \]
    5. Applied rewrites33.2%

      \[\leadsto \color{blue}{\left(1 + -0.5 \cdot {x}^{2}\right)} \cdot \frac{y}{y} \]
    6. Applied rewrites33.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, -0.5, 1\right)} \cdot \frac{y}{y} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 7: 35.1% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\cos x \cdot \frac{\sinh y}{y} \leq -0.04:\\ \;\;\;\;\mathsf{fma}\left(x \cdot x, -0.5, 1\right) \cdot \frac{y}{y}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot 2\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (* (cos x) (/ (sinh y) y)) -0.04)
   (* (fma (* x x) -0.5 1.0) (/ y y))
   (* 0.5 2.0)))
double code(double x, double y) {
	double tmp;
	if ((cos(x) * (sinh(y) / y)) <= -0.04) {
		tmp = fma((x * x), -0.5, 1.0) * (y / y);
	} else {
		tmp = 0.5 * 2.0;
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if (Float64(cos(x) * Float64(sinh(y) / y)) <= -0.04)
		tmp = Float64(fma(Float64(x * x), -0.5, 1.0) * Float64(y / y));
	else
		tmp = Float64(0.5 * 2.0);
	end
	return tmp
end
code[x_, y_] := If[LessEqual[N[(N[Cos[x], $MachinePrecision] * N[(N[Sinh[y], $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision], -0.04], N[(N[(N[(x * x), $MachinePrecision] * -0.5 + 1.0), $MachinePrecision] * N[(y / y), $MachinePrecision]), $MachinePrecision], N[(0.5 * 2.0), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\cos x \cdot \frac{\sinh y}{y} \leq -0.04:\\
\;\;\;\;\mathsf{fma}\left(x \cdot x, -0.5, 1\right) \cdot \frac{y}{y}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot 2\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (cos.f64 x) (/.f64 (sinh.f64 y) y)) < -0.0400000000000000008

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in y around 0

      \[\leadsto \cos x \cdot \frac{\color{blue}{y}}{y} \]
    3. Applied rewrites51.4%

      \[\leadsto \cos x \cdot \frac{\color{blue}{y}}{y} \]
    4. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\left(1 + \frac{-1}{2} \cdot {x}^{2}\right)} \cdot \frac{y}{y} \]
    5. Applied rewrites33.2%

      \[\leadsto \color{blue}{\left(1 + -0.5 \cdot {x}^{2}\right)} \cdot \frac{y}{y} \]
    6. Applied rewrites33.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, -0.5, 1\right)} \cdot \frac{y}{y} \]

    if -0.0400000000000000008 < (*.f64 (cos.f64 x) (/.f64 (sinh.f64 y) y))

    1. Initial program 100.0%

      \[\cos x \cdot \frac{\sinh y}{y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{e^{y} - \frac{1}{e^{y}}}{y}} \]
    3. Applied rewrites39.6%

      \[\leadsto \color{blue}{0.5 \cdot \frac{e^{y} - \frac{1}{e^{y}}}{y}} \]
    4. Taylor expanded in y around 0

      \[\leadsto \frac{1}{2} \cdot 2 \]
    5. Applied rewrites29.0%

      \[\leadsto 0.5 \cdot 2 \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 8: 29.0% accurate, 13.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot 2 \end{array} \]
(FPCore (x y) :precision binary64 (* 0.5 2.0))
double code(double x, double y) {
	return 0.5 * 2.0;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = 0.5d0 * 2.0d0
end function
public static double code(double x, double y) {
	return 0.5 * 2.0;
}
def code(x, y):
	return 0.5 * 2.0
function code(x, y)
	return Float64(0.5 * 2.0)
end
function tmp = code(x, y)
	tmp = 0.5 * 2.0;
end
code[x_, y_] := N[(0.5 * 2.0), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot 2
\end{array}
Derivation
  1. Initial program 100.0%

    \[\cos x \cdot \frac{\sinh y}{y} \]
  2. Taylor expanded in x around 0

    \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{e^{y} - \frac{1}{e^{y}}}{y}} \]
  3. Applied rewrites39.6%

    \[\leadsto \color{blue}{0.5 \cdot \frac{e^{y} - \frac{1}{e^{y}}}{y}} \]
  4. Taylor expanded in y around 0

    \[\leadsto \frac{1}{2} \cdot 2 \]
  5. Applied rewrites29.0%

    \[\leadsto 0.5 \cdot 2 \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 2025153 
(FPCore (x y)
  :name "Linear.Quaternion:$csin from linear-1.19.1.3"
  :precision binary64
  (* (cos x) (/ (sinh y) y)))