
(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 14 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 (* ux (pow (- maxCos 1.0) 2.0))))
(*
(sin (* (* uy 2.0) (PI)))
(sqrt (* (- (/ (- (* t_0 t_0) 4.0) (- (+ t_0 2.0))) (* maxCos 2.0)) ux)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := ux \cdot {\left(maxCos - 1\right)}^{2}\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(\frac{t\_0 \cdot t\_0 - 4}{-\left(t\_0 + 2\right)} - maxCos \cdot 2\right) \cdot ux}
\end{array}
\end{array}
Initial program 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
+-commutativeN/A
associate-*r*N/A
mul-1-negN/A
lower-fma.f32N/A
lower-neg.f32N/A
lower-pow.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3298.3
Applied rewrites98.3%
flip-+N/A
lower-/.f32N/A
Applied rewrites98.3%
Final simplification98.3%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sin (* (* uy 2.0) (PI)))
(sqrt
(*
(- (fma (- ux) (fma (- maxCos 2.0) maxCos 1.0) 2.0) (* maxCos 2.0))
ux))))\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(\mathsf{fma}\left(-ux, \mathsf{fma}\left(maxCos - 2, maxCos, 1\right), 2\right) - maxCos \cdot 2\right) \cdot ux}
\end{array}
Initial program 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
+-commutativeN/A
associate-*r*N/A
mul-1-negN/A
lower-fma.f32N/A
lower-neg.f32N/A
lower-pow.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3298.3
Applied rewrites98.3%
Taylor expanded in maxCos around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower--.f3298.3
Applied rewrites98.3%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (* (* uy 2.0) (PI))) (sqrt (fma (* (- (* ux 2.0) 2.0) ux) maxCos (* (fma -1.0 ux 2.0) ux)))))
\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\mathsf{fma}\left(\left(ux \cdot 2 - 2\right) \cdot ux, maxCos, \mathsf{fma}\left(-1, ux, 2\right) \cdot ux\right)}
\end{array}
Initial program 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
+-commutativeN/A
associate-*r*N/A
mul-1-negN/A
lower-fma.f32N/A
lower-neg.f32N/A
lower-pow.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3298.3
Applied rewrites98.3%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3297.7
Applied rewrites97.7%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (* (* uy 2.0) (PI))) (sqrt (* (+ (fma (- (* ux 2.0) 2.0) maxCos (- ux)) 2.0) ux))))
\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(\mathsf{fma}\left(ux \cdot 2 - 2, maxCos, -ux\right) + 2\right) \cdot ux}
\end{array}
Initial program 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
+-commutativeN/A
associate-*r*N/A
mul-1-negN/A
lower-fma.f32N/A
lower-neg.f32N/A
lower-pow.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3298.3
Applied rewrites98.3%
Taylor expanded in maxCos around 0
+-commutativeN/A
lower-+.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
mul-1-negN/A
lower-neg.f3297.7
Applied rewrites97.7%
(FPCore (ux uy maxCos) :precision binary32 (* (sin (* (* uy 2.0) (PI))) (sqrt (* (- (fma -1.0 ux 2.0) (* maxCos 2.0)) ux))))
\begin{array}{l}
\\
\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, ux, 2\right) - maxCos \cdot 2\right) \cdot ux}
\end{array}
Initial program 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
+-commutativeN/A
associate-*r*N/A
mul-1-negN/A
lower-fma.f32N/A
lower-neg.f32N/A
lower-pow.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3298.3
Applied rewrites98.3%
Taylor expanded in maxCos around 0
+-commutativeN/A
lower-fma.f3297.0
Applied rewrites97.0%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= maxCos 3.9999998989515007e-5)
(* (sin (* (* uy 2.0) (PI))) (sqrt (* (fma -1.0 ux 2.0) ux)))
(*
(* (* (PI) uy) 2.0)
(sqrt
(*
(* ux ux)
(-
(* 2.0 (/ 1.0 ux))
(fma
-1.0
(- maxCos 1.0)
(fma 2.0 (/ maxCos ux) (* maxCos (- maxCos 1.0))))))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;maxCos \leq 3.9999998989515007 \cdot 10^{-5}:\\
\;\;\;\;\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\mathsf{fma}\left(-1, ux, 2\right) \cdot ux}\\
\mathbf{else}:\\
\;\;\;\;\left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{\left(ux \cdot ux\right) \cdot \left(2 \cdot \frac{1}{ux} - \mathsf{fma}\left(-1, maxCos - 1, \mathsf{fma}\left(2, \frac{maxCos}{ux}, maxCos \cdot \left(maxCos - 1\right)\right)\right)\right)}\\
\end{array}
\end{array}
if maxCos < 3.9999999e-5Initial program 53.0%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
+-commutativeN/A
associate-*r*N/A
mul-1-negN/A
lower-fma.f32N/A
lower-neg.f32N/A
lower-pow.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3298.3
Applied rewrites98.3%
Taylor expanded in maxCos around 0
+-commutativeN/A
lower-fma.f3297.7
Applied rewrites97.7%
if 3.9999999e-5 < maxCos Initial program 56.2%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3250.7
Applied rewrites50.7%
Taylor expanded in ux around 0
+-commutative7.1
*-commutative7.1
*-commutative7.1
distribute-lft-out7.1
Applied rewrites7.1%
Taylor expanded in ux around inf
lower-*.f32N/A
pow2N/A
lower-*.f32N/A
lower--.f32N/A
lower-*.f32N/A
lower-/.f32N/A
lower-fma.f32N/A
lower--.f32N/A
lower-fma.f32N/A
lower-/.f32N/A
lower-*.f32N/A
lower--.f3281.3
Applied rewrites81.3%
Final simplification96.0%
(FPCore (ux uy maxCos)
:precision binary32
(*
(* (* (PI) uy) 2.0)
(sqrt
(*
(* ux ux)
(-
(* 2.0 (/ 1.0 ux))
(fma
-1.0
(- maxCos 1.0)
(fma 2.0 (/ maxCos ux) (* maxCos (- maxCos 1.0)))))))))\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{\left(ux \cdot ux\right) \cdot \left(2 \cdot \frac{1}{ux} - \mathsf{fma}\left(-1, maxCos - 1, \mathsf{fma}\left(2, \frac{maxCos}{ux}, maxCos \cdot \left(maxCos - 1\right)\right)\right)\right)}
\end{array}
Initial program 53.3%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3248.1
Applied rewrites48.1%
Taylor expanded in ux around 0
+-commutative7.1
*-commutative7.1
*-commutative7.1
distribute-lft-out7.1
Applied rewrites7.1%
Taylor expanded in ux around inf
lower-*.f32N/A
pow2N/A
lower-*.f32N/A
lower--.f32N/A
lower-*.f32N/A
lower-/.f32N/A
lower-fma.f32N/A
lower--.f32N/A
lower-fma.f32N/A
lower-/.f32N/A
lower-*.f32N/A
lower--.f3281.9
Applied rewrites81.9%
Final simplification81.9%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (+ (- 1.0 ux) (* ux maxCos))) (t_1 (* (* (PI) uy) 2.0)))
(if (<= (* t_0 t_0) 0.9994999766349792)
(* t_1 (sqrt (- 1.0 (* (- (fma maxCos ux 1.0) ux) t_0))))
(* t_1 (sqrt (fma -2.0 (* maxCos ux) (* 2.0 ux)))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
t_1 := \left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\\
\mathbf{if}\;t\_0 \cdot t\_0 \leq 0.9994999766349792:\\
\;\;\;\;t\_1 \cdot \sqrt{1 - \left(\mathsf{fma}\left(maxCos, ux, 1\right) - ux\right) \cdot t\_0}\\
\mathbf{else}:\\
\;\;\;\;t\_1 \cdot \sqrt{\mathsf{fma}\left(-2, maxCos \cdot ux, 2 \cdot ux\right)}\\
\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.999499977Initial program 91.6%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3279.3
Applied rewrites79.3%
+-commutativeN/A
*-commutativeN/A
associate-+r-N/A
+-commutativeN/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f3279.4
Applied rewrites79.4%
if 0.999499977 < (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos))) Initial program 34.7%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3232.9
Applied rewrites32.9%
Taylor expanded in ux around 0
+-commutative7.2
*-commutative7.2
*-commutative7.2
distribute-lft-out7.2
Applied rewrites7.2%
Taylor expanded in ux around 0
lower-*.f32N/A
lower--.f32N/A
lower-*.f3277.9
Applied rewrites77.9%
Taylor expanded in maxCos around 0
lower-fma.f32N/A
lower-*.f32N/A
lower-*.f3277.9
Applied rewrites77.9%
Final simplification78.4%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (PI) uy) 2.0))
(t_1 (+ (- 1.0 ux) (* ux maxCos)))
(t_2 (fma maxCos ux (- 1.0 ux))))
(if (<= (* t_1 t_1) 0.9994999766349792)
(* t_0 (sqrt (- 1.0 (* t_2 t_2))))
(* t_0 (sqrt (fma -2.0 (* maxCos ux) (* 2.0 ux)))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\\
t_1 := \left(1 - ux\right) + ux \cdot maxCos\\
t_2 := \mathsf{fma}\left(maxCos, ux, 1 - ux\right)\\
\mathbf{if}\;t\_1 \cdot t\_1 \leq 0.9994999766349792:\\
\;\;\;\;t\_0 \cdot \sqrt{1 - t\_2 \cdot t\_2}\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \sqrt{\mathsf{fma}\left(-2, maxCos \cdot ux, 2 \cdot ux\right)}\\
\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.999499977Initial program 91.6%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3279.3
Applied rewrites79.3%
lower-*.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower--.f3279.3
Applied rewrites79.3%
if 0.999499977 < (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos))) Initial program 34.7%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3232.9
Applied rewrites32.9%
Taylor expanded in ux around 0
+-commutative7.2
*-commutative7.2
*-commutative7.2
distribute-lft-out7.2
Applied rewrites7.2%
Taylor expanded in ux around 0
lower-*.f32N/A
lower--.f32N/A
lower-*.f3277.9
Applied rewrites77.9%
Taylor expanded in maxCos around 0
lower-fma.f32N/A
lower-*.f32N/A
lower-*.f3277.9
Applied rewrites77.9%
Final simplification78.4%
(FPCore (ux uy maxCos)
:precision binary32
(*
(* 2.0 (* uy (PI)))
(sqrt
(*
(- (fma (- ux) (fma (- maxCos 2.0) maxCos 1.0) 2.0) (* maxCos 2.0))
ux))))\begin{array}{l}
\\
\left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{\left(\mathsf{fma}\left(-ux, \mathsf{fma}\left(maxCos - 2, maxCos, 1\right), 2\right) - maxCos \cdot 2\right) \cdot ux}
\end{array}
Initial program 53.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
+-commutativeN/A
associate-*r*N/A
mul-1-negN/A
lower-fma.f32N/A
lower-neg.f32N/A
lower-pow.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3298.3
Applied rewrites98.3%
Taylor expanded in maxCos around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower--.f3298.3
Applied rewrites98.3%
Taylor expanded in uy around 0
lower-*.f32N/A
lower-*.f32N/A
lower-PI.f3281.9
Applied rewrites81.9%
(FPCore (ux uy maxCos) :precision binary32 (* (* (* (PI) uy) 2.0) (sqrt (fma -2.0 (* maxCos ux) (* 2.0 ux)))))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{\mathsf{fma}\left(-2, maxCos \cdot ux, 2 \cdot ux\right)}
\end{array}
Initial program 53.3%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3248.1
Applied rewrites48.1%
Taylor expanded in ux around 0
+-commutative7.1
*-commutative7.1
*-commutative7.1
distribute-lft-out7.1
Applied rewrites7.1%
Taylor expanded in ux around 0
lower-*.f32N/A
lower--.f32N/A
lower-*.f3267.7
Applied rewrites67.7%
Taylor expanded in maxCos around 0
lower-fma.f32N/A
lower-*.f32N/A
lower-*.f3267.7
Applied rewrites67.7%
Final simplification67.7%
(FPCore (ux uy maxCos) :precision binary32 (* (* (* (PI) uy) 2.0) (sqrt (* ux (- 2.0 (* 2.0 maxCos))))))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}
\end{array}
Initial program 53.3%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3248.1
Applied rewrites48.1%
Taylor expanded in ux around 0
+-commutative7.1
*-commutative7.1
*-commutative7.1
distribute-lft-out7.1
Applied rewrites7.1%
Taylor expanded in ux around 0
lower-*.f32N/A
lower--.f32N/A
lower-*.f3267.7
Applied rewrites67.7%
Final simplification67.7%
(FPCore (ux uy maxCos) :precision binary32 (* (* (* (PI) uy) 2.0) (sqrt (* ux 2.0))))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{ux \cdot 2}
\end{array}
Initial program 53.3%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3248.1
Applied rewrites48.1%
Taylor expanded in ux around 0
+-commutative7.1
*-commutative7.1
*-commutative7.1
distribute-lft-out7.1
Applied rewrites7.1%
Taylor expanded in ux around 0
lower-*.f32N/A
lower--.f32N/A
lower-*.f3267.7
Applied rewrites67.7%
Taylor expanded in maxCos around 0
Applied rewrites65.2%
Final simplification65.2%
(FPCore (ux uy maxCos) :precision binary32 (* (* (* (PI) uy) 2.0) (sqrt (- 1.0 1.0))))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{1 - 1}
\end{array}
Initial program 53.3%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3248.1
Applied rewrites48.1%
Taylor expanded in ux around 0
+-commutative7.1
*-commutative7.1
*-commutative7.1
distribute-lft-out7.1
Applied rewrites7.1%
herbie shell --seed 2025044
(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)))))))