
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (- 1.0 ux) maxCos) ux))
(t_1 (sqrt (- 1.0 (* t_0 t_0))))
(t_2 (* (* uy 2.0) (PI))))
(+ (+ (* (* (cos t_2) t_1) xi) (* (* (sin t_2) t_1) yi)) (* t_0 zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\
t_1 := \sqrt{1 - t\_0 \cdot t\_0}\\
t_2 := \left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\\
\left(\left(\cos t\_2 \cdot t\_1\right) \cdot xi + \left(\sin t\_2 \cdot t\_1\right) \cdot yi\right) + t\_0 \cdot zi
\end{array}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 12 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (- 1.0 ux) maxCos) ux))
(t_1 (sqrt (- 1.0 (* t_0 t_0))))
(t_2 (* (* uy 2.0) (PI))))
(+ (+ (* (* (cos t_2) t_1) xi) (* (* (sin t_2) t_1) yi)) (* t_0 zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\
t_1 := \sqrt{1 - t\_0 \cdot t\_0}\\
t_2 := \left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\\
\left(\left(\cos t\_2 \cdot t\_1\right) \cdot xi + \left(\sin t\_2 \cdot t\_1\right) \cdot yi\right) + t\_0 \cdot zi
\end{array}
\end{array}
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (PI) (* 2.0 uy)))
(t_1 (* (* maxCos (- 1.0 ux)) ux))
(t_2 (sqrt (- 1.0 (* t_1 t_1)))))
(+
(* zi (* (- maxCos (* maxCos ux)) ux))
(+ (* yi (* (sin t_0) t_2)) (* xi (* t_2 (cos t_0)))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\\
t_1 := \left(maxCos \cdot \left(1 - ux\right)\right) \cdot ux\\
t_2 := \sqrt{1 - t\_1 \cdot t\_1}\\
zi \cdot \left(\left(maxCos - maxCos \cdot ux\right) \cdot ux\right) + \left(yi \cdot \left(\sin t\_0 \cdot t\_2\right) + xi \cdot \left(t\_2 \cdot \cos t\_0\right)\right)
\end{array}
\end{array}
Initial program 98.9%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3298.9
Applied rewrites98.9%
Final simplification98.9%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* maxCos (- 1.0 ux)) ux)))
(-
(+
(* (cos (* (* (PI) uy) 2.0)) xi)
(* yi (* (sin (* (PI) (* 2.0 uy))) (sqrt (- 1.0 (* t_0 t_0))))))
(* (* (* (- ux 1.0) maxCos) ux) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(maxCos \cdot \left(1 - ux\right)\right) \cdot ux\\
\left(\cos \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot xi + yi \cdot \left(\sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}\right)\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
\end{array}
Initial program 98.9%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3298.9
Applied rewrites98.9%
Final simplification98.9%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (PI) uy) 2.0))
(t_1 (* (* maxCos (- 1.0 ux)) ux))
(t_2 (sqrt (- 1.0 (* t_1 t_1)))))
(if (<= (* 2.0 uy) 0.00047759999870322645)
(+
(+ (* (* t_0 t_2) yi) (* xi (* t_2 (cos (* (PI) (* 2.0 uy))))))
(* zi (* (- maxCos (* maxCos ux)) ux)))
(+ (* (sin t_0) yi) (* (cos t_0) xi)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\\
t_1 := \left(maxCos \cdot \left(1 - ux\right)\right) \cdot ux\\
t_2 := \sqrt{1 - t\_1 \cdot t\_1}\\
\mathbf{if}\;2 \cdot uy \leq 0.00047759999870322645:\\
\;\;\;\;\left(\left(t\_0 \cdot t\_2\right) \cdot yi + xi \cdot \left(t\_2 \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\right)\right) + zi \cdot \left(\left(maxCos - maxCos \cdot ux\right) \cdot ux\right)\\
\mathbf{else}:\\
\;\;\;\;\sin t\_0 \cdot yi + \cos t\_0 \cdot xi\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 4.77599999e-4Initial program 99.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f3299.3
Applied rewrites99.3%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3299.2
Applied rewrites99.2%
if 4.77599999e-4 < (*.f32 uy #s(literal 2 binary32)) Initial program 98.2%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-fma.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3211.9
Applied rewrites12.7%
Applied rewrites93.3%
Final simplification97.2%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (PI) uy) 2.0)))
(-
(+ (* (sin t_0) yi) (* (cos t_0) xi))
(* (* (* (- ux 1.0) maxCos) ux) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\\
\left(\sin t\_0 \cdot yi + \cos t\_0 \cdot xi\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
\end{array}
Initial program 98.9%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3298.9
Applied rewrites98.9%
Taylor expanded in maxCos around 0
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3298.9
Applied rewrites98.9%
Final simplification98.9%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (PI) uy) 2.0))
(t_1 (* (* maxCos (- 1.0 ux)) ux))
(t_2 (* (cos t_0) xi)))
(if (<= (* 2.0 uy) 0.00047759999870322645)
(-
(+ (* (* t_0 (sqrt (- 1.0 (* t_1 t_1)))) yi) t_2)
(* (* (* (- ux 1.0) maxCos) ux) zi))
(+ (* (sin t_0) yi) t_2))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\\
t_1 := \left(maxCos \cdot \left(1 - ux\right)\right) \cdot ux\\
t_2 := \cos t\_0 \cdot xi\\
\mathbf{if}\;2 \cdot uy \leq 0.00047759999870322645:\\
\;\;\;\;\left(\left(t\_0 \cdot \sqrt{1 - t\_1 \cdot t\_1}\right) \cdot yi + t\_2\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\\
\mathbf{else}:\\
\;\;\;\;\sin t\_0 \cdot yi + t\_2\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 4.77599999e-4Initial program 99.3%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3299.2
Applied rewrites99.2%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3299.2
Applied rewrites99.2%
if 4.77599999e-4 < (*.f32 uy #s(literal 2 binary32)) Initial program 98.2%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-fma.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3211.9
Applied rewrites11.9%
Applied rewrites93.3%
Final simplification97.2%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* maxCos (- 1.0 ux)) ux)) (t_1 (* (* (PI) uy) 2.0)))
(-
(+ (* (* t_1 (sqrt (- 1.0 (* t_0 t_0)))) yi) (* (cos t_1) xi))
(* (* (* (- ux 1.0) maxCos) ux) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(maxCos \cdot \left(1 - ux\right)\right) \cdot ux\\
t_1 := \left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\\
\left(\left(t\_1 \cdot \sqrt{1 - t\_0 \cdot t\_0}\right) \cdot yi + \cos t\_1 \cdot xi\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
\end{array}
Initial program 98.9%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3291.2
Applied rewrites91.2%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3291.2
Applied rewrites91.2%
Final simplification91.2%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* maxCos (- 1.0 ux)) ux)))
(-
(+
(* (* 1.0 (sqrt (- 1.0 (* t_0 t_0)))) xi)
(* (sin (* (* (PI) uy) 2.0)) yi))
(* (* (* (- ux 1.0) maxCos) ux) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(maxCos \cdot \left(1 - ux\right)\right) \cdot ux\\
\left(\left(1 \cdot \sqrt{1 - t\_0 \cdot t\_0}\right) \cdot xi + \sin \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot yi\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
\end{array}
Initial program 98.9%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3298.9
Applied rewrites98.9%
Taylor expanded in uy around 0
Applied rewrites88.5%
Final simplification88.5%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (PI) uy) 2.0)))
(if (<= (* 2.0 uy) 0.028999999165534973)
(+ (* t_0 yi) xi)
(* (cos t_0) xi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\\
\mathbf{if}\;2 \cdot uy \leq 0.028999999165534973:\\
\;\;\;\;t\_0 \cdot yi + xi\\
\mathbf{else}:\\
\;\;\;\;\cos t\_0 \cdot xi\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 0.0289999992Initial program 99.2%
Taylor expanded in uy around 0
+-commutativeN/A
associate-+r+N/A
associate-*r*N/A
distribute-rgt-outN/A
Applied rewrites6.4%
Taylor expanded in maxCos around 0
Applied rewrites51.9%
Applied rewrites86.9%
if 0.0289999992 < (*.f32 uy #s(literal 2 binary32)) Initial program 97.8%
Taylor expanded in maxCos around 0
*-commutativeN/A
lower-fma.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f326.5
Applied rewrites6.5%
Taylor expanded in xi around inf
Applied rewrites48.7%
Final simplification79.7%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* yi (PI)) uy)) (t_1 (fma 2.0 t_0 xi)))
(if (<= xi -2.0000000390829628e-25)
t_1
(if (<= xi 1.000000031374395e-22) (* t_0 2.0) t_1))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy\\
t_1 := \mathsf{fma}\left(2, t\_0, xi\right)\\
\mathbf{if}\;xi \leq -2.0000000390829628 \cdot 10^{-25}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;xi \leq 1.000000031374395 \cdot 10^{-22}:\\
\;\;\;\;t\_0 \cdot 2\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if xi < -2.00000004e-25 or 1.00000003e-22 < xi Initial program 99.0%
Taylor expanded in uy around 0
+-commutativeN/A
associate-+r+N/A
associate-*r*N/A
distribute-rgt-outN/A
Applied rewrites5.2%
Taylor expanded in maxCos around 0
Applied rewrites62.5%
Applied rewrites62.5%
if -2.00000004e-25 < xi < 1.00000003e-22Initial program 98.7%
Taylor expanded in uy around 0
+-commutativeN/A
associate-+r+N/A
associate-*r*N/A
distribute-rgt-outN/A
Applied rewrites9.0%
Taylor expanded in maxCos around 0
Applied rewrites21.8%
Taylor expanded in xi around 0
Applied rewrites52.0%
Final simplification58.4%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ (* (* (* (PI) uy) 2.0) yi) xi))
\begin{array}{l}
\\
\left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot yi + xi
\end{array}
Initial program 98.9%
Taylor expanded in uy around 0
+-commutativeN/A
associate-+r+N/A
associate-*r*N/A
distribute-rgt-outN/A
Applied rewrites6.7%
Taylor expanded in maxCos around 0
Applied rewrites46.4%
Applied rewrites76.4%
Final simplification76.4%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* (* (* yi (PI)) uy) 2.0))
\begin{array}{l}
\\
\left(\left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right) \cdot 2
\end{array}
Initial program 98.9%
Taylor expanded in uy around 0
+-commutativeN/A
associate-+r+N/A
associate-*r*N/A
distribute-rgt-outN/A
Applied rewrites6.8%
Taylor expanded in maxCos around 0
Applied rewrites46.4%
Taylor expanded in xi around 0
Applied rewrites32.4%
Final simplification32.4%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* (* zi ux) maxCos))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return (zi * ux) * maxCos;
}
real(4) function code(xi, yi, zi, ux, uy, maxcos)
real(4), intent (in) :: xi
real(4), intent (in) :: yi
real(4), intent (in) :: zi
real(4), intent (in) :: ux
real(4), intent (in) :: uy
real(4), intent (in) :: maxcos
code = (zi * ux) * maxcos
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(zi * ux) * maxCos) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = (zi * ux) * maxCos; end
\begin{array}{l}
\\
\left(zi \cdot ux\right) \cdot maxCos
\end{array}
Initial program 98.9%
Taylor expanded in zi around inf
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-*.f32N/A
lower--.f3212.8
Applied rewrites12.8%
Taylor expanded in ux around 0
Applied rewrites11.3%
herbie shell --seed 2024264
(FPCore (xi yi zi ux uy maxCos)
:name "UniformSampleCone 2"
:precision binary32
:pre (and (and (and (and (and (and (<= -10000.0 xi) (<= xi 10000.0)) (and (<= -10000.0 yi) (<= yi 10000.0))) (and (<= -10000.0 zi) (<= zi 10000.0))) (and (<= 2.328306437e-10 ux) (<= ux 1.0))) (and (<= 2.328306437e-10 uy) (<= uy 1.0))) (and (<= 0.0 maxCos) (<= maxCos 1.0)))
(+ (+ (* (* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* (* (* (- 1.0 ux) maxCos) ux) (* (* (- 1.0 ux) maxCos) ux))))) xi) (* (* (sin (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* (* (* (- 1.0 ux) maxCos) ux) (* (* (- 1.0 ux) maxCos) ux))))) yi)) (* (* (* (- 1.0 ux) maxCos) ux) zi)))