
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \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)))) (cos (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (* (PI) u2)) (t_1 (cos t_0)) (t_2 (sin t_0)) (t_3 (+ t_2 t_1)))
(if (<= (- 1.0 u1) 0.9998000264167786)
(*
(*
(*
(/ t_3 (/ 1.0 (- 1.0 (* t_2 t_1))))
(/ (- t_1 t_2) (+ (pow t_1 3.0) (pow t_2 3.0))))
t_3)
(sqrt (- (log (- 1.0 u1)))))
(* (cos (* (* 2.0 (PI)) u2)) (sqrt u1)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{PI}\left(\right) \cdot u2\\
t_1 := \cos t\_0\\
t_2 := \sin t\_0\\
t_3 := t\_2 + t\_1\\
\mathbf{if}\;1 - u1 \leq 0.9998000264167786:\\
\;\;\;\;\left(\left(\frac{t\_3}{\frac{1}{1 - t\_2 \cdot t\_1}} \cdot \frac{t\_1 - t\_2}{{t\_1}^{3} + {t\_2}^{3}}\right) \cdot t\_3\right) \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot \sqrt{u1}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999800026Initial program 89.1%
lift-cos.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
cos-2N/A
difference-of-squaresN/A
lower-*.f32N/A
lower-+.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
Applied rewrites89.1%
lift--.f32N/A
flip--N/A
difference-of-squaresN/A
lift-+.f32N/A
lift--.f32N/A
*-commutativeN/A
flip3-+N/A
div-invN/A
Applied rewrites89.2%
if 0.999800026 < (-.f32 #s(literal 1 binary32) u1) Initial program 41.8%
Applied rewrites58.8%
Taylor expanded in u1 around 0
lower-sqrt.f3290.9
Applied rewrites90.9%
Final simplification90.3%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (* (PI) u2)) (t_1 (sin t_0)) (t_2 (cos t_0)))
(if (<= (- 1.0 u1) 0.9998000264167786)
(* (* (- t_2 t_1) (+ t_1 t_2)) (sqrt (- (log (- 1.0 u1)))))
(* (cos (* (* 2.0 (PI)) u2)) (sqrt u1)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{PI}\left(\right) \cdot u2\\
t_1 := \sin t\_0\\
t_2 := \cos t\_0\\
\mathbf{if}\;1 - u1 \leq 0.9998000264167786:\\
\;\;\;\;\left(\left(t\_2 - t\_1\right) \cdot \left(t\_1 + t\_2\right)\right) \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot \sqrt{u1}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999800026Initial program 89.1%
lift-cos.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
cos-2N/A
difference-of-squaresN/A
lower-*.f32N/A
lower-+.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
Applied rewrites89.1%
if 0.999800026 < (-.f32 #s(literal 1 binary32) u1) Initial program 41.8%
Applied rewrites57.5%
Taylor expanded in u1 around 0
lower-sqrt.f3290.9
Applied rewrites90.9%
Final simplification90.3%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (- (log (- 1.0 u1)))) (t_1 (cos (* (* 2.0 (PI)) u2))))
(if (<= t_0 0.00019999999494757503)
(* t_1 (sqrt u1))
(* (/ 1.0 (/ 1.0 t_1)) (sqrt t_0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
t_1 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \leq 0.00019999999494757503:\\
\;\;\;\;t\_1 \cdot \sqrt{u1}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{1}{t\_1}} \cdot \sqrt{t\_0}\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.99999995e-4Initial program 41.8%
Applied rewrites57.0%
Taylor expanded in u1 around 0
lower-sqrt.f3290.9
Applied rewrites90.9%
if 1.99999995e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 89.1%
lift-cos.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
cos-2N/A
flip--N/A
cos-sin-sumN/A
lower-/.f32N/A
Applied rewrites89.1%
lift-/.f32N/A
clear-numN/A
lower-/.f32N/A
lower-/.f3289.1
/-rgt-identityN/A
lift--.f32N/A
lift-pow.f32N/A
sqr-powN/A
lift-pow.f32N/A
sqr-powN/A
cos-sin-sumN/A
Applied rewrites89.1%
Final simplification90.3%
(FPCore (cosTheta_i u1 u2) :precision binary32 (let* ((t_0 (cos (* (* 2.0 (PI)) u2))) (t_1 (sqrt (- (log (- 1.0 u1)))))) (if (<= (* t_0 t_1) 0.019999999552965164) (* t_0 (sqrt u1)) (* 1.0 t_1))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
t_1 := \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{if}\;t\_0 \cdot t\_1 \leq 0.019999999552965164:\\
\;\;\;\;t\_0 \cdot \sqrt{u1}\\
\mathbf{else}:\\
\;\;\;\;1 \cdot t\_1\\
\end{array}
\end{array}
if (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2))) < 0.0199999996Initial program 45.3%
Applied rewrites54.0%
Taylor expanded in u1 around 0
lower-sqrt.f3288.4
Applied rewrites88.4%
if 0.0199999996 < (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2))) Initial program 90.2%
Taylor expanded in u2 around 0
Applied rewrites81.4%
Final simplification86.2%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (- (log (- 1.0 u1))))))
(if (<= (* (cos (* (* 2.0 (PI)) u2)) t_0) 0.01600000075995922)
(* 1.0 (sqrt u1))
(* 1.0 t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{if}\;\cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot t\_0 \leq 0.01600000075995922:\\
\;\;\;\;1 \cdot \sqrt{u1}\\
\mathbf{else}:\\
\;\;\;\;1 \cdot t\_0\\
\end{array}
\end{array}
if (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2))) < 0.0160000008Initial program 44.2%
Applied rewrites54.1%
Taylor expanded in u1 around 0
lower-sqrt.f3289.1
Applied rewrites89.1%
Taylor expanded in u2 around 0
Applied rewrites71.0%
if 0.0160000008 < (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2))) Initial program 89.3%
Taylor expanded in u2 around 0
Applied rewrites80.4%
Final simplification74.2%
(FPCore (cosTheta_i u1 u2) :precision binary32 (let* ((t_0 (- (log (- 1.0 u1)))) (t_1 (cos (* (* 2.0 (PI)) u2)))) (if (<= t_0 0.00019999999494757503) (* t_1 (sqrt u1)) (* t_1 (sqrt t_0)))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
t_1 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \leq 0.00019999999494757503:\\
\;\;\;\;t\_1 \cdot \sqrt{u1}\\
\mathbf{else}:\\
\;\;\;\;t\_1 \cdot \sqrt{t\_0}\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.99999995e-4Initial program 41.8%
Applied rewrites57.5%
Taylor expanded in u1 around 0
lower-sqrt.f3290.9
Applied rewrites90.9%
if 1.99999995e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 89.1%
Final simplification90.2%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* 1.0 (sqrt u1)))
float code(float cosTheta_i, float u1, float u2) {
return 1.0f * sqrtf(u1);
}
real(4) function code(costheta_i, u1, u2)
real(4), intent (in) :: costheta_i
real(4), intent (in) :: u1
real(4), intent (in) :: u2
code = 1.0e0 * sqrt(u1)
end function
function code(cosTheta_i, u1, u2) return Float32(Float32(1.0) * sqrt(u1)) end
function tmp = code(cosTheta_i, u1, u2) tmp = single(1.0) * sqrt(u1); end
\begin{array}{l}
\\
1 \cdot \sqrt{u1}
\end{array}
Initial program 59.5%
Applied rewrites47.3%
Taylor expanded in u1 around 0
lower-sqrt.f3276.2
Applied rewrites76.2%
Taylor expanded in u2 around 0
Applied rewrites63.9%
Final simplification63.9%
herbie shell --seed 2024278
(FPCore (cosTheta_i u1 u2)
:name "Beckmann Sample, near normal, slope_x"
: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)))) (cos (* (* 2.0 (PI)) u2))))