
(FPCore (ux uy maxCos) :precision binary32 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos)))) (* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* t_0 t_0))))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 16 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (ux uy maxCos) :precision binary32 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos)))) (* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* t_0 t_0))))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}
(FPCore (ux uy maxCos) :precision binary32 (* (sin (fma (- uy) (* (PI) 2.0) (/ (PI) 2.0))) (sqrt (* (- 2.0 (fma (pow (- maxCos 1.0) 2.0) ux (* 2.0 maxCos))) ux))))
\begin{array}{l}
\\
\sin \left(\mathsf{fma}\left(-uy, \mathsf{PI}\left(\right) \cdot 2, \frac{\mathsf{PI}\left(\right)}{2}\right)\right) \cdot \sqrt{\left(2 - \mathsf{fma}\left({\left(maxCos - 1\right)}^{2}, ux, 2 \cdot maxCos\right)\right) \cdot ux}
\end{array}
Initial program 56.3%
Taylor expanded in ux around 0
Applied rewrites99.0%
lift-cos.f32N/A
cos-neg-revN/A
sin-+PI/2-revN/A
lower-sin.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
distribute-lft-neg-inN/A
lower-fma.f32N/A
lower-neg.f32N/A
*-commutativeN/A
lower-*.f32N/A
lift-PI.f32N/A
lower-/.f3299.2
Applied rewrites99.2%
Final simplification99.2%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (fma 0.5 (PI) (* (* (PI) uy) -2.0))) (sqrt (* (- 2.0 (fma (pow (- maxCos 1.0) 2.0) ux (* 2.0 maxCos))) ux))))
\begin{array}{l}
\\
\sin \left(\mathsf{fma}\left(0.5, \mathsf{PI}\left(\right), \left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot -2\right)\right) \cdot \sqrt{\left(2 - \mathsf{fma}\left({\left(maxCos - 1\right)}^{2}, ux, 2 \cdot maxCos\right)\right) \cdot ux}
\end{array}
Initial program 56.3%
Taylor expanded in ux around 0
Applied rewrites99.0%
lift-cos.f32N/A
cos-neg-revN/A
sin-+PI/2-revN/A
lower-sin.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
distribute-lft-neg-inN/A
lower-fma.f32N/A
lower-neg.f32N/A
*-commutativeN/A
lower-*.f32N/A
lift-PI.f32N/A
lower-/.f3299.2
Applied rewrites99.2%
Taylor expanded in uy around 0
Applied rewrites99.0%
Final simplification99.0%
(FPCore (ux uy maxCos) :precision binary32 (* (cos (* (* uy 2.0) (PI))) (sqrt (* (- 2.0 (fma (pow (- maxCos 1.0) 2.0) ux (* 2.0 maxCos))) ux))))
\begin{array}{l}
\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(2 - \mathsf{fma}\left({\left(maxCos - 1\right)}^{2}, ux, 2 \cdot maxCos\right)\right) \cdot ux}
\end{array}
Initial program 56.3%
Taylor expanded in ux around 0
Applied rewrites99.0%
Final simplification99.0%
(FPCore (ux uy maxCos) :precision binary32 (* (cos (* (* uy 2.0) (PI))) (sqrt (* (- 2.0 (fma (fma ux (+ -2.0 maxCos) 2.0) maxCos ux)) ux))))
\begin{array}{l}
\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(2 - \mathsf{fma}\left(\mathsf{fma}\left(ux, -2 + maxCos, 2\right), maxCos, ux\right)\right) \cdot ux}
\end{array}
Initial program 56.3%
Taylor expanded in ux around 0
Applied rewrites99.0%
Taylor expanded in maxCos around 0
Applied rewrites98.9%
Final simplification98.9%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= uy 8.499999967170879e-5)
(*
1.0
(sqrt (* (- 2.0 (fma (pow (- maxCos 1.0) 2.0) ux (* 2.0 maxCos))) ux)))
(* (cos (* (* uy 2.0) (PI))) (sqrt (* (- 2.0 ux) ux)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;uy \leq 8.499999967170879 \cdot 10^{-5}:\\
\;\;\;\;1 \cdot \sqrt{\left(2 - \mathsf{fma}\left({\left(maxCos - 1\right)}^{2}, ux, 2 \cdot maxCos\right)\right) \cdot ux}\\
\mathbf{else}:\\
\;\;\;\;\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(2 - ux\right) \cdot ux}\\
\end{array}
\end{array}
if uy < 8.49999997e-5Initial program 55.8%
Taylor expanded in ux around 0
Applied rewrites99.5%
Taylor expanded in uy around 0
Applied rewrites99.6%
if 8.49999997e-5 < uy Initial program 57.0%
Taylor expanded in ux around 0
Applied rewrites98.1%
Taylor expanded in maxCos around 0
Applied rewrites93.2%
Final simplification97.1%
(FPCore (ux uy maxCos) :precision binary32 (* (cos (* (+ uy uy) (PI))) (sqrt (* (- 2.0 (fma (fma -2.0 ux 2.0) maxCos ux)) ux))))
\begin{array}{l}
\\
\cos \left(\left(uy + uy\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(2 - \mathsf{fma}\left(\mathsf{fma}\left(-2, ux, 2\right), maxCos, ux\right)\right) \cdot ux}
\end{array}
Initial program 56.3%
Taylor expanded in ux around 0
Applied rewrites99.0%
Taylor expanded in maxCos around 0
Applied rewrites98.3%
lift-*.f32N/A
*-commutativeN/A
count-2-revN/A
lower-+.f3298.3
Applied rewrites98.3%
Final simplification98.3%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= uy 8.499999967170879e-5)
(*
1.0
(sqrt
(*
(- (fma (- ux) (* (- maxCos 1.0) (+ -1.0 maxCos)) 2.0) (* 2.0 maxCos))
ux)))
(* (cos (* (* uy 2.0) (PI))) (sqrt (* (- 2.0 ux) ux)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;uy \leq 8.499999967170879 \cdot 10^{-5}:\\
\;\;\;\;1 \cdot \sqrt{\left(\mathsf{fma}\left(-ux, \left(maxCos - 1\right) \cdot \left(-1 + maxCos\right), 2\right) - 2 \cdot maxCos\right) \cdot ux}\\
\mathbf{else}:\\
\;\;\;\;\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(2 - ux\right) \cdot ux}\\
\end{array}
\end{array}
if uy < 8.49999997e-5Initial program 55.8%
Taylor expanded in uy around 0
Applied rewrites55.7%
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
distribute-rgt-inN/A
associate--r+N/A
lower--.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lift-+.f32N/A
lift-*.f32N/A
*-commutativeN/A
+-commutativeN/A
lift-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
Applied rewrites54.3%
Taylor expanded in ux around 0
Applied rewrites99.5%
if 8.49999997e-5 < uy Initial program 57.0%
Taylor expanded in ux around 0
Applied rewrites98.1%
Taylor expanded in maxCos around 0
Applied rewrites93.2%
Final simplification97.0%
(FPCore (ux uy maxCos) :precision binary32 (* (cos (* (* uy 2.0) (PI))) (sqrt (* (- 2.0 (fma 2.0 maxCos ux)) ux))))
\begin{array}{l}
\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(2 - \mathsf{fma}\left(2, maxCos, ux\right)\right) \cdot ux}
\end{array}
Initial program 56.3%
Taylor expanded in ux around 0
Applied rewrites99.0%
Taylor expanded in maxCos around 0
Applied rewrites98.3%
Taylor expanded in ux around 0
Applied rewrites97.4%
Final simplification97.4%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (fma maxCos ux (- 1.0 ux))) (t_1 (+ (- 1.0 ux) (* ux maxCos))))
(if (<= (* t_1 t_1) 0.9995499849319458)
(sqrt (fma (- ux (fma maxCos ux 1.0)) t_0 1.0))
(* 1.0 (sqrt (- (* (- 2.0 maxCos) ux) (* (* t_0 maxCos) ux)))))))
float code(float ux, float uy, float maxCos) {
float t_0 = fmaf(maxCos, ux, (1.0f - ux));
float t_1 = (1.0f - ux) + (ux * maxCos);
float tmp;
if ((t_1 * t_1) <= 0.9995499849319458f) {
tmp = sqrtf(fmaf((ux - fmaf(maxCos, ux, 1.0f)), t_0, 1.0f));
} else {
tmp = 1.0f * sqrtf((((2.0f - maxCos) * ux) - ((t_0 * maxCos) * ux)));
}
return tmp;
}
function code(ux, uy, maxCos) t_0 = fma(maxCos, ux, Float32(Float32(1.0) - ux)) t_1 = Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos)) tmp = Float32(0.0) if (Float32(t_1 * t_1) <= Float32(0.9995499849319458)) tmp = sqrt(fma(Float32(ux - fma(maxCos, ux, Float32(1.0))), t_0, Float32(1.0))); else tmp = Float32(Float32(1.0) * sqrt(Float32(Float32(Float32(Float32(2.0) - maxCos) * ux) - Float32(Float32(t_0 * maxCos) * ux)))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(maxCos, ux, 1 - ux\right)\\
t_1 := \left(1 - ux\right) + ux \cdot maxCos\\
\mathbf{if}\;t\_1 \cdot t\_1 \leq 0.9995499849319458:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(ux - \mathsf{fma}\left(maxCos, ux, 1\right), t\_0, 1\right)}\\
\mathbf{else}:\\
\;\;\;\;1 \cdot \sqrt{\left(2 - maxCos\right) \cdot ux - \left(t\_0 \cdot maxCos\right) \cdot ux}\\
\end{array}
\end{array}
if (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos))) < 0.999549985Initial program 88.2%
lift--.f32N/A
lift-*.f32N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-neg.f3288.6
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f3288.6
Applied rewrites88.6%
Taylor expanded in uy around 0
Applied rewrites74.0%
if 0.999549985 < (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos))) Initial program 38.9%
Taylor expanded in uy around 0
Applied rewrites33.2%
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
distribute-rgt-inN/A
associate--r+N/A
lower--.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lift-+.f32N/A
lift-*.f32N/A
*-commutativeN/A
+-commutativeN/A
lift-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
Applied rewrites31.1%
Taylor expanded in ux around 0
Applied rewrites74.0%
(FPCore (ux uy maxCos)
:precision binary32
(*
1.0
(sqrt
(*
(- (fma (- ux) (* (- maxCos 1.0) (+ -1.0 maxCos)) 2.0) (* 2.0 maxCos))
ux))))
float code(float ux, float uy, float maxCos) {
return 1.0f * sqrtf(((fmaf(-ux, ((maxCos - 1.0f) * (-1.0f + maxCos)), 2.0f) - (2.0f * maxCos)) * ux));
}
function code(ux, uy, maxCos) return Float32(Float32(1.0) * sqrt(Float32(Float32(fma(Float32(-ux), Float32(Float32(maxCos - Float32(1.0)) * Float32(Float32(-1.0) + maxCos)), Float32(2.0)) - Float32(Float32(2.0) * maxCos)) * ux))) end
\begin{array}{l}
\\
1 \cdot \sqrt{\left(\mathsf{fma}\left(-ux, \left(maxCos - 1\right) \cdot \left(-1 + maxCos\right), 2\right) - 2 \cdot maxCos\right) \cdot ux}
\end{array}
Initial program 56.3%
Taylor expanded in uy around 0
Applied rewrites47.5%
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
distribute-rgt-inN/A
associate--r+N/A
lower--.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lift-+.f32N/A
lift-*.f32N/A
*-commutativeN/A
+-commutativeN/A
lift-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
Applied rewrites46.2%
Taylor expanded in ux around 0
Applied rewrites79.4%
(FPCore (ux uy maxCos) :precision binary32 (* 1.0 (sqrt (- 1.0 (* (- (fma maxCos ux 1.0) ux) (+ (- 1.0 ux) (* ux maxCos)))))))
float code(float ux, float uy, float maxCos) {
return 1.0f * sqrtf((1.0f - ((fmaf(maxCos, ux, 1.0f) - ux) * ((1.0f - ux) + (ux * maxCos)))));
}
function code(ux, uy, maxCos) return Float32(Float32(1.0) * sqrt(Float32(Float32(1.0) - Float32(Float32(fma(maxCos, ux, Float32(1.0)) - ux) * Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos)))))) end
\begin{array}{l}
\\
1 \cdot \sqrt{1 - \left(\mathsf{fma}\left(maxCos, ux, 1\right) - ux\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)}
\end{array}
Initial program 56.3%
Taylor expanded in uy around 0
Applied rewrites47.5%
lift-+.f32N/A
lift-*.f32N/A
*-commutativeN/A
+-commutativeN/A
lift--.f32N/A
associate-+r-N/A
lower--.f32N/A
lower-fma.f3247.5
Applied rewrites47.5%
(FPCore (ux uy maxCos) :precision binary32 (let* ((t_0 (fma maxCos ux (- 1.0 ux)))) (* 1.0 (sqrt (- 1.0 (* t_0 t_0))))))
float code(float ux, float uy, float maxCos) {
float t_0 = fmaf(maxCos, ux, (1.0f - ux));
return 1.0f * sqrtf((1.0f - (t_0 * t_0)));
}
function code(ux, uy, maxCos) t_0 = fma(maxCos, ux, Float32(Float32(1.0) - ux)) return Float32(Float32(1.0) * sqrt(Float32(Float32(1.0) - Float32(t_0 * t_0)))) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(maxCos, ux, 1 - ux\right)\\
1 \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}
Initial program 56.3%
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f3256.3
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f3256.3
Applied rewrites56.3%
Taylor expanded in uy around 0
Applied rewrites47.5%
(FPCore (ux uy maxCos) :precision binary32 (sqrt (fma (- ux (fma maxCos ux 1.0)) (fma maxCos ux (- 1.0 ux)) 1.0)))
float code(float ux, float uy, float maxCos) {
return sqrtf(fmaf((ux - fmaf(maxCos, ux, 1.0f)), fmaf(maxCos, ux, (1.0f - ux)), 1.0f));
}
function code(ux, uy, maxCos) return sqrt(fma(Float32(ux - fma(maxCos, ux, Float32(1.0))), fma(maxCos, ux, Float32(Float32(1.0) - ux)), Float32(1.0))) end
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(ux - \mathsf{fma}\left(maxCos, ux, 1\right), \mathsf{fma}\left(maxCos, ux, 1 - ux\right), 1\right)}
\end{array}
Initial program 56.3%
lift--.f32N/A
lift-*.f32N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-neg.f3256.4
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f3256.4
Applied rewrites56.4%
Taylor expanded in uy around 0
Applied rewrites47.5%
(FPCore (ux uy maxCos) :precision binary32 (* 1.0 (sqrt (- 1.0 (fma (fma 2.0 maxCos -2.0) ux 1.0)))))
float code(float ux, float uy, float maxCos) {
return 1.0f * sqrtf((1.0f - fmaf(fmaf(2.0f, maxCos, -2.0f), ux, 1.0f)));
}
function code(ux, uy, maxCos) return Float32(Float32(1.0) * sqrt(Float32(Float32(1.0) - fma(fma(Float32(2.0), maxCos, Float32(-2.0)), ux, Float32(1.0))))) end
\begin{array}{l}
\\
1 \cdot \sqrt{1 - \mathsf{fma}\left(\mathsf{fma}\left(2, maxCos, -2\right), ux, 1\right)}
\end{array}
Initial program 56.3%
Taylor expanded in uy around 0
Applied rewrites47.5%
Taylor expanded in ux around 0
Applied rewrites39.6%
(FPCore (ux uy maxCos) :precision binary32 (* 1.0 (sqrt (- 1.0 (fma -2.0 ux 1.0)))))
float code(float ux, float uy, float maxCos) {
return 1.0f * sqrtf((1.0f - fmaf(-2.0f, ux, 1.0f)));
}
function code(ux, uy, maxCos) return Float32(Float32(1.0) * sqrt(Float32(Float32(1.0) - fma(Float32(-2.0), ux, Float32(1.0))))) end
\begin{array}{l}
\\
1 \cdot \sqrt{1 - \mathsf{fma}\left(-2, ux, 1\right)}
\end{array}
Initial program 56.3%
Taylor expanded in uy around 0
Applied rewrites47.5%
Taylor expanded in ux around 0
Applied rewrites39.6%
Taylor expanded in maxCos around 0
Applied rewrites39.0%
(FPCore (ux uy maxCos) :precision binary32 (* 1.0 (sqrt (- 1.0 1.0))))
float code(float ux, float uy, float maxCos) {
return 1.0f * sqrtf((1.0f - 1.0f));
}
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(ux, uy, maxcos)
use fmin_fmax_functions
real(4), intent (in) :: ux
real(4), intent (in) :: uy
real(4), intent (in) :: maxcos
code = 1.0e0 * sqrt((1.0e0 - 1.0e0))
end function
function code(ux, uy, maxCos) return Float32(Float32(1.0) * sqrt(Float32(Float32(1.0) - Float32(1.0)))) end
function tmp = code(ux, uy, maxCos) tmp = single(1.0) * sqrt((single(1.0) - single(1.0))); end
\begin{array}{l}
\\
1 \cdot \sqrt{1 - 1}
\end{array}
Initial program 56.3%
Taylor expanded in uy around 0
Applied rewrites47.5%
Taylor expanded in ux around 0
Applied rewrites6.6%
herbie shell --seed 2025018
(FPCore (ux uy maxCos)
:name "UniformSampleCone, x"
:precision binary32
:pre (and (and (and (<= 2.328306437e-10 ux) (<= ux 1.0)) (and (<= 2.328306437e-10 uy) (<= uy 1.0))) (and (<= 0.0 maxCos) (<= maxCos 1.0)))
(* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (+ (- 1.0 ux) (* ux maxCos)))))))