Beckmann Sample, near normal, slope_x

Percentage Accurate: 57.4% → 99.1%
Time: 16.7s
Alternatives: 12
Speedup: 3.1×

Specification

?
\[\left(\left(cosTheta\_i > 0.9999 \land cosTheta\_i \leq 1\right) \land \left(2.328306437 \cdot 10^{-10} \leq u1 \land u1 \leq 1\right)\right) \land \left(2.328306437 \cdot 10^{-10} \leq u2 \land u2 \leq 1\right)\]
\[\begin{array}{l} \\ \sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-logf((1.0f - u1))) * cosf(((2.0f * ((float) M_PI)) * u2));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt(-log((single(1.0) - u1))) * cos(((single(2.0) * single(pi)) * u2));
end
\begin{array}{l}

\\
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)
\end{array}

Sampling outcomes in binary32 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 12 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: 57.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-logf((1.0f - u1))) * cosf(((2.0f * ((float) M_PI)) * u2));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt(-log((single(1.0) - u1))) * cos(((single(2.0) * single(pi)) * u2));
end
\begin{array}{l}

\\
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)
\end{array}

Alternative 1: 99.1% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)\\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(t\_0 + \left(\left(0.5 + t\_0 \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \left(\sqrt[3]{{\pi}^{3} \cdot {u2}^{3}}\right)\right)\right) \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (cos (* u2 (* 2.0 PI)))))
   (*
    (sqrt (- (log1p (- u1))))
    (+
     t_0
     (-
      (+ 0.5 (* t_0 -0.5))
      (* (sin (* u2 PI)) (sin (cbrt (* (pow PI 3.0) (pow u2 3.0))))))))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = cosf((u2 * (2.0f * ((float) M_PI))));
	return sqrtf(-log1pf(-u1)) * (t_0 + ((0.5f + (t_0 * -0.5f)) - (sinf((u2 * ((float) M_PI))) * sinf(cbrtf((powf(((float) M_PI), 3.0f) * powf(u2, 3.0f)))))));
}
function code(cosTheta_i, u1, u2)
	t_0 = cos(Float32(u2 * Float32(Float32(2.0) * Float32(pi))))
	return Float32(sqrt(Float32(-log1p(Float32(-u1)))) * Float32(t_0 + Float32(Float32(Float32(0.5) + Float32(t_0 * Float32(-0.5))) - Float32(sin(Float32(u2 * Float32(pi))) * sin(cbrt(Float32((Float32(pi) ^ Float32(3.0)) * (u2 ^ Float32(3.0)))))))))
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)\\
\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(t\_0 + \left(\left(0.5 + t\_0 \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \left(\sqrt[3]{{\pi}^{3} \cdot {u2}^{3}}\right)\right)\right)
\end{array}
\end{array}
Derivation
  1. Initial program 56.7%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg56.7%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.1%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. associate-*l*99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
    2. cos-299.0%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\cos \left(\pi \cdot u2\right) \cdot \cos \left(\pi \cdot u2\right) - \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)} \]
    3. prod-diff99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\mathsf{fma}\left(\cos \left(\pi \cdot u2\right), \cos \left(\pi \cdot u2\right), -\sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right)} \]
    4. fma-neg99.0%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\color{blue}{\left(\cos \left(\pi \cdot u2\right) \cdot \cos \left(\pi \cdot u2\right) - \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right) \]
    5. cos-299.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\color{blue}{\cos \left(2 \cdot \left(\pi \cdot u2\right)\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right) \]
    6. associate-*l*99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \color{blue}{\left(\left(2 \cdot \pi\right) \cdot u2\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right) \]
    7. *-commutative99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(\color{blue}{\left(\pi \cdot 2\right)} \cdot u2\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right) \]
    8. associate-*l*99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \color{blue}{\left(\pi \cdot \left(2 \cdot u2\right)\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right) \]
    9. sqr-sin-a99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(\pi \cdot \left(2 \cdot u2\right)\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \color{blue}{0.5 - 0.5 \cdot \cos \left(2 \cdot \left(\pi \cdot u2\right)\right)}\right)\right) \]
    10. associate-*l*99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(\pi \cdot \left(2 \cdot u2\right)\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), 0.5 - 0.5 \cdot \cos \color{blue}{\left(\left(2 \cdot \pi\right) \cdot u2\right)}\right)\right) \]
  6. Applied egg-rr99.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\cos \left(\pi \cdot \left(2 \cdot u2\right)\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), 0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right)\right)} \]
  7. Step-by-step derivation
    1. *-commutative99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \color{blue}{\left(\left(2 \cdot u2\right) \cdot \pi\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), 0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right)\right) \]
    2. *-commutative99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(\color{blue}{\left(u2 \cdot 2\right)} \cdot \pi\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), 0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right)\right) \]
    3. associate-*l*99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \color{blue}{\left(u2 \cdot \left(2 \cdot \pi\right)\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), 0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right)\right) \]
    4. fma-undefine99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \color{blue}{\left(\left(-\sin \left(\pi \cdot u2\right)\right) \cdot \sin \left(\pi \cdot u2\right) + \left(0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right)\right)}\right) \]
    5. +-commutative99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \color{blue}{\left(\left(0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right) + \left(-\sin \left(\pi \cdot u2\right)\right) \cdot \sin \left(\pi \cdot u2\right)\right)}\right) \]
    6. distribute-lft-neg-out99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right) + \color{blue}{\left(-\sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)}\right)\right) \]
    7. unsub-neg99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \color{blue}{\left(\left(0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right) - \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)}\right) \]
  8. Simplified99.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \left(u2 \cdot \pi\right)\right)\right)} \]
  9. Step-by-step derivation
    1. *-commutative99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \color{blue}{\left(\pi \cdot u2\right)}\right)\right) \]
    2. add-cbrt-cube99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \left(\color{blue}{\sqrt[3]{\left(\pi \cdot \pi\right) \cdot \pi}} \cdot u2\right)\right)\right) \]
    3. add-cbrt-cube99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \left(\sqrt[3]{\left(\pi \cdot \pi\right) \cdot \pi} \cdot \color{blue}{\sqrt[3]{\left(u2 \cdot u2\right) \cdot u2}}\right)\right)\right) \]
    4. cbrt-unprod99.2%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \color{blue}{\left(\sqrt[3]{\left(\left(\pi \cdot \pi\right) \cdot \pi\right) \cdot \left(\left(u2 \cdot u2\right) \cdot u2\right)}\right)}\right)\right) \]
    5. pow399.2%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \left(\sqrt[3]{\color{blue}{{\pi}^{3}} \cdot \left(\left(u2 \cdot u2\right) \cdot u2\right)}\right)\right)\right) \]
    6. pow399.2%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \left(\sqrt[3]{{\pi}^{3} \cdot \color{blue}{{u2}^{3}}}\right)\right)\right) \]
  10. Applied egg-rr99.2%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \color{blue}{\left(\sqrt[3]{{\pi}^{3} \cdot {u2}^{3}}\right)}\right)\right) \]
  11. Final simplification99.2%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \left(\sqrt[3]{{\pi}^{3} \cdot {u2}^{3}}\right)\right)\right) \]
  12. Add Preprocessing

Alternative 2: 99.0% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(u2 \cdot \pi\right)\\ t_1 := \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)\\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(t\_1 + \left(\left(0.5 + t\_1 \cdot -0.5\right) - t\_0 \cdot t\_0\right)\right) \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (sin (* u2 PI))) (t_1 (cos (* u2 (* 2.0 PI)))))
   (* (sqrt (- (log1p (- u1)))) (+ t_1 (- (+ 0.5 (* t_1 -0.5)) (* t_0 t_0))))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = sinf((u2 * ((float) M_PI)));
	float t_1 = cosf((u2 * (2.0f * ((float) M_PI))));
	return sqrtf(-log1pf(-u1)) * (t_1 + ((0.5f + (t_1 * -0.5f)) - (t_0 * t_0)));
}
function code(cosTheta_i, u1, u2)
	t_0 = sin(Float32(u2 * Float32(pi)))
	t_1 = cos(Float32(u2 * Float32(Float32(2.0) * Float32(pi))))
	return Float32(sqrt(Float32(-log1p(Float32(-u1)))) * Float32(t_1 + Float32(Float32(Float32(0.5) + Float32(t_1 * Float32(-0.5))) - Float32(t_0 * t_0))))
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(u2 \cdot \pi\right)\\
t_1 := \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)\\
\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(t\_1 + \left(\left(0.5 + t\_1 \cdot -0.5\right) - t\_0 \cdot t\_0\right)\right)
\end{array}
\end{array}
Derivation
  1. Initial program 56.7%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg56.7%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.1%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. associate-*l*99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
    2. cos-299.0%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\cos \left(\pi \cdot u2\right) \cdot \cos \left(\pi \cdot u2\right) - \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)} \]
    3. prod-diff99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\mathsf{fma}\left(\cos \left(\pi \cdot u2\right), \cos \left(\pi \cdot u2\right), -\sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right)} \]
    4. fma-neg99.0%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\color{blue}{\left(\cos \left(\pi \cdot u2\right) \cdot \cos \left(\pi \cdot u2\right) - \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right) \]
    5. cos-299.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\color{blue}{\cos \left(2 \cdot \left(\pi \cdot u2\right)\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right) \]
    6. associate-*l*99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \color{blue}{\left(\left(2 \cdot \pi\right) \cdot u2\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right) \]
    7. *-commutative99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(\color{blue}{\left(\pi \cdot 2\right)} \cdot u2\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right) \]
    8. associate-*l*99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \color{blue}{\left(\pi \cdot \left(2 \cdot u2\right)\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)\right) \]
    9. sqr-sin-a99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(\pi \cdot \left(2 \cdot u2\right)\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), \color{blue}{0.5 - 0.5 \cdot \cos \left(2 \cdot \left(\pi \cdot u2\right)\right)}\right)\right) \]
    10. associate-*l*99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(\pi \cdot \left(2 \cdot u2\right)\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), 0.5 - 0.5 \cdot \cos \color{blue}{\left(\left(2 \cdot \pi\right) \cdot u2\right)}\right)\right) \]
  6. Applied egg-rr99.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\cos \left(\pi \cdot \left(2 \cdot u2\right)\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), 0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right)\right)} \]
  7. Step-by-step derivation
    1. *-commutative99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \color{blue}{\left(\left(2 \cdot u2\right) \cdot \pi\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), 0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right)\right) \]
    2. *-commutative99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(\color{blue}{\left(u2 \cdot 2\right)} \cdot \pi\right) + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), 0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right)\right) \]
    3. associate-*l*99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \color{blue}{\left(u2 \cdot \left(2 \cdot \pi\right)\right)} + \mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), 0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right)\right) \]
    4. fma-undefine99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \color{blue}{\left(\left(-\sin \left(\pi \cdot u2\right)\right) \cdot \sin \left(\pi \cdot u2\right) + \left(0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right)\right)}\right) \]
    5. +-commutative99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \color{blue}{\left(\left(0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right) + \left(-\sin \left(\pi \cdot u2\right)\right) \cdot \sin \left(\pi \cdot u2\right)\right)}\right) \]
    6. distribute-lft-neg-out99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right) + \color{blue}{\left(-\sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)}\right)\right) \]
    7. unsub-neg99.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \color{blue}{\left(\left(0.5 - 0.5 \cdot \cos \left(\pi \cdot \left(2 \cdot u2\right)\right)\right) - \sin \left(\pi \cdot u2\right) \cdot \sin \left(\pi \cdot u2\right)\right)}\right) \]
  8. Simplified99.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \left(u2 \cdot \pi\right)\right)\right)} \]
  9. Final simplification99.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) + \left(\left(0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot -0.5\right) - \sin \left(u2 \cdot \pi\right) \cdot \sin \left(u2 \cdot \pi\right)\right)\right) \]
  10. Add Preprocessing

Alternative 3: 90.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)\\ \mathbf{if}\;t\_0 \leq 0.9999589920043945:\\ \;\;\;\;t\_0 \cdot \sqrt{u1}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (cos (* u2 (* 2.0 PI)))))
   (if (<= t_0 0.9999589920043945)
     (* t_0 (sqrt u1))
     (sqrt (- (log1p (- u1)))))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = cosf((u2 * (2.0f * ((float) M_PI))));
	float tmp;
	if (t_0 <= 0.9999589920043945f) {
		tmp = t_0 * sqrtf(u1);
	} else {
		tmp = sqrtf(-log1pf(-u1));
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = cos(Float32(u2 * Float32(Float32(2.0) * Float32(pi))))
	tmp = Float32(0.0)
	if (t_0 <= Float32(0.9999589920043945))
		tmp = Float32(t_0 * sqrt(u1));
	else
		tmp = sqrt(Float32(-log1p(Float32(-u1))));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)\\
\mathbf{if}\;t\_0 \leq 0.9999589920043945:\\
\;\;\;\;t\_0 \cdot \sqrt{u1}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2)) < 0.99995899

    1. Initial program 54.2%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg54.2%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-define97.8%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Simplified97.8%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-cbrt-cube97.8%

        \[\leadsto \color{blue}{\sqrt[3]{\left(\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)}\right) \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)}}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. pow1/394.9%

        \[\leadsto \color{blue}{{\left(\left(\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)}\right) \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)}\right)}^{0.3333333333333333}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    6. Applied egg-rr76.7%

      \[\leadsto \color{blue}{{\left({\left(\mathsf{log1p}\left(u1\right)\right)}^{1.5}\right)}^{0.3333333333333333}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    7. Taylor expanded in u1 around 0 80.4%

      \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]

    if 0.99995899 < (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2))

    1. Initial program 57.6%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg57.6%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-define99.5%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Simplified99.5%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in u2 around 0 96.6%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification92.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \leq 0.9999589920043945:\\ \;\;\;\;\cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{u1}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 99.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log1p (- u1)))) (cos (* u2 (* 2.0 PI)))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-log1pf(-u1)) * cosf((u2 * (2.0f * ((float) M_PI))));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log1p(Float32(-u1)))) * cos(Float32(u2 * Float32(Float32(2.0) * Float32(pi)))))
end
\begin{array}{l}

\\
\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)
\end{array}
Derivation
  1. Initial program 56.7%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg56.7%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.1%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Final simplification99.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \]
  6. Add Preprocessing

Alternative 5: 94.7% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;u2 \cdot \left(2 \cdot \pi\right) \leq 0.0013500000350177288:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(u1 \cdot \cos \left(2 \cdot \left(u2 \cdot \pi\right)\right)\right) \cdot \sqrt{0.5 + \frac{1}{u1}}\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (if (<= (* u2 (* 2.0 PI)) 0.0013500000350177288)
   (sqrt (- (log1p (- u1))))
   (* (* u1 (cos (* 2.0 (* u2 PI)))) (sqrt (+ 0.5 (/ 1.0 u1))))))
float code(float cosTheta_i, float u1, float u2) {
	float tmp;
	if ((u2 * (2.0f * ((float) M_PI))) <= 0.0013500000350177288f) {
		tmp = sqrtf(-log1pf(-u1));
	} else {
		tmp = (u1 * cosf((2.0f * (u2 * ((float) M_PI))))) * sqrtf((0.5f + (1.0f / u1)));
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	tmp = Float32(0.0)
	if (Float32(u2 * Float32(Float32(2.0) * Float32(pi))) <= Float32(0.0013500000350177288))
		tmp = sqrt(Float32(-log1p(Float32(-u1))));
	else
		tmp = Float32(Float32(u1 * cos(Float32(Float32(2.0) * Float32(u2 * Float32(pi))))) * sqrt(Float32(Float32(0.5) + Float32(Float32(1.0) / u1))));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;u2 \cdot \left(2 \cdot \pi\right) \leq 0.0013500000350177288:\\
\;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\

\mathbf{else}:\\
\;\;\;\;\left(u1 \cdot \cos \left(2 \cdot \left(u2 \cdot \pi\right)\right)\right) \cdot \sqrt{0.5 + \frac{1}{u1}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2) < 0.00135000004

    1. Initial program 57.1%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg57.1%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-define99.6%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Simplified99.6%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in u2 around 0 99.2%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]

    if 0.00135000004 < (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2)

    1. Initial program 56.1%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg56.1%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-define98.2%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Simplified98.2%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-cube-cbrt97.3%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\left(\sqrt[3]{\cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \cdot \sqrt[3]{\cos \left(\left(2 \cdot \pi\right) \cdot u2\right)}\right) \cdot \sqrt[3]{\cos \left(\left(2 \cdot \pi\right) \cdot u2\right)}\right)} \]
      2. pow397.2%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{{\left(\sqrt[3]{\cos \left(\left(2 \cdot \pi\right) \cdot u2\right)}\right)}^{3}} \]
      3. *-commutative97.2%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot {\left(\sqrt[3]{\cos \left(\color{blue}{\left(\pi \cdot 2\right)} \cdot u2\right)}\right)}^{3} \]
      4. associate-*l*97.2%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot {\left(\sqrt[3]{\cos \color{blue}{\left(\pi \cdot \left(2 \cdot u2\right)\right)}}\right)}^{3} \]
    6. Applied egg-rr97.2%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{{\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3}} \]
    7. Taylor expanded in u1 around 0 87.9%

      \[\leadsto \sqrt{-\color{blue}{u1 \cdot \left(-0.5 \cdot u1 - 1\right)}} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
    8. Taylor expanded in u1 around inf 87.9%

      \[\leadsto \sqrt{-\color{blue}{-1 \cdot \left({u1}^{2} \cdot \left(0.5 + \frac{1}{u1}\right)\right)}} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
    9. Step-by-step derivation
      1. mul-1-neg87.9%

        \[\leadsto \sqrt{-\color{blue}{\left(-{u1}^{2} \cdot \left(0.5 + \frac{1}{u1}\right)\right)}} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
      2. distribute-rgt-neg-in87.9%

        \[\leadsto \sqrt{-\color{blue}{{u1}^{2} \cdot \left(-\left(0.5 + \frac{1}{u1}\right)\right)}} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
      3. +-commutative87.9%

        \[\leadsto \sqrt{-{u1}^{2} \cdot \left(-\color{blue}{\left(\frac{1}{u1} + 0.5\right)}\right)} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
    10. Simplified87.9%

      \[\leadsto \sqrt{-\color{blue}{{u1}^{2} \cdot \left(-\left(\frac{1}{u1} + 0.5\right)\right)}} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
    11. Taylor expanded in u2 around inf 88.3%

      \[\leadsto \color{blue}{\left(u1 \cdot \cos \left(2 \cdot \left(u2 \cdot \pi\right)\right)\right) \cdot \sqrt{0.5 + \frac{1}{u1}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification95.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;u2 \cdot \left(2 \cdot \pi\right) \leq 0.0013500000350177288:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(u1 \cdot \cos \left(2 \cdot \left(u2 \cdot \pi\right)\right)\right) \cdot \sqrt{0.5 + \frac{1}{u1}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 80.1% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \end{array} \]
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt (- (log1p (- u1)))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-log1pf(-u1));
}
function code(cosTheta_i, u1, u2)
	return sqrt(Float32(-log1p(Float32(-u1))))
end
\begin{array}{l}

\\
\sqrt{-\mathsf{log1p}\left(-u1\right)}
\end{array}
Derivation
  1. Initial program 56.7%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg56.7%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.1%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u2 around 0 81.3%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  6. Final simplification81.3%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \]
  7. Add Preprocessing

Alternative 7: 76.4% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 - u1 \cdot -0.25\right)\right)\right)} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (sqrt
  (* u1 (+ 1.0 (* u1 (+ 0.5 (* u1 (- 0.3333333333333333 (* u1 -0.25)))))))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 * (1.0f + (u1 * (0.5f + (u1 * (0.3333333333333333f - (u1 * -0.25f))))))));
}
real(4) function code(costheta_i, u1, u2)
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt((u1 * (1.0e0 + (u1 * (0.5e0 + (u1 * (0.3333333333333333e0 - (u1 * (-0.25e0)))))))))
end function
function code(cosTheta_i, u1, u2)
	return sqrt(Float32(u1 * Float32(Float32(1.0) + Float32(u1 * Float32(Float32(0.5) + Float32(u1 * Float32(Float32(0.3333333333333333) - Float32(u1 * Float32(-0.25)))))))))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 * (single(1.0) + (u1 * (single(0.5) + (u1 * (single(0.3333333333333333) - (u1 * single(-0.25)))))))));
end
\begin{array}{l}

\\
\sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 - u1 \cdot -0.25\right)\right)\right)}
\end{array}
Derivation
  1. Initial program 56.7%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg56.7%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.1%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u2 around 0 81.3%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  6. Taylor expanded in u1 around 0 77.5%

    \[\leadsto \sqrt{-\color{blue}{u1 \cdot \left(u1 \cdot \left(u1 \cdot \left(-0.25 \cdot u1 - 0.3333333333333333\right) - 0.5\right) - 1\right)}} \cdot 1 \]
  7. Final simplification77.5%

    \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 - u1 \cdot -0.25\right)\right)\right)} \]
  8. Add Preprocessing

Alternative 8: 75.1% accurate, 2.8× speedup?

\[\begin{array}{l} \\ \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 - u1 \cdot -0.3333333333333333\right)\right)} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (sqrt (* u1 (+ 1.0 (* u1 (- 0.5 (* u1 -0.3333333333333333)))))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 * (1.0f + (u1 * (0.5f - (u1 * -0.3333333333333333f))))));
}
real(4) function code(costheta_i, u1, u2)
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt((u1 * (1.0e0 + (u1 * (0.5e0 - (u1 * (-0.3333333333333333e0)))))))
end function
function code(cosTheta_i, u1, u2)
	return sqrt(Float32(u1 * Float32(Float32(1.0) + Float32(u1 * Float32(Float32(0.5) - Float32(u1 * Float32(-0.3333333333333333)))))))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 * (single(1.0) + (u1 * (single(0.5) - (u1 * single(-0.3333333333333333)))))));
end
\begin{array}{l}

\\
\sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 - u1 \cdot -0.3333333333333333\right)\right)}
\end{array}
Derivation
  1. Initial program 56.7%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg56.7%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.1%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u2 around 0 81.3%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  6. Taylor expanded in u1 around 0 76.1%

    \[\leadsto \sqrt{-\color{blue}{u1 \cdot \left(u1 \cdot \left(-0.3333333333333333 \cdot u1 - 0.5\right) - 1\right)}} \cdot 1 \]
  7. Final simplification76.1%

    \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 - u1 \cdot -0.3333333333333333\right)\right)} \]
  8. Add Preprocessing

Alternative 9: 72.3% accurate, 2.9× speedup?

\[\begin{array}{l} \\ u1 \cdot \sqrt{0.5 + \frac{1}{u1}} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* u1 (sqrt (+ 0.5 (/ 1.0 u1)))))
float code(float cosTheta_i, float u1, float u2) {
	return u1 * sqrtf((0.5f + (1.0f / u1)));
}
real(4) function code(costheta_i, u1, u2)
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = u1 * sqrt((0.5e0 + (1.0e0 / u1)))
end function
function code(cosTheta_i, u1, u2)
	return Float32(u1 * sqrt(Float32(Float32(0.5) + Float32(Float32(1.0) / u1))))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = u1 * sqrt((single(0.5) + (single(1.0) / u1)));
end
\begin{array}{l}

\\
u1 \cdot \sqrt{0.5 + \frac{1}{u1}}
\end{array}
Derivation
  1. Initial program 56.7%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg56.7%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.1%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. add-cube-cbrt98.7%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\left(\sqrt[3]{\cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \cdot \sqrt[3]{\cos \left(\left(2 \cdot \pi\right) \cdot u2\right)}\right) \cdot \sqrt[3]{\cos \left(\left(2 \cdot \pi\right) \cdot u2\right)}\right)} \]
    2. pow398.7%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{{\left(\sqrt[3]{\cos \left(\left(2 \cdot \pi\right) \cdot u2\right)}\right)}^{3}} \]
    3. *-commutative98.7%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot {\left(\sqrt[3]{\cos \left(\color{blue}{\left(\pi \cdot 2\right)} \cdot u2\right)}\right)}^{3} \]
    4. associate-*l*98.7%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot {\left(\sqrt[3]{\cos \color{blue}{\left(\pi \cdot \left(2 \cdot u2\right)\right)}}\right)}^{3} \]
  6. Applied egg-rr98.7%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{{\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3}} \]
  7. Taylor expanded in u1 around 0 88.7%

    \[\leadsto \sqrt{-\color{blue}{u1 \cdot \left(-0.5 \cdot u1 - 1\right)}} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
  8. Taylor expanded in u1 around inf 88.6%

    \[\leadsto \sqrt{-\color{blue}{-1 \cdot \left({u1}^{2} \cdot \left(0.5 + \frac{1}{u1}\right)\right)}} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
  9. Step-by-step derivation
    1. mul-1-neg88.6%

      \[\leadsto \sqrt{-\color{blue}{\left(-{u1}^{2} \cdot \left(0.5 + \frac{1}{u1}\right)\right)}} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
    2. distribute-rgt-neg-in88.6%

      \[\leadsto \sqrt{-\color{blue}{{u1}^{2} \cdot \left(-\left(0.5 + \frac{1}{u1}\right)\right)}} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
    3. +-commutative88.6%

      \[\leadsto \sqrt{-{u1}^{2} \cdot \left(-\color{blue}{\left(\frac{1}{u1} + 0.5\right)}\right)} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
  10. Simplified88.6%

    \[\leadsto \sqrt{-\color{blue}{{u1}^{2} \cdot \left(-\left(\frac{1}{u1} + 0.5\right)\right)}} \cdot {\left(\sqrt[3]{\cos \left(\pi \cdot \left(2 \cdot u2\right)\right)}\right)}^{3} \]
  11. Taylor expanded in u2 around 0 73.8%

    \[\leadsto \color{blue}{u1 \cdot \sqrt{0.5 + \frac{1}{u1}}} \]
  12. Final simplification73.8%

    \[\leadsto u1 \cdot \sqrt{0.5 + \frac{1}{u1}} \]
  13. Add Preprocessing

Alternative 10: 72.5% accurate, 2.9× speedup?

\[\begin{array}{l} \\ \sqrt{u1 \cdot \left(1 - u1 \cdot -0.5\right)} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (sqrt (* u1 (- 1.0 (* u1 -0.5)))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 * (1.0f - (u1 * -0.5f))));
}
real(4) function code(costheta_i, u1, u2)
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt((u1 * (1.0e0 - (u1 * (-0.5e0)))))
end function
function code(cosTheta_i, u1, u2)
	return sqrt(Float32(u1 * Float32(Float32(1.0) - Float32(u1 * Float32(-0.5)))))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 * (single(1.0) - (u1 * single(-0.5)))));
end
\begin{array}{l}

\\
\sqrt{u1 \cdot \left(1 - u1 \cdot -0.5\right)}
\end{array}
Derivation
  1. Initial program 56.7%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg56.7%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.1%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u2 around 0 81.3%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  6. Taylor expanded in u1 around 0 74.0%

    \[\leadsto \sqrt{-\color{blue}{u1 \cdot \left(-0.5 \cdot u1 - 1\right)}} \cdot 1 \]
  7. Final simplification74.0%

    \[\leadsto \sqrt{u1 \cdot \left(1 - u1 \cdot -0.5\right)} \]
  8. Add Preprocessing

Alternative 11: 5.0% accurate, 3.0× speedup?

\[\begin{array}{l} \\ -\sqrt{u1} \end{array} \]
(FPCore (cosTheta_i u1 u2) :precision binary32 (- (sqrt u1)))
float code(float cosTheta_i, float u1, float u2) {
	return -sqrtf(u1);
}
real(4) function code(costheta_i, u1, u2)
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = -sqrt(u1)
end function
function code(cosTheta_i, u1, u2)
	return Float32(-sqrt(u1))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = -sqrt(u1);
end
\begin{array}{l}

\\
-\sqrt{u1}
\end{array}
Derivation
  1. Initial program 56.7%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg56.7%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.1%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u2 around 0 81.3%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  6. Taylor expanded in u1 around 0 -0.0%

    \[\leadsto \color{blue}{\left(\sqrt{u1} \cdot {\left(\sqrt{-1}\right)}^{2}\right)} \cdot 1 \]
  7. Step-by-step derivation
    1. *-commutative-0.0%

      \[\leadsto \color{blue}{\left({\left(\sqrt{-1}\right)}^{2} \cdot \sqrt{u1}\right)} \cdot 1 \]
    2. unpow2-0.0%

      \[\leadsto \left(\color{blue}{\left(\sqrt{-1} \cdot \sqrt{-1}\right)} \cdot \sqrt{u1}\right) \cdot 1 \]
    3. rem-square-sqrt5.1%

      \[\leadsto \left(\color{blue}{-1} \cdot \sqrt{u1}\right) \cdot 1 \]
    4. mul-1-neg5.1%

      \[\leadsto \color{blue}{\left(-\sqrt{u1}\right)} \cdot 1 \]
  8. Simplified5.1%

    \[\leadsto \color{blue}{\left(-\sqrt{u1}\right)} \cdot 1 \]
  9. Final simplification5.1%

    \[\leadsto -\sqrt{u1} \]
  10. Add Preprocessing

Alternative 12: 64.6% accurate, 3.1× speedup?

\[\begin{array}{l} \\ \sqrt{u1} \end{array} \]
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt u1))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(u1);
}
real(4) function code(costheta_i, u1, u2)
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt(u1)
end function
function code(cosTheta_i, u1, u2)
	return sqrt(u1)
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt(u1);
end
\begin{array}{l}

\\
\sqrt{u1}
\end{array}
Derivation
  1. Initial program 56.7%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg56.7%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.1%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u2 around 0 81.3%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  6. Taylor expanded in u1 around 0 66.0%

    \[\leadsto \sqrt{-\color{blue}{-1 \cdot u1}} \cdot 1 \]
  7. Simplified66.0%

    \[\leadsto \sqrt{-\color{blue}{\left(-u1\right)}} \cdot 1 \]
  8. Final simplification66.0%

    \[\leadsto \sqrt{u1} \]
  9. Add Preprocessing

Reproduce

?
herbie shell --seed 2024078 
(FPCore (cosTheta_i u1 u2)
  :name "Beckmann Sample, near normal, slope_x"
  :precision binary32
  :pre (and (and (and (> cosTheta_i 0.9999) (<= cosTheta_i 1.0)) (and (<= 2.328306437e-10 u1) (<= u1 1.0))) (and (<= 2.328306437e-10 u2) (<= u2 1.0)))
  (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2))))