
(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 (- (log (- 1.0 u1)))) (t_1 (sqrt (PI))))
(if (<= t_0 0.0001221999991685152)
(*
(sqrt (/ (* (+ u1 1.0) (* (- u1) u1)) (- u1)))
(cos (* (* 2.0 (PI)) u2)))
(* (sqrt t_0) (cos (* (* (* u2 2.0) t_1) t_1))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
t_1 := \sqrt{\mathsf{PI}\left(\right)}\\
\mathbf{if}\;t\_0 \leq 0.0001221999991685152:\\
\;\;\;\;\sqrt{\frac{\left(u1 + 1\right) \cdot \left(\left(-u1\right) \cdot u1\right)}{-u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_0} \cdot \cos \left(\left(\left(u2 \cdot 2\right) \cdot t\_1\right) \cdot t\_1\right)\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.222e-4Initial program 36.3%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.6
Applied rewrites92.6%
+-lft-identityN/A
flip-+N/A
lower-/.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower--.f3292.6
Applied rewrites92.6%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
lower-neg.f32N/A
unpow2N/A
lower-*.f3292.6
Applied rewrites92.6%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.6
Applied rewrites92.6%
if 1.222e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 88.1%
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.f3288.1
Applied rewrites88.1%
Final simplification90.8%
(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.015799999237060547)
(* (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.015799999237060547:\\
\;\;\;\;\sqrt{\frac{\left(u1 + 1\right) \cdot \left(\left(-u1\right) \cdot u1\right)}{-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.0158Initial program 42.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3289.0
Applied rewrites89.0%
+-lft-identityN/A
flip-+N/A
lower-/.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower--.f3289.0
Applied rewrites89.0%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
lower-neg.f32N/A
unpow2N/A
lower-*.f3289.0
Applied rewrites89.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3289.0
Applied rewrites89.0%
if 0.0158 < (*.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 91.0%
Taylor expanded in u2 around 0
Applied rewrites79.5%
Final simplification86.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.015799999237060547)
(* (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.015799999237060547:\\
\;\;\;\;\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.0158Initial program 42.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3289.0
Applied rewrites89.0%
+-lft-identityN/A
flip-+N/A
lower-/.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower--.f3289.0
Applied rewrites89.0%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
lower-neg.f32N/A
unpow2N/A
lower-*.f3289.0
Applied rewrites89.0%
lift--.f32N/A
sub0-negN/A
lift-neg.f32N/A
remove-double-negN/A
Applied rewrites89.0%
if 0.0158 < (*.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 91.0%
Taylor expanded in u2 around 0
Applied rewrites79.5%
Final simplification86.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.015799999237060547)
(* (sqrt (/ (* (- 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.015799999237060547:\\
\;\;\;\;\sqrt{\frac{\left(-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.0158Initial program 42.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3289.0
Applied rewrites89.0%
+-lft-identityN/A
flip-+N/A
lower-/.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower--.f3289.0
Applied rewrites89.0%
Taylor expanded in u1 around 0
mul-1-negN/A
unpow2N/A
distribute-lft-neg-inN/A
mul-1-negN/A
lower-*.f32N/A
mul-1-negN/A
lower-neg.f3289.0
Applied rewrites89.0%
if 0.0158 < (*.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 91.0%
Taylor expanded in u2 around 0
Applied rewrites79.5%
Final simplification86.0%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (- (log (- 1.0 u1)))))
(if (<= t_0 0.0001221999991685152)
(*
(sqrt (/ (* (+ u1 1.0) (* (- u1) u1)) (- u1)))
(cos (* (* 2.0 (PI)) u2)))
(* (sqrt t_0) (/ 1.0 (/ 1.0 (cos (* u2 (* (PI) 2.0)))))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\log \left(1 - u1\right)\\
\mathbf{if}\;t\_0 \leq 0.0001221999991685152:\\
\;\;\;\;\sqrt{\frac{\left(u1 + 1\right) \cdot \left(\left(-u1\right) \cdot u1\right)}{-u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_0} \cdot \frac{1}{\frac{1}{\cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)}}\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.222e-4Initial program 36.3%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.6
Applied rewrites92.6%
+-lft-identityN/A
flip-+N/A
lower-/.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower--.f3292.6
Applied rewrites92.6%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
lower-neg.f32N/A
unpow2N/A
lower-*.f3292.6
Applied rewrites92.6%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.6
Applied rewrites92.6%
if 1.222e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 88.1%
lift-cos.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
cos-2N/A
flip--N/A
cos-sin-sumN/A
lower-/.f32N/A
Applied rewrites88.0%
lift-/.f32N/A
clear-numN/A
lower-/.f32N/A
lower-/.f3287.9
/-rgt-identityN/A
lift--.f32N/A
lift-pow.f32N/A
sqr-powN/A
lift-pow.f32N/A
sqr-powN/A
cos-sin-sumN/A
Applied rewrites88.1%
Final simplification90.8%
(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.015799999237060547) (* (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.015799999237060547:\\
\;\;\;\;\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.0158Initial program 42.0%
Applied rewrites52.9%
Taylor expanded in u1 around 0
lower-sqrt.f3289.0
Applied rewrites89.0%
if 0.0158 < (*.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 91.0%
Taylor expanded in u2 around 0
Applied rewrites79.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (sqrt (- (log (- 1.0 u1))))))
(if (<= (* t_0 (cos (* (* 2.0 (PI)) u2))) 0.009999999776482582)
(sqrt u1)
(* 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.009999999776482582:\\
\;\;\;\;\sqrt{u1}\\
\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.00999999978Initial program 37.6%
Applied rewrites51.2%
Taylor expanded in u2 around 0
lower-sqrt.f32N/A
lower-log1p.f3273.1
Applied rewrites73.1%
Taylor expanded in u1 around 0
Applied rewrites73.1%
if 0.00999999978 < (*.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.6%
Taylor expanded in u2 around 0
Applied rewrites76.7%
(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.0001221999991685152)
(* (sqrt (/ (* (+ u1 1.0) (* (- u1) u1)) (- u1))) t_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.0001221999991685152:\\
\;\;\;\;\sqrt{\frac{\left(u1 + 1\right) \cdot \left(\left(-u1\right) \cdot u1\right)}{-u1}} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{t\_1} \cdot t\_0\\
\end{array}
\end{array}
if (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) < 1.222e-4Initial program 36.3%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.6
Applied rewrites92.6%
+-lft-identityN/A
flip-+N/A
lower-/.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower--.f3292.6
Applied rewrites92.6%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
lower-neg.f32N/A
unpow2N/A
lower-*.f3292.6
Applied rewrites92.6%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3292.6
Applied rewrites92.6%
if 1.222e-4 < (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1))) Initial program 88.1%
Final simplification90.7%
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt u1))
float code(float cosTheta_i, float u1, float u2) {
return 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 = sqrt(u1)
end function
function code(cosTheta_i, u1, u2) return sqrt(u1) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt(u1); end
\begin{array}{l}
\\
\sqrt{u1}
\end{array}
Initial program 57.5%
Applied rewrites45.0%
Taylor expanded in u2 around 0
lower-sqrt.f32N/A
lower-log1p.f3264.8
Applied rewrites64.8%
Taylor expanded in u1 around 0
Applied rewrites64.8%
herbie shell --seed 2024314
(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))))