
(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 5 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))))
(if (<= (- 1.0 u1) 0.999779999256134)
(* (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.999779999256134:\\
\;\;\;\;\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.999779999Initial program 90.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.f3291.1
Applied rewrites91.1%
if 0.999779999 < (-.f32 #s(literal 1 binary32) u1) Initial program 39.1%
Applied rewrites51.3%
Taylor expanded in u1 around 0
lower-sqrt.f3291.5
Applied rewrites91.5%
Final simplification91.4%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (* t_0 (sqrt (- (log (- 1.0 u1))))) 0.019300000742077827)
(* t_0 (sqrt u1))
(* (sqrt 2.0) (sqrt (log (sqrt (/ 1.0 (- 1.0 u1)))))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)} \leq 0.019300000742077827:\\
\;\;\;\;t\_0 \cdot \sqrt{u1}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{2} \cdot \sqrt{\log \left(\sqrt{\frac{1}{1 - u1}}\right)}\\
\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.0193000007Initial program 41.8%
Applied rewrites49.0%
Taylor expanded in u1 around 0
lower-sqrt.f3289.6
Applied rewrites89.6%
if 0.0193000007 < (*.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 92.0%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
inv-powN/A
sqr-powN/A
log-prodN/A
lower-+.f32N/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-evalN/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-eval88.6
Applied rewrites88.6%
Taylor expanded in u2 around 0
lower-*.f32N/A
lower-sqrt.f32N/A
lower-log.f32N/A
lower-sqrt.f32N/A
sub-negN/A
mul-1-negN/A
lower-/.f32N/A
mul-1-negN/A
sub-negN/A
lower--.f32N/A
lower-sqrt.f3276.8
Applied rewrites76.8%
Final simplification85.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.999779999256134)
(* 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.999779999256134:\\
\;\;\;\;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.999779999Initial program 90.9%
if 0.999779999 < (-.f32 #s(literal 1 binary32) u1) Initial program 39.1%
Applied rewrites51.3%
Taylor expanded in u1 around 0
lower-sqrt.f3291.5
Applied rewrites91.5%
Final simplification91.3%
(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 57.9%
Applied rewrites45.4%
Taylor expanded in u1 around 0
lower-sqrt.f3276.5
Applied rewrites76.5%
Final simplification76.5%
(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 57.9%
Applied rewrites45.4%
Taylor expanded in u1 around 0
lower-sqrt.f3276.5
Applied rewrites76.5%
Taylor expanded in u2 around 0
Applied rewrites65.6%
Final simplification65.6%
herbie shell --seed 2024243
(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))))