
(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 (* (* 2.0 (PI)) u2)) (t_1 (sin t_0)) (t_2 (cos t_0)))
(if (<= (- 1.0 u1) 0.9998149871826172)
(*
(/
(- (+ t_2 1.0) (* (/ t_1 (/ -1.0 (- t_2 1.0))) (/ t_1 (pow t_1 2.0))))
2.0)
(sqrt (- (log (- 1.0 u1)))))
(* (sqrt u1) t_2))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\\
t_1 := \sin t\_0\\
t_2 := \cos t\_0\\
\mathbf{if}\;1 - u1 \leq 0.9998149871826172:\\
\;\;\;\;\frac{\left(t\_2 + 1\right) - \frac{t\_1}{\frac{-1}{t\_2 - 1}} \cdot \frac{t\_1}{{t\_1}^{2}}}{2} \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{u1} \cdot t\_2\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.99981499Initial program 87.9%
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 rewrites88.0%
Applied rewrites88.1%
if 0.99981499 < (-.f32 #s(literal 1 binary32) u1) Initial program 38.2%
Applied rewrites52.0%
Taylor expanded in u1 around 0
lower-sqrt.f3292.3
Applied rewrites92.3%
Final simplification90.8%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.9998149871826172)
(*
(- (* (- t_0 1.0) 0.5) (* (- -1.0 t_0) 0.5))
(sqrt (- (log (- 1.0 u1)))))
(* (sqrt u1) 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.9998149871826172:\\
\;\;\;\;\left(\left(t\_0 - 1\right) \cdot 0.5 - \left(-1 - t\_0\right) \cdot 0.5\right) \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{u1} \cdot t\_0\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.99981499Initial program 87.9%
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 rewrites88.0%
lift-/.f32N/A
lift--.f32N/A
div-subN/A
lower--.f32N/A
div-invN/A
metadata-evalN/A
lower-*.f32N/A
div-invN/A
metadata-evalN/A
lower-*.f3288.0
Applied rewrites88.0%
if 0.99981499 < (-.f32 #s(literal 1 binary32) u1) Initial program 38.2%
Applied rewrites52.6%
Taylor expanded in u1 around 0
lower-sqrt.f3292.3
Applied rewrites92.3%
Final simplification90.8%
(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.013500000350177288) (* (sqrt u1) t_0) (* 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.013500000350177288:\\
\;\;\;\;\sqrt{u1} \cdot t\_0\\
\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.0135000004Initial program 40.2%
Applied rewrites49.4%
Taylor expanded in u1 around 0
lower-sqrt.f3290.6
Applied rewrites90.6%
if 0.0135000004 < (*.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.5%
Taylor expanded in u2 around 0
Applied rewrites75.8%
Final simplification85.6%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (- (log (- 1.0 u1))))))
(if (<= (* (cos (* (* 2.0 (PI)) u2)) t_0) 0.013500000350177288)
(sqrt (fma u1 (* -0.5 u1) 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.013500000350177288:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(u1, -0.5 \cdot u1, u1\right)}\\
\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.0135000004Initial program 40.2%
Applied rewrites49.4%
Taylor expanded in u2 around 0
lower-sqrt.f32N/A
lower-log1p.f3274.2
Applied rewrites74.2%
Taylor expanded in u1 around 0
Applied rewrites73.7%
Applied rewrites73.6%
if 0.0135000004 < (*.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.5%
Taylor expanded in u2 around 0
Applied rewrites75.8%
Final simplification66.8%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.9998149871826172)
(* t_0 (sqrt (- (log (- 1.0 u1)))))
(* (sqrt u1) 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.9998149871826172:\\
\;\;\;\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{u1} \cdot t\_0\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.99981499Initial program 87.9%
if 0.99981499 < (-.f32 #s(literal 1 binary32) u1) Initial program 38.2%
Applied rewrites52.8%
Taylor expanded in u1 around 0
lower-sqrt.f3292.3
Applied rewrites92.3%
Final simplification90.7%
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt (fma u1 (* -0.5 u1) u1)))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf(fmaf(u1, (-0.5f * u1), u1));
}
function code(cosTheta_i, u1, u2) return sqrt(fma(u1, Float32(Float32(-0.5) * u1), u1)) end
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(u1, -0.5 \cdot u1, u1\right)}
\end{array}
Initial program 56.3%
Applied rewrites42.4%
Taylor expanded in u2 around 0
lower-sqrt.f32N/A
lower-log1p.f3266.6
Applied rewrites66.6%
Taylor expanded in u1 around 0
Applied rewrites66.6%
Applied rewrites66.6%
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt (* (fma u1 -0.5 1.0) u1)))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf((fmaf(u1, -0.5f, 1.0f) * u1));
}
function code(cosTheta_i, u1, u2) return sqrt(Float32(fma(u1, Float32(-0.5), Float32(1.0)) * u1)) end
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(u1, -0.5, 1\right) \cdot u1}
\end{array}
Initial program 56.3%
Applied rewrites42.7%
Taylor expanded in u2 around 0
lower-sqrt.f32N/A
lower-log1p.f3266.6
Applied rewrites66.6%
Taylor expanded in u1 around 0
Applied rewrites66.2%
Applied rewrites66.2%
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt (* (* -0.5 u1) u1)))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf(((-0.5f * u1) * 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((((-0.5e0) * u1) * u1))
end function
function code(cosTheta_i, u1, u2) return sqrt(Float32(Float32(Float32(-0.5) * u1) * u1)) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt(((single(-0.5) * u1) * u1)); end
\begin{array}{l}
\\
\sqrt{\left(-0.5 \cdot u1\right) \cdot u1}
\end{array}
Initial program 56.3%
Applied rewrites42.5%
Taylor expanded in u2 around 0
lower-sqrt.f32N/A
lower-log1p.f3266.6
Applied rewrites66.6%
Taylor expanded in u1 around 0
Applied rewrites66.6%
Taylor expanded in u1 around inf
Applied rewrites-0.0%
herbie shell --seed 2024294
(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))))