
(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 9 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 (* (* (- (/ (* (- 1.0 maxCos) 2.0) ux) (pow (- 1.0 maxCos) 2.0)) ux) ux))))
\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(\left(\frac{\left(1 - maxCos\right) \cdot 2}{ux} - {\left(1 - maxCos\right)}^{2}\right) \cdot ux\right) \cdot ux}
\end{array}
Initial program 56.3%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
flip--N/A
clear-numN/A
lower-/.f32N/A
lower-/.f32N/A
lower-+.f32N/A
lower--.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
metadata-evalN/A
lower--.f32N/A
pow2N/A
lower-pow.f32N/A
lower--.f3256.2
lift-*.f32N/A
*-commutativeN/A
lower-*.f3256.2
Applied rewrites56.2%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.3%
Applied rewrites98.3%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sin (* (* uy 2.0) (PI)))
(sqrt
(*
(-
(/ (- 1.0 maxCos) ux)
(- (/ (- maxCos 1.0) ux) (* (- 1.0 maxCos) (- maxCos 1.0))))
(* ux ux)))))\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(\frac{1 - maxCos}{ux} - \left(\frac{maxCos - 1}{ux} - \left(1 - maxCos\right) \cdot \left(maxCos - 1\right)\right)\right) \cdot \left(ux \cdot ux\right)}
\end{array}
Initial program 56.3%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
flip--N/A
clear-numN/A
lower-/.f32N/A
lower-/.f32N/A
lower-+.f32N/A
lower--.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
metadata-evalN/A
lower--.f32N/A
pow2N/A
lower-pow.f32N/A
lower--.f3256.2
lift-*.f32N/A
*-commutativeN/A
lower-*.f3256.2
Applied rewrites56.2%
Taylor expanded in maxCos around 0
associate-/l*N/A
lower-*.f32N/A
lower--.f32N/A
lower-/.f32N/A
lower--.f32N/A
unpow2N/A
lower-*.f32N/A
lower-+.f3254.1
Applied rewrites54.1%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.3%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= (* uy 2.0) 0.0002086000022245571)
(*
(* (* (PI) 2.0) uy)
(sqrt
(*
(- (- 2.0 (* (* (- maxCos 1.0) (+ -1.0 maxCos)) ux)) (* 2.0 maxCos))
ux)))
(* (sin (* (* uy 2.0) (PI))) (sqrt (* (- (/ 2.0 ux) 1.0) (* ux ux))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;uy \cdot 2 \leq 0.0002086000022245571:\\
\;\;\;\;\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}\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(\frac{2}{ux} - 1\right) \cdot \left(ux \cdot ux\right)}\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 2.08600002e-4Initial program 56.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3256.3
Applied rewrites56.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
lift-fma.f32N/A
*-commutativeN/A
Applied rewrites29.8%
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
distribute-rgt-outN/A
lower-*.f32N/A
lower--.f32N/A
lower-+.f32N/A
lower-*.f3298.4
Applied rewrites98.4%
if 2.08600002e-4 < (*.f32 uy #s(literal 2 binary32)) Initial program 56.4%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
flip--N/A
clear-numN/A
lower-/.f32N/A
lower-/.f32N/A
lower-+.f32N/A
lower--.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
metadata-evalN/A
lower--.f32N/A
pow2N/A
lower-pow.f32N/A
lower--.f3256.2
lift-*.f32N/A
*-commutativeN/A
lower-*.f3256.2
Applied rewrites56.2%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.0%
Taylor expanded in maxCos around 0
Applied rewrites92.9%
Final simplification96.3%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (* ux ux) (* (- 1.0 maxCos) (- (/ 2.0 ux) (- 1.0 maxCos))))) (sin (* (PI) (* uy 2.0)))))
\begin{array}{l}
\\
\sqrt{\left(ux \cdot ux\right) \cdot \left(\left(1 - maxCos\right) \cdot \left(\frac{2}{ux} - \left(1 - maxCos\right)\right)\right)} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(uy \cdot 2\right)\right)
\end{array}
Initial program 56.3%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
flip--N/A
clear-numN/A
lower-/.f32N/A
lower-/.f32N/A
lower-+.f32N/A
lower--.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
metadata-evalN/A
lower--.f32N/A
pow2N/A
lower-pow.f32N/A
lower--.f3256.2
lift-*.f32N/A
*-commutativeN/A
lower-*.f3256.2
Applied rewrites56.2%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.3%
Applied rewrites98.3%
lift-*.f32N/A
*-commutativeN/A
lower-*.f3298.3
Applied rewrites98.2%
Final simplification98.2%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= (* uy 2.0) 0.0032999999821186066)
(*
(* (* (PI) 2.0) uy)
(sqrt
(*
(- (- 2.0 (* (* (- maxCos 1.0) (+ -1.0 maxCos)) ux)) (* 2.0 maxCos))
ux)))
(* (sin (* (* uy 2.0) (PI))) (sqrt (* (* 2.0 ux) (- 1.0 maxCos))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;uy \cdot 2 \leq 0.0032999999821186066:\\
\;\;\;\;\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}\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(2 \cdot ux\right) \cdot \left(1 - maxCos\right)}\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 0.0033Initial program 56.7%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3256.3
Applied rewrites56.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
lift-fma.f32N/A
*-commutativeN/A
Applied rewrites29.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
distribute-rgt-outN/A
lower-*.f32N/A
lower--.f32N/A
lower-+.f32N/A
lower-*.f3296.7
Applied rewrites96.7%
if 0.0033 < (*.f32 uy #s(literal 2 binary32)) Initial program 55.4%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
flip--N/A
clear-numN/A
lower-/.f32N/A
lower-/.f32N/A
lower-+.f32N/A
lower--.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
metadata-evalN/A
lower--.f32N/A
pow2N/A
lower-pow.f32N/A
lower--.f3255.0
lift-*.f32N/A
*-commutativeN/A
lower-*.f3255.0
Applied rewrites55.0%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites97.8%
Taylor expanded in ux around 0
Applied rewrites77.1%
Final simplification91.1%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= (* uy 2.0) 0.011599999852478504)
(*
(* (* (PI) 2.0) uy)
(sqrt
(*
(- (- 2.0 (* (* (- maxCos 1.0) (+ -1.0 maxCos)) ux)) (* 2.0 maxCos))
ux)))
(* (sin (* (* uy 2.0) (PI))) (sqrt (* (fma -2.0 maxCos 2.0) ux)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;uy \cdot 2 \leq 0.011599999852478504:\\
\;\;\;\;\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}\\
\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 uy #s(literal 2 binary32)) < 0.0115999999Initial program 56.6%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3255.9
Applied rewrites55.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
lift-fma.f32N/A
*-commutativeN/A
Applied rewrites25.1%
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
distribute-rgt-outN/A
lower-*.f32N/A
lower--.f32N/A
lower-+.f32N/A
lower-*.f3295.5
Applied rewrites95.5%
if 0.0115999999 < (*.f32 uy #s(literal 2 binary32)) Initial program 55.4%
Taylor expanded in ux around 0
cancel-sign-sub-invN/A
metadata-evalN/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f326.7
Applied rewrites6.7%
Final simplification73.0%
(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 56.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3249.1
Applied rewrites49.1%
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
lift-fma.f32N/A
*-commutativeN/A
Applied rewrites27.8%
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
distribute-rgt-outN/A
lower-*.f32N/A
lower--.f32N/A
lower-+.f32N/A
lower-*.f3281.4
Applied rewrites81.4%
Final simplification81.4%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (PI) 2.0) uy)))
(if (<= maxCos 5.999999848427251e-5)
(* t_0 (sqrt (* (- 2.0 ux) ux)))
(* t_0 (sqrt (* (+ (* -2.0 maxCos) 2.0) ux))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\\
\mathbf{if}\;maxCos \leq 5.999999848427251 \cdot 10^{-5}:\\
\;\;\;\;t\_0 \cdot \sqrt{\left(2 - ux\right) \cdot ux}\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \sqrt{\left(-2 \cdot maxCos + 2\right) \cdot ux}\\
\end{array}
\end{array}
if maxCos < 5.99999985e-5Initial program 57.0%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3249.4
Applied rewrites49.4%
lift-+.f32N/A
lift--.f32N/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate--r-N/A
lift--.f32N/A
flip--N/A
metadata-evalN/A
unpow2N/A
lift-pow.f32N/A
lift--.f32N/A
lift-+.f32N/A
clear-numN/A
lift-/.f32N/A
lift-/.f3249.4
lift-/.f32N/A
clear-numN/A
Applied rewrites48.3%
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-*.f3280.7
Applied rewrites80.7%
Taylor expanded in maxCos around 0
Applied rewrites80.1%
if 5.99999985e-5 < maxCos Initial program 51.4%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3246.9
Applied rewrites46.9%
Taylor expanded in ux around 0
cancel-sign-sub-invN/A
metadata-evalN/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3245.3
Applied rewrites45.3%
Applied rewrites74.6%
Final simplification79.4%
(FPCore (ux uy maxCos) :precision binary32 (* (* (* (PI) 2.0) uy) (sqrt (* (- 2.0 ux) ux))))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right) \cdot \sqrt{\left(2 - ux\right) \cdot ux}
\end{array}
Initial program 56.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3249.1
Applied rewrites49.1%
lift-+.f32N/A
lift--.f32N/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate--r-N/A
lift--.f32N/A
flip--N/A
metadata-evalN/A
unpow2N/A
lift-pow.f32N/A
lift--.f32N/A
lift-+.f32N/A
clear-numN/A
lift-/.f32N/A
lift-/.f3249.1
lift-/.f32N/A
clear-numN/A
Applied rewrites44.6%
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-*.f3281.4
Applied rewrites81.4%
Taylor expanded in maxCos around 0
Applied rewrites76.1%
herbie shell --seed 2024318
(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)))))))