
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(* (/ 1.0 (sqrt PI)) (/ (sqrt (- (- 1.0 cosTheta) cosTheta)) cosTheta))
(exp (* (- cosTheta) cosTheta))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (((1.0f / sqrtf(((float) M_PI))) * (sqrtf(((1.0f - cosTheta) - cosTheta)) / cosTheta)) * expf((-cosTheta * cosTheta))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(Float32(Float32(Float32(1.0) / sqrt(Float32(pi))) * Float32(sqrt(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta)) / cosTheta)) * exp(Float32(Float32(-cosTheta) * cosTheta))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / ((single(1.0) + c) + (((single(1.0) / sqrt(single(pi))) * (sqrt(((single(1.0) - cosTheta) - cosTheta)) / cosTheta)) * exp((-cosTheta * cosTheta)))); end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \left(\frac{1}{\sqrt{\pi}} \cdot \frac{\sqrt{\left(1 - cosTheta\right) - cosTheta}}{cosTheta}\right) \cdot e^{\left(-cosTheta\right) \cdot cosTheta}}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(* (/ 1.0 (sqrt PI)) (/ (sqrt (- (- 1.0 cosTheta) cosTheta)) cosTheta))
(exp (* (- cosTheta) cosTheta))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (((1.0f / sqrtf(((float) M_PI))) * (sqrtf(((1.0f - cosTheta) - cosTheta)) / cosTheta)) * expf((-cosTheta * cosTheta))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(Float32(Float32(Float32(1.0) / sqrt(Float32(pi))) * Float32(sqrt(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta)) / cosTheta)) * exp(Float32(Float32(-cosTheta) * cosTheta))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / ((single(1.0) + c) + (((single(1.0) / sqrt(single(pi))) * (sqrt(((single(1.0) - cosTheta) - cosTheta)) / cosTheta)) * exp((-cosTheta * cosTheta)))); end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \left(\frac{1}{\sqrt{\pi}} \cdot \frac{\sqrt{\left(1 - cosTheta\right) - cosTheta}}{cosTheta}\right) \cdot e^{\left(-cosTheta\right) \cdot cosTheta}}
\end{array}
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(/ (sqrt (fma cosTheta -2.0 1.0)) (* cosTheta (sqrt PI)))
(exp (* cosTheta (- cosTheta)))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + ((sqrtf(fmaf(cosTheta, -2.0f, 1.0f)) / (cosTheta * sqrtf(((float) M_PI)))) * expf((cosTheta * -cosTheta))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(Float32(sqrt(fma(cosTheta, Float32(-2.0), Float32(1.0))) / Float32(cosTheta * sqrt(Float32(pi)))) * exp(Float32(cosTheta * Float32(-cosTheta)))))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{cosTheta \cdot \sqrt{\pi}} \cdot e^{cosTheta \cdot \left(-cosTheta\right)}}
\end{array}
Initial program 97.8%
*-un-lft-identity97.8%
inv-pow97.8%
sqrt-pow297.8%
metadata-eval97.8%
Applied egg-rr97.8%
*-lft-identity97.8%
Simplified97.8%
associate-*r/98.1%
associate--l-98.1%
Applied egg-rr98.1%
clear-num98.0%
inv-pow98.0%
count-298.0%
Applied egg-rr98.0%
unpow-198.0%
associate-/r*98.0%
cancel-sign-sub-inv98.0%
metadata-eval98.0%
+-commutative98.0%
*-commutative98.0%
fma-undefine98.0%
Simplified98.0%
associate-/r/98.0%
div-inv98.8%
pow-flip98.8%
metadata-eval98.8%
pow1/298.8%
Applied egg-rr98.8%
associate-*l/98.9%
*-lft-identity98.9%
Simplified98.9%
Final simplification98.9%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(exp (* cosTheta (- cosTheta)))
(/ (sqrt (/ (fma cosTheta -2.0 1.0) PI)) cosTheta)))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (expf((cosTheta * -cosTheta)) * (sqrtf((fmaf(cosTheta, -2.0f, 1.0f) / ((float) M_PI))) / cosTheta)));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(exp(Float32(cosTheta * Float32(-cosTheta))) * Float32(sqrt(Float32(fma(cosTheta, Float32(-2.0), Float32(1.0)) / Float32(pi))) / cosTheta)))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + e^{cosTheta \cdot \left(-cosTheta\right)} \cdot \frac{\sqrt{\frac{\mathsf{fma}\left(cosTheta, -2, 1\right)}{\pi}}}{cosTheta}}
\end{array}
Initial program 97.8%
*-un-lft-identity97.8%
inv-pow97.8%
sqrt-pow297.8%
metadata-eval97.8%
Applied egg-rr97.8%
*-lft-identity97.8%
Simplified97.8%
associate-*r/98.1%
associate--l-98.1%
Applied egg-rr98.1%
clear-num98.0%
inv-pow98.0%
count-298.0%
Applied egg-rr98.0%
unpow-198.0%
associate-/r*98.0%
cancel-sign-sub-inv98.0%
metadata-eval98.0%
+-commutative98.0%
*-commutative98.0%
fma-undefine98.0%
Simplified98.0%
associate-/l/98.0%
pow1/298.0%
metadata-eval98.0%
pow-pow98.0%
inv-pow98.0%
unpow-prod-down98.2%
div-inv98.2%
pow1/298.2%
clear-num98.2%
*-un-lft-identity98.2%
Applied egg-rr98.2%
*-lft-identity98.2%
Simplified98.2%
Final simplification98.2%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
1.0
(*
(/ (exp (- (pow cosTheta 2.0))) cosTheta)
(sqrt (/ (- 1.0 (* cosTheta 2.0)) PI))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + ((expf(-powf(cosTheta, 2.0f)) / cosTheta) * sqrtf(((1.0f - (cosTheta * 2.0f)) / ((float) M_PI)))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(exp(Float32(-(cosTheta ^ Float32(2.0)))) / cosTheta) * sqrt(Float32(Float32(Float32(1.0) - Float32(cosTheta * Float32(2.0))) / Float32(pi)))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + ((exp(-(cosTheta ^ single(2.0))) / cosTheta) * sqrt(((single(1.0) - (cosTheta * single(2.0))) / single(pi))))); end
\begin{array}{l}
\\
\frac{1}{1 + \frac{e^{-{cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - cosTheta \cdot 2}{\pi}}}
\end{array}
Initial program 97.8%
Taylor expanded in c around 0 97.5%
Final simplification97.5%
(FPCore (cosTheta c) :precision binary32 (* cosTheta (+ (sqrt PI) (* (* cosTheta PI) (+ (pow PI -0.5) (- -1.0 c))))))
float code(float cosTheta, float c) {
return cosTheta * (sqrtf(((float) M_PI)) + ((cosTheta * ((float) M_PI)) * (powf(((float) M_PI), -0.5f) + (-1.0f - c))));
}
function code(cosTheta, c) return Float32(cosTheta * Float32(sqrt(Float32(pi)) + Float32(Float32(cosTheta * Float32(pi)) * Float32((Float32(pi) ^ Float32(-0.5)) + Float32(Float32(-1.0) - c))))) end
function tmp = code(cosTheta, c) tmp = cosTheta * (sqrt(single(pi)) + ((cosTheta * single(pi)) * ((single(pi) ^ single(-0.5)) + (single(-1.0) - c)))); end
\begin{array}{l}
\\
cosTheta \cdot \left(\sqrt{\pi} + \left(cosTheta \cdot \pi\right) \cdot \left({\pi}^{-0.5} + \left(-1 - c\right)\right)\right)
\end{array}
Initial program 97.8%
associate-+l+97.8%
+-commutative97.8%
fma-define97.8%
times-frac98.8%
*-lft-identity98.8%
associate--l-98.9%
sub-neg98.9%
neg-mul-198.9%
distribute-lft-out98.9%
distribute-rgt-out98.9%
metadata-eval98.9%
exp-prod98.9%
Simplified98.9%
*-un-lft-identity98.9%
times-frac97.8%
inv-pow97.8%
sqrt-pow297.8%
metadata-eval97.8%
+-commutative97.8%
fma-define97.8%
Applied egg-rr97.8%
*-commutative97.8%
associate-*l/98.1%
*-lft-identity98.1%
times-frac98.1%
/-rgt-identity98.1%
Simplified98.1%
Taylor expanded in cosTheta around 0 95.9%
mul-1-neg95.9%
unsub-neg95.9%
associate-*r*95.9%
mul-1-neg95.9%
unsub-neg95.9%
Simplified95.9%
associate-+r-95.9%
pow1/295.9%
inv-pow95.9%
pow-pow95.9%
metadata-eval95.9%
Applied egg-rr95.9%
Final simplification95.9%
(FPCore (cosTheta c) :precision binary32 (* cosTheta (- (sqrt PI) (* (* cosTheta PI) (- 1.0 (sqrt (/ 1.0 PI)))))))
float code(float cosTheta, float c) {
return cosTheta * (sqrtf(((float) M_PI)) - ((cosTheta * ((float) M_PI)) * (1.0f - sqrtf((1.0f / ((float) M_PI))))));
}
function code(cosTheta, c) return Float32(cosTheta * Float32(sqrt(Float32(pi)) - Float32(Float32(cosTheta * Float32(pi)) * Float32(Float32(1.0) - sqrt(Float32(Float32(1.0) / Float32(pi))))))) end
function tmp = code(cosTheta, c) tmp = cosTheta * (sqrt(single(pi)) - ((cosTheta * single(pi)) * (single(1.0) - sqrt((single(1.0) / single(pi)))))); end
\begin{array}{l}
\\
cosTheta \cdot \left(\sqrt{\pi} - \left(cosTheta \cdot \pi\right) \cdot \left(1 - \sqrt{\frac{1}{\pi}}\right)\right)
\end{array}
Initial program 97.8%
associate-+l+97.8%
+-commutative97.8%
fma-define97.8%
times-frac98.8%
*-lft-identity98.8%
associate--l-98.9%
sub-neg98.9%
neg-mul-198.9%
distribute-lft-out98.9%
distribute-rgt-out98.9%
metadata-eval98.9%
exp-prod98.9%
Simplified98.9%
*-un-lft-identity98.9%
times-frac97.8%
inv-pow97.8%
sqrt-pow297.8%
metadata-eval97.8%
+-commutative97.8%
fma-define97.8%
Applied egg-rr97.8%
*-commutative97.8%
associate-*l/98.1%
*-lft-identity98.1%
times-frac98.1%
/-rgt-identity98.1%
Simplified98.1%
Taylor expanded in cosTheta around 0 95.9%
mul-1-neg95.9%
unsub-neg95.9%
associate-*r*95.9%
mul-1-neg95.9%
unsub-neg95.9%
Simplified95.9%
Taylor expanded in c around 0 95.6%
Final simplification95.6%
(FPCore (cosTheta c) :precision binary32 (* cosTheta (sqrt PI)))
float code(float cosTheta, float c) {
return cosTheta * sqrtf(((float) M_PI));
}
function code(cosTheta, c) return Float32(cosTheta * sqrt(Float32(pi))) end
function tmp = code(cosTheta, c) tmp = cosTheta * sqrt(single(pi)); end
\begin{array}{l}
\\
cosTheta \cdot \sqrt{\pi}
\end{array}
Initial program 97.8%
associate-+l+97.8%
+-commutative97.8%
fma-define97.8%
times-frac98.8%
*-lft-identity98.8%
associate--l-98.9%
sub-neg98.9%
neg-mul-198.9%
distribute-lft-out98.9%
distribute-rgt-out98.9%
metadata-eval98.9%
exp-prod98.9%
Simplified98.9%
*-un-lft-identity98.9%
times-frac97.8%
inv-pow97.8%
sqrt-pow297.8%
metadata-eval97.8%
+-commutative97.8%
fma-define97.8%
Applied egg-rr97.8%
*-commutative97.8%
associate-*l/98.1%
*-lft-identity98.1%
times-frac98.1%
/-rgt-identity98.1%
Simplified98.1%
Taylor expanded in cosTheta around 0 93.1%
Final simplification93.1%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 c))
float code(float cosTheta, float c) {
return 1.0f / c;
}
real(4) function code(costheta, c)
real(4), intent (in) :: costheta
real(4), intent (in) :: c
code = 1.0e0 / c
end function
function code(cosTheta, c) return Float32(Float32(1.0) / c) end
function tmp = code(cosTheta, c) tmp = single(1.0) / c; end
\begin{array}{l}
\\
\frac{1}{c}
\end{array}
Initial program 97.8%
associate-+l+97.8%
+-commutative97.8%
fma-define97.8%
times-frac98.8%
*-lft-identity98.8%
associate--l-98.9%
sub-neg98.9%
neg-mul-198.9%
distribute-lft-out98.9%
distribute-rgt-out98.9%
metadata-eval98.9%
exp-prod98.9%
Simplified98.9%
Taylor expanded in c around inf 4.7%
Final simplification4.7%
herbie shell --seed 2024075
(FPCore (cosTheta c)
:name "Beckmann Sample, normalization factor"
:precision binary32
:pre (and (and (< 0.0 cosTheta) (< cosTheta 0.9999)) (and (< -1.0 c) (< c 1.0)))
(/ 1.0 (+ (+ 1.0 c) (* (* (/ 1.0 (sqrt PI)) (/ (sqrt (- (- 1.0 cosTheta) cosTheta)) cosTheta)) (exp (* (- cosTheta) cosTheta))))))