
(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)))) (t_1 (cbrt (PI))))
(if (<= (- 1.0 u1) 0.9998199939727783)
(*
(sqrt (- (log (- 1.0 u1))))
(cos
(*
(* (* (* (* t_1 u2) 2.0) t_1) (pow (PI) 0.25))
(pow (PI) 0.08333333333333333))))
(* (sqrt (- (- u1))) (* (pow t_0 2.0) (/ 1.0 t_0))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)\\
t_1 := \sqrt[3]{\mathsf{PI}\left(\right)}\\
\mathbf{if}\;1 - u1 \leq 0.9998199939727783:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(\left(\left(\left(t\_1 \cdot u2\right) \cdot 2\right) \cdot t\_1\right) \cdot {\mathsf{PI}\left(\right)}^{0.25}\right) \cdot {\mathsf{PI}\left(\right)}^{0.08333333333333333}\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\left(-u1\right)} \cdot \left({t\_0}^{2} \cdot \frac{1}{t\_0}\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999819994Initial program 89.0%
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.f3289.0
Applied rewrites89.0%
lift-*.f32N/A
unpow1N/A
sqr-powN/A
associate-*l*N/A
lower-*.f32N/A
metadata-evalN/A
pow1/2N/A
lift-cbrt.f32N/A
pow1/3N/A
sqrt-pow1N/A
lower-pow.f32N/A
metadata-evalN/A
lower-*.f32N/A
metadata-evalN/A
pow1/2N/A
lift-cbrt.f32N/A
pow1/3N/A
sqrt-pow1N/A
lower-pow.f32N/A
metadata-eval89.1
Applied rewrites89.1%
lift-*.f32N/A
*-commutativeN/A
lift-pow.f32N/A
unpow2N/A
associate-*r*N/A
lift-cbrt.f32N/A
pow1/3N/A
metadata-evalN/A
pow-sqrN/A
lift-pow.f32N/A
sqr-powN/A
associate-*r*N/A
associate-*r*N/A
lower-*.f32N/A
Applied rewrites89.1%
if 0.999819994 < (-.f32 #s(literal 1 binary32) u1) Initial program 40.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3291.2
Applied rewrites91.2%
lift-cos.f32N/A
lift-*.f32N/A
lift-*.f32N/A
lift-PI.f32N/A
lift-PI.f32N/A
associate-*l*N/A
cos-2N/A
difference-of-squaresN/A
lower-*.f32N/A
lower-+.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
Applied rewrites90.9%
lift-*.f32N/A
Applied rewrites91.2%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* u2 (* (PI) 2.0)))))
(if (<= (- 1.0 u1) 0.9998199939727783)
(*
(sqrt (- (log (- 1.0 u1))))
(cos
(*
(* (pow (PI) 0.8333333333333334) u2)
(* (pow (PI) 0.16666666666666666) 2.0))))
(* (sqrt (- (- u1))) (* (pow t_0 2.0) (/ 1.0 t_0))))))\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.9998199939727783:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left({\mathsf{PI}\left(\right)}^{0.8333333333333334} \cdot u2\right) \cdot \left({\mathsf{PI}\left(\right)}^{0.16666666666666666} \cdot 2\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\left(-u1\right)} \cdot \left({t\_0}^{2} \cdot \frac{1}{t\_0}\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999819994Initial program 89.0%
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.f3289.0
Applied rewrites89.0%
lift-*.f32N/A
unpow1N/A
sqr-powN/A
associate-*l*N/A
lower-*.f32N/A
metadata-evalN/A
pow1/2N/A
lift-cbrt.f32N/A
pow1/3N/A
sqrt-pow1N/A
lower-pow.f32N/A
metadata-evalN/A
lower-*.f32N/A
metadata-evalN/A
pow1/2N/A
lift-cbrt.f32N/A
pow1/3N/A
sqrt-pow1N/A
lower-pow.f32N/A
metadata-eval89.1
Applied rewrites89.1%
lift-*.f32N/A
lift-*.f32N/A
lift-*.f32N/A
*-commutativeN/A
associate-*l*N/A
associate-*r*N/A
lower-*.f32N/A
Applied rewrites89.1%
if 0.999819994 < (-.f32 #s(literal 1 binary32) u1) Initial program 40.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3291.2
Applied rewrites91.2%
lift-cos.f32N/A
lift-*.f32N/A
lift-*.f32N/A
lift-PI.f32N/A
lift-PI.f32N/A
associate-*l*N/A
cos-2N/A
difference-of-squaresN/A
lower-*.f32N/A
lower-+.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
Applied rewrites90.9%
lift-*.f32N/A
Applied rewrites91.2%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* u2 (* (PI) 2.0)))))
(if (<= (- 1.0 u1) 0.9998199939727783)
(* (sqrt (- (log (- 1.0 u1)))) (cos (* (PI) (+ u2 u2))))
(* (sqrt (- (- u1))) (* (pow t_0 2.0) (/ 1.0 t_0))))))\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.9998199939727783:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\left(-u1\right)} \cdot \left({t\_0}^{2} \cdot \frac{1}{t\_0}\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999819994Initial program 89.0%
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.f3289.0
Applied rewrites89.0%
lift-*.f32N/A
unpow1N/A
sqr-powN/A
associate-*l*N/A
lower-*.f32N/A
metadata-evalN/A
pow1/2N/A
lift-cbrt.f32N/A
pow1/3N/A
sqrt-pow1N/A
lower-pow.f32N/A
metadata-evalN/A
lower-*.f32N/A
metadata-evalN/A
pow1/2N/A
lift-cbrt.f32N/A
pow1/3N/A
sqrt-pow1N/A
lower-pow.f32N/A
metadata-eval89.1
Applied rewrites89.1%
lift-*.f32N/A
lift-*.f32N/A
associate-*r*N/A
*-commutativeN/A
lift-*.f32N/A
lift-*.f32N/A
associate-*r*N/A
lift-pow.f32N/A
lift-pow.f32N/A
pow-prod-upN/A
metadata-evalN/A
pow1/3N/A
lift-cbrt.f32N/A
associate-*r*N/A
Applied rewrites89.0%
if 0.999819994 < (-.f32 #s(literal 1 binary32) u1) Initial program 40.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3291.2
Applied rewrites91.2%
lift-cos.f32N/A
lift-*.f32N/A
lift-*.f32N/A
lift-PI.f32N/A
lift-PI.f32N/A
associate-*l*N/A
cos-2N/A
difference-of-squaresN/A
lower-*.f32N/A
lower-+.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
Applied rewrites90.9%
lift-*.f32N/A
Applied rewrites91.2%
(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.014919999986886978) (* (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.014919999986886978:\\
\;\;\;\;\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.01492Initial program 42.1%
Applied rewrites56.6%
Taylor expanded in u1 around 0
lower-sqrt.f3289.8
Applied rewrites89.8%
if 0.01492 < (*.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 89.7%
Taylor expanded in u2 around 0
Applied rewrites78.0%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (- (log (- 1.0 u1))))))
(if (<= (* t_0 (cos (* (* 2.0 (PI)) u2))) 0.010999999940395355)
(* (sqrt (- (- u1))) 1.0)
(* 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.010999999940395355:\\
\;\;\;\;\sqrt{-\left(-u1\right)} \cdot 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.0109999999Initial program 40.2%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3290.9
Applied rewrites90.9%
Taylor expanded in u2 around 0
Applied rewrites78.3%
if 0.0109999999 < (*.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 88.2%
Taylor expanded in u2 around 0
Applied rewrites76.5%
(FPCore (cosTheta_i u1 u2) :precision binary32 (if (<= (- 1.0 u1) 0.9998199939727783) (* (sqrt (- (log (- 1.0 u1)))) (cos (* (PI) (+ u2 u2)))) (* (sqrt u1) (cos (* (* 2.0 (PI)) u2)))))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;1 - u1 \leq 0.9998199939727783:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{u1} \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.999819994Initial program 89.0%
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.f3289.0
Applied rewrites89.0%
lift-*.f32N/A
unpow1N/A
sqr-powN/A
associate-*l*N/A
lower-*.f32N/A
metadata-evalN/A
pow1/2N/A
lift-cbrt.f32N/A
pow1/3N/A
sqrt-pow1N/A
lower-pow.f32N/A
metadata-evalN/A
lower-*.f32N/A
metadata-evalN/A
pow1/2N/A
lift-cbrt.f32N/A
pow1/3N/A
sqrt-pow1N/A
lower-pow.f32N/A
metadata-eval89.1
Applied rewrites89.1%
lift-*.f32N/A
lift-*.f32N/A
associate-*r*N/A
*-commutativeN/A
lift-*.f32N/A
lift-*.f32N/A
associate-*r*N/A
lift-pow.f32N/A
lift-pow.f32N/A
pow-prod-upN/A
metadata-evalN/A
pow1/3N/A
lift-cbrt.f32N/A
associate-*r*N/A
Applied rewrites89.0%
if 0.999819994 < (-.f32 #s(literal 1 binary32) u1) Initial program 40.0%
Applied rewrites61.4%
Taylor expanded in u1 around 0
lower-sqrt.f3291.2
Applied rewrites91.2%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (- u1))) 1.0))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf(-(-u1)) * 1.0f;
}
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)) * 1.0e0
end function
function code(cosTheta_i, u1, u2) return Float32(sqrt(Float32(-Float32(-u1))) * Float32(1.0)) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt(-(-u1)) * single(1.0); end
\begin{array}{l}
\\
\sqrt{-\left(-u1\right)} \cdot 1
\end{array}
Initial program 60.7%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3275.0
Applied rewrites75.0%
Taylor expanded in u2 around 0
Applied rewrites66.5%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (- (sqrt u1)) 1.0))
float code(float cosTheta_i, float u1, float u2) {
return -sqrtf(u1) * 1.0f;
}
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) * 1.0e0
end function
function code(cosTheta_i, u1, u2) return Float32(Float32(-sqrt(u1)) * Float32(1.0)) end
function tmp = code(cosTheta_i, u1, u2) tmp = -sqrt(u1) * single(1.0); end
\begin{array}{l}
\\
\left(-\sqrt{u1}\right) \cdot 1
\end{array}
Initial program 60.7%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3275.0
Applied rewrites75.0%
Taylor expanded in u2 around 0
Applied rewrites66.5%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
lower-*.f32N/A
lower-sqrt.f324.6
Applied rewrites4.6%
Applied rewrites4.6%
herbie shell --seed 2024324
(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))))