
(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 12 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 (* (* (sqrt (- (log1p (- u1)))) 2.0) (/ (+ (sin (* (PI) (+ u2 u2))) (sin (fma (PI) u2 (* (- (PI)) u2)))) 2.0)))
\begin{array}{l}
\\
\left(\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot 2\right) \cdot \frac{\sin \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right) + \sin \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), u2, \left(-\mathsf{PI}\left(\right)\right) \cdot u2\right)\right)}{2}
\end{array}
Initial program 55.5%
lift-*.f32N/A
lift-sin.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
sin-2N/A
associate-*r*N/A
lower-*.f32N/A
Applied rewrites98.5%
lift-*.f32N/A
*-commutativeN/A
lift-sin.f32N/A
lift-cos.f32N/A
cos-neg-revN/A
sin-cos-multN/A
lower-/.f32N/A
Applied rewrites98.6%
lift--.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
fp-cancel-sign-subN/A
lift-*.f32N/A
distribute-lft-outN/A
lower-*.f32N/A
lower-+.f3298.6
Applied rewrites98.6%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= u1 0.03799999877810478)
(*
(sqrt (* (fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 1.0) u1))
(sin (* (* 2.0 (PI)) u2)))
(* (sqrt (- (log (- 1.0 u1)))) (sin (* (PI) (+ u2 u2))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;u1 \leq 0.03799999877810478:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)\\
\end{array}
\end{array}
if u1 < 0.0379999988Initial program 47.8%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
lower-fma.f3298.5
Applied rewrites98.5%
if 0.0379999988 < u1 Initial program 97.5%
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
count-2-revN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
lower-fma.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
*-commutativeN/A
lower-*.f3297.6
Applied rewrites97.6%
lift-fma.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*r*N/A
*-commutativeN/A
lift-pow.f32N/A
unpow2N/A
cube-unmultN/A
lift-cbrt.f32N/A
rem-cube-cbrtN/A
distribute-lft-outN/A
lower-*.f32N/A
lower-+.f3297.5
Applied rewrites97.5%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (log1p (- u1)))) (sin (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 55.5%
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f3298.5
Applied rewrites98.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= u1 0.03799999877810478)
(*
(sqrt (* (fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 1.0) u1))
(sin (* (* 2.0 (PI)) u2)))
(*
(sqrt (- (log (- 1.0 u1))))
(*
(+
(fma (* (* u2 u2) -1.3333333333333333) (* (* (PI) (PI)) (PI)) (PI))
(PI))
u2))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;u1 \leq 0.03799999877810478:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \left(\left(\mathsf{fma}\left(\left(u2 \cdot u2\right) \cdot -1.3333333333333333, \left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \mathsf{PI}\left(\right), \mathsf{PI}\left(\right)\right) + \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\end{array}
\end{array}
if u1 < 0.0379999988Initial program 47.8%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
lower-fma.f3298.5
Applied rewrites98.5%
if 0.0379999988 < u1 Initial program 97.5%
Taylor expanded in u2 around 0
*-commutativeN/A
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
Applied rewrites90.6%
Applied rewrites90.6%
Final simplification97.3%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= u2 0.00022000000171829015)
(* (* (sqrt (- (log1p (- u1)))) 2.0) (/ (* (* (PI) u2) 2.0) 2.0))
(*
(sqrt (* (fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 1.0) u1))
(sin (* (* 2.0 (PI)) u2)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;u2 \leq 0.00022000000171829015:\\
\;\;\;\;\left(\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot 2\right) \cdot \frac{\left(\mathsf{PI}\left(\right) \cdot u2\right) \cdot 2}{2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\end{array}
\end{array}
if u2 < 2.20000002e-4Initial program 56.4%
lift-*.f32N/A
lift-sin.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
sin-2N/A
associate-*r*N/A
lower-*.f32N/A
Applied rewrites98.8%
lift-*.f32N/A
*-commutativeN/A
lift-sin.f32N/A
lift-cos.f32N/A
cos-neg-revN/A
sin-cos-multN/A
lower-/.f32N/A
Applied rewrites98.7%
Taylor expanded in u2 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3298.4
Applied rewrites98.4%
if 2.20000002e-4 < u2 Initial program 53.9%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
lower-fma.f3293.7
Applied rewrites93.7%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= u2 0.00022000000171829015)
(* (* (sqrt (- (log1p (- u1)))) 2.0) (/ (* (* (PI) u2) 2.0) 2.0))
(*
(sqrt (* (fma (fma 0.3333333333333333 u1 0.5) u1 1.0) u1))
(sin (* (* 2.0 (PI)) u2)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;u2 \leq 0.00022000000171829015:\\
\;\;\;\;\left(\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot 2\right) \cdot \frac{\left(\mathsf{PI}\left(\right) \cdot u2\right) \cdot 2}{2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\end{array}
\end{array}
if u2 < 2.20000002e-4Initial program 56.4%
lift-*.f32N/A
lift-sin.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
sin-2N/A
associate-*r*N/A
lower-*.f32N/A
Applied rewrites98.8%
lift-*.f32N/A
*-commutativeN/A
lift-sin.f32N/A
lift-cos.f32N/A
cos-neg-revN/A
sin-cos-multN/A
lower-/.f32N/A
Applied rewrites98.7%
Taylor expanded in u2 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3298.4
Applied rewrites98.4%
if 2.20000002e-4 < u2 Initial program 53.9%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
lower-fma.f3292.3
Applied rewrites92.3%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (* (fma (fma 0.3333333333333333 u1 0.5) u1 1.0) u1)) (sin (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 55.5%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
lower-fma.f3291.6
Applied rewrites91.6%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (* (fma 0.5 u1 1.0) u1)) (sin (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(0.5, u1, 1\right) \cdot u1} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 55.5%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3288.7
Applied rewrites88.7%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (/ 1.0 u1))))
(if (<= u2 0.0012499999720603228)
(*
(fma
(fma
(fma
(* (- 0.25 (/ 0.0625 u1)) (sqrt u1))
0.5
(* 0.16666666666666666 t_0))
u1
(* t_0 0.25))
(* u1 u1)
(sqrt u1))
(* (* (PI) u2) 2.0))
(* (sqrt u1) (sin (* (* 2.0 (PI)) u2))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{1}{u1}}\\
\mathbf{if}\;u2 \leq 0.0012499999720603228:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\left(0.25 - \frac{0.0625}{u1}\right) \cdot \sqrt{u1}, 0.5, 0.16666666666666666 \cdot t\_0\right), u1, t\_0 \cdot 0.25\right), u1 \cdot u1, \sqrt{u1}\right) \cdot \left(\left(\mathsf{PI}\left(\right) \cdot u2\right) \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{u1} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\end{array}
\end{array}
if u2 < 0.00124999997Initial program 57.7%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
pow2N/A
lower-pow.f32N/A
lower-pow.f32N/A
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f32N/A
metadata-eval98.0
Applied rewrites98.0%
Taylor expanded in u1 around 0
lower-sqrt.f3276.9
Applied rewrites76.9%
Taylor expanded in u2 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3276.6
Applied rewrites76.6%
Taylor expanded in u1 around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites92.0%
if 0.00124999997 < u2 Initial program 50.2%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
pow2N/A
lower-pow.f32N/A
lower-pow.f32N/A
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f32N/A
metadata-eval97.6
Applied rewrites97.6%
Taylor expanded in u1 around 0
lower-sqrt.f3282.6
Applied rewrites82.6%
Final simplification89.3%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= u2 0.0012499999720603228)
(*
(fma
(fma (sqrt (/ 1.0 u1)) 0.25 (* 0.16666666666666666 (sqrt u1)))
(* u1 u1)
(sqrt u1))
(* (* (PI) u2) 2.0))
(* (sqrt u1) (sin (* (* 2.0 (PI)) u2)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;u2 \leq 0.0012499999720603228:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\sqrt{\frac{1}{u1}}, 0.25, 0.16666666666666666 \cdot \sqrt{u1}\right), u1 \cdot u1, \sqrt{u1}\right) \cdot \left(\left(\mathsf{PI}\left(\right) \cdot u2\right) \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{u1} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\end{array}
\end{array}
if u2 < 0.00124999997Initial program 57.7%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
pow2N/A
lower-pow.f32N/A
lower-pow.f32N/A
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f32N/A
metadata-eval98.0
Applied rewrites98.0%
Taylor expanded in u1 around 0
lower-sqrt.f3276.9
Applied rewrites76.9%
Taylor expanded in u2 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3276.6
Applied rewrites76.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.7
Applied rewrites89.7%
if 0.00124999997 < u2 Initial program 50.2%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
pow2N/A
lower-pow.f32N/A
lower-pow.f32N/A
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f32N/A
metadata-eval97.6
Applied rewrites97.6%
Taylor expanded in u1 around 0
lower-sqrt.f3282.6
Applied rewrites82.6%
Final simplification87.7%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (fma (fma (sqrt (/ 1.0 u1)) 0.25 (* 0.16666666666666666 (sqrt u1))) (* u1 u1) (sqrt u1)) (* (* (PI) u2) 2.0)))
\begin{array}{l}
\\
\mathsf{fma}\left(\mathsf{fma}\left(\sqrt{\frac{1}{u1}}, 0.25, 0.16666666666666666 \cdot \sqrt{u1}\right), u1 \cdot u1, \sqrt{u1}\right) \cdot \left(\left(\mathsf{PI}\left(\right) \cdot u2\right) \cdot 2\right)
\end{array}
Initial program 55.5%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
pow2N/A
lower-pow.f32N/A
lower-pow.f32N/A
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f32N/A
metadata-eval97.9
Applied rewrites97.9%
Taylor expanded in u1 around 0
lower-sqrt.f3278.5
Applied rewrites78.5%
Taylor expanded in u2 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3267.7
Applied rewrites67.7%
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.f3277.4
Applied rewrites77.4%
Final simplification77.4%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt u1) (* (* (PI) u2) 2.0)))
\begin{array}{l}
\\
\sqrt{u1} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot u2\right) \cdot 2\right)
\end{array}
Initial program 55.5%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
pow2N/A
lower-pow.f32N/A
lower-pow.f32N/A
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f32N/A
metadata-eval97.9
Applied rewrites97.9%
Taylor expanded in u1 around 0
lower-sqrt.f3278.5
Applied rewrites78.5%
Taylor expanded in u2 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3267.7
Applied rewrites67.7%
Final simplification67.7%
herbie shell --seed 2024350
(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))))