
(FPCore (alphax alphay u0 cos2phi sin2phi) :precision binary32 (/ (- (log (- 1.0 u0))) (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
return -logf((1.0f - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
real(4), intent (in) :: alphax
real(4), intent (in) :: alphay
real(4), intent (in) :: u0
real(4), intent (in) :: cos2phi
real(4), intent (in) :: sin2phi
code = -log((1.0e0 - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi) return Float32(Float32(-log(Float32(Float32(1.0) - u0))) / Float32(Float32(cos2phi / Float32(alphax * alphax)) + Float32(sin2phi / Float32(alphay * alphay)))) end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi) tmp = -log((single(1.0) - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay))); end
\begin{array}{l}
\\
\frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (alphax alphay u0 cos2phi sin2phi) :precision binary32 (/ (- (log (- 1.0 u0))) (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
return -logf((1.0f - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
real(4), intent (in) :: alphax
real(4), intent (in) :: alphay
real(4), intent (in) :: u0
real(4), intent (in) :: cos2phi
real(4), intent (in) :: sin2phi
code = -log((1.0e0 - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi) return Float32(Float32(-log(Float32(Float32(1.0) - u0))) / Float32(Float32(cos2phi / Float32(alphax * alphax)) + Float32(sin2phi / Float32(alphay * alphay)))) end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi) tmp = -log((single(1.0) - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay))); end
\begin{array}{l}
\\
\frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}
\end{array}
(FPCore (alphax alphay u0 cos2phi sin2phi)
:precision binary32
(let* ((t_0 (+ (/ sin2phi (* alphay alphay)) (/ cos2phi (* alphax alphax)))))
(if (<= (- 1.0 u0) 0.9965999722480774)
(/ (- (log (- 1.0 u0))) t_0)
(/ (- (* (+ 1.0 (* -0.5 u0)) u0) (* (- u0) u0)) t_0))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
float t_0 = (sin2phi / (alphay * alphay)) + (cos2phi / (alphax * alphax));
float tmp;
if ((1.0f - u0) <= 0.9965999722480774f) {
tmp = -logf((1.0f - u0)) / t_0;
} else {
tmp = (((1.0f + (-0.5f * u0)) * u0) - (-u0 * u0)) / t_0;
}
return tmp;
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
real(4), intent (in) :: alphax
real(4), intent (in) :: alphay
real(4), intent (in) :: u0
real(4), intent (in) :: cos2phi
real(4), intent (in) :: sin2phi
real(4) :: t_0
real(4) :: tmp
t_0 = (sin2phi / (alphay * alphay)) + (cos2phi / (alphax * alphax))
if ((1.0e0 - u0) <= 0.9965999722480774e0) then
tmp = -log((1.0e0 - u0)) / t_0
else
tmp = (((1.0e0 + ((-0.5e0) * u0)) * u0) - (-u0 * u0)) / t_0
end if
code = tmp
end function
function code(alphax, alphay, u0, cos2phi, sin2phi) t_0 = Float32(Float32(sin2phi / Float32(alphay * alphay)) + Float32(cos2phi / Float32(alphax * alphax))) tmp = Float32(0.0) if (Float32(Float32(1.0) - u0) <= Float32(0.9965999722480774)) tmp = Float32(Float32(-log(Float32(Float32(1.0) - u0))) / t_0); else tmp = Float32(Float32(Float32(Float32(Float32(1.0) + Float32(Float32(-0.5) * u0)) * u0) - Float32(Float32(-u0) * u0)) / t_0); end return tmp end
function tmp_2 = code(alphax, alphay, u0, cos2phi, sin2phi) t_0 = (sin2phi / (alphay * alphay)) + (cos2phi / (alphax * alphax)); tmp = single(0.0); if ((single(1.0) - u0) <= single(0.9965999722480774)) tmp = -log((single(1.0) - u0)) / t_0; else tmp = (((single(1.0) + (single(-0.5) * u0)) * u0) - (-u0 * u0)) / t_0; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{sin2phi}{alphay \cdot alphay} + \frac{cos2phi}{alphax \cdot alphax}\\
\mathbf{if}\;1 - u0 \leq 0.9965999722480774:\\
\;\;\;\;\frac{-\log \left(1 - u0\right)}{t\_0}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(1 + -0.5 \cdot u0\right) \cdot u0 - \left(-u0\right) \cdot u0}{t\_0}\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) u0) < 0.996599972Initial program 92.6%
if 0.996599972 < (-.f32 #s(literal 1 binary32) u0) Initial program 50.2%
lift-log.f32N/A
lift--.f32N/A
flip--N/A
log-divN/A
lower--.f32N/A
metadata-evalN/A
sub-negN/A
lower-log1p.f32N/A
distribute-lft-neg-inN/A
lower-*.f32N/A
lower-neg.f32N/A
lower-log1p.f3286.3
Applied rewrites86.3%
Taylor expanded in u0 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3286.4
Applied rewrites86.3%
Applied rewrites97.7%
Taylor expanded in u0 around 0
unpow2N/A
associate-*r*N/A
lower-*.f32N/A
mul-1-negN/A
lower-neg.f3297.7
Applied rewrites97.7%
Final simplification96.5%
(FPCore (alphax alphay u0 cos2phi sin2phi) :precision binary32 (/ (- (* (+ 1.0 (* -0.5 u0)) u0) (* (- u0) u0)) (+ (/ sin2phi (* alphay alphay)) (/ cos2phi (* alphax alphax)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
return (((1.0f + (-0.5f * u0)) * u0) - (-u0 * u0)) / ((sin2phi / (alphay * alphay)) + (cos2phi / (alphax * alphax)));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
real(4), intent (in) :: alphax
real(4), intent (in) :: alphay
real(4), intent (in) :: u0
real(4), intent (in) :: cos2phi
real(4), intent (in) :: sin2phi
code = (((1.0e0 + ((-0.5e0) * u0)) * u0) - (-u0 * u0)) / ((sin2phi / (alphay * alphay)) + (cos2phi / (alphax * alphax)))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi) return Float32(Float32(Float32(Float32(Float32(1.0) + Float32(Float32(-0.5) * u0)) * u0) - Float32(Float32(-u0) * u0)) / Float32(Float32(sin2phi / Float32(alphay * alphay)) + Float32(cos2phi / Float32(alphax * alphax)))) end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi) tmp = (((single(1.0) + (single(-0.5) * u0)) * u0) - (-u0 * u0)) / ((sin2phi / (alphay * alphay)) + (cos2phi / (alphax * alphax))); end
\begin{array}{l}
\\
\frac{\left(1 + -0.5 \cdot u0\right) \cdot u0 - \left(-u0\right) \cdot u0}{\frac{sin2phi}{alphay \cdot alphay} + \frac{cos2phi}{alphax \cdot alphax}}
\end{array}
Initial program 60.5%
lift-log.f32N/A
lift--.f32N/A
flip--N/A
log-divN/A
lower--.f32N/A
metadata-evalN/A
sub-negN/A
lower-log1p.f32N/A
distribute-lft-neg-inN/A
lower-*.f32N/A
lower-neg.f32N/A
lower-log1p.f3276.3
Applied rewrites76.3%
Taylor expanded in u0 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3276.3
Applied rewrites76.3%
Applied rewrites88.2%
Taylor expanded in u0 around 0
unpow2N/A
associate-*r*N/A
lower-*.f32N/A
mul-1-negN/A
lower-neg.f3288.2
Applied rewrites88.2%
Final simplification88.2%
(FPCore (alphax alphay u0 cos2phi sin2phi) :precision binary32 (/ (- u0) (- (/ 1.0 (* (/ -1.0 cos2phi) (* alphax alphax))) (/ sin2phi (* alphay alphay)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
return -u0 / ((1.0f / ((-1.0f / cos2phi) * (alphax * alphax))) - (sin2phi / (alphay * alphay)));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
real(4), intent (in) :: alphax
real(4), intent (in) :: alphay
real(4), intent (in) :: u0
real(4), intent (in) :: cos2phi
real(4), intent (in) :: sin2phi
code = -u0 / ((1.0e0 / (((-1.0e0) / cos2phi) * (alphax * alphax))) - (sin2phi / (alphay * alphay)))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi) return Float32(Float32(-u0) / Float32(Float32(Float32(1.0) / Float32(Float32(Float32(-1.0) / cos2phi) * Float32(alphax * alphax))) - Float32(sin2phi / Float32(alphay * alphay)))) end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi) tmp = -u0 / ((single(1.0) / ((single(-1.0) / cos2phi) * (alphax * alphax))) - (sin2phi / (alphay * alphay))); end
\begin{array}{l}
\\
\frac{-u0}{\frac{1}{\frac{-1}{cos2phi} \cdot \left(alphax \cdot alphax\right)} - \frac{sin2phi}{alphay \cdot alphay}}
\end{array}
Initial program 60.5%
lift-/.f32N/A
clear-numN/A
lower-/.f32N/A
lower-/.f3260.5
Applied rewrites60.5%
lift-/.f32N/A
clear-numN/A
associate-/r/N/A
lower-*.f32N/A
lower-/.f3260.5
Applied rewrites60.5%
Taylor expanded in u0 around 0
mul-1-negN/A
lower-neg.f3276.0
Applied rewrites76.0%
Final simplification76.0%
(FPCore (alphax alphay u0 cos2phi sin2phi) :precision binary32 (/ u0 (+ (/ cos2phi (* alphax alphax)) (/ 1.0 (/ alphay (/ sin2phi alphay))))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
return u0 / ((cos2phi / (alphax * alphax)) + (1.0f / (alphay / (sin2phi / alphay))));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
real(4), intent (in) :: alphax
real(4), intent (in) :: alphay
real(4), intent (in) :: u0
real(4), intent (in) :: cos2phi
real(4), intent (in) :: sin2phi
code = u0 / ((cos2phi / (alphax * alphax)) + (1.0e0 / (alphay / (sin2phi / alphay))))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi) return Float32(u0 / Float32(Float32(cos2phi / Float32(alphax * alphax)) + Float32(Float32(1.0) / Float32(alphay / Float32(sin2phi / alphay))))) end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi) tmp = u0 / ((cos2phi / (alphax * alphax)) + (single(1.0) / (alphay / (sin2phi / alphay)))); end
\begin{array}{l}
\\
\frac{u0}{\frac{cos2phi}{alphax \cdot alphax} + \frac{1}{\frac{alphay}{\frac{sin2phi}{alphay}}}}
\end{array}
Initial program 60.5%
Taylor expanded in u0 around 0
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f3276.0
Applied rewrites76.0%
Applied rewrites76.0%
Final simplification76.0%
(FPCore (alphax alphay u0 cos2phi sin2phi) :precision binary32 (/ u0 (+ (/ sin2phi (* alphay alphay)) (/ cos2phi (* alphax alphax)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
return u0 / ((sin2phi / (alphay * alphay)) + (cos2phi / (alphax * alphax)));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
real(4), intent (in) :: alphax
real(4), intent (in) :: alphay
real(4), intent (in) :: u0
real(4), intent (in) :: cos2phi
real(4), intent (in) :: sin2phi
code = u0 / ((sin2phi / (alphay * alphay)) + (cos2phi / (alphax * alphax)))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi) return Float32(u0 / Float32(Float32(sin2phi / Float32(alphay * alphay)) + Float32(cos2phi / Float32(alphax * alphax)))) end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi) tmp = u0 / ((sin2phi / (alphay * alphay)) + (cos2phi / (alphax * alphax))); end
\begin{array}{l}
\\
\frac{u0}{\frac{sin2phi}{alphay \cdot alphay} + \frac{cos2phi}{alphax \cdot alphax}}
\end{array}
Initial program 60.5%
Taylor expanded in u0 around 0
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f3276.0
Applied rewrites76.0%
(FPCore (alphax alphay u0 cos2phi sin2phi) :precision binary32 (if (<= sin2phi 2.1599999314535287e-20) (* (/ u0 cos2phi) (* alphax alphax)) (/ (* (* alphay alphay) u0) sin2phi)))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
float tmp;
if (sin2phi <= 2.1599999314535287e-20f) {
tmp = (u0 / cos2phi) * (alphax * alphax);
} else {
tmp = ((alphay * alphay) * u0) / sin2phi;
}
return tmp;
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
real(4), intent (in) :: alphax
real(4), intent (in) :: alphay
real(4), intent (in) :: u0
real(4), intent (in) :: cos2phi
real(4), intent (in) :: sin2phi
real(4) :: tmp
if (sin2phi <= 2.1599999314535287e-20) then
tmp = (u0 / cos2phi) * (alphax * alphax)
else
tmp = ((alphay * alphay) * u0) / sin2phi
end if
code = tmp
end function
function code(alphax, alphay, u0, cos2phi, sin2phi) tmp = Float32(0.0) if (sin2phi <= Float32(2.1599999314535287e-20)) tmp = Float32(Float32(u0 / cos2phi) * Float32(alphax * alphax)); else tmp = Float32(Float32(Float32(alphay * alphay) * u0) / sin2phi); end return tmp end
function tmp_2 = code(alphax, alphay, u0, cos2phi, sin2phi) tmp = single(0.0); if (sin2phi <= single(2.1599999314535287e-20)) tmp = (u0 / cos2phi) * (alphax * alphax); else tmp = ((alphay * alphay) * u0) / sin2phi; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;sin2phi \leq 2.1599999314535287 \cdot 10^{-20}:\\
\;\;\;\;\frac{u0}{cos2phi} \cdot \left(alphax \cdot alphax\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(alphay \cdot alphay\right) \cdot u0}{sin2phi}\\
\end{array}
\end{array}
if sin2phi < 2.15999993e-20Initial program 56.4%
Taylor expanded in u0 around 0
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f3274.4
Applied rewrites74.4%
Taylor expanded in alphax around 0
Applied rewrites56.9%
Applied rewrites56.9%
if 2.15999993e-20 < sin2phi Initial program 62.2%
Taylor expanded in u0 around 0
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f3276.7
Applied rewrites76.7%
Taylor expanded in alphax around inf
Applied rewrites71.2%
Final simplification67.0%
(FPCore (alphax alphay u0 cos2phi sin2phi) :precision binary32 (/ (* (* alphax alphax) u0) cos2phi))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
return ((alphax * alphax) * u0) / cos2phi;
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
real(4), intent (in) :: alphax
real(4), intent (in) :: alphay
real(4), intent (in) :: u0
real(4), intent (in) :: cos2phi
real(4), intent (in) :: sin2phi
code = ((alphax * alphax) * u0) / cos2phi
end function
function code(alphax, alphay, u0, cos2phi, sin2phi) return Float32(Float32(Float32(alphax * alphax) * u0) / cos2phi) end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi) tmp = ((alphax * alphax) * u0) / cos2phi; end
\begin{array}{l}
\\
\frac{\left(alphax \cdot alphax\right) \cdot u0}{cos2phi}
\end{array}
Initial program 60.5%
Taylor expanded in u0 around 0
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f3276.0
Applied rewrites76.0%
Taylor expanded in alphax around 0
Applied rewrites25.7%
Final simplification25.7%
(FPCore (alphax alphay u0 cos2phi sin2phi) :precision binary32 (* (/ u0 cos2phi) (* alphax alphax)))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
return (u0 / cos2phi) * (alphax * alphax);
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
real(4), intent (in) :: alphax
real(4), intent (in) :: alphay
real(4), intent (in) :: u0
real(4), intent (in) :: cos2phi
real(4), intent (in) :: sin2phi
code = (u0 / cos2phi) * (alphax * alphax)
end function
function code(alphax, alphay, u0, cos2phi, sin2phi) return Float32(Float32(u0 / cos2phi) * Float32(alphax * alphax)) end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi) tmp = (u0 / cos2phi) * (alphax * alphax); end
\begin{array}{l}
\\
\frac{u0}{cos2phi} \cdot \left(alphax \cdot alphax\right)
\end{array}
Initial program 60.5%
Taylor expanded in u0 around 0
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f32N/A
lower-/.f32N/A
unpow2N/A
lower-*.f3276.0
Applied rewrites76.0%
Taylor expanded in alphax around 0
Applied rewrites25.7%
Applied rewrites25.7%
Final simplification25.7%
herbie shell --seed 2024271
(FPCore (alphax alphay u0 cos2phi sin2phi)
:name "Beckmann Distribution sample, tan2theta, alphax != alphay, u1 <= 0.5"
:precision binary32
:pre (and (and (and (and (and (<= 0.0001 alphax) (<= alphax 1.0)) (and (<= 0.0001 alphay) (<= alphay 1.0))) (and (<= 2.328306437e-10 u0) (<= u0 1.0))) (and (<= 0.0 cos2phi) (<= cos2phi 1.0))) (<= 0.0 sin2phi))
(/ (- (log (- 1.0 u0))) (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))