bug323 (missed optimization)

Percentage Accurate: 7.2% → 10.7%
Time: 7.7s
Alternatives: 6
Speedup: 0.9×

Specification

?
\[0 \leq x \land x \leq 0.5\]
\[\begin{array}{l} \\ \cos^{-1} \left(1 - x\right) \end{array} \]
(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:

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.

Accuracy vs Speed?

Herbie found 6 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 7.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos^{-1} \left(1 - x\right) \end{array} \]
(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}

Alternative 1: 10.7% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(\sin^{-1} 1, \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right), \left(\pi \cdot \pi\right) \cdot 0.25\right)\\ \mathbf{if}\;x \leq 5.5 \cdot 10^{-17}:\\ \;\;\;\;\mathsf{fma}\left(\pi, \frac{\left(\pi \cdot \pi\right) \cdot 0.125}{t\_0}, -\frac{{\sin^{-1} 1}^{3}}{t\_0}\right)\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(1 - x\right)\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (fma (asin 1.0) (fma PI 0.5 (asin 1.0)) (* (* PI PI) 0.25))))
   (if (<= x 5.5e-17)
     (fma PI (/ (* (* PI PI) 0.125) t_0) (- (/ (pow (asin 1.0) 3.0) t_0)))
     (acos (- 1.0 x)))))
double code(double x) {
	double t_0 = fma(asin(1.0), fma(((double) M_PI), 0.5, asin(1.0)), ((((double) M_PI) * ((double) M_PI)) * 0.25));
	double tmp;
	if (x <= 5.5e-17) {
		tmp = fma(((double) M_PI), (((((double) M_PI) * ((double) M_PI)) * 0.125) / t_0), -(pow(asin(1.0), 3.0) / t_0));
	} else {
		tmp = acos((1.0 - x));
	}
	return tmp;
}
function code(x)
	t_0 = fma(asin(1.0), fma(pi, 0.5, asin(1.0)), Float64(Float64(pi * pi) * 0.25))
	tmp = 0.0
	if (x <= 5.5e-17)
		tmp = fma(pi, Float64(Float64(Float64(pi * pi) * 0.125) / t_0), Float64(-Float64((asin(1.0) ^ 3.0) / t_0)));
	else
		tmp = acos(Float64(1.0 - x));
	end
	return tmp
end
code[x_] := Block[{t$95$0 = N[(N[ArcSin[1.0], $MachinePrecision] * N[(Pi * 0.5 + N[ArcSin[1.0], $MachinePrecision]), $MachinePrecision] + N[(N[(Pi * Pi), $MachinePrecision] * 0.25), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, 5.5e-17], N[(Pi * N[(N[(N[(Pi * Pi), $MachinePrecision] * 0.125), $MachinePrecision] / t$95$0), $MachinePrecision] + (-N[(N[Power[N[ArcSin[1.0], $MachinePrecision], 3.0], $MachinePrecision] / t$95$0), $MachinePrecision])), $MachinePrecision], N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\sin^{-1} 1, \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right), \left(\pi \cdot \pi\right) \cdot 0.25\right)\\
\mathbf{if}\;x \leq 5.5 \cdot 10^{-17}:\\
\;\;\;\;\mathsf{fma}\left(\pi, \frac{\left(\pi \cdot \pi\right) \cdot 0.125}{t\_0}, -\frac{{\sin^{-1} 1}^{3}}{t\_0}\right)\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(1 - x\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 5.50000000000000001e-17

    1. Initial program 3.9%

      \[\cos^{-1} \left(1 - x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \cos^{-1} \color{blue}{1} \]
    4. Step-by-step derivation
      1. Simplified3.9%

        \[\leadsto \cos^{-1} \color{blue}{1} \]
      2. Step-by-step derivation
        1. acos-asinN/A

          \[\leadsto \color{blue}{\frac{\mathsf{PI}\left(\right)}{2} - \sin^{-1} 1} \]
        2. flip--N/A

          \[\leadsto \color{blue}{\frac{\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2} - \sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}} \]
        3. div-subN/A

          \[\leadsto \color{blue}{\frac{\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1} - \frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}} \]
        4. sub-negN/A

          \[\leadsto \color{blue}{\frac{\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1} + \left(\mathsf{neg}\left(\frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}\right)\right)} \]
        5. div-invN/A

          \[\leadsto \color{blue}{\left(\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}\right) \cdot \frac{1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}} + \left(\mathsf{neg}\left(\frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}\right)\right) \]
        6. accelerator-lowering-fma.f64N/A

          \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}, \frac{1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}, \mathsf{neg}\left(\frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}\right)\right)} \]
      3. Applied egg-rr7.3%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot 0.25, \frac{1}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)}, -\frac{{\sin^{-1} 1}^{2}}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)}\right)} \]
      4. Applied egg-rr7.3%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\pi, \frac{\left(\pi \cdot \pi\right) \cdot 0.125}{\mathsf{fma}\left(\sin^{-1} 1, \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right), \left(\pi \cdot \pi\right) \cdot 0.25\right)}, \frac{{\sin^{-1} 1}^{3}}{-\mathsf{fma}\left(\sin^{-1} 1, \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right), \left(\pi \cdot \pi\right) \cdot 0.25\right)}\right)} \]

      if 5.50000000000000001e-17 < x

      1. Initial program 55.6%

        \[\cos^{-1} \left(1 - x\right) \]
      2. Add Preprocessing
    5. Recombined 2 regimes into one program.
    6. Final simplification9.6%

      \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 5.5 \cdot 10^{-17}:\\ \;\;\;\;\mathsf{fma}\left(\pi, \frac{\left(\pi \cdot \pi\right) \cdot 0.125}{\mathsf{fma}\left(\sin^{-1} 1, \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right), \left(\pi \cdot \pi\right) \cdot 0.25\right)}, -\frac{{\sin^{-1} 1}^{3}}{\mathsf{fma}\left(\sin^{-1} 1, \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right), \left(\pi \cdot \pi\right) \cdot 0.25\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(1 - x\right)\\ \end{array} \]
    7. Add Preprocessing

    Alternative 2: 10.7% accurate, 0.2× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)\\ \mathbf{if}\;1 - x \leq 0.9999999999999991:\\ \;\;\;\;\cos^{-1} \left(1 - x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot 0.25, t\_0, -t\_0 \cdot {\sin^{-1} 1}^{2}\right)}{{t\_0}^{2}}\\ \end{array} \end{array} \]
    (FPCore (x)
     :precision binary64
     (let* ((t_0 (fma PI 0.5 (asin 1.0))))
       (if (<= (- 1.0 x) 0.9999999999999991)
         (acos (- 1.0 x))
         (/
          (fma (* (* PI PI) 0.25) t_0 (- (* t_0 (pow (asin 1.0) 2.0))))
          (pow t_0 2.0)))))
    double code(double x) {
    	double t_0 = fma(((double) M_PI), 0.5, asin(1.0));
    	double tmp;
    	if ((1.0 - x) <= 0.9999999999999991) {
    		tmp = acos((1.0 - x));
    	} else {
    		tmp = fma(((((double) M_PI) * ((double) M_PI)) * 0.25), t_0, -(t_0 * pow(asin(1.0), 2.0))) / pow(t_0, 2.0);
    	}
    	return tmp;
    }
    
    function code(x)
    	t_0 = fma(pi, 0.5, asin(1.0))
    	tmp = 0.0
    	if (Float64(1.0 - x) <= 0.9999999999999991)
    		tmp = acos(Float64(1.0 - x));
    	else
    		tmp = Float64(fma(Float64(Float64(pi * pi) * 0.25), t_0, Float64(-Float64(t_0 * (asin(1.0) ^ 2.0)))) / (t_0 ^ 2.0));
    	end
    	return tmp
    end
    
    code[x_] := Block[{t$95$0 = N[(Pi * 0.5 + N[ArcSin[1.0], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(1.0 - x), $MachinePrecision], 0.9999999999999991], N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision], N[(N[(N[(N[(Pi * Pi), $MachinePrecision] * 0.25), $MachinePrecision] * t$95$0 + (-N[(t$95$0 * N[Power[N[ArcSin[1.0], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision])), $MachinePrecision] / N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_0 := \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)\\
    \mathbf{if}\;1 - x \leq 0.9999999999999991:\\
    \;\;\;\;\cos^{-1} \left(1 - x\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;\frac{\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot 0.25, t\_0, -t\_0 \cdot {\sin^{-1} 1}^{2}\right)}{{t\_0}^{2}}\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (-.f64 #s(literal 1 binary64) x) < 0.99999999999999911

      1. Initial program 55.6%

        \[\cos^{-1} \left(1 - x\right) \]
      2. Add Preprocessing

      if 0.99999999999999911 < (-.f64 #s(literal 1 binary64) x)

      1. Initial program 3.9%

        \[\cos^{-1} \left(1 - x\right) \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \cos^{-1} \color{blue}{1} \]
      4. Step-by-step derivation
        1. Simplified3.9%

          \[\leadsto \cos^{-1} \color{blue}{1} \]
        2. Step-by-step derivation
          1. acos-asinN/A

            \[\leadsto \color{blue}{\frac{\mathsf{PI}\left(\right)}{2} - \sin^{-1} 1} \]
          2. flip--N/A

            \[\leadsto \color{blue}{\frac{\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2} - \sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}} \]
          3. div-subN/A

            \[\leadsto \color{blue}{\frac{\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1} - \frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}} \]
          4. sub-negN/A

            \[\leadsto \color{blue}{\frac{\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1} + \left(\mathsf{neg}\left(\frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}\right)\right)} \]
          5. div-invN/A

            \[\leadsto \color{blue}{\left(\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}\right) \cdot \frac{1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}} + \left(\mathsf{neg}\left(\frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}\right)\right) \]
          6. accelerator-lowering-fma.f64N/A

            \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}, \frac{1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}, \mathsf{neg}\left(\frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}\right)\right)} \]
        3. Applied egg-rr7.3%

          \[\leadsto \color{blue}{\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot 0.25, \frac{1}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)}, -\frac{{\sin^{-1} 1}^{2}}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)}\right)} \]
        4. Step-by-step derivation
          1. un-div-invN/A

            \[\leadsto \color{blue}{\frac{\left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \frac{1}{4}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}} + \left(\mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right) \]
          2. distribute-neg-fracN/A

            \[\leadsto \frac{\left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \frac{1}{4}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1} + \color{blue}{\frac{\mathsf{neg}\left({\sin^{-1} 1}^{2}\right)}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}} \]
          3. frac-addN/A

            \[\leadsto \color{blue}{\frac{\left(\left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \frac{1}{4}\right) \cdot \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1\right) + \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1\right) \cdot \left(\mathsf{neg}\left({\sin^{-1} 1}^{2}\right)\right)}{\left(\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1\right) \cdot \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1\right)}} \]
          4. /-lowering-/.f64N/A

            \[\leadsto \color{blue}{\frac{\left(\left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \frac{1}{4}\right) \cdot \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1\right) + \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1\right) \cdot \left(\mathsf{neg}\left({\sin^{-1} 1}^{2}\right)\right)}{\left(\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1\right) \cdot \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1\right)}} \]
        5. Applied egg-rr7.3%

          \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot 0.25, \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right), \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right) \cdot \left(-{\sin^{-1} 1}^{2}\right)\right)}{{\left(\mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)\right)}^{2}}} \]
      5. Recombined 2 regimes into one program.
      6. Final simplification9.5%

        \[\leadsto \begin{array}{l} \mathbf{if}\;1 - x \leq 0.9999999999999991:\\ \;\;\;\;\cos^{-1} \left(1 - x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot 0.25, \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right), -\mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right) \cdot {\sin^{-1} 1}^{2}\right)}{{\left(\mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)\right)}^{2}}\\ \end{array} \]
      7. Add Preprocessing

      Alternative 3: 10.7% accurate, 0.2× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)\\ \mathbf{if}\;x \leq 5.5 \cdot 10^{-17}:\\ \;\;\;\;\mathsf{fma}\left(\frac{0.25}{t\_0}, \pi \cdot \pi, \frac{{\sin^{-1} 1}^{2}}{-t\_0}\right)\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(1 - x\right)\\ \end{array} \end{array} \]
      (FPCore (x)
       :precision binary64
       (let* ((t_0 (fma PI 0.5 (asin 1.0))))
         (if (<= x 5.5e-17)
           (fma (/ 0.25 t_0) (* PI PI) (/ (pow (asin 1.0) 2.0) (- t_0)))
           (acos (- 1.0 x)))))
      double code(double x) {
      	double t_0 = fma(((double) M_PI), 0.5, asin(1.0));
      	double tmp;
      	if (x <= 5.5e-17) {
      		tmp = fma((0.25 / t_0), (((double) M_PI) * ((double) M_PI)), (pow(asin(1.0), 2.0) / -t_0));
      	} else {
      		tmp = acos((1.0 - x));
      	}
      	return tmp;
      }
      
      function code(x)
      	t_0 = fma(pi, 0.5, asin(1.0))
      	tmp = 0.0
      	if (x <= 5.5e-17)
      		tmp = fma(Float64(0.25 / t_0), Float64(pi * pi), Float64((asin(1.0) ^ 2.0) / Float64(-t_0)));
      	else
      		tmp = acos(Float64(1.0 - x));
      	end
      	return tmp
      end
      
      code[x_] := Block[{t$95$0 = N[(Pi * 0.5 + N[ArcSin[1.0], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, 5.5e-17], N[(N[(0.25 / t$95$0), $MachinePrecision] * N[(Pi * Pi), $MachinePrecision] + N[(N[Power[N[ArcSin[1.0], $MachinePrecision], 2.0], $MachinePrecision] / (-t$95$0)), $MachinePrecision]), $MachinePrecision], N[ArcCos[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      t_0 := \mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)\\
      \mathbf{if}\;x \leq 5.5 \cdot 10^{-17}:\\
      \;\;\;\;\mathsf{fma}\left(\frac{0.25}{t\_0}, \pi \cdot \pi, \frac{{\sin^{-1} 1}^{2}}{-t\_0}\right)\\
      
      \mathbf{else}:\\
      \;\;\;\;\cos^{-1} \left(1 - x\right)\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if x < 5.50000000000000001e-17

        1. Initial program 3.9%

          \[\cos^{-1} \left(1 - x\right) \]
        2. Add Preprocessing
        3. Taylor expanded in x around 0

          \[\leadsto \cos^{-1} \color{blue}{1} \]
        4. Step-by-step derivation
          1. Simplified3.9%

            \[\leadsto \cos^{-1} \color{blue}{1} \]
          2. Step-by-step derivation
            1. acos-asinN/A

              \[\leadsto \color{blue}{\frac{\mathsf{PI}\left(\right)}{2} - \sin^{-1} 1} \]
            2. flip--N/A

              \[\leadsto \color{blue}{\frac{\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2} - \sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}} \]
            3. div-subN/A

              \[\leadsto \color{blue}{\frac{\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1} - \frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}} \]
            4. sub-negN/A

              \[\leadsto \color{blue}{\frac{\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1} + \left(\mathsf{neg}\left(\frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}\right)\right)} \]
            5. div-invN/A

              \[\leadsto \color{blue}{\left(\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}\right) \cdot \frac{1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}} + \left(\mathsf{neg}\left(\frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}\right)\right) \]
            6. accelerator-lowering-fma.f64N/A

              \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\mathsf{PI}\left(\right)}{2} \cdot \frac{\mathsf{PI}\left(\right)}{2}, \frac{1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}, \mathsf{neg}\left(\frac{\sin^{-1} 1 \cdot \sin^{-1} 1}{\frac{\mathsf{PI}\left(\right)}{2} + \sin^{-1} 1}\right)\right)} \]
          3. Applied egg-rr7.3%

            \[\leadsto \color{blue}{\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot 0.25, \frac{1}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)}, -\frac{{\sin^{-1} 1}^{2}}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)}\right)} \]
          4. Step-by-step derivation
            1. associate-*l*N/A

              \[\leadsto \color{blue}{\left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \left(\frac{1}{4} \cdot \frac{1}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)} + \left(\mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right) \]
            2. *-commutativeN/A

              \[\leadsto \color{blue}{\left(\frac{1}{4} \cdot \frac{1}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right) \cdot \left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right)} + \left(\mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right) \]
            3. accelerator-lowering-fma.f64N/A

              \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{4} \cdot \frac{1}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}, \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right), \mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right)} \]
            4. un-div-invN/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\frac{1}{4}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}}, \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right), \mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right) \]
            5. /-lowering-/.f64N/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\frac{1}{4}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}}, \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right), \mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right) \]
            6. accelerator-lowering-fma.f64N/A

              \[\leadsto \mathsf{fma}\left(\frac{\frac{1}{4}}{\color{blue}{\mathsf{fma}\left(\mathsf{PI}\left(\right), \frac{1}{2}, \sin^{-1} 1\right)}}, \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right), \mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right) \]
            7. PI-lowering-PI.f64N/A

              \[\leadsto \mathsf{fma}\left(\frac{\frac{1}{4}}{\mathsf{fma}\left(\color{blue}{\mathsf{PI}\left(\right)}, \frac{1}{2}, \sin^{-1} 1\right)}, \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right), \mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right) \]
            8. asin-lowering-asin.f64N/A

              \[\leadsto \mathsf{fma}\left(\frac{\frac{1}{4}}{\mathsf{fma}\left(\mathsf{PI}\left(\right), \frac{1}{2}, \color{blue}{\sin^{-1} 1}\right)}, \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right), \mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right) \]
            9. *-lowering-*.f64N/A

              \[\leadsto \mathsf{fma}\left(\frac{\frac{1}{4}}{\mathsf{fma}\left(\mathsf{PI}\left(\right), \frac{1}{2}, \sin^{-1} 1\right)}, \color{blue}{\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)}, \mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right) \]
            10. PI-lowering-PI.f64N/A

              \[\leadsto \mathsf{fma}\left(\frac{\frac{1}{4}}{\mathsf{fma}\left(\mathsf{PI}\left(\right), \frac{1}{2}, \sin^{-1} 1\right)}, \color{blue}{\mathsf{PI}\left(\right)} \cdot \mathsf{PI}\left(\right), \mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right) \]
            11. PI-lowering-PI.f64N/A

              \[\leadsto \mathsf{fma}\left(\frac{\frac{1}{4}}{\mathsf{fma}\left(\mathsf{PI}\left(\right), \frac{1}{2}, \sin^{-1} 1\right)}, \mathsf{PI}\left(\right) \cdot \color{blue}{\mathsf{PI}\left(\right)}, \mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)\right) \]
            12. neg-lowering-neg.f64N/A

              \[\leadsto \mathsf{fma}\left(\frac{\frac{1}{4}}{\mathsf{fma}\left(\mathsf{PI}\left(\right), \frac{1}{2}, \sin^{-1} 1\right)}, \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right), \color{blue}{\mathsf{neg}\left(\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}\right)}\right) \]
            13. /-lowering-/.f64N/A

              \[\leadsto \mathsf{fma}\left(\frac{\frac{1}{4}}{\mathsf{fma}\left(\mathsf{PI}\left(\right), \frac{1}{2}, \sin^{-1} 1\right)}, \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right), \mathsf{neg}\left(\color{blue}{\frac{{\sin^{-1} 1}^{2}}{\mathsf{PI}\left(\right) \cdot \frac{1}{2} + \sin^{-1} 1}}\right)\right) \]
          5. Applied egg-rr7.3%

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

          if 5.50000000000000001e-17 < x

          1. Initial program 55.6%

            \[\cos^{-1} \left(1 - x\right) \]
          2. Add Preprocessing
        5. Recombined 2 regimes into one program.
        6. Final simplification9.5%

          \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 5.5 \cdot 10^{-17}:\\ \;\;\;\;\mathsf{fma}\left(\frac{0.25}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)}, \pi \cdot \pi, \frac{{\sin^{-1} 1}^{2}}{-\mathsf{fma}\left(\pi, 0.5, \sin^{-1} 1\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(1 - x\right)\\ \end{array} \]
        7. Add Preprocessing

        Alternative 4: 9.8% accurate, 0.9× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 5.5 \cdot 10^{-17}:\\ \;\;\;\;\cos^{-1} \left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(1 - x\right)\\ \end{array} \end{array} \]
        (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(Float64(-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} \left(-x\right)\\
        
        \mathbf{else}:\\
        \;\;\;\;\cos^{-1} \left(1 - x\right)\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if x < 5.50000000000000001e-17

          1. Initial program 3.9%

            \[\cos^{-1} \left(1 - x\right) \]
          2. Add Preprocessing
          3. Taylor expanded in x around inf

            \[\leadsto \cos^{-1} \color{blue}{\left(-1 \cdot x\right)} \]
          4. Step-by-step derivation
            1. mul-1-negN/A

              \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{neg}\left(x\right)\right)} \]
            2. neg-lowering-neg.f646.4

              \[\leadsto \cos^{-1} \color{blue}{\left(-x\right)} \]
          5. Simplified6.4%

            \[\leadsto \cos^{-1} \color{blue}{\left(-x\right)} \]

          if 5.50000000000000001e-17 < x

          1. Initial program 55.6%

            \[\cos^{-1} \left(1 - x\right) \]
          2. Add Preprocessing
        3. Recombined 2 regimes into one program.
        4. Add Preprocessing

        Alternative 5: 7.0% accurate, 1.0× speedup?

        \[\begin{array}{l} \\ \cos^{-1} \left(-x\right) \end{array} \]
        (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(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}
        
        Derivation
        1. Initial program 6.4%

          \[\cos^{-1} \left(1 - x\right) \]
        2. Add Preprocessing
        3. Taylor expanded in x around inf

          \[\leadsto \cos^{-1} \color{blue}{\left(-1 \cdot x\right)} \]
        4. Step-by-step derivation
          1. mul-1-negN/A

            \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{neg}\left(x\right)\right)} \]
          2. neg-lowering-neg.f646.7

            \[\leadsto \cos^{-1} \color{blue}{\left(-x\right)} \]
        5. Simplified6.7%

          \[\leadsto \cos^{-1} \color{blue}{\left(-x\right)} \]
        6. Add Preprocessing

        Alternative 6: 3.8% accurate, 1.0× speedup?

        \[\begin{array}{l} \\ \cos^{-1} 1 \end{array} \]
        (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}
        
        Derivation
        1. Initial program 6.4%

          \[\cos^{-1} \left(1 - x\right) \]
        2. Add Preprocessing
        3. Taylor expanded in x around 0

          \[\leadsto \cos^{-1} \color{blue}{1} \]
        4. Step-by-step derivation
          1. Simplified3.9%

            \[\leadsto \cos^{-1} \color{blue}{1} \]
          2. Add Preprocessing

          Developer Target 1: 100.0% accurate, 0.8× speedup?

          \[\begin{array}{l} \\ 2 \cdot \sin^{-1} \left(\sqrt{\frac{x}{2}}\right) \end{array} \]
          (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}
          

          Reproduce

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