
(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 8 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 (* (sqrt (* (- (- 2.0 (* (pow (- maxCos 1.0) 2.0) ux)) (* maxCos 2.0)) ux)) (sin (* 2.0 (* uy (PI))))))
\begin{array}{l}
\\
\sqrt{\left(\left(2 - {\left(maxCos - 1\right)}^{2} \cdot ux\right) - maxCos \cdot 2\right) \cdot ux} \cdot \sin \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right)
\end{array}
Initial program 58.8%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f3259.6
Applied rewrites59.6%
lift-*.f32N/A
rem-square-sqrtN/A
lift-sqrt.f32N/A
lift-sqrt.f32N/A
associate-*r*N/A
lift-*.f32N/A
associate-*r*N/A
lift-*.f32N/A
lift-*.f32N/A
lower-*.f3259.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f3259.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f3259.5
Applied rewrites59.5%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3297.6
Applied rewrites97.6%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*r*N/A
lift-*.f32N/A
associate-*l*N/A
lift-sqrt.f32N/A
lift-sqrt.f32N/A
rem-square-sqrtN/A
associate-*l*N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f3298.5
Applied rewrites98.5%
Final simplification98.5%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= (* 2.0 uy) 0.0006500000017695129)
(*
(sqrt (* (- (- 2.0 (* (pow (- maxCos 1.0) 2.0) ux)) (* maxCos 2.0)) ux))
(* (* 2.0 (PI)) uy))
(* (sqrt (+ (* (- 1.0 ux) ux) ux)) (sin (* 2.0 (* uy (PI)))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;2 \cdot uy \leq 0.0006500000017695129:\\
\;\;\;\;\sqrt{\left(\left(2 - {\left(maxCos - 1\right)}^{2} \cdot ux\right) - maxCos \cdot 2\right) \cdot ux} \cdot \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(1 - ux\right) \cdot ux + ux} \cdot \sin \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right)\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 6.50000002e-4Initial program 59.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3259.3
Applied rewrites59.3%
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
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites37.3%
Taylor expanded in ux around 0
*-commutativeN/A
associate-*r*N/A
+-commutativeN/A
*-commutativeN/A
*-commutativeN/A
distribute-lft-inN/A
metadata-evalN/A
sub-negN/A
unpow2N/A
associate-*r*N/A
lower-*.f32N/A
Applied rewrites98.3%
if 6.50000002e-4 < (*.f32 uy #s(literal 2 binary32)) Initial program 57.9%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3240.6
Applied rewrites40.6%
lift-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift--.f32N/A
sub-negN/A
lift-neg.f32N/A
associate-+r+N/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
+-commutativeN/A
lift-+.f32N/A
distribute-rgt-inN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites18.9%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-*.f32N/A
lower-sqrt.f32N/A
associate-*r*N/A
mul-1-negN/A
cancel-sign-subN/A
lower-+.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3292.5
Applied rewrites92.5%
Final simplification96.1%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= (* 2.0 uy) 0.0006500000017695129)
(*
(sqrt
(-
(* (* ux ux) (- (/ 2.0 ux) (- (/ maxCos ux) (- maxCos 1.0))))
(* (* (fma maxCos ux (- 1.0 ux)) maxCos) ux)))
(* (* 2.0 (PI)) uy))
(* (sqrt (+ (* (- 1.0 ux) ux) ux)) (sin (* 2.0 (* uy (PI)))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;2 \cdot uy \leq 0.0006500000017695129:\\
\;\;\;\;\sqrt{\left(ux \cdot ux\right) \cdot \left(\frac{2}{ux} - \left(\frac{maxCos}{ux} - \left(maxCos - 1\right)\right)\right) - \left(\mathsf{fma}\left(maxCos, ux, 1 - ux\right) \cdot maxCos\right) \cdot ux} \cdot \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(1 - ux\right) \cdot ux + ux} \cdot \sin \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right)\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 6.50000002e-4Initial program 59.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3259.3
Applied rewrites59.3%
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
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites35.5%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f32N/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
lower-/.f32N/A
lower--.f32N/A
unpow2N/A
lower-*.f3297.6
Applied rewrites97.6%
if 6.50000002e-4 < (*.f32 uy #s(literal 2 binary32)) Initial program 57.9%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3240.6
Applied rewrites40.6%
lift-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift--.f32N/A
sub-negN/A
lift-neg.f32N/A
associate-+r+N/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
+-commutativeN/A
lift-+.f32N/A
distribute-rgt-inN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites18.9%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-*.f32N/A
lower-sqrt.f32N/A
associate-*r*N/A
mul-1-negN/A
cancel-sign-subN/A
lower-+.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3292.5
Applied rewrites92.5%
Final simplification89.5%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* 2.0 (PI)) uy)))
(if (<= (* 2.0 uy) 0.03999999910593033)
(*
(sqrt
(-
(* (* ux ux) (- (/ 2.0 ux) (- (/ maxCos ux) (- maxCos 1.0))))
(* (* (fma maxCos ux (- 1.0 ux)) maxCos) ux)))
t_0)
(* (sqrt (* (fma -2.0 maxCos 2.0) ux)) (sin t_0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot uy\\
\mathbf{if}\;2 \cdot uy \leq 0.03999999910593033:\\
\;\;\;\;\sqrt{\left(ux \cdot ux\right) \cdot \left(\frac{2}{ux} - \left(\frac{maxCos}{ux} - \left(maxCos - 1\right)\right)\right) - \left(\mathsf{fma}\left(maxCos, ux, 1 - ux\right) \cdot maxCos\right) \cdot ux} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux} \cdot \sin t\_0\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 0.0399999991Initial program 58.9%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3256.9
Applied rewrites56.9%
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
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites29.2%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f32N/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
lower-/.f32N/A
lower--.f32N/A
unpow2N/A
lower-*.f3290.5
Applied rewrites89.9%
if 0.0399999991 < (*.f32 uy #s(literal 2 binary32)) Initial program 58.3%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f3259.3
Applied rewrites59.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
associate-*r*N/A
lower-sin.f32N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f32N/A
cancel-sign-sub-invN/A
metadata-evalN/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f327.0
Applied rewrites7.0%
Final simplification77.1%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sqrt
(-
(* (* ux ux) (- (/ 2.0 ux) (- (/ maxCos ux) (- maxCos 1.0))))
(* (* (fma maxCos ux (- 1.0 ux)) maxCos) ux)))
(* (* 2.0 (PI)) uy)))\begin{array}{l}
\\
\sqrt{\left(ux \cdot ux\right) \cdot \left(\frac{2}{ux} - \left(\frac{maxCos}{ux} - \left(maxCos - 1\right)\right)\right) - \left(\mathsf{fma}\left(maxCos, ux, 1 - ux\right) \cdot maxCos\right) \cdot ux} \cdot \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right)
\end{array}
Initial program 58.8%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3252.2
Applied rewrites52.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
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites30.6%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f32N/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
lower-/.f32N/A
lower--.f32N/A
unpow2N/A
lower-*.f3281.2
Applied rewrites81.0%
Final simplification81.2%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sqrt
(*
(-
(/ 2.0 ux)
(- (fma (/ maxCos ux) 2.0 (* (- maxCos 1.0) maxCos)) (- maxCos 1.0)))
(* ux ux)))
(* (* 2.0 (PI)) uy)))\begin{array}{l}
\\
\sqrt{\left(\frac{2}{ux} - \left(\mathsf{fma}\left(\frac{maxCos}{ux}, 2, \left(maxCos - 1\right) \cdot maxCos\right) - \left(maxCos - 1\right)\right)\right) \cdot \left(ux \cdot ux\right)} \cdot \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right)
\end{array}
Initial program 58.8%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3252.2
Applied rewrites52.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
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites28.3%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites77.5%
Final simplification77.5%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (+ (* (- 1.0 ux) ux) ux)) (* (* 2.0 (PI)) uy)))
\begin{array}{l}
\\
\sqrt{\left(1 - ux\right) \cdot ux + ux} \cdot \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right)
\end{array}
Initial program 58.8%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3252.2
Applied rewrites52.2%
lift-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift--.f32N/A
sub-negN/A
lift-neg.f32N/A
associate-+r+N/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
+-commutativeN/A
lift-+.f32N/A
distribute-rgt-inN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites20.1%
Taylor expanded in maxCos around 0
associate-*r*N/A
mul-1-negN/A
cancel-sign-subN/A
lower-+.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3277.6
Applied rewrites77.6%
Final simplification77.6%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (- 1.0 1.0)) (* (* 2.0 (PI)) uy)))
\begin{array}{l}
\\
\sqrt{1 - 1} \cdot \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right)
\end{array}
Initial program 58.8%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3252.2
Applied rewrites52.2%
lift-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift--.f32N/A
sub-negN/A
lift-neg.f32N/A
associate-+r+N/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
+-commutativeN/A
lift-+.f32N/A
distribute-rgt-inN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites20.1%
Taylor expanded in ux around 0
Applied rewrites7.1%
Final simplification7.1%
herbie shell --seed 2024295
(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)))))))