
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf(-logf((1.0f - u1))) * cosf(((2.0f * ((float) M_PI)) * u2));
}
function code(cosTheta_i, u1, u2) return Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2))) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt(-log((single(1.0) - u1))) * cos(((single(2.0) * single(pi)) * u2)); end
\begin{array}{l}
\\
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf(-logf((1.0f - u1))) * cosf(((2.0f * ((float) M_PI)) * u2));
}
function code(cosTheta_i, u1, u2) return Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2))) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt(-log((single(1.0) - u1))) * cos(((single(2.0) * single(pi)) * u2)); end
\begin{array}{l}
\\
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)
\end{array}
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (cos (* 2.0 (* PI u2))) (sqrt (- (log1p (- u1))))))
float code(float cosTheta_i, float u1, float u2) {
return cosf((2.0f * (((float) M_PI) * u2))) * sqrtf(-log1pf(-u1));
}
function code(cosTheta_i, u1, u2) return Float32(cos(Float32(Float32(2.0) * Float32(Float32(pi) * u2))) * sqrt(Float32(-log1p(Float32(-u1))))) end
\begin{array}{l}
\\
\cos \left(2 \cdot \left(\pi \cdot u2\right)\right) \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)}
\end{array}
Initial program 56.9%
sub-neg56.9%
log1p-def98.8%
associate-*l*98.8%
Simplified98.8%
Final simplification98.8%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* u2 (* 2.0 PI)))))
(if (<= t_0 0.9999974966049194)
(* t_0 (sqrt (- u1 (* u1 (* u1 -0.5)))))
(sqrt (- (log1p (- u1)))))))
float code(float cosTheta_i, float u1, float u2) {
float t_0 = cosf((u2 * (2.0f * ((float) M_PI))));
float tmp;
if (t_0 <= 0.9999974966049194f) {
tmp = t_0 * sqrtf((u1 - (u1 * (u1 * -0.5f))));
} else {
tmp = sqrtf(-log1pf(-u1));
}
return tmp;
}
function code(cosTheta_i, u1, u2) t_0 = cos(Float32(u2 * Float32(Float32(2.0) * Float32(pi)))) tmp = Float32(0.0) if (t_0 <= Float32(0.9999974966049194)) tmp = Float32(t_0 * sqrt(Float32(u1 - Float32(u1 * Float32(u1 * Float32(-0.5)))))); else tmp = sqrt(Float32(-log1p(Float32(-u1)))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)\\
\mathbf{if}\;t_0 \leq 0.9999974966049194:\\
\;\;\;\;t_0 \cdot \sqrt{u1 - u1 \cdot \left(u1 \cdot -0.5\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\
\end{array}
\end{array}
if (cos.f32 (*.f32 (*.f32 2 (PI.f32)) u2)) < 0.999997497Initial program 56.6%
Taylor expanded in u1 around 0 86.5%
+-commutative45.4%
mul-1-neg45.4%
unsub-neg45.4%
unpow245.4%
associate-*r*45.4%
Simplified86.5%
if 0.999997497 < (cos.f32 (*.f32 (*.f32 2 (PI.f32)) u2)) Initial program 57.2%
sub-neg57.2%
log1p-def99.6%
associate-*l*99.6%
Simplified99.6%
add-cbrt-cube99.6%
pow1/399.6%
add-exp-log99.6%
pow-exp99.6%
pow399.6%
log-pow99.6%
Applied egg-rr99.6%
*-commutative99.6%
associate-*r*99.6%
metadata-eval99.6%
*-un-lft-identity99.6%
add-exp-log99.6%
add-sqr-sqrt99.6%
associate-*r*99.6%
Applied egg-rr99.6%
Taylor expanded in u2 around 0 98.5%
Final simplification93.2%
(FPCore (cosTheta_i u1 u2) :precision binary32 (if (<= (* u2 (* 2.0 PI)) 0.006000000052154064) (sqrt (- (log1p (- u1)))) (* (cos (* 2.0 (* PI u2))) (sqrt u1))))
float code(float cosTheta_i, float u1, float u2) {
float tmp;
if ((u2 * (2.0f * ((float) M_PI))) <= 0.006000000052154064f) {
tmp = sqrtf(-log1pf(-u1));
} else {
tmp = cosf((2.0f * (((float) M_PI) * u2))) * sqrtf(u1);
}
return tmp;
}
function code(cosTheta_i, u1, u2) tmp = Float32(0.0) if (Float32(u2 * Float32(Float32(2.0) * Float32(pi))) <= Float32(0.006000000052154064)) tmp = sqrt(Float32(-log1p(Float32(-u1)))); else tmp = Float32(cos(Float32(Float32(2.0) * Float32(Float32(pi) * u2))) * sqrt(u1)); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;u2 \cdot \left(2 \cdot \pi\right) \leq 0.006000000052154064:\\
\;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\cos \left(2 \cdot \left(\pi \cdot u2\right)\right) \cdot \sqrt{u1}\\
\end{array}
\end{array}
if (*.f32 (*.f32 2 (PI.f32)) u2) < 0.00600000005Initial program 58.1%
sub-neg58.1%
log1p-def99.5%
associate-*l*99.5%
Simplified99.5%
add-cbrt-cube99.5%
pow1/399.5%
add-exp-log99.5%
pow-exp99.5%
pow399.5%
log-pow99.5%
Applied egg-rr99.5%
*-commutative99.5%
associate-*r*99.5%
metadata-eval99.5%
*-un-lft-identity99.5%
add-exp-log99.5%
add-sqr-sqrt99.5%
associate-*r*99.5%
Applied egg-rr99.5%
Taylor expanded in u2 around 0 96.6%
if 0.00600000005 < (*.f32 (*.f32 2 (PI.f32)) u2) Initial program 55.0%
sub-neg55.0%
log1p-def97.7%
associate-*l*97.7%
Simplified97.7%
add-cbrt-cube97.7%
pow1/394.5%
Applied egg-rr72.4%
Taylor expanded in u1 around 0 76.0%
Final simplification88.7%
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt (- (log1p (- u1)))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf(-log1pf(-u1));
}
function code(cosTheta_i, u1, u2) return sqrt(Float32(-log1p(Float32(-u1)))) end
\begin{array}{l}
\\
\sqrt{-\mathsf{log1p}\left(-u1\right)}
\end{array}
Initial program 56.9%
sub-neg56.9%
log1p-def98.8%
associate-*l*98.8%
Simplified98.8%
add-cbrt-cube98.8%
pow1/398.8%
add-exp-log98.8%
pow-exp98.7%
pow398.8%
log-pow98.7%
Applied egg-rr98.7%
*-commutative98.7%
associate-*r*98.8%
metadata-eval98.8%
*-un-lft-identity98.8%
add-exp-log98.8%
add-sqr-sqrt98.7%
associate-*r*98.8%
Applied egg-rr98.8%
Taylor expanded in u2 around 0 75.3%
Final simplification75.3%
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt (* u1 (- 1.0 (* u1 -0.5)))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf((u1 * (1.0f - (u1 * -0.5f))));
}
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 - (u1 * (-0.5e0)))))
end function
function code(cosTheta_i, u1, u2) return sqrt(Float32(u1 * Float32(Float32(1.0) - Float32(u1 * Float32(-0.5))))) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt((u1 * (single(1.0) - (u1 * single(-0.5))))); end
\begin{array}{l}
\\
\sqrt{u1 \cdot \left(1 - u1 \cdot -0.5\right)}
\end{array}
Initial program 56.9%
Taylor expanded in u2 around 0 47.7%
Taylor expanded in u1 around 0 68.7%
+-commutative68.7%
mul-1-neg68.7%
unsub-neg68.7%
unpow268.7%
associate-*r*68.7%
Simplified68.7%
*-un-lft-identity68.7%
distribute-rgt-out--68.6%
*-commutative68.6%
Applied egg-rr68.6%
Final simplification68.6%
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt (- u1 (* u1 (* u1 -0.5)))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf((u1 - (u1 * (u1 * -0.5f))));
}
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 - (u1 * (u1 * (-0.5e0)))))
end function
function code(cosTheta_i, u1, u2) return sqrt(Float32(u1 - Float32(u1 * Float32(u1 * Float32(-0.5))))) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt((u1 - (u1 * (u1 * single(-0.5))))); end
\begin{array}{l}
\\
\sqrt{u1 - u1 \cdot \left(u1 \cdot -0.5\right)}
\end{array}
Initial program 56.9%
Taylor expanded in u2 around 0 47.7%
Taylor expanded in u1 around 0 68.7%
+-commutative68.7%
mul-1-neg68.7%
unsub-neg68.7%
unpow268.7%
associate-*r*68.7%
Simplified68.7%
Final simplification68.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 56.9%
Taylor expanded in u2 around 0 47.7%
Taylor expanded in u1 around 0 61.8%
mul-1-neg61.8%
Simplified61.8%
Final simplification61.8%
herbie shell --seed 2023200
(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))))