
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
double code(double x, double y) {
return ((x - y) * (x + y)) / ((x * x) + (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 = ((x - y) * (x + y)) / ((x * x) + (y * y))
end function
public static double code(double x, double y) {
return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
def code(x, y): return ((x - y) * (x + y)) / ((x * x) + (y * y))
function code(x, y) return Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y))) end
function tmp = code(x, y) tmp = ((x - y) * (x + y)) / ((x * x) + (y * y)); end
code[x_, y_] := N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
double code(double x, double y) {
return ((x - y) * (x + y)) / ((x * x) + (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 = ((x - y) * (x + y)) / ((x * x) + (y * y))
end function
public static double code(double x, double y) {
return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
def code(x, y): return ((x - y) * (x + y)) / ((x * x) + (y * y))
function code(x, y) return Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y))) end
function tmp = code(x, y) tmp = ((x - y) * (x + y)) / ((x * x) + (y * y)); end
code[x_, y_] := N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\end{array}
y_m = (fabs.f64 y)
(FPCore (x y_m)
:precision binary64
(let* ((t_0 (* (log x) 2.0)))
(if (<= y_m 2.25e-181)
(fma
(exp
(/
(- (pow (* (log y_m) 2.0) 2.0) (pow t_0 2.0))
(fma (* -1.0 (log y_m)) -2.0 t_0)))
-2.0
1.0)
(if (<= y_m 1.08e-44)
(/ (* (- x y_m) (+ x y_m)) (+ (* x x) (* y_m y_m)))
(* -1.0 (+ (* 0.0 (/ x y_m)) 1.0))))))y_m = fabs(y);
double code(double x, double y_m) {
double t_0 = log(x) * 2.0;
double tmp;
if (y_m <= 2.25e-181) {
tmp = fma(exp(((pow((log(y_m) * 2.0), 2.0) - pow(t_0, 2.0)) / fma((-1.0 * log(y_m)), -2.0, t_0))), -2.0, 1.0);
} else if (y_m <= 1.08e-44) {
tmp = ((x - y_m) * (x + y_m)) / ((x * x) + (y_m * y_m));
} else {
tmp = -1.0 * ((0.0 * (x / y_m)) + 1.0);
}
return tmp;
}
y_m = abs(y) function code(x, y_m) t_0 = Float64(log(x) * 2.0) tmp = 0.0 if (y_m <= 2.25e-181) tmp = fma(exp(Float64(Float64((Float64(log(y_m) * 2.0) ^ 2.0) - (t_0 ^ 2.0)) / fma(Float64(-1.0 * log(y_m)), -2.0, t_0))), -2.0, 1.0); elseif (y_m <= 1.08e-44) tmp = Float64(Float64(Float64(x - y_m) * Float64(x + y_m)) / Float64(Float64(x * x) + Float64(y_m * y_m))); else tmp = Float64(-1.0 * Float64(Float64(0.0 * Float64(x / y_m)) + 1.0)); end return tmp end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := Block[{t$95$0 = N[(N[Log[x], $MachinePrecision] * 2.0), $MachinePrecision]}, If[LessEqual[y$95$m, 2.25e-181], N[(N[Exp[N[(N[(N[Power[N[(N[Log[y$95$m], $MachinePrecision] * 2.0), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision] / N[(N[(-1.0 * N[Log[y$95$m], $MachinePrecision]), $MachinePrecision] * -2.0 + t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * -2.0 + 1.0), $MachinePrecision], If[LessEqual[y$95$m, 1.08e-44], N[(N[(N[(x - y$95$m), $MachinePrecision] * N[(x + y$95$m), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y$95$m * y$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-1.0 * N[(N[(0.0 * N[(x / y$95$m), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
y_m = \left|y\right|
\\
\begin{array}{l}
t_0 := \log x \cdot 2\\
\mathbf{if}\;y\_m \leq 2.25 \cdot 10^{-181}:\\
\;\;\;\;\mathsf{fma}\left(e^{\frac{{\left(\log y\_m \cdot 2\right)}^{2} - {t\_0}^{2}}{\mathsf{fma}\left(-1 \cdot \log y\_m, -2, t\_0\right)}}, -2, 1\right)\\
\mathbf{elif}\;y\_m \leq 1.08 \cdot 10^{-44}:\\
\;\;\;\;\frac{\left(x - y\_m\right) \cdot \left(x + y\_m\right)}{x \cdot x + y\_m \cdot y\_m}\\
\mathbf{else}:\\
\;\;\;\;-1 \cdot \left(0 \cdot \frac{x}{y\_m} + 1\right)\\
\end{array}
\end{array}
if y < 2.2499999999999999e-181Initial program 66.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
pow2N/A
pow2N/A
frac-timesN/A
sqr-neg-revN/A
mul-1-negN/A
mul-1-negN/A
pow2N/A
lower-pow.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6439.9
Applied rewrites39.9%
lift-pow.f64N/A
lift-*.f64N/A
lift-/.f64N/A
*-commutativeN/A
pow2N/A
mul-1-negN/A
mul-1-negN/A
sqr-neg-revN/A
frac-timesN/A
pow2N/A
pow-to-expN/A
pow2N/A
pow-to-expN/A
div-expN/A
lower-exp.f64N/A
lower--.f64N/A
lower-*.f64N/A
lower-log.f64N/A
lower-*.f64N/A
lower-log.f6415.3
Applied rewrites15.3%
lift--.f64N/A
lift-*.f64N/A
lift-log.f64N/A
lift-*.f64N/A
lift-log.f64N/A
distribute-rgt-out--N/A
distribute-lft-out--N/A
flip--N/A
lower-/.f64N/A
Applied rewrites15.3%
if 2.2499999999999999e-181 < y < 1.07999999999999994e-44Initial program 99.9%
if 1.07999999999999994e-44 < y Initial program 100.0%
Taylor expanded in y around -inf
lower--.f64N/A
*-commutativeN/A
div-addN/A
associate-*r/N/A
+-commutativeN/A
lower-*.f64N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f64100.0
Applied rewrites100.0%
Final simplification31.9%
y_m = (fabs.f64 y)
(FPCore (x y_m)
:precision binary64
(let* ((t_0 (* (log x) 2.0)))
(if (<= y_m 2.25e-181)
(fma
(exp
(/
(- (pow (* (log y_m) 2.0) 2.0) (pow t_0 2.0))
(fma (* -1.0 (log y_m)) -2.0 t_0)))
-2.0
1.0)
(if (<= y_m 1.08e-44)
(/
(* (- x y_m) (* (fma (/ x y_m) 1.0 1.0) y_m))
(+ (* x x) (* y_m y_m)))
(* -1.0 (+ (* 0.0 (/ x y_m)) 1.0))))))y_m = fabs(y);
double code(double x, double y_m) {
double t_0 = log(x) * 2.0;
double tmp;
if (y_m <= 2.25e-181) {
tmp = fma(exp(((pow((log(y_m) * 2.0), 2.0) - pow(t_0, 2.0)) / fma((-1.0 * log(y_m)), -2.0, t_0))), -2.0, 1.0);
} else if (y_m <= 1.08e-44) {
tmp = ((x - y_m) * (fma((x / y_m), 1.0, 1.0) * y_m)) / ((x * x) + (y_m * y_m));
} else {
tmp = -1.0 * ((0.0 * (x / y_m)) + 1.0);
}
return tmp;
}
y_m = abs(y) function code(x, y_m) t_0 = Float64(log(x) * 2.0) tmp = 0.0 if (y_m <= 2.25e-181) tmp = fma(exp(Float64(Float64((Float64(log(y_m) * 2.0) ^ 2.0) - (t_0 ^ 2.0)) / fma(Float64(-1.0 * log(y_m)), -2.0, t_0))), -2.0, 1.0); elseif (y_m <= 1.08e-44) tmp = Float64(Float64(Float64(x - y_m) * Float64(fma(Float64(x / y_m), 1.0, 1.0) * y_m)) / Float64(Float64(x * x) + Float64(y_m * y_m))); else tmp = Float64(-1.0 * Float64(Float64(0.0 * Float64(x / y_m)) + 1.0)); end return tmp end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := Block[{t$95$0 = N[(N[Log[x], $MachinePrecision] * 2.0), $MachinePrecision]}, If[LessEqual[y$95$m, 2.25e-181], N[(N[Exp[N[(N[(N[Power[N[(N[Log[y$95$m], $MachinePrecision] * 2.0), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision] / N[(N[(-1.0 * N[Log[y$95$m], $MachinePrecision]), $MachinePrecision] * -2.0 + t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * -2.0 + 1.0), $MachinePrecision], If[LessEqual[y$95$m, 1.08e-44], N[(N[(N[(x - y$95$m), $MachinePrecision] * N[(N[(N[(x / y$95$m), $MachinePrecision] * 1.0 + 1.0), $MachinePrecision] * y$95$m), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y$95$m * y$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-1.0 * N[(N[(0.0 * N[(x / y$95$m), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
y_m = \left|y\right|
\\
\begin{array}{l}
t_0 := \log x \cdot 2\\
\mathbf{if}\;y\_m \leq 2.25 \cdot 10^{-181}:\\
\;\;\;\;\mathsf{fma}\left(e^{\frac{{\left(\log y\_m \cdot 2\right)}^{2} - {t\_0}^{2}}{\mathsf{fma}\left(-1 \cdot \log y\_m, -2, t\_0\right)}}, -2, 1\right)\\
\mathbf{elif}\;y\_m \leq 1.08 \cdot 10^{-44}:\\
\;\;\;\;\frac{\left(x - y\_m\right) \cdot \left(\mathsf{fma}\left(\frac{x}{y\_m}, 1, 1\right) \cdot y\_m\right)}{x \cdot x + y\_m \cdot y\_m}\\
\mathbf{else}:\\
\;\;\;\;-1 \cdot \left(0 \cdot \frac{x}{y\_m} + 1\right)\\
\end{array}
\end{array}
if y < 2.2499999999999999e-181Initial program 66.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
pow2N/A
pow2N/A
frac-timesN/A
sqr-neg-revN/A
mul-1-negN/A
mul-1-negN/A
pow2N/A
lower-pow.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6439.9
Applied rewrites39.9%
lift-pow.f64N/A
lift-*.f64N/A
lift-/.f64N/A
*-commutativeN/A
pow2N/A
mul-1-negN/A
mul-1-negN/A
sqr-neg-revN/A
frac-timesN/A
pow2N/A
pow-to-expN/A
pow2N/A
pow-to-expN/A
div-expN/A
lower-exp.f64N/A
lower--.f64N/A
lower-*.f64N/A
lower-log.f64N/A
lower-*.f64N/A
lower-log.f6415.3
Applied rewrites15.3%
lift--.f64N/A
lift-*.f64N/A
lift-log.f64N/A
lift-*.f64N/A
lift-log.f64N/A
distribute-rgt-out--N/A
distribute-lft-out--N/A
flip--N/A
lower-/.f64N/A
Applied rewrites15.3%
if 2.2499999999999999e-181 < y < 1.07999999999999994e-44Initial program 99.9%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
frac-2negN/A
mul-1-negN/A
*-commutativeN/A
mul-1-negN/A
*-commutativeN/A
times-fracN/A
metadata-evalN/A
lower-fma.f64N/A
lower-/.f6499.8
Applied rewrites99.8%
if 1.07999999999999994e-44 < y Initial program 100.0%
Taylor expanded in y around -inf
lower--.f64N/A
*-commutativeN/A
div-addN/A
associate-*r/N/A
+-commutativeN/A
lower-*.f64N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f64100.0
Applied rewrites100.0%
Final simplification31.8%
y_m = (fabs.f64 y)
(FPCore (x y_m)
:precision binary64
(let* ((t_0 (/ (* (- x y_m) (+ x y_m)) (+ (* x x) (* y_m y_m))))
(t_1 (* -1.0 (+ (* 0.0 (/ x y_m)) 1.0))))
(if (<= t_0 -0.5)
t_1
(if (<= t_0 2.0) (fma (pow (* (/ y_m x) -1.0) 2.0) -2.0 1.0) t_1))))y_m = fabs(y);
double code(double x, double y_m) {
double t_0 = ((x - y_m) * (x + y_m)) / ((x * x) + (y_m * y_m));
double t_1 = -1.0 * ((0.0 * (x / y_m)) + 1.0);
double tmp;
if (t_0 <= -0.5) {
tmp = t_1;
} else if (t_0 <= 2.0) {
tmp = fma(pow(((y_m / x) * -1.0), 2.0), -2.0, 1.0);
} else {
tmp = t_1;
}
return tmp;
}
y_m = abs(y) function code(x, y_m) t_0 = Float64(Float64(Float64(x - y_m) * Float64(x + y_m)) / Float64(Float64(x * x) + Float64(y_m * y_m))) t_1 = Float64(-1.0 * Float64(Float64(0.0 * Float64(x / y_m)) + 1.0)) tmp = 0.0 if (t_0 <= -0.5) tmp = t_1; elseif (t_0 <= 2.0) tmp = fma((Float64(Float64(y_m / x) * -1.0) ^ 2.0), -2.0, 1.0); else tmp = t_1; end return tmp end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := Block[{t$95$0 = N[(N[(N[(x - y$95$m), $MachinePrecision] * N[(x + y$95$m), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y$95$m * y$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(-1.0 * N[(N[(0.0 * N[(x / y$95$m), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -0.5], t$95$1, If[LessEqual[t$95$0, 2.0], N[(N[Power[N[(N[(y$95$m / x), $MachinePrecision] * -1.0), $MachinePrecision], 2.0], $MachinePrecision] * -2.0 + 1.0), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
y_m = \left|y\right|
\\
\begin{array}{l}
t_0 := \frac{\left(x - y\_m\right) \cdot \left(x + y\_m\right)}{x \cdot x + y\_m \cdot y\_m}\\
t_1 := -1 \cdot \left(0 \cdot \frac{x}{y\_m} + 1\right)\\
\mathbf{if}\;t\_0 \leq -0.5:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 2:\\
\;\;\;\;\mathsf{fma}\left({\left(\frac{y\_m}{x} \cdot -1\right)}^{2}, -2, 1\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < -0.5 or 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) Initial program 60.9%
Taylor expanded in y around -inf
lower--.f64N/A
*-commutativeN/A
div-addN/A
associate-*r/N/A
+-commutativeN/A
lower-*.f64N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f6488.1
Applied rewrites88.1%
if -0.5 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
pow2N/A
pow2N/A
frac-timesN/A
sqr-neg-revN/A
mul-1-negN/A
mul-1-negN/A
pow2N/A
lower-pow.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6498.3
Applied rewrites98.3%
Final simplification91.2%
y_m = (fabs.f64 y)
(FPCore (x y_m)
:precision binary64
(let* ((t_0 (* (log x) 2.0))
(t_1 (/ (* (- x y_m) (+ x y_m)) (+ (* x x) (* y_m y_m))))
(t_2 (* -1.0 (+ (* 0.0 (/ x y_m)) 1.0))))
(if (<= t_1 -0.5)
t_2
(if (<= t_1 2.0)
(fma
(exp
(/
(- (pow (* (log y_m) 2.0) 2.0) (pow t_0 2.0))
(fma (* -1.0 (log y_m)) -2.0 t_0)))
-2.0
1.0)
t_2))))y_m = fabs(y);
double code(double x, double y_m) {
double t_0 = log(x) * 2.0;
double t_1 = ((x - y_m) * (x + y_m)) / ((x * x) + (y_m * y_m));
double t_2 = -1.0 * ((0.0 * (x / y_m)) + 1.0);
double tmp;
if (t_1 <= -0.5) {
tmp = t_2;
} else if (t_1 <= 2.0) {
tmp = fma(exp(((pow((log(y_m) * 2.0), 2.0) - pow(t_0, 2.0)) / fma((-1.0 * log(y_m)), -2.0, t_0))), -2.0, 1.0);
} else {
tmp = t_2;
}
return tmp;
}
y_m = abs(y) function code(x, y_m) t_0 = Float64(log(x) * 2.0) t_1 = Float64(Float64(Float64(x - y_m) * Float64(x + y_m)) / Float64(Float64(x * x) + Float64(y_m * y_m))) t_2 = Float64(-1.0 * Float64(Float64(0.0 * Float64(x / y_m)) + 1.0)) tmp = 0.0 if (t_1 <= -0.5) tmp = t_2; elseif (t_1 <= 2.0) tmp = fma(exp(Float64(Float64((Float64(log(y_m) * 2.0) ^ 2.0) - (t_0 ^ 2.0)) / fma(Float64(-1.0 * log(y_m)), -2.0, t_0))), -2.0, 1.0); else tmp = t_2; end return tmp end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := Block[{t$95$0 = N[(N[Log[x], $MachinePrecision] * 2.0), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(x - y$95$m), $MachinePrecision] * N[(x + y$95$m), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y$95$m * y$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(-1.0 * N[(N[(0.0 * N[(x / y$95$m), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -0.5], t$95$2, If[LessEqual[t$95$1, 2.0], N[(N[Exp[N[(N[(N[Power[N[(N[Log[y$95$m], $MachinePrecision] * 2.0), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision] / N[(N[(-1.0 * N[Log[y$95$m], $MachinePrecision]), $MachinePrecision] * -2.0 + t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * -2.0 + 1.0), $MachinePrecision], t$95$2]]]]]
\begin{array}{l}
y_m = \left|y\right|
\\
\begin{array}{l}
t_0 := \log x \cdot 2\\
t_1 := \frac{\left(x - y\_m\right) \cdot \left(x + y\_m\right)}{x \cdot x + y\_m \cdot y\_m}\\
t_2 := -1 \cdot \left(0 \cdot \frac{x}{y\_m} + 1\right)\\
\mathbf{if}\;t\_1 \leq -0.5:\\
\;\;\;\;t\_2\\
\mathbf{elif}\;t\_1 \leq 2:\\
\;\;\;\;\mathsf{fma}\left(e^{\frac{{\left(\log y\_m \cdot 2\right)}^{2} - {t\_0}^{2}}{\mathsf{fma}\left(-1 \cdot \log y\_m, -2, t\_0\right)}}, -2, 1\right)\\
\mathbf{else}:\\
\;\;\;\;t\_2\\
\end{array}
\end{array}
if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < -0.5 or 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) Initial program 60.9%
Taylor expanded in y around -inf
lower--.f64N/A
*-commutativeN/A
div-addN/A
associate-*r/N/A
+-commutativeN/A
lower-*.f64N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f6488.1
Applied rewrites88.1%
if -0.5 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
pow2N/A
pow2N/A
frac-timesN/A
sqr-neg-revN/A
mul-1-negN/A
mul-1-negN/A
pow2N/A
lower-pow.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6498.3
Applied rewrites98.3%
lift-pow.f64N/A
lift-*.f64N/A
lift-/.f64N/A
*-commutativeN/A
pow2N/A
mul-1-negN/A
mul-1-negN/A
sqr-neg-revN/A
frac-timesN/A
pow2N/A
pow-to-expN/A
pow2N/A
pow-to-expN/A
div-expN/A
lower-exp.f64N/A
lower--.f64N/A
lower-*.f64N/A
lower-log.f64N/A
lower-*.f64N/A
lower-log.f6450.3
Applied rewrites50.3%
lift--.f64N/A
lift-*.f64N/A
lift-log.f64N/A
lift-*.f64N/A
lift-log.f64N/A
distribute-rgt-out--N/A
distribute-lft-out--N/A
flip--N/A
lower-/.f64N/A
Applied rewrites50.3%
Final simplification76.7%
y_m = (fabs.f64 y)
(FPCore (x y_m)
:precision binary64
(let* ((t_0 (/ (* (- x y_m) (+ x y_m)) (+ (* x x) (* y_m y_m))))
(t_1 (* -1.0 (+ (* 0.0 (/ x y_m)) 1.0))))
(if (<= t_0 -0.5)
t_1
(if (<= t_0 2.0)
(fma (exp (- (* (log y_m) 2.0) (* (log x) 2.0))) -2.0 1.0)
t_1))))y_m = fabs(y);
double code(double x, double y_m) {
double t_0 = ((x - y_m) * (x + y_m)) / ((x * x) + (y_m * y_m));
double t_1 = -1.0 * ((0.0 * (x / y_m)) + 1.0);
double tmp;
if (t_0 <= -0.5) {
tmp = t_1;
} else if (t_0 <= 2.0) {
tmp = fma(exp(((log(y_m) * 2.0) - (log(x) * 2.0))), -2.0, 1.0);
} else {
tmp = t_1;
}
return tmp;
}
y_m = abs(y) function code(x, y_m) t_0 = Float64(Float64(Float64(x - y_m) * Float64(x + y_m)) / Float64(Float64(x * x) + Float64(y_m * y_m))) t_1 = Float64(-1.0 * Float64(Float64(0.0 * Float64(x / y_m)) + 1.0)) tmp = 0.0 if (t_0 <= -0.5) tmp = t_1; elseif (t_0 <= 2.0) tmp = fma(exp(Float64(Float64(log(y_m) * 2.0) - Float64(log(x) * 2.0))), -2.0, 1.0); else tmp = t_1; end return tmp end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := Block[{t$95$0 = N[(N[(N[(x - y$95$m), $MachinePrecision] * N[(x + y$95$m), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y$95$m * y$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(-1.0 * N[(N[(0.0 * N[(x / y$95$m), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -0.5], t$95$1, If[LessEqual[t$95$0, 2.0], N[(N[Exp[N[(N[(N[Log[y$95$m], $MachinePrecision] * 2.0), $MachinePrecision] - N[(N[Log[x], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * -2.0 + 1.0), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
y_m = \left|y\right|
\\
\begin{array}{l}
t_0 := \frac{\left(x - y\_m\right) \cdot \left(x + y\_m\right)}{x \cdot x + y\_m \cdot y\_m}\\
t_1 := -1 \cdot \left(0 \cdot \frac{x}{y\_m} + 1\right)\\
\mathbf{if}\;t\_0 \leq -0.5:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 2:\\
\;\;\;\;\mathsf{fma}\left(e^{\log y\_m \cdot 2 - \log x \cdot 2}, -2, 1\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < -0.5 or 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) Initial program 60.9%
Taylor expanded in y around -inf
lower--.f64N/A
*-commutativeN/A
div-addN/A
associate-*r/N/A
+-commutativeN/A
lower-*.f64N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f6488.1
Applied rewrites88.1%
if -0.5 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
pow2N/A
pow2N/A
frac-timesN/A
sqr-neg-revN/A
mul-1-negN/A
mul-1-negN/A
pow2N/A
lower-pow.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6498.3
Applied rewrites98.3%
lift-pow.f64N/A
lift-*.f64N/A
lift-/.f64N/A
*-commutativeN/A
pow2N/A
mul-1-negN/A
mul-1-negN/A
sqr-neg-revN/A
frac-timesN/A
pow2N/A
pow-to-expN/A
pow2N/A
pow-to-expN/A
div-expN/A
lower-exp.f64N/A
lower--.f64N/A
lower-*.f64N/A
lower-log.f64N/A
lower-*.f64N/A
lower-log.f6450.3
Applied rewrites50.3%
Final simplification76.7%
y_m = (fabs.f64 y) (FPCore (x y_m) :precision binary64 (* -1.0 (+ (* 0.0 (/ x y_m)) 1.0)))
y_m = fabs(y);
double code(double x, double y_m) {
return -1.0 * ((0.0 * (x / y_m)) + 1.0);
}
y_m = private
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_m)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y_m
code = (-1.0d0) * ((0.0d0 * (x / y_m)) + 1.0d0)
end function
y_m = Math.abs(y);
public static double code(double x, double y_m) {
return -1.0 * ((0.0 * (x / y_m)) + 1.0);
}
y_m = math.fabs(y) def code(x, y_m): return -1.0 * ((0.0 * (x / y_m)) + 1.0)
y_m = abs(y) function code(x, y_m) return Float64(-1.0 * Float64(Float64(0.0 * Float64(x / y_m)) + 1.0)) end
y_m = abs(y); function tmp = code(x, y_m) tmp = -1.0 * ((0.0 * (x / y_m)) + 1.0); end
y_m = N[Abs[y], $MachinePrecision] code[x_, y$95$m_] := N[(-1.0 * N[(N[(0.0 * N[(x / y$95$m), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y_m = \left|y\right|
\\
-1 \cdot \left(0 \cdot \frac{x}{y\_m} + 1\right)
\end{array}
Initial program 72.6%
Taylor expanded in y around -inf
lower--.f64N/A
*-commutativeN/A
div-addN/A
associate-*r/N/A
+-commutativeN/A
lower-*.f64N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f6462.1
Applied rewrites62.1%
Final simplification62.1%
herbie shell --seed 2025065
(FPCore (x y)
:name "Kahan p9 Example"
:precision binary64
:pre (and (and (< 0.0 x) (< x 1.0)) (< y 1.0))
:alt
(! :herbie-platform c (if (< 1/2 (fabs (/ x y)) 2) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1 (/ 2 (+ 1 (* (/ x y) (/ x y)))))))
(/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))