
(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 (fma (/ (/ (sqrt (+ 1.0 (* cosTheta -2.0))) cosTheta) (sqrt PI)) (pow (exp cosTheta) (- cosTheta)) (+ 1.0 c))))
float code(float cosTheta, float c) {
return 1.0f / fmaf(((sqrtf((1.0f + (cosTheta * -2.0f))) / cosTheta) / sqrtf(((float) M_PI))), powf(expf(cosTheta), -cosTheta), (1.0f + c));
}
function code(cosTheta, c) return Float32(Float32(1.0) / fma(Float32(Float32(sqrt(Float32(Float32(1.0) + Float32(cosTheta * Float32(-2.0)))) / cosTheta) / sqrt(Float32(pi))), (exp(cosTheta) ^ Float32(-cosTheta)), Float32(Float32(1.0) + c))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(\frac{\frac{\sqrt{1 + cosTheta \cdot -2}}{cosTheta}}{\sqrt{\pi}}, {\left(e^{cosTheta}\right)}^{\left(-cosTheta\right)}, 1 + c\right)}
\end{array}
Initial program 97.9%
+-commutative97.9%
fma-define97.9%
associate-*l/98.5%
*-lft-identity98.5%
associate--l-98.5%
sub-neg98.5%
neg-mul-198.5%
distribute-lft-out98.5%
distribute-rgt-out98.5%
metadata-eval98.5%
distribute-lft-neg-out98.5%
distribute-rgt-neg-out98.5%
exp-prod98.5%
Simplified98.5%
Final simplification98.5%
(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.9%
associate-+l+97.9%
+-commutative97.9%
fma-define97.9%
times-frac98.5%
*-lft-identity98.5%
associate--l-98.5%
sub-neg98.5%
neg-mul-198.5%
distribute-lft-out98.5%
distribute-rgt-out98.5%
metadata-eval98.5%
exp-prod98.5%
Simplified98.5%
Final simplification98.5%
(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.9%
associate-+l+97.9%
+-commutative97.9%
fma-define97.9%
times-frac98.5%
*-lft-identity98.5%
associate--l-98.5%
sub-neg98.5%
neg-mul-198.5%
distribute-lft-out98.5%
distribute-rgt-out98.5%
metadata-eval98.5%
exp-prod98.5%
Simplified98.5%
*-un-lft-identity98.5%
associate-/r*97.9%
sqrt-undiv98.0%
+-commutative98.0%
fma-define98.0%
Applied egg-rr98.0%
*-lft-identity98.0%
Simplified98.0%
Final simplification98.0%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
1.0
(+
c
(*
(/ (exp (- (pow cosTheta 2.0))) cosTheta)
(sqrt (/ (+ 1.0 (* cosTheta -2.0)) PI)))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (c + ((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(c + 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) + (c + ((exp(-(cosTheta ^ single(2.0))) / cosTheta) * sqrt(((single(1.0) + (cosTheta * single(-2.0))) / single(pi)))))); end
\begin{array}{l}
\\
\frac{1}{1 + \left(c + \frac{e^{-{cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 + cosTheta \cdot -2}{\pi}}\right)}
\end{array}
Initial program 97.9%
+-commutative97.9%
fma-define97.9%
associate-*l/98.5%
*-lft-identity98.5%
associate--l-98.5%
sub-neg98.5%
neg-mul-198.5%
distribute-lft-out98.5%
distribute-rgt-out98.5%
metadata-eval98.5%
distribute-lft-neg-out98.5%
distribute-rgt-neg-out98.5%
exp-prod98.5%
Simplified98.5%
Taylor expanded in c around 0 98.0%
Final simplification98.0%
(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.9%
add-cube-cbrt97.9%
cbrt-unprod97.9%
frac-times97.9%
metadata-eval97.9%
add-sqr-sqrt97.9%
inv-pow97.9%
sqrt-pow297.9%
metadata-eval97.9%
Applied egg-rr97.9%
Taylor expanded in c around 0 97.2%
neg-mul-197.2%
*-commutative97.2%
*-commutative97.2%
cancel-sign-sub-inv97.2%
metadata-eval97.2%
*-commutative97.2%
Simplified97.2%
Final simplification97.2%
(FPCore (cosTheta c)
:precision binary32
(let* ((t_0 (sqrt (/ 1.0 PI))))
(/
1.0
(+
1.0
(+ c (+ (* t_0 (* cosTheta -1.5)) (* t_0 (+ -1.0 (/ 1.0 cosTheta)))))))))
float code(float cosTheta, float c) {
float t_0 = sqrtf((1.0f / ((float) M_PI)));
return 1.0f / (1.0f + (c + ((t_0 * (cosTheta * -1.5f)) + (t_0 * (-1.0f + (1.0f / cosTheta))))));
}
function code(cosTheta, c) t_0 = sqrt(Float32(Float32(1.0) / Float32(pi))) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(c + Float32(Float32(t_0 * Float32(cosTheta * Float32(-1.5))) + Float32(t_0 * Float32(Float32(-1.0) + Float32(Float32(1.0) / cosTheta))))))) end
function tmp = code(cosTheta, c) t_0 = sqrt((single(1.0) / single(pi))); tmp = single(1.0) / (single(1.0) + (c + ((t_0 * (cosTheta * single(-1.5))) + (t_0 * (single(-1.0) + (single(1.0) / cosTheta)))))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{1}{\pi}}\\
\frac{1}{1 + \left(c + \left(t\_0 \cdot \left(cosTheta \cdot -1.5\right) + t\_0 \cdot \left(-1 + \frac{1}{cosTheta}\right)\right)\right)}
\end{array}
\end{array}
Initial program 97.9%
+-commutative97.9%
fma-define97.9%
associate-*l/98.5%
*-lft-identity98.5%
associate--l-98.5%
sub-neg98.5%
neg-mul-198.5%
distribute-lft-out98.5%
distribute-rgt-out98.5%
metadata-eval98.5%
distribute-lft-neg-out98.5%
distribute-rgt-neg-out98.5%
exp-prod98.5%
Simplified98.5%
Taylor expanded in c around 0 98.0%
Taylor expanded in cosTheta around 0 96.3%
+-commutative96.3%
associate-+l+96.3%
*-commutative96.3%
distribute-rgt-out96.3%
metadata-eval96.3%
associate-*l*96.3%
+-commutative96.3%
distribute-rgt-out96.3%
Simplified96.3%
Final simplification96.3%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ 1.0 (* (sqrt (/ 1.0 PI)) (+ -1.0 (/ 1.0 cosTheta))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (sqrtf((1.0f / ((float) M_PI))) * (-1.0f + (1.0f / cosTheta))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(sqrt(Float32(Float32(1.0) / Float32(pi))) * Float32(Float32(-1.0) + Float32(Float32(1.0) / cosTheta))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + (sqrt((single(1.0) / single(pi))) * (single(-1.0) + (single(1.0) / cosTheta)))); end
\begin{array}{l}
\\
\frac{1}{1 + \sqrt{\frac{1}{\pi}} \cdot \left(-1 + \frac{1}{cosTheta}\right)}
\end{array}
Initial program 97.9%
+-commutative97.9%
fma-define97.9%
associate-*l/98.5%
*-lft-identity98.5%
associate--l-98.5%
sub-neg98.5%
neg-mul-198.5%
distribute-lft-out98.5%
distribute-rgt-out98.5%
metadata-eval98.5%
distribute-lft-neg-out98.5%
distribute-rgt-neg-out98.5%
exp-prod98.5%
Simplified98.5%
Taylor expanded in cosTheta around 0 94.9%
distribute-rgt-out94.9%
Simplified94.9%
Taylor expanded in c around 0 94.8%
Final simplification94.8%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ 1.0 (* (sqrt (/ 1.0 PI)) (/ (- 1.0 cosTheta) cosTheta)))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (sqrtf((1.0f / ((float) M_PI))) * ((1.0f - cosTheta) / cosTheta)));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(sqrt(Float32(Float32(1.0) / Float32(pi))) * Float32(Float32(Float32(1.0) - cosTheta) / cosTheta)))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + (sqrt((single(1.0) / single(pi))) * ((single(1.0) - cosTheta) / cosTheta))); end
\begin{array}{l}
\\
\frac{1}{1 + \sqrt{\frac{1}{\pi}} \cdot \frac{1 - cosTheta}{cosTheta}}
\end{array}
Initial program 97.9%
+-commutative97.9%
fma-define97.9%
associate-*l/98.5%
*-lft-identity98.5%
associate--l-98.5%
sub-neg98.5%
neg-mul-198.5%
distribute-lft-out98.5%
distribute-rgt-out98.5%
metadata-eval98.5%
distribute-lft-neg-out98.5%
distribute-rgt-neg-out98.5%
exp-prod98.5%
Simplified98.5%
Taylor expanded in cosTheta around 0 94.9%
distribute-rgt-out94.9%
Simplified94.9%
+-commutative94.9%
add-sqr-sqrt94.4%
fma-define94.4%
metadata-eval94.4%
metadata-eval94.4%
metadata-eval94.4%
pow-div94.4%
pow194.4%
pow194.4%
fma-neg94.4%
add-sqr-sqrt94.9%
div-sub94.9%
Applied egg-rr94.9%
Taylor expanded in c around 0 94.8%
Final simplification94.8%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ 1.0 (+ c (/ (+ -1.0 (/ 1.0 cosTheta)) (sqrt PI))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (c + ((-1.0f + (1.0f / cosTheta)) / sqrtf(((float) M_PI)))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(c + Float32(Float32(Float32(-1.0) + Float32(Float32(1.0) / cosTheta)) / sqrt(Float32(pi)))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + (c + ((single(-1.0) + (single(1.0) / cosTheta)) / sqrt(single(pi))))); end
\begin{array}{l}
\\
\frac{1}{1 + \left(c + \frac{-1 + \frac{1}{cosTheta}}{\sqrt{\pi}}\right)}
\end{array}
Initial program 97.9%
+-commutative97.9%
fma-define97.9%
associate-*l/98.5%
*-lft-identity98.5%
associate--l-98.5%
sub-neg98.5%
neg-mul-198.5%
distribute-lft-out98.5%
distribute-rgt-out98.5%
metadata-eval98.5%
distribute-lft-neg-out98.5%
distribute-rgt-neg-out98.5%
exp-prod98.5%
Simplified98.5%
Taylor expanded in cosTheta around 0 94.9%
distribute-rgt-out94.9%
Simplified94.9%
*-commutative94.9%
+-commutative94.9%
add-sqr-sqrt94.4%
fma-define94.4%
metadata-eval94.4%
metadata-eval94.4%
metadata-eval94.4%
pow-div94.4%
pow194.4%
pow194.4%
fma-neg94.4%
add-sqr-sqrt94.9%
div-sub94.9%
sqrt-div94.9%
metadata-eval94.9%
div-inv95.4%
clear-num95.3%
div-sub95.3%
add-sqr-sqrt94.6%
fma-neg94.6%
Applied egg-rr95.3%
associate-/r/94.9%
associate-*l/95.4%
*-lft-identity95.4%
+-commutative95.4%
Simplified95.4%
Final simplification95.4%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ 1.0 (+ c (/ (- 1.0 cosTheta) (* cosTheta (sqrt PI)))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (c + ((1.0f - cosTheta) / (cosTheta * sqrtf(((float) M_PI))))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(c + Float32(Float32(Float32(1.0) - cosTheta) / Float32(cosTheta * sqrt(Float32(pi))))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + (c + ((single(1.0) - cosTheta) / (cosTheta * sqrt(single(pi)))))); end
\begin{array}{l}
\\
\frac{1}{1 + \left(c + \frac{1 - cosTheta}{cosTheta \cdot \sqrt{\pi}}\right)}
\end{array}
Initial program 97.9%
+-commutative97.9%
fma-define97.9%
associate-*l/98.5%
*-lft-identity98.5%
associate--l-98.5%
sub-neg98.5%
neg-mul-198.5%
distribute-lft-out98.5%
distribute-rgt-out98.5%
metadata-eval98.5%
distribute-lft-neg-out98.5%
distribute-rgt-neg-out98.5%
exp-prod98.5%
Simplified98.5%
Taylor expanded in cosTheta around 0 94.9%
distribute-rgt-out94.9%
Simplified94.9%
*-commutative94.9%
+-commutative94.9%
add-sqr-sqrt94.4%
fma-define94.4%
metadata-eval94.4%
metadata-eval94.4%
metadata-eval94.4%
pow-div94.4%
pow194.4%
pow194.4%
fma-neg94.4%
add-sqr-sqrt94.9%
div-sub94.9%
sqrt-div94.9%
metadata-eval94.9%
div-inv95.4%
associate-/l/95.4%
Applied egg-rr95.4%
Final simplification95.4%
(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.9%
+-commutative97.9%
fma-define97.9%
associate-*l/98.5%
*-lft-identity98.5%
associate--l-98.5%
sub-neg98.5%
neg-mul-198.5%
distribute-lft-out98.5%
distribute-rgt-out98.5%
metadata-eval98.5%
distribute-lft-neg-out98.5%
distribute-rgt-neg-out98.5%
exp-prod98.5%
Simplified98.5%
Taylor expanded in cosTheta around 0 94.9%
distribute-rgt-out94.9%
Simplified94.9%
Taylor expanded in cosTheta around 0 92.7%
Final simplification92.7%
(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.9%
associate-+l+97.9%
+-commutative97.9%
fma-define97.9%
times-frac98.5%
*-lft-identity98.5%
associate--l-98.5%
sub-neg98.5%
neg-mul-198.5%
distribute-lft-out98.5%
distribute-rgt-out98.5%
metadata-eval98.5%
exp-prod98.5%
Simplified98.5%
Taylor expanded in c around inf 4.9%
Final simplification4.9%
herbie shell --seed 2024046
(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))))))