
(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 13 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)
(*
(/ 1.0 (* cosTheta (sqrt (/ PI (fma cosTheta -2.0 1.0)))))
(exp (* cosTheta (- cosTheta)))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + ((1.0f / (cosTheta * sqrtf((((float) M_PI) / fmaf(cosTheta, -2.0f, 1.0f))))) * expf((cosTheta * -cosTheta))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(Float32(Float32(1.0) / Float32(cosTheta * sqrt(Float32(Float32(pi) / fma(cosTheta, Float32(-2.0), Float32(1.0)))))) * exp(Float32(cosTheta * Float32(-cosTheta)))))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \frac{1}{cosTheta \cdot \sqrt{\frac{\pi}{\mathsf{fma}\left(cosTheta, -2, 1\right)}}} \cdot e^{cosTheta \cdot \left(-cosTheta\right)}}
\end{array}
Initial program 97.8%
associate-*l/98.3%
*-un-lft-identity98.3%
associate--l-98.3%
Applied egg-rr98.3%
clear-num98.3%
inv-pow98.3%
count-298.3%
Applied egg-rr98.3%
unpow-198.3%
associate-/r/98.2%
sub-neg98.2%
*-commutative98.2%
distribute-rgt-neg-in98.2%
metadata-eval98.2%
Simplified98.2%
add098.2%
sqrt-undiv98.3%
+-commutative98.3%
fma-define98.3%
Applied egg-rr98.3%
add098.3%
Simplified98.3%
Final simplification98.3%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
1.0
(+
c
(*
(sqrt (/ (+ 1.0 (* cosTheta -2.0)) PI))
(/ (exp (- (pow cosTheta 2.0))) cosTheta))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (c + (sqrtf(((1.0f + (cosTheta * -2.0f)) / ((float) M_PI))) * (expf(-powf(cosTheta, 2.0f)) / cosTheta))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(c + Float32(sqrt(Float32(Float32(Float32(1.0) + Float32(cosTheta * Float32(-2.0))) / Float32(pi))) * Float32(exp(Float32(-(cosTheta ^ Float32(2.0)))) / cosTheta))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + (c + (sqrt(((single(1.0) + (cosTheta * single(-2.0))) / single(pi))) * (exp(-(cosTheta ^ single(2.0))) / cosTheta)))); end
\begin{array}{l}
\\
\frac{1}{1 + \left(c + \sqrt{\frac{1 + cosTheta \cdot -2}{\pi}} \cdot \frac{e^{-{cosTheta}^{2}}}{cosTheta}\right)}
\end{array}
Initial program 97.8%
associate-*l/98.3%
*-un-lft-identity98.3%
associate--l-98.3%
Applied egg-rr98.3%
Taylor expanded in c around 0 97.8%
+-commutative97.8%
*-commutative97.8%
sub-neg97.8%
*-commutative97.8%
distribute-rgt-neg-in97.8%
metadata-eval97.8%
neg-mul-197.8%
Simplified97.8%
Final simplification97.8%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(exp (* cosTheta (- cosTheta)))
(/ (pow (/ PI (fma cosTheta -2.0 1.0)) -0.5) cosTheta)))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (expf((cosTheta * -cosTheta)) * (powf((((float) M_PI) / fmaf(cosTheta, -2.0f, 1.0f)), -0.5f) / cosTheta)));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(exp(Float32(cosTheta * Float32(-cosTheta))) * Float32((Float32(Float32(pi) / fma(cosTheta, Float32(-2.0), Float32(1.0))) ^ Float32(-0.5)) / cosTheta)))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + e^{cosTheta \cdot \left(-cosTheta\right)} \cdot \frac{{\left(\frac{\pi}{\mathsf{fma}\left(cosTheta, -2, 1\right)}\right)}^{-0.5}}{cosTheta}}
\end{array}
Initial program 97.8%
associate-*l/98.3%
*-un-lft-identity98.3%
associate--l-98.3%
Applied egg-rr98.3%
clear-num98.3%
inv-pow98.3%
count-298.3%
Applied egg-rr98.3%
unpow-198.3%
associate-/r/98.2%
sub-neg98.2%
*-commutative98.2%
distribute-rgt-neg-in98.2%
metadata-eval98.2%
Simplified98.2%
add098.2%
sqrt-undiv98.3%
+-commutative98.3%
fma-define98.3%
Applied egg-rr98.3%
add098.3%
Simplified98.3%
add098.3%
associate-/r*97.9%
pow1/297.9%
pow-flip97.9%
metadata-eval97.9%
Applied egg-rr97.9%
add097.9%
Simplified97.9%
Final simplification97.9%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
1.0
(*
(sqrt (/ (+ 1.0 (* cosTheta -2.0)) PI))
(/ (exp (- (pow cosTheta 2.0))) cosTheta)))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (sqrtf(((1.0f + (cosTheta * -2.0f)) / ((float) M_PI))) * (expf(-powf(cosTheta, 2.0f)) / cosTheta)));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(sqrt(Float32(Float32(Float32(1.0) + Float32(cosTheta * Float32(-2.0))) / Float32(pi))) * Float32(exp(Float32(-(cosTheta ^ Float32(2.0)))) / cosTheta)))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + (sqrt(((single(1.0) + (cosTheta * single(-2.0))) / single(pi))) * (exp(-(cosTheta ^ single(2.0))) / cosTheta))); end
\begin{array}{l}
\\
\frac{1}{1 + \sqrt{\frac{1 + cosTheta \cdot -2}{\pi}} \cdot \frac{e^{-{cosTheta}^{2}}}{cosTheta}}
\end{array}
Initial program 97.8%
+-commutative97.8%
associate-+l+97.8%
+-commutative97.8%
associate-*l*97.8%
*-commutative97.8%
associate-*l*97.8%
fma-undefine97.8%
Simplified97.8%
Taylor expanded in c around 0 97.1%
Final simplification97.1%
(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 97.8%
associate-*l/98.3%
*-un-lft-identity98.3%
associate--l-98.3%
Applied egg-rr98.3%
Taylor expanded in cosTheta around 0 95.3%
mul-1-neg95.3%
unsub-neg95.3%
Simplified95.3%
Final simplification95.3%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ c (fma (sqrt (/ 1.0 PI)) (+ -1.0 (/ 1.0 cosTheta)) 1.0))))
float code(float cosTheta, float c) {
return 1.0f / (c + fmaf(sqrtf((1.0f / ((float) M_PI))), (-1.0f + (1.0f / cosTheta)), 1.0f));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(c + fma(sqrt(Float32(Float32(1.0) / Float32(pi))), Float32(Float32(-1.0) + Float32(Float32(1.0) / cosTheta)), Float32(1.0)))) end
\begin{array}{l}
\\
\frac{1}{c + \mathsf{fma}\left(\sqrt{\frac{1}{\pi}}, -1 + \frac{1}{cosTheta}, 1\right)}
\end{array}
Initial program 97.8%
+-commutative97.8%
associate-+l+97.8%
+-commutative97.8%
associate-*l*97.8%
*-commutative97.8%
associate-*l*97.8%
fma-undefine97.8%
Simplified97.8%
Taylor expanded in cosTheta around 0 94.5%
+-commutative94.5%
distribute-rgt-out94.5%
fma-define94.6%
Simplified94.6%
Final simplification94.6%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ 1.0 (- (/ 1.0 (* cosTheta (sqrt PI))) (pow PI -0.5)))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + ((1.0f / (cosTheta * sqrtf(((float) M_PI)))) - powf(((float) M_PI), -0.5f)));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(Float32(1.0) / Float32(cosTheta * sqrt(Float32(pi)))) - (Float32(pi) ^ Float32(-0.5))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + ((single(1.0) / (cosTheta * sqrt(single(pi)))) - (single(pi) ^ single(-0.5)))); end
\begin{array}{l}
\\
\frac{1}{1 + \left(\frac{1}{cosTheta \cdot \sqrt{\pi}} - {\pi}^{-0.5}\right)}
\end{array}
Initial program 97.8%
+-commutative97.8%
associate-+l+97.8%
+-commutative97.8%
associate-*l*97.8%
*-commutative97.8%
associate-*l*97.8%
fma-undefine97.8%
Simplified97.8%
Taylor expanded in c around 0 97.1%
+-commutative97.1%
*-commutative97.1%
neg-mul-197.1%
fma-define97.1%
+-commutative97.1%
*-commutative97.1%
fma-undefine97.1%
neg-mul-197.1%
Simplified97.1%
Taylor expanded in cosTheta around 0 94.4%
add094.4%
pow1/294.4%
inv-pow94.4%
pow-pow94.4%
metadata-eval94.4%
Applied egg-rr94.4%
add094.4%
Simplified94.4%
inv-pow94.4%
sqrt-div94.4%
metadata-eval94.4%
inv-pow94.4%
unpow-prod-down94.9%
inv-pow94.9%
*-commutative94.9%
Applied egg-rr94.9%
Final simplification94.9%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ 1.0 (- (/ (/ 1.0 cosTheta) (sqrt PI)) (pow PI -0.5)))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (((1.0f / cosTheta) / sqrtf(((float) M_PI))) - powf(((float) M_PI), -0.5f)));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(Float32(Float32(1.0) / cosTheta) / sqrt(Float32(pi))) - (Float32(pi) ^ Float32(-0.5))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + (((single(1.0) / cosTheta) / sqrt(single(pi))) - (single(pi) ^ single(-0.5)))); end
\begin{array}{l}
\\
\frac{1}{1 + \left(\frac{\frac{1}{cosTheta}}{\sqrt{\pi}} - {\pi}^{-0.5}\right)}
\end{array}
Initial program 97.8%
+-commutative97.8%
associate-+l+97.8%
+-commutative97.8%
associate-*l*97.8%
*-commutative97.8%
associate-*l*97.8%
fma-undefine97.8%
Simplified97.8%
Taylor expanded in c around 0 97.1%
+-commutative97.1%
*-commutative97.1%
neg-mul-197.1%
fma-define97.1%
+-commutative97.1%
*-commutative97.1%
fma-undefine97.1%
neg-mul-197.1%
Simplified97.1%
Taylor expanded in cosTheta around 0 94.4%
add094.4%
pow1/294.4%
inv-pow94.4%
pow-pow94.4%
metadata-eval94.4%
Applied egg-rr94.4%
add094.4%
Simplified94.4%
inv-pow94.4%
sqrt-div94.4%
metadata-eval94.4%
inv-pow94.4%
unpow-prod-down94.9%
inv-pow94.9%
associate-/r*94.9%
Applied egg-rr94.9%
Final simplification94.9%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ (+ 1.0 c) (* (sqrt (/ 1.0 PI)) (+ -1.0 (/ 1.0 cosTheta))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (sqrtf((1.0f / ((float) M_PI))) * (-1.0f + (1.0f / cosTheta))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + 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) + c) + (sqrt((single(1.0) / single(pi))) * (single(-1.0) + (single(1.0) / cosTheta)))); end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \sqrt{\frac{1}{\pi}} \cdot \left(-1 + \frac{1}{cosTheta}\right)}
\end{array}
Initial program 97.8%
associate-*l/98.3%
*-un-lft-identity98.3%
associate--l-98.3%
Applied egg-rr98.3%
Taylor expanded in cosTheta around 0 94.5%
associate-+r+94.5%
distribute-rgt-out94.5%
Simplified94.5%
Final simplification94.5%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ 1.0 (* (pow PI -0.5) (+ -1.0 (/ 1.0 cosTheta))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (powf(((float) M_PI), -0.5f) * (-1.0f + (1.0f / cosTheta))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32((Float32(pi) ^ Float32(-0.5)) * Float32(Float32(-1.0) + Float32(Float32(1.0) / cosTheta))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + ((single(pi) ^ single(-0.5)) * (single(-1.0) + (single(1.0) / cosTheta)))); end
\begin{array}{l}
\\
\frac{1}{1 + {\pi}^{-0.5} \cdot \left(-1 + \frac{1}{cosTheta}\right)}
\end{array}
Initial program 97.8%
+-commutative97.8%
associate-+l+97.8%
+-commutative97.8%
associate-*l*97.8%
*-commutative97.8%
associate-*l*97.8%
fma-undefine97.8%
Simplified97.8%
Taylor expanded in c around 0 97.1%
+-commutative97.1%
*-commutative97.1%
neg-mul-197.1%
fma-define97.1%
+-commutative97.1%
*-commutative97.1%
fma-undefine97.1%
neg-mul-197.1%
Simplified97.1%
Taylor expanded in cosTheta around 0 94.4%
expm1-log1p-u87.5%
log1p-define87.5%
expm1-undefine87.4%
add-exp-log94.4%
+-commutative94.4%
distribute-rgt-out94.4%
fma-define94.4%
pow1/294.4%
inv-pow94.4%
pow-pow94.4%
metadata-eval94.4%
Applied egg-rr94.4%
fma-undefine94.4%
associate--l+94.4%
metadata-eval94.4%
add094.4%
Simplified94.4%
Final simplification94.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.8%
+-commutative97.8%
associate-+l+97.8%
+-commutative97.8%
associate-*l*97.8%
*-commutative97.8%
associate-*l*97.8%
fma-undefine97.8%
Simplified97.8%
Taylor expanded in cosTheta around 0 92.8%
Final simplification92.8%
(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.8%
add-cube-cbrt96.9%
pow397.0%
Applied egg-rr96.4%
Taylor expanded in cosTheta around 0 90.5%
Taylor expanded in cosTheta around inf 11.2%
pow-base-111.2%
associate-*r/11.2%
metadata-eval11.2%
Simplified11.2%
Taylor expanded in c around 0 11.2%
mul-1-neg11.2%
unsub-neg11.2%
Simplified11.2%
Final simplification11.2%
(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 97.8%
add-cube-cbrt96.9%
pow397.0%
Applied egg-rr96.4%
Taylor expanded in cosTheta around 0 90.5%
Taylor expanded in cosTheta around inf 11.2%
pow-base-111.2%
associate-*r/11.2%
metadata-eval11.2%
Simplified11.2%
Taylor expanded in c around 0 11.2%
Final simplification11.2%
herbie shell --seed 2024044
(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))))))