
(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
(*
(sqrt
(-
(*
(* ux ux)
(- (* (- 1.0 maxCos) maxCos) (/ (- maxCos (- 2.0 maxCos)) ux)))
(* (* ux ux) (- 1.0 maxCos))))
(sin (* (PI) (* 2.0 uy)))))\begin{array}{l}
\\
\sqrt{\left(ux \cdot ux\right) \cdot \left(\left(1 - maxCos\right) \cdot maxCos - \frac{maxCos - \left(2 - maxCos\right)}{ux}\right) - \left(ux \cdot ux\right) \cdot \left(1 - maxCos\right)} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.7%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
lift-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
distribute-rgt-inN/A
distribute-neg-inN/A
associate-+l+N/A
distribute-rgt-neg-inN/A
lift-*.f32N/A
associate-*l*N/A
lower-fma.f32N/A
Applied rewrites18.2%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.0%
Applied rewrites98.0%
Final simplification98.0%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sqrt
(*
(-
(- maxCos (- (/ (- maxCos (- 2.0 maxCos)) ux) -1.0))
(* (- maxCos 1.0) maxCos))
(* ux ux)))
(sin (* (PI) (* 2.0 uy)))))\begin{array}{l}
\\
\sqrt{\left(\left(maxCos - \left(\frac{maxCos - \left(2 - maxCos\right)}{ux} - -1\right)\right) - \left(maxCos - 1\right) \cdot maxCos\right) \cdot \left(ux \cdot ux\right)} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.7%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
lift-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
distribute-rgt-inN/A
distribute-neg-inN/A
associate-+l+N/A
distribute-rgt-neg-inN/A
lift-*.f32N/A
associate-*l*N/A
lower-fma.f32N/A
Applied rewrites18.3%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.0%
Applied rewrites98.0%
Final simplification98.0%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sqrt
(*
(*
(-
(- (* (- 1.0 maxCos) maxCos) (/ (- maxCos (- 2.0 maxCos)) ux))
(- 1.0 maxCos))
ux)
ux))
(sin (* (PI) (* 2.0 uy)))))\begin{array}{l}
\\
\sqrt{\left(\left(\left(\left(1 - maxCos\right) \cdot maxCos - \frac{maxCos - \left(2 - maxCos\right)}{ux}\right) - \left(1 - maxCos\right)\right) \cdot ux\right) \cdot ux} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.7%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
lift-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
distribute-rgt-inN/A
distribute-neg-inN/A
associate-+l+N/A
distribute-rgt-neg-inN/A
lift-*.f32N/A
associate-*l*N/A
lower-fma.f32N/A
Applied rewrites18.2%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.0%
Applied rewrites98.0%
Final simplification98.0%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= (* 2.0 uy) 0.0010499999625608325)
(*
(sqrt
(*
(- (- 2.0 (* (* (- maxCos 1.0) (- maxCos 1.0)) ux)) (* maxCos 2.0))
ux))
(* (* (PI) 2.0) uy))
(* (sqrt (* (- (/ 2.0 ux) 1.0) (* ux ux))) (sin (* (PI) (* 2.0 uy))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;2 \cdot uy \leq 0.0010499999625608325:\\
\;\;\;\;\sqrt{\left(\left(2 - \left(\left(maxCos - 1\right) \cdot \left(maxCos - 1\right)\right) \cdot ux\right) - maxCos \cdot 2\right) \cdot ux} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(\frac{2}{ux} - 1\right) \cdot \left(ux \cdot ux\right)} \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.00104999996Initial program 56.4%
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
cancel-sign-sub-invN/A
lift-+.f32N/A
distribute-rgt-inN/A
lift--.f32N/A
sub-negN/A
lift-neg.f32N/A
+-commutativeN/A
distribute-rgt-inN/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-+r+N/A
lift-+.f32N/A
lift-+.f32N/A
Applied rewrites49.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-*.f3297.8
Applied rewrites97.8%
if 0.00104999996 < (*.f32 uy #s(literal 2 binary32)) Initial program 54.3%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
lift-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
distribute-rgt-inN/A
distribute-neg-inN/A
associate-+l+N/A
distribute-rgt-neg-inN/A
lift-*.f32N/A
associate-*l*N/A
lower-fma.f32N/A
Applied rewrites10.0%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites97.3%
Taylor expanded in maxCos around 0
Applied rewrites89.8%
Final simplification95.1%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= (* 2.0 uy) 0.0010499999625608325)
(*
(sqrt
(*
(- (- 2.0 (* (* (- maxCos 1.0) (- maxCos 1.0)) ux)) (* maxCos 2.0))
ux))
(* (* (PI) 2.0) uy))
(* (sin (* (* (PI) uy) 2.0)) (sqrt (+ (* (- 1.0 ux) ux) ux)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;2 \cdot uy \leq 0.0010499999625608325:\\
\;\;\;\;\sqrt{\left(\left(2 - \left(\left(maxCos - 1\right) \cdot \left(maxCos - 1\right)\right) \cdot ux\right) - maxCos \cdot 2\right) \cdot ux} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{\left(1 - ux\right) \cdot ux + ux}\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 0.00104999996Initial program 56.4%
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
cancel-sign-sub-invN/A
lift-+.f32N/A
distribute-rgt-inN/A
lift--.f32N/A
sub-negN/A
lift-neg.f32N/A
+-commutativeN/A
distribute-rgt-inN/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-+r+N/A
lift-+.f32N/A
lift-+.f32N/A
Applied rewrites47.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
distribute-rgt-outN/A
lower-*.f32N/A
lower--.f32N/A
lower-+.f32N/A
lower-*.f3297.8
Applied rewrites97.8%
if 0.00104999996 < (*.f32 uy #s(literal 2 binary32)) Initial program 54.3%
Taylor expanded in uy around 0
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3234.0
Applied rewrites34.0%
lift--.f32N/A
lift-*.f32N/A
cancel-sign-sub-invN/A
lift-+.f32N/A
distribute-rgt-inN/A
lift--.f32N/A
sub-negN/A
lift-neg.f32N/A
+-commutativeN/A
distribute-rgt-inN/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-+r+N/A
lift-+.f32N/A
lift-+.f32N/A
Applied rewrites33.9%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-*.f32N/A
lower-sqrt.f32N/A
lower--.f32N/A
associate-*r*N/A
lower-*.f32N/A
mul-1-negN/A
lower-neg.f32N/A
lower--.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3289.7
Applied rewrites89.7%
Final simplification95.1%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (- (- 2.0 (* (* (- maxCos 1.0) (- maxCos 1.0)) ux)) (* maxCos 2.0)) ux)) (* (* (PI) 2.0) uy)))
\begin{array}{l}
\\
\sqrt{\left(\left(2 - \left(\left(maxCos - 1\right) \cdot \left(maxCos - 1\right)\right) \cdot ux\right) - maxCos \cdot 2\right) \cdot ux} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)
\end{array}
Initial program 55.7%
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
cancel-sign-sub-invN/A
lift-+.f32N/A
distribute-rgt-inN/A
lift--.f32N/A
sub-negN/A
lift-neg.f32N/A
+-commutativeN/A
distribute-rgt-inN/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-+r+N/A
lift-+.f32N/A
lift-+.f32N/A
Applied rewrites46.0%
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-*.f3279.5
Applied rewrites79.5%
Final simplification79.5%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (+ (* -2.0 maxCos) 2.0) ux)) (* (* (PI) 2.0) uy)))
\begin{array}{l}
\\
\sqrt{\left(-2 \cdot maxCos + 2\right) \cdot ux} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)
\end{array}
Initial program 55.7%
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.f3261.6
Applied rewrites58.1%
Applied rewrites64.2%
Final simplification64.2%
(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.7%
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
cancel-sign-sub-invN/A
lift-+.f32N/A
distribute-rgt-inN/A
lift--.f32N/A
sub-negN/A
lift-neg.f32N/A
+-commutativeN/A
distribute-rgt-inN/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-+r+N/A
lift-+.f32N/A
lift-+.f32N/A
Applied rewrites46.7%
Taylor expanded in maxCos around 0
lower--.f32N/A
associate-*r*N/A
lower-*.f32N/A
mul-1-negN/A
lower-neg.f32N/A
lower--.f3275.0
Applied rewrites75.0%
Final simplification75.0%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* 2.0 ux)) (* (* (PI) 2.0) uy)))
\begin{array}{l}
\\
\sqrt{2 \cdot ux} \cdot \left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right)
\end{array}
Initial program 55.7%
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.f3261.6
Applied rewrites61.6%
Taylor expanded in maxCos around 0
Applied rewrites61.6%
Final simplification61.6%
herbie shell --seed 2024331
(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)))))))