
(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 6 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 (<= (- 1.0 u1) 0.9940000176429749)
(* (sqrt (- (log (- 1.0 u1)))) t_0)
(* (sqrt (- (* (/ 1.0 (- -1.0 (* -0.5 u1))) 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.9940000176429749:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\frac{1}{-1 - -0.5 \cdot u1} \cdot u1} \cdot t\_0\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.994000018Initial program 95.9%
if 0.994000018 < (-.f32 #s(literal 1 binary32) u1) Initial program 46.7%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3269.5
Applied rewrites68.3%
Applied rewrites97.9%
Taylor expanded in u1 around 0
Applied rewrites98.4%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (* (/ 1.0 (- -1.0 (* -0.5 u1))) u1))) (cos (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{-\frac{1}{-1 - -0.5 \cdot u1} \cdot u1} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 58.6%
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 rewrites88.2%
Taylor expanded in u1 around 0
Applied rewrites90.0%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (* (- (* -0.5 u1) 1.0) u1))) (cos (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{-\left(-0.5 \cdot u1 - 1\right) \cdot u1} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Initial program 58.6%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3261.6
Applied rewrites61.8%
Applied rewrites88.3%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (cos (* (* u2 2.0) (PI))) (sqrt u1)))
\begin{array}{l}
\\
\cos \left(\left(u2 \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{u1}
\end{array}
Initial program 58.6%
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.5
Applied rewrites3.5%
Applied rewrites76.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 58.6%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3276.5
Applied rewrites76.5%
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 58.6%
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.5
Applied rewrites3.5%
Taylor expanded in u2 around 0
Applied rewrites4.8%
herbie shell --seed 2024322
(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))))