
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (log (- 1.0 u1)))) (sin (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 11 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (log (- 1.0 u1)))) (sin (* (* 2.0 (PI)) u2))))
\begin{array}{l}
\\
\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (- (log1p (- u1)))) (sin (* (PI) (+ u2 u2)))))
\begin{array}{l}
\\
\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)
\end{array}
Initial program 56.2%
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f3298.5
Applied rewrites98.5%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
*-commutativeN/A
lower-fma.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
*-commutativeN/A
lower-*.f3298.5
Applied rewrites98.5%
lift-fma.f32N/A
lift-*.f32N/A
associate-*r*N/A
lift-pow.f32N/A
pow-plusN/A
lift-cbrt.f32N/A
metadata-evalN/A
rem-cube-cbrtN/A
*-commutativeN/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3298.5
Applied rewrites98.5%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(if (<= u1 0.03999999910593033)
(*
(sqrt (* (fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 1.0) u1))
(sin (* (PI) (+ u2 u2))))
(*
(sqrt (- (log (- 1.0 u1))))
(*
(+
(fma (* (* u2 u2) -1.3333333333333333) (* (* (PI) (PI)) (PI)) (PI))
(PI))
u2))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;u1 \leq 0.03999999910593033:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \left(\left(\mathsf{fma}\left(\left(u2 \cdot u2\right) \cdot -1.3333333333333333, \left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \mathsf{PI}\left(\right), \mathsf{PI}\left(\right)\right) + \mathsf{PI}\left(\right)\right) \cdot u2\right)\\
\end{array}
\end{array}
if u1 < 0.0399999991Initial program 49.7%
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f3298.6
Applied rewrites98.6%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
*-commutativeN/A
lower-fma.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
*-commutativeN/A
lower-*.f3298.5
Applied rewrites98.5%
lift-fma.f32N/A
lift-*.f32N/A
associate-*r*N/A
lift-pow.f32N/A
pow-plusN/A
lift-cbrt.f32N/A
metadata-evalN/A
rem-cube-cbrtN/A
*-commutativeN/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3298.6
Applied rewrites98.6%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
lower-fma.f3298.6
Applied rewrites98.6%
if 0.0399999991 < u1 Initial program 96.9%
Taylor expanded in u2 around 0
*-commutativeN/A
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
Applied rewrites90.1%
Applied rewrites90.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (* (fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 1.0) u1)) (sin (* (PI) (+ u2 u2)))))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)
\end{array}
Initial program 56.2%
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f3298.5
Applied rewrites98.5%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
*-commutativeN/A
lower-fma.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
*-commutativeN/A
lower-*.f3298.5
Applied rewrites98.5%
lift-fma.f32N/A
lift-*.f32N/A
associate-*r*N/A
lift-pow.f32N/A
pow-plusN/A
lift-cbrt.f32N/A
metadata-evalN/A
rem-cube-cbrtN/A
*-commutativeN/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3298.5
Applied rewrites98.5%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
lower-fma.f3294.2
Applied rewrites94.2%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (* (fma (fma 0.3333333333333333 u1 0.5) u1 1.0) u1)) (sin (* (PI) (+ u2 u2)))))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)
\end{array}
Initial program 56.2%
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f3298.5
Applied rewrites98.5%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
*-commutativeN/A
lower-fma.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
*-commutativeN/A
lower-*.f3298.5
Applied rewrites98.5%
lift-fma.f32N/A
lift-*.f32N/A
associate-*r*N/A
lift-pow.f32N/A
pow-plusN/A
lift-cbrt.f32N/A
metadata-evalN/A
rem-cube-cbrtN/A
*-commutativeN/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3298.5
Applied rewrites98.5%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
+-commutativeN/A
lower-fma.f3292.5
Applied rewrites92.5%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (* (fma 0.5 u1 1.0) u1)) (sin (* (PI) (+ u2 u2)))))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(0.5, u1, 1\right) \cdot u1} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(u2 + u2\right)\right)
\end{array}
Initial program 56.2%
lift-log.f32N/A
lift--.f32N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
distribute-lft-neg-inN/A
*-lft-identityN/A
lower-log1p.f32N/A
lower-neg.f3298.5
Applied rewrites98.5%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
*-commutativeN/A
lower-fma.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
*-commutativeN/A
lower-*.f3298.5
Applied rewrites98.5%
lift-fma.f32N/A
lift-*.f32N/A
associate-*r*N/A
lift-pow.f32N/A
pow-plusN/A
lift-cbrt.f32N/A
metadata-evalN/A
rem-cube-cbrtN/A
*-commutativeN/A
lift-*.f32N/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-+.f3298.5
Applied rewrites98.5%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3288.6
Applied rewrites88.6%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sin (* (* (PI) u2) 2.0)) (sqrt u1)))
\begin{array}{l}
\\
\sin \left(\left(\mathsf{PI}\left(\right) \cdot u2\right) \cdot 2\right) \cdot \sqrt{u1}
\end{array}
Initial program 56.2%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
lift--.f32N/A
flip--N/A
associate-/r/N/A
flip-+N/A
lift--.f32N/A
frac-timesN/A
log-divN/A
lower--.f32N/A
lower-log.f32N/A
lower-*.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower-log.f32N/A
Applied rewrites56.1%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3277.7
Applied rewrites77.7%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (* 2.0 (pow (* u1 u1) 0.25)) (* (PI) u2)))
\begin{array}{l}
\\
\left(2 \cdot {\left(u1 \cdot u1\right)}^{0.25}\right) \cdot \left(\mathsf{PI}\left(\right) \cdot u2\right)
\end{array}
Initial program 56.2%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
lift--.f32N/A
flip--N/A
associate-/r/N/A
flip-+N/A
lift--.f32N/A
frac-timesN/A
log-divN/A
lower--.f32N/A
lower-log.f32N/A
lower-*.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower-log.f32N/A
Applied rewrites56.1%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3277.7
Applied rewrites77.7%
Taylor expanded in u2 around 0
Applied rewrites68.1%
Applied rewrites68.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (* (* (sqrt u1) u2) (PI)) 2.0))
\begin{array}{l}
\\
\left(\left(\sqrt{u1} \cdot u2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot 2
\end{array}
Initial program 56.2%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
lift--.f32N/A
flip--N/A
associate-/r/N/A
flip-+N/A
lift--.f32N/A
frac-timesN/A
log-divN/A
lower--.f32N/A
lower-log.f32N/A
lower-*.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower-log.f32N/A
Applied rewrites56.1%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3277.7
Applied rewrites77.7%
Taylor expanded in u2 around 0
Applied rewrites68.1%
Applied rewrites68.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (* (* (sqrt u1) 2.0) (PI)) u2))
\begin{array}{l}
\\
\left(\left(\sqrt{u1} \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot u2
\end{array}
Initial program 56.2%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
lift--.f32N/A
flip--N/A
associate-/r/N/A
flip-+N/A
lift--.f32N/A
frac-timesN/A
log-divN/A
lower--.f32N/A
lower-log.f32N/A
lower-*.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower-log.f32N/A
Applied rewrites56.1%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3277.7
Applied rewrites77.7%
Taylor expanded in u2 around 0
Applied rewrites68.1%
Applied rewrites68.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (* 2.0 (sqrt u1)) (* (PI) u2)))
\begin{array}{l}
\\
\left(2 \cdot \sqrt{u1}\right) \cdot \left(\mathsf{PI}\left(\right) \cdot u2\right)
\end{array}
Initial program 56.2%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
lift--.f32N/A
flip--N/A
associate-/r/N/A
flip-+N/A
lift--.f32N/A
frac-timesN/A
log-divN/A
lower--.f32N/A
lower-log.f32N/A
lower-*.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower-log.f32N/A
Applied rewrites56.1%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3277.7
Applied rewrites77.7%
Taylor expanded in u2 around 0
Applied rewrites68.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (* 2.0 u2) (sqrt u1)))
float code(float cosTheta_i, float u1, float u2) {
return (2.0f * u2) * sqrtf(u1);
}
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_i, u1, u2)
use fmin_fmax_functions
real(4), intent (in) :: costheta_i
real(4), intent (in) :: u1
real(4), intent (in) :: u2
code = (2.0e0 * u2) * sqrt(u1)
end function
function code(cosTheta_i, u1, u2) return Float32(Float32(Float32(2.0) * u2) * sqrt(u1)) end
function tmp = code(cosTheta_i, u1, u2) tmp = (single(2.0) * u2) * sqrt(u1); end
\begin{array}{l}
\\
\left(2 \cdot u2\right) \cdot \sqrt{u1}
\end{array}
Initial program 56.2%
lift-neg.f32N/A
lift-log.f32N/A
neg-logN/A
lift--.f32N/A
flip--N/A
associate-/r/N/A
flip-+N/A
lift--.f32N/A
frac-timesN/A
log-divN/A
lower--.f32N/A
lower-log.f32N/A
lower-*.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower-log.f32N/A
Applied rewrites56.1%
Taylor expanded in u1 around 0
*-commutativeN/A
lower-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-sin.f32N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f3277.7
Applied rewrites77.7%
Taylor expanded in u2 around 0
Applied rewrites68.1%
Applied rewrites25.4%
herbie shell --seed 2024359
(FPCore (cosTheta_i u1 u2)
:name "Beckmann Sample, near normal, slope_y"
:precision binary32
:pre (and (and (and (> cosTheta_i 0.9999) (<= cosTheta_i 1.0)) (and (<= 2.328306437e-10 u1) (<= u1 1.0))) (and (<= 2.328306437e-10 u2) (<= u2 1.0)))
(* (sqrt (- (log (- 1.0 u1)))) (sin (* (* 2.0 (PI)) u2))))