
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf((u1 / (1.0f - u1))) * cosf((6.28318530718f * u2));
}
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))) * cos((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2) return Float32(sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) * cos(Float32(Float32(6.28318530718) * u2))) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt((u1 / (single(1.0) - u1))) * cos((single(6.28318530718) * u2)); end
\begin{array}{l}
\\
\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right)
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 16 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf((u1 / (1.0f - u1))) * cosf((6.28318530718f * u2));
}
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))) * cos((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2) return Float32(sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) * cos(Float32(Float32(6.28318530718) * u2))) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt((u1 / (single(1.0) - u1))) * cos((single(6.28318530718) * u2)); end
\begin{array}{l}
\\
\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right)
\end{array}
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (/ 1.0 (+ (/ 1.0 u1) -1.0))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf((1.0f / ((1.0f / u1) + -1.0f))) * cosf((6.28318530718f * u2));
}
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((1.0e0 / ((1.0e0 / u1) + (-1.0e0)))) * cos((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2) return Float32(sqrt(Float32(Float32(1.0) / Float32(Float32(Float32(1.0) / u1) + Float32(-1.0)))) * cos(Float32(Float32(6.28318530718) * u2))) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt((single(1.0) / ((single(1.0) / u1) + single(-1.0)))) * cos((single(6.28318530718) * u2)); end
\begin{array}{l}
\\
\sqrt{\frac{1}{\frac{1}{u1} + -1}} \cdot \cos \left(6.28318530718 \cdot u2\right)
\end{array}
Initial program 99.1%
lift-/.f32N/A
clear-numN/A
lower-/.f32N/A
lift--.f32N/A
div-subN/A
sub-negN/A
*-inversesN/A
metadata-evalN/A
lower-+.f32N/A
lower-/.f3299.1
Applied rewrites99.1%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (cos (* 6.28318530718 u2))) (t_1 (sqrt (/ u1 (- 1.0 u1)))))
(if (<= (* t_0 t_1) 0.054999999701976776)
(* t_0 (sqrt (fma u1 (fma u1 u1 u1) u1)))
(fma
(* t_1 (fma (* u2 u2) -85.45681720672748 64.93939402268539))
(* (* u2 u2) (* u2 u2))
(* t_1 (fma -19.739208802181317 (* u2 u2) 1.0))))))
float code(float cosTheta_i, float u1, float u2) {
float t_0 = cosf((6.28318530718f * u2));
float t_1 = sqrtf((u1 / (1.0f - u1)));
float tmp;
if ((t_0 * t_1) <= 0.054999999701976776f) {
tmp = t_0 * sqrtf(fmaf(u1, fmaf(u1, u1, u1), u1));
} else {
tmp = fmaf((t_1 * fmaf((u2 * u2), -85.45681720672748f, 64.93939402268539f)), ((u2 * u2) * (u2 * u2)), (t_1 * fmaf(-19.739208802181317f, (u2 * u2), 1.0f)));
}
return tmp;
}
function code(cosTheta_i, u1, u2) t_0 = cos(Float32(Float32(6.28318530718) * u2)) t_1 = sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) tmp = Float32(0.0) if (Float32(t_0 * t_1) <= Float32(0.054999999701976776)) tmp = Float32(t_0 * sqrt(fma(u1, fma(u1, u1, u1), u1))); else tmp = fma(Float32(t_1 * fma(Float32(u2 * u2), Float32(-85.45681720672748), Float32(64.93939402268539))), Float32(Float32(u2 * u2) * Float32(u2 * u2)), Float32(t_1 * fma(Float32(-19.739208802181317), Float32(u2 * u2), Float32(1.0)))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(6.28318530718 \cdot u2\right)\\
t_1 := \sqrt{\frac{u1}{1 - u1}}\\
\mathbf{if}\;t\_0 \cdot t\_1 \leq 0.054999999701976776:\\
\;\;\;\;t\_0 \cdot \sqrt{\mathsf{fma}\left(u1, \mathsf{fma}\left(u1, u1, u1\right), u1\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t\_1 \cdot \mathsf{fma}\left(u2 \cdot u2, -85.45681720672748, 64.93939402268539\right), \left(u2 \cdot u2\right) \cdot \left(u2 \cdot u2\right), t\_1 \cdot \mathsf{fma}\left(-19.739208802181317, u2 \cdot u2, 1\right)\right)\\
\end{array}
\end{array}
if (*.f32 (sqrt.f32 (/.f32 u1 (-.f32 #s(literal 1 binary32) u1))) (cos.f32 (*.f32 #s(literal 314159265359/50000000000 binary32) u2))) < 0.0549999997Initial program 99.0%
Taylor expanded in u1 around 0
+-commutativeN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f32N/A
+-commutativeN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f3298.5
Applied rewrites98.5%
if 0.0549999997 < (*.f32 (sqrt.f32 (/.f32 u1 (-.f32 #s(literal 1 binary32) u1))) (cos.f32 (*.f32 #s(literal 314159265359/50000000000 binary32) u2))) Initial program 99.2%
Taylor expanded in u2 around 0
*-rgt-identityN/A
sub-negN/A
rgt-mult-inverseN/A
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
*-rgt-identityN/A
distribute-lft-inN/A
+-commutativeN/A
sub-negN/A
associate-*r*N/A
lower-sqrt.f32N/A
*-rgt-identityN/A
lower-/.f32N/A
associate-*r*N/A
sub-negN/A
+-commutativeN/A
distribute-lft-inN/A
Applied rewrites85.5%
Taylor expanded in u2 around 0
Applied rewrites97.3%
Final simplification98.2%
herbie shell --seed 2024228
(FPCore (cosTheta_i u1 u2)
:name "Trowbridge-Reitz 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 (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))