
(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 (* u2 (PI))))
(if (<= (- 1.0 u1) 0.9998599886894226)
(*
(* (sqrt (- (log (- 1.0 u1)) (log (pow (- 1.0 u1) 2.0)))) 2.0)
(* (cos t_0) (sin t_0)))
(* (/ u1 (sqrt u1)) (sin (* (* 2.0 (PI)) u2))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := u2 \cdot \mathsf{PI}\left(\right)\\
\mathbf{if}\;1 - u1 \leq 0.9998599886894226:\\
\;\;\;\;\left(\sqrt{\log \left(1 - u1\right) - \log \left({\left(1 - u1\right)}^{2}\right)} \cdot 2\right) \cdot \left(\cos t\_0 \cdot \sin t\_0\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{u1}{\sqrt{u1}} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999859989Initial program 88.5%
Applied rewrites32.1%
Applied rewrites46.1%
Taylor expanded in u1 around 0
mul-1-negN/A
sub-negN/A
lower--.f3288.7
Applied rewrites88.7%
if 0.999859989 < (-.f32 #s(literal 1 binary32) u1) Initial program 37.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.2
Applied rewrites4.2%
Applied rewrites91.7%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (- (log (- 1.0 u1)))) (t_1 (* u2 (PI))))
(if (<= t_0 0.0001221999991685152)
(* (/ u1 (sqrt u1)) (sin (* (* 2.0 (PI)) u2)))
(* (sqrt t_0) (* (* (cos t_1) (sin t_1)) 2.0)))))\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.0001221999991685152:\\
\;\;\;\;\frac{u1}{\sqrt{u1}} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_0} \cdot \left(\left(\cos t\_1 \cdot \sin t\_1\right) \cdot 2\right)\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.222e-4Initial program 36.7%
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 rewrites92.4%
if 1.222e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 87.9%
lift-sin.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
sin-2N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f3287.9
Applied rewrites87.9%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (- (log (- 1.0 u1)))))
(if (<= t_0 0.0001221999991685152)
(* (/ u1 (sqrt u1)) (sin (* (* 2.0 (PI)) u2)))
(* (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.0001221999991685152:\\
\;\;\;\;\frac{u1}{\sqrt{u1}} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\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.222e-4Initial program 36.7%
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 rewrites92.4%
if 1.222e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 87.9%
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.f3287.5
Applied rewrites87.5%
lift-*.f32N/A
lift-*.f32N/A
associate-*r*N/A
lift-*.f32N/A
associate-*r*N/A
lift-pow.f32N/A
pow-plusN/A
lift-cbrt.f32N/A
metadata-evalN/A
rem-cube-cbrtN/A
*-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
count-2N/A
lift-*.f32N/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3287.9
Applied rewrites87.9%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (/ u1 (sqrt u1)) (sin (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\frac{u1}{\sqrt{u1}} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 57.7%
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%
Applied rewrites76.5%
(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 57.7%
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%
Applied rewrites76.5%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (* (* (PI) u2) 2.0) (sqrt u1)))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot u2\right) \cdot 2\right) \cdot \sqrt{u1}
\end{array}
Initial program 57.7%
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%
Applied rewrites76.5%
Taylor expanded in u2 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3266.1
Applied rewrites66.1%
herbie shell --seed 2024314
(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))))