
(FPCore (x) :precision binary64 (acos (- 1.0 x)))
double code(double x) {
return acos((1.0 - x));
}
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 = acos((1.0d0 - x))
end function
public static double code(double x) {
return Math.acos((1.0 - x));
}
def code(x): return math.acos((1.0 - x))
function code(x) return acos(Float64(1.0 - x)) end
function tmp = code(x) tmp = acos((1.0 - x)); end
code[x_] := N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\cos^{-1} \left(1 - x\right)
\end{array}
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (acos (- 1.0 x)))
double code(double x) {
return acos((1.0 - x));
}
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 = acos((1.0d0 - x))
end function
public static double code(double x) {
return Math.acos((1.0 - x));
}
def code(x): return math.acos((1.0 - x))
function code(x) return acos(Float64(1.0 - x)) end
function tmp = code(x) tmp = acos((1.0 - x)); end
code[x_] := N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\cos^{-1} \left(1 - x\right)
\end{array}
(FPCore (x) :precision binary64 (fma (exp (- PI)) (* (exp PI) PI) (- (acos (- x 1.0)))))
double code(double x) {
return fma(exp(-((double) M_PI)), (exp(((double) M_PI)) * ((double) M_PI)), -acos((x - 1.0)));
}
function code(x) return fma(exp(Float64(-pi)), Float64(exp(pi) * pi), Float64(-acos(Float64(x - 1.0)))) end
code[x_] := N[(N[Exp[(-Pi)], $MachinePrecision] * N[(N[Exp[Pi], $MachinePrecision] * Pi), $MachinePrecision] + (-N[ArcCos[N[(x - 1.0), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(e^{-\pi}, e^{\pi} \cdot \pi, -\cos^{-1} \left(x - 1\right)\right)
\end{array}
Initial program 6.8%
Applied rewrites6.8%
Applied rewrites6.8%
Applied rewrites9.0%
Applied rewrites10.4%
(FPCore (x) :precision binary64 (if (<= x 4.5e-17) (acos (- x)) (fma (* PI x) (/ 1.0 x) (- (acos (- 1.0 x)) PI))))
double code(double x) {
double tmp;
if (x <= 4.5e-17) {
tmp = acos(-x);
} else {
tmp = fma((((double) M_PI) * x), (1.0 / x), (acos((1.0 - x)) - ((double) M_PI)));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 4.5e-17) tmp = acos(Float64(-x)); else tmp = fma(Float64(pi * x), Float64(1.0 / x), Float64(acos(Float64(1.0 - x)) - pi)); end return tmp end
code[x_] := If[LessEqual[x, 4.5e-17], N[ArcCos[(-x)], $MachinePrecision], N[(N[(Pi * x), $MachinePrecision] * N[(1.0 / x), $MachinePrecision] + N[(N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision] - Pi), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.5 \cdot 10^{-17}:\\
\;\;\;\;\cos^{-1} \left(-x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\pi \cdot x, \frac{1}{x}, \cos^{-1} \left(1 - x\right) - \pi\right)\\
\end{array}
\end{array}
if x < 4.49999999999999978e-17Initial program 6.8%
Taylor expanded in x around inf
Applied rewrites7.0%
Applied rewrites7.0%
if 4.49999999999999978e-17 < x Initial program 6.8%
Applied rewrites6.8%
Applied rewrites6.8%
Applied rewrites7.7%
Applied rewrites7.7%
(FPCore (x) :precision binary64 (if (<= x 4.5e-17) (acos (- x)) (fma (* PI x) (/ 1.0 x) (- (acos (- x 1.0))))))
double code(double x) {
double tmp;
if (x <= 4.5e-17) {
tmp = acos(-x);
} else {
tmp = fma((((double) M_PI) * x), (1.0 / x), -acos((x - 1.0)));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 4.5e-17) tmp = acos(Float64(-x)); else tmp = fma(Float64(pi * x), Float64(1.0 / x), Float64(-acos(Float64(x - 1.0)))); end return tmp end
code[x_] := If[LessEqual[x, 4.5e-17], N[ArcCos[(-x)], $MachinePrecision], N[(N[(Pi * x), $MachinePrecision] * N[(1.0 / x), $MachinePrecision] + (-N[ArcCos[N[(x - 1.0), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.5 \cdot 10^{-17}:\\
\;\;\;\;\cos^{-1} \left(-x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\pi \cdot x, \frac{1}{x}, -\cos^{-1} \left(x - 1\right)\right)\\
\end{array}
\end{array}
if x < 4.49999999999999978e-17Initial program 6.8%
Taylor expanded in x around inf
Applied rewrites7.0%
Applied rewrites7.0%
if 4.49999999999999978e-17 < x Initial program 6.8%
Applied rewrites6.8%
Applied rewrites6.8%
Applied rewrites7.7%
(FPCore (x) :precision binary64 (if (<= x 3.5e-19) (acos (- x)) (fma (/ PI (+ x x)) x (asin (- x 1.0)))))
double code(double x) {
double tmp;
if (x <= 3.5e-19) {
tmp = acos(-x);
} else {
tmp = fma((((double) M_PI) / (x + x)), x, asin((x - 1.0)));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 3.5e-19) tmp = acos(Float64(-x)); else tmp = fma(Float64(pi / Float64(x + x)), x, asin(Float64(x - 1.0))); end return tmp end
code[x_] := If[LessEqual[x, 3.5e-19], N[ArcCos[(-x)], $MachinePrecision], N[(N[(Pi / N[(x + x), $MachinePrecision]), $MachinePrecision] * x + N[ArcSin[N[(x - 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.5 \cdot 10^{-19}:\\
\;\;\;\;\cos^{-1} \left(-x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\pi}{x + x}, x, \sin^{-1} \left(x - 1\right)\right)\\
\end{array}
\end{array}
if x < 3.50000000000000015e-19Initial program 6.8%
Taylor expanded in x around inf
Applied rewrites7.0%
Applied rewrites7.0%
if 3.50000000000000015e-19 < x Initial program 6.8%
Applied rewrites6.8%
Applied rewrites6.8%
Applied rewrites7.7%
(FPCore (x) :precision binary64 (if (<= x 5.6e-17) (acos (- x)) (fma PI 0.5 (asin (- x 1.0)))))
double code(double x) {
double tmp;
if (x <= 5.6e-17) {
tmp = acos(-x);
} else {
tmp = fma(((double) M_PI), 0.5, asin((x - 1.0)));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 5.6e-17) tmp = acos(Float64(-x)); else tmp = fma(pi, 0.5, asin(Float64(x - 1.0))); end return tmp end
code[x_] := If[LessEqual[x, 5.6e-17], N[ArcCos[(-x)], $MachinePrecision], N[(Pi * 0.5 + N[ArcSin[N[(x - 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.6 \cdot 10^{-17}:\\
\;\;\;\;\cos^{-1} \left(-x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(x - 1\right)\right)\\
\end{array}
\end{array}
if x < 5.5999999999999998e-17Initial program 6.8%
Taylor expanded in x around inf
Applied rewrites7.0%
Applied rewrites7.0%
if 5.5999999999999998e-17 < x Initial program 6.8%
Applied rewrites6.8%
Taylor expanded in x around 0
Applied rewrites6.8%
Applied rewrites6.8%
(FPCore (x) :precision binary64 (if (<= x 5.6e-17) (acos (- x)) (acos (- 1.0 x))))
double code(double x) {
double tmp;
if (x <= 5.6e-17) {
tmp = acos(-x);
} else {
tmp = acos((1.0 - x));
}
return tmp;
}
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
real(8) :: tmp
if (x <= 5.6d-17) then
tmp = acos(-x)
else
tmp = acos((1.0d0 - x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 5.6e-17) {
tmp = Math.acos(-x);
} else {
tmp = Math.acos((1.0 - x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 5.6e-17: tmp = math.acos(-x) else: tmp = math.acos((1.0 - x)) return tmp
function code(x) tmp = 0.0 if (x <= 5.6e-17) tmp = acos(Float64(-x)); else tmp = acos(Float64(1.0 - x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 5.6e-17) tmp = acos(-x); else tmp = acos((1.0 - x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 5.6e-17], N[ArcCos[(-x)], $MachinePrecision], N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.6 \cdot 10^{-17}:\\
\;\;\;\;\cos^{-1} \left(-x\right)\\
\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(1 - x\right)\\
\end{array}
\end{array}
if x < 5.5999999999999998e-17Initial program 6.8%
Taylor expanded in x around inf
Applied rewrites7.0%
Applied rewrites7.0%
if 5.5999999999999998e-17 < x Initial program 6.8%
(FPCore (x) :precision binary64 (fma (* PI x) (/ 1.0 x) (- (acos (* (/ (- x 1.0) x) x)))))
double code(double x) {
return fma((((double) M_PI) * x), (1.0 / x), -acos((((x - 1.0) / x) * x)));
}
function code(x) return fma(Float64(pi * x), Float64(1.0 / x), Float64(-acos(Float64(Float64(Float64(x - 1.0) / x) * x)))) end
code[x_] := N[(N[(Pi * x), $MachinePrecision] * N[(1.0 / x), $MachinePrecision] + (-N[ArcCos[N[(N[(N[(x - 1.0), $MachinePrecision] / x), $MachinePrecision] * x), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\pi \cdot x, \frac{1}{x}, -\cos^{-1} \left(\frac{x - 1}{x} \cdot x\right)\right)
\end{array}
Initial program 6.8%
Applied rewrites6.8%
Applied rewrites6.8%
Applied rewrites7.7%
Applied rewrites8.4%
(FPCore (x) :precision binary64 (acos (- x)))
double code(double x) {
return acos(-x);
}
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 = acos(-x)
end function
public static double code(double x) {
return Math.acos(-x);
}
def code(x): return math.acos(-x)
function code(x) return acos(Float64(-x)) end
function tmp = code(x) tmp = acos(-x); end
code[x_] := N[ArcCos[(-x)], $MachinePrecision]
\begin{array}{l}
\\
\cos^{-1} \left(-x\right)
\end{array}
Initial program 6.8%
Taylor expanded in x around inf
Applied rewrites7.0%
Applied rewrites7.0%
(FPCore (x) :precision binary64 (acos 1.0))
double code(double x) {
return acos(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 = acos(1.0d0)
end function
public static double code(double x) {
return Math.acos(1.0);
}
def code(x): return math.acos(1.0)
function code(x) return acos(1.0) end
function tmp = code(x) tmp = acos(1.0); end
code[x_] := N[ArcCos[1.0], $MachinePrecision]
\begin{array}{l}
\\
\cos^{-1} 1
\end{array}
Initial program 6.8%
Taylor expanded in x around 0
Applied rewrites3.8%
herbie shell --seed 2025153
(FPCore (x)
:name "bug323 (missed optimization)"
:precision binary64
:pre (and (<= 0.0 x) (<= x 0.5))
(acos (- 1.0 x)))