
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (log (- 1.0 u1)))) (sin (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (log (- 1.0 u1)))) (sin (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cbrt (PI))) (t_1 (pow t_0 2.0)) (t_2 (* u2 (PI))))
(if (<= (- 1.0 u1) 0.9998400211334229)
(*
(sin (* (* (* (* u2 t_1) 2.0) (cbrt t_0)) (cbrt t_1)))
(sqrt (- (log (- 1.0 u1)))))
(* (sqrt u1) (* (* (cos t_2) (sin t_2)) 2.0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\mathsf{PI}\left(\right)}\\
t_1 := {t\_0}^{2}\\
t_2 := u2 \cdot \mathsf{PI}\left(\right)\\
\mathbf{if}\;1 - u1 \leq 0.9998400211334229:\\
\;\;\;\;\sin \left(\left(\left(\left(u2 \cdot t\_1\right) \cdot 2\right) \cdot \sqrt[3]{t\_0}\right) \cdot \sqrt[3]{t\_1}\right) \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{u1} \cdot \left(\left(\cos t\_2 \cdot \sin t\_2\right) \cdot 2\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999840021Initial program 89.7%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*r*N/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lift-PI.f32N/A
lower-cbrt.f3289.6
Applied rewrites89.6%
lift-*.f32N/A
*-commutativeN/A
rem-cbrt-cubeN/A
cube-unmultN/A
unpow2N/A
lift-pow.f32N/A
cbrt-prodN/A
pow1/3N/A
pow1/3N/A
*-commutativeN/A
associate-*l*N/A
lower-*.f32N/A
pow1/3N/A
lower-cbrt.f32N/A
lower-*.f32N/A
Applied rewrites89.7%
if 0.999840021 < (-.f32 #s(literal 1 binary32) u1) Initial program 39.4%
Applied rewrites39.9%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3291.9
Applied rewrites91.9%
Applied rewrites91.9%
Final simplification90.9%
(FPCore (cosTheta_i u1 u2) :precision binary32 (let* ((t_0 (- (log (- 1.0 u1)))) (t_1 (sin (* (* 2.0 (PI)) u2)))) (if (<= t_0 0.00015999999595806003) (* t_1 (sqrt u1)) (* t_1 (sqrt t_0)))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
t_1 := \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \leq 0.00015999999595806003:\\
\;\;\;\;t\_1 \cdot \sqrt{u1}\\
\mathbf{else}:\\
\;\;\;\;t\_1 \cdot \sqrt{t\_0}\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.59999996e-4Initial program 39.4%
Applied rewrites42.2%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3291.9
Applied rewrites91.9%
if 1.59999996e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 89.7%
Final simplification90.9%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (* u2 (PI))))
(if (<= (- 1.0 u1) 0.9998400211334229)
(* (sin (* (* 2.0 (PI)) u2)) (sqrt (- (log (- 1.0 u1)))))
(* (sqrt u1) (* (* (cos t_0) (sin t_0)) 2.0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := u2 \cdot \mathsf{PI}\left(\right)\\
\mathbf{if}\;1 - u1 \leq 0.9998400211334229:\\
\;\;\;\;\sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{u1} \cdot \left(\left(\cos t\_0 \cdot \sin t\_0\right) \cdot 2\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999840021Initial program 89.7%
if 0.999840021 < (-.f32 #s(literal 1 binary32) u1) Initial program 39.4%
Applied rewrites41.2%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3291.9
Applied rewrites91.9%
Applied rewrites91.9%
Final simplification90.9%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= (- 1.0 u1) 0.9800000190734863)
(*
(* (* (fma (* -1.3333333333333333 (* u2 u2)) (* (PI) (PI)) 2.0) (PI)) u2)
(sqrt (- (log (- 1.0 u1)))))
(* (sin (* (* 2.0 (PI)) u2)) (sqrt u1))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;1 - u1 \leq 0.9800000190734863:\\
\;\;\;\;\left(\left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right), 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot \sqrt{u1}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.980000019Initial program 97.0%
Taylor expanded in u2 around 0
*-commutativeN/A
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
Applied rewrites86.6%
if 0.980000019 < (-.f32 #s(literal 1 binary32) u1) Initial program 53.4%
Applied rewrites39.3%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3282.4
Applied rewrites82.4%
Final simplification83.3%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sin (* (* 2.0 (PI)) u2)) (sqrt u1)))
\begin{array}{l}
\\
\sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot \sqrt{u1}
\end{array}
Initial program 62.8%
Applied rewrites37.6%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3273.3
Applied rewrites73.3%
Final simplification73.3%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (* (+ u2 u2) (PI)) (sqrt u1)))
\begin{array}{l}
\\
\left(\left(u2 + u2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{u1}
\end{array}
Initial program 62.8%
Applied rewrites37.8%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3273.3
Applied rewrites73.3%
Taylor expanded in u2 around 0
Applied rewrites63.9%
Applied rewrites63.9%
Final simplification63.9%
herbie shell --seed 2024295
(FPCore (cosTheta_i u1 u2)
:name "Beckmann Sample, near normal, slope_y"
: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)))) (sin (* (* 2.0 (PI)) u2))))