bug323 (missed optimization)

?

Percentage Accurate: 7.1% → 10.7%
Time: 14.4s
Precision: binary64
Cost: 97664

?

\[0 \leq x \land x \leq 0.5\]
\[\cos^{-1} \left(1 - x\right) \]
\[\begin{array}{l} t_0 := \sin^{-1} \left(1 - x\right)\\ t_1 := \mathsf{fma}\left(\pi, 0.5, t_0\right)\\ \mathsf{fma}\left(\frac{\pi}{\sqrt{t_1}}, 0.25 \cdot \left(\pi \cdot \sqrt{\frac{1}{t_0 + \pi \cdot 0.5}}\right), \frac{-{t_0}^{2}}{t_1}\right) \end{array} \]
(FPCore (x) :precision binary64 (acos (- 1.0 x)))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (asin (- 1.0 x))) (t_1 (fma PI 0.5 t_0)))
   (fma
    (/ PI (sqrt t_1))
    (* 0.25 (* PI (sqrt (/ 1.0 (+ t_0 (* PI 0.5))))))
    (/ (- (pow t_0 2.0)) t_1))))
double code(double x) {
	return acos((1.0 - x));
}
double code(double x) {
	double t_0 = asin((1.0 - x));
	double t_1 = fma(((double) M_PI), 0.5, t_0);
	return fma((((double) M_PI) / sqrt(t_1)), (0.25 * (((double) M_PI) * sqrt((1.0 / (t_0 + (((double) M_PI) * 0.5)))))), (-pow(t_0, 2.0) / t_1));
}
function code(x)
	return acos(Float64(1.0 - x))
end
function code(x)
	t_0 = asin(Float64(1.0 - x))
	t_1 = fma(pi, 0.5, t_0)
	return fma(Float64(pi / sqrt(t_1)), Float64(0.25 * Float64(pi * sqrt(Float64(1.0 / Float64(t_0 + Float64(pi * 0.5)))))), Float64(Float64(-(t_0 ^ 2.0)) / t_1))
end
code[x_] := N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]
code[x_] := Block[{t$95$0 = N[ArcSin[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(Pi * 0.5 + t$95$0), $MachinePrecision]}, N[(N[(Pi / N[Sqrt[t$95$1], $MachinePrecision]), $MachinePrecision] * N[(0.25 * N[(Pi * N[Sqrt[N[(1.0 / N[(t$95$0 + N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[((-N[Power[t$95$0, 2.0], $MachinePrecision]) / t$95$1), $MachinePrecision]), $MachinePrecision]]]
\cos^{-1} \left(1 - x\right)
\begin{array}{l}
t_0 := \sin^{-1} \left(1 - x\right)\\
t_1 := \mathsf{fma}\left(\pi, 0.5, t_0\right)\\
\mathsf{fma}\left(\frac{\pi}{\sqrt{t_1}}, 0.25 \cdot \left(\pi \cdot \sqrt{\frac{1}{t_0 + \pi \cdot 0.5}}\right), \frac{-{t_0}^{2}}{t_1}\right)
\end{array}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 9 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Target

Original7.1%
Target100.0%
Herbie10.7%
\[2 \cdot \sin^{-1} \left(\sqrt{\frac{x}{2}}\right) \]

Derivation?

  1. Initial program 8.3%

    \[\cos^{-1} \left(1 - x\right) \]
  2. Applied egg-rr8.2%

    \[\leadsto \color{blue}{\frac{\left(\pi \cdot 0.5\right) \cdot \left(\pi \cdot 0.5\right) - \sin^{-1} \left(1 - x\right) \cdot \sin^{-1} \left(1 - x\right)}{\pi \cdot 0.5 + \sin^{-1} \left(1 - x\right)}} \]
  3. Applied egg-rr11.6%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\pi}{\sqrt{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(1 - x\right)\right)}}, \frac{0.25 \cdot \pi}{\sqrt{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(1 - x\right)\right)}}, -\frac{{\sin^{-1} \left(1 - x\right)}^{2}}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(1 - x\right)\right)}\right)} \]
  4. Simplified11.6%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\pi}{\sqrt{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(1 - x\right)\right)}}, \frac{0.25 \cdot \pi}{\sqrt{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(1 - x\right)\right)}}, \frac{-{\sin^{-1} \left(1 - x\right)}^{2}}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(1 - x\right)\right)}\right)} \]
  5. Taylor expanded in x around 0 11.7%

    \[\leadsto \mathsf{fma}\left(\frac{\pi}{\sqrt{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(1 - x\right)\right)}}, \color{blue}{0.25 \cdot \left(\sqrt{\frac{1}{\sin^{-1} \left(1 - x\right) + 0.5 \cdot \pi}} \cdot \pi\right)}, \frac{-{\sin^{-1} \left(1 - x\right)}^{2}}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(1 - x\right)\right)}\right) \]
  6. Final simplification11.7%

    \[\leadsto \mathsf{fma}\left(\frac{\pi}{\sqrt{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(1 - x\right)\right)}}, 0.25 \cdot \left(\pi \cdot \sqrt{\frac{1}{\sin^{-1} \left(1 - x\right) + \pi \cdot 0.5}}\right), \frac{-{\sin^{-1} \left(1 - x\right)}^{2}}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(1 - x\right)\right)}\right) \]

Alternatives

Alternative 1
Accuracy10.6%
Cost78336
\[\begin{array}{l} t_0 := \sin^{-1} \left(1 - x\right)\\ \frac{\left(\pi \cdot 0.5\right) \cdot \left(\pi \cdot 0.5\right) - t_0 \cdot \mathsf{fma}\left({\left(\sqrt[3]{\pi}\right)}^{2}, 0.5 \cdot \sqrt[3]{\pi}, -\cos^{-1} \left(1 - x\right)\right)}{t_0 + \pi \cdot 0.5} \end{array} \]
Alternative 2
Accuracy10.6%
Cost39744
\[\begin{array}{l} t_0 := \cos^{-1} \left(1 - x\right)\\ t_1 := \mathsf{hypot}\left(1, 1 + t_0\right)\\ \frac{1 - \mathsf{fma}\left(t_1, t_1, -1\right)}{-2 - t_0} \end{array} \]
Alternative 3
Accuracy10.6%
Cost32576
\[\sqrt{0.5 \cdot {\pi}^{2}} \cdot \sqrt{0.5} - \sin^{-1} \left(1 - x\right) \]
Alternative 4
Accuracy10.6%
Cost32384
\[\mathsf{fma}\left({\left(\sqrt{0.5}\right)}^{2}, \pi, -\sin^{-1} \left(1 - x\right)\right) \]
Alternative 5
Accuracy10.6%
Cost26048
\[\pi \cdot {\left(\sqrt{0.5}\right)}^{2} - \sin^{-1} \left(1 - x\right) \]
Alternative 6
Accuracy9.7%
Cost13316
\[\begin{array}{l} t_0 := \cos^{-1} \left(1 - x\right)\\ \mathbf{if}\;x \leq 5.6 \cdot 10^{-17}:\\ \;\;\;\;\pi - t_0\\ \mathbf{else}:\\ \;\;\;\;{\left(\frac{1}{t_0}\right)}^{-1}\\ \end{array} \]
Alternative 7
Accuracy9.7%
Cost13188
\[\begin{array}{l} t_0 := \cos^{-1} \left(1 - x\right)\\ \mathbf{if}\;x \leq 5.6 \cdot 10^{-17}:\\ \;\;\;\;\pi - t_0\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Accuracy7.1%
Cost6592
\[\cos^{-1} \left(1 - x\right) \]

Reproduce?

herbie shell --seed 2023161 
(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)))