
(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
(let* ((t_0 (sin (* (PI) (* 2.0 uy)))))
(if (<= maxCos 9.9999998245167e-14)
(* (sqrt (* (- (* (/ 2.0 ux) ux) ux) ux)) t_0)
(*
(sqrt
(*
(* ux ux)
(*
(-
(/ (- (/ (/ 2.0 maxCos) maxCos) (/ 2.0 maxCos)) ux)
(- (/ 1.0 (* maxCos maxCos)) (- (/ 2.0 maxCos) 1.0)))
(* maxCos maxCos))))
t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\\
\mathbf{if}\;maxCos \leq 9.9999998245167 \cdot 10^{-14}:\\
\;\;\;\;\sqrt{\left(\frac{2}{ux} \cdot ux - ux\right) \cdot ux} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(ux \cdot ux\right) \cdot \left(\left(\frac{\frac{\frac{2}{maxCos}}{maxCos} - \frac{2}{maxCos}}{ux} - \left(\frac{1}{maxCos \cdot maxCos} - \left(\frac{2}{maxCos} - 1\right)\right)\right) \cdot \left(maxCos \cdot maxCos\right)\right)} \cdot t\_0\\
\end{array}
\end{array}
if maxCos < 9.99999982e-14Initial program 55.3%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
lower--.f32N/A
lower--.f3255.3
lift-*.f32N/A
*-commutativeN/A
lower-*.f3255.3
Applied rewrites55.3%
Taylor expanded in ux around inf
Applied rewrites15.8%
Taylor expanded in maxCos around 0
Applied rewrites98.2%
Applied rewrites98.4%
if 9.99999982e-14 < maxCos Initial program 57.1%
Taylor expanded in maxCos around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites55.5%
Taylor expanded in ux around inf
Applied rewrites98.5%
Final simplification98.4%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (sin (* (PI) (* 2.0 uy)))))
(if (<= maxCos 2.999999892949745e-8)
(* (sqrt (* (- (* (/ 2.0 ux) ux) ux) ux)) t_0)
(*
(sqrt
(*
(-
(*
(+
(/ (- 2.0 (/ 2.0 ux)) maxCos)
(/ (/ (- (/ 2.0 ux) 1.0) maxCos) maxCos))
(* ux ux))
(* ux ux))
(* maxCos maxCos)))
t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\\
\mathbf{if}\;maxCos \leq 2.999999892949745 \cdot 10^{-8}:\\
\;\;\;\;\sqrt{\left(\frac{2}{ux} \cdot ux - ux\right) \cdot ux} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(\left(\frac{2 - \frac{2}{ux}}{maxCos} + \frac{\frac{\frac{2}{ux} - 1}{maxCos}}{maxCos}\right) \cdot \left(ux \cdot ux\right) - ux \cdot ux\right) \cdot \left(maxCos \cdot maxCos\right)} \cdot t\_0\\
\end{array}
\end{array}
if maxCos < 2.99999989e-8Initial program 55.9%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
lower--.f32N/A
lower--.f3255.9
lift-*.f32N/A
*-commutativeN/A
lower-*.f3255.9
Applied rewrites55.9%
Taylor expanded in ux around inf
Applied rewrites15.7%
Taylor expanded in maxCos around 0
Applied rewrites98.2%
Applied rewrites98.4%
if 2.99999989e-8 < maxCos Initial program 56.1%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
lower--.f32N/A
lower--.f3256.6
lift-*.f32N/A
*-commutativeN/A
lower-*.f3256.6
Applied rewrites56.6%
Taylor expanded in ux around inf
Applied rewrites22.3%
Taylor expanded in maxCos around inf
Applied rewrites98.0%
Final simplification98.3%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (sin (* (PI) (* 2.0 uy)))))
(if (<= maxCos 1.0000000116860974e-7)
(* (sqrt (* (- (* (/ 2.0 ux) ux) ux) ux)) t_0)
(*
(sqrt
(*
(-
(/ (- (/ (* (- 2.0 ux) ux) maxCos) (* (* (- 1.0 ux) ux) 2.0)) maxCos)
(* ux ux))
(* maxCos maxCos)))
t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\\
\mathbf{if}\;maxCos \leq 1.0000000116860974 \cdot 10^{-7}:\\
\;\;\;\;\sqrt{\left(\frac{2}{ux} \cdot ux - ux\right) \cdot ux} \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(\frac{\frac{\left(2 - ux\right) \cdot ux}{maxCos} - \left(\left(1 - ux\right) \cdot ux\right) \cdot 2}{maxCos} - ux \cdot ux\right) \cdot \left(maxCos \cdot maxCos\right)} \cdot t\_0\\
\end{array}
\end{array}
if maxCos < 1.00000001e-7Initial program 55.9%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
lower--.f32N/A
lower--.f3255.9
lift-*.f32N/A
*-commutativeN/A
lower-*.f3255.9
Applied rewrites55.9%
Taylor expanded in ux around inf
Applied rewrites16.1%
Taylor expanded in maxCos around 0
Applied rewrites98.2%
Applied rewrites98.4%
if 1.00000001e-7 < maxCos Initial program 55.7%
Taylor expanded in maxCos around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites54.1%
Taylor expanded in ux around 0
Applied rewrites97.9%
Final simplification98.3%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (- (* (/ 2.0 ux) ux) ux) ux)) (sin (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(\frac{2}{ux} \cdot ux - ux\right) \cdot ux} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.9%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
lower--.f32N/A
lower--.f3256.0
lift-*.f32N/A
*-commutativeN/A
lower-*.f3256.0
Applied rewrites56.0%
Taylor expanded in ux around inf
Applied rewrites17.3%
Taylor expanded in maxCos around 0
Applied rewrites92.9%
Applied rewrites93.1%
Final simplification93.1%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= (* 2.0 uy) 0.004240000154823065)
(*
(sqrt
(-
(* (fma maxCos ux (- 1.0 ux)) (- ux (* ux maxCos)))
(- (* ux maxCos) ux)))
(* (* (PI) 2.0) uy))
(* (sqrt (* 2.0 ux)) (sin (* (PI) (* 2.0 uy))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;2 \cdot uy \leq 0.004240000154823065:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(maxCos, ux, 1 - ux\right) \cdot \left(ux - ux \cdot maxCos\right) - \left(ux \cdot maxCos - ux\right)} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{2 \cdot ux} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 0.00424000015Initial 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
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate--r-N/A
lift--.f32N/A
sub-negN/A
distribute-lft-inN/A
lower-+.f32N/A
lower-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
Applied rewrites20.8%
lift--.f32N/A
lift-+.f32N/A
associate--r+N/A
*-rgt-identityN/A
lift-*.f32N/A
lower-*.f32N/A
*-commutativeN/A
lift-neg.f32N/A
cancel-sign-subN/A
*-commutativeN/A
lower-+.f32N/A
Applied rewrites95.4%
if 0.00424000015 < (*.f32 uy #s(literal 2 binary32)) Initial program 53.9%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
lower--.f32N/A
lower--.f3253.9
lift-*.f32N/A
*-commutativeN/A
lower-*.f3253.9
Applied rewrites53.9%
Taylor expanded in ux around inf
Applied rewrites7.6%
Taylor expanded in maxCos around 0
Applied rewrites91.8%
Taylor expanded in ux around 0
Applied rewrites75.5%
Final simplification52.7%
(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 55.9%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
lower--.f32N/A
lower--.f3256.0
lift-*.f32N/A
*-commutativeN/A
lower-*.f3256.0
Applied rewrites56.0%
Taylor expanded in ux around inf
Applied rewrites17.3%
Taylor expanded in maxCos around 0
Applied rewrites92.9%
Taylor expanded in ux around 0
Applied rewrites93.1%
Final simplification93.1%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sqrt
(-
(* (fma maxCos ux (- 1.0 ux)) (- ux (* ux maxCos)))
(- (* ux maxCos) ux)))
(* (* (PI) 2.0) uy)))\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(maxCos, ux, 1 - ux\right) \cdot \left(ux - ux \cdot maxCos\right) - \left(ux \cdot maxCos - ux\right)} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)
\end{array}
Initial program 55.9%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3248.8
Applied rewrites48.8%
lift-*.f32N/A
lift-+.f32N/A
lift--.f32N/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate--r-N/A
lift--.f32N/A
sub-negN/A
distribute-lft-inN/A
lower-+.f32N/A
lower-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
Applied rewrites48.2%
lift--.f32N/A
lift-+.f32N/A
associate--r+N/A
*-rgt-identityN/A
lift-*.f32N/A
lower-*.f32N/A
*-commutativeN/A
lift-neg.f32N/A
cancel-sign-subN/A
*-commutativeN/A
lower-+.f32N/A
Applied rewrites80.3%
Final simplification80.3%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (+ (* (- 1.0 ux) ux) ux)) (* (* (PI) 2.0) uy)))
\begin{array}{l}
\\
\sqrt{\left(1 - ux\right) \cdot ux + ux} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)
\end{array}
Initial program 55.9%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3248.8
Applied rewrites48.8%
lift-*.f32N/A
lift-+.f32N/A
lift--.f32N/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate--r-N/A
lift--.f32N/A
sub-negN/A
distribute-lft-inN/A
lower-+.f32N/A
lower-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
Applied rewrites19.9%
Taylor expanded in maxCos around 0
associate-*r*N/A
mul-1-negN/A
cancel-sign-subN/A
lower-+.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3277.4
Applied rewrites77.4%
Final simplification77.4%
(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.9%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3248.8
Applied rewrites48.8%
Taylor expanded in ux around 0
cancel-sign-sub-invN/A
metadata-evalN/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3263.3
Applied rewrites-0.0%
Final simplification63.3%
(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.9%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3248.8
Applied rewrites48.8%
Taylor expanded in ux around 0
Applied rewrites7.1%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
neg-mul-1N/A
metadata-evalN/A
lower-fma.f32N/A
metadata-eval20.0
Applied rewrites20.0%
Final simplification20.0%
(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.9%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3248.8
Applied rewrites48.8%
Taylor expanded in ux around 0
Applied rewrites7.1%
Final simplification7.1%
herbie shell --seed 2024255
(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)))))))