
(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 maxCos) ux) (* (- 2.0 (* (pow (- maxCos 1.0) 2.0) ux)) ux))) (sin (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(-2 \cdot maxCos\right) \cdot ux + \left(2 - {\left(maxCos - 1\right)}^{2} \cdot ux\right) \cdot ux} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.8%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites56.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
lower-pow.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3298.2
Applied rewrites98.2%
Applied rewrites98.2%
Final simplification98.2%
(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 55.8%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites56.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
lower-pow.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3298.2
Applied rewrites98.2%
Final simplification98.2%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (+ (* (- 2.0 ux) ux) (* (* -2.0 maxCos) ux))) (sin (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(2 - ux\right) \cdot ux + \left(-2 \cdot maxCos\right) \cdot ux} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.8%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites56.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
lower-pow.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3298.2
Applied rewrites98.2%
Applied rewrites98.2%
Taylor expanded in maxCos around 0
Applied rewrites96.2%
Final simplification96.2%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (* (PI) (* 2.0 uy))))
(if (<= (* 2.0 uy) 0.00019500000053085387)
(*
t_0
(sqrt
(*
(* ux ux)
(-
(- (* (- 1.0 maxCos) maxCos) (/ (- maxCos (- 2.0 maxCos)) ux))
(- 1.0 maxCos)))))
(* (sqrt (* (- 2.0 ux) ux)) (sin t_0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\\
\mathbf{if}\;2 \cdot uy \leq 0.00019500000053085387:\\
\;\;\;\;t\_0 \cdot \sqrt{\left(ux \cdot ux\right) \cdot \left(\left(\left(1 - maxCos\right) \cdot maxCos - \frac{maxCos - \left(2 - maxCos\right)}{ux}\right) - \left(1 - maxCos\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(2 - ux\right) \cdot ux} \cdot \sin t\_0\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 1.95000001e-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%
Applied rewrites20.4%
lift-*.f32N/A
*-commutativeN/A
lower-*.f3220.4
Applied rewrites20.4%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.3%
if 1.95000001e-4 < (*.f32 uy #s(literal 2 binary32)) Initial program 55.1%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites55.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
lower-pow.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3297.7
Applied rewrites97.7%
Taylor expanded in maxCos around 0
Applied rewrites91.9%
Final simplification95.5%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (PI) 2.0) uy)) (t_1 (+ (* maxCos ux) (- 1.0 ux))))
(if (<= t_1 0.9999099969863892)
(* (sqrt (- 1.0 (* (- 1.0 (- ux (* maxCos ux))) t_1))) t_0)
(* (sqrt (* (fma -2.0 maxCos 2.0) ux)) t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\\
t_1 := maxCos \cdot ux + \left(1 - ux\right)\\
\mathbf{if}\;t\_1 \leq 0.9999099969863892:\\
\;\;\;\;\sqrt{1 - \left(1 - \left(ux - maxCos \cdot ux\right)\right) \cdot t\_1} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux} \cdot t\_0\\
\end{array}
\end{array}
if (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) < 0.999909997Initial program 88.0%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3272.9
Applied rewrites72.9%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
lift--.f32N/A
lower--.f3273.1
Applied rewrites73.1%
if 0.999909997 < (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) Initial program 35.1%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3231.6
Applied rewrites31.6%
Taylor expanded in ux around 0
Applied rewrites7.2%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
neg-mul-1N/A
lower-fma.f3216.8
Applied rewrites16.8%
Taylor expanded in ux around 0
mul-1-negN/A
*-commutativeN/A
distribute-lft-neg-inN/A
sub-negN/A
metadata-evalN/A
distribute-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
metadata-evalN/A
+-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3269.1
Applied rewrites6.7%
Final simplification70.7%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (PI) 2.0) uy)) (t_1 (+ (* maxCos ux) (- 1.0 ux))))
(if (<= t_1 0.9999099969863892)
(* (sqrt (- 1.0 (* t_1 t_1))) t_0)
(* (sqrt (* (fma -2.0 maxCos 2.0) ux)) t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\\
t_1 := maxCos \cdot ux + \left(1 - ux\right)\\
\mathbf{if}\;t\_1 \leq 0.9999099969863892:\\
\;\;\;\;\sqrt{1 - t\_1 \cdot t\_1} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux} \cdot t\_0\\
\end{array}
\end{array}
if (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) < 0.999909997Initial program 88.0%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3272.9
Applied rewrites72.9%
if 0.999909997 < (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) Initial program 35.1%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3231.6
Applied rewrites31.6%
Taylor expanded in ux around 0
Applied rewrites7.2%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
neg-mul-1N/A
lower-fma.f3216.8
Applied rewrites16.8%
Taylor expanded in ux around 0
mul-1-negN/A
*-commutativeN/A
distribute-lft-neg-inN/A
sub-negN/A
metadata-evalN/A
distribute-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
metadata-evalN/A
+-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3269.1
Applied rewrites6.7%
Final simplification70.6%
(FPCore (ux uy maxCos)
:precision binary32
(*
(* (PI) (* 2.0 uy))
(sqrt
(*
(* ux ux)
(-
(- (* (- 1.0 maxCos) maxCos) (/ (- maxCos (- 2.0 maxCos)) ux))
(- 1.0 maxCos))))))\begin{array}{l}
\\
\left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right) \cdot \sqrt{\left(ux \cdot ux\right) \cdot \left(\left(\left(1 - maxCos\right) \cdot maxCos - \frac{maxCos - \left(2 - maxCos\right)}{ux}\right) - \left(1 - maxCos\right)\right)}
\end{array}
Initial program 55.8%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3247.7
Applied rewrites47.7%
Applied rewrites19.8%
lift-*.f32N/A
*-commutativeN/A
lower-*.f3219.8
Applied rewrites19.8%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites77.8%
Final simplification77.8%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (PI) 2.0) uy)) (t_1 (+ (* maxCos ux) (- 1.0 ux))))
(if (<= t_1 0.9999099969863892)
(* (sqrt (- 1.0 (* (- 1.0 ux) t_1))) t_0)
(* (sqrt (* (fma -2.0 maxCos 2.0) ux)) t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\\
t_1 := maxCos \cdot ux + \left(1 - ux\right)\\
\mathbf{if}\;t\_1 \leq 0.9999099969863892:\\
\;\;\;\;\sqrt{1 - \left(1 - ux\right) \cdot t\_1} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux} \cdot t\_0\\
\end{array}
\end{array}
if (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) < 0.999909997Initial program 88.0%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3272.9
Applied rewrites72.9%
Taylor expanded in maxCos around 0
lower--.f3268.7
Applied rewrites68.7%
if 0.999909997 < (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) Initial program 35.1%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3231.6
Applied rewrites31.6%
Taylor expanded in ux around 0
Applied rewrites7.2%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
neg-mul-1N/A
lower-fma.f3216.8
Applied rewrites16.8%
Taylor expanded in ux around 0
mul-1-negN/A
*-commutativeN/A
distribute-lft-neg-inN/A
sub-negN/A
metadata-evalN/A
distribute-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
metadata-evalN/A
+-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3269.1
Applied rewrites6.7%
Final simplification68.9%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (fma -2.0 maxCos 2.0) ux)) (* (* (PI) 2.0) uy)))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)
\end{array}
Initial program 55.8%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3247.7
Applied rewrites47.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.8
Applied rewrites19.8%
Taylor expanded in ux around 0
mul-1-negN/A
*-commutativeN/A
distribute-lft-neg-inN/A
sub-negN/A
metadata-evalN/A
distribute-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
metadata-evalN/A
+-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3260.9
Applied rewrites60.9%
Final simplification60.9%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (fma -1.0 1.0 1.0)) (* (* (PI) 2.0) uy)))
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(-1, 1, 1\right)} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)
\end{array}
Initial program 55.8%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3247.7
Applied rewrites47.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.8
Applied rewrites19.8%
Final simplification19.8%
(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 55.8%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3247.7
Applied rewrites47.7%
Taylor expanded in ux around 0
Applied rewrites7.1%
Final simplification7.1%
herbie shell --seed 2024249
(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)))))))