
(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 7 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 (- (log (- 1.0 u1)))) (t_1 (* u2 (PI))))
(if (<= t_0 0.00011200000153621659)
(* (* (sin t_1) 2.0) (* (cos t_1) (sqrt u1)))
(*
(sqrt t_0)
(sin
(*
(* (* u2 (* (sqrt (PI)) 2.0)) (cbrt (PI)))
(pow (PI) 0.16666666666666666)))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
t_1 := u2 \cdot \mathsf{PI}\left(\right)\\
\mathbf{if}\;t\_0 \leq 0.00011200000153621659:\\
\;\;\;\;\left(\sin t\_1 \cdot 2\right) \cdot \left(\cos t\_1 \cdot \sqrt{u1}\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_0} \cdot \sin \left(\left(\left(u2 \cdot \left(\sqrt{\mathsf{PI}\left(\right)} \cdot 2\right)\right) \cdot \sqrt[3]{\mathsf{PI}\left(\right)}\right) \cdot {\mathsf{PI}\left(\right)}^{0.16666666666666666}\right)\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.12000002e-4Initial program 37.6%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f324.2
Applied rewrites4.2%
Applied rewrites93.5%
if 1.12000002e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 89.0%
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.f3288.9
Applied rewrites88.9%
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
lift-pow.f32N/A
pow-plusN/A
lift-cbrt.f32N/A
metadata-evalN/A
rem-cube-cbrtN/A
lift-PI.f32N/A
add-sqr-sqrtN/A
associate-*r*N/A
lift-PI.f32N/A
pow1/2N/A
metadata-evalN/A
metadata-evalN/A
pow-prod-upN/A
pow1/3N/A
lift-cbrt.f32N/A
associate-*r*N/A
Applied rewrites89.1%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (- (log (- 1.0 u1)))) (t_1 (* u2 (PI))))
(if (<= t_0 0.00011200000153621659)
(* (* (sin t_1) 2.0) (* (cos t_1) (sqrt u1)))
(* (sqrt t_0) (sin (* (PI) (+ u2 u2)))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
t_1 := u2 \cdot \mathsf{PI}\left(\right)\\
\mathbf{if}\;t\_0 \leq 0.00011200000153621659:\\
\;\;\;\;\left(\sin t\_1 \cdot 2\right) \cdot \left(\cos t\_1 \cdot \sqrt{u1}\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_0} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.12000002e-4Initial program 37.6%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f324.2
Applied rewrites4.2%
Applied rewrites93.5%
if 1.12000002e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 89.0%
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.f3288.9
Applied rewrites88.9%
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
lift-*.f32N/A
*-commutativeN/A
lift-pow.f32N/A
pow-plusN/A
lift-cbrt.f32N/A
metadata-evalN/A
rem-cube-cbrtN/A
associate-*r*N/A
lift-*.f32N/A
count-2N/A
lift-*.f32N/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3289.0
Applied rewrites89.0%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (- (log (- 1.0 u1)))) (t_1 (* u2 (PI))))
(if (<= t_0 0.00011200000153621659)
(* (* (cos t_1) 2.0) (* (sin t_1) (sqrt u1)))
(* (sqrt t_0) (sin (* (PI) (+ u2 u2)))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
t_1 := u2 \cdot \mathsf{PI}\left(\right)\\
\mathbf{if}\;t\_0 \leq 0.00011200000153621659:\\
\;\;\;\;\left(\cos t\_1 \cdot 2\right) \cdot \left(\sin t\_1 \cdot \sqrt{u1}\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_0} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.12000002e-4Initial program 37.6%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f324.2
Applied rewrites4.2%
Applied rewrites93.3%
if 1.12000002e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 89.0%
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.f3288.9
Applied rewrites88.9%
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
lift-*.f32N/A
*-commutativeN/A
lift-pow.f32N/A
pow-plusN/A
lift-cbrt.f32N/A
metadata-evalN/A
rem-cube-cbrtN/A
associate-*r*N/A
lift-*.f32N/A
count-2N/A
lift-*.f32N/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3289.0
Applied rewrites89.0%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (- (log (- 1.0 u1)))))
(if (<= t_0 0.00011200000153621659)
(* (sin (* (* u2 2.0) (PI))) (sqrt u1))
(* (sqrt t_0) (sin (* (PI) (+ u2 u2)))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
\mathbf{if}\;t\_0 \leq 0.00011200000153621659:\\
\;\;\;\;\sin \left(\left(u2 \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{u1}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_0} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.12000002e-4Initial program 37.6%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f324.2
Applied rewrites4.2%
lift-*.f32N/A
*-commutativeN/A
lower-*.f324.2
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*r*N/A
lift-*.f32N/A
lower-*.f324.2
Applied rewrites93.3%
if 1.12000002e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 89.0%
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.f3288.9
Applied rewrites88.9%
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
lift-*.f32N/A
*-commutativeN/A
lift-pow.f32N/A
pow-plusN/A
lift-cbrt.f32N/A
metadata-evalN/A
rem-cube-cbrtN/A
associate-*r*N/A
lift-*.f32N/A
count-2N/A
lift-*.f32N/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3289.0
Applied rewrites89.0%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sin (* (* u2 2.0) (PI))) (sqrt u1)))
\begin{array}{l}
\\
\sin \left(\left(u2 \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{u1}
\end{array}
Initial program 58.9%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f324.0
Applied rewrites4.0%
lift-*.f32N/A
*-commutativeN/A
lower-*.f324.0
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*r*N/A
lift-*.f32N/A
lower-*.f324.0
Applied rewrites76.6%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (* (* (PI) u2) 2.0) (sqrt (* (fma (fma 0.3333333333333333 u1 -0.5) u1 1.0) u1))))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot u2\right) \cdot 2\right) \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, -0.5\right), u1, 1\right) \cdot u1}
\end{array}
Initial program 58.9%
Applied rewrites13.5%
Taylor expanded in u1 around 0
associate-*r*N/A
distribute-rgt-outN/A
+-commutativeN/A
lower-*.f32N/A
Applied rewrites76.6%
Taylor expanded in u2 around 0
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f32N/A
lower-log1p.f3267.3
Applied rewrites67.3%
Taylor expanded in u1 around 0
Applied rewrites67.5%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (* (* 2.0 (sqrt u1)) (PI)) u2))
\begin{array}{l}
\\
\left(\left(2 \cdot \sqrt{u1}\right) \cdot \mathsf{PI}\left(\right)\right) \cdot u2
\end{array}
Initial program 58.9%
Applied rewrites12.8%
Taylor expanded in u1 around 0
associate-*r*N/A
distribute-rgt-outN/A
+-commutativeN/A
lower-*.f32N/A
Applied rewrites76.6%
Taylor expanded in u2 around 0
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f32N/A
lower-log1p.f3267.3
Applied rewrites67.3%
Taylor expanded in u1 around 0
Applied rewrites67.4%
herbie shell --seed 2024318
(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))))