
(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 (cbrt (PI))) (t_1 (* (* maxCos (- 1.0 ux)) ux)))
(-
(+
(* (sin (* (* uy (PI)) 2.0)) yi)
(*
xi
(*
(sqrt (- 1.0 (* t_1 t_1)))
(cos (* (* (* uy 2.0) t_0) (pow t_0 2.0))))))
(* (* (* (- ux 1.0) maxCos) ux) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\mathsf{PI}\left(\right)}\\
t_1 := \left(maxCos \cdot \left(1 - ux\right)\right) \cdot ux\\
\left(\sin \left(\left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot 2\right) \cdot yi + xi \cdot \left(\sqrt{1 - t\_1 \cdot t\_1} \cdot \cos \left(\left(\left(uy \cdot 2\right) \cdot t\_0\right) \cdot {t\_0}^{2}\right)\right)\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
\end{array}
Initial program 99.0%
lift-*.f32N/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f3299.0
lift-*.f32N/A
*-commutativeN/A
lower-*.f3299.0
Applied rewrites99.0%
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.f3299.0
Applied rewrites99.0%
Final simplification99.0%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* maxCos (- 1.0 ux)) ux)))
(-
(+
(* (* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* t_0 t_0)))) xi)
(* (sin (* (* uy (PI)) 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(\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(uy \cdot \mathsf{PI}\left(\right)\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 99.0%
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.f3299.0
Applied rewrites99.0%
Final simplification99.0%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* uy (PI)) 2.0)))
(-
(+ (* (cos t_0) xi) (* (sin t_0) yi))
(* (* (* (- ux 1.0) maxCos) ux) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot 2\\
\left(\cos t\_0 \cdot xi + \sin t\_0 \cdot yi\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
\end{array}
Initial program 99.0%
lift-*.f32N/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f3299.0
lift-*.f32N/A
*-commutativeN/A
lower-*.f3299.0
Applied rewrites99.0%
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.f3299.0
Applied rewrites99.0%
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.f3299.0
Applied rewrites99.0%
Final simplification99.0%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (sqrt (PI))) (t_1 (* (* maxCos (- 1.0 ux)) ux)))
(-
(+
(* (* (* yi (PI)) uy) 2.0)
(* (* (cos (* (* t_0 (* uy 2.0)) t_0)) (sqrt (- 1.0 (* t_1 t_1)))) xi))
(* (* (* (- ux 1.0) maxCos) ux) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\mathsf{PI}\left(\right)}\\
t_1 := \left(maxCos \cdot \left(1 - ux\right)\right) \cdot ux\\
\left(\left(\left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right) \cdot 2 + \left(\cos \left(\left(t\_0 \cdot \left(uy \cdot 2\right)\right) \cdot t\_0\right) \cdot \sqrt{1 - t\_1 \cdot t\_1}\right) \cdot xi\right) - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
\end{array}
Initial program 99.0%
lift-*.f32N/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f3299.0
lift-*.f32N/A
*-commutativeN/A
lower-*.f3299.0
Applied rewrites99.0%
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.f3299.0
Applied rewrites99.0%
lift-*.f32N/A
lift-*.f32N/A
associate-*r*N/A
lift-pow.f32N/A
unpow2N/A
pow3N/A
lift-cbrt.f32N/A
rem-cube-cbrtN/A
*-commutativeN/A
lift-PI.f32N/A
add-sqr-sqrtN/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lift-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lift-PI.f32N/A
lower-sqrt.f32N/A
lift-PI.f32N/A
lower-sqrt.f3299.0
Applied rewrites99.0%
Taylor expanded in uy around 0
Applied rewrites89.2%
Final simplification89.2%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (sin (* (* uy (PI)) 2.0)) yi)))
(if (<= yi -9.9999998245167e-15)
t_0
(if (<= yi 4.99999991225835e-14)
(-
(*
(sqrt
(- 1.0 (* (* maxCos maxCos) (* (* ux ux) (pow (- 1.0 ux) 2.0)))))
xi)
(* (* (* (- ux 1.0) maxCos) ux) zi))
t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot 2\right) \cdot yi\\
\mathbf{if}\;yi \leq -9.9999998245167 \cdot 10^{-15}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;yi \leq 4.99999991225835 \cdot 10^{-14}:\\
\;\;\;\;\sqrt{1 - \left(maxCos \cdot maxCos\right) \cdot \left(\left(ux \cdot ux\right) \cdot {\left(1 - ux\right)}^{2}\right)} \cdot xi - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if yi < -9.99999982e-15 or 4.99999991e-14 < yi Initial program 98.8%
Taylor expanded in yi around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites5.2%
Taylor expanded in ux around 0
Applied rewrites64.2%
if -9.99999982e-15 < yi < 4.99999991e-14Initial program 99.1%
lift-*.f32N/A
*-commutativeN/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-*l*N/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-*.f32N/A
lift-PI.f32N/A
lower-cbrt.f3299.1
lift-*.f32N/A
*-commutativeN/A
lower-*.f3299.1
Applied rewrites99.1%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites75.4%
Final simplification70.8%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (sin (* (* uy (PI)) 2.0)) yi)))
(if (<= yi -9.9999998245167e-15)
t_0
(if (<= yi 4.99999991225835e-14)
(-
(*
(sqrt
(fma (* (- maxCos) maxCos) (* (* ux ux) (pow (- 1.0 ux) 2.0)) 1.0))
xi)
(* (* (* (- ux 1.0) maxCos) ux) zi))
t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot 2\right) \cdot yi\\
\mathbf{if}\;yi \leq -9.9999998245167 \cdot 10^{-15}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;yi \leq 4.99999991225835 \cdot 10^{-14}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\left(-maxCos\right) \cdot maxCos, \left(ux \cdot ux\right) \cdot {\left(1 - ux\right)}^{2}, 1\right)} \cdot xi - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if yi < -9.99999982e-15 or 4.99999991e-14 < yi Initial program 98.8%
Taylor expanded in yi around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites5.1%
Taylor expanded in ux around 0
Applied rewrites64.2%
if -9.99999982e-15 < yi < 4.99999991e-14Initial program 99.1%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites8.4%
Final simplification31.3%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* (sin (* (* uy (PI)) 2.0)) yi))
\begin{array}{l}
\\
\sin \left(\left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot 2\right) \cdot yi
\end{array}
Initial program 99.0%
Taylor expanded in yi around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites6.1%
Taylor expanded in ux around 0
Applied rewrites35.1%
Final simplification35.1%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* (fma (* (+ -0.5 ux) (* maxCos maxCos)) (* ux ux) 1.0) (* (* (* yi (PI)) uy) 2.0)))
\begin{array}{l}
\\
\mathsf{fma}\left(\left(-0.5 + ux\right) \cdot \left(maxCos \cdot maxCos\right), ux \cdot ux, 1\right) \cdot \left(\left(\left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right) \cdot 2\right)
\end{array}
Initial program 99.0%
Taylor expanded in yi around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites5.7%
Taylor expanded in ux around 0
Applied rewrites19.4%
Taylor expanded in uy around 0
Applied rewrites28.4%
Final simplification28.4%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* (* (* maxCos (- 1.0 ux)) zi) ux))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return ((maxCos * (1.0f - ux)) * zi) * 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 * (1.0e0 - ux)) * zi) * ux
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(Float32(maxCos * Float32(Float32(1.0) - ux)) * zi) * ux) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = ((maxCos * (single(1.0) - ux)) * zi) * ux; end
\begin{array}{l}
\\
\left(\left(maxCos \cdot \left(1 - ux\right)\right) \cdot zi\right) \cdot ux
\end{array}
Initial program 99.0%
Taylor expanded in zi around inf
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3213.6
Applied rewrites13.6%
Applied rewrites13.6%
Final simplification13.6%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* (* zi ux) (* maxCos (- 1.0 ux))))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return (zi * ux) * (maxCos * (1.0f - 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 = (zi * ux) * (maxcos * (1.0e0 - ux))
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(zi * ux) * Float32(maxCos * Float32(Float32(1.0) - ux))) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = (zi * ux) * (maxCos * (single(1.0) - ux)); end
\begin{array}{l}
\\
\left(zi \cdot ux\right) \cdot \left(maxCos \cdot \left(1 - ux\right)\right)
\end{array}
Initial program 99.0%
Taylor expanded in zi around inf
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3213.6
Applied rewrites13.6%
Applied rewrites13.6%
(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 99.0%
Taylor expanded in zi around inf
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3213.6
Applied rewrites13.6%
Taylor expanded in ux around 0
Applied rewrites12.4%
herbie shell --seed 2024283
(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)))