
(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 9 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 (* u2 (PI)))
(t_1 (cos t_0))
(t_2 (sin t_0))
(t_3 (- (log (- 1.0 u1)))))
(if (<= t_3 0.00019999999494757503)
(*
(sqrt (- (- u1)))
(+
0.5
(- (* (cos (* (* 2.0 (PI)) u2)) 0.5) (pow (sin (* (PI) u2)) 2.0))))
(* (sqrt t_3) (* (+ t_1 t_2) (- t_1 t_2))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := u2 \cdot \mathsf{PI}\left(\right)\\
t_1 := \cos t\_0\\
t_2 := \sin t\_0\\
t_3 := -\log \left(1 - u1\right)\\
\mathbf{if}\;t\_3 \leq 0.00019999999494757503:\\
\;\;\;\;\sqrt{-\left(-u1\right)} \cdot \left(0.5 + \left(\cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot 0.5 - {\sin \left(\mathsf{PI}\left(\right) \cdot u2\right)}^{2}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_3} \cdot \left(\left(t\_1 + t\_2\right) \cdot \left(t\_1 - t\_2\right)\right)\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.99999995e-4Initial program 36.7%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.5
Applied rewrites92.5%
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
lower--.f32N/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-*.f32N/A
Applied rewrites92.6%
lift--.f32N/A
lift-*.f32N/A
lift-cos.f32N/A
lift-cos.f32N/A
sqr-cos-aN/A
associate--l+N/A
lower-+.f32N/A
lower--.f32N/A
Applied rewrites92.6%
if 1.99999995e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 90.2%
lift-cos.f32N/A
lift-*.f32N/A
lift-*.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
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
Applied rewrites90.4%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (* u2 (PI))) (t_1 (- (log (- 1.0 u1)))))
(if (<= t_1 0.00019999999494757503)
(*
(sqrt (- (- u1)))
(+
0.5
(- (* (cos (* (* 2.0 (PI)) u2)) 0.5) (pow (sin (* (PI) u2)) 2.0))))
(* (sqrt t_1) (- (pow (cos t_0) 2.0) (pow (sin t_0) 2.0))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := u2 \cdot \mathsf{PI}\left(\right)\\
t_1 := -\log \left(1 - u1\right)\\
\mathbf{if}\;t\_1 \leq 0.00019999999494757503:\\
\;\;\;\;\sqrt{-\left(-u1\right)} \cdot \left(0.5 + \left(\cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot 0.5 - {\sin \left(\mathsf{PI}\left(\right) \cdot u2\right)}^{2}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_1} \cdot \left({\cos t\_0}^{2} - {\sin t\_0}^{2}\right)\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.99999995e-4Initial program 36.7%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.5
Applied rewrites92.5%
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
lower--.f32N/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-*.f32N/A
Applied rewrites92.6%
lift--.f32N/A
lift-*.f32N/A
lift-cos.f32N/A
lift-cos.f32N/A
sqr-cos-aN/A
associate--l+N/A
lower-+.f32N/A
lower--.f32N/A
Applied rewrites92.6%
if 1.99999995e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 90.2%
lift-cos.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
cos-2N/A
lower--.f32N/A
pow2N/A
lower-pow.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f3290.3
Applied rewrites90.3%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))) (t_1 (- (log (- 1.0 u1)))))
(if (<= t_1 0.00019999999494757503)
(* (sqrt (- (- u1))) (+ 0.5 (- (* t_0 0.5) (pow (sin (* (PI) u2)) 2.0))))
(* (sqrt t_1) t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
t_1 := -\log \left(1 - u1\right)\\
\mathbf{if}\;t\_1 \leq 0.00019999999494757503:\\
\;\;\;\;\sqrt{-\left(-u1\right)} \cdot \left(0.5 + \left(t\_0 \cdot 0.5 - {\sin \left(\mathsf{PI}\left(\right) \cdot u2\right)}^{2}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_1} \cdot t\_0\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.99999995e-4Initial program 36.7%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.5
Applied rewrites92.5%
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
lower--.f32N/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-*.f32N/A
Applied rewrites92.6%
lift--.f32N/A
lift-*.f32N/A
lift-cos.f32N/A
lift-cos.f32N/A
sqr-cos-aN/A
associate--l+N/A
lower-+.f32N/A
lower--.f32N/A
Applied rewrites92.6%
if 1.99999995e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 90.2%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (log (- 1.0 u1))) (t_1 (cos (* (* 2.0 (PI)) u2))))
(if (<= (* (sqrt (- t_0)) t_1) 0.014999999664723873)
(* (sqrt u1) t_1)
(* (* (sqrt 2.0) (sqrt (* -0.5 t_0))) 1.0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \log \left(1 - u1\right)\\
t_1 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;\sqrt{-t\_0} \cdot t\_1 \leq 0.014999999664723873:\\
\;\;\;\;\sqrt{u1} \cdot t\_1\\
\mathbf{else}:\\
\;\;\;\;\left(\sqrt{2} \cdot \sqrt{-0.5 \cdot t\_0}\right) \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.0149999997Initial program 39.6%
Applied rewrites53.5%
Taylor expanded in u1 around 0
lower-sqrt.f3290.4
Applied rewrites90.4%
if 0.0149999997 < (*.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.5%
Taylor expanded in u2 around 0
Applied rewrites80.3%
lift-sqrt.f32N/A
lift-neg.f32N/A
neg-mul-1N/A
metadata-evalN/A
associate-*r*N/A
lift-log.f32N/A
log-powN/A
lift-pow.f32N/A
lift-log.f32N/A
sqrt-prodN/A
lower-*.f32N/A
lower-sqrt.f32N/A
lower-sqrt.f3276.9
lift-log.f32N/A
lift-pow.f32N/A
log-powN/A
lift-log.f32N/A
lower-*.f3280.5
lift-log.f32N/A
lift--.f32N/A
sub-negN/A
lift-neg.f32N/A
lower-log1p.f3249.9
Applied rewrites49.9%
lift-log1p.f32N/A
lift-neg.f32N/A
sub-negN/A
lower-log.f32N/A
lift--.f3280.5
Applied rewrites80.5%
(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.014999999664723873) (* (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.014999999664723873:\\
\;\;\;\;\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.0149999997Initial program 39.6%
Applied rewrites51.4%
Taylor expanded in u1 around 0
lower-sqrt.f3290.4
Applied rewrites90.4%
if 0.0149999997 < (*.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.5%
Taylor expanded in u2 around 0
Applied rewrites80.3%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (- (log (- 1.0 u1))))))
(if (<= (* t_0 (cos (* (* 2.0 (PI)) u2))) 0.014000000432133675)
(* (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.014000000432133675:\\
\;\;\;\;\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.0140000004Initial program 39.0%
Taylor expanded in u2 around 0
Applied rewrites33.3%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3273.4
Applied rewrites73.4%
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.3%
Taylor expanded in u2 around 0
Applied rewrites79.7%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.9998000264167786)
(* (sqrt (- (log (- 1.0 u1)))) t_0)
(* (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.9998000264167786:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{u1} \cdot t\_0\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999800026Initial program 90.2%
if 0.999800026 < (-.f32 #s(literal 1 binary32) u1) Initial program 36.7%
Applied rewrites54.9%
Taylor expanded in u1 around 0
lower-sqrt.f3292.5
Applied rewrites92.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(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 59.3%
Taylor expanded in u2 around 0
Applied rewrites51.6%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3264.1
Applied rewrites64.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (/ 0.0 0.0) 1.0))
float code(float cosTheta_i, float u1, float u2) {
return (0.0f / 0.0f) * 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 = (0.0e0 / 0.0e0) * 1.0e0
end function
function code(cosTheta_i, u1, u2) return Float32(Float32(Float32(0.0) / Float32(0.0)) * Float32(1.0)) end
function tmp = code(cosTheta_i, u1, u2) tmp = (single(0.0) / single(0.0)) * single(1.0); end
\begin{array}{l}
\\
\frac{0}{0} \cdot 1
\end{array}
Initial program 59.3%
Taylor expanded in u2 around 0
Applied rewrites51.6%
lift-sqrt.f32N/A
lift-neg.f32N/A
neg-mul-1N/A
metadata-evalN/A
associate-*r*N/A
lift-log.f32N/A
log-powN/A
lift-pow.f32N/A
lift-log.f32N/A
sqrt-prodN/A
lower-*.f32N/A
lower-sqrt.f32N/A
lower-sqrt.f3247.3
lift-log.f32N/A
lift-pow.f32N/A
log-powN/A
lift-log.f32N/A
lower-*.f3251.7
lift-log.f32N/A
lift--.f32N/A
sub-negN/A
lift-neg.f32N/A
lower-log1p.f3263.8
Applied rewrites63.8%
Applied rewrites-0.0%
herbie shell --seed 2024271
(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))))