
(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 9 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 (* (* maxCos (- 1.0 ux)) ux)) (t_1 (* (PI) (* 2.0 uy))))
(-
(+
(*
yi
(*
(sqrt
(*
(pow ux 4.0)
(-
(/
(-
(/ (- (/ (/ 1.0 ux) ux) (* maxCos maxCos)) ux)
(* (* maxCos maxCos) -2.0))
ux)
(* maxCos maxCos))))
(sin t_1)))
(* xi (* (sqrt (- 1.0 (* t_0 t_0))) (cos t_1))))
(* (* (* (- 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 := \mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\\
\left(yi \cdot \left(\sqrt{{ux}^{4} \cdot \left(\frac{\frac{\frac{\frac{1}{ux}}{ux} - maxCos \cdot maxCos}{ux} - \left(maxCos \cdot maxCos\right) \cdot -2}{ux} - maxCos \cdot maxCos\right)} \cdot \sin t\_1\right) + xi \cdot \left(\sqrt{1 - t\_0 \cdot t\_0} \cdot \cos t\_1\right)\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
\end{array}
Initial program 98.7%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.8%
Applied rewrites98.8%
Final simplification98.8%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(-
(+
(*
(*
(sqrt
(*
(-
(/
(-
(/ (- (/ 1.0 (* ux ux)) (* maxCos maxCos)) ux)
(* (* maxCos maxCos) -2.0))
ux)
(* maxCos maxCos))
(pow ux 4.0)))
(sin (* (PI) (* 2.0 uy))))
yi)
(* (cos (* (* (PI) uy) 2.0)) xi))
(* (* (* (- ux 1.0) maxCos) ux) zi)))\begin{array}{l}
\\
\left(\left(\sqrt{\left(\frac{\frac{\frac{1}{ux \cdot ux} - maxCos \cdot maxCos}{ux} - \left(maxCos \cdot maxCos\right) \cdot -2}{ux} - maxCos \cdot maxCos\right) \cdot {ux}^{4}} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\right) \cdot yi + \cos \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot xi\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
Initial program 98.7%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.8%
Taylor expanded in maxCos around 0
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3298.8
Applied rewrites98.8%
Final simplification98.8%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* maxCos (- 1.0 ux)) ux))
(t_1 (sqrt (- 1.0 (* t_0 t_0))))
(t_2 (* (PI) (* 2.0 uy))))
(-
(+ (* (* (sin t_2) t_1) yi) (* xi (* t_1 (cos t_2))))
(* (* (* (- 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 := \sqrt{1 - t\_0 \cdot t\_0}\\
t_2 := \mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\\
\left(\left(\sin t\_2 \cdot t\_1\right) \cdot yi + xi \cdot \left(t\_1 \cdot \cos t\_2\right)\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
\end{array}
Initial program 98.7%
Final simplification98.7%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (PI) (* 2.0 uy))) (t_1 (* (* maxCos (- 1.0 ux)) ux)))
(-
(+
(* (* (sqrt (* (/ 1.0 (* ux ux)) (* ux ux))) (sin t_0)) yi)
(* xi (* (sqrt (- 1.0 (* t_1 t_1))) (cos t_0))))
(* (* (* (- ux 1.0) maxCos) ux) zi))))\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\\
\left(\left(\sqrt{\frac{1}{ux \cdot ux} \cdot \left(ux \cdot ux\right)} \cdot \sin t\_0\right) \cdot yi + xi \cdot \left(\sqrt{1 - t\_1 \cdot t\_1} \cdot \cos 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.7%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.8%
Applied rewrites98.6%
Taylor expanded in maxCos around 0
Applied rewrites98.6%
Final simplification98.6%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (- (+ (* (* (sqrt 1.0) (sin (* (PI) (* 2.0 uy)))) yi) (* (cos (* (* (PI) uy) 2.0)) xi)) (* (* (* (- ux 1.0) maxCos) ux) zi)))
\begin{array}{l}
\\
\left(\left(\sqrt{1} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\right) \cdot yi + \cos \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot xi\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
Initial program 98.7%
Taylor expanded in ux around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.8%
Taylor expanded in maxCos around 0
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3298.8
Applied rewrites98.8%
Taylor expanded in maxCos around 0
Applied rewrites98.6%
Final simplification98.6%
(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.7%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3290.7
Applied rewrites90.7%
Taylor expanded in maxCos around 0
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3290.7
Applied rewrites90.7%
Final simplification90.7%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (if (<= (* 2.0 uy) 0.05299999937415123) (+ (fma (* zi ux) maxCos (* (* (* yi (PI)) uy) 2.0)) xi) (* (cos (* (* (PI) uy) 2.0)) xi)))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;2 \cdot uy \leq 0.05299999937415123:\\
\;\;\;\;\mathsf{fma}\left(zi \cdot ux, maxCos, \left(\left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right) \cdot 2\right) + xi\\
\mathbf{else}:\\
\;\;\;\;\cos \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot xi\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 0.0529999994Initial program 99.0%
Taylor expanded in xi around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites10.6%
Taylor expanded in uy around 0
Applied rewrites11.7%
Taylor expanded in ux around 0
Applied rewrites84.0%
if 0.0529999994 < (*.f32 uy #s(literal 2 binary32)) Initial program 97.6%
lift-*.f32N/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lift-PI.f32N/A
lower-cbrt.f3297.1
Applied rewrites97.1%
Taylor expanded in xi around inf
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
Applied rewrites56.7%
Taylor expanded in maxCos around 0
Applied rewrites56.7%
Final simplification68.4%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ (fma (* zi ux) maxCos (* (* (* yi (PI)) uy) 2.0)) xi))
\begin{array}{l}
\\
\mathsf{fma}\left(zi \cdot ux, maxCos, \left(\left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right) \cdot 2\right) + xi
\end{array}
Initial program 98.7%
Taylor expanded in xi around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites10.1%
Taylor expanded in uy around 0
Applied rewrites13.1%
Taylor expanded in ux around 0
Applied rewrites72.6%
Final simplification67.2%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* 1.0 xi))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return 1.0f * xi;
}
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 = 1.0e0 * xi
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(1.0) * xi) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = single(1.0) * xi; end
\begin{array}{l}
\\
1 \cdot xi
\end{array}
Initial program 98.7%
Taylor expanded in xi around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites10.1%
Taylor expanded in uy around 0
Applied rewrites45.0%
Taylor expanded in maxCos around 0
Applied rewrites45.0%
herbie shell --seed 2024270
(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)))