
(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 (* (PI) (* uy 2.0))))
(+
(- (* (cos t_0) xi) (* (* (* (- ux 1.0) maxCos) ux) zi))
(*
(sin t_0)
(* (sqrt (- 1.0 (pow (* (* maxCos (- 1.0 ux)) ux) 2.0))) yi)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{PI}\left(\right) \cdot \left(uy \cdot 2\right)\\
\left(\cos t\_0 \cdot xi - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right) + \sin t\_0 \cdot \left(\sqrt{1 - {\left(\left(maxCos \cdot \left(1 - ux\right)\right) \cdot ux\right)}^{2}} \cdot yi\right)
\end{array}
\end{array}
Initial program 99.1%
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.1
Applied rewrites99.1%
lift-+.f32N/A
Applied rewrites99.1%
Final simplification99.1%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* maxCos (- 1.0 ux)) ux)))
(-
(+
(* (* (sqrt (- 1.0 (* t_0 t_0))) (sin (* (PI) (* uy 2.0)))) yi)
(* (cos (* (* (PI) uy) 2.0)) 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\\
\left(\left(\sqrt{1 - t\_0 \cdot t\_0} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(uy \cdot 2\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}
\end{array}
Initial program 99.1%
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.1
Applied rewrites99.1%
Final simplification99.1%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ (* (sin (* (* (PI) uy) 2.0)) yi) (- (* (cos (* (PI) (* uy 2.0))) xi) (* (* (* (- ux 1.0) maxCos) ux) zi))))
\begin{array}{l}
\\
\sin \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot yi + \left(\cos \left(\mathsf{PI}\left(\right) \cdot \left(uy \cdot 2\right)\right) \cdot xi - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right)
\end{array}
Initial program 99.1%
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.1
Applied rewrites99.1%
lift-+.f32N/A
Applied rewrites99.1%
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%
Final simplification98.9%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* maxCos (- 1.0 ux)) ux)))
(-
(+
(fma (* (* uy uy) -2.0) (* (* (PI) (PI)) xi) xi)
(* (* (sqrt (- 1.0 (* t_0 t_0))) (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(\mathsf{fma}\left(\left(uy \cdot uy\right) \cdot -2, \left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot xi, xi\right) + \left(\sqrt{1 - t\_0 \cdot t\_0} \cdot \sin \left(\mathsf{PI}\left(\right) \cdot \left(uy \cdot 2\right)\right)\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.1%
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.1
Applied rewrites99.1%
Taylor expanded in uy around 0
Applied rewrites89.9%
Final simplification55.5%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ (* (* (* zi (- 1.0 ux)) ux) maxCos) (* (fma (* (PI) yi) (* uy 2.0) xi) (sqrt (- 1.0 (pow (* (* maxCos (- 1.0 ux)) ux) 2.0))))))
\begin{array}{l}
\\
\left(\left(zi \cdot \left(1 - ux\right)\right) \cdot ux\right) \cdot maxCos + \mathsf{fma}\left(\mathsf{PI}\left(\right) \cdot yi, uy \cdot 2, xi\right) \cdot \sqrt{1 - {\left(\left(maxCos \cdot \left(1 - ux\right)\right) \cdot ux\right)}^{2}}
\end{array}
Initial program 99.1%
Taylor expanded in uy around 0
+-commutativeN/A
associate-+r+N/A
associate-*r*N/A
distribute-rgt-outN/A
Applied rewrites7.3%
Applied rewrites57.0%
Final simplification57.0%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (- (* (sqrt 1.0) xi) (* (* (* (- ux 1.0) maxCos) ux) zi)))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return (sqrtf(1.0f) * xi) - ((((ux - 1.0f) * 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 = (sqrt(1.0e0) * xi) - ((((ux - 1.0e0) * maxcos) * ux) * zi)
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(sqrt(Float32(1.0)) * xi) - Float32(Float32(Float32(Float32(ux - Float32(1.0)) * maxCos) * ux) * zi)) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = (sqrt(single(1.0)) * xi) - ((((ux - single(1.0)) * maxCos) * ux) * zi); end
\begin{array}{l}
\\
\sqrt{1} \cdot xi - \left(\left(\left(ux - 1\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
Initial program 99.1%
Taylor expanded in uy around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites7.9%
Taylor expanded in ux around 0
Applied rewrites57.0%
Final simplification57.0%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ (* (* maxCos ux) zi) xi))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return ((maxCos * ux) * zi) + 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 = ((maxcos * ux) * zi) + xi
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(Float32(maxCos * ux) * zi) + xi) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = ((maxCos * ux) * zi) + xi; end
\begin{array}{l}
\\
\left(maxCos \cdot ux\right) \cdot zi + xi
\end{array}
Initial program 99.1%
Taylor expanded in uy around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites7.2%
Taylor expanded in ux around 0
Applied rewrites51.0%
Applied rewrites55.4%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (fma (* zi ux) maxCos xi))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return fmaf((zi * ux), maxCos, xi);
}
function code(xi, yi, zi, ux, uy, maxCos) return fma(Float32(zi * ux), maxCos, xi) end
\begin{array}{l}
\\
\mathsf{fma}\left(zi \cdot ux, maxCos, xi\right)
\end{array}
Initial program 99.1%
Taylor expanded in uy around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites7.2%
Taylor expanded in ux around 0
Applied rewrites51.0%
Taylor expanded in xi around 0
Applied rewrites12.3%
Taylor expanded in xi around 0
Applied rewrites51.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 99.1%
Taylor expanded in uy around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites7.1%
Taylor expanded in ux around 0
Applied rewrites51.0%
Taylor expanded in xi around 0
Applied rewrites12.3%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* (* zi maxCos) ux))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return (zi * maxCos) * 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 * maxcos) * ux
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(zi * maxCos) * ux) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = (zi * maxCos) * ux; end
\begin{array}{l}
\\
\left(zi \cdot maxCos\right) \cdot ux
\end{array}
Initial program 99.1%
Taylor expanded in uy around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites7.4%
Taylor expanded in ux around 0
Applied rewrites51.0%
Taylor expanded in xi around 0
Applied rewrites12.3%
Applied rewrites12.3%
(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 99.1%
Taylor expanded in uy around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites7.4%
Taylor expanded in ux around 0
Applied rewrites51.0%
Taylor expanded in xi around 0
Applied rewrites12.3%
Applied rewrites12.3%
herbie shell --seed 2024304
(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)))