
(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 uy) (PI))) (sqrt (* (- 2.0 (+ (* (fma (+ -2.0 maxCos) ux 2.0) maxCos) ux)) ux))))
\begin{array}{l}
\\
\sin \left(\left(uy + uy\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(2 - \left(\mathsf{fma}\left(-2 + maxCos, ux, 2\right) \cdot maxCos + ux\right)\right) \cdot ux}
\end{array}
Initial program 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
associate-*r*N/A
mul-1-negN/A
fp-cancel-sub-signN/A
associate--l-N/A
+-commutativeN/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3298.4
Applied rewrites98.4%
Taylor expanded in maxCos around 0
Applied rewrites98.4%
Applied rewrites98.5%
lift-*.f32N/A
*-commutativeN/A
count-2-revN/A
lower-+.f3298.5
Applied rewrites98.5%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (* (+ uy uy) (PI))) (sqrt (* (- 2.0 (fma (fma ux (+ -2.0 maxCos) 2.0) maxCos ux)) ux))))
\begin{array}{l}
\\
\sin \left(\left(uy + uy\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 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
associate-*r*N/A
mul-1-negN/A
fp-cancel-sub-signN/A
associate--l-N/A
+-commutativeN/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3298.4
Applied rewrites98.4%
Taylor expanded in maxCos around 0
Applied rewrites98.4%
lift-*.f32N/A
*-commutativeN/A
count-2-revN/A
lower-+.f3298.4
Applied rewrites98.4%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (* (* uy 2.0) (PI))) (sqrt (* (- 2.0 (fma (fma -2.0 ux 2.0) maxCos ux)) ux))))
\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\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 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
associate-*r*N/A
mul-1-negN/A
fp-cancel-sub-signN/A
associate--l-N/A
+-commutativeN/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3298.4
Applied rewrites98.4%
Taylor expanded in maxCos around 0
Applied rewrites97.7%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= maxCos 1.500000053056283e-7)
(* (sin (* (* uy 2.0) (PI))) (sqrt (* (- 2.0 ux) ux)))
(*
(* (* 2.0 uy) (PI))
(sqrt
(*
(*
(-
(- (/ (fma -2.0 maxCos 2.0) ux) 1.0)
(- (fma maxCos maxCos (- maxCos)) maxCos))
ux)
ux)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;maxCos \leq 1.500000053056283 \cdot 10^{-7}:\\
\;\;\;\;\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(2 - ux\right) \cdot ux}\\
\mathbf{else}:\\
\;\;\;\;\left(\left(2 \cdot uy\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(\left(\left(\frac{\mathsf{fma}\left(-2, maxCos, 2\right)}{ux} - 1\right) - \left(\mathsf{fma}\left(maxCos, maxCos, -maxCos\right) - maxCos\right)\right) \cdot ux\right) \cdot ux}\\
\end{array}
\end{array}
if maxCos < 1.5000001e-7Initial program 52.8%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
associate-*r*N/A
mul-1-negN/A
fp-cancel-sub-signN/A
associate--l-N/A
+-commutativeN/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3298.5
Applied rewrites98.5%
Taylor expanded in maxCos around 0
Applied rewrites98.5%
if 1.5000001e-7 < maxCos Initial program 55.5%
Taylor expanded in uy around 0
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-PI.f3249.5
Applied rewrites49.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
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites48.8%
Taylor expanded in ux around -inf
lower-*.f32N/A
unpow2N/A
lower-*.f32N/A
associate--r+N/A
lower--.f32N/A
+-commutativeN/A
associate-*r/N/A
metadata-evalN/A
associate-*r/N/A
div-addN/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
lower-fma.f32N/A
mul-1-negN/A
+-commutativeN/A
Applied rewrites83.0%
Applied rewrites83.1%
Final simplification95.4%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (* (* uy 2.0) (PI))) (sqrt (* (- 2.0 (fma 2.0 maxCos ux)) ux))))
\begin{array}{l}
\\
\sin \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 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
associate-*r*N/A
mul-1-negN/A
fp-cancel-sub-signN/A
associate--l-N/A
+-commutativeN/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3298.4
Applied rewrites98.4%
Taylor expanded in maxCos around 0
Applied rewrites98.4%
Taylor expanded in ux around 0
Applied rewrites97.0%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (+ (- 1.0 ux) (* ux maxCos))) (t_1 (* (* 2.0 uy) (PI))))
(if (<= (* t_0 t_0) 0.9985799789428711)
(* t_1 (sqrt (- 1.0 (* t_0 (- 1.0 ux)))))
(* t_1 (sqrt (* (fma -2.0 maxCos 2.0) ux))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
t_1 := \left(2 \cdot uy\right) \cdot \mathsf{PI}\left(\right)\\
\mathbf{if}\;t\_0 \cdot t\_0 \leq 0.9985799789428711:\\
\;\;\;\;t\_1 \cdot \sqrt{1 - t\_0 \cdot \left(1 - ux\right)}\\
\mathbf{else}:\\
\;\;\;\;t\_1 \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.99857998Initial program 90.3%
Taylor expanded in uy around 0
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-PI.f3281.3
Applied rewrites81.3%
Taylor expanded in maxCos around 0
lower--.f3278.2
Applied rewrites78.2%
if 0.99857998 < (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos))) Initial program 39.1%
Taylor expanded in uy around 0
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-PI.f3236.6
Applied rewrites36.6%
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 rewrites34.3%
Taylor expanded in ux around -inf
lower-*.f32N/A
unpow2N/A
lower-*.f32N/A
associate--r+N/A
lower--.f32N/A
+-commutativeN/A
associate-*r/N/A
metadata-evalN/A
associate-*r/N/A
div-addN/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
lower-fma.f32N/A
mul-1-negN/A
+-commutativeN/A
Applied rewrites82.8%
Taylor expanded in ux around 0
Applied rewrites78.3%
Final simplification78.2%
(FPCore (ux uy maxCos) :precision binary32 (* (* (* (PI) uy) 2.0) (sqrt (* (- 2.0 (+ (* (fma (+ -2.0 maxCos) ux 2.0) maxCos) ux)) ux))))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{\left(2 - \left(\mathsf{fma}\left(-2 + maxCos, ux, 2\right) \cdot maxCos + ux\right)\right) \cdot ux}
\end{array}
Initial program 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
associate-*r*N/A
mul-1-negN/A
fp-cancel-sub-signN/A
associate--l-N/A
+-commutativeN/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3298.4
Applied rewrites98.4%
Taylor expanded in maxCos around 0
Applied rewrites98.4%
Applied rewrites98.5%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3284.0
Applied rewrites84.0%
(FPCore (ux uy maxCos) :precision binary32 (* (* 2.0 (* uy (PI))) (sqrt (* (- 2.0 (fma (fma ux (+ -2.0 maxCos) 2.0) maxCos ux)) ux))))
\begin{array}{l}
\\
\left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\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 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
associate-*r*N/A
mul-1-negN/A
fp-cancel-sub-signN/A
associate--l-N/A
+-commutativeN/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3298.4
Applied rewrites98.4%
Taylor expanded in maxCos around 0
Applied rewrites98.4%
Taylor expanded in uy around 0
lower-*.f32N/A
lower-*.f32N/A
lower-PI.f3284.0
Applied rewrites84.0%
(FPCore (ux uy maxCos) :precision binary32 (* (* (* 2.0 uy) (PI)) (sqrt (* (fma -2.0 maxCos 2.0) ux))))
\begin{array}{l}
\\
\left(\left(2 \cdot uy\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux}
\end{array}
Initial program 53.3%
Taylor expanded in uy around 0
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-PI.f3249.0
Applied rewrites49.0%
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 rewrites47.4%
Taylor expanded in ux around -inf
lower-*.f32N/A
unpow2N/A
lower-*.f32N/A
associate--r+N/A
lower--.f32N/A
+-commutativeN/A
associate-*r/N/A
metadata-evalN/A
associate-*r/N/A
div-addN/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
lower-fma.f32N/A
mul-1-negN/A
+-commutativeN/A
Applied rewrites83.8%
Taylor expanded in ux around 0
Applied rewrites70.1%
Final simplification70.1%
(FPCore (ux uy maxCos) :precision binary32 (* (* (* 2.0 uy) (PI)) (sqrt (- 1.0 (fma -2.0 ux 1.0)))))
\begin{array}{l}
\\
\left(\left(2 \cdot uy\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - \mathsf{fma}\left(-2, ux, 1\right)}
\end{array}
Initial program 53.3%
Taylor expanded in uy around 0
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-PI.f3249.0
Applied rewrites49.0%
Taylor expanded in ux around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
metadata-evalN/A
fp-cancel-sub-sign-invN/A
metadata-evalN/A
metadata-evalN/A
lower-fma.f3241.7
Applied rewrites41.7%
Taylor expanded in maxCos around 0
Applied rewrites40.9%
(FPCore (ux uy maxCos) :precision binary32 (* (* (* 2.0 uy) (PI)) (sqrt (- 1.0 1.0))))
\begin{array}{l}
\\
\left(\left(2 \cdot uy\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - 1}
\end{array}
Initial program 53.3%
Taylor expanded in uy around 0
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-PI.f3249.0
Applied rewrites49.0%
Taylor expanded in ux around 0
Applied rewrites7.2%
herbie shell --seed 2025017
(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)))))))