
(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}
Herbie found 10 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 (exp (* cosTheta cosTheta))))
(*
(* (sqrt PI) t_0)
(/
cosTheta
(fma
(- c -1.0)
(* (* (sqrt PI) cosTheta) t_0)
(sqrt (- (- 1.0 cosTheta) cosTheta)))))))
float code(float cosTheta, float c) {
float t_0 = expf((cosTheta * cosTheta));
return (sqrtf(((float) M_PI)) * t_0) * (cosTheta / fmaf((c - -1.0f), ((sqrtf(((float) M_PI)) * cosTheta) * t_0), sqrtf(((1.0f - cosTheta) - cosTheta))));
}
function code(cosTheta, c) t_0 = exp(Float32(cosTheta * cosTheta)) return Float32(Float32(sqrt(Float32(pi)) * t_0) * Float32(cosTheta / fma(Float32(c - Float32(-1.0)), Float32(Float32(sqrt(Float32(pi)) * cosTheta) * t_0), sqrt(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta))))) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{cosTheta \cdot cosTheta}\\
\left(\sqrt{\pi} \cdot t\_0\right) \cdot \frac{cosTheta}{\mathsf{fma}\left(c - -1, \left(\sqrt{\pi} \cdot cosTheta\right) \cdot t\_0, \sqrt{\left(1 - cosTheta\right) - cosTheta}\right)}
\end{array}
\end{array}
Initial program 97.8%
lift-*.f32N/A
lift-*.f32N/A
lift-/.f32N/A
lift-/.f32N/A
frac-timesN/A
*-lft-identityN/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
frac-timesN/A
*-rgt-identityN/A
lower-/.f32N/A
Applied rewrites98.5%
lift-/.f32N/A
lift-+.f32N/A
lift-/.f32N/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f32N/A
Applied rewrites98.7%
lift-/.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*r*N/A
associate-/l*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-/.f3298.7
lift-fma.f32N/A
Applied rewrites98.7%
(FPCore (cosTheta c) :precision binary32 (let* ((t_0 (* (exp (* cosTheta cosTheta)) (* (sqrt PI) cosTheta)))) (/ t_0 (fma t_0 (- c -1.0) (sqrt (- (- 1.0 cosTheta) cosTheta))))))
float code(float cosTheta, float c) {
float t_0 = expf((cosTheta * cosTheta)) * (sqrtf(((float) M_PI)) * cosTheta);
return t_0 / fmaf(t_0, (c - -1.0f), sqrtf(((1.0f - cosTheta) - cosTheta)));
}
function code(cosTheta, c) t_0 = Float32(exp(Float32(cosTheta * cosTheta)) * Float32(sqrt(Float32(pi)) * cosTheta)) return Float32(t_0 / fma(t_0, Float32(c - Float32(-1.0)), sqrt(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta)))) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{cosTheta \cdot cosTheta} \cdot \left(\sqrt{\pi} \cdot cosTheta\right)\\
\frac{t\_0}{\mathsf{fma}\left(t\_0, c - -1, \sqrt{\left(1 - cosTheta\right) - cosTheta}\right)}
\end{array}
\end{array}
Initial program 97.8%
lift-*.f32N/A
lift-*.f32N/A
lift-/.f32N/A
lift-/.f32N/A
frac-timesN/A
*-lft-identityN/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
frac-timesN/A
*-rgt-identityN/A
lower-/.f32N/A
Applied rewrites98.5%
lift-/.f32N/A
lift-+.f32N/A
lift-/.f32N/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f32N/A
Applied rewrites98.7%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(-
c
(-
-1.0
(/
(sqrt (- (- 1.0 cosTheta) cosTheta))
(* (* cosTheta (sqrt PI)) (exp (* cosTheta cosTheta))))))))
float code(float cosTheta, float c) {
return 1.0f / (c - (-1.0f - (sqrtf(((1.0f - cosTheta) - cosTheta)) / ((cosTheta * sqrtf(((float) M_PI))) * expf((cosTheta * cosTheta))))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(c - Float32(Float32(-1.0) - Float32(sqrt(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta)) / Float32(Float32(cosTheta * sqrt(Float32(pi))) * exp(Float32(cosTheta * cosTheta))))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (c - (single(-1.0) - (sqrt(((single(1.0) - cosTheta) - cosTheta)) / ((cosTheta * sqrt(single(pi))) * exp((cosTheta * cosTheta)))))); end
\begin{array}{l}
\\
\frac{1}{c - \left(-1 - \frac{\sqrt{\left(1 - cosTheta\right) - cosTheta}}{\left(cosTheta \cdot \sqrt{\pi}\right) \cdot e^{cosTheta \cdot cosTheta}}\right)}
\end{array}
Initial program 97.8%
Applied rewrites98.0%
lift-/.f32N/A
lift-sqrt.f32N/A
lift-/.f32N/A
sqrt-undivN/A
lift-sqrt.f32N/A
lift-sqrt.f32N/A
associate-/r*N/A
*-commutativeN/A
lift-*.f32N/A
associate-*r*N/A
*-commutativeN/A
lift-exp.f32N/A
lift-*.f32N/A
lift-sqrt.f32N/A
lift-PI.f32N/A
Applied rewrites98.5%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(-
c
(-
-1.0
(/
(sqrt (/ (- (- 1.0 cosTheta) cosTheta) PI))
(* (exp (* cosTheta cosTheta)) cosTheta))))))
float code(float cosTheta, float c) {
return 1.0f / (c - (-1.0f - (sqrtf((((1.0f - cosTheta) - cosTheta) / ((float) M_PI))) / (expf((cosTheta * cosTheta)) * cosTheta))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(c - Float32(Float32(-1.0) - Float32(sqrt(Float32(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta) / Float32(pi))) / Float32(exp(Float32(cosTheta * cosTheta)) * cosTheta))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (c - (single(-1.0) - (sqrt((((single(1.0) - cosTheta) - cosTheta) / single(pi))) / (exp((cosTheta * cosTheta)) * cosTheta)))); end
\begin{array}{l}
\\
\frac{1}{c - \left(-1 - \frac{\sqrt{\frac{\left(1 - cosTheta\right) - cosTheta}{\pi}}}{e^{cosTheta \cdot cosTheta} \cdot cosTheta}\right)}
\end{array}
Initial program 97.8%
Applied rewrites98.0%
(FPCore (cosTheta c)
:precision binary32
(*
(* (sqrt PI) 1.0)
(/
cosTheta
(fma
(- c -1.0)
(* (* (sqrt PI) cosTheta) 1.0)
(sqrt (- (- 1.0 cosTheta) cosTheta))))))
float code(float cosTheta, float c) {
return (sqrtf(((float) M_PI)) * 1.0f) * (cosTheta / fmaf((c - -1.0f), ((sqrtf(((float) M_PI)) * cosTheta) * 1.0f), sqrtf(((1.0f - cosTheta) - cosTheta))));
}
function code(cosTheta, c) return Float32(Float32(sqrt(Float32(pi)) * Float32(1.0)) * Float32(cosTheta / fma(Float32(c - Float32(-1.0)), Float32(Float32(sqrt(Float32(pi)) * cosTheta) * Float32(1.0)), sqrt(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta))))) end
\begin{array}{l}
\\
\left(\sqrt{\pi} \cdot 1\right) \cdot \frac{cosTheta}{\mathsf{fma}\left(c - -1, \left(\sqrt{\pi} \cdot cosTheta\right) \cdot 1, \sqrt{\left(1 - cosTheta\right) - cosTheta}\right)}
\end{array}
Initial program 97.8%
lift-*.f32N/A
lift-*.f32N/A
lift-/.f32N/A
lift-/.f32N/A
frac-timesN/A
*-lft-identityN/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
frac-timesN/A
*-rgt-identityN/A
lower-/.f32N/A
Applied rewrites98.5%
lift-/.f32N/A
lift-+.f32N/A
lift-/.f32N/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f32N/A
Applied rewrites98.7%
lift-/.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*r*N/A
associate-/l*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-/.f3298.7
lift-fma.f32N/A
Applied rewrites98.7%
Taylor expanded in cosTheta around 0
Applied rewrites95.9%
Taylor expanded in cosTheta around 0
Applied rewrites96.0%
(FPCore (cosTheta c) :precision binary32 (/ (* 1.0 (* (sqrt PI) cosTheta)) (+ 1.0 (* cosTheta (- (fma -0.5 cosTheta (* (sqrt PI) (+ 1.0 c))) 1.0)))))
float code(float cosTheta, float c) {
return (1.0f * (sqrtf(((float) M_PI)) * cosTheta)) / (1.0f + (cosTheta * (fmaf(-0.5f, cosTheta, (sqrtf(((float) M_PI)) * (1.0f + c))) - 1.0f)));
}
function code(cosTheta, c) return Float32(Float32(Float32(1.0) * Float32(sqrt(Float32(pi)) * cosTheta)) / Float32(Float32(1.0) + Float32(cosTheta * Float32(fma(Float32(-0.5), cosTheta, Float32(sqrt(Float32(pi)) * Float32(Float32(1.0) + c))) - Float32(1.0))))) end
\begin{array}{l}
\\
\frac{1 \cdot \left(\sqrt{\pi} \cdot cosTheta\right)}{1 + cosTheta \cdot \left(\mathsf{fma}\left(-0.5, cosTheta, \sqrt{\pi} \cdot \left(1 + c\right)\right) - 1\right)}
\end{array}
Initial program 97.8%
lift-*.f32N/A
lift-*.f32N/A
lift-/.f32N/A
lift-/.f32N/A
frac-timesN/A
*-lft-identityN/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
frac-timesN/A
*-rgt-identityN/A
lower-/.f32N/A
Applied rewrites98.5%
lift-/.f32N/A
lift-+.f32N/A
lift-/.f32N/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f32N/A
Applied rewrites98.7%
Taylor expanded in cosTheta around 0
lower-+.f32N/A
lower-*.f32N/A
lower--.f32N/A
lower-fma.f32N/A
lower-*.f32N/A
lower-sqrt.f32N/A
lower-PI.f32N/A
lower-+.f3296.9
Applied rewrites96.9%
Taylor expanded in cosTheta around 0
Applied rewrites95.9%
(FPCore (cosTheta c) :precision binary32 (* (- (sqrt PI) (* (* cosTheta (fma (- c -1.0) (sqrt PI) -1.0)) (sqrt PI))) cosTheta))
float code(float cosTheta, float c) {
return (sqrtf(((float) M_PI)) - ((cosTheta * fmaf((c - -1.0f), sqrtf(((float) M_PI)), -1.0f)) * sqrtf(((float) M_PI)))) * cosTheta;
}
function code(cosTheta, c) return Float32(Float32(sqrt(Float32(pi)) - Float32(Float32(cosTheta * fma(Float32(c - Float32(-1.0)), sqrt(Float32(pi)), Float32(-1.0))) * sqrt(Float32(pi)))) * cosTheta) end
\begin{array}{l}
\\
\left(\sqrt{\pi} - \left(cosTheta \cdot \mathsf{fma}\left(c - -1, \sqrt{\pi}, -1\right)\right) \cdot \sqrt{\pi}\right) \cdot cosTheta
\end{array}
Initial program 97.8%
lift-*.f32N/A
lift-*.f32N/A
lift-/.f32N/A
lift-/.f32N/A
frac-timesN/A
*-lft-identityN/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
frac-timesN/A
*-rgt-identityN/A
lower-/.f32N/A
Applied rewrites98.5%
lift-/.f32N/A
lift-+.f32N/A
lift-/.f32N/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f32N/A
Applied rewrites98.7%
Taylor expanded in cosTheta around 0
lower-*.f32N/A
lower-+.f32N/A
lower-sqrt.f32N/A
lower-PI.f32N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f32N/A
Applied rewrites95.7%
lift-*.f32N/A
*-commutativeN/A
lower-*.f3295.7
Applied rewrites95.7%
(FPCore (cosTheta c) :precision binary32 (* cosTheta (+ (sqrt PI) (* -1.0 (* cosTheta (* (sqrt PI) (- (sqrt PI) 1.0)))))))
float code(float cosTheta, float c) {
return cosTheta * (sqrtf(((float) M_PI)) + (-1.0f * (cosTheta * (sqrtf(((float) M_PI)) * (sqrtf(((float) M_PI)) - 1.0f)))));
}
function code(cosTheta, c) return Float32(cosTheta * Float32(sqrt(Float32(pi)) + Float32(Float32(-1.0) * Float32(cosTheta * Float32(sqrt(Float32(pi)) * Float32(sqrt(Float32(pi)) - Float32(1.0))))))) end
function tmp = code(cosTheta, c) tmp = cosTheta * (sqrt(single(pi)) + (single(-1.0) * (cosTheta * (sqrt(single(pi)) * (sqrt(single(pi)) - single(1.0)))))); end
\begin{array}{l}
\\
cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\sqrt{\pi} \cdot \left(\sqrt{\pi} - 1\right)\right)\right)\right)
\end{array}
Initial program 97.8%
lift-*.f32N/A
lift-*.f32N/A
lift-/.f32N/A
lift-/.f32N/A
frac-timesN/A
*-lft-identityN/A
lift-exp.f32N/A
lift-*.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
exp-negN/A
frac-timesN/A
*-rgt-identityN/A
lower-/.f32N/A
Applied rewrites98.5%
lift-/.f32N/A
lift-+.f32N/A
lift-/.f32N/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f32N/A
Applied rewrites98.7%
Taylor expanded in cosTheta around 0
lower-*.f32N/A
lower-+.f32N/A
lower-sqrt.f32N/A
lower-PI.f32N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f32N/A
Applied rewrites95.7%
Taylor expanded in c around 0
lower--.f32N/A
lower-sqrt.f32N/A
lower-PI.f3295.6
Applied rewrites95.6%
(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%
Taylor expanded in cosTheta around 0
lower-*.f32N/A
lower-sqrt.f32N/A
lower-PI.f3292.9
Applied rewrites92.9%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 c))
float code(float cosTheta, float c) {
return 1.0f / c;
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(4) function code(costheta, c)
use fmin_fmax_functions
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.8%
Taylor expanded in c around inf
lower-/.f325.0
Applied rewrites5.0%
herbie shell --seed 2025162
(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))))))