
(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 11 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
(let* ((t_0
(fma
cosTheta
(/ (fma cosTheta (fma cosTheta 0.5 -1.5) -1.0) (sqrt PI))
cosTheta)))
(/
1.0
(/ (- (/ 1.0 PI) (* t_0 t_0)) (* cosTheta (- (/ 1.0 (sqrt PI)) t_0))))))
float code(float cosTheta, float c) {
float t_0 = fmaf(cosTheta, (fmaf(cosTheta, fmaf(cosTheta, 0.5f, -1.5f), -1.0f) / sqrtf(((float) M_PI))), cosTheta);
return 1.0f / (((1.0f / ((float) M_PI)) - (t_0 * t_0)) / (cosTheta * ((1.0f / sqrtf(((float) M_PI))) - t_0)));
}
function code(cosTheta, c) t_0 = fma(cosTheta, Float32(fma(cosTheta, fma(cosTheta, Float32(0.5), Float32(-1.5)), Float32(-1.0)) / sqrt(Float32(pi))), cosTheta) return Float32(Float32(1.0) / Float32(Float32(Float32(Float32(1.0) / Float32(pi)) - Float32(t_0 * t_0)) / Float32(cosTheta * Float32(Float32(Float32(1.0) / sqrt(Float32(pi))) - t_0)))) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(cosTheta, \frac{\mathsf{fma}\left(cosTheta, \mathsf{fma}\left(cosTheta, 0.5, -1.5\right), -1\right)}{\sqrt{\pi}}, cosTheta\right)\\
\frac{1}{\frac{\frac{1}{\pi} - t\_0 \cdot t\_0}{cosTheta \cdot \left(\frac{1}{\sqrt{\pi}} - t\_0\right)}}
\end{array}
\end{array}
Initial program 97.5%
Taylor expanded in cosTheta around 0
lower-/.f32N/A
Applied rewrites97.2%
Taylor expanded in c around 0
Applied rewrites97.2%
Applied rewrites97.0%
Applied rewrites98.0%
Final simplification98.0%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(/
(fma
cosTheta
(/ (fma cosTheta (fma cosTheta 0.5 -1.5) -1.0) (sqrt PI))
cosTheta)
cosTheta)
(/ 1.0 (* cosTheta (sqrt PI))))))
float code(float cosTheta, float c) {
return 1.0f / ((fmaf(cosTheta, (fmaf(cosTheta, fmaf(cosTheta, 0.5f, -1.5f), -1.0f) / sqrtf(((float) M_PI))), cosTheta) / cosTheta) + (1.0f / (cosTheta * sqrtf(((float) M_PI)))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(fma(cosTheta, Float32(fma(cosTheta, fma(cosTheta, Float32(0.5), Float32(-1.5)), Float32(-1.0)) / sqrt(Float32(pi))), cosTheta) / cosTheta) + Float32(Float32(1.0) / Float32(cosTheta * sqrt(Float32(pi)))))) end
\begin{array}{l}
\\
\frac{1}{\frac{\mathsf{fma}\left(cosTheta, \frac{\mathsf{fma}\left(cosTheta, \mathsf{fma}\left(cosTheta, 0.5, -1.5\right), -1\right)}{\sqrt{\pi}}, cosTheta\right)}{cosTheta} + \frac{1}{cosTheta \cdot \sqrt{\pi}}}
\end{array}
Initial program 97.5%
Taylor expanded in cosTheta around 0
lower-/.f32N/A
Applied rewrites97.2%
Taylor expanded in c around 0
Applied rewrites97.2%
Applied rewrites97.0%
Applied rewrites97.9%
(FPCore (cosTheta c)
:precision binary32
(let* ((t_0 (sqrt (/ 1.0 PI))))
(/
1.0
(+
1.0
(+
(/ t_0 cosTheta)
(fma t_0 (fma cosTheta (fma cosTheta 0.5 -1.5) -1.0) c))))))
float code(float cosTheta, float c) {
float t_0 = sqrtf((1.0f / ((float) M_PI)));
return 1.0f / (1.0f + ((t_0 / cosTheta) + fmaf(t_0, fmaf(cosTheta, fmaf(cosTheta, 0.5f, -1.5f), -1.0f), c)));
}
function code(cosTheta, c) t_0 = sqrt(Float32(Float32(1.0) / Float32(pi))) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(t_0 / cosTheta) + fma(t_0, fma(cosTheta, fma(cosTheta, Float32(0.5), Float32(-1.5)), Float32(-1.0)), c)))) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{1}{\pi}}\\
\frac{1}{1 + \left(\frac{t\_0}{cosTheta} + \mathsf{fma}\left(t\_0, \mathsf{fma}\left(cosTheta, \mathsf{fma}\left(cosTheta, 0.5, -1.5\right), -1\right), c\right)\right)}
\end{array}
\end{array}
Initial program 97.5%
Taylor expanded in cosTheta around 0
lower-/.f32N/A
Applied rewrites97.2%
Taylor expanded in c around 0
Applied rewrites97.2%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(/
(fma
(/ (fma cosTheta (fma cosTheta 0.5 -1.5) -1.0) (sqrt PI))
cosTheta
(+ cosTheta (/ 1.0 (sqrt PI))))
cosTheta)))
float code(float cosTheta, float c) {
return 1.0f / (fmaf((fmaf(cosTheta, fmaf(cosTheta, 0.5f, -1.5f), -1.0f) / sqrtf(((float) M_PI))), cosTheta, (cosTheta + (1.0f / sqrtf(((float) M_PI))))) / cosTheta);
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(fma(Float32(fma(cosTheta, fma(cosTheta, Float32(0.5), Float32(-1.5)), Float32(-1.0)) / sqrt(Float32(pi))), cosTheta, Float32(cosTheta + Float32(Float32(1.0) / sqrt(Float32(pi))))) / cosTheta)) end
\begin{array}{l}
\\
\frac{1}{\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left(cosTheta, \mathsf{fma}\left(cosTheta, 0.5, -1.5\right), -1\right)}{\sqrt{\pi}}, cosTheta, cosTheta + \frac{1}{\sqrt{\pi}}\right)}{cosTheta}}
\end{array}
Initial program 97.5%
Taylor expanded in cosTheta around 0
lower-/.f32N/A
Applied rewrites97.2%
Taylor expanded in c around 0
Applied rewrites97.2%
Applied rewrites97.2%
Final simplification97.2%
(FPCore (cosTheta c)
:precision binary32
(let* ((t_0 (sqrt (/ 1.0 PI))))
(/
1.0
(/
(+ t_0 (fma cosTheta (fma t_0 (fma -1.5 cosTheta -1.0) c) cosTheta))
cosTheta))))
float code(float cosTheta, float c) {
float t_0 = sqrtf((1.0f / ((float) M_PI)));
return 1.0f / ((t_0 + fmaf(cosTheta, fmaf(t_0, fmaf(-1.5f, cosTheta, -1.0f), c), cosTheta)) / cosTheta);
}
function code(cosTheta, c) t_0 = sqrt(Float32(Float32(1.0) / Float32(pi))) return Float32(Float32(1.0) / Float32(Float32(t_0 + fma(cosTheta, fma(t_0, fma(Float32(-1.5), cosTheta, Float32(-1.0)), c), cosTheta)) / cosTheta)) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{1}{\pi}}\\
\frac{1}{\frac{t\_0 + \mathsf{fma}\left(cosTheta, \mathsf{fma}\left(t\_0, \mathsf{fma}\left(-1.5, cosTheta, -1\right), c\right), cosTheta\right)}{cosTheta}}
\end{array}
\end{array}
Initial program 97.5%
Taylor expanded in cosTheta around 0
lower-/.f32N/A
Applied rewrites96.7%
(FPCore (cosTheta c) :precision binary32 (* cosTheta (fma (fma PI (- c (sqrt (/ 1.0 PI))) PI) (- cosTheta) (sqrt PI))))
float code(float cosTheta, float c) {
return cosTheta * fmaf(fmaf(((float) M_PI), (c - sqrtf((1.0f / ((float) M_PI)))), ((float) M_PI)), -cosTheta, sqrtf(((float) M_PI)));
}
function code(cosTheta, c) return Float32(cosTheta * fma(fma(Float32(pi), Float32(c - sqrt(Float32(Float32(1.0) / Float32(pi)))), Float32(pi)), Float32(-cosTheta), sqrt(Float32(pi)))) end
\begin{array}{l}
\\
cosTheta \cdot \mathsf{fma}\left(\mathsf{fma}\left(\pi, c - \sqrt{\frac{1}{\pi}}, \pi\right), -cosTheta, \sqrt{\pi}\right)
\end{array}
Initial program 97.5%
Taylor expanded in cosTheta around 0
lower-*.f32N/A
+-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites96.4%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ (+ 1.0 c) (* (/ 1.0 (* cosTheta (sqrt PI))) (- 1.0 cosTheta)))))
float code(float cosTheta, float c) {
return 1.0f / ((1.0f + c) + ((1.0f / (cosTheta * sqrtf(((float) M_PI)))) * (1.0f - 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(pi)))) * Float32(Float32(1.0) - cosTheta)))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / ((single(1.0) + c) + ((single(1.0) / (cosTheta * sqrt(single(pi)))) * (single(1.0) - cosTheta))); end
\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \frac{1}{cosTheta \cdot \sqrt{\pi}} \cdot \left(1 - cosTheta\right)}
\end{array}
Initial program 97.5%
Taylor expanded in cosTheta around 0
associate-*r*N/A
lower-/.f32N/A
mul-1-negN/A
cancel-sign-sub-invN/A
*-lft-identityN/A
distribute-rgt-out--N/A
lower-*.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-PI.f32N/A
lower--.f3295.4
Applied rewrites95.4%
Applied rewrites96.0%
Final simplification96.0%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ (* (/ 1.0 (* cosTheta (sqrt PI))) (- 1.0 cosTheta)) 1.0)))
float code(float cosTheta, float c) {
return 1.0f / (((1.0f / (cosTheta * sqrtf(((float) M_PI)))) * (1.0f - cosTheta)) + 1.0f);
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(Float32(Float32(1.0) / Float32(cosTheta * sqrt(Float32(pi)))) * Float32(Float32(1.0) - cosTheta)) + Float32(1.0))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (((single(1.0) / (cosTheta * sqrt(single(pi)))) * (single(1.0) - cosTheta)) + single(1.0)); end
\begin{array}{l}
\\
\frac{1}{\frac{1}{cosTheta \cdot \sqrt{\pi}} \cdot \left(1 - cosTheta\right) + 1}
\end{array}
Initial program 97.5%
Taylor expanded in cosTheta around 0
associate-*r*N/A
lower-/.f32N/A
mul-1-negN/A
cancel-sign-sub-invN/A
*-lft-identityN/A
distribute-rgt-out--N/A
lower-*.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-PI.f32N/A
lower--.f3295.4
Applied rewrites95.4%
Taylor expanded in c around 0
Applied rewrites95.4%
Applied rewrites96.0%
Final simplification96.0%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ 1.0 (/ (- 1.0 cosTheta) (* cosTheta (sqrt PI))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + ((1.0f - cosTheta) / (cosTheta * sqrtf(((float) M_PI)))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(Float32(1.0) - cosTheta) / Float32(cosTheta * sqrt(Float32(pi)))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + ((single(1.0) - cosTheta) / (cosTheta * sqrt(single(pi))))); end
\begin{array}{l}
\\
\frac{1}{1 + \frac{1 - cosTheta}{cosTheta \cdot \sqrt{\pi}}}
\end{array}
Initial program 97.5%
Taylor expanded in cosTheta around 0
associate-*r*N/A
lower-/.f32N/A
mul-1-negN/A
cancel-sign-sub-invN/A
*-lft-identityN/A
distribute-rgt-out--N/A
lower-*.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-PI.f32N/A
lower--.f3295.4
Applied rewrites95.4%
Taylor expanded in c around 0
Applied rewrites95.4%
Applied rewrites95.2%
Applied rewrites96.0%
(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.5%
Taylor expanded in cosTheta around 0
lower-*.f32N/A
lower-sqrt.f32N/A
lower-PI.f3294.1
Applied rewrites94.1%
(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.5%
Taylor expanded in c around inf
lower-/.f324.9
Applied rewrites4.9%
herbie shell --seed 2024219
(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))))))