
(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 (* (sin (* (* uy 2.0) (PI))) (sqrt (* (+ (fma (- (fma (- maxCos) ux (* 2.0 ux)) 2.0) maxCos (- ux)) 2.0) ux))))
\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(\mathsf{fma}\left(\mathsf{fma}\left(-maxCos, ux, 2 \cdot ux\right) - 2, maxCos, -ux\right) + 2\right) \cdot ux}
\end{array}
Initial program 59.9%
lift-+.f32N/A
+-commutativeN/A
lift--.f32N/A
associate-+r-N/A
flip--N/A
lower-/.f32N/A
lower--.f32N/A
lower-*.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
lower-+.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f3259.2
Applied rewrites59.2%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.6%
Taylor expanded in maxCos around 0
Applied rewrites98.6%
Final simplification98.6%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (fma (- (* (- 2.0 maxCos) ux) 2.0) maxCos (+ (- ux) 2.0)) ux)) (sin (* (PI) (* uy 2.0)))))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(\left(2 - maxCos\right) \cdot ux - 2, maxCos, \left(-ux\right) + 2\right) \cdot ux} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(uy \cdot 2\right)\right)
\end{array}
Initial program 59.9%
lift-+.f32N/A
+-commutativeN/A
lift--.f32N/A
associate-+r-N/A
flip--N/A
lower-/.f32N/A
lower--.f32N/A
lower-*.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
lower-+.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f3259.2
Applied rewrites59.2%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.6%
Taylor expanded in maxCos around 0
Applied rewrites98.6%
lift-*.f32N/A
*-commutativeN/A
lower-*.f3298.6
Applied rewrites98.6%
Final simplification98.6%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (* (+ uy uy) (PI))) (sqrt (* (+ (fma (- (* 2.0 ux) 2.0) maxCos (- ux)) 2.0) ux))))
\begin{array}{l}
\\
\sin \left(\left(uy + uy\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(\mathsf{fma}\left(2 \cdot ux - 2, maxCos, -ux\right) + 2\right) \cdot ux}
\end{array}
Initial program 59.9%
lift-+.f32N/A
+-commutativeN/A
lift--.f32N/A
associate-+r-N/A
flip--N/A
lower-/.f32N/A
lower--.f32N/A
lower-*.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
lower-+.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f3259.2
Applied rewrites59.2%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.6%
Taylor expanded in maxCos around 0
Applied rewrites98.4%
lift-*.f32N/A
*-commutativeN/A
count-2-revN/A
lower-+.f3298.4
Applied rewrites98.4%
Final simplification98.4%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (* (* uy 2.0) (PI))) (sqrt (fma (* -2.0 ux) maxCos (* (- 2.0 ux) ux)))))
\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\mathsf{fma}\left(-2 \cdot ux, maxCos, \left(2 - ux\right) \cdot ux\right)}
\end{array}
Initial program 59.9%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
fp-cancel-sub-sign-invN/A
metadata-evalN/A
+-commutativeN/A
lower-fma.f32N/A
associate-*r*N/A
mul-1-negN/A
fp-cancel-sub-signN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f3298.6
Applied rewrites98.6%
Taylor expanded in maxCos around 0
Applied rewrites98.5%
Taylor expanded in ux around 0
Applied rewrites97.8%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (* (* uy 2.0) (PI))) (sqrt (* (- 2.0 ux) ux))))
\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(2 - ux\right) \cdot ux}
\end{array}
Initial program 59.9%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
fp-cancel-sub-sign-invN/A
metadata-evalN/A
+-commutativeN/A
lower-fma.f32N/A
associate-*r*N/A
mul-1-negN/A
fp-cancel-sub-signN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f3298.6
Applied rewrites98.6%
Taylor expanded in maxCos around 0
Applied rewrites93.5%
(FPCore (ux uy maxCos) :precision binary32 (* (* (* (PI) uy) 2.0) (sqrt (* (+ (fma (- (fma (- maxCos) ux (* 2.0 ux)) 2.0) maxCos (- ux)) 2.0) ux))))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{\left(\mathsf{fma}\left(\mathsf{fma}\left(-maxCos, ux, 2 \cdot ux\right) - 2, maxCos, -ux\right) + 2\right) \cdot ux}
\end{array}
Initial program 59.9%
lift-+.f32N/A
+-commutativeN/A
lift--.f32N/A
associate-+r-N/A
flip--N/A
lower-/.f32N/A
lower--.f32N/A
lower-*.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
lower-+.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f3259.2
Applied rewrites59.2%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.6%
Taylor expanded in maxCos around 0
Applied rewrites98.6%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3282.5
Applied rewrites82.5%
Final simplification82.5%
(FPCore (ux uy maxCos)
:precision binary32
(*
(* (* (PI) 2.0) uy)
(sqrt
(*
(- (- 2.0 (* (* (- maxCos 1.0) (+ -1.0 maxCos)) ux)) (* 2.0 maxCos))
ux))))\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right) \cdot \sqrt{\left(\left(2 - \left(\left(maxCos - 1\right) \cdot \left(-1 + maxCos\right)\right) \cdot ux\right) - 2 \cdot maxCos\right) \cdot ux}
\end{array}
Initial program 59.9%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3253.5
Applied rewrites53.5%
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
lift--.f32N/A
lift-*.f32N/A
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
lift--.f32N/A
lift-*.f32N/A
distribute-rgt-inN/A
associate--r+N/A
lower--.f32N/A
Applied rewrites52.2%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites82.5%
Final simplification82.5%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= ux 0.00011000000085914508)
(* (* (+ (PI) (PI)) uy) (sqrt (* (fma -2.0 maxCos 2.0) ux)))
(*
(* (* (PI) uy) 2.0)
(sqrt (fma (- ux (fma maxCos ux 1.0)) (- (fma maxCos ux 1.0) ux) 1.0)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;ux \leq 0.00011000000085914508:\\
\;\;\;\;\left(\left(\mathsf{PI}\left(\right) + \mathsf{PI}\left(\right)\right) \cdot uy\right) \cdot \sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux}\\
\mathbf{else}:\\
\;\;\;\;\left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{\mathsf{fma}\left(ux - \mathsf{fma}\left(maxCos, ux, 1\right), \mathsf{fma}\left(maxCos, ux, 1\right) - ux, 1\right)}\\
\end{array}
\end{array}
if ux < 1.10000001e-4Initial program 38.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3236.2
Applied rewrites36.2%
Taylor expanded in ux around 0
metadata-evalN/A
fp-cancel-sign-sub-invN/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3278.2
Applied rewrites78.2%
Applied rewrites78.2%
if 1.10000001e-4 < ux Initial program 90.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.f3290.2
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f3290.2
Applied rewrites90.2%
Taylor expanded in uy around 0
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f32N/A
+-commutativeN/A
lower-fma.f32N/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f32N/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f3277.5
Applied rewrites77.5%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= ux 0.00011000000085914508)
(* (* (+ (PI) (PI)) uy) (sqrt (* (fma -2.0 maxCos 2.0) ux)))
(*
(* (* (PI) 2.0) uy)
(sqrt (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (- 1.0 ux)))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;ux \leq 0.00011000000085914508:\\
\;\;\;\;\left(\left(\mathsf{PI}\left(\right) + \mathsf{PI}\left(\right)\right) \cdot uy\right) \cdot \sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux}\\
\mathbf{else}:\\
\;\;\;\;\left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(1 - ux\right)}\\
\end{array}
\end{array}
if ux < 1.10000001e-4Initial program 38.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3236.2
Applied rewrites36.2%
Taylor expanded in ux around 0
metadata-evalN/A
fp-cancel-sign-sub-invN/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3278.2
Applied rewrites78.2%
Applied rewrites78.2%
if 1.10000001e-4 < ux Initial program 90.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3277.9
Applied rewrites77.9%
Taylor expanded in maxCos around 0
lower--.f3276.5
Applied rewrites76.5%
(FPCore (ux uy maxCos) :precision binary32 (* (* (* (PI) uy) 2.0) (sqrt (* (+ (fma (- (* 2.0 ux) 2.0) maxCos (- ux)) 2.0) ux))))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{\left(\mathsf{fma}\left(2 \cdot ux - 2, maxCos, -ux\right) + 2\right) \cdot ux}
\end{array}
Initial program 59.9%
lift-+.f32N/A
+-commutativeN/A
lift--.f32N/A
associate-+r-N/A
flip--N/A
lower-/.f32N/A
lower--.f32N/A
lower-*.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
lower-+.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f3259.2
Applied rewrites59.2%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.6%
Taylor expanded in maxCos around 0
Applied rewrites98.4%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3282.4
Applied rewrites82.4%
Final simplification82.4%
(FPCore (ux uy maxCos) :precision binary32 (* (* (+ (PI) (PI)) uy) (sqrt (* (fma -2.0 maxCos 2.0) ux))))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) + \mathsf{PI}\left(\right)\right) \cdot uy\right) \cdot \sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux}
\end{array}
Initial program 59.9%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3253.5
Applied rewrites53.5%
Taylor expanded in ux around 0
metadata-evalN/A
fp-cancel-sign-sub-invN/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3265.3
Applied rewrites65.3%
Applied rewrites65.3%
herbie shell --seed 2025006
(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)))))))