specialize for Size==0 in order to catch user bugs and not clutter

the compiler output with an infinite recursion. Also add a #define switch
for loop unrolling.
This commit is contained in:
Benoit Jacob
2007-12-11 10:04:39 +00:00
parent 9d51572cbe
commit fc924bc7d4
4 changed files with 62 additions and 13 deletions

View File

@@ -31,7 +31,7 @@ template<int Index, int Rows, typename Derived> struct TraceUnroller
static void run(const Derived &mat, typename Derived::Scalar &trace)
{
TraceUnroller<Index-1, Rows, Derived>::run(mat, trace);
trace += mat(Index, Index);
trace += mat.read(Index, Index);
}
};
@@ -39,7 +39,7 @@ template<int Rows, typename Derived> struct TraceUnroller<0, Rows, Derived>
{
static void run(const Derived &mat, typename Derived::Scalar &trace)
{
trace = mat(0, 0);
trace = mat.read(0, 0);
}
};
@@ -52,12 +52,22 @@ template<int Index, typename Derived> struct TraceUnroller<Index, Dynamic, Deriv
}
};
// prevent buggy user code from causing an infinite recursion
template<int Index, typename Derived> struct TraceUnroller<Index, 0, Derived>
{
static void run(const Derived &mat, typename Derived::Scalar &trace)
{
EIGEN_UNUSED(mat);
EIGEN_UNUSED(trace);
}
};
template<typename Scalar, typename Derived>
Scalar MatrixBase<Scalar, Derived>::trace() const
{
assert(rows() == cols());
Scalar res;
if(RowsAtCompileTime != Dynamic && RowsAtCompileTime <= 16)
if(EIGEN_UNROLLED_LOOPS && RowsAtCompileTime != Dynamic && RowsAtCompileTime <= 16)
TraceUnroller<RowsAtCompileTime-1, RowsAtCompileTime, Derived>
::run(*static_cast<const Derived*>(this), res);
else