
(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 (cos (* u2 (* (PI) 2.0))))
(t_1 (cbrt (PI)))
(t_2 (* u2 (PI)))
(t_3 (cos t_2)))
(if (<= (- 1.0 u1) 0.9998400211334229)
(* (sqrt (- (log (- 1.0 u1)))) (/ (- (+ t_0 1.0) (- 1.0 t_0)) 2.0))
(*
(sqrt (- (- u1)))
(- (* t_3 t_3) (* (sin (* (pow t_1 2.0) (* t_1 u2))) (sin t_2)))))))\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)}\\
t_2 := u2 \cdot \mathsf{PI}\left(\right)\\
t_3 := \cos t\_2\\
\mathbf{if}\;1 - u1 \leq 0.9998400211334229:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \frac{\left(t\_0 + 1\right) - \left(1 - t\_0\right)}{2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\left(-u1\right)} \cdot \left(t\_3 \cdot t\_3 - \sin \left({t\_1}^{2} \cdot \left(t\_1 \cdot u2\right)\right) \cdot \sin t\_2\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.999840021Initial program 90.3%
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 rewrites90.4%
if 0.999840021 < (-.f32 #s(literal 1 binary32) u1) Initial program 36.8%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3291.9
Applied rewrites91.9%
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 rewrites91.9%
lift-*.f32N/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/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
lift-PI.f32N/A
lower-cbrt.f3291.9
Applied rewrites91.9%
(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.023499999195337296)
(* (sqrt (/ (* (* (- (- u1) 1.0) u1) u1) (- 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.023499999195337296:\\
\;\;\;\;\sqrt{\frac{\left(\left(\left(-u1\right) - 1\right) \cdot u1\right) \cdot u1}{-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.0234999992Initial program 40.8%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3289.1
Applied rewrites89.1%
lift-neg.f32N/A
neg-sub0N/A
flip--N/A
lower-/.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower-+.f3289.1
Applied rewrites89.1%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
lower-neg.f3289.2
Applied rewrites89.2%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3289.2
Applied rewrites89.2%
if 0.0234999992 < (*.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.2%
Taylor expanded in u2 around 0
Applied rewrites83.7%
Final simplification87.4%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* u2 (* (PI) 2.0)))))
(if (<= (- 1.0 u1) 0.9998400211334229)
(* (sqrt (- (log (- 1.0 u1)))) (/ (- (+ t_0 1.0) (- 1.0 t_0)) 2.0))
(*
(sqrt (- (* (fma -0.5 u1 1.0) u1) (log1p (* (- u1) u1))))
(cos (* (* 2.0 (PI)) u2))))))\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.9998400211334229:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \frac{\left(t\_0 + 1\right) - \left(1 - t\_0\right)}{2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(-0.5, u1, 1\right) \cdot u1 - \mathsf{log1p}\left(\left(-u1\right) \cdot u1\right)} \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.999840021Initial program 90.3%
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 rewrites90.4%
if 0.999840021 < (-.f32 #s(literal 1 binary32) u1) Initial program 36.8%
Applied rewrites52.2%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3251.7
Applied rewrites51.0%
(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.023499999195337296) (* (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.023499999195337296:\\
\;\;\;\;\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.0234999992Initial program 40.8%
Applied rewrites50.1%
Taylor expanded in u1 around 0
lower-sqrt.f3289.1
Applied rewrites89.1%
if 0.0234999992 < (*.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.2%
Taylor expanded in u2 around 0
Applied rewrites83.7%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (- (log (- 1.0 u1))))))
(if (<= (* t_0 (cos (* (* 2.0 (PI)) u2))) 0.012500000186264515)
(* (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.012500000186264515:\\
\;\;\;\;\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.0125000002Initial program 38.6%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3290.5
Applied rewrites90.5%
Taylor expanded in u2 around 0
Applied rewrites75.7%
if 0.0125000002 < (*.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.6%
Taylor expanded in u2 around 0
Applied rewrites80.8%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (- (log (- 1.0 u1)))))
(if (<= t_0 0.00015999999595806003)
(*
(sqrt (/ (* (* (- (- u1) 1.0) u1) u1) (- u1)))
(cos (* (* 2.0 (PI)) u2)))
(* (sqrt t_0) (cos (* (PI) (+ u2 u2)))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
\mathbf{if}\;t\_0 \leq 0.00015999999595806003:\\
\;\;\;\;\sqrt{\frac{\left(\left(\left(-u1\right) - 1\right) \cdot u1\right) \cdot u1}{-u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_0} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.59999996e-4Initial program 37.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3291.7
Applied rewrites91.7%
lift-neg.f32N/A
neg-sub0N/A
flip--N/A
lower-/.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower-+.f3291.7
Applied rewrites91.7%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
lower-neg.f3291.8
Applied rewrites91.8%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3291.8
Applied rewrites91.8%
if 1.59999996e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 90.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.f3290.4
Applied rewrites90.4%
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
lift-*.f32N/A
*-commutativeN/A
lift-sqrt.f32N/A
lift-sqrt.f32N/A
rem-square-sqrtN/A
associate-*r*N/A
lift-*.f32N/A
count-2N/A
lift-*.f32N/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3290.6
Applied rewrites90.6%
Final simplification91.3%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= (- 1.0 u1) 0.9998400211334229)
(* (sqrt (- (log (- 1.0 u1)))) (cos (* (PI) (+ u2 u2))))
(*
(sqrt (- (* (fma -0.5 u1 1.0) u1) (log1p (* (- u1) u1))))
(cos (* (* 2.0 (PI)) u2)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;1 - u1 \leq 0.9998400211334229:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(-0.5, u1, 1\right) \cdot u1 - \mathsf{log1p}\left(\left(-u1\right) \cdot u1\right)} \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.999840021Initial program 90.3%
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.f3290.1
Applied rewrites90.1%
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
lift-*.f32N/A
*-commutativeN/A
lift-sqrt.f32N/A
lift-sqrt.f32N/A
rem-square-sqrtN/A
associate-*r*N/A
lift-*.f32N/A
count-2N/A
lift-*.f32N/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3290.3
Applied rewrites90.3%
if 0.999840021 < (-.f32 #s(literal 1 binary32) u1) Initial program 36.8%
Applied rewrites54.1%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3251.5
Applied rewrites50.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 57.7%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3275.7
Applied rewrites75.7%
Taylor expanded in u2 around 0
Applied rewrites65.6%
(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 57.7%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3275.7
Applied rewrites75.7%
Taylor expanded in u2 around 0
Applied rewrites65.6%
Taylor expanded in u1 around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
lower-*.f32N/A
lower-sqrt.f324.5
Applied rewrites4.5%
Applied rewrites4.5%
herbie shell --seed 2024307
(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))))