
(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 6 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 (cbrt (PI))) (t_1 (pow t_0 2.0)))
(if (<= (- 1.0 u1) 0.9999099969863892)
(*
(cos (* (* (* (* u2 (cbrt t_0)) (cbrt t_1)) 2.0) t_1))
(sqrt (- (log (- 1.0 u1)))))
(* (cos (* (* 2.0 (PI)) u2)) (sqrt u1)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\mathsf{PI}\left(\right)}\\
t_1 := {t\_0}^{2}\\
\mathbf{if}\;1 - u1 \leq 0.9999099969863892:\\
\;\;\;\;\cos \left(\left(\left(\left(u2 \cdot \sqrt[3]{t\_0}\right) \cdot \sqrt[3]{t\_1}\right) \cdot 2\right) \cdot t\_1\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.999909997Initial program 90.6%
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.f3290.6
Applied rewrites90.6%
lift-*.f32N/A
lift-cbrt.f32N/A
pow1/3N/A
rem-cube-cbrtN/A
lift-cbrt.f32N/A
pow3N/A
unpow2N/A
lift-pow.f32N/A
unpow-prod-downN/A
associate-*l*N/A
lower-*.f32N/A
pow1/3N/A
lower-cbrt.f32N/A
lower-*.f32N/A
pow1/3N/A
lower-cbrt.f3290.6
Applied rewrites90.6%
if 0.999909997 < (-.f32 #s(literal 1 binary32) u1) Initial program 34.3%
Applied rewrites52.2%
Taylor expanded in u1 around 0
lower-sqrt.f3293.9
Applied rewrites93.9%
Final simplification92.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cbrt (PI))))
(if (<= (- 1.0 u1) 0.9999099969863892)
(* (cos (* (* (* u2 t_0) 2.0) (pow t_0 2.0))) (sqrt (- (log (- 1.0 u1)))))
(* (cos (* (* 2.0 (PI)) u2)) (sqrt u1)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\mathsf{PI}\left(\right)}\\
\mathbf{if}\;1 - u1 \leq 0.9999099969863892:\\
\;\;\;\;\cos \left(\left(\left(u2 \cdot t\_0\right) \cdot 2\right) \cdot {t\_0}^{2}\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.999909997Initial program 90.6%
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.f3290.6
Applied rewrites90.6%
if 0.999909997 < (-.f32 #s(literal 1 binary32) u1) Initial program 34.3%
Applied rewrites55.7%
Taylor expanded in u1 around 0
lower-sqrt.f3293.9
Applied rewrites93.9%
Final simplification92.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.9999099969863892)
(* t_0 (sqrt (- (log (- 1.0 u1)))))
(* t_0 (sqrt u1)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;1 - u1 \leq 0.9999099969863892:\\
\;\;\;\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \sqrt{u1}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999909997Initial program 90.6%
if 0.999909997 < (-.f32 #s(literal 1 binary32) u1) Initial program 34.3%
Applied rewrites52.4%
Taylor expanded in u1 around 0
lower-sqrt.f3293.9
Applied rewrites93.9%
Final simplification92.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (* (* 2.0 (PI)) u2)) (t_1 (cos t_0)))
(if (<= t_0 0.02419999986886978)
(*
(+
(* (fma (sqrt u1) 0.16666666666666666 (/ 0.25 (sqrt u1))) (* u1 u1))
(sqrt u1))
t_1)
(* (/ u1 (sqrt u1)) t_1))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\\
t_1 := \cos t\_0\\
\mathbf{if}\;t\_0 \leq 0.02419999986886978:\\
\;\;\;\;\left(\mathsf{fma}\left(\sqrt{u1}, 0.16666666666666666, \frac{0.25}{\sqrt{u1}}\right) \cdot \left(u1 \cdot u1\right) + \sqrt{u1}\right) \cdot t\_1\\
\mathbf{else}:\\
\;\;\;\;\frac{u1}{\sqrt{u1}} \cdot t\_1\\
\end{array}
\end{array}
if (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2) < 0.0241999999Initial program 59.7%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3274.6
Applied rewrites74.6%
lift-sqrt.f32N/A
pow1/2N/A
metadata-evalN/A
pow-to-expN/A
lower-exp.f32N/A
lower-*.f32N/A
lower-log.f32N/A
metadata-eval73.1
Applied rewrites73.1%
Taylor expanded in u1 around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/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.f3274.6
Applied rewrites74.6%
Applied rewrites86.1%
if 0.0241999999 < (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2) Initial program 55.5%
Applied rewrites14.5%
Taylor expanded in u1 around 0
lower-sqrt.f3277.4
Applied rewrites77.4%
Applied rewrites77.3%
Applied rewrites77.6%
Final simplification84.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (cos (* (* 2.0 (PI)) u2)) (sqrt u1)))
\begin{array}{l}
\\
\cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot \sqrt{u1}
\end{array}
Initial program 58.7%
Applied rewrites44.0%
Taylor expanded in u1 around 0
lower-sqrt.f3275.3
Applied rewrites75.3%
Final simplification75.3%
(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 58.7%
Applied rewrites43.8%
Taylor expanded in u1 around 0
lower-sqrt.f3275.3
Applied rewrites75.3%
Taylor expanded in u2 around 0
Applied rewrites63.1%
Final simplification63.1%
herbie shell --seed 2024282
(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))))