
(FPCore (ux uy maxCos) :precision binary32 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos)))) (* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* t_0 t_0))))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 11 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (ux uy maxCos) :precision binary32 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos)))) (* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* t_0 t_0))))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}
(FPCore (ux uy maxCos)
:precision binary32
(*
(sqrt
(*
(* ux ux)
(-
(/ 2.0 ux)
(- (* (- (+ (/ 2.0 ux) maxCos) 1.0) maxCos) (- maxCos 1.0)))))
(cos (* (PI) (* 2.0 uy)))))\begin{array}{l}
\\
\sqrt{\left(ux \cdot ux\right) \cdot \left(\frac{2}{ux} - \left(\left(\left(\frac{2}{ux} + maxCos\right) - 1\right) \cdot maxCos - \left(maxCos - 1\right)\right)\right)} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.6%
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
distribute-rgt-inN/A
associate--r+N/A
lower--.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites38.0%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites79.8%
Taylor expanded in maxCos around 0
Applied rewrites98.7%
Final simplification98.7%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sqrt
(*
(- (/ 2.0 ux) (- (* (- (/ 2.0 ux) 1.0) maxCos) (- maxCos 1.0)))
(* ux ux)))
(cos (* (PI) (* 2.0 uy)))))\begin{array}{l}
\\
\sqrt{\left(\frac{2}{ux} - \left(\left(\frac{2}{ux} - 1\right) \cdot maxCos - \left(maxCos - 1\right)\right)\right) \cdot \left(ux \cdot ux\right)} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.6%
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
distribute-rgt-inN/A
associate--r+N/A
lower--.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites38.0%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites79.8%
Taylor expanded in maxCos around 0
Applied rewrites97.7%
Final simplification97.7%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (- (/ 2.0 ux) (- (* (/ maxCos ux) 2.0) (- maxCos 1.0))) (* ux ux))) (cos (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(\frac{2}{ux} - \left(\frac{maxCos}{ux} \cdot 2 - \left(maxCos - 1\right)\right)\right) \cdot \left(ux \cdot ux\right)} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.6%
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
distribute-rgt-inN/A
associate--r+N/A
lower--.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites38.0%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites79.8%
Taylor expanded in ux around 0
Applied rewrites97.0%
Final simplification97.0%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= (* 2.0 uy) 0.00019500000053085387)
(*
(sqrt
(+ (* (- 2.0 (* (pow (- maxCos 1.0) 2.0) ux)) ux) (* (* -2.0 maxCos) ux)))
1.0)
(* (sqrt (* (- 2.0 ux) ux)) (cos (* (PI) (* 2.0 uy))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;2 \cdot uy \leq 0.00019500000053085387:\\
\;\;\;\;\sqrt{\left(2 - {\left(maxCos - 1\right)}^{2} \cdot ux\right) \cdot ux + \left(-2 \cdot maxCos\right) \cdot ux} \cdot 1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(2 - ux\right) \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 1.95000001e-4Initial program 56.0%
Taylor expanded in uy around 0
Applied rewrites56.0%
Taylor expanded in ux around 0
Applied rewrites6.6%
Taylor expanded in ux around 0
Applied rewrites33.5%
Applied rewrites99.5%
if 1.95000001e-4 < (*.f32 uy #s(literal 2 binary32)) Initial program 55.0%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
associate--r+N/A
associate-*r/N/A
metadata-evalN/A
associate-*r/N/A
div-subN/A
cancel-sign-sub-invN/A
metadata-evalN/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
unpow2N/A
lower-*.f3225.9
Applied rewrites25.9%
Taylor expanded in maxCos around 0
Applied rewrites92.3%
Taylor expanded in ux around 0
Applied rewrites92.4%
Final simplification96.3%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= (* 2.0 uy) 0.00019500000053085387)
(*
(sqrt
(*
(- (- (* (* (- 1.0 maxCos) (- maxCos 1.0)) ux) (- maxCos 2.0)) maxCos)
ux))
1.0)
(* (sqrt (* (- 2.0 ux) ux)) (cos (* (PI) (* 2.0 uy))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;2 \cdot uy \leq 0.00019500000053085387:\\
\;\;\;\;\sqrt{\left(\left(\left(\left(1 - maxCos\right) \cdot \left(maxCos - 1\right)\right) \cdot ux - \left(maxCos - 2\right)\right) - maxCos\right) \cdot ux} \cdot 1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(2 - ux\right) \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 1.95000001e-4Initial program 56.0%
Taylor expanded in uy around 0
Applied rewrites56.0%
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
distribute-lft-inN/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lift-fma.f32N/A
lift-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lift-fma.f32N/A
lift-*.f32N/A
*-commutativeN/A
associate-*l*N/A
lift-*.f32N/A
lift-*.f32N/A
Applied rewrites52.9%
lift-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*l*N/A
lower-fma.f32N/A
Applied rewrites20.3%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites99.3%
if 1.95000001e-4 < (*.f32 uy #s(literal 2 binary32)) Initial program 55.0%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
associate--r+N/A
associate-*r/N/A
metadata-evalN/A
associate-*r/N/A
div-subN/A
cancel-sign-sub-invN/A
metadata-evalN/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
unpow2N/A
lower-*.f3225.9
Applied rewrites25.9%
Taylor expanded in maxCos around 0
Applied rewrites92.3%
Taylor expanded in ux around 0
Applied rewrites92.4%
Final simplification96.2%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= (* 2.0 uy) 0.004000000189989805)
(*
(sqrt
(*
(- (- (* (* (- 1.0 maxCos) (- maxCos 1.0)) ux) (- maxCos 2.0)) maxCos)
ux))
1.0)
(* (sqrt (* ux 2.0)) (cos (* (PI) (* 2.0 uy))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;2 \cdot uy \leq 0.004000000189989805:\\
\;\;\;\;\sqrt{\left(\left(\left(\left(1 - maxCos\right) \cdot \left(maxCos - 1\right)\right) \cdot ux - \left(maxCos - 2\right)\right) - maxCos\right) \cdot ux} \cdot 1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{ux \cdot 2} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 0.00400000019Initial program 57.3%
Taylor expanded in uy around 0
Applied rewrites56.8%
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
distribute-lft-inN/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lift-fma.f32N/A
lift-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lift-fma.f32N/A
lift-*.f32N/A
*-commutativeN/A
associate-*l*N/A
lift-*.f32N/A
lift-*.f32N/A
Applied rewrites53.9%
lift-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*l*N/A
lower-fma.f32N/A
Applied rewrites20.4%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites97.4%
if 0.00400000019 < (*.f32 uy #s(literal 2 binary32)) Initial program 52.4%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
associate--r+N/A
associate-*r/N/A
metadata-evalN/A
associate-*r/N/A
div-subN/A
cancel-sign-sub-invN/A
metadata-evalN/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
unpow2N/A
lower-*.f328.4
Applied rewrites8.4%
Taylor expanded in maxCos around 0
Applied rewrites91.5%
Taylor expanded in ux around 0
Applied rewrites74.1%
Final simplification89.2%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sqrt
(*
(- (- (* (* (- 1.0 maxCos) (- maxCos 1.0)) ux) (- maxCos 2.0)) maxCos)
ux))
1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf(((((((1.0f - maxCos) * (maxCos - 1.0f)) * ux) - (maxCos - 2.0f)) - maxCos) * ux)) * 1.0f;
}
real(4) function code(ux, uy, maxcos)
real(4), intent (in) :: ux
real(4), intent (in) :: uy
real(4), intent (in) :: maxcos
code = sqrt(((((((1.0e0 - maxcos) * (maxcos - 1.0e0)) * ux) - (maxcos - 2.0e0)) - maxcos) * ux)) * 1.0e0
end function
function code(ux, uy, maxCos) return Float32(sqrt(Float32(Float32(Float32(Float32(Float32(Float32(Float32(1.0) - maxCos) * Float32(maxCos - Float32(1.0))) * ux) - Float32(maxCos - Float32(2.0))) - maxCos) * ux)) * Float32(1.0)) end
function tmp = code(ux, uy, maxCos) tmp = sqrt(((((((single(1.0) - maxCos) * (maxCos - single(1.0))) * ux) - (maxCos - single(2.0))) - maxCos) * ux)) * single(1.0); end
\begin{array}{l}
\\
\sqrt{\left(\left(\left(\left(1 - maxCos\right) \cdot \left(maxCos - 1\right)\right) \cdot ux - \left(maxCos - 2\right)\right) - maxCos\right) \cdot ux} \cdot 1
\end{array}
Initial program 55.6%
Taylor expanded in uy around 0
Applied rewrites46.2%
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
distribute-lft-inN/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lift-fma.f32N/A
lift-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lift-fma.f32N/A
lift-*.f32N/A
*-commutativeN/A
associate-*l*N/A
lift-*.f32N/A
lift-*.f32N/A
Applied rewrites44.3%
lift-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
associate-*l*N/A
lower-fma.f32N/A
Applied rewrites19.2%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites76.0%
Final simplification76.0%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (- (* (- 2.0 ux) ux) (* (fma ux -2.0 2.0) (* ux maxCos)))) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf((((2.0f - ux) * ux) - (fmaf(ux, -2.0f, 2.0f) * (ux * maxCos)))) * 1.0f;
}
function code(ux, uy, maxCos) return Float32(sqrt(Float32(Float32(Float32(Float32(2.0) - ux) * ux) - Float32(fma(ux, Float32(-2.0), Float32(2.0)) * Float32(ux * maxCos)))) * Float32(1.0)) end
\begin{array}{l}
\\
\sqrt{\left(2 - ux\right) \cdot ux - \mathsf{fma}\left(ux, -2, 2\right) \cdot \left(ux \cdot maxCos\right)} \cdot 1
\end{array}
Initial program 55.6%
Taylor expanded in uy around 0
Applied rewrites46.2%
Taylor expanded in ux around 0
Applied rewrites6.6%
Taylor expanded in ux around 0
Applied rewrites25.7%
Taylor expanded in maxCos around 0
Applied rewrites74.7%
Final simplification74.7%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (- 2.0 ux) ux)) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf(((2.0f - ux) * ux)) * 1.0f;
}
real(4) function code(ux, uy, maxcos)
real(4), intent (in) :: ux
real(4), intent (in) :: uy
real(4), intent (in) :: maxcos
code = sqrt(((2.0e0 - ux) * ux)) * 1.0e0
end function
function code(ux, uy, maxCos) return Float32(sqrt(Float32(Float32(Float32(2.0) - ux) * ux)) * Float32(1.0)) end
function tmp = code(ux, uy, maxCos) tmp = sqrt(((single(2.0) - ux) * ux)) * single(1.0); end
\begin{array}{l}
\\
\sqrt{\left(2 - ux\right) \cdot ux} \cdot 1
\end{array}
Initial program 55.6%
Taylor expanded in uy around 0
Applied rewrites46.2%
Taylor expanded in ux around 0
Applied rewrites6.6%
Taylor expanded in ux around 0
Applied rewrites25.7%
Taylor expanded in maxCos around 0
Applied rewrites70.8%
Final simplification70.8%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (fma -1.0 1.0 1.0)) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf(fmaf(-1.0f, 1.0f, 1.0f)) * 1.0f;
}
function code(ux, uy, maxCos) return Float32(sqrt(fma(Float32(-1.0), Float32(1.0), Float32(1.0))) * Float32(1.0)) end
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(-1, 1, 1\right)} \cdot 1
\end{array}
Initial program 55.6%
Taylor expanded in uy around 0
Applied rewrites46.2%
Taylor expanded in ux around 0
Applied rewrites6.6%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
neg-mul-1N/A
lower-fma.f3219.2
Applied rewrites19.2%
Taylor expanded in uy around 0
Applied rewrites19.2%
Final simplification9.3%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (- 1.0 1.0)) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf((1.0f - 1.0f)) * 1.0f;
}
real(4) function code(ux, uy, maxcos)
real(4), intent (in) :: ux
real(4), intent (in) :: uy
real(4), intent (in) :: maxcos
code = sqrt((1.0e0 - 1.0e0)) * 1.0e0
end function
function code(ux, uy, maxCos) return Float32(sqrt(Float32(Float32(1.0) - Float32(1.0))) * Float32(1.0)) end
function tmp = code(ux, uy, maxCos) tmp = sqrt((single(1.0) - single(1.0))) * single(1.0); end
\begin{array}{l}
\\
\sqrt{1 - 1} \cdot 1
\end{array}
Initial program 55.6%
Taylor expanded in uy around 0
Applied rewrites46.2%
Taylor expanded in ux around 0
Applied rewrites6.6%
Final simplification6.6%
herbie shell --seed 2024249
(FPCore (ux uy maxCos)
:name "UniformSampleCone, x"
:precision binary32
:pre (and (and (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) (* ux maxCos)) (+ (- 1.0 ux) (* ux maxCos)))))))