
(FPCore (ux uy maxCos) :precision binary32 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos)))) (* (sin (* (* 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\\
\sin \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 14 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (ux uy maxCos) :precision binary32 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos)))) (* (sin (* (* 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\\
\sin \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
(let* ((t_0 (pow (fma -1.0 maxCos 1.0) 2.0))
(t_1 (/ (fma -2.0 maxCos 2.0) ux)))
(*
(sin (* (* uy 2.0) (PI)))
(sqrt (* (/ (- (* t_1 t_1) (* t_0 t_0)) (+ t_1 t_0)) (* ux ux))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := {\left(\mathsf{fma}\left(-1, maxCos, 1\right)\right)}^{2}\\
t_1 := \frac{\mathsf{fma}\left(-2, maxCos, 2\right)}{ux}\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{t\_1 \cdot t\_1 - t\_0 \cdot t\_0}{t\_1 + t\_0} \cdot \left(ux \cdot ux\right)}
\end{array}
\end{array}
Initial program 59.1%
Taylor expanded in ux around -inf
Applied rewrites98.2%
Applied rewrites98.2%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (+ (- 1.0 ux) (* ux maxCos))))
(if (<= (* t_0 t_0) 0.9980000257492065)
(*
(* (PI) (* 2.0 uy))
(sqrt
(- 1.0 (* (+ (/ (- 1.0 (* ux ux)) (+ 1.0 ux)) (* ux maxCos)) t_0))))
(* (sin (* (* uy 2.0) (PI))) (sqrt (* (fma -2.0 maxCos 2.0) ux))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\mathbf{if}\;t\_0 \cdot t\_0 \leq 0.9980000257492065:\\
\;\;\;\;\left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right) \cdot \sqrt{1 - \left(\frac{1 - ux \cdot ux}{1 + ux} + ux \cdot maxCos\right) \cdot t\_0}\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\mathsf{fma}\left(-2, maxCos, 2\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.998000026Initial program 91.9%
Taylor expanded in uy around 0
Applied rewrites81.5%
lift--.f32N/A
flip--N/A
lower-/.f32N/A
metadata-evalN/A
pow2N/A
lower--.f32N/A
pow2N/A
lift-*.f32N/A
lower-+.f3281.8
Applied rewrites81.8%
if 0.998000026 < (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos))) Initial program 43.1%
Taylor expanded in ux around 0
Applied rewrites89.1%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sin (* (* uy 2.0) (PI)))
(sqrt
(*
ux
(+ 2.0 (fma -2.0 maxCos (* (- ux) (+ 1.0 (* maxCos (- maxCos 2.0))))))))))\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(2 + \mathsf{fma}\left(-2, maxCos, \left(-ux\right) \cdot \left(1 + maxCos \cdot \left(maxCos - 2\right)\right)\right)\right)}
\end{array}
Initial program 59.1%
Taylor expanded in ux around -inf
Applied rewrites98.2%
Applied rewrites98.2%
Taylor expanded in ux around 0
Applied rewrites98.2%
Taylor expanded in maxCos around 0
Applied rewrites98.2%
Final simplification98.2%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (* (* uy 2.0) (PI))) (sqrt (* ux (+ 2.0 (fma -1.0 ux (* maxCos (- (* 2.0 ux) 2.0))))))))
\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(2 + \mathsf{fma}\left(-1, ux, maxCos \cdot \left(2 \cdot ux - 2\right)\right)\right)}
\end{array}
Initial program 59.1%
Taylor expanded in ux around -inf
Applied rewrites98.2%
Applied rewrites98.2%
Taylor expanded in ux around 0
Applied rewrites98.2%
Taylor expanded in maxCos around 0
Applied rewrites97.5%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (* 2.0 (/ 1.0 ux))))
(if (<= maxCos 4.999999987376214e-7)
(* (sin (* (* uy 2.0) (PI))) (sqrt (* ux (+ 2.0 (- ux)))))
(*
(* 2.0 (* uy (PI)))
(sqrt
(*
(*
(* maxCos maxCos)
(- (/ (- (fma -1.0 (/ (- t_0 1.0) maxCos) t_0) 2.0) (- maxCos)) 1.0))
(* ux ux)))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := 2 \cdot \frac{1}{ux}\\
\mathbf{if}\;maxCos \leq 4.999999987376214 \cdot 10^{-7}:\\
\;\;\;\;\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(2 + \left(-ux\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{\left(\left(maxCos \cdot maxCos\right) \cdot \left(\frac{\mathsf{fma}\left(-1, \frac{t\_0 - 1}{maxCos}, t\_0\right) - 2}{-maxCos} - 1\right)\right) \cdot \left(ux \cdot ux\right)}\\
\end{array}
\end{array}
if maxCos < 4.99999999e-7Initial program 59.0%
Taylor expanded in ux around -inf
Applied rewrites98.2%
Applied rewrites98.2%
Taylor expanded in ux around 0
Applied rewrites98.2%
Taylor expanded in maxCos around 0
Applied rewrites98.1%
if 4.99999999e-7 < maxCos Initial program 59.7%
Taylor expanded in ux around -inf
Applied rewrites98.3%
Taylor expanded in maxCos around -inf
Applied rewrites98.0%
Taylor expanded in uy around 0
Applied rewrites84.3%
Final simplification95.6%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (* (* uy 2.0) (PI))) (sqrt (* ux (+ 2.0 (fma -2.0 maxCos (- ux)))))))
\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(2 + \mathsf{fma}\left(-2, maxCos, -ux\right)\right)}
\end{array}
Initial program 59.1%
Taylor expanded in ux around -inf
Applied rewrites98.2%
Applied rewrites98.2%
Taylor expanded in ux around 0
Applied rewrites98.2%
Taylor expanded in maxCos around 0
Applied rewrites96.7%
Final simplification96.7%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (* 2.0 (/ 1.0 ux))))
(if (<= maxCos 2.000000033724767e-16)
(*
(* (PI) (* 2.0 uy))
(sqrt
(-
1.0
(*
(+ (- 1.0 ux) (* ux maxCos))
(* ux (- (+ maxCos (/ 1.0 ux)) 1.0))))))
(*
(* 2.0 (* uy (PI)))
(sqrt
(*
(*
(* maxCos maxCos)
(- (/ (- (fma -1.0 (/ (- t_0 1.0) maxCos) t_0) 2.0) (- maxCos)) 1.0))
(* ux ux)))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := 2 \cdot \frac{1}{ux}\\
\mathbf{if}\;maxCos \leq 2.000000033724767 \cdot 10^{-16}:\\
\;\;\;\;\left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(ux \cdot \left(\left(maxCos + \frac{1}{ux}\right) - 1\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{\left(\left(maxCos \cdot maxCos\right) \cdot \left(\frac{\mathsf{fma}\left(-1, \frac{t\_0 - 1}{maxCos}, t\_0\right) - 2}{-maxCos} - 1\right)\right) \cdot \left(ux \cdot ux\right)}\\
\end{array}
\end{array}
if maxCos < 2.00000003e-16Initial program 60.7%
Taylor expanded in uy around 0
Applied rewrites53.9%
Taylor expanded in ux around inf
Applied rewrites54.4%
if 2.00000003e-16 < maxCos Initial program 56.8%
Taylor expanded in ux around -inf
Applied rewrites98.5%
Taylor expanded in maxCos around -inf
Applied rewrites97.3%
Taylor expanded in uy around 0
Applied rewrites80.4%
Final simplification65.1%
(FPCore (ux uy maxCos)
:precision binary32
(*
(* (PI) (* 2.0 uy))
(sqrt
(-
1.0
(* (+ (- 1.0 ux) (* ux maxCos)) (* ux (- (+ maxCos (/ 1.0 ux)) 1.0)))))))\begin{array}{l}
\\
\left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(ux \cdot \left(\left(maxCos + \frac{1}{ux}\right) - 1\right)\right)}
\end{array}
Initial program 59.1%
Taylor expanded in uy around 0
Applied rewrites52.4%
Taylor expanded in ux around inf
Applied rewrites52.7%
(FPCore (ux uy maxCos) :precision binary32 (* (* (PI) (* 2.0 uy)) (sqrt (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (+ 1.0 (* ux (- maxCos 1.0))))))))
\begin{array}{l}
\\
\left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(1 + ux \cdot \left(maxCos - 1\right)\right)}
\end{array}
Initial program 59.1%
Taylor expanded in uy around 0
Applied rewrites52.4%
Taylor expanded in ux around 0
Applied rewrites52.4%
(FPCore (ux uy maxCos) :precision binary32 (let* ((t_0 (fma maxCos ux (- 1.0 ux)))) (* (* 2.0 (* uy (PI))) (sqrt (- 1.0 (* t_0 t_0))))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(maxCos, ux, 1 - ux\right)\\
\left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}
Initial program 59.1%
lift-+.f32N/A
lift--.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lift--.f3259.1
lift-+.f32N/A
lift--.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lift--.f3259.1
Applied rewrites59.1%
Taylor expanded in uy around 0
Applied rewrites52.4%
(FPCore (ux uy maxCos) :precision binary32 (* (* (PI) (* 2.0 uy)) (sqrt (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (- 1.0 ux))))))
\begin{array}{l}
\\
\left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(1 - ux\right)}
\end{array}
Initial program 59.1%
Taylor expanded in uy around 0
Applied rewrites52.4%
Taylor expanded in maxCos around 0
Applied rewrites51.3%
(FPCore (ux uy maxCos) :precision binary32 (* (* 2.0 (* uy (PI))) (sqrt (- 1.0 (fma (- (* maxCos 2.0) 2.0) ux 1.0)))))
\begin{array}{l}
\\
\left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{1 - \mathsf{fma}\left(maxCos \cdot 2 - 2, ux, 1\right)}
\end{array}
Initial program 59.1%
Taylor expanded in ux around 0
Applied rewrites46.8%
Taylor expanded in uy around 0
Applied rewrites43.0%
(FPCore (ux uy maxCos) :precision binary32 (* (* 2.0 (* uy (PI))) (sqrt (- 1.0 (fma -2.0 ux 1.0)))))
\begin{array}{l}
\\
\left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{1 - \mathsf{fma}\left(-2, ux, 1\right)}
\end{array}
Initial program 59.1%
Taylor expanded in ux around 0
Applied rewrites46.8%
Taylor expanded in uy around 0
Applied rewrites43.0%
Taylor expanded in maxCos around 0
Applied rewrites42.6%
(FPCore (ux uy maxCos) :precision binary32 (* (* (PI) (* 2.0 uy)) (sqrt (- 1.0 1.0))))
\begin{array}{l}
\\
\left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right) \cdot \sqrt{1 - 1}
\end{array}
Initial program 59.1%
Taylor expanded in uy around 0
Applied rewrites52.4%
Taylor expanded in ux around 0
Applied rewrites7.1%
herbie shell --seed 2025025
(FPCore (ux uy maxCos)
:name "UniformSampleCone, y"
: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)))
(* (sin (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (+ (- 1.0 ux) (* ux maxCos)))))))