
(FPCore (x) :precision binary64 (/ x (+ (* x x) 1.0)))
double code(double x) {
return x / ((x * x) + 1.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)
use fmin_fmax_functions
real(8), intent (in) :: x
code = x / ((x * x) + 1.0d0)
end function
public static double code(double x) {
return x / ((x * x) + 1.0);
}
def code(x): return x / ((x * x) + 1.0)
function code(x) return Float64(x / Float64(Float64(x * x) + 1.0)) end
function tmp = code(x) tmp = x / ((x * x) + 1.0); end
code[x_] := N[(x / N[(N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{x \cdot x + 1}
\end{array}
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (/ x (+ (* x x) 1.0)))
double code(double x) {
return x / ((x * x) + 1.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)
use fmin_fmax_functions
real(8), intent (in) :: x
code = x / ((x * x) + 1.0d0)
end function
public static double code(double x) {
return x / ((x * x) + 1.0);
}
def code(x): return x / ((x * x) + 1.0)
function code(x) return Float64(x / Float64(Float64(x * x) + 1.0)) end
function tmp = code(x) tmp = x / ((x * x) + 1.0); end
code[x_] := N[(x / N[(N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{x \cdot x + 1}
\end{array}
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
:precision binary64
(let* ((t_0 (pow (* x_m x_m) -1.0)))
(*
x_s
(if (<= x_m 2000.0)
(/
x_m
(/
(fma (pow (* (* x_m x_m) -1.0) 2.0) 1.0 -1.0)
(fma (* x_m x_m) 1.0 -1.0)))
(-
(/ (fma (pow t_0 2.0) 1.0 1.0) x_m)
(/ (fma t_0 1.0 (pow (pow (* x_m x_m) 3.0) -1.0)) x_m))))))x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
double t_0 = pow((x_m * x_m), -1.0);
double tmp;
if (x_m <= 2000.0) {
tmp = x_m / (fma(pow(((x_m * x_m) * -1.0), 2.0), 1.0, -1.0) / fma((x_m * x_m), 1.0, -1.0));
} else {
tmp = (fma(pow(t_0, 2.0), 1.0, 1.0) / x_m) - (fma(t_0, 1.0, pow(pow((x_m * x_m), 3.0), -1.0)) / x_m);
}
return x_s * tmp;
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) t_0 = Float64(x_m * x_m) ^ -1.0 tmp = 0.0 if (x_m <= 2000.0) tmp = Float64(x_m / Float64(fma((Float64(Float64(x_m * x_m) * -1.0) ^ 2.0), 1.0, -1.0) / fma(Float64(x_m * x_m), 1.0, -1.0))); else tmp = Float64(Float64(fma((t_0 ^ 2.0), 1.0, 1.0) / x_m) - Float64(fma(t_0, 1.0, ((Float64(x_m * x_m) ^ 3.0) ^ -1.0)) / x_m)); end return Float64(x_s * tmp) end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := Block[{t$95$0 = N[Power[N[(x$95$m * x$95$m), $MachinePrecision], -1.0], $MachinePrecision]}, N[(x$95$s * If[LessEqual[x$95$m, 2000.0], N[(x$95$m / N[(N[(N[Power[N[(N[(x$95$m * x$95$m), $MachinePrecision] * -1.0), $MachinePrecision], 2.0], $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / N[(N[(x$95$m * x$95$m), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[Power[t$95$0, 2.0], $MachinePrecision] * 1.0 + 1.0), $MachinePrecision] / x$95$m), $MachinePrecision] - N[(N[(t$95$0 * 1.0 + N[Power[N[Power[N[(x$95$m * x$95$m), $MachinePrecision], 3.0], $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision] / x$95$m), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
\begin{array}{l}
t_0 := {\left(x\_m \cdot x\_m\right)}^{-1}\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 2000:\\
\;\;\;\;\frac{x\_m}{\frac{\mathsf{fma}\left({\left(\left(x\_m \cdot x\_m\right) \cdot -1\right)}^{2}, 1, -1\right)}{\mathsf{fma}\left(x\_m \cdot x\_m, 1, -1\right)}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left({t\_0}^{2}, 1, 1\right)}{x\_m} - \frac{\mathsf{fma}\left(t\_0, 1, {\left({\left(x\_m \cdot x\_m\right)}^{3}\right)}^{-1}\right)}{x\_m}\\
\end{array}
\end{array}
\end{array}
if x < 2e3Initial program 100.0%
lift-+.f64N/A
lift-*.f64N/A
pow2N/A
flip-+N/A
lower-/.f64N/A
Applied rewrites100.0%
if 2e3 < x Initial program 53.6%
Taylor expanded in x around inf
div-subN/A
lower--.f64N/A
Applied rewrites100.0%
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
:precision binary64
(let* ((t_0 (pow (* x_m x_m) -1.0)))
(*
x_s
(if (<= x_m 100.0)
(/ x_m (+ (* x_m x_m) 1.0))
(-
(/ (fma (pow t_0 2.0) 1.0 1.0) x_m)
(/ (fma t_0 1.0 (pow (pow (* x_m x_m) 3.0) -1.0)) x_m))))))x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
double t_0 = pow((x_m * x_m), -1.0);
double tmp;
if (x_m <= 100.0) {
tmp = x_m / ((x_m * x_m) + 1.0);
} else {
tmp = (fma(pow(t_0, 2.0), 1.0, 1.0) / x_m) - (fma(t_0, 1.0, pow(pow((x_m * x_m), 3.0), -1.0)) / x_m);
}
return x_s * tmp;
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) t_0 = Float64(x_m * x_m) ^ -1.0 tmp = 0.0 if (x_m <= 100.0) tmp = Float64(x_m / Float64(Float64(x_m * x_m) + 1.0)); else tmp = Float64(Float64(fma((t_0 ^ 2.0), 1.0, 1.0) / x_m) - Float64(fma(t_0, 1.0, ((Float64(x_m * x_m) ^ 3.0) ^ -1.0)) / x_m)); end return Float64(x_s * tmp) end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := Block[{t$95$0 = N[Power[N[(x$95$m * x$95$m), $MachinePrecision], -1.0], $MachinePrecision]}, N[(x$95$s * If[LessEqual[x$95$m, 100.0], N[(x$95$m / N[(N[(x$95$m * x$95$m), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[Power[t$95$0, 2.0], $MachinePrecision] * 1.0 + 1.0), $MachinePrecision] / x$95$m), $MachinePrecision] - N[(N[(t$95$0 * 1.0 + N[Power[N[Power[N[(x$95$m * x$95$m), $MachinePrecision], 3.0], $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision] / x$95$m), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
\begin{array}{l}
t_0 := {\left(x\_m \cdot x\_m\right)}^{-1}\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 100:\\
\;\;\;\;\frac{x\_m}{x\_m \cdot x\_m + 1}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left({t\_0}^{2}, 1, 1\right)}{x\_m} - \frac{\mathsf{fma}\left(t\_0, 1, {\left({\left(x\_m \cdot x\_m\right)}^{3}\right)}^{-1}\right)}{x\_m}\\
\end{array}
\end{array}
\end{array}
if x < 100Initial program 100.0%
if 100 < x Initial program 53.9%
Taylor expanded in x around inf
div-subN/A
lower--.f64N/A
Applied rewrites100.0%
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
:precision binary64
(let* ((t_0 (pow (* x_m x_m) -1.0)))
(*
x_s
(-
(/ (fma (pow t_0 2.0) 1.0 1.0) x_m)
(/ (fma t_0 1.0 (pow (pow (* x_m x_m) 3.0) -1.0)) x_m)))))x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
double t_0 = pow((x_m * x_m), -1.0);
return x_s * ((fma(pow(t_0, 2.0), 1.0, 1.0) / x_m) - (fma(t_0, 1.0, pow(pow((x_m * x_m), 3.0), -1.0)) / x_m));
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) t_0 = Float64(x_m * x_m) ^ -1.0 return Float64(x_s * Float64(Float64(fma((t_0 ^ 2.0), 1.0, 1.0) / x_m) - Float64(fma(t_0, 1.0, ((Float64(x_m * x_m) ^ 3.0) ^ -1.0)) / x_m))) end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := Block[{t$95$0 = N[Power[N[(x$95$m * x$95$m), $MachinePrecision], -1.0], $MachinePrecision]}, N[(x$95$s * N[(N[(N[(N[Power[t$95$0, 2.0], $MachinePrecision] * 1.0 + 1.0), $MachinePrecision] / x$95$m), $MachinePrecision] - N[(N[(t$95$0 * 1.0 + N[Power[N[Power[N[(x$95$m * x$95$m), $MachinePrecision], 3.0], $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision] / x$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
\begin{array}{l}
t_0 := {\left(x\_m \cdot x\_m\right)}^{-1}\\
x\_s \cdot \left(\frac{\mathsf{fma}\left({t\_0}^{2}, 1, 1\right)}{x\_m} - \frac{\mathsf{fma}\left(t\_0, 1, {\left({\left(x\_m \cdot x\_m\right)}^{3}\right)}^{-1}\right)}{x\_m}\right)
\end{array}
\end{array}
Initial program 77.3%
Taylor expanded in x around inf
div-subN/A
lower--.f64N/A
Applied rewrites49.5%
herbie shell --seed 2025093
(FPCore (x)
:name "x / (x^2 + 1)"
:precision binary64
(/ x (+ (* x x) 1.0)))