
(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
(+
(/
(* (exp (* (- cosTheta) cosTheta)) (sqrt (- (- 1.0 cosTheta) cosTheta)))
(* (sqrt PI) cosTheta))
(+ c 1.0))))
float code(float cosTheta, float c) {
return 1.0f / (((expf((-cosTheta * cosTheta)) * sqrtf(((1.0f - cosTheta) - cosTheta))) / (sqrtf(((float) M_PI)) * cosTheta)) + (c + 1.0f));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(exp(Float32(Float32(-cosTheta) * cosTheta)) * sqrt(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta))) / Float32(sqrt(Float32(pi)) * cosTheta)) + Float32(c + Float32(1.0)))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (((exp((-cosTheta * cosTheta)) * sqrt(((single(1.0) - cosTheta) - cosTheta))) / (sqrt(single(pi)) * cosTheta)) + (c + single(1.0))); end
\begin{array}{l}
\\
\frac{1}{\frac{e^{\left(-cosTheta\right) \cdot cosTheta} \cdot \sqrt{\left(1 - cosTheta\right) - cosTheta}}{\sqrt{\pi} \cdot cosTheta} + \left(c + 1\right)}
\end{array}
Initial program 97.7%
lift-*.f32N/A
lift-*.f32N/A
lift-/.f32N/A
lift-/.f32N/A
frac-timesN/A
*-lft-identityN/A
associate-*l/N/A
lower-/.f32N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f3298.6
Applied rewrites98.6%
Final simplification98.6%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(/
(sqrt (/ (- (- 1.0 cosTheta) cosTheta) PI))
(* (exp (* cosTheta cosTheta)) cosTheta))
(+ c 1.0))))
float code(float cosTheta, float c) {
return 1.0f / ((sqrtf((((1.0f - cosTheta) - cosTheta) / ((float) M_PI))) / (expf((cosTheta * cosTheta)) * cosTheta)) + (c + 1.0f));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(sqrt(Float32(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta) / Float32(pi))) / Float32(exp(Float32(cosTheta * cosTheta)) * cosTheta)) + Float32(c + Float32(1.0)))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / ((sqrt((((single(1.0) - cosTheta) - cosTheta) / single(pi))) / (exp((cosTheta * cosTheta)) * cosTheta)) + (c + single(1.0))); end
\begin{array}{l}
\\
\frac{1}{\frac{\sqrt{\frac{\left(1 - cosTheta\right) - cosTheta}{\pi}}}{e^{cosTheta \cdot cosTheta} \cdot cosTheta} + \left(c + 1\right)}
\end{array}
Initial program 97.7%
lift-*.f32N/A
lift-*.f32N/A
lift-/.f32N/A
lift-/.f32N/A
frac-timesN/A
*-lft-identityN/A
associate-*l/N/A
lower-/.f32N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f3298.6
Applied rewrites98.6%
Applied rewrites98.1%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (fma (exp (* (- cosTheta) cosTheta)) (/ (sqrt (/ (fma -2.0 cosTheta 1.0) PI)) cosTheta) 1.0)))
float code(float cosTheta, float c) {
return 1.0f / fmaf(expf((-cosTheta * cosTheta)), (sqrtf((fmaf(-2.0f, cosTheta, 1.0f) / ((float) M_PI))) / cosTheta), 1.0f);
}
function code(cosTheta, c) return Float32(Float32(1.0) / fma(exp(Float32(Float32(-cosTheta) * cosTheta)), Float32(sqrt(Float32(fma(Float32(-2.0), cosTheta, Float32(1.0)) / Float32(pi))) / cosTheta), Float32(1.0))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(e^{\left(-cosTheta\right) \cdot cosTheta}, \frac{\sqrt{\frac{\mathsf{fma}\left(-2, cosTheta, 1\right)}{\pi}}}{cosTheta}, 1\right)}
\end{array}
Initial program 97.7%
lift-*.f32N/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
un-div-invN/A
lower-/.f32N/A
Applied rewrites98.1%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
*-commutativeN/A
times-fracN/A
rec-expN/A
mul-1-negN/A
lower-fma.f32N/A
Applied rewrites97.8%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(fma
(fma
(fma
(fma -0.16666666666666666 (* cosTheta cosTheta) 0.5)
(* cosTheta cosTheta)
-1.0)
(* cosTheta cosTheta)
1.0)
(/ (sqrt (/ (fma -2.0 cosTheta 1.0) PI)) cosTheta)
1.0)))
float code(float cosTheta, float c) {
return 1.0f / fmaf(fmaf(fmaf(fmaf(-0.16666666666666666f, (cosTheta * cosTheta), 0.5f), (cosTheta * cosTheta), -1.0f), (cosTheta * cosTheta), 1.0f), (sqrtf((fmaf(-2.0f, cosTheta, 1.0f) / ((float) M_PI))) / cosTheta), 1.0f);
}
function code(cosTheta, c) return Float32(Float32(1.0) / fma(fma(fma(fma(Float32(-0.16666666666666666), Float32(cosTheta * cosTheta), Float32(0.5)), Float32(cosTheta * cosTheta), Float32(-1.0)), Float32(cosTheta * cosTheta), Float32(1.0)), Float32(sqrt(Float32(fma(Float32(-2.0), cosTheta, Float32(1.0)) / Float32(pi))) / cosTheta), Float32(1.0))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.16666666666666666, cosTheta \cdot cosTheta, 0.5\right), cosTheta \cdot cosTheta, -1\right), cosTheta \cdot cosTheta, 1\right), \frac{\sqrt{\frac{\mathsf{fma}\left(-2, cosTheta, 1\right)}{\pi}}}{cosTheta}, 1\right)}
\end{array}
Initial program 97.7%
lift-*.f32N/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
un-div-invN/A
lower-/.f32N/A
Applied rewrites98.1%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
*-commutativeN/A
times-fracN/A
rec-expN/A
mul-1-negN/A
lower-fma.f32N/A
Applied rewrites97.8%
Taylor expanded in cosTheta around 0
Applied rewrites97.7%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (fma (fma (fma 0.5 (* cosTheta cosTheta) -1.0) (* cosTheta cosTheta) 1.0) (/ (sqrt (/ (fma -2.0 cosTheta 1.0) PI)) cosTheta) 1.0)))
float code(float cosTheta, float c) {
return 1.0f / fmaf(fmaf(fmaf(0.5f, (cosTheta * cosTheta), -1.0f), (cosTheta * cosTheta), 1.0f), (sqrtf((fmaf(-2.0f, cosTheta, 1.0f) / ((float) M_PI))) / cosTheta), 1.0f);
}
function code(cosTheta, c) return Float32(Float32(1.0) / fma(fma(fma(Float32(0.5), Float32(cosTheta * cosTheta), Float32(-1.0)), Float32(cosTheta * cosTheta), Float32(1.0)), Float32(sqrt(Float32(fma(Float32(-2.0), cosTheta, Float32(1.0)) / Float32(pi))) / cosTheta), Float32(1.0))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.5, cosTheta \cdot cosTheta, -1\right), cosTheta \cdot cosTheta, 1\right), \frac{\sqrt{\frac{\mathsf{fma}\left(-2, cosTheta, 1\right)}{\pi}}}{cosTheta}, 1\right)}
\end{array}
Initial program 97.7%
lift-*.f32N/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
un-div-invN/A
lower-/.f32N/A
Applied rewrites98.1%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
*-commutativeN/A
times-fracN/A
rec-expN/A
mul-1-negN/A
lower-fma.f32N/A
Applied rewrites97.8%
Taylor expanded in cosTheta around 0
Applied rewrites97.6%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (fma (- 1.0 (* cosTheta cosTheta)) (/ (sqrt (/ (fma -2.0 cosTheta 1.0) PI)) cosTheta) 1.0)))
float code(float cosTheta, float c) {
return 1.0f / fmaf((1.0f - (cosTheta * cosTheta)), (sqrtf((fmaf(-2.0f, cosTheta, 1.0f) / ((float) M_PI))) / cosTheta), 1.0f);
}
function code(cosTheta, c) return Float32(Float32(1.0) / fma(Float32(Float32(1.0) - Float32(cosTheta * cosTheta)), Float32(sqrt(Float32(fma(Float32(-2.0), cosTheta, Float32(1.0)) / Float32(pi))) / cosTheta), Float32(1.0))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(1 - cosTheta \cdot cosTheta, \frac{\sqrt{\frac{\mathsf{fma}\left(-2, cosTheta, 1\right)}{\pi}}}{cosTheta}, 1\right)}
\end{array}
Initial program 97.7%
lift-*.f32N/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
un-div-invN/A
lower-/.f32N/A
Applied rewrites98.1%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
*-commutativeN/A
times-fracN/A
rec-expN/A
mul-1-negN/A
lower-fma.f32N/A
Applied rewrites97.8%
Taylor expanded in cosTheta around 0
Applied rewrites97.1%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ (/ (/ (fma (fma -1.5 cosTheta -1.0) cosTheta 1.0) cosTheta) (sqrt PI)) (+ c 1.0))))
float code(float cosTheta, float c) {
return 1.0f / (((fmaf(fmaf(-1.5f, cosTheta, -1.0f), cosTheta, 1.0f) / cosTheta) / sqrtf(((float) M_PI))) + (c + 1.0f));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(fma(fma(Float32(-1.5), cosTheta, Float32(-1.0)), cosTheta, Float32(1.0)) / cosTheta) / sqrt(Float32(pi))) + Float32(c + Float32(1.0)))) end
\begin{array}{l}
\\
\frac{1}{\frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left(-1.5, cosTheta, -1\right), cosTheta, 1\right)}{cosTheta}}{\sqrt{\pi}} + \left(c + 1\right)}
\end{array}
Initial program 97.7%
lift-*.f32N/A
lift-*.f32N/A
*-commutativeN/A
lift-/.f32N/A
div-invN/A
associate-*l/N/A
clear-numN/A
lower-/.f32N/A
lower-/.f32N/A
*-commutativeN/A
lower-*.f3298.3
Applied rewrites98.3%
Taylor expanded in cosTheta around 0
lower-/.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
sub-negN/A
metadata-evalN/A
lower-fma.f3297.1
Applied rewrites97.1%
lift-+.f32N/A
lift-+.f32N/A
+-commutativeN/A
lower-+.f32N/A
Applied rewrites97.1%
(FPCore (cosTheta c) :precision binary32 (* (fma (- cosTheta) (fma (- c (sqrt (/ 1.0 PI))) PI PI) (sqrt PI)) cosTheta))
float code(float cosTheta, float c) {
return fmaf(-cosTheta, fmaf((c - sqrtf((1.0f / ((float) M_PI)))), ((float) M_PI), ((float) M_PI)), sqrtf(((float) M_PI))) * cosTheta;
}
function code(cosTheta, c) return Float32(fma(Float32(-cosTheta), fma(Float32(c - sqrt(Float32(Float32(1.0) / Float32(pi)))), Float32(pi), Float32(pi)), sqrt(Float32(pi))) * cosTheta) end
\begin{array}{l}
\\
\mathsf{fma}\left(-cosTheta, \mathsf{fma}\left(c - \sqrt{\frac{1}{\pi}}, \pi, \pi\right), \sqrt{\pi}\right) \cdot cosTheta
\end{array}
Initial program 97.7%
Taylor expanded in cosTheta around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites96.1%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ (* (/ 1.0 (* (sqrt PI) cosTheta)) (- 1.0 cosTheta)) (+ c 1.0))))
float code(float cosTheta, float c) {
return 1.0f / (((1.0f / (sqrtf(((float) M_PI)) * cosTheta)) * (1.0f - cosTheta)) + (c + 1.0f));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(Float32(1.0) / Float32(sqrt(Float32(pi)) * cosTheta)) * Float32(Float32(1.0) - cosTheta)) + Float32(c + Float32(1.0)))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (((single(1.0) / (sqrt(single(pi)) * cosTheta)) * (single(1.0) - cosTheta)) + (c + single(1.0))); end
\begin{array}{l}
\\
\frac{1}{\frac{1}{\sqrt{\pi} \cdot cosTheta} \cdot \left(1 - cosTheta\right) + \left(c + 1\right)}
\end{array}
Initial program 97.7%
lift-*.f32N/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
un-div-invN/A
lower-/.f32N/A
Applied rewrites98.1%
Taylor expanded in cosTheta around 0
associate-*r*N/A
lower-/.f32N/A
distribute-rgt1-inN/A
+-commutativeN/A
lower-*.f32N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-PI.f3295.4
Applied rewrites95.4%
Applied rewrites95.9%
Final simplification95.9%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ (+ (/ (- 1.0 cosTheta) (* (sqrt PI) cosTheta)) c) 1.0)))
float code(float cosTheta, float c) {
return 1.0f / ((((1.0f - cosTheta) / (sqrtf(((float) M_PI)) * cosTheta)) + c) + 1.0f);
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(Float32(Float32(1.0) - cosTheta) / Float32(sqrt(Float32(pi)) * cosTheta)) + c) + Float32(1.0))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / ((((single(1.0) - cosTheta) / (sqrt(single(pi)) * cosTheta)) + c) + single(1.0)); end
\begin{array}{l}
\\
\frac{1}{\left(\frac{1 - cosTheta}{\sqrt{\pi} \cdot cosTheta} + c\right) + 1}
\end{array}
Initial program 97.7%
lift-*.f32N/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
un-div-invN/A
lower-/.f32N/A
Applied rewrites98.1%
Taylor expanded in cosTheta around 0
associate-*r*N/A
lower-/.f32N/A
distribute-rgt1-inN/A
+-commutativeN/A
lower-*.f32N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-PI.f3295.4
Applied rewrites95.4%
lift-+.f32N/A
lift-+.f32N/A
associate-+l+N/A
+-commutativeN/A
lower-+.f32N/A
Applied rewrites95.9%
(FPCore (cosTheta c) :precision binary32 (* (sqrt PI) cosTheta))
float code(float cosTheta, float c) {
return sqrtf(((float) M_PI)) * cosTheta;
}
function code(cosTheta, c) return Float32(sqrt(Float32(pi)) * cosTheta) end
function tmp = code(cosTheta, c) tmp = sqrt(single(pi)) * cosTheta; end
\begin{array}{l}
\\
\sqrt{\pi} \cdot cosTheta
\end{array}
Initial program 97.7%
Taylor expanded in cosTheta around 0
*-commutativeN/A
lower-*.f32N/A
lower-sqrt.f32N/A
lower-PI.f3292.5
Applied rewrites92.5%
(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.7%
Taylor expanded in c around inf
lower-/.f325.0
Applied rewrites5.0%
herbie shell --seed 2024235
(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))))))