
(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 10 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 (* (* 2.0 (PI)) u2))))
(if (<= (* t_0 (sqrt (- (log (- 1.0 u1))))) 0.0949999988079071)
(* (sqrt (- (- (* (* -0.5 u1) u1) u1))) t_0)
(* (sqrt 2.0) (sqrt (log (sqrt (/ 1.0 (- 1.0 u1)))))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)} \leq 0.0949999988079071:\\
\;\;\;\;\sqrt{-\left(\left(-0.5 \cdot u1\right) \cdot u1 - u1\right)} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{2} \cdot \sqrt{\log \left(\sqrt{\frac{1}{1 - u1}}\right)}\\
\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.0949999988Initial program 52.2%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3262.0
Applied rewrites61.1%
Applied rewrites96.6%
if 0.0949999988 < (*.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 95.9%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
inv-powN/A
sqr-powN/A
log-prodN/A
lower-+.f32N/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-evalN/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-eval93.2
Applied rewrites93.2%
Taylor expanded in u2 around 0
lower-*.f32N/A
lower-sqrt.f32N/A
lower-log.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower--.f32N/A
lower-sqrt.f3282.1
Applied rewrites82.1%
Final simplification93.3%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cbrt (PI))))
(if (<= (- 1.0 u1) 0.9937999844551086)
(* (cos (* (* (* u2 t_0) 2.0) (pow t_0 2.0))) (sqrt (- (log (- 1.0 u1)))))
(*
(cos (* (* 2.0 (PI)) u2))
(sqrt
(-
(*
(/ (fma (* (* u1 u1) 0.25) u1 (- u1)) (- (* -0.5 u1) -1.0))
(/ u1 u1))))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\mathsf{PI}\left(\right)}\\
\mathbf{if}\;1 - u1 \leq 0.9937999844551086:\\
\;\;\;\;\cos \left(\left(\left(u2 \cdot t\_0\right) \cdot 2\right) \cdot {t\_0}^{2}\right) \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \cdot \sqrt{-\frac{\mathsf{fma}\left(\left(u1 \cdot u1\right) \cdot 0.25, u1, -u1\right)}{-0.5 \cdot u1 - -1} \cdot \frac{u1}{u1}}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.993799984Initial program 95.2%
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.f3295.4
Applied rewrites95.4%
if 0.993799984 < (-.f32 #s(literal 1 binary32) u1) Initial program 50.7%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3263.5
Applied rewrites62.2%
Applied rewrites97.6%
Applied rewrites72.8%
Final simplification77.1%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (* t_0 (sqrt (- (log (- 1.0 u1))))) 0.17000000178813934)
(*
t_0
(sqrt
(-
(*
(/ (fma (* (* u1 u1) 0.25) u1 (- u1)) (- (* -0.5 u1) -1.0))
(/ u1 u1)))))
(* (sqrt 2.0) (sqrt (log (sqrt (/ 1.0 (- 1.0 u1)))))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)} \leq 0.17000000178813934:\\
\;\;\;\;t\_0 \cdot \sqrt{-\frac{\mathsf{fma}\left(\left(u1 \cdot u1\right) \cdot 0.25, u1, -u1\right)}{-0.5 \cdot u1 - -1} \cdot \frac{u1}{u1}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{2} \cdot \sqrt{\log \left(\sqrt{\frac{1}{1 - u1}}\right)}\\
\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.170000002Initial program 54.6%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3260.4
Applied rewrites59.5%
Applied rewrites95.1%
Applied rewrites71.0%
if 0.170000002 < (*.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 97.3%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
inv-powN/A
sqr-powN/A
log-prodN/A
lower-+.f32N/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-evalN/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-eval95.1
Applied rewrites95.1%
Taylor expanded in u2 around 0
lower-*.f32N/A
lower-sqrt.f32N/A
lower-log.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower--.f32N/A
lower-sqrt.f3284.6
Applied rewrites84.6%
Final simplification68.8%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (* t_0 (sqrt (- (log (- 1.0 u1))))) 0.17000000178813934)
(*
(sqrt (- (* (/ (fma 0.25 (* u1 u1) -1.0) (- (* -0.5 u1) -1.0)) u1)))
t_0)
(* (sqrt 2.0) (sqrt (log (sqrt (/ 1.0 (- 1.0 u1)))))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)} \leq 0.17000000178813934:\\
\;\;\;\;\sqrt{-\frac{\mathsf{fma}\left(0.25, u1 \cdot u1, -1\right)}{-0.5 \cdot u1 - -1} \cdot u1} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{2} \cdot \sqrt{\log \left(\sqrt{\frac{1}{1 - u1}}\right)}\\
\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.170000002Initial program 54.6%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3259.8
Applied rewrites59.8%
Applied rewrites95.0%
Applied rewrites70.2%
if 0.170000002 < (*.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 97.3%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
inv-powN/A
sqr-powN/A
log-prodN/A
lower-+.f32N/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-evalN/A
lower-log.f32N/A
lower-pow.f32N/A
metadata-eval95.1
Applied rewrites95.1%
Taylor expanded in u2 around 0
lower-*.f32N/A
lower-sqrt.f32N/A
lower-log.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower--.f32N/A
lower-sqrt.f3284.6
Applied rewrites84.6%
Final simplification69.9%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* 2.0 (PI)) u2))))
(if (<= (- 1.0 u1) 0.9937999844551086)
(* t_0 (sqrt (- (log (- 1.0 u1)))))
(*
t_0
(sqrt
(-
(*
(/ (fma (* (* u1 u1) 0.25) u1 (- u1)) (- (* -0.5 u1) -1.0))
(/ u1 u1))))))))\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.9937999844551086:\\
\;\;\;\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \sqrt{-\frac{\mathsf{fma}\left(\left(u1 \cdot u1\right) \cdot 0.25, u1, -u1\right)}{-0.5 \cdot u1 - -1} \cdot \frac{u1}{u1}}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.993799984Initial program 95.2%
if 0.993799984 < (-.f32 #s(literal 1 binary32) u1) Initial program 50.7%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3262.2
Applied rewrites62.0%
Applied rewrites97.6%
Applied rewrites74.1%
Final simplification75.5%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (- (* (* -0.5 u1) u1) u1))) (cos (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{-\left(\left(-0.5 \cdot u1\right) \cdot u1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 62.3%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3256.5
Applied rewrites56.2%
Applied rewrites87.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (cos (* (+ u2 u2) (PI))) (sqrt (- (* (- (* -0.5 u1) 1.0) u1)))))
\begin{array}{l}
\\
\cos \left(\left(u2 + u2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{-\left(-0.5 \cdot u1 - 1\right) \cdot u1}
\end{array}
Initial program 62.3%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3256.7
Applied rewrites57.0%
Applied rewrites87.1%
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
*-commutativeN/A
lift-PI.f32N/A
count-2N/A
lift-PI.f32N/A
lift-PI.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3287.1
Applied rewrites87.1%
Final simplification87.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt u1) (cos (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{u1} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 62.3%
Applied rewrites42.4%
Taylor expanded in u1 around 0
lower-sqrt.f3274.2
Applied rewrites74.2%
(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(Float32(-Float32(-u1)))) end
function tmp = code(cosTheta_i, u1, u2) tmp = single(1.0) * sqrt(-(-u1)); end
\begin{array}{l}
\\
1 \cdot \sqrt{-\left(-u1\right)}
\end{array}
Initial program 62.3%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3274.2
Applied rewrites74.2%
Taylor expanded in u2 around 0
Applied rewrites64.0%
Final simplification64.0%
(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 62.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.4
Applied rewrites3.4%
Taylor expanded in u2 around 0
Applied rewrites4.4%
herbie shell --seed 2024268
(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))))