
(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 12 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))
(* 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.7%
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.7
Applied rewrites98.7%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (PI) (* 2.0 uy))))
(+
(+ (* (sin t_0) yi) (* xi (cos t_0)))
(* (* (* (- 1.0 ux) maxCos) ux) zi))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\\
\left(\sin t\_0 \cdot yi + xi \cdot \cos t\_0\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
\end{array}
Initial program 98.7%
Taylor expanded in ux around 0
*-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-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3231.4
Applied rewrites31.4%
Applied rewrites98.6%
(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))
(* 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 + \left(\left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right) \cdot 2\right) + t\_0 \cdot zi
\end{array}
\end{array}
Initial program 98.7%
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.7
Applied rewrites98.7%
Taylor expanded in uy around 0
Applied rewrites87.2%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (PI) uy)) (t_1 (* (* (* (- 1.0 ux) maxCos) ux) zi)))
(if (<= (* uy 2.0) 0.10000000149011612)
(+
(*
(+ (* (* (/ (* (PI) yi) xi) uy) 2.0) (+ 1.0 (* (pow t_0 2.0) -2.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}\;uy \cdot 2 \leq 0.10000000149011612:\\
\;\;\;\;\left(\left(\frac{\mathsf{PI}\left(\right) \cdot yi}{xi} \cdot uy\right) \cdot 2 + \left(1 + {t\_0}^{2} \cdot -2\right)\right) \cdot xi + t\_1\\
\mathbf{else}:\\
\;\;\;\;\sin \left(t\_0 \cdot 2\right) \cdot yi + t\_1\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 0.100000001Initial program 99.2%
Taylor expanded in ux around 0
*-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-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3235.9
Applied rewrites35.9%
Taylor expanded in uy around 0
Applied rewrites52.6%
Taylor expanded in xi around inf
Applied rewrites52.6%
Applied rewrites91.7%
if 0.100000001 < (*.f32 uy #s(literal 2 binary32)) Initial program 96.2%
Taylor expanded in ux around 0
*-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-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f326.7
Applied rewrites6.8%
Taylor expanded in xi around 0
Applied rewrites58.2%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (* (- 1.0 ux) maxCos) ux) zi)))
(if (<= (* uy 2.0) 0.10000000149011612)
(+
(*
(-
(* (* (* (PI) (PI)) xi) -2.0)
(/ (- (* -2.0 (* yi (PI))) (/ xi uy)) uy))
(* uy uy))
t_0)
(+ (* (sin (* (* (PI) uy) 2.0)) yi) t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\\
\mathbf{if}\;uy \cdot 2 \leq 0.10000000149011612:\\
\;\;\;\;\left(\left(\left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot xi\right) \cdot -2 - \frac{-2 \cdot \left(yi \cdot \mathsf{PI}\left(\right)\right) - \frac{xi}{uy}}{uy}\right) \cdot \left(uy \cdot uy\right) + t\_0\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot yi + t\_0\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 0.100000001Initial program 99.2%
Taylor expanded in ux around 0
*-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-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3235.9
Applied rewrites35.9%
Taylor expanded in uy around 0
Applied rewrites52.6%
Taylor expanded in uy around -inf
Applied rewrites91.7%
if 0.100000001 < (*.f32 uy #s(literal 2 binary32)) Initial program 96.2%
Taylor expanded in ux around 0
*-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-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f326.7
Applied rewrites6.7%
Taylor expanded in xi around 0
Applied rewrites58.2%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ (* (- (* (* (* (PI) (PI)) xi) -2.0) (/ (- (* -2.0 (* yi (PI))) (/ xi uy)) uy)) (* uy uy)) (* (* (* (- 1.0 ux) maxCos) ux) zi)))
\begin{array}{l}
\\
\left(\left(\left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot xi\right) \cdot -2 - \frac{-2 \cdot \left(yi \cdot \mathsf{PI}\left(\right)\right) - \frac{xi}{uy}}{uy}\right) \cdot \left(uy \cdot uy\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
Initial program 98.7%
Taylor expanded in ux around 0
*-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-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3231.4
Applied rewrites31.4%
Taylor expanded in uy around 0
Applied rewrites46.8%
Taylor expanded in uy around -inf
Applied rewrites82.0%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ (+ (* (fma -2.0 (* (* (* (PI) (PI)) xi) uy) (* (* yi (PI)) 2.0)) uy) xi) (* (* (* (- 1.0 ux) maxCos) ux) zi)))
\begin{array}{l}
\\
\left(\mathsf{fma}\left(-2, \left(\left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot xi\right) \cdot uy, \left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot 2\right) \cdot uy + xi\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
Initial program 98.7%
Taylor expanded in ux around 0
*-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-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3231.4
Applied rewrites31.4%
Taylor expanded in uy around 0
Applied rewrites46.8%
Applied rewrites79.0%
(FPCore (xi yi zi ux uy maxCos)
:precision binary32
(let* ((t_0 (* (* (* (- 1.0 ux) maxCos) ux) zi)))
(if (or (<= xi -5.499999825460374e-22) (not (<= xi 2.000000026702864e-10)))
(+ (* (- xi) -1.0) t_0)
(+ (* (* (* yi (PI)) uy) 2.0) t_0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\\
\mathbf{if}\;xi \leq -5.499999825460374 \cdot 10^{-22} \lor \neg \left(xi \leq 2.000000026702864 \cdot 10^{-10}\right):\\
\;\;\;\;\left(-xi\right) \cdot -1 + t\_0\\
\mathbf{else}:\\
\;\;\;\;\left(\left(yi \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right) \cdot 2 + t\_0\\
\end{array}
\end{array}
if xi < -5.4999998e-22 or 2.00000003e-10 < xi Initial program 98.9%
Taylor expanded in ux around 0
*-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-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3211.2
Applied rewrites11.2%
Taylor expanded in uy around 0
Applied rewrites67.8%
Taylor expanded in xi around -inf
Applied rewrites67.6%
Taylor expanded in uy around 0
Applied rewrites67.8%
if -5.4999998e-22 < xi < 2.00000003e-10Initial program 98.6%
Taylor expanded in ux around 0
*-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-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3248.5
Applied rewrites48.5%
Taylor expanded in uy around 0
Applied rewrites29.1%
Taylor expanded in xi around 0
Applied rewrites60.2%
Final simplification63.7%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (+ (* (- xi) -1.0) (* (* (* (- 1.0 ux) maxCos) ux) zi)))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return (-xi * -1.0f) + ((((1.0f - ux) * 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 * (-1.0e0)) + ((((1.0e0 - ux) * maxcos) * ux) * zi)
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(Float32(-xi) * Float32(-1.0)) + Float32(Float32(Float32(Float32(Float32(1.0) - ux) * maxCos) * ux) * zi)) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = (-xi * single(-1.0)) + ((((single(1.0) - ux) * maxCos) * ux) * zi); end
\begin{array}{l}
\\
\left(-xi\right) \cdot -1 + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\end{array}
Initial program 98.7%
Taylor expanded in ux around 0
*-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-*.f32N/A
lower-sin.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f3231.4
Applied rewrites31.4%
Taylor expanded in uy around 0
Applied rewrites46.8%
Taylor expanded in xi around -inf
Applied rewrites46.8%
Taylor expanded in uy around 0
Applied rewrites46.8%
(FPCore (xi yi zi ux uy maxCos) :precision binary32 (* (* (- zi (* zi ux)) ux) maxCos))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
return ((zi - (zi * ux)) * 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 - (zi * ux)) * ux) * maxcos
end function
function code(xi, yi, zi, ux, uy, maxCos) return Float32(Float32(Float32(zi - Float32(zi * ux)) * ux) * maxCos) end
function tmp = code(xi, yi, zi, ux, uy, maxCos) tmp = ((zi - (zi * ux)) * ux) * maxCos; end
\begin{array}{l}
\\
\left(\left(zi - zi \cdot ux\right) \cdot ux\right) \cdot maxCos
\end{array}
Initial program 98.7%
Taylor expanded in zi around inf
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3213.0
Applied rewrites13.0%
Taylor expanded in ux around 0
Applied rewrites13.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.7%
Taylor expanded in zi around inf
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3213.0
Applied rewrites13.0%
Applied rewrites13.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.7%
Taylor expanded in zi around inf
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3213.0
Applied rewrites13.0%
Taylor expanded in ux around 0
Applied rewrites12.1%
herbie shell --seed 2024318
(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)))