
(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 9 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
(fma
(/ (sqrt (+ 1.0 (* cosTheta -2.0))) (* cosTheta (sqrt PI)))
(pow (exp (- cosTheta)) cosTheta)
c))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + fmaf((sqrtf((1.0f + (cosTheta * -2.0f))) / (cosTheta * sqrtf(((float) M_PI)))), powf(expf(-cosTheta), cosTheta), c));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + fma(Float32(sqrt(Float32(Float32(1.0) + Float32(cosTheta * Float32(-2.0)))) / Float32(cosTheta * sqrt(Float32(pi)))), (exp(Float32(-cosTheta)) ^ cosTheta), c))) end
\begin{array}{l}
\\
\frac{1}{1 + \mathsf{fma}\left(\frac{\sqrt{1 + cosTheta \cdot -2}}{cosTheta \cdot \sqrt{\pi}}, {\left(e^{-cosTheta}\right)}^{cosTheta}, c\right)}
\end{array}
Initial program 97.5%
associate-+l+97.5%
+-commutative97.5%
fma-define97.5%
Simplified98.4%
Final simplification98.4%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
1.0
(fma
(/ (sqrt (/ (fma cosTheta -2.0 1.0) PI)) cosTheta)
(pow (exp (- cosTheta)) cosTheta)
c))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + fmaf((sqrtf((fmaf(cosTheta, -2.0f, 1.0f) / ((float) M_PI))) / cosTheta), powf(expf(-cosTheta), cosTheta), c));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + fma(Float32(sqrt(Float32(fma(cosTheta, Float32(-2.0), Float32(1.0)) / Float32(pi))) / cosTheta), (exp(Float32(-cosTheta)) ^ cosTheta), c))) end
\begin{array}{l}
\\
\frac{1}{1 + \mathsf{fma}\left(\frac{\sqrt{\frac{\mathsf{fma}\left(cosTheta, -2, 1\right)}{\pi}}}{cosTheta}, {\left(e^{-cosTheta}\right)}^{cosTheta}, c\right)}
\end{array}
Initial program 97.5%
associate-+l+97.5%
+-commutative97.5%
fma-define97.5%
Simplified98.4%
*-un-lft-identity98.4%
associate-/r*97.8%
sqrt-undiv97.9%
+-commutative97.9%
fma-define97.9%
Applied egg-rr97.9%
*-lft-identity97.9%
Simplified97.9%
Final simplification97.9%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
1.0
(/
(*
(exp (- (pow cosTheta 2.0)))
(sqrt (* cosTheta (- (/ 1.0 (* cosTheta PI)) (/ 2.0 PI)))))
cosTheta))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + ((expf(-powf(cosTheta, 2.0f)) * sqrtf((cosTheta * ((1.0f / (cosTheta * ((float) M_PI))) - (2.0f / ((float) M_PI)))))) / cosTheta));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(exp(Float32(-(cosTheta ^ Float32(2.0)))) * sqrt(Float32(cosTheta * Float32(Float32(Float32(1.0) / Float32(cosTheta * Float32(pi))) - Float32(Float32(2.0) / Float32(pi)))))) / cosTheta))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + ((exp(-(cosTheta ^ single(2.0))) * sqrt((cosTheta * ((single(1.0) / (cosTheta * single(pi))) - (single(2.0) / single(pi)))))) / cosTheta)); end
\begin{array}{l}
\\
\frac{1}{1 + \frac{e^{-{cosTheta}^{2}} \cdot \sqrt{cosTheta \cdot \left(\frac{1}{cosTheta \cdot \pi} - \frac{2}{\pi}\right)}}{cosTheta}}
\end{array}
Initial program 97.5%
+-commutative97.5%
fma-define97.5%
associate-*l/98.2%
*-lft-identity98.2%
associate--l-98.1%
sub-neg98.1%
neg-mul-198.1%
distribute-lft-out98.1%
distribute-rgt-out98.1%
metadata-eval98.1%
distribute-lft-neg-out98.1%
distribute-rgt-neg-out98.1%
exp-prod98.1%
Simplified98.1%
clear-num98.1%
inv-pow98.1%
+-commutative98.1%
fma-define98.1%
Applied egg-rr98.1%
unpow-198.1%
Simplified98.1%
Taylor expanded in c around 0 97.4%
associate-*l/97.7%
neg-mul-197.7%
+-commutative97.7%
*-commutative97.7%
fma-undefine97.7%
Simplified97.7%
Taylor expanded in cosTheta around inf 97.7%
associate-*r/97.7%
metadata-eval97.7%
Simplified97.7%
Final simplification97.7%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
1.0
(/
(*
(exp (- (pow cosTheta 2.0)))
(sqrt (+ (* -2.0 (/ cosTheta PI)) (/ 1.0 PI))))
cosTheta))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + ((expf(-powf(cosTheta, 2.0f)) * sqrtf(((-2.0f * (cosTheta / ((float) M_PI))) + (1.0f / ((float) M_PI))))) / cosTheta));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(exp(Float32(-(cosTheta ^ Float32(2.0)))) * sqrt(Float32(Float32(Float32(-2.0) * Float32(cosTheta / Float32(pi))) + Float32(Float32(1.0) / Float32(pi))))) / cosTheta))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + ((exp(-(cosTheta ^ single(2.0))) * sqrt(((single(-2.0) * (cosTheta / single(pi))) + (single(1.0) / single(pi))))) / cosTheta)); end
\begin{array}{l}
\\
\frac{1}{1 + \frac{e^{-{cosTheta}^{2}} \cdot \sqrt{-2 \cdot \frac{cosTheta}{\pi} + \frac{1}{\pi}}}{cosTheta}}
\end{array}
Initial program 97.5%
+-commutative97.5%
fma-define97.5%
associate-*l/98.2%
*-lft-identity98.2%
associate--l-98.1%
sub-neg98.1%
neg-mul-198.1%
distribute-lft-out98.1%
distribute-rgt-out98.1%
metadata-eval98.1%
distribute-lft-neg-out98.1%
distribute-rgt-neg-out98.1%
exp-prod98.1%
Simplified98.1%
clear-num98.1%
inv-pow98.1%
+-commutative98.1%
fma-define98.1%
Applied egg-rr98.1%
unpow-198.1%
Simplified98.1%
Taylor expanded in c around 0 97.4%
associate-*l/97.7%
neg-mul-197.7%
+-commutative97.7%
*-commutative97.7%
fma-undefine97.7%
Simplified97.7%
Taylor expanded in cosTheta around 0 97.7%
Final simplification97.7%
(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.5%
+-commutative97.5%
fma-define97.5%
associate-*l/98.2%
*-lft-identity98.2%
associate--l-98.1%
sub-neg98.1%
neg-mul-198.1%
distribute-lft-out98.1%
distribute-rgt-out98.1%
metadata-eval98.1%
distribute-lft-neg-out98.1%
distribute-rgt-neg-out98.1%
exp-prod98.1%
Simplified98.1%
clear-num98.1%
inv-pow98.1%
+-commutative98.1%
fma-define98.1%
Applied egg-rr98.1%
unpow-198.1%
Simplified98.1%
inv-pow98.1%
add-sqr-sqrt97.3%
unpow-prod-down97.2%
Applied egg-rr97.2%
pow-sqr97.7%
associate-*l/97.7%
*-lft-identity97.7%
metadata-eval97.7%
Simplified97.7%
Taylor expanded in c around 0 97.4%
Final simplification97.4%
(FPCore (cosTheta c) :precision binary32 (* cosTheta (+ (sqrt PI) (* (* cosTheta PI) (+ -1.0 (- (sqrt (/ 1.0 PI)) c))))))
float code(float cosTheta, float c) {
return cosTheta * (sqrtf(((float) M_PI)) + ((cosTheta * ((float) M_PI)) * (-1.0f + (sqrtf((1.0f / ((float) M_PI))) - c))));
}
function code(cosTheta, c) return Float32(cosTheta * Float32(sqrt(Float32(pi)) + Float32(Float32(cosTheta * Float32(pi)) * Float32(Float32(-1.0) + Float32(sqrt(Float32(Float32(1.0) / Float32(pi))) - c))))) end
function tmp = code(cosTheta, c) tmp = cosTheta * (sqrt(single(pi)) + ((cosTheta * single(pi)) * (single(-1.0) + (sqrt((single(1.0) / single(pi))) - c)))); end
\begin{array}{l}
\\
cosTheta \cdot \left(\sqrt{\pi} + \left(cosTheta \cdot \pi\right) \cdot \left(-1 + \left(\sqrt{\frac{1}{\pi}} - c\right)\right)\right)
\end{array}
Initial program 97.5%
associate-+l+97.5%
+-commutative97.5%
fma-define97.5%
Simplified98.4%
Taylor expanded in cosTheta around 0 96.7%
mul-1-neg96.7%
unsub-neg96.7%
*-commutative96.7%
*-commutative96.7%
associate-*l*96.7%
mul-1-neg96.7%
unsub-neg96.7%
*-commutative96.7%
Simplified96.7%
Final simplification96.7%
(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.5%
+-commutative97.5%
fma-define97.5%
associate-*l/98.2%
*-lft-identity98.2%
associate--l-98.1%
sub-neg98.1%
neg-mul-198.1%
distribute-lft-out98.1%
distribute-rgt-out98.1%
metadata-eval98.1%
distribute-lft-neg-out98.1%
distribute-rgt-neg-out98.1%
exp-prod98.1%
Simplified98.1%
clear-num98.1%
inv-pow98.1%
+-commutative98.1%
fma-define98.1%
Applied egg-rr98.1%
unpow-198.1%
Simplified98.1%
Taylor expanded in c around 0 97.4%
associate-*l/97.7%
neg-mul-197.7%
+-commutative97.7%
*-commutative97.7%
fma-undefine97.7%
Simplified97.7%
Taylor expanded in cosTheta around 0 96.5%
mul-1-neg96.5%
unsub-neg96.5%
associate-*r*96.5%
mul-1-neg96.5%
unsub-neg96.5%
Simplified96.5%
Final simplification96.5%
(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.5%
associate-+l+97.5%
+-commutative97.5%
fma-define97.5%
Simplified98.4%
Taylor expanded in cosTheta around 0 94.4%
Final simplification94.4%
(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}
\\
1 - c
\end{array}
Initial program 97.5%
*-un-lft-identity97.5%
add-cube-cbrt97.5%
prod-diff97.5%
sub-neg97.5%
add-sqr-sqrt-0.0%
sqrt-unprod93.1%
sqr-neg93.1%
sqrt-unprod93.1%
add-sqr-sqrt93.1%
pow293.1%
pow293.1%
pow293.1%
Applied egg-rr93.1%
fma-undefine93.1%
*-lft-identity93.1%
associate-+l+93.1%
metadata-eval93.1%
rem-square-sqrt93.1%
rem-square-sqrt93.1%
hypot-undefine93.1%
hypot-undefine93.1%
associate-+l+93.1%
Simplified93.1%
Taylor expanded in cosTheta around inf 10.2%
Taylor expanded in c around 0 10.2%
mul-1-neg10.2%
unsub-neg10.2%
Simplified10.2%
Final simplification10.2%
herbie shell --seed 2024067
(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))))))