
(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 11 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 (* (* (- 1.0 ux) maxCos) ux)))
(+
(+
(* (* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* t_0 t_0)))) xi)
(* (sin (* (* (PI) uy) 2.0)) yi))
(* (* (* ux (- 1.0 ux)) maxCos) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\
\left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \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(ux \cdot \left(1 - ux\right)\right) \cdot maxCos\right) \cdot zi
\end{array}
\end{array}
Initial program 98.8%
Taylor expanded in ux 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%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f3298.9
Applied rewrites98.9%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (PI) uy) 2.0)))
(+
(+ (* (cos t_0) xi) (* (sin t_0) yi))
(* (* (* (- 1.0 ux) maxCos) ux) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\\
\left(\cos t\_0 \cdot xi + \sin t\_0 \cdot yi\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
\end{array}
Initial program 98.8%
Taylor expanded in ux 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 ux around 0
*-commutativeN/A
lower-*.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3298.8
Applied rewrites98.8%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (- 1.0 ux) maxCos) ux)))
(+
(+
(* (* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* t_0 t_0)))) xi)
(* (* (* yi (PI)) uy) 2.0))
(* (* (* ux (- 1.0 ux)) maxCos) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\
\left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}\right) \cdot xi + \left(\left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right) \cdot 2\right) + \left(\left(ux \cdot \left(1 - ux\right)\right) \cdot maxCos\right) \cdot zi
\end{array}
\end{array}
Initial program 98.8%
Taylor expanded in ux 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%
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f3298.9
Applied rewrites98.9%
Taylor expanded in uy around 0
Applied rewrites89.4%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (PI) uy) 2.0)))
(if (or (<= yi -8.499999921696144e-7) (not (<= yi 9.9999998245167e-14)))
(* (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\\
\mathbf{if}\;yi \leq -8.499999921696144 \cdot 10^{-7} \lor \neg \left(yi \leq 9.9999998245167 \cdot 10^{-14}\right):\\
\;\;\;\;\sin t\_0 \cdot yi\\
\mathbf{else}:\\
\;\;\;\;\cos t\_0 \cdot xi\\
\end{array}
\end{array}
if yi < -8.49999992e-7 or 9.99999982e-14 < yi Initial program 98.6%
Taylor expanded in maxCos around 0
+-commutativeN/A
associate-+l+N/A
*-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-fma.f32N/A
Applied rewrites8.5%
Taylor expanded in maxCos around inf
Applied rewrites12.6%
Taylor expanded in yi around inf
Applied rewrites78.7%
if -8.49999992e-7 < yi < 9.99999982e-14Initial program 98.9%
Taylor expanded in maxCos around 0
+-commutativeN/A
associate-+l+N/A
*-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-fma.f32N/A
Applied rewrites12.7%
Taylor expanded in xi around inf
Applied rewrites68.1%
Final simplification71.2%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (if (or (<= yi -8.499999921696144e-7) (not (<= yi 9.9999998245167e-14))) (* (sin (* (* (PI) uy) 2.0)) yi) (+ (fma (* (* yi (PI)) uy) 2.0 (* (* (* zi (- 1.0 ux)) ux) maxCos)) xi)))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;yi \leq -8.499999921696144 \cdot 10^{-7} \lor \neg \left(yi \leq 9.9999998245167 \cdot 10^{-14}\right):\\
\;\;\;\;\sin \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot yi\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy, 2, \left(\left(zi \cdot \left(1 - ux\right)\right) \cdot ux\right) \cdot maxCos\right) + xi\\
\end{array}
\end{array}
if yi < -8.49999992e-7 or 9.99999982e-14 < yi Initial program 98.6%
Taylor expanded in maxCos around 0
+-commutativeN/A
associate-+l+N/A
*-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-fma.f32N/A
Applied rewrites8.4%
Taylor expanded in maxCos around inf
Applied rewrites12.5%
Taylor expanded in yi around inf
Applied rewrites78.7%
if -8.49999992e-7 < yi < 9.99999982e-14Initial program 98.9%
Taylor expanded in maxCos around 0
+-commutativeN/A
associate-+l+N/A
*-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-fma.f32N/A
Applied rewrites12.7%
Taylor expanded in uy around 0
Applied rewrites65.3%
Final simplification60.9%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(if (or (<= yi -8.499999921696144e-7) (not (<= yi 9.9999998245167e-14)))
(fma
(fma (* (- 1.0 ux) zi) ux (/ xi maxCos))
maxCos
(* (* (* (PI) yi) uy) 2.0))
(+ (fma (* (* yi (PI)) uy) 2.0 (* (* (* zi (- 1.0 ux)) ux) maxCos)) xi)))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;yi \leq -8.499999921696144 \cdot 10^{-7} \lor \neg \left(yi \leq 9.9999998245167 \cdot 10^{-14}\right):\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\left(1 - ux\right) \cdot zi, ux, \frac{xi}{maxCos}\right), maxCos, \left(\left(\mathsf{PI}\left(\right) \cdot yi\right) \cdot uy\right) \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy, 2, \left(\left(zi \cdot \left(1 - ux\right)\right) \cdot ux\right) \cdot maxCos\right) + xi\\
\end{array}
\end{array}
if yi < -8.49999992e-7 or 9.99999982e-14 < yi Initial program 98.6%
Taylor expanded in maxCos around 0
+-commutativeN/A
associate-+l+N/A
*-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-fma.f32N/A
Applied rewrites8.4%
Taylor expanded in maxCos around inf
Applied rewrites12.5%
Taylor expanded in uy around 0
Applied rewrites58.7%
if -8.49999992e-7 < yi < 9.99999982e-14Initial program 98.9%
Taylor expanded in maxCos around 0
+-commutativeN/A
associate-+l+N/A
*-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-fma.f32N/A
Applied rewrites12.7%
Taylor expanded in uy around 0
Applied rewrites65.3%
Final simplification63.7%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ (fma (* (* yi (PI)) uy) 2.0 (* (* (* zi (- 1.0 ux)) ux) maxCos)) xi))
\begin{array}{l}
\\
\mathsf{fma}\left(\left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy, 2, \left(\left(zi \cdot \left(1 - ux\right)\right) \cdot ux\right) \cdot maxCos\right) + xi
\end{array}
Initial program 98.8%
Taylor expanded in maxCos around 0
+-commutativeN/A
associate-+l+N/A
*-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-fma.f32N/A
Applied rewrites11.4%
Taylor expanded in uy around 0
Applied rewrites51.1%
Final simplification51.1%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (fma (* maxCos ux) (* zi (- 1.0 ux)) xi))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return fmaf((maxCos * ux), (zi * (1.0f - ux)), xi);
}
function code(xi, yi, zi, ux, uy, maxCos) return fma(Float32(maxCos * ux), Float32(zi * Float32(Float32(1.0) - ux)), xi) end
\begin{array}{l}
\\
\mathsf{fma}\left(maxCos \cdot ux, zi \cdot \left(1 - ux\right), xi\right)
\end{array}
Initial program 98.8%
Taylor expanded in maxCos around 0
+-commutativeN/A
associate-+l+N/A
*-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-fma.f32N/A
Applied rewrites11.4%
Taylor expanded in uy around 0
Applied rewrites44.1%
Final simplification44.2%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* (* (* (- 1.0 ux) zi) ux) maxCos))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return (((1.0f - ux) * 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 = (((1.0e0 - ux) * zi) * ux) * maxcos
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(Float32(Float32(Float32(1.0) - ux) * zi) * ux) * maxCos) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = (((single(1.0) - ux) * zi) * ux) * maxCos; end
\begin{array}{l}
\\
\left(\left(\left(1 - ux\right) \cdot zi\right) \cdot ux\right) \cdot maxCos
\end{array}
Initial program 98.8%
Taylor expanded in zi around inf
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3214.0
Applied rewrites14.0%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* (* maxCos (* zi (- 1.0 ux))) ux))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return (maxCos * (zi * (1.0f - ux))) * ux;
}
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 = (maxcos * (zi * (1.0e0 - ux))) * ux
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(maxCos * Float32(zi * Float32(Float32(1.0) - ux))) * ux) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = (maxCos * (zi * (single(1.0) - ux))) * ux; end
\begin{array}{l}
\\
\left(maxCos \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) \cdot ux
\end{array}
Initial program 98.8%
Taylor expanded in zi around inf
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3214.0
Applied rewrites14.0%
Applied rewrites14.0%
(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.8%
Taylor expanded in zi around inf
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3214.0
Applied rewrites14.0%
Taylor expanded in ux around 0
Applied rewrites12.2%
herbie shell --seed 2024324
(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)))