
(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
(+
(/
(exp (* cosTheta (- cosTheta)))
(* cosTheta (sqrt (/ PI (fma cosTheta -2.0 1.0)))))
(+ 1.0 c))))
float code(float cosTheta, float c) {
return 1.0f / ((expf((cosTheta * -cosTheta)) / (cosTheta * sqrtf((((float) M_PI) / fmaf(cosTheta, -2.0f, 1.0f))))) + (1.0f + c));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(exp(Float32(cosTheta * Float32(-cosTheta))) / Float32(cosTheta * sqrt(Float32(Float32(pi) / fma(cosTheta, Float32(-2.0), Float32(1.0)))))) + Float32(Float32(1.0) + c))) end
\begin{array}{l}
\\
\frac{1}{\frac{e^{cosTheta \cdot \left(-cosTheta\right)}}{cosTheta \cdot \sqrt{\frac{\pi}{\mathsf{fma}\left(cosTheta, -2, 1\right)}}} + \left(1 + c\right)}
\end{array}
Initial program 98.1%
Taylor expanded in c around 0
associate-+r+N/A
+-commutativeN/A
lower-fma.f32N/A
Applied rewrites98.2%
lift-fma.f32N/A
lift-PI.f32N/A
clear-numN/A
sqrt-divN/A
metadata-evalN/A
lower-/.f32N/A
lower-sqrt.f32N/A
lower-/.f3298.2
Applied rewrites98.2%
lift-neg.f32N/A
lift-*.f32N/A
lift-exp.f32N/A
lift-/.f32N/A
lift-PI.f32N/A
lift-fma.f32N/A
lift-/.f32N/A
lift-sqrt.f32N/A
lift-/.f32N/A
lift-+.f32N/A
lower-+.f32N/A
Applied rewrites98.6%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (fma (exp (* cosTheta (- cosTheta))) (/ (sqrt (/ (fma cosTheta -2.0 1.0) PI)) cosTheta) (+ 1.0 c))))
float code(float cosTheta, float c) {
return 1.0f / fmaf(expf((cosTheta * -cosTheta)), (sqrtf((fmaf(cosTheta, -2.0f, 1.0f) / ((float) M_PI))) / cosTheta), (1.0f + c));
}
function code(cosTheta, c) return Float32(Float32(1.0) / fma(exp(Float32(cosTheta * Float32(-cosTheta))), Float32(sqrt(Float32(fma(cosTheta, Float32(-2.0), Float32(1.0)) / Float32(pi))) / cosTheta), Float32(Float32(1.0) + c))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(e^{cosTheta \cdot \left(-cosTheta\right)}, \frac{\sqrt{\frac{\mathsf{fma}\left(cosTheta, -2, 1\right)}{\pi}}}{cosTheta}, 1 + c\right)}
\end{array}
Initial program 98.1%
Taylor expanded in c around 0
associate-+r+N/A
+-commutativeN/A
lower-fma.f32N/A
Applied rewrites98.2%
lift-neg.f32N/A
lift-*.f32N/A
lift-exp.f32N/A
lift-/.f32N/A
lift-fma.f32N/A
lift-PI.f32N/A
lift-/.f32N/A
lift-sqrt.f32N/A
lift-+.f32N/A
Applied rewrites98.2%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(/
(fma (* cosTheta cosTheta) (fma (* cosTheta cosTheta) 0.5 -1.0) 1.0)
(* cosTheta (sqrt (/ PI (fma cosTheta -2.0 1.0))))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (fmaf((cosTheta * cosTheta), fmaf((cosTheta * cosTheta), 0.5f, -1.0f), 1.0f) / (cosTheta * sqrtf((((float) M_PI) / fmaf(cosTheta, -2.0f, 1.0f))))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(fma(Float32(cosTheta * cosTheta), fma(Float32(cosTheta * cosTheta), Float32(0.5), Float32(-1.0)), Float32(1.0)) / Float32(cosTheta * sqrt(Float32(Float32(pi) / fma(cosTheta, Float32(-2.0), Float32(1.0)))))))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \frac{\mathsf{fma}\left(cosTheta \cdot cosTheta, \mathsf{fma}\left(cosTheta \cdot cosTheta, 0.5, -1\right), 1\right)}{cosTheta \cdot \sqrt{\frac{\pi}{\mathsf{fma}\left(cosTheta, -2, 1\right)}}}}
\end{array}
Initial program 98.1%
Taylor expanded in c around 0
associate-+r+N/A
+-commutativeN/A
lower-fma.f32N/A
Applied rewrites98.2%
lift-fma.f32N/A
lift-PI.f32N/A
clear-numN/A
sqrt-divN/A
metadata-evalN/A
lower-/.f32N/A
lower-sqrt.f32N/A
lower-/.f3298.2
Applied rewrites98.2%
lift-neg.f32N/A
lift-*.f32N/A
lift-exp.f32N/A
lift-/.f32N/A
lift-PI.f32N/A
lift-fma.f32N/A
lift-/.f32N/A
lift-sqrt.f32N/A
lift-/.f32N/A
lift-+.f32N/A
lower-+.f32N/A
Applied rewrites98.6%
Taylor expanded in cosTheta around 0
+-commutativeN/A
lower-fma.f32N/A
unpow2N/A
lower-*.f32N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f32N/A
unpow2N/A
lower-*.f3298.1
Applied rewrites98.1%
Final simplification98.1%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(fma
(/
(fma cosTheta (* cosTheta (fma (* cosTheta cosTheta) 0.5 -1.0)) 1.0)
cosTheta)
(sqrt (/ (fma cosTheta -2.0 1.0) PI))
(+ 1.0 c))))
float code(float cosTheta, float c) {
return 1.0f / fmaf((fmaf(cosTheta, (cosTheta * fmaf((cosTheta * cosTheta), 0.5f, -1.0f)), 1.0f) / cosTheta), sqrtf((fmaf(cosTheta, -2.0f, 1.0f) / ((float) M_PI))), (1.0f + c));
}
function code(cosTheta, c) return Float32(Float32(1.0) / fma(Float32(fma(cosTheta, Float32(cosTheta * fma(Float32(cosTheta * cosTheta), Float32(0.5), Float32(-1.0))), Float32(1.0)) / cosTheta), sqrt(Float32(fma(cosTheta, Float32(-2.0), Float32(1.0)) / Float32(pi))), Float32(Float32(1.0) + c))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(\frac{\mathsf{fma}\left(cosTheta, cosTheta \cdot \mathsf{fma}\left(cosTheta \cdot cosTheta, 0.5, -1\right), 1\right)}{cosTheta}, \sqrt{\frac{\mathsf{fma}\left(cosTheta, -2, 1\right)}{\pi}}, 1 + c\right)}
\end{array}
Initial program 98.1%
Taylor expanded in c around 0
associate-+r+N/A
+-commutativeN/A
lower-fma.f32N/A
Applied rewrites98.2%
Taylor expanded in cosTheta around 0
lower-/.f32N/A
+-commutativeN/A
unpow2N/A
associate-*l*N/A
lower-fma.f32N/A
lower-*.f32N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f32N/A
unpow2N/A
lower-*.f3297.6
Applied rewrites97.6%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(/
(fma cosTheta (- cosTheta) 1.0)
(* cosTheta (sqrt (/ PI (fma cosTheta -2.0 1.0))))))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + (fmaf(cosTheta, -cosTheta, 1.0f) / (cosTheta * sqrtf((((float) M_PI) / fmaf(cosTheta, -2.0f, 1.0f))))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(fma(cosTheta, Float32(-cosTheta), Float32(1.0)) / Float32(cosTheta * sqrt(Float32(Float32(pi) / fma(cosTheta, Float32(-2.0), Float32(1.0)))))))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \frac{\mathsf{fma}\left(cosTheta, -cosTheta, 1\right)}{cosTheta \cdot \sqrt{\frac{\pi}{\mathsf{fma}\left(cosTheta, -2, 1\right)}}}}
\end{array}
Initial program 98.1%
Taylor expanded in c around 0
associate-+r+N/A
+-commutativeN/A
lower-fma.f32N/A
Applied rewrites98.2%
lift-fma.f32N/A
lift-PI.f32N/A
clear-numN/A
sqrt-divN/A
metadata-evalN/A
lower-/.f32N/A
lower-sqrt.f32N/A
lower-/.f3298.2
Applied rewrites98.2%
lift-neg.f32N/A
lift-*.f32N/A
lift-exp.f32N/A
lift-/.f32N/A
lift-PI.f32N/A
lift-fma.f32N/A
lift-/.f32N/A
lift-sqrt.f32N/A
lift-/.f32N/A
lift-+.f32N/A
lower-+.f32N/A
Applied rewrites98.6%
Taylor expanded in cosTheta around 0
+-commutativeN/A
mul-1-negN/A
unpow2N/A
distribute-rgt-neg-inN/A
mul-1-negN/A
lower-fma.f32N/A
mul-1-negN/A
lower-neg.f3297.2
Applied rewrites97.2%
Final simplification97.2%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (fma (/ (fma cosTheta (- cosTheta) 1.0) cosTheta) (sqrt (/ (fma cosTheta -2.0 1.0) PI)) (+ 1.0 c))))
float code(float cosTheta, float c) {
return 1.0f / fmaf((fmaf(cosTheta, -cosTheta, 1.0f) / cosTheta), sqrtf((fmaf(cosTheta, -2.0f, 1.0f) / ((float) M_PI))), (1.0f + c));
}
function code(cosTheta, c) return Float32(Float32(1.0) / fma(Float32(fma(cosTheta, Float32(-cosTheta), Float32(1.0)) / cosTheta), sqrt(Float32(fma(cosTheta, Float32(-2.0), Float32(1.0)) / Float32(pi))), Float32(Float32(1.0) + c))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(\frac{\mathsf{fma}\left(cosTheta, -cosTheta, 1\right)}{cosTheta}, \sqrt{\frac{\mathsf{fma}\left(cosTheta, -2, 1\right)}{\pi}}, 1 + c\right)}
\end{array}
Initial program 98.1%
Taylor expanded in c around 0
associate-+r+N/A
+-commutativeN/A
lower-fma.f32N/A
Applied rewrites98.2%
Taylor expanded in cosTheta around 0
lower-/.f32N/A
+-commutativeN/A
mul-1-negN/A
unpow2N/A
distribute-rgt-neg-inN/A
mul-1-negN/A
lower-fma.f32N/A
mul-1-negN/A
lower-neg.f3296.7
Applied rewrites96.7%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (/ (fma (fma cosTheta (fma cosTheta -1.5 -1.0) 1.0) (sqrt (/ 1.0 PI)) cosTheta) cosTheta)))
float code(float cosTheta, float c) {
return 1.0f / (fmaf(fmaf(cosTheta, fmaf(cosTheta, -1.5f, -1.0f), 1.0f), sqrtf((1.0f / ((float) M_PI))), cosTheta) / cosTheta);
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(fma(fma(cosTheta, fma(cosTheta, Float32(-1.5), Float32(-1.0)), Float32(1.0)), sqrt(Float32(Float32(1.0) / Float32(pi))), cosTheta) / cosTheta)) end
\begin{array}{l}
\\
\frac{1}{\frac{\mathsf{fma}\left(\mathsf{fma}\left(cosTheta, \mathsf{fma}\left(cosTheta, -1.5, -1\right), 1\right), \sqrt{\frac{1}{\pi}}, cosTheta\right)}{cosTheta}}
\end{array}
Initial program 98.1%
Taylor expanded in cosTheta around 0
lower-/.f32N/A
Applied rewrites95.7%
Taylor expanded in c around 0
+-commutativeN/A
distribute-rgt1-inN/A
lower-fma.f32N/A
lower-fma.f32N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-PI.f3295.0
Applied rewrites95.0%
(FPCore (cosTheta c) :precision binary32 (/ cosTheta (fma (fma cosTheta (fma cosTheta -1.5 -1.0) 1.0) (sqrt (/ 1.0 PI)) cosTheta)))
float code(float cosTheta, float c) {
return cosTheta / fmaf(fmaf(cosTheta, fmaf(cosTheta, -1.5f, -1.0f), 1.0f), sqrtf((1.0f / ((float) M_PI))), cosTheta);
}
function code(cosTheta, c) return Float32(cosTheta / fma(fma(cosTheta, fma(cosTheta, Float32(-1.5), Float32(-1.0)), Float32(1.0)), sqrt(Float32(Float32(1.0) / Float32(pi))), cosTheta)) end
\begin{array}{l}
\\
\frac{cosTheta}{\mathsf{fma}\left(\mathsf{fma}\left(cosTheta, \mathsf{fma}\left(cosTheta, -1.5, -1\right), 1\right), \sqrt{\frac{1}{\pi}}, cosTheta\right)}
\end{array}
Initial program 98.1%
Taylor expanded in cosTheta around 0
lower-/.f32N/A
Applied rewrites95.7%
Taylor expanded in c around 0
lower-/.f32N/A
+-commutativeN/A
distribute-rgt1-inN/A
lower-fma.f32N/A
lower-fma.f32N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-PI.f3295.0
Applied rewrites95.0%
(FPCore (cosTheta c) :precision binary32 (* cosTheta (fma (+ PI (fma PI c (- (sqrt PI)))) (- cosTheta) (sqrt PI))))
float code(float cosTheta, float c) {
return cosTheta * fmaf((((float) M_PI) + fmaf(((float) M_PI), c, -sqrtf(((float) M_PI)))), -cosTheta, sqrtf(((float) M_PI)));
}
function code(cosTheta, c) return Float32(cosTheta * fma(Float32(Float32(pi) + fma(Float32(pi), c, Float32(-sqrt(Float32(pi))))), Float32(-cosTheta), sqrt(Float32(pi)))) end
\begin{array}{l}
\\
cosTheta \cdot \mathsf{fma}\left(\pi + \mathsf{fma}\left(\pi, c, -\sqrt{\pi}\right), -cosTheta, \sqrt{\pi}\right)
\end{array}
Initial program 98.1%
Taylor expanded in cosTheta around 0
lower-*.f32N/A
+-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites94.2%
Taylor expanded in c around 0
lower-+.f32N/A
lower-PI.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-PI.f32N/A
mul-1-negN/A
lower-neg.f32N/A
lower-sqrt.f32N/A
lower-PI.f3294.2
Applied rewrites94.2%
(FPCore (cosTheta c) :precision binary32 (* cosTheta (fma (- PI (sqrt PI)) (- cosTheta) (sqrt PI))))
float code(float cosTheta, float c) {
return cosTheta * fmaf((((float) M_PI) - sqrtf(((float) M_PI))), -cosTheta, sqrtf(((float) M_PI)));
}
function code(cosTheta, c) return Float32(cosTheta * fma(Float32(Float32(pi) - sqrt(Float32(pi))), Float32(-cosTheta), sqrt(Float32(pi)))) end
\begin{array}{l}
\\
cosTheta \cdot \mathsf{fma}\left(\pi - \sqrt{\pi}, -cosTheta, \sqrt{\pi}\right)
\end{array}
Initial program 98.1%
Taylor expanded in cosTheta around 0
lower-*.f32N/A
+-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites94.2%
Taylor expanded in c around 0
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
lower-PI.f32N/A
lower-sqrt.f32N/A
lower-PI.f3293.9
Applied rewrites93.9%
(FPCore (cosTheta c) :precision binary32 (/ (sqrt PI) (/ 1.0 cosTheta)))
float code(float cosTheta, float c) {
return sqrtf(((float) M_PI)) / (1.0f / cosTheta);
}
function code(cosTheta, c) return Float32(sqrt(Float32(pi)) / Float32(Float32(1.0) / cosTheta)) end
function tmp = code(cosTheta, c) tmp = sqrt(single(pi)) / (single(1.0) / cosTheta); end
\begin{array}{l}
\\
\frac{\sqrt{\pi}}{\frac{1}{cosTheta}}
\end{array}
Initial program 98.1%
Taylor expanded in cosTheta around 0
associate-*l/N/A
*-lft-identityN/A
lower-/.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-PI.f3290.6
Applied rewrites90.6%
lift-PI.f32N/A
lift-/.f32N/A
lift-sqrt.f32N/A
div-invN/A
lift-/.f32N/A
associate-/r*N/A
lift-sqrt.f32N/A
lift-/.f32N/A
sqrt-divN/A
metadata-evalN/A
lift-sqrt.f32N/A
remove-double-divN/A
lower-/.f3291.2
Applied rewrites91.2%
(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 98.1%
Taylor expanded in cosTheta around 0
lower-*.f32N/A
lower-sqrt.f32N/A
lower-PI.f3291.2
Applied rewrites91.2%
(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 98.1%
Taylor expanded in c around inf
lower-/.f325.1
Applied rewrites5.1%
herbie shell --seed 2024220
(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))))))