
(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 (+ (pow t_1 8.0) (+ (pow t_2 8.0) (pow (* t_1 t_2) 4.0))))
(t_4 (cos (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.9998000264167786)
(* (* (/ t_4 t_3) t_3) (sqrt (- (log (- 1.0 u1)))))
(* t_4 (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\_1}^{8} + \left({t\_2}^{8} + {\left(t\_1 \cdot t\_2\right)}^{4}\right)\\
t_4 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;1 - u1 \leq 0.9998000264167786:\\
\;\;\;\;\left(\frac{t\_4}{t\_3} \cdot t\_3\right) \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;t\_4 \cdot \sqrt{u1}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999800026Initial program 89.6%
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.5%
Applied rewrites89.6%
if 0.999800026 < (-.f32 #s(literal 1 binary32) u1) Initial program 36.4%
Applied rewrites51.7%
Taylor expanded in u1 around 0
lower-sqrt.f3292.1
Applied rewrites92.1%
Final simplification91.1%
(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.02800000086426735) (* 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.02800000086426735:\\
\;\;\;\;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.0280000009Initial program 41.8%
Applied rewrites47.2%
Taylor expanded in u1 around 0
lower-sqrt.f3288.7
Applied rewrites88.7%
if 0.0280000009 < (*.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.9%
Taylor expanded in u2 around 0
Applied rewrites79.9%
Final simplification86.0%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (- (log (- 1.0 u1))))))
(if (<= (* (cos (* (* 2.0 (PI)) u2)) t_0) 0.014000000432133675)
(* 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.014000000432133675:\\
\;\;\;\;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.0140000004Initial program 38.5%
Applied rewrites47.5%
Taylor expanded in u1 around 0
lower-sqrt.f3290.6
Applied rewrites90.6%
Taylor expanded in u2 around 0
Applied rewrites70.0%
if 0.0140000004 < (*.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.4%
Taylor expanded in u2 around 0
Applied rewrites77.4%
Final simplification72.7%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.9998000264167786)
(/ 1.0 (/ (/ 1.0 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.9998000264167786:\\
\;\;\;\;\frac{1}{\frac{\frac{1}{t\_0}}{\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.999800026Initial program 89.6%
Applied rewrites16.7%
Applied rewrites29.4%
lift-log1p.f32N/A
lower-log.f32N/A
lift-neg.f32N/A
sub-negN/A
lower--.f3289.6
Applied rewrites89.6%
if 0.999800026 < (-.f32 #s(literal 1 binary32) u1) Initial program 36.4%
Applied rewrites49.2%
Taylor expanded in u1 around 0
lower-sqrt.f3292.1
Applied rewrites92.1%
Final simplification91.1%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (PI))))
(if (<= (- 1.0 u1) 0.9998000264167786)
(* (cos (* (* t_0 (* 2.0 u2)) t_0)) (sqrt (- (log (- 1.0 u1)))))
(* (cos (* (* 2.0 (PI)) u2)) (sqrt u1)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\mathsf{PI}\left(\right)}\\
\mathbf{if}\;1 - u1 \leq 0.9998000264167786:\\
\;\;\;\;\cos \left(\left(t\_0 \cdot \left(2 \cdot u2\right)\right) \cdot t\_0\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.6%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*r*N/A
lift-PI.f32N/A
add-sqr-sqrtN/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-sqrt.f32N/A
lift-PI.f32N/A
lower-sqrt.f3289.6
Applied rewrites89.6%
if 0.999800026 < (-.f32 #s(literal 1 binary32) u1) Initial program 36.4%
Applied rewrites51.9%
Taylor expanded in u1 around 0
lower-sqrt.f3292.1
Applied rewrites92.1%
Final simplification91.1%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.9998000264167786)
(* 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.9998000264167786:\\
\;\;\;\;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.999800026Initial program 89.6%
if 0.999800026 < (-.f32 #s(literal 1 binary32) u1) Initial program 36.4%
Applied rewrites49.5%
Taylor expanded in u1 around 0
lower-sqrt.f3292.1
Applied rewrites92.1%
Final simplification91.1%
(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.6%
Applied rewrites41.7%
Taylor expanded in u1 around 0
lower-sqrt.f3276.1
Applied rewrites76.1%
Taylor expanded in u2 around 0
Applied rewrites62.3%
Final simplification62.3%
herbie shell --seed 2024276
(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))))