
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(* (/ 1.0 (sqrt (PI))) (/ (sqrt (- (- 1.0 cosTheta) cosTheta)) cosTheta))
(exp (* (- cosTheta) cosTheta))))))\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \left(\frac{1}{\sqrt{\mathsf{PI}\left(\right)}} \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))))))\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \left(\frac{1}{\sqrt{\mathsf{PI}\left(\right)}} \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)
(*
(/ (sqrt (fma cosTheta -2.0 1.0)) (* (sqrt (PI)) cosTheta))
(exp (* (- cosTheta) cosTheta))))))\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\sqrt{\mathsf{PI}\left(\right)} \cdot cosTheta} \cdot e^{\left(-cosTheta\right) \cdot cosTheta}}
\end{array}
Initial program 98.0%
lift-*.f32N/A
lift-/.f32N/A
lift-PI.f32N/A
lift-sqrt.f32N/A
lift-/.f32N/A
lift-sqrt.f32N/A
lift--.f32N/A
lift--.f32N/A
frac-timesN/A
*-commutativeN/A
lower-/.f32N/A
Applied rewrites98.6%
lift-*.f32N/A
lift-sqrt.f32N/A
lift-fma.f32N/A
*-lft-identityN/A
lift-fma.f32N/A
lift-sqrt.f3298.6
lift-fma.f32N/A
*-commutativeN/A
lower-fma.f3298.6
Applied rewrites98.6%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
1.0
(*
(/ (sqrt (fma cosTheta -2.0 1.0)) (* (sqrt (PI)) cosTheta))
(exp (* (- cosTheta) cosTheta))))))\begin{array}{l}
\\
\frac{1}{1 + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\sqrt{\mathsf{PI}\left(\right)} \cdot cosTheta} \cdot e^{\left(-cosTheta\right) \cdot cosTheta}}
\end{array}
Initial program 98.0%
lift-*.f32N/A
lift-/.f32N/A
lift-PI.f32N/A
lift-sqrt.f32N/A
lift-/.f32N/A
lift-sqrt.f32N/A
lift--.f32N/A
lift--.f32N/A
frac-timesN/A
*-commutativeN/A
lower-/.f32N/A
Applied rewrites98.6%
lift-*.f32N/A
lift-sqrt.f32N/A
lift-fma.f32N/A
*-lft-identityN/A
lift-fma.f32N/A
lift-sqrt.f3298.6
lift-fma.f32N/A
*-commutativeN/A
lower-fma.f3298.6
Applied rewrites98.6%
Taylor expanded in c around 0
Applied rewrites98.5%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(/ (sqrt (fma -2.0 cosTheta 1.0)) (* (sqrt (PI)) cosTheta))
(fma
(-
(*
(fma -0.16666666666666666 (* cosTheta cosTheta) 0.5)
(* cosTheta cosTheta))
1.0)
(* cosTheta cosTheta)
1.0)))))\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \frac{\sqrt{\mathsf{fma}\left(-2, cosTheta, 1\right)}}{\sqrt{\mathsf{PI}\left(\right)} \cdot cosTheta} \cdot \mathsf{fma}\left(\mathsf{fma}\left(-0.16666666666666666, cosTheta \cdot cosTheta, 0.5\right) \cdot \left(cosTheta \cdot cosTheta\right) - 1, cosTheta \cdot cosTheta, 1\right)}
\end{array}
Initial program 98.0%
lift-*.f32N/A
lift-/.f32N/A
lift-PI.f32N/A
lift-sqrt.f32N/A
lift-/.f32N/A
lift-sqrt.f32N/A
lift--.f32N/A
lift--.f32N/A
frac-timesN/A
*-commutativeN/A
lower-/.f32N/A
Applied rewrites98.6%
Taylor expanded in cosTheta around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f32N/A
unpow2N/A
lower-*.f32N/A
unpow2N/A
lower-*.f32N/A
unpow2N/A
lower-*.f3298.5
Applied rewrites98.5%
Final simplification98.5%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(/ (sqrt (- (- 1.0 cosTheta) cosTheta)) (* (sqrt (PI)) cosTheta))
(fma (- (* (* cosTheta cosTheta) 0.5) 1.0) (* cosTheta cosTheta) 1.0)))))\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \frac{\sqrt{\left(1 - cosTheta\right) - cosTheta}}{\sqrt{\mathsf{PI}\left(\right)} \cdot cosTheta} \cdot \mathsf{fma}\left(\left(cosTheta \cdot cosTheta\right) \cdot 0.5 - 1, cosTheta \cdot cosTheta, 1\right)}
\end{array}
Initial program 98.0%
lift-*.f32N/A
lift-/.f32N/A
lift-PI.f32N/A
lift-sqrt.f32N/A
lift-/.f32N/A
lift-sqrt.f32N/A
lift--.f32N/A
lift--.f32N/A
frac-timesN/A
*-commutativeN/A
lower-/.f32N/A
Applied rewrites98.6%
lift-*.f32N/A
lift-sqrt.f32N/A
lift-fma.f32N/A
*-lft-identityN/A
lift-fma.f32N/A
lift-sqrt.f3298.6
lift-fma.f32N/A
*-commutativeN/A
lower-fma.f3298.6
Applied rewrites98.6%
Taylor expanded in cosTheta around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
pow2N/A
lift-*.f32N/A
pow2N/A
lift-*.f3298.2
Applied rewrites98.2%
lift-fma.f32N/A
+-commutativeN/A
*-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
count-2-revN/A
associate--l-N/A
lower--.f32N/A
lower--.f3298.3
Applied rewrites98.3%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
(+ 1.0 c)
(*
(/ (sqrt (fma cosTheta -2.0 1.0)) (* (sqrt (PI)) cosTheta))
(fma (- cosTheta) cosTheta 1.0)))))\begin{array}{l}
\\
\frac{1}{\left(1 + c\right) + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\sqrt{\mathsf{PI}\left(\right)} \cdot cosTheta} \cdot \mathsf{fma}\left(-cosTheta, cosTheta, 1\right)}
\end{array}
Initial program 98.0%
lift-*.f32N/A
lift-/.f32N/A
lift-PI.f32N/A
lift-sqrt.f32N/A
lift-/.f32N/A
lift-sqrt.f32N/A
lift--.f32N/A
lift--.f32N/A
frac-timesN/A
*-commutativeN/A
lower-/.f32N/A
Applied rewrites98.6%
lift-*.f32N/A
lift-sqrt.f32N/A
lift-fma.f32N/A
*-lft-identityN/A
lift-fma.f32N/A
lift-sqrt.f3298.6
lift-fma.f32N/A
*-commutativeN/A
lower-fma.f3298.6
Applied rewrites98.6%
Taylor expanded in cosTheta around 0
+-commutativeN/A
mul-1-negN/A
pow2N/A
distribute-lft-neg-outN/A
lower-fma.f32N/A
lift-neg.f3297.7
Applied rewrites97.7%
(FPCore (cosTheta c)
:precision binary32
(/
1.0
(+
1.0
(*
(/ (sqrt (fma cosTheta -2.0 1.0)) (* (sqrt (PI)) cosTheta))
(fma (- cosTheta) cosTheta 1.0)))))\begin{array}{l}
\\
\frac{1}{1 + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\sqrt{\mathsf{PI}\left(\right)} \cdot cosTheta} \cdot \mathsf{fma}\left(-cosTheta, cosTheta, 1\right)}
\end{array}
Initial program 98.0%
lift-*.f32N/A
lift-/.f32N/A
lift-PI.f32N/A
lift-sqrt.f32N/A
lift-/.f32N/A
lift-sqrt.f32N/A
lift--.f32N/A
lift--.f32N/A
frac-timesN/A
*-commutativeN/A
lower-/.f32N/A
Applied rewrites98.6%
lift-*.f32N/A
lift-sqrt.f32N/A
lift-fma.f32N/A
*-lft-identityN/A
lift-fma.f32N/A
lift-sqrt.f3298.6
lift-fma.f32N/A
*-commutativeN/A
lower-fma.f3298.6
Applied rewrites98.6%
Taylor expanded in c around 0
Applied rewrites98.5%
Taylor expanded in cosTheta around 0
+-commutativeN/A
mul-1-negN/A
pow2N/A
distribute-lft-neg-outN/A
lower-fma.f32N/A
lift-neg.f3297.7
Applied rewrites97.7%
(FPCore (cosTheta c) :precision binary32 (/ 1.0 (fma (/ (sqrt (/ (fma cosTheta -2.0 1.0) (PI))) cosTheta) (fma (- cosTheta) cosTheta 1.0) (+ c 1.0))))
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(\frac{\sqrt{\frac{\mathsf{fma}\left(cosTheta, -2, 1\right)}{\mathsf{PI}\left(\right)}}}{cosTheta}, \mathsf{fma}\left(-cosTheta, cosTheta, 1\right), c + 1\right)}
\end{array}
Initial program 98.0%
Taylor expanded in cosTheta around 0
+-commutativeN/A
mul-1-negN/A
unpow2N/A
distribute-lft-neg-outN/A
lower-fma.f32N/A
lift-neg.f3297.2
Applied rewrites97.2%
Applied rewrites97.2%
(FPCore (cosTheta c) :precision binary32 (let* ((t_0 (sqrt (PI)))) (* (fma (* (+ (+ (/ -1.0 t_0) c) 1.0) (PI)) (- cosTheta) t_0) cosTheta)))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\mathsf{PI}\left(\right)}\\
\mathsf{fma}\left(\left(\left(\frac{-1}{t\_0} + c\right) + 1\right) \cdot \mathsf{PI}\left(\right), -cosTheta, t\_0\right) \cdot cosTheta
\end{array}
\end{array}
Initial program 98.0%
Taylor expanded in cosTheta around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites96.4%
Applied rewrites96.4%
(FPCore (cosTheta c) :precision binary32 (let* ((t_0 (sqrt (PI)))) (* (fma (- cosTheta) (* (+ (/ -1.0 t_0) 1.0) (PI)) t_0) cosTheta)))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\mathsf{PI}\left(\right)}\\
\mathsf{fma}\left(-cosTheta, \left(\frac{-1}{t\_0} + 1\right) \cdot \mathsf{PI}\left(\right), t\_0\right) \cdot cosTheta
\end{array}
\end{array}
Initial program 98.0%
Taylor expanded in cosTheta around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites96.4%
Taylor expanded in c around 0
mul-1-negN/A
sqrt-divN/A
metadata-evalN/A
distribute-neg-fracN/A
metadata-evalN/A
lower-/.f32N/A
lift-sqrt.f32N/A
lift-PI.f3296.4
Applied rewrites96.4%
(FPCore (cosTheta c) :precision binary32 (* (sqrt (PI)) cosTheta))
\begin{array}{l}
\\
\sqrt{\mathsf{PI}\left(\right)} \cdot cosTheta
\end{array}
Initial program 98.0%
Taylor expanded in cosTheta around 0
*-commutativeN/A
lower-*.f32N/A
lift-sqrt.f32N/A
lift-PI.f3293.6
Applied rewrites93.6%
(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 98.0%
Taylor expanded in c around inf
Applied rewrites4.9%
herbie shell --seed 2025051
(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))))))