
(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 10 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 (- (log (- 1.0 u1)))))
(if (<= t_1 0.0031999999191612005)
(*
(sin (* (* (* u2 t_0) 2.0) (pow t_0 2.0)))
(sqrt (- (* (/ (- 1.0 (* 0.25 (* u1 u1))) (- -1.0 (* -0.5 u1))) u1))))
(* (sin (* (* 2.0 (PI)) u2)) (sqrt t_1)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\mathsf{PI}\left(\right)}\\
t_1 := -\log \left(1 - u1\right)\\
\mathbf{if}\;t\_1 \leq 0.0031999999191612005:\\
\;\;\;\;\sin \left(\left(\left(u2 \cdot t\_0\right) \cdot 2\right) \cdot {t\_0}^{2}\right) \cdot \sqrt{-\frac{1 - 0.25 \cdot \left(u1 \cdot u1\right)}{-1 - -0.5 \cdot u1} \cdot u1}\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot \sqrt{t\_1}\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 0.00319999992Initial program 45.1%
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
associate-*l*N/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f3245.0
Applied rewrites45.0%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3225.9
Applied rewrites25.1%
Applied rewrites96.8%
if 0.00319999992 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 94.0%
Final simplification96.2%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (- (log (- 1.0 u1)))) (t_1 (cbrt (PI))))
(if (<= t_0 0.0031999999191612005)
(*
(sqrt (- (* (+ (* -0.5 u1) -1.0) u1)))
(sin (* (* (* u2 t_1) 2.0) (pow t_1 2.0))))
(* (sin (* (* 2.0 (PI)) u2)) (sqrt t_0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
t_1 := \sqrt[3]{\mathsf{PI}\left(\right)}\\
\mathbf{if}\;t\_0 \leq 0.0031999999191612005:\\
\;\;\;\;\sqrt{-\left(-0.5 \cdot u1 + -1\right) \cdot u1} \cdot \sin \left(\left(\left(u2 \cdot t\_1\right) \cdot 2\right) \cdot {t\_1}^{2}\right)\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot \sqrt{t\_0}\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 0.00319999992Initial program 45.1%
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
associate-*l*N/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f3245.0
Applied rewrites45.0%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3225.6
Applied rewrites25.9%
Applied rewrites96.6%
if 0.00319999992 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 94.0%
Final simplification96.0%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (fma (sqrt u1) 0.16666666666666666 (/ 0.25 (sqrt u1))))
(t_1 (sin (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.9991999864578247)
(* t_1 (sqrt (- (log (- 1.0 u1)))))
(*
(*
(/ 1.0 (- (* t_0 (* u1 u1)) (sqrt u1)))
(- (* (pow u1 4.0) (pow t_0 2.0)) u1))
t_1))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\sqrt{u1}, 0.16666666666666666, \frac{0.25}{\sqrt{u1}}\right)\\
t_1 := \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;1 - u1 \leq 0.9991999864578247:\\
\;\;\;\;t\_1 \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\left(\frac{1}{t\_0 \cdot \left(u1 \cdot u1\right) - \sqrt{u1}} \cdot \left({u1}^{4} \cdot {t\_0}^{2} - u1\right)\right) \cdot t\_1\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999199986Initial program 91.3%
if 0.999199986 < (-.f32 #s(literal 1 binary32) u1) Initial program 41.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3290.7
Applied rewrites90.7%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
lower-*.f32N/A
lower-pow.f32N/A
metadata-evalN/A
lower-pow.f32N/A
metadata-eval90.5
Applied rewrites90.5%
Taylor expanded in u1 around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-*.f32N/A
lower-sqrt.f32N/A
unpow2N/A
lower-*.f32N/A
lower-sqrt.f3290.7
Applied rewrites90.7%
Applied rewrites98.2%
Final simplification95.1%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= (- (log (- 1.0 u1))) 0.0016499999910593033)
(*
(+
(* (fma (sqrt u1) 0.16666666666666666 (/ 0.25 (sqrt u1))) (* u1 u1))
(sqrt u1))
(sin (* (* 2.0 (PI)) u2)))
(*
(sqrt (log (sqrt (/ 1.0 (- 1.0 u1)))))
(* (* (* (sqrt 2.0) (PI)) 2.0) u2))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-\log \left(1 - u1\right) \leq 0.0016499999910593033:\\
\;\;\;\;\left(\mathsf{fma}\left(\sqrt{u1}, 0.16666666666666666, \frac{0.25}{\sqrt{u1}}\right) \cdot \left(u1 \cdot u1\right) + \sqrt{u1}\right) \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\log \left(\sqrt{\frac{1}{1 - u1}}\right)} \cdot \left(\left(\left(\sqrt{2} \cdot \mathsf{PI}\left(\right)\right) \cdot 2\right) \cdot u2\right)\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 0.00165Initial program 42.4%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3289.8
Applied rewrites89.8%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
lower-*.f32N/A
lower-pow.f32N/A
metadata-evalN/A
lower-pow.f32N/A
metadata-eval89.6
Applied rewrites89.6%
Taylor expanded in u1 around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-*.f32N/A
lower-sqrt.f32N/A
unpow2N/A
lower-*.f32N/A
lower-sqrt.f3289.8
Applied rewrites89.8%
Applied rewrites98.1%
if 0.00165 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 92.0%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
inv-powN/A
sqr-powN/A
log-prodN/A
lower-+.f32N/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-evalN/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-eval87.6
Applied rewrites87.6%
Taylor expanded in u2 around 0
*-commutativeN/A
associate-*l*N/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sqrt.f32N/A
lower-PI.f32N/A
Applied rewrites77.9%
Final simplification90.9%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= (- (log (- 1.0 u1))) 0.0016199999954551458)
(* (sqrt u1) (sin (* (* 2.0 (PI)) u2)))
(*
(sqrt (log (sqrt (/ 1.0 (- 1.0 u1)))))
(* (* (* (sqrt 2.0) (PI)) 2.0) u2))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-\log \left(1 - u1\right) \leq 0.0016199999954551458:\\
\;\;\;\;\sqrt{u1} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\log \left(\sqrt{\frac{1}{1 - u1}}\right)} \cdot \left(\left(\left(\sqrt{2} \cdot \mathsf{PI}\left(\right)\right) \cdot 2\right) \cdot u2\right)\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 0.00162Initial program 42.1%
Applied rewrites40.7%
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.f3289.9
Applied rewrites89.9%
if 0.00162 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 92.0%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
inv-powN/A
sqr-powN/A
log-prodN/A
lower-+.f32N/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-evalN/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-eval87.4
Applied rewrites87.4%
Taylor expanded in u2 around 0
*-commutativeN/A
associate-*l*N/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sqrt.f32N/A
lower-PI.f32N/A
Applied rewrites77.8%
Final simplification86.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (- (log (- 1.0 u1)))))
(if (<= t_0 0.019999999552965164)
(* (sqrt u1) (sin (* (* 2.0 (PI)) u2)))
(*
(* (* (fma (* -1.3333333333333333 (* u2 u2)) (* (PI) (PI)) 2.0) (PI)) u2)
(sqrt t_0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
\mathbf{if}\;t\_0 \leq 0.019999999552965164:\\
\;\;\;\;\sqrt{u1} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\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{t\_0}\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 0.0199999996Initial program 48.7%
Applied rewrites41.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.f3285.1
Applied rewrites85.1%
if 0.0199999996 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 96.6%
Taylor expanded in u2 around 0
*-commutativeN/A
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
Applied rewrites85.1%
Final simplification75.2%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sin (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.9991999864578247)
(* t_0 (sqrt (- (log (- 1.0 u1)))))
(*
(+
(* (fma (sqrt u1) 0.16666666666666666 (/ 0.25 (sqrt u1))) (* u1 u1))
(sqrt u1))
t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;1 - u1 \leq 0.9991999864578247:\\
\;\;\;\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\left(\mathsf{fma}\left(\sqrt{u1}, 0.16666666666666666, \frac{0.25}{\sqrt{u1}}\right) \cdot \left(u1 \cdot u1\right) + \sqrt{u1}\right) \cdot t\_0\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999199986Initial program 91.3%
if 0.999199986 < (-.f32 #s(literal 1 binary32) u1) Initial program 41.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3290.7
Applied rewrites90.7%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
lower-*.f32N/A
lower-pow.f32N/A
metadata-evalN/A
lower-pow.f32N/A
metadata-eval90.5
Applied rewrites90.5%
Taylor expanded in u1 around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-*.f32N/A
lower-sqrt.f32N/A
unpow2N/A
lower-*.f32N/A
lower-sqrt.f3290.7
Applied rewrites90.7%
Applied rewrites98.2%
Final simplification95.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt u1) (sin (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{u1} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 56.3%
Applied rewrites39.1%
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.f3278.0
Applied rewrites78.0%
Final simplification78.0%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (* (* (sqrt u1) u2) (PI)) 2.0))
\begin{array}{l}
\\
\left(\left(\sqrt{u1} \cdot u2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot 2
\end{array}
Initial program 56.3%
Applied rewrites37.4%
Taylor expanded in u2 around 0
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-*.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.f32N/A
lower-log1p.f3269.1
Applied rewrites69.1%
Taylor expanded in u1 around 0
Applied rewrites69.5%
Applied rewrites69.5%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (* u2 (PI)) (* (sqrt u1) 2.0)))
\begin{array}{l}
\\
\left(u2 \cdot \mathsf{PI}\left(\right)\right) \cdot \left(\sqrt{u1} \cdot 2\right)
\end{array}
Initial program 56.3%
Applied rewrites37.7%
Taylor expanded in u2 around 0
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-*.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.f32N/A
lower-log1p.f3269.1
Applied rewrites69.1%
Taylor expanded in u1 around 0
Applied rewrites69.5%
Final simplification69.5%
herbie shell --seed 2024304
(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))))