
(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 8 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 (cos (* u2 (* (PI) 2.0)))))
(if (<= (- 1.0 u1) 0.9998499751091003)
(* (sqrt (- (log (- 1.0 u1)))) (/ (- (+ t_0 1.0) (- 1.0 t_0)) 2.0))
(* (pow (* (- u1) (- u1)) 0.25) (cos (* (* 2.0 (PI)) u2))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)\\
\mathbf{if}\;1 - u1 \leq 0.9998499751091003:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \frac{\left(t\_0 + 1\right) - \left(1 - t\_0\right)}{2}\\
\mathbf{else}:\\
\;\;\;\;{\left(\left(-u1\right) \cdot \left(-u1\right)\right)}^{0.25} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999849975Initial program 87.3%
lift-cos.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
cos-2N/A
cos-multN/A
sin-multN/A
sub-divN/A
lower-/.f32N/A
Applied rewrites87.4%
if 0.999849975 < (-.f32 #s(literal 1 binary32) u1) Initial program 36.7%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.7
Applied rewrites92.7%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
pow-prod-downN/A
lower-pow.f32N/A
lower-*.f32N/A
metadata-eval92.7
Applied rewrites92.7%
Final simplification90.8%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (- (log (- 1.0 u1))))) (t_1 (cos (* (* 2.0 (PI)) u2))))
(if (<= (* t_0 t_1) 0.013000000268220901)
(* (pow (* (- u1) (- u1)) 0.25) t_1)
(* t_0 1.0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{-\log \left(1 - u1\right)}\\
t_1 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \cdot t\_1 \leq 0.013000000268220901:\\
\;\;\;\;{\left(\left(-u1\right) \cdot \left(-u1\right)\right)}^{0.25} \cdot t\_1\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot 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.0130000003Initial program 39.4%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3290.8
Applied rewrites90.8%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
pow-prod-downN/A
lower-pow.f32N/A
lower-*.f32N/A
metadata-eval90.8
Applied rewrites90.8%
if 0.0130000003 < (*.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 87.7%
Taylor expanded in u2 around 0
Applied rewrites77.9%
Final simplification86.7%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (- (log (- 1.0 u1))))) (t_1 (cos (* (* 2.0 (PI)) u2))))
(if (<= (* t_0 t_1) 0.013000000268220901)
(* (sqrt (* (* u1 u1) (/ -1.0 (- u1)))) t_1)
(* t_0 1.0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{-\log \left(1 - u1\right)}\\
t_1 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \cdot t\_1 \leq 0.013000000268220901:\\
\;\;\;\;\sqrt{\left(u1 \cdot u1\right) \cdot \frac{-1}{-u1}} \cdot t\_1\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot 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.0130000003Initial program 39.4%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3290.8
Applied rewrites90.8%
lift-neg.f32N/A
neg-sub0N/A
flip--N/A
div-invN/A
lower-*.f32N/A
metadata-evalN/A
sub0-negN/A
lower-neg.f32N/A
pow2N/A
lower-pow.f32N/A
+-lft-identityN/A
lower-/.f3290.8
Applied rewrites90.8%
Taylor expanded in u1 around 0
unpow2N/A
lower-*.f3290.8
Applied rewrites90.8%
if 0.0130000003 < (*.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 87.7%
Taylor expanded in u2 around 0
Applied rewrites77.9%
Final simplification86.7%
(FPCore (cosTheta_i u1 u2) :precision binary32 (let* ((t_0 (sqrt (- (log (- 1.0 u1))))) (t_1 (cos (* (* 2.0 (PI)) u2)))) (if (<= (* t_0 t_1) 0.013000000268220901) (* (sqrt u1) t_1) (* t_0 1.0))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{-\log \left(1 - u1\right)}\\
t_1 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \cdot t\_1 \leq 0.013000000268220901:\\
\;\;\;\;\sqrt{u1} \cdot t\_1\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot 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.0130000003Initial program 39.4%
Applied rewrites52.5%
Taylor expanded in u1 around 0
lower-sqrt.f3290.8
Applied rewrites90.8%
if 0.0130000003 < (*.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 87.7%
Taylor expanded in u2 around 0
Applied rewrites77.9%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (- (log (- 1.0 u1))))))
(if (<= (* t_0 (cos (* (* 2.0 (PI)) u2))) 0.01080000028014183)
(sqrt u1)
(* t_0 1.0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{if}\;t\_0 \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \leq 0.01080000028014183:\\
\;\;\;\;\sqrt{u1}\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot 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.0108000003Initial program 38.6%
Applied rewrites52.4%
Taylor expanded in u2 around 0
lower-sqrt.f32N/A
lower-log1p.f3272.5
Applied rewrites72.5%
Taylor expanded in u1 around 0
Applied rewrites72.5%
if 0.0108000003 < (*.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 87.0%
Taylor expanded in u2 around 0
Applied rewrites77.0%
(FPCore (cosTheta_i u1 u2) :precision binary32 (if (<= (- 1.0 u1) 0.9998499751091003) (* (sqrt (- (log (- 1.0 u1)))) (/ 1.0 (/ 1.0 (cos (* u2 (* (PI) 2.0)))))) (* (pow (* (- u1) (- u1)) 0.25) (cos (* (* 2.0 (PI)) u2)))))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;1 - u1 \leq 0.9998499751091003:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \frac{1}{\frac{1}{\cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)}}\\
\mathbf{else}:\\
\;\;\;\;{\left(\left(-u1\right) \cdot \left(-u1\right)\right)}^{0.25} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999849975Initial program 87.3%
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 rewrites87.2%
lift-/.f32N/A
clear-numN/A
lower-/.f32N/A
lower-/.f3287.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 rewrites87.3%
if 0.999849975 < (-.f32 #s(literal 1 binary32) u1) Initial program 36.7%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.7
Applied rewrites92.7%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
pow-prod-downN/A
lower-pow.f32N/A
lower-*.f32N/A
metadata-eval92.7
Applied rewrites92.7%
Final simplification90.8%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.9998499751091003)
(* (sqrt (- (log (- 1.0 u1)))) t_0)
(* (pow (* (- u1) (- u1)) 0.25) t_0))))\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.9998499751091003:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;{\left(\left(-u1\right) \cdot \left(-u1\right)\right)}^{0.25} \cdot t\_0\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999849975Initial program 87.3%
if 0.999849975 < (-.f32 #s(literal 1 binary32) u1) Initial program 36.7%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.7
Applied rewrites92.7%
lift-sqrt.f32N/A
pow1/2N/A
sqr-powN/A
pow-prod-downN/A
lower-pow.f32N/A
lower-*.f32N/A
metadata-eval92.7
Applied rewrites92.7%
Final simplification90.8%
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt u1))
float code(float cosTheta_i, float u1, float u2) {
return 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 = sqrt(u1)
end function
function code(cosTheta_i, u1, u2) return sqrt(u1) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt(u1); end
\begin{array}{l}
\\
\sqrt{u1}
\end{array}
Initial program 54.9%
Applied rewrites45.5%
Taylor expanded in u2 around 0
lower-sqrt.f32N/A
lower-log1p.f3266.1
Applied rewrites66.1%
Taylor expanded in u1 around 0
Applied rewrites66.1%
herbie shell --seed 2024323
(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))))