
(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 5 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 (fma (pow (pow (* PI 0.5) 0.3333333333333333) 2.0) (cbrt (* PI 0.5)) (- (asin (- 1.0 x)))))
double code(double x) {
return fma(pow(pow((((double) M_PI) * 0.5), 0.3333333333333333), 2.0), cbrt((((double) M_PI) * 0.5)), -asin((1.0 - x)));
}
function code(x) return fma(((Float64(pi * 0.5) ^ 0.3333333333333333) ^ 2.0), cbrt(Float64(pi * 0.5)), Float64(-asin(Float64(1.0 - x)))) end
code[x_] := N[(N[Power[N[Power[N[(Pi * 0.5), $MachinePrecision], 0.3333333333333333], $MachinePrecision], 2.0], $MachinePrecision] * N[Power[N[(Pi * 0.5), $MachinePrecision], 1/3], $MachinePrecision] + (-N[ArcSin[N[(1.0 - x), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left({\left({\left(\pi \cdot 0.5\right)}^{0.3333333333333333}\right)}^{2}, \sqrt[3]{\pi \cdot 0.5}, -\sin^{-1} \left(1 - x\right)\right)
\end{array}
Initial program 8.3%
acos-asin8.3%
add-cube-cbrt6.5%
fma-neg6.5%
pow26.5%
div-inv6.5%
metadata-eval6.5%
div-inv6.5%
metadata-eval6.5%
Applied egg-rr6.5%
pow1/311.9%
Applied egg-rr11.9%
Final simplification11.9%
(FPCore (x) :precision binary64 (let* ((t_0 (cbrt (asin (- 1.0 x))))) (- (* PI 0.5) (* t_0 (pow t_0 2.0)))))
double code(double x) {
double t_0 = cbrt(asin((1.0 - x)));
return (((double) M_PI) * 0.5) - (t_0 * pow(t_0, 2.0));
}
public static double code(double x) {
double t_0 = Math.cbrt(Math.asin((1.0 - x)));
return (Math.PI * 0.5) - (t_0 * Math.pow(t_0, 2.0));
}
function code(x) t_0 = cbrt(asin(Float64(1.0 - x))) return Float64(Float64(pi * 0.5) - Float64(t_0 * (t_0 ^ 2.0))) end
code[x_] := Block[{t$95$0 = N[Power[N[ArcSin[N[(1.0 - x), $MachinePrecision]], $MachinePrecision], 1/3], $MachinePrecision]}, N[(N[(Pi * 0.5), $MachinePrecision] - N[(t$95$0 * N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\sin^{-1} \left(1 - x\right)}\\
\pi \cdot 0.5 - t_0 \cdot {t_0}^{2}
\end{array}
\end{array}
Initial program 8.3%
expm1-log1p-u8.3%
expm1-udef8.3%
log1p-udef8.3%
rem-exp-log8.3%
Applied egg-rr8.3%
add-exp-log8.3%
log1p-udef8.3%
expm1-udef8.3%
expm1-log1p-u8.3%
acos-asin8.3%
div-inv8.3%
metadata-eval8.3%
add-cbrt-cube6.5%
unpow26.5%
cbrt-prod11.8%
cancel-sign-sub-inv11.8%
unpow211.8%
cbrt-prod11.8%
pow211.8%
Applied egg-rr11.8%
Final simplification11.8%
(FPCore (x) :precision binary64 (if (<= x 5.5e-17) (+ (* PI 0.5) (asin (- 1.0 x))) (exp (log (acos (- 1.0 x))))))
double code(double x) {
double tmp;
if (x <= 5.5e-17) {
tmp = (((double) M_PI) * 0.5) + asin((1.0 - x));
} else {
tmp = exp(log(acos((1.0 - x))));
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= 5.5e-17) {
tmp = (Math.PI * 0.5) + Math.asin((1.0 - x));
} else {
tmp = Math.exp(Math.log(Math.acos((1.0 - x))));
}
return tmp;
}
def code(x): tmp = 0 if x <= 5.5e-17: tmp = (math.pi * 0.5) + math.asin((1.0 - x)) else: tmp = math.exp(math.log(math.acos((1.0 - x)))) return tmp
function code(x) tmp = 0.0 if (x <= 5.5e-17) tmp = Float64(Float64(pi * 0.5) + asin(Float64(1.0 - x))); else tmp = exp(log(acos(Float64(1.0 - x)))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 5.5e-17) tmp = (pi * 0.5) + asin((1.0 - x)); else tmp = exp(log(acos((1.0 - x)))); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 5.5e-17], N[(N[(Pi * 0.5), $MachinePrecision] + N[ArcSin[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Exp[N[Log[N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.5 \cdot 10^{-17}:\\
\;\;\;\;\pi \cdot 0.5 + \sin^{-1} \left(1 - x\right)\\
\mathbf{else}:\\
\;\;\;\;e^{\log \cos^{-1} \left(1 - x\right)}\\
\end{array}
\end{array}
if x < 5.50000000000000001e-17Initial program 3.8%
expm1-log1p-u3.8%
expm1-udef3.8%
log1p-udef3.8%
rem-exp-log3.8%
Applied egg-rr3.8%
add-exp-log3.8%
log1p-udef3.8%
expm1-udef3.8%
expm1-log1p-u3.8%
acos-asin3.8%
div-inv3.8%
metadata-eval3.8%
add-sqr-sqrt7.6%
cancel-sign-sub-inv7.6%
add-sqr-sqrt0.0%
sqrt-unprod6.7%
sqr-neg6.7%
add-sqr-sqrt6.7%
add-sqr-sqrt6.7%
Applied egg-rr6.7%
if 5.50000000000000001e-17 < x Initial program 81.1%
add-exp-log81.1%
Applied egg-rr81.1%
Final simplification11.0%
(FPCore (x) :precision binary64 (if (<= x 5.5e-17) (+ (* PI 0.5) (asin (- 1.0 x))) (acos (- 1.0 x))))
double code(double x) {
double tmp;
if (x <= 5.5e-17) {
tmp = (((double) M_PI) * 0.5) + asin((1.0 - x));
} else {
tmp = acos((1.0 - x));
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= 5.5e-17) {
tmp = (Math.PI * 0.5) + Math.asin((1.0 - x));
} else {
tmp = Math.acos((1.0 - x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 5.5e-17: tmp = (math.pi * 0.5) + math.asin((1.0 - x)) else: tmp = math.acos((1.0 - x)) return tmp
function code(x) tmp = 0.0 if (x <= 5.5e-17) tmp = Float64(Float64(pi * 0.5) + asin(Float64(1.0 - 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 = (pi * 0.5) + asin((1.0 - x)); else tmp = acos((1.0 - x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 5.5e-17], N[(N[(Pi * 0.5), $MachinePrecision] + N[ArcSin[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.5 \cdot 10^{-17}:\\
\;\;\;\;\pi \cdot 0.5 + \sin^{-1} \left(1 - x\right)\\
\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(1 - x\right)\\
\end{array}
\end{array}
if x < 5.50000000000000001e-17Initial program 3.8%
expm1-log1p-u3.8%
expm1-udef3.8%
log1p-udef3.8%
rem-exp-log3.8%
Applied egg-rr3.8%
add-exp-log3.8%
log1p-udef3.8%
expm1-udef3.8%
expm1-log1p-u3.8%
acos-asin3.8%
div-inv3.8%
metadata-eval3.8%
add-sqr-sqrt7.6%
cancel-sign-sub-inv7.6%
add-sqr-sqrt0.0%
sqrt-unprod6.7%
sqr-neg6.7%
add-sqr-sqrt6.7%
add-sqr-sqrt6.7%
Applied egg-rr6.7%
if 5.50000000000000001e-17 < x Initial program 81.1%
Final simplification11.0%
(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}
Initial program 8.3%
Final simplification8.3%
(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 2023315
(FPCore (x)
:name "bug323 (missed optimization)"
:precision binary64
:pre (and (<= 0.0 x) (<= x 0.5))
:herbie-target
(* 2.0 (asin (sqrt (/ x 2.0))))
(acos (- 1.0 x)))