
(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 15 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) (log1p (* (- u1) u1)))) (sin (* (* (PI) 2.0) u2))))
\begin{array}{l}
\\
\sqrt{\mathsf{log1p}\left(u1\right) - \mathsf{log1p}\left(\left(-u1\right) \cdot u1\right)} \cdot \sin \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)
\end{array}
Initial program 55.3%
lift-log.f32N/A
lift--.f32N/A
flip--N/A
log-divN/A
lower--.f32N/A
metadata-evalN/A
fp-cancel-sub-sign-invN/A
lower-log1p.f32N/A
lower-*.f32N/A
lower-neg.f32N/A
lower-log1p.f3298.6
Applied rewrites98.6%
Taylor expanded in u2 around inf
Applied rewrites98.6%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (log (- 1.0 u1))))
(if (<= t_0 -0.0560000017285347)
(* (sqrt (- t_0)) (* (* (PI) 2.0) u2))
(*
(sqrt (fma (* (fma 0.3333333333333333 u1 0.5) u1) u1 u1))
(sin (* (* 2.0 (PI)) u2))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \log \left(1 - u1\right)\\
\mathbf{if}\;t\_0 \leq -0.0560000017285347:\\
\;\;\;\;\sqrt{-t\_0} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right) \cdot u1, u1, u1\right)} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\end{array}
\end{array}
if (log.f32 (-.f32 #s(literal 1 binary32) u1)) < -0.0560000017Initial program 97.9%
Taylor expanded in u2 around 0
Applied rewrites85.4%
if -0.0560000017 < (log.f32 (-.f32 #s(literal 1 binary32) u1)) Initial program 49.5%
Taylor expanded in u1 around 0
Applied rewrites98.3%
Applied rewrites98.4%
Taylor expanded in u1 around 0
Applied rewrites97.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (log (- 1.0 u1))))
(if (<= t_0 -0.009999999776482582)
(* (sqrt (- t_0)) (* (* (PI) 2.0) u2))
(* (sqrt (fma (* 0.5 u1) u1 u1)) (sin (* (* 2.0 (PI)) u2))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \log \left(1 - u1\right)\\
\mathbf{if}\;t\_0 \leq -0.009999999776482582:\\
\;\;\;\;\sqrt{-t\_0} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(0.5 \cdot u1, u1, u1\right)} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\end{array}
\end{array}
if (log.f32 (-.f32 #s(literal 1 binary32) u1)) < -0.00999999978Initial program 96.2%
Taylor expanded in u2 around 0
Applied rewrites84.2%
if -0.00999999978 < (log.f32 (-.f32 #s(literal 1 binary32) u1)) Initial program 46.1%
Taylor expanded in u1 around 0
Applied rewrites97.1%
Applied rewrites97.1%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (log (- 1.0 u1))))
(if (<= t_0 -0.009999999776482582)
(* (sqrt (- t_0)) (* (* (PI) 2.0) u2))
(* (sqrt (* (fma 0.5 u1 1.0) u1)) (sin (* (* 2.0 (PI)) u2))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \log \left(1 - u1\right)\\
\mathbf{if}\;t\_0 \leq -0.009999999776482582:\\
\;\;\;\;\sqrt{-t\_0} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\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}
\end{array}
if (log.f32 (-.f32 #s(literal 1 binary32) u1)) < -0.00999999978Initial program 96.2%
Taylor expanded in u2 around 0
Applied rewrites84.2%
if -0.00999999978 < (log.f32 (-.f32 #s(literal 1 binary32) u1)) Initial program 46.1%
Taylor expanded in u1 around 0
Applied rewrites97.1%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sin (* (* 2.0 (PI)) u2))))
(if (<= u1 0.03500000014901161)
(*
(sqrt (fma (* (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1) u1 u1))
t_0)
(* (sqrt (- (log (- 1.0 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}\;u1 \leq 0.03500000014901161:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1, u1, u1\right)} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\
\end{array}
\end{array}
if u1 < 0.0350000001Initial program 48.4%
Taylor expanded in u1 around 0
Applied rewrites98.5%
Applied rewrites98.5%
if 0.0350000001 < u1 Initial program 97.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(*
(sqrt
(/
(*
(-
(*
(fma 0.3333333333333333 u1 0.5)
(* u1 (* (fma 0.3333333333333333 u1 0.5) u1)))
1.0)
u1)
(fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 -1.0)))
(sin (* (* 2.0 (PI)) u2))))\begin{array}{l}
\\
\sqrt{\frac{\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right) \cdot \left(u1 \cdot \left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right) \cdot u1\right)\right) - 1\right) \cdot u1}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, -1\right)}} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 55.3%
Taylor expanded in u1 around 0
Applied rewrites94.4%
Applied rewrites94.4%
Taylor expanded in u1 around 0
Applied rewrites94.9%
Applied rewrites94.9%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(*
(sqrt
(/
(*
(-
(*
(* (fma (fma 0.3611111111111111 u1 0.3333333333333333) u1 0.25) u1)
u1)
1.0)
u1)
(fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 -1.0)))
(sin (* (* 2.0 (PI)) u2))))\begin{array}{l}
\\
\sqrt{\frac{\left(\left(\mathsf{fma}\left(\mathsf{fma}\left(0.3611111111111111, u1, 0.3333333333333333\right), u1, 0.25\right) \cdot u1\right) \cdot u1 - 1\right) \cdot u1}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, -1\right)}} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 55.3%
Taylor expanded in u1 around 0
Applied rewrites94.4%
Applied rewrites94.4%
Taylor expanded in u1 around 0
Applied rewrites94.6%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= u1 0.054999999701976776)
(*
(sqrt (* (fma (fma 0.3333333333333333 u1 0.5) u1 1.0) u1))
(sin (* (* 2.0 (PI)) u2)))
(* (sqrt (- (log (- 1.0 u1)))) (* (* (PI) 2.0) u2))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;u1 \leq 0.054999999701976776:\\
\;\;\;\;\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)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)\\
\end{array}
\end{array}
if u1 < 0.0549999997Initial program 49.5%
Taylor expanded in u1 around 0
Applied rewrites97.5%
if 0.0549999997 < u1 Initial program 97.9%
Taylor expanded in u2 around 0
Applied rewrites85.4%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (fma (* (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1) u1 u1)) (sin (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1, u1, u1\right)} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 55.3%
Taylor expanded in u1 around 0
Applied rewrites94.4%
Applied rewrites94.5%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (* (fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 1.0) u1)) (sin (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\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}
Initial program 55.3%
Taylor expanded in u1 around 0
Applied rewrites94.4%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= u2 0.0006300000241026282)
(*
(sqrt (* (fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 1.0) u1))
(* (* (PI) 2.0) u2))
(* (sqrt u1) (sin (* (* 2.0 (PI)) u2)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;u2 \leq 0.0006300000241026282:\\
\;\;\;\;\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 \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\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 < 6.30000024e-4Initial program 56.0%
Taylor expanded in u1 around 0
Applied rewrites93.8%
Taylor expanded in u2 around 0
Applied rewrites93.3%
if 6.30000024e-4 < u2 Initial program 54.1%
Taylor expanded in u1 around 0
Applied rewrites80.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (* (fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 1.0) u1)) (* (* (PI) 2.0) u2)))
\begin{array}{l}
\\
\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 \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)
\end{array}
Initial program 55.3%
Taylor expanded in u1 around 0
Applied rewrites94.4%
Taylor expanded in u2 around 0
Applied rewrites78.9%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (* (fma (fma 0.3333333333333333 u1 0.5) u1 1.0) u1)) (* (* (PI) 2.0) u2)))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)
\end{array}
Initial program 55.3%
Taylor expanded in u1 around 0
Applied rewrites77.9%
Taylor expanded in u2 around 0
Applied rewrites67.7%
Taylor expanded in u1 around 0
Applied rewrites77.5%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (* (fma 0.5 u1 1.0) u1)) (* (* (PI) 2.0) u2)))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(0.5, u1, 1\right) \cdot u1} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)
\end{array}
Initial program 55.3%
Taylor expanded in u1 around 0
Applied rewrites89.4%
Taylor expanded in u2 around 0
Applied rewrites75.2%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt u1) (* (* (PI) 2.0) u2)))
\begin{array}{l}
\\
\sqrt{u1} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)
\end{array}
Initial program 55.3%
Taylor expanded in u1 around 0
Applied rewrites77.9%
Taylor expanded in u2 around 0
Applied rewrites67.7%
herbie shell --seed 2025019
(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))))