
(FPCore (x) :precision binary64 (acos (- 1.0 x)))
double code(double x) {
return acos((1.0 - x));
}
real(8) function code(x)
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}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (acos (- 1.0 x)))
double code(double x) {
return acos((1.0 - x));
}
real(8) function code(x)
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 (let* ((t_0 (asin (- 1.0 x))) (t_1 (sqrt t_0)) (t_2 (fma (- t_1) t_1 t_0))) (+ t_2 (+ (acos (- 1.0 x)) t_2))))
double code(double x) {
double t_0 = asin((1.0 - x));
double t_1 = sqrt(t_0);
double t_2 = fma(-t_1, t_1, t_0);
return t_2 + (acos((1.0 - x)) + t_2);
}
function code(x) t_0 = asin(Float64(1.0 - x)) t_1 = sqrt(t_0) t_2 = fma(Float64(-t_1), t_1, t_0) return Float64(t_2 + Float64(acos(Float64(1.0 - x)) + t_2)) end
code[x_] := Block[{t$95$0 = N[ArcSin[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Sqrt[t$95$0], $MachinePrecision]}, Block[{t$95$2 = N[((-t$95$1) * t$95$1 + t$95$0), $MachinePrecision]}, N[(t$95$2 + N[(N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision] + t$95$2), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin^{-1} \left(1 - x\right)\\
t_1 := \sqrt{t\_0}\\
t_2 := \mathsf{fma}\left(-t\_1, t\_1, t\_0\right)\\
t\_2 + \left(\cos^{-1} \left(1 - x\right) + t\_2\right)
\end{array}
\end{array}
Initial program 7.4%
acos-asin7.4%
*-un-lft-identity7.4%
add-sqr-sqrt10.6%
prod-diff10.6%
add-sqr-sqrt10.6%
fma-neg10.6%
*-un-lft-identity10.6%
acos-asin10.6%
add-sqr-sqrt10.6%
Applied egg-rr10.6%
acos-asin7.4%
*-un-lft-identity7.4%
add-sqr-sqrt10.6%
prod-diff10.6%
add-sqr-sqrt10.6%
fma-neg10.6%
*-un-lft-identity10.6%
acos-asin10.6%
add-sqr-sqrt10.6%
Applied egg-rr10.6%
Final simplification10.6%
(FPCore (x) :precision binary64 (fma (* (sqrt PI) (sqrt 0.5)) (sqrt (* PI 0.5)) (- (asin (- 1.0 x)))))
double code(double x) {
return fma((sqrt(((double) M_PI)) * sqrt(0.5)), sqrt((((double) M_PI) * 0.5)), -asin((1.0 - x)));
}
function code(x) return fma(Float64(sqrt(pi) * sqrt(0.5)), sqrt(Float64(pi * 0.5)), Float64(-asin(Float64(1.0 - x)))) end
code[x_] := N[(N[(N[Sqrt[Pi], $MachinePrecision] * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision] * N[Sqrt[N[(Pi * 0.5), $MachinePrecision]], $MachinePrecision] + (-N[ArcSin[N[(1.0 - x), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sqrt{\pi} \cdot \sqrt{0.5}, \sqrt{\pi \cdot 0.5}, -\sin^{-1} \left(1 - x\right)\right)
\end{array}
Initial program 7.4%
acos-asin7.4%
add-sqr-sqrt5.6%
fma-neg5.6%
div-inv5.6%
metadata-eval5.6%
div-inv5.6%
metadata-eval5.6%
Applied egg-rr5.6%
sqrt-prod10.6%
Applied egg-rr10.6%
(FPCore (x) :precision binary64 (+ (* PI (pow (sqrt 0.5) 2.0)) (- (acos (- 1.0 x)) (* PI 0.5))))
double code(double x) {
return (((double) M_PI) * pow(sqrt(0.5), 2.0)) + (acos((1.0 - x)) - (((double) M_PI) * 0.5));
}
public static double code(double x) {
return (Math.PI * Math.pow(Math.sqrt(0.5), 2.0)) + (Math.acos((1.0 - x)) - (Math.PI * 0.5));
}
def code(x): return (math.pi * math.pow(math.sqrt(0.5), 2.0)) + (math.acos((1.0 - x)) - (math.pi * 0.5))
function code(x) return Float64(Float64(pi * (sqrt(0.5) ^ 2.0)) + Float64(acos(Float64(1.0 - x)) - Float64(pi * 0.5))) end
function tmp = code(x) tmp = (pi * (sqrt(0.5) ^ 2.0)) + (acos((1.0 - x)) - (pi * 0.5)); end
code[x_] := N[(N[(Pi * N[Power[N[Sqrt[0.5], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision] + N[(N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision] - N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\pi \cdot {\left(\sqrt{0.5}\right)}^{2} + \left(\cos^{-1} \left(1 - x\right) - \pi \cdot 0.5\right)
\end{array}
Initial program 7.4%
acos-asin7.4%
add-sqr-sqrt5.6%
fma-neg5.6%
div-inv5.6%
metadata-eval5.6%
div-inv5.6%
metadata-eval5.6%
Applied egg-rr5.6%
Taylor expanded in x around 0 10.6%
asin-acos10.6%
div-inv10.6%
metadata-eval10.6%
Applied egg-rr10.6%
Final simplification10.6%
(FPCore (x) :precision binary64 (if (<= x 5.5e-17) (acos x) (cbrt (pow (acos (- 1.0 x)) 3.0))))
double code(double x) {
double tmp;
if (x <= 5.5e-17) {
tmp = acos(x);
} else {
tmp = cbrt(pow(acos((1.0 - x)), 3.0));
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= 5.5e-17) {
tmp = Math.acos(x);
} else {
tmp = Math.cbrt(Math.pow(Math.acos((1.0 - x)), 3.0));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 5.5e-17) tmp = acos(x); else tmp = cbrt((acos(Float64(1.0 - x)) ^ 3.0)); end return tmp end
code[x_] := If[LessEqual[x, 5.5e-17], N[ArcCos[x], $MachinePrecision], N[Power[N[Power[N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision], 3.0], $MachinePrecision], 1/3], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.5 \cdot 10^{-17}:\\
\;\;\;\;\cos^{-1} x\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{{\cos^{-1} \left(1 - x\right)}^{3}}\\
\end{array}
\end{array}
if x < 5.50000000000000001e-17Initial program 3.9%
Taylor expanded in x around inf 6.5%
neg-mul-16.5%
Simplified6.5%
*-un-lft-identity6.5%
*-commutative6.5%
add-sqr-sqrt0.0%
sqrt-unprod6.5%
sqr-neg6.5%
sqrt-unprod6.5%
add-sqr-sqrt6.5%
Applied egg-rr6.5%
*-rgt-identity6.5%
Simplified6.5%
if 5.50000000000000001e-17 < x Initial program 60.1%
add-cbrt-cube60.1%
pow360.2%
Applied egg-rr60.2%
(FPCore (x) :precision binary64 (- (* PI (pow (sqrt 0.5) 2.0)) (asin (- 1.0 x))))
double code(double x) {
return (((double) M_PI) * pow(sqrt(0.5), 2.0)) - asin((1.0 - x));
}
public static double code(double x) {
return (Math.PI * Math.pow(Math.sqrt(0.5), 2.0)) - Math.asin((1.0 - x));
}
def code(x): return (math.pi * math.pow(math.sqrt(0.5), 2.0)) - math.asin((1.0 - x))
function code(x) return Float64(Float64(pi * (sqrt(0.5) ^ 2.0)) - asin(Float64(1.0 - x))) end
function tmp = code(x) tmp = (pi * (sqrt(0.5) ^ 2.0)) - asin((1.0 - x)); end
code[x_] := N[(N[(Pi * N[Power[N[Sqrt[0.5], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision] - N[ArcSin[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\pi \cdot {\left(\sqrt{0.5}\right)}^{2} - \sin^{-1} \left(1 - x\right)
\end{array}
Initial program 7.4%
acos-asin7.4%
add-sqr-sqrt5.6%
fma-neg5.6%
div-inv5.6%
metadata-eval5.6%
div-inv5.6%
metadata-eval5.6%
Applied egg-rr5.6%
Taylor expanded in x around 0 10.6%
(FPCore (x) :precision binary64 (- (cbrt (pow (* PI 0.5) 3.0)) (asin (- 1.0 x))))
double code(double x) {
return cbrt(pow((((double) M_PI) * 0.5), 3.0)) - asin((1.0 - x));
}
public static double code(double x) {
return Math.cbrt(Math.pow((Math.PI * 0.5), 3.0)) - Math.asin((1.0 - x));
}
function code(x) return Float64(cbrt((Float64(pi * 0.5) ^ 3.0)) - asin(Float64(1.0 - x))) end
code[x_] := N[(N[Power[N[Power[N[(Pi * 0.5), $MachinePrecision], 3.0], $MachinePrecision], 1/3], $MachinePrecision] - N[ArcSin[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{{\left(\pi \cdot 0.5\right)}^{3}} - \sin^{-1} \left(1 - x\right)
\end{array}
Initial program 7.4%
acos-asin7.4%
add-sqr-sqrt5.6%
fma-neg5.6%
div-inv5.6%
metadata-eval5.6%
div-inv5.6%
metadata-eval5.6%
Applied egg-rr5.6%
Taylor expanded in x around 0 10.6%
add-cbrt-cube10.5%
pow310.5%
sqrt-pow210.6%
metadata-eval10.6%
metadata-eval10.6%
Applied egg-rr10.6%
(FPCore (x) :precision binary64 (if (<= x 5.5e-17) (acos x) (acos (- 1.0 x))))
double code(double x) {
double tmp;
if (x <= 5.5e-17) {
tmp = acos(x);
} else {
tmp = acos((1.0 - x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 5.5d-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.5e-17) {
tmp = Math.acos(x);
} else {
tmp = Math.acos((1.0 - x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 5.5e-17: tmp = math.acos(x) else: tmp = math.acos((1.0 - x)) return tmp
function code(x) tmp = 0.0 if (x <= 5.5e-17) tmp = acos(x); else tmp = acos(Float64(1.0 - x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 5.5e-17) tmp = acos(x); else tmp = acos((1.0 - x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 5.5e-17], N[ArcCos[x], $MachinePrecision], N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.5 \cdot 10^{-17}:\\
\;\;\;\;\cos^{-1} x\\
\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(1 - x\right)\\
\end{array}
\end{array}
if x < 5.50000000000000001e-17Initial program 3.9%
Taylor expanded in x around inf 6.5%
neg-mul-16.5%
Simplified6.5%
*-un-lft-identity6.5%
*-commutative6.5%
add-sqr-sqrt0.0%
sqrt-unprod6.5%
sqr-neg6.5%
sqrt-unprod6.5%
add-sqr-sqrt6.5%
Applied egg-rr6.5%
*-rgt-identity6.5%
Simplified6.5%
if 5.50000000000000001e-17 < x Initial program 60.1%
(FPCore (x) :precision binary64 (acos x))
double code(double x) {
return acos(x);
}
real(8) function code(x)
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(x) end
function tmp = code(x) tmp = acos(x); end
code[x_] := N[ArcCos[x], $MachinePrecision]
\begin{array}{l}
\\
\cos^{-1} x
\end{array}
Initial program 7.4%
Taylor expanded in x around inf 6.9%
neg-mul-16.9%
Simplified6.9%
*-un-lft-identity6.9%
*-commutative6.9%
add-sqr-sqrt0.0%
sqrt-unprod6.9%
sqr-neg6.9%
sqrt-unprod6.9%
add-sqr-sqrt6.9%
Applied egg-rr6.9%
*-rgt-identity6.9%
Simplified6.9%
(FPCore (x) :precision binary64 (acos 1.0))
double code(double x) {
return acos(1.0);
}
real(8) function code(x)
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 7.4%
Taylor expanded in x around 0 3.8%
(FPCore (x) :precision binary64 (* 2.0 (asin (sqrt (/ x 2.0)))))
double code(double x) {
return 2.0 * asin(sqrt((x / 2.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 * asin(sqrt((x / 2.0d0)))
end function
public static double code(double x) {
return 2.0 * Math.asin(Math.sqrt((x / 2.0)));
}
def code(x): return 2.0 * math.asin(math.sqrt((x / 2.0)))
function code(x) return Float64(2.0 * asin(sqrt(Float64(x / 2.0)))) end
function tmp = code(x) tmp = 2.0 * asin(sqrt((x / 2.0))); end
code[x_] := N[(2.0 * N[ArcSin[N[Sqrt[N[(x / 2.0), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \sin^{-1} \left(\sqrt{\frac{x}{2}}\right)
\end{array}
herbie shell --seed 2024116
(FPCore (x)
:name "bug323 (missed optimization)"
:precision binary64
:pre (and (<= 0.0 x) (<= x 0.5))
:alt
(! :herbie-platform default (* 2 (asin (sqrt (/ x 2)))))
(acos (- 1.0 x)))