
(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 (cbrt (* (PI) (PI)))) (t_1 (cbrt (PI))))
(if (<= (- 1.0 u1) 0.999888002872467)
(* (sqrt (- (log (- 1.0 u1)))) (cos (* (* (* 2.0 u2) t_0) t_1)))
(* (cos (* t_0 (* (* t_1 u2) 2.0))) (sqrt u1)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)}\\
t_1 := \sqrt[3]{\mathsf{PI}\left(\right)}\\
\mathbf{if}\;1 - u1 \leq 0.999888002872467:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(\left(2 \cdot u2\right) \cdot t\_0\right) \cdot t\_1\right)\\
\mathbf{else}:\\
\;\;\;\;\cos \left(t\_0 \cdot \left(\left(t\_1 \cdot u2\right) \cdot 2\right)\right) \cdot \sqrt{u1}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999888003Initial program 89.2%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*r*N/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lift-PI.f32N/A
lower-cbrt.f3289.3
Applied rewrites89.3%
Taylor expanded in u2 around 0
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-cbrt.f32N/A
unpow2N/A
lower-*.f32N/A
lower-PI.f32N/A
lower-PI.f3289.3
Applied rewrites89.3%
if 0.999888003 < (-.f32 #s(literal 1 binary32) u1) Initial program 37.3%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f323.6
Applied rewrites3.6%
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.6
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.6
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.6
Applied rewrites93.4%
lift-*.f32N/A
lift-*.f32N/A
associate-*r*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.f3293.5
Applied rewrites93.5%
lift-pow.f32N/A
unpow2N/A
lift-cbrt.f32N/A
lift-cbrt.f32N/A
cbrt-unprodN/A
lift-PI.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lift-PI.f32N/A
lift-PI.f32N/A
lower-*.f3293.5
Applied rewrites93.5%
(FPCore (cosTheta_i u1 u2) :precision binary32 (if (<= (- 1.0 u1) 0.999888002872467) (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 (PI)) u2))) (* (cos (* (cbrt (* (PI) (PI))) (* (* (cbrt (PI)) u2) 2.0))) (sqrt u1))))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;1 - u1 \leq 0.999888002872467:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\cos \left(\sqrt[3]{\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)} \cdot \left(\left(\sqrt[3]{\mathsf{PI}\left(\right)} \cdot u2\right) \cdot 2\right)\right) \cdot \sqrt{u1}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999888003Initial program 89.2%
if 0.999888003 < (-.f32 #s(literal 1 binary32) u1) Initial program 37.3%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f323.6
Applied rewrites3.6%
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.6
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.6
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.6
Applied rewrites93.4%
lift-*.f32N/A
lift-*.f32N/A
associate-*r*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.f3293.5
Applied rewrites93.5%
lift-pow.f32N/A
unpow2N/A
lift-cbrt.f32N/A
lift-cbrt.f32N/A
cbrt-unprodN/A
lift-PI.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lift-PI.f32N/A
lift-PI.f32N/A
lower-*.f3293.5
Applied rewrites93.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (PI))))
(if (<= (- 1.0 u1) 0.999888002872467)
(* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 (PI)) u2)))
(* (cos (* (* (* u2 2.0) t_0) t_0)) (sqrt u1)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\mathsf{PI}\left(\right)}\\
\mathbf{if}\;1 - u1 \leq 0.999888002872467:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\cos \left(\left(\left(u2 \cdot 2\right) \cdot t\_0\right) \cdot t\_0\right) \cdot \sqrt{u1}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999888003Initial program 89.2%
if 0.999888003 < (-.f32 #s(literal 1 binary32) u1) Initial program 37.3%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f323.6
Applied rewrites3.6%
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.6
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.6
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.6
Applied rewrites93.4%
lift-*.f32N/A
lift-*.f32N/A
*-commutativeN/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.f3293.4
Applied rewrites93.4%
(FPCore (cosTheta_i u1 u2) :precision binary32 (let* ((t_0 (sqrt (PI)))) (* (cos (* (* (* u2 2.0) t_0) t_0)) (sqrt u1))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\mathsf{PI}\left(\right)}\\
\cos \left(\left(\left(u2 \cdot 2\right) \cdot t\_0\right) \cdot t\_0\right) \cdot \sqrt{u1}
\end{array}
\end{array}
Initial program 58.8%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f323.5
Applied rewrites3.5%
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
Applied rewrites76.6%
lift-*.f32N/A
lift-*.f32N/A
*-commutativeN/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.f3276.6
Applied rewrites76.6%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (cos (* (PI) (+ u2 u2))) (sqrt u1)))
\begin{array}{l}
\\
\cos \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right) \cdot \sqrt{u1}
\end{array}
Initial program 58.8%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f323.5
Applied rewrites3.5%
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
Applied rewrites76.6%
lift-*.f32N/A
lift-*.f32N/A
associate-*r*N/A
lift-*.f32N/A
*-commutativeN/A
count-2N/A
lift-*.f32N/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3276.6
Applied rewrites76.6%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* 1.0 (pow (* u1 u1) 0.25)))
float code(float cosTheta_i, float u1, float u2) {
return 1.0f * powf((u1 * u1), 0.25f);
}
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 * ((u1 * u1) ** 0.25e0)
end function
function code(cosTheta_i, u1, u2) return Float32(Float32(1.0) * (Float32(u1 * u1) ^ Float32(0.25))) end
function tmp = code(cosTheta_i, u1, u2) tmp = single(1.0) * ((u1 * u1) ^ single(0.25)); end
\begin{array}{l}
\\
1 \cdot {\left(u1 \cdot u1\right)}^{0.25}
\end{array}
Initial program 58.8%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f323.5
Applied rewrites3.5%
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
Applied rewrites76.6%
Taylor expanded in u2 around 0
Applied rewrites66.5%
Applied rewrites66.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 58.8%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f323.5
Applied rewrites3.5%
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f323.5
Applied rewrites76.6%
Taylor expanded in u2 around 0
Applied rewrites66.5%
herbie shell --seed 2024318
(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))))