
(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 (cbrt (PI))))
(if (<= (- 1.0 u1) 0.9943000078201294)
(* (cos (* (* (pow t_0 2.0) (* 2.0 u2)) t_0)) (sqrt (- (log (- 1.0 u1)))))
(*
(cos (* (* (PI) 2.0) u2))
(sqrt (- (/ (* 1.0 u1) (- -1.0 (* -0.5 u1)))))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\mathsf{PI}\left(\right)}\\
\mathbf{if}\;1 - u1 \leq 0.9943000078201294:\\
\;\;\;\;\cos \left(\left({t\_0}^{2} \cdot \left(2 \cdot u2\right)\right) \cdot t\_0\right) \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\cos \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right) \cdot \sqrt{-\frac{1 \cdot u1}{-1 - -0.5 \cdot u1}}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.994300008Initial program 96.3%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*r*N/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lift-PI.f32N/A
lower-cbrt.f3296.3
Applied rewrites96.3%
if 0.994300008 < (-.f32 #s(literal 1 binary32) u1) Initial program 44.5%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3269.2
Applied rewrites69.5%
Applied rewrites98.0%
Taylor expanded in u1 around 0
Applied rewrites98.5%
Final simplification98.0%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* (PI) 2.0) u2))))
(if (<= t_0 0.9999899864196777)
(* (sqrt u1) t_0)
(*
1.0
(sqrt (- (/ (* (- 1.0 (* (* u1 u1) 0.25)) u1) (- -1.0 (* -0.5 u1)))))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \leq 0.9999899864196777:\\
\;\;\;\;\sqrt{u1} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;1 \cdot \sqrt{-\frac{\left(1 - \left(u1 \cdot u1\right) \cdot 0.25\right) \cdot u1}{-1 - -0.5 \cdot u1}}\\
\end{array}
\end{array}
if (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2)) < 0.999989986Initial program 58.1%
Applied rewrites10.8%
Taylor expanded in u1 around 0
lower-sqrt.f3277.5
Applied rewrites77.5%
if 0.999989986 < (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2)) Initial program 56.6%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3276.1
Applied rewrites75.8%
Applied rewrites87.2%
Taylor expanded in u2 around 0
Applied rewrites86.1%
Final simplification83.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* (* (PI) 2.0) u2))))
(if (<= (- 1.0 u1) 0.9943000078201294)
(* t_0 (sqrt (- (log (- 1.0 u1)))))
(* t_0 (sqrt (- (/ (* 1.0 u1) (- -1.0 (* -0.5 u1)))))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)\\
\mathbf{if}\;1 - u1 \leq 0.9943000078201294:\\
\;\;\;\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \sqrt{-\frac{1 \cdot u1}{-1 - -0.5 \cdot u1}}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u1) < 0.994300008Initial program 96.3%
if 0.994300008 < (-.f32 #s(literal 1 binary32) u1) Initial program 44.5%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3269.7
Applied rewrites69.0%
Applied rewrites98.0%
Taylor expanded in u1 around 0
Applied rewrites98.5%
Final simplification98.0%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (cos (* (* (PI) 2.0) u2)) (sqrt (- (/ (* 1.0 u1) (- -1.0 (* -0.5 u1)))))))
\begin{array}{l}
\\
\cos \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right) \cdot \sqrt{-\frac{1 \cdot u1}{-1 - -0.5 \cdot u1}}
\end{array}
Initial program 57.0%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3260.9
Applied rewrites60.6%
Applied rewrites87.9%
Taylor expanded in u1 around 0
Applied rewrites89.7%
Final simplification89.7%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (+ (* (* 0.5 u1) u1) u1)) (cos (* (* (PI) 2.0) u2))))
\begin{array}{l}
\\
\sqrt{\left(0.5 \cdot u1\right) \cdot u1 + u1} \cdot \cos \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)
\end{array}
Initial program 57.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3276.5
Applied rewrites76.5%
Taylor expanded in u1 around 0
*-commutativeN/A
lft-mult-inverseN/A
distribute-rgt-inN/A
+-commutativeN/A
lower-*.f32N/A
distribute-rgt-inN/A
lft-mult-inverseN/A
lower-fma.f3261.9
Applied rewrites61.2%
Applied rewrites87.9%
Final simplification87.9%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (* (- 1.0 (* -0.5 u1)) u1)) (cos (* (* (PI) 2.0) u2))))
\begin{array}{l}
\\
\sqrt{\left(1 - -0.5 \cdot u1\right) \cdot u1} \cdot \cos \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot u2\right)
\end{array}
Initial program 57.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3276.5
Applied rewrites76.5%
Taylor expanded in u1 around 0
*-commutativeN/A
lft-mult-inverseN/A
distribute-rgt-inN/A
+-commutativeN/A
lower-*.f32N/A
distribute-rgt-inN/A
lft-mult-inverseN/A
lower-fma.f3261.2
Applied rewrites61.6%
Applied rewrites87.8%
Final simplification87.8%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* 1.0 (sqrt (- (/ (* (- 1.0 (* (* u1 u1) 0.25)) u1) (- -1.0 (* -0.5 u1)))))))
float code(float cosTheta_i, float u1, float u2) {
return 1.0f * sqrtf(-(((1.0f - ((u1 * u1) * 0.25f)) * u1) / (-1.0f - (-0.5f * 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(-(((1.0e0 - ((u1 * u1) * 0.25e0)) * u1) / ((-1.0e0) - ((-0.5e0) * u1))))
end function
function code(cosTheta_i, u1, u2) return Float32(Float32(1.0) * sqrt(Float32(-Float32(Float32(Float32(Float32(1.0) - Float32(Float32(u1 * u1) * Float32(0.25))) * u1) / Float32(Float32(-1.0) - Float32(Float32(-0.5) * u1)))))) end
function tmp = code(cosTheta_i, u1, u2) tmp = single(1.0) * sqrt(-(((single(1.0) - ((u1 * u1) * single(0.25))) * u1) / (single(-1.0) - (single(-0.5) * u1)))); end
\begin{array}{l}
\\
1 \cdot \sqrt{-\frac{\left(1 - \left(u1 \cdot u1\right) \cdot 0.25\right) \cdot u1}{-1 - -0.5 \cdot u1}}
\end{array}
Initial program 57.0%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3260.8
Applied rewrites61.0%
Applied rewrites87.9%
Taylor expanded in u2 around 0
Applied rewrites73.4%
Final simplification73.4%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (* (- (* -0.5 u1) 1.0) u1))) 1.0))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf(-(((-0.5f * u1) - 1.0f) * 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(-((((-0.5e0) * u1) - 1.0e0) * u1)) * 1.0e0
end function
function code(cosTheta_i, u1, u2) return Float32(sqrt(Float32(-Float32(Float32(Float32(Float32(-0.5) * u1) - Float32(1.0)) * u1))) * Float32(1.0)) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt(-(((single(-0.5) * u1) - single(1.0)) * u1)) * single(1.0); end
\begin{array}{l}
\\
\sqrt{-\left(-0.5 \cdot u1 - 1\right) \cdot u1} \cdot 1
\end{array}
Initial program 57.0%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3260.4
Applied rewrites60.4%
Applied rewrites87.8%
Taylor expanded in u2 around 0
Applied rewrites73.3%
(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.0%
Taylor expanded in u1 around 0
mul-1-negN/A
lower-neg.f3276.5
Applied rewrites76.5%
Taylor expanded in u2 around 0
Applied rewrites65.8%
(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.0%
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.9%
herbie shell --seed 2024267
(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))))