
(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 12 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
(+
c
(fma
(/ (sqrt (fma cosTheta -2.0 1.0)) (* cosTheta (sqrt PI)))
(pow (exp (- cosTheta)) cosTheta)
1.0))))
float code(float cosTheta, float c) {
return 1.0f / (c + fmaf((sqrtf(fmaf(cosTheta, -2.0f, 1.0f)) / (cosTheta * sqrtf(((float) M_PI)))), powf(expf(-cosTheta), cosTheta), 1.0f));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(c + fma(Float32(sqrt(fma(cosTheta, Float32(-2.0), Float32(1.0))) / Float32(cosTheta * sqrt(Float32(pi)))), (exp(Float32(-cosTheta)) ^ cosTheta), Float32(1.0)))) end
\begin{array}{l}
\\
\frac{1}{c + \mathsf{fma}\left(\frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{cosTheta \cdot \sqrt{\pi}}, {\left(e^{-cosTheta}\right)}^{cosTheta}, 1\right)}
\end{array}
Initial program 98.0%
+-commutative98.0%
associate-+r+98.0%
+-commutative98.0%
fma-def98.0%
Simplified98.6%
Final simplification98.6%
(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 98.0%
associate-+l+98.0%
+-commutative98.0%
fma-def98.0%
times-frac98.6%
*-lft-identity98.6%
associate--l-98.6%
count-298.6%
*-commutative98.6%
exp-prod98.6%
Simplified98.6%
Final simplification98.6%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(exp (* cosTheta (- cosTheta)))
(* (sqrt (fma cosTheta -2.0 1.0)) (/ (/ 1.0 cosTheta) (sqrt PI)))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (expf((cosTheta * -cosTheta)) * (sqrtf(fmaf(cosTheta, -2.0f, 1.0f)) * ((1.0f / cosTheta) / sqrtf(((float) M_PI))))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(exp(Float32(cosTheta * Float32(-cosTheta))) * Float32(sqrt(fma(cosTheta, Float32(-2.0), Float32(1.0))) * Float32(Float32(Float32(1.0) / cosTheta) / sqrt(Float32(pi))))))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + e^{cosTheta \cdot \left(-cosTheta\right)} \cdot \left(\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)} \cdot \frac{\frac{1}{cosTheta}}{\sqrt{\pi}}\right)}
\end{array}
Initial program 98.0%
associate-*l/98.4%
*-un-lft-identity98.4%
div-inv98.3%
associate-/l*98.4%
associate--l-98.4%
count-298.4%
cancel-sign-sub-inv98.4%
metadata-eval98.4%
*-commutative98.4%
+-commutative98.4%
fma-udef98.4%
Applied egg-rr98.4%
*-un-lft-identity98.4%
add-sqr-sqrt97.7%
times-frac97.5%
frac-2neg97.5%
frac-2neg97.5%
associate-/r/97.8%
/-rgt-identity97.8%
frac-2neg97.8%
frac-2neg97.8%
associate-/r/97.6%
*-commutative97.6%
/-rgt-identity97.6%
Applied egg-rr97.6%
associate-*l/97.7%
associate-/l*97.6%
associate-/l*97.7%
rem-square-sqrt98.5%
associate-/r/98.5%
*-commutative98.5%
associate-/r*98.4%
Simplified98.4%
Final simplification98.4%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(/ (sqrt (fma cosTheta -2.0 1.0)) (/ (sqrt PI) (/ 1.0 cosTheta)))
(exp (* cosTheta (- cosTheta)))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + ((sqrtf(fmaf(cosTheta, -2.0f, 1.0f)) / (sqrtf(((float) M_PI)) / (1.0f / cosTheta))) * 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(sqrt(Float32(pi)) / Float32(Float32(1.0) / cosTheta))) * exp(Float32(cosTheta * Float32(-cosTheta)))))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\frac{\sqrt{\pi}}{\frac{1}{cosTheta}}} \cdot e^{cosTheta \cdot \left(-cosTheta\right)}}
\end{array}
Initial program 98.0%
associate-*l/98.4%
*-un-lft-identity98.4%
div-inv98.3%
associate-/l*98.4%
associate--l-98.4%
count-298.4%
cancel-sign-sub-inv98.4%
metadata-eval98.4%
*-commutative98.4%
+-commutative98.4%
fma-udef98.4%
Applied egg-rr98.4%
Final simplification98.4%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(exp (* cosTheta (- cosTheta)))
(/ (/ (sqrt (fma cosTheta -2.0 1.0)) cosTheta) (sqrt PI))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (expf((cosTheta * -cosTheta)) * ((sqrtf(fmaf(cosTheta, -2.0f, 1.0f)) / cosTheta) / sqrtf(((float) M_PI)))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(exp(Float32(cosTheta * Float32(-cosTheta))) * Float32(Float32(sqrt(fma(cosTheta, Float32(-2.0), Float32(1.0))) / cosTheta) / sqrt(Float32(pi)))))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + e^{cosTheta \cdot \left(-cosTheta\right)} \cdot \frac{\frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{cosTheta}}{\sqrt{\pi}}}
\end{array}
Initial program 98.0%
frac-times98.7%
*-un-lft-identity98.7%
associate--l-98.6%
count-298.6%
cancel-sign-sub-inv98.6%
metadata-eval98.6%
*-commutative98.6%
+-commutative98.6%
fma-udef98.6%
associate-/l/98.3%
Applied egg-rr98.3%
Final simplification98.3%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(exp (* cosTheta (- cosTheta)))
(/ (sqrt (/ (- -1.0 (* cosTheta -2.0)) (- PI))) cosTheta)))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (expf((cosTheta * -cosTheta)) * (sqrtf(((-1.0f - (cosTheta * -2.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(Float32(Float32(-1.0) - Float32(cosTheta * Float32(-2.0))) / Float32(-Float32(pi)))) / cosTheta)))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / ((single(1.0) + c) + (exp((cosTheta * -cosTheta)) * (sqrt(((single(-1.0) - (cosTheta * single(-2.0))) / -single(pi))) / cosTheta))); end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + e^{cosTheta \cdot \left(-cosTheta\right)} \cdot \frac{\sqrt{\frac{-1 - cosTheta \cdot -2}{-\pi}}}{cosTheta}}
\end{array}
Initial program 98.0%
associate-*l/98.4%
*-un-lft-identity98.4%
div-inv98.3%
associate-/l*98.4%
associate--l-98.4%
count-298.4%
cancel-sign-sub-inv98.4%
metadata-eval98.4%
*-commutative98.4%
+-commutative98.4%
fma-udef98.4%
Applied egg-rr98.4%
associate-/r/98.1%
*-un-lft-identity98.1%
un-div-inv98.2%
Applied egg-rr98.3%
*-lft-identity98.3%
Simplified98.3%
frac-2neg98.3%
distribute-frac-neg98.3%
Applied egg-rr98.3%
distribute-neg-frac98.3%
neg-sub098.3%
fma-udef98.3%
*-commutative98.3%
+-commutative98.3%
associate--r+98.3%
metadata-eval98.3%
*-commutative98.3%
Simplified98.3%
Final simplification98.3%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(exp (* cosTheta (- cosTheta)))
(/ (+ (/ 1.0 cosTheta) -1.0) (sqrt PI))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (expf((cosTheta * -cosTheta)) * (((1.0f / cosTheta) + -1.0f) / sqrtf(((float) M_PI)))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(exp(Float32(cosTheta * Float32(-cosTheta))) * Float32(Float32(Float32(Float32(1.0) / cosTheta) + Float32(-1.0)) / sqrt(Float32(pi)))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / ((single(1.0) + c) + (exp((cosTheta * -cosTheta)) * (((single(1.0) / cosTheta) + single(-1.0)) / sqrt(single(pi))))); end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + e^{cosTheta \cdot \left(-cosTheta\right)} \cdot \frac{\frac{1}{cosTheta} + -1}{\sqrt{\pi}}}
\end{array}
Initial program 98.0%
associate-*l/98.4%
*-un-lft-identity98.4%
div-inv98.3%
associate-/l*98.4%
associate--l-98.4%
count-298.4%
cancel-sign-sub-inv98.4%
metadata-eval98.4%
*-commutative98.4%
+-commutative98.4%
fma-udef98.4%
Applied egg-rr98.4%
Taylor expanded in cosTheta around 0 95.2%
distribute-rgt-out95.1%
Simplified95.1%
*-commutative95.1%
sqrt-div95.1%
metadata-eval95.1%
un-div-inv95.5%
Applied egg-rr95.5%
Final simplification95.5%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(exp (* cosTheta (- cosTheta)))
(/ (/ (- 1.0 cosTheta) cosTheta) (sqrt PI))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (expf((cosTheta * -cosTheta)) * (((1.0f - cosTheta) / cosTheta) / sqrtf(((float) M_PI)))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(exp(Float32(cosTheta * Float32(-cosTheta))) * Float32(Float32(Float32(Float32(1.0) - cosTheta) / cosTheta) / sqrt(Float32(pi)))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / ((single(1.0) + c) + (exp((cosTheta * -cosTheta)) * (((single(1.0) - cosTheta) / cosTheta) / sqrt(single(pi))))); end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + e^{cosTheta \cdot \left(-cosTheta\right)} \cdot \frac{\frac{1 - cosTheta}{cosTheta}}{\sqrt{\pi}}}
\end{array}
Initial program 98.0%
frac-times98.7%
*-un-lft-identity98.7%
associate--l-98.6%
count-298.6%
cancel-sign-sub-inv98.6%
metadata-eval98.6%
*-commutative98.6%
+-commutative98.6%
fma-udef98.6%
associate-/l/98.3%
Applied egg-rr98.3%
Taylor expanded in cosTheta around 0 95.5%
mul-1-neg95.5%
Simplified95.5%
Final simplification95.5%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ 1.0 (+ c (- (/ 1.0 (* cosTheta (sqrt PI))) (sqrt (/ 1.0 PI)))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (c + ((1.0f / (cosTheta * sqrtf(((float) M_PI)))) - sqrtf((1.0f / ((float) M_PI))))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(c + Float32(Float32(Float32(1.0) / Float32(cosTheta * sqrt(Float32(pi)))) - sqrt(Float32(Float32(1.0) / Float32(pi))))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + (c + ((single(1.0) / (cosTheta * sqrt(single(pi)))) - sqrt((single(1.0) / single(pi)))))); end
\begin{array}{l}
\\
\frac{1}{1 + \left(c + \left(\frac{1}{cosTheta \cdot \sqrt{\pi}} - \sqrt{\frac{1}{\pi}}\right)\right)}
\end{array}
Initial program 98.0%
+-commutative98.0%
fma-def98.0%
associate-*l/98.4%
*-lft-identity98.4%
sub-neg98.4%
sub-neg98.4%
associate-+l+98.3%
count-298.3%
*-commutative98.3%
distribute-lft-neg-in98.3%
distribute-rgt-neg-in98.3%
metadata-eval98.3%
exp-prod98.3%
Simplified98.3%
Taylor expanded in cosTheta around 0 94.7%
sqrt-div94.7%
metadata-eval94.7%
frac-times95.3%
metadata-eval95.3%
Applied egg-rr95.3%
Final simplification95.3%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ 1.0 (* (+ (/ 1.0 cosTheta) -1.0) (sqrt (/ 1.0 PI))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (((1.0f / cosTheta) + -1.0f) * sqrtf((1.0f / ((float) M_PI)))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(Float32(Float32(1.0) / cosTheta) + Float32(-1.0)) * sqrt(Float32(Float32(1.0) / Float32(pi)))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + (((single(1.0) / cosTheta) + single(-1.0)) * sqrt((single(1.0) / single(pi))))); end
\begin{array}{l}
\\
\frac{1}{1 + \left(\frac{1}{cosTheta} + -1\right) \cdot \sqrt{\frac{1}{\pi}}}
\end{array}
Initial program 98.0%
+-commutative98.0%
fma-def98.0%
associate-*l/98.4%
*-lft-identity98.4%
sub-neg98.4%
sub-neg98.4%
associate-+l+98.3%
count-298.3%
*-commutative98.3%
distribute-lft-neg-in98.3%
distribute-rgt-neg-in98.3%
metadata-eval98.3%
exp-prod98.3%
Simplified98.3%
Taylor expanded in cosTheta around 0 94.7%
Taylor expanded in c around 0 94.8%
distribute-rgt-out94.7%
Simplified94.7%
Final simplification94.7%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ c (+ 1.0 (/ (+ (/ 1.0 cosTheta) -1.0) (sqrt PI))))))
float code(float cosTheta, float c) {
return 1.0f / (c + (1.0f + (((1.0f / cosTheta) + -1.0f) / sqrtf(((float) M_PI)))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(c + Float32(Float32(1.0) + Float32(Float32(Float32(Float32(1.0) / cosTheta) + Float32(-1.0)) / sqrt(Float32(pi)))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (c + (single(1.0) + (((single(1.0) / cosTheta) + single(-1.0)) / sqrt(single(pi))))); end
\begin{array}{l}
\\
\frac{1}{c + \left(1 + \frac{\frac{1}{cosTheta} + -1}{\sqrt{\pi}}\right)}
\end{array}
Initial program 98.0%
+-commutative98.0%
fma-def98.0%
associate-*l/98.4%
*-lft-identity98.4%
sub-neg98.4%
sub-neg98.4%
associate-+l+98.3%
count-298.3%
*-commutative98.3%
distribute-lft-neg-in98.3%
distribute-rgt-neg-in98.3%
metadata-eval98.3%
exp-prod98.3%
Simplified98.3%
Taylor expanded in cosTheta around 0 94.7%
div-inv94.7%
distribute-rgt-out94.7%
sqrt-div94.7%
metadata-eval94.7%
associate-*l/95.0%
*-un-lft-identity95.0%
+-commutative95.0%
Applied egg-rr95.0%
*-lft-identity95.0%
associate-+r+95.0%
+-commutative95.0%
associate-+l+95.0%
+-commutative95.0%
Simplified95.0%
Final simplification95.0%
(FPCore (cosTheta c) :precision binary32 1.0)
float code(float cosTheta, float c) {
return 1.0f;
}
real(4) function code(costheta, c)
real(4), intent (in) :: costheta
real(4), intent (in) :: c
code = 1.0e0
end function
function code(cosTheta, c) return Float32(1.0) end
function tmp = code(cosTheta, c) tmp = single(1.0); end
\begin{array}{l}
\\
1
\end{array}
Initial program 98.0%
+-commutative98.0%
associate-+r+98.0%
+-commutative98.0%
fma-def98.0%
Simplified98.6%
Taylor expanded in cosTheta around inf 10.7%
Taylor expanded in c around 0 10.7%
Final simplification10.7%
herbie shell --seed 2023336
(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))))))