
(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 11 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 (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(\left(2 - {\left(maxCos - 1\right)}^{2} \cdot ux\right) - maxCos \cdot 2\right) \cdot ux} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 60.3%
lift-+.f32N/A
lift--.f32N/A
flip--N/A
metadata-evalN/A
div-subN/A
associate-+l-N/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower--.f32N/A
lower-/.f32N/A
lower-*.f32N/A
+-commutativeN/A
lower-+.f3260.4
lift-*.f32N/A
*-commutativeN/A
lower-*.f3260.4
Applied rewrites60.4%
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-*.f3298.5
Applied rewrites98.5%
Final simplification98.5%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (- (* (- 2.0 ux) ux) (* (* (fma -2.0 ux 2.0) ux) maxCos))) (sin (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(2 - ux\right) \cdot ux - \left(\mathsf{fma}\left(-2, ux, 2\right) \cdot ux\right) \cdot maxCos} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 60.3%
lift-+.f32N/A
lift--.f32N/A
flip--N/A
metadata-evalN/A
div-subN/A
associate-+l-N/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower--.f32N/A
lower-/.f32N/A
lower-*.f32N/A
+-commutativeN/A
lower-+.f3260.4
lift-*.f32N/A
*-commutativeN/A
lower-*.f3260.4
Applied rewrites60.4%
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-*.f3298.5
Applied rewrites98.5%
Taylor expanded in maxCos around 0
Applied rewrites86.6%
Final simplification87.3%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (- (- 2.0 (* (fma -2.0 ux 2.0) maxCos)) ux) ux)) (sin (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(\left(2 - \mathsf{fma}\left(-2, ux, 2\right) \cdot maxCos\right) - ux\right) \cdot ux} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 60.3%
lift-+.f32N/A
lift--.f32N/A
flip--N/A
metadata-evalN/A
div-subN/A
associate-+l-N/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower--.f32N/A
lower-/.f32N/A
lower-*.f32N/A
+-commutativeN/A
lower-+.f3260.4
lift-*.f32N/A
*-commutativeN/A
lower-*.f3260.4
Applied rewrites60.4%
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-*.f3298.5
Applied rewrites98.5%
Taylor expanded in maxCos around 0
Applied rewrites86.5%
Final simplification86.6%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (- 2.0 ux) ux)) (sin (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(2 - ux\right) \cdot ux} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 60.3%
lift-+.f32N/A
lift--.f32N/A
flip--N/A
metadata-evalN/A
div-subN/A
associate-+l-N/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower--.f32N/A
lower-/.f32N/A
lower-*.f32N/A
+-commutativeN/A
lower-+.f3260.4
lift-*.f32N/A
*-commutativeN/A
lower-*.f3260.4
Applied rewrites60.4%
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-*.f3298.5
Applied rewrites98.5%
Taylor expanded in maxCos around 0
Applied rewrites93.3%
Final simplification93.3%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (+ (* maxCos ux) (- 1.0 ux))))
(if (<= (- 1.0 (* t_0 t_0)) 0.0003100000030826777)
(* (* 2.0 (PI)) (* (sqrt (* (fma -2.0 maxCos 2.0) ux)) uy))
(*
(sqrt (- 1.0 (* (* (- (+ (/ 1.0 ux) maxCos) 1.0) ux) t_0)))
(* (* (PI) uy) 2.0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := maxCos \cdot ux + \left(1 - ux\right)\\
\mathbf{if}\;1 - t\_0 \cdot t\_0 \leq 0.0003100000030826777:\\
\;\;\;\;\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot \left(\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux} \cdot uy\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{1 - \left(\left(\left(\frac{1}{ux} + maxCos\right) - 1\right) \cdot ux\right) \cdot t\_0} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)))) < 3.10000003e-4Initial program 37.2%
Taylor expanded in uy around 0
distribute-rgt-inN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
associate-*l*N/A
Applied rewrites33.3%
Taylor expanded in uy around 0
Applied rewrites33.3%
Taylor expanded in ux around 0
Applied rewrites71.2%
if 3.10000003e-4 < (-.f32 #s(literal 1 binary32) (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)))) Initial program 89.0%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f3289.6
Applied rewrites89.6%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3279.7
Applied rewrites79.7%
Final simplification75.2%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (+ (* maxCos ux) (- 1.0 ux))))
(if (<= (- 1.0 (* t_0 t_0)) 0.00031800000579096377)
(* (* 2.0 (PI)) (* (sqrt (* (fma -2.0 maxCos 2.0) ux)) uy))
(*
(sqrt (- 1.0 (* (+ (* (- maxCos 1.0) ux) 1.0) t_0)))
(* (* (PI) 2.0) uy)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := maxCos \cdot ux + \left(1 - ux\right)\\
\mathbf{if}\;1 - t\_0 \cdot t\_0 \leq 0.00031800000579096377:\\
\;\;\;\;\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot \left(\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux} \cdot uy\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{1 - \left(\left(maxCos - 1\right) \cdot ux + 1\right) \cdot t\_0} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)))) < 3.18000006e-4Initial program 37.5%
Taylor expanded in uy around 0
distribute-rgt-inN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
associate-*l*N/A
Applied rewrites33.5%
Taylor expanded in uy around 0
Applied rewrites33.5%
Taylor expanded in ux around 0
Applied rewrites72.0%
if 3.18000006e-4 < (-.f32 #s(literal 1 binary32) (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)))) Initial program 89.2%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3279.2
Applied rewrites79.2%
lift-+.f32N/A
lift--.f32N/A
sub-negN/A
neg-mul-1N/A
*-commutativeN/A
associate-+r+N/A
lift-*.f32N/A
distribute-lft-inN/A
lift-+.f32N/A
lift-*.f32N/A
+-commutativeN/A
lower-+.f3279.4
lift-*.f32N/A
*-commutativeN/A
lift-+.f32N/A
+-commutativeN/A
metadata-evalN/A
sub-negN/A
lower-*.f32N/A
lower--.f3279.4
Applied rewrites79.4%
Final simplification75.3%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (+ (* maxCos ux) (- 1.0 ux))))
(if (<= (- 1.0 (* t_0 t_0)) 0.00031800000579096377)
(* (* 2.0 (PI)) (* (sqrt (* (fma -2.0 maxCos 2.0) ux)) uy))
(* (sqrt (- 1.0 (* (- 1.0 ux) t_0))) (* (* (PI) 2.0) uy)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := maxCos \cdot ux + \left(1 - ux\right)\\
\mathbf{if}\;1 - t\_0 \cdot t\_0 \leq 0.00031800000579096377:\\
\;\;\;\;\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot \left(\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux} \cdot uy\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{1 - \left(1 - ux\right) \cdot t\_0} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)))) < 3.18000006e-4Initial program 37.5%
Taylor expanded in uy around 0
distribute-rgt-inN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
associate-*l*N/A
Applied rewrites33.5%
Taylor expanded in uy around 0
Applied rewrites33.6%
Taylor expanded in ux around 0
Applied rewrites71.3%
if 3.18000006e-4 < (-.f32 #s(literal 1 binary32) (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)))) Initial program 89.2%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3279.2
Applied rewrites79.2%
Taylor expanded in maxCos around 0
lower--.f3276.2
Applied rewrites76.2%
Final simplification73.8%
(FPCore (ux uy maxCos) :precision binary32 (* (* 2.0 (PI)) (* (sqrt (* (fma -2.0 maxCos 2.0) ux)) uy)))
\begin{array}{l}
\\
\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot \left(\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux} \cdot uy\right)
\end{array}
Initial program 60.3%
Taylor expanded in uy around 0
distribute-rgt-inN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
associate-*l*N/A
Applied rewrites52.0%
Taylor expanded in uy around 0
Applied rewrites52.2%
Taylor expanded in ux around 0
Applied rewrites61.6%
Final simplification62.0%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (fma -1.0 1.0 1.0)) (fma uy (PI) (* (PI) uy))))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(-1, 1, 1\right)} \cdot \mathsf{fma}\left(uy, \mathsf{PI}\left(\right), \mathsf{PI}\left(\right) \cdot uy\right)
\end{array}
Initial program 60.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3253.7
Applied rewrites53.7%
Taylor expanded in ux around 0
Applied rewrites7.1%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
neg-mul-1N/A
lower-fma.f3219.5
Applied rewrites20.3%
Applied rewrites21.4%
Final simplification21.4%
(FPCore (ux uy maxCos) :precision binary32 (* (* (+ uy uy) (PI)) (sqrt (fma -1.0 1.0 1.0))))
\begin{array}{l}
\\
\left(\left(uy + uy\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\mathsf{fma}\left(-1, 1, 1\right)}
\end{array}
Initial program 60.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3253.7
Applied rewrites53.7%
Taylor expanded in ux around 0
Applied rewrites7.1%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
neg-mul-1N/A
lower-fma.f3219.5
Applied rewrites20.4%
Applied rewrites20.4%
Final simplification20.4%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (- 1.0 1.0)) (* (* (PI) 2.0) uy)))
\begin{array}{l}
\\
\sqrt{1 - 1} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)
\end{array}
Initial program 60.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3253.7
Applied rewrites53.7%
Taylor expanded in ux around 0
Applied rewrites7.1%
Final simplification7.1%
herbie shell --seed 2024268
(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)))))))