
(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 15 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))
(t_1 (sqrt (- 1.0 (* t_0 t_0))))
(t_2 (* (PI) uy)))
(+
(+
(* (* (cos (* (* uy 2.0) (PI))) t_1) xi)
(* (* (* 2.0 (* (sin t_2) (cos t_2))) t_1) yi))
(* (- 1.0 ux) (* (* maxCos ux) 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 := \mathsf{PI}\left(\right) \cdot uy\\
\left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot t\_1\right) \cdot xi + \left(\left(2 \cdot \left(\sin t\_2 \cdot \cos t\_2\right)\right) \cdot t\_1\right) \cdot yi\right) + \left(1 - ux\right) \cdot \left(\left(maxCos \cdot ux\right) \cdot zi\right)
\end{array}
\end{array}
Initial program 98.8%
lift-*.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
associate-*l*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f3298.8
Applied rewrites98.8%
lift-sin.f32N/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*l*N/A
lift-*.f32N/A
*-commutativeN/A
sin-2N/A
lower-*.f32N/A
lower-*.f32N/A
lower-sin.f32N/A
lower-cos.f3298.8
Applied rewrites98.8%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (- 1.0 ux) maxCos) ux)) (t_1 (* (* uy 2.0) (PI))))
(+
(+
(* (* (cos t_1) (sqrt (- 1.0 (* t_0 t_0)))) xi)
(* (* (sin t_1) (sqrt (- 1.0 (* (* maxCos ux) (* maxCos ux))))) yi))
(* (- 1.0 ux) (* (* maxCos ux) zi)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\
t_1 := \left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\\
\left(\left(\cos t\_1 \cdot \sqrt{1 - t\_0 \cdot t\_0}\right) \cdot xi + \left(\sin t\_1 \cdot \sqrt{1 - \left(maxCos \cdot ux\right) \cdot \left(maxCos \cdot ux\right)}\right) \cdot yi\right) + \left(1 - ux\right) \cdot \left(\left(maxCos \cdot ux\right) \cdot zi\right)
\end{array}
\end{array}
Initial program 98.8%
lift-*.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
associate-*l*N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f3298.8
Applied rewrites98.8%
Taylor expanded in ux around 0
unpow2N/A
unpow2N/A
unswap-sqrN/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.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)
(* (sin (* (* (PI) uy) 2.0)) yi))
(* t_0 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) + t\_0 \cdot zi
\end{array}
\end{array}
Initial program 98.8%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-sin.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/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 (* -2.0 (* (PI) uy))))
(+
(- (* xi (cos t_0)) (* (sin t_0) yi))
(* (* (* (- 1.0 ux) maxCos) ux) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -2 \cdot \left(\mathsf{PI}\left(\right) \cdot uy\right)\\
\left(xi \cdot \cos t\_0 - \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-fma.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-cos.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
*-commutativeN/A
lower-*.f32N/A
Applied rewrites32.3%
Applied rewrites98.6%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (- 1.0 ux) maxCos) ux)))
(+
(+
(* (sqrt (- 1.0 (* (* ux ux) (* maxCos maxCos)))) xi)
(* (* (sin (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* t_0 t_0)))) yi))
(* t_0 zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\
\left(\sqrt{1 - \left(ux \cdot ux\right) \cdot \left(maxCos \cdot maxCos\right)} \cdot xi + \left(\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}\right) \cdot yi\right) + t\_0 \cdot zi
\end{array}
\end{array}
Initial program 98.8%
Taylor expanded in uy around 0
lower-sqrt.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f32N/A
unpow2N/A
lower-*.f32N/A
unpow2N/A
lower-*.f3289.0
Applied rewrites89.0%
Taylor expanded in ux around 0
Applied rewrites89.0%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (- 1.0 ux) maxCos) ux)))
(+
(+
(* 1.0 xi)
(* (* (sin (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* t_0 t_0)))) yi))
(* t_0 zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\
\left(1 \cdot xi + \left(\sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}\right) \cdot yi\right) + t\_0 \cdot zi
\end{array}
\end{array}
Initial program 98.8%
Taylor expanded in uy around 0
lower-sqrt.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f32N/A
unpow2N/A
lower-*.f32N/A
unpow2N/A
lower-*.f3289.0
Applied rewrites89.0%
Taylor expanded in ux around 0
Applied rewrites89.0%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (- 1.0 ux) maxCos) ux))
(t_1 (* t_0 zi))
(t_2 (sqrt (- 1.0 (* t_0 t_0)))))
(if (<= uy 0.03500000014901161)
(+
(+
(* (* (fma (* -2.0 (* uy uy)) (* (PI) (PI)) 1.0) t_2) xi)
(* (* (* (* (PI) 2.0) uy) t_2) yi))
t_1)
(+ (* (sin (* (* (PI) uy) 2.0)) yi) t_1))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\
t_1 := t\_0 \cdot zi\\
t_2 := \sqrt{1 - t\_0 \cdot t\_0}\\
\mathbf{if}\;uy \leq 0.03500000014901161:\\
\;\;\;\;\left(\left(\mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right), 1\right) \cdot t\_2\right) \cdot xi + \left(\left(\left(\mathsf{PI}\left(\right) \cdot 2\right) \cdot uy\right) \cdot t\_2\right) \cdot yi\right) + t\_1\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot yi + t\_1\\
\end{array}
\end{array}
if uy < 0.0350000001Initial program 99.1%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites60.8%
Taylor expanded in uy around 0
Applied rewrites95.1%
Taylor expanded in uy around 0
+-commutativeN/A
associate-*r*N/A
lower-fma.f32N/A
lower-*.f32N/A
unpow2N/A
lower-*.f32N/A
unpow2N/A
lower-*.f32N/A
lower-PI.f32N/A
lower-PI.f3290.0
Applied rewrites89.6%
if 0.0350000001 < uy Initial program 97.4%
Taylor expanded in ux around 0
*-commutativeN/A
lower-fma.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-cos.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
*-commutativeN/A
lower-*.f32N/A
Applied rewrites10.3%
Taylor expanded in xi around 0
Applied rewrites61.2%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (PI) uy)) (t_1 (* (* (* (- 1.0 ux) maxCos) ux) zi)))
(if (or (<= xi -3.0000000340435383e-19) (not (<= xi 3.999999999279835e-23)))
(+ (* (cos (* -2.0 t_0)) xi) t_1)
(+ (* (sin (* t_0 2.0)) yi) t_1))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{PI}\left(\right) \cdot uy\\
t_1 := \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\\
\mathbf{if}\;xi \leq -3.0000000340435383 \cdot 10^{-19} \lor \neg \left(xi \leq 3.999999999279835 \cdot 10^{-23}\right):\\
\;\;\;\;\cos \left(-2 \cdot t\_0\right) \cdot xi + t\_1\\
\mathbf{else}:\\
\;\;\;\;\sin \left(t\_0 \cdot 2\right) \cdot yi + t\_1\\
\end{array}
\end{array}
if xi < -3.00000003e-19 or 4e-23 < xi Initial program 99.1%
Taylor expanded in ux around 0
*-commutativeN/A
lower-fma.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-cos.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
*-commutativeN/A
lower-*.f32N/A
Applied rewrites15.7%
Taylor expanded in xi around inf
Applied rewrites75.0%
if -3.00000003e-19 < xi < 4e-23Initial program 98.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-fma.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-cos.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
*-commutativeN/A
lower-*.f32N/A
Applied rewrites52.5%
Taylor expanded in xi around 0
Applied rewrites76.1%
Final simplification75.5%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ (* (cos (* -2.0 (* (PI) uy))) xi) (* (* (* (- 1.0 ux) maxCos) ux) zi)))
\begin{array}{l}
\\
\cos \left(-2 \cdot \left(\mathsf{PI}\left(\right) \cdot uy\right)\right) \cdot xi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
Initial program 98.8%
Taylor expanded in ux around 0
*-commutativeN/A
lower-fma.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
lower-cos.f32N/A
count-2-revN/A
distribute-lft-inN/A
count-2-revN/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
*-commutativeN/A
lower-*.f32N/A
Applied rewrites32.0%
Taylor expanded in xi around inf
Applied rewrites55.8%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ xi (* (* zi ux) maxCos)))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return xi + ((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 = xi + ((zi * ux) * maxcos)
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(xi + Float32(Float32(zi * ux) * maxCos)) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = xi + ((zi * ux) * maxCos); end
\begin{array}{l}
\\
xi + \left(zi \cdot ux\right) \cdot maxCos
\end{array}
Initial program 98.8%
Taylor expanded in uy around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites13.4%
Taylor expanded in ux around 0
Applied rewrites42.8%
Applied rewrites47.0%
Final simplification47.0%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ xi (* (* maxCos ux) zi)))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return xi + ((maxCos * ux) * zi);
}
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 = xi + ((maxcos * ux) * zi)
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(xi + Float32(Float32(maxCos * ux) * zi)) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = xi + ((maxCos * ux) * zi); end
\begin{array}{l}
\\
xi + \left(maxCos \cdot ux\right) \cdot zi
\end{array}
Initial program 98.8%
Taylor expanded in uy around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites13.4%
Taylor expanded in ux around 0
Applied rewrites42.8%
Applied rewrites46.9%
Final simplification46.9%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ (* (* zi maxCos) ux) xi))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return ((zi * maxCos) * ux) + 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 = ((zi * maxcos) * ux) + xi
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(Float32(zi * maxCos) * ux) + xi) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = ((zi * maxCos) * ux) + xi; end
\begin{array}{l}
\\
\left(zi \cdot maxCos\right) \cdot ux + xi
\end{array}
Initial program 98.8%
Taylor expanded in uy around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites13.4%
Taylor expanded in ux around 0
Applied rewrites42.8%
Applied rewrites46.9%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (fma maxCos (* zi ux) xi))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return fmaf(maxCos, (zi * ux), xi);
}
function code(xi, yi, zi, ux, uy, maxCos) return fma(maxCos, Float32(zi * ux), xi) end
\begin{array}{l}
\\
\mathsf{fma}\left(maxCos, zi \cdot ux, xi\right)
\end{array}
Initial program 98.8%
Taylor expanded in uy around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites13.4%
Taylor expanded in ux around 0
Applied rewrites42.8%
Applied rewrites42.8%
(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 uy around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites13.4%
Taylor expanded in ux around 0
Applied rewrites42.8%
Taylor expanded in xi around 0
Applied rewrites11.8%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* (* maxCos ux) zi))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return (maxCos * ux) * zi;
}
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 * ux) * zi
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(maxCos * ux) * zi) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = (maxCos * ux) * zi; end
\begin{array}{l}
\\
\left(maxCos \cdot ux\right) \cdot zi
\end{array}
Initial program 98.8%
Taylor expanded in uy around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites13.4%
Taylor expanded in ux around 0
Applied rewrites42.8%
Taylor expanded in xi around 0
Applied rewrites11.8%
Applied rewrites11.8%
herbie shell --seed 2024339
(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)))