
(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 7 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
(*
(/ (exp (* -1.0 (pow cosTheta 2.0))) cosTheta)
(sqrt (/ (- 1.0 (* 2.0 cosTheta)) PI)))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (c + ((expf((-1.0f * powf(cosTheta, 2.0f))) / cosTheta) * sqrtf(((1.0f - (2.0f * cosTheta)) / ((float) M_PI))))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(c + Float32(Float32(exp(Float32(Float32(-1.0) * (cosTheta ^ Float32(2.0)))) / cosTheta) * sqrt(Float32(Float32(Float32(1.0) - Float32(Float32(2.0) * cosTheta)) / Float32(pi))))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + (c + ((exp((single(-1.0) * (cosTheta ^ single(2.0)))) / cosTheta) * sqrt(((single(1.0) - (single(2.0) * cosTheta)) / single(pi)))))); end
\begin{array}{l}
\\
\frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\pi}}\right)}
\end{array}
Initial program 97.8%
Taylor expanded in c around 0
lower-+.f32N/A
lower-+.f32N/A
lower-*.f32N/A
lower-/.f32N/A
lower-exp.f32N/A
lower-*.f32N/A
lower-pow.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower--.f32N/A
lower-*.f32N/A
lift-PI.f3297.9
Applied rewrites97.9%
(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}
Initial program 97.8%
(FPCore (cosTheta c) :precision binary32 (* cosTheta (+ (sqrt PI) (* -1.0 (* cosTheta (* PI (+ 1.0 (+ c (* -1.0 (sqrt (/ 1.0 PI)))))))))))
float code(float cosTheta, float c) {
return cosTheta * (sqrtf(((float) M_PI)) + (-1.0f * (cosTheta * (((float) M_PI) * (1.0f + (c + (-1.0f * sqrtf((1.0f / ((float) M_PI))))))))));
}
function code(cosTheta, c) return Float32(cosTheta * Float32(sqrt(Float32(pi)) + Float32(Float32(-1.0) * Float32(cosTheta * Float32(Float32(pi) * Float32(Float32(1.0) + Float32(c + Float32(Float32(-1.0) * sqrt(Float32(Float32(1.0) / Float32(pi))))))))))) end
function tmp = code(cosTheta, c) tmp = cosTheta * (sqrt(single(pi)) + (single(-1.0) * (cosTheta * (single(pi) * (single(1.0) + (c + (single(-1.0) * sqrt((single(1.0) / single(pi)))))))))); end
\begin{array}{l}
\\
cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\pi}}\right)\right)\right)\right)\right)
\end{array}
Initial program 97.8%
Taylor expanded in cosTheta around 0
lower-*.f32N/A
lower-+.f32N/A
lift-sqrt.f32N/A
lift-PI.f32N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-+.f32N/A
lower-+.f32N/A
Applied rewrites95.8%
(FPCore (cosTheta c) :precision binary32 (* cosTheta (+ (sqrt PI) (* -1.0 (* cosTheta (* PI (+ 1.0 (* -1.0 (sqrt (/ 1.0 PI))))))))))
float code(float cosTheta, float c) {
return cosTheta * (sqrtf(((float) M_PI)) + (-1.0f * (cosTheta * (((float) M_PI) * (1.0f + (-1.0f * sqrtf((1.0f / ((float) M_PI)))))))));
}
function code(cosTheta, c) return Float32(cosTheta * Float32(sqrt(Float32(pi)) + Float32(Float32(-1.0) * Float32(cosTheta * Float32(Float32(pi) * Float32(Float32(1.0) + Float32(Float32(-1.0) * sqrt(Float32(Float32(1.0) / Float32(pi)))))))))) end
function tmp = code(cosTheta, c) tmp = cosTheta * (sqrt(single(pi)) + (single(-1.0) * (cosTheta * (single(pi) * (single(1.0) + (single(-1.0) * sqrt((single(1.0) / single(pi))))))))); end
\begin{array}{l}
\\
cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + -1 \cdot \sqrt{\frac{1}{\pi}}\right)\right)\right)\right)
\end{array}
Initial program 97.8%
Taylor expanded in cosTheta around 0
lower-*.f32N/A
lower-+.f32N/A
lift-sqrt.f32N/A
lift-PI.f32N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-+.f32N/A
lower-+.f32N/A
Applied rewrites95.8%
Taylor expanded in c around 0
lift-/.f32N/A
lift-PI.f32N/A
lift-sqrt.f32N/A
lift-*.f3295.7
Applied rewrites95.7%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (+ 1.0 (+ c (* (/ 1.0 cosTheta) (sqrt (/ (- 1.0 (* 2.0 cosTheta)) PI)))))))
float code(float cosTheta, float c) {
return 1.0f / (1.0f + (c + ((1.0f / cosTheta) * sqrtf(((1.0f - (2.0f * cosTheta)) / ((float) M_PI))))));
}
function code(cosTheta, c) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(c + Float32(Float32(Float32(1.0) / cosTheta) * sqrt(Float32(Float32(Float32(1.0) - Float32(Float32(2.0) * cosTheta)) / Float32(pi))))))) end
function tmp = code(cosTheta, c) tmp = single(1.0) / (single(1.0) + (c + ((single(1.0) / cosTheta) * sqrt(((single(1.0) - (single(2.0) * cosTheta)) / single(pi)))))); end
\begin{array}{l}
\\
\frac{1}{1 + \left(c + \frac{1}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\pi}}\right)}
\end{array}
Initial program 97.8%
Taylor expanded in c around 0
lower-+.f32N/A
lower-+.f32N/A
lower-*.f32N/A
lower-/.f32N/A
lower-exp.f32N/A
lower-*.f32N/A
lower-pow.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower--.f32N/A
lower-*.f32N/A
lift-PI.f3297.9
Applied rewrites97.9%
Taylor expanded in cosTheta around 0
Applied rewrites95.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 97.8%
Taylor expanded in cosTheta around 0
lower-*.f32N/A
lift-sqrt.f32N/A
lift-PI.f3293.0
Applied rewrites93.0%
(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 2025132
(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))))))