rework asserts system so as to minimize the impact of debugging code on performance

This commit is contained in:
Benoit Jacob 2007-12-11 10:06:43 +00:00
parent effaee9bc7
commit d50ce24dd9
2 changed files with 38 additions and 40 deletions

View File

@ -42,7 +42,7 @@ template<typename Scalar, typename Derived> class MatrixBase
typedef typename ForwardDecl<Derived>::Ref Ref; typedef typename ForwardDecl<Derived>::Ref Ref;
typedef typename NumTraits<Scalar>::Real RealScalar; typedef typename NumTraits<Scalar>::Real RealScalar;
int rows() const { return static_cast<const Derived *>(this)->_rows(); } int rows() const { return static_cast<const Derived *>(this)->_rows(); }
int cols() const { return static_cast<const Derived *>(this)->_cols(); } int cols() const { return static_cast<const Derived *>(this)->_cols(); }
int size() const { return rows() * cols(); } int size() const { return rows() * cols(); }
@ -50,24 +50,6 @@ template<typename Scalar, typename Derived> class MatrixBase
Ref ref() const Ref ref() const
{ return static_cast<const Derived *>(this)->_ref(); } { return static_cast<const Derived *>(this)->_ref(); }
Scalar& write(int row, int col)
{
// from this single point, we can check all writes to all matrix/expression
// types. We only want this however for internal testing, as this is very slow.
eigen_internal_assert(row >= 0 && row < rows()
&& col >= 0 && col < cols());
return static_cast<Derived *>(this)->_write(row, col);
}
Scalar read(int row, int col) const
{
// from this single point, we can check all reads to all matrix/expression
// types. We only want this however for internal testing, as this is very slow.
eigen_internal_assert(row >= 0 && row < rows()
&& col >= 0 && col < cols());
return static_cast<const Derived *>(this)->_read(row, col);
}
template<typename OtherDerived> template<typename OtherDerived>
Derived& operator=(const MatrixBase<Scalar, OtherDerived>& other) Derived& operator=(const MatrixBase<Scalar, OtherDerived>& other)
{ {
@ -132,7 +114,7 @@ template<typename Scalar, typename Derived> class MatrixBase
) const; ) const;
template<typename OtherDerived> template<typename OtherDerived>
Product<Derived, OtherDerived> const Product<Derived, OtherDerived>
lazyProduct(const MatrixBase<Scalar, OtherDerived>& other) const EIGEN_ALWAYS_INLINE; lazyProduct(const MatrixBase<Scalar, OtherDerived>& other) const EIGEN_ALWAYS_INLINE;
Opposite<Derived> operator-() const; Opposite<Derived> operator-() const;
@ -156,49 +138,53 @@ template<typename Scalar, typename Derived> class MatrixBase
Derived& operator/=(const std::complex<float>& other); Derived& operator/=(const std::complex<float>& other);
Derived& operator/=(const std::complex<double>& other); Derived& operator/=(const std::complex<double>& other);
Scalar operator()(int row, int col) const Scalar read(int row, int col, AssertLevel assertLevel = InternalDebugging) const
{ {
assert(row >= 0 && row < rows() eigen_assert(assertLevel, row >= 0 && row < rows()
&& col >= 0 && col < cols()); && col >= 0 && col < cols());
return read(row, col); return static_cast<const Derived *>(this)->_read(row, col);
} }
Scalar operator()(int row, int col) const { return read(row, col, UserDebugging); }
Scalar& operator()(int row, int col) Scalar& write(int row, int col, AssertLevel assertLevel = InternalDebugging)
{ {
assert(row >= 0 && row < rows() eigen_assert(assertLevel, row >= 0 && row < rows()
&& col >= 0 && col < cols()); && col >= 0 && col < cols());
return write(row, col); return static_cast<Derived *>(this)->_write(row, col);
} }
Scalar& operator()(int row, int col) { return write(row, col, UserDebugging); }
Scalar operator[](int index) const Scalar read(int index, AssertLevel assertLevel = InternalDebugging) const
{ {
assert(IsVector); eigen_assert(assertLevel, IsVector);
if(RowsAtCompileTime == 1) if(RowsAtCompileTime == 1)
{ {
assert(index >= 0 && index < cols()); eigen_assert(assertLevel, index >= 0 && index < cols());
return read(0, index); return read(0, index);
} }
else else
{ {
assert(index >= 0 && index < rows()); eigen_assert(assertLevel, index >= 0 && index < rows());
return read(index, 0); return read(index, 0);
} }
} }
Scalar operator[](int index) const { return read(index, UserDebugging); }
Scalar& operator[](int index) Scalar& write(int index, AssertLevel assertLevel = InternalDebugging)
{ {
assert(IsVector); eigen_assert(assertLevel, IsVector);
if(RowsAtCompileTime == 1) if(RowsAtCompileTime == 1)
{ {
assert(index >= 0 && index < cols()); eigen_assert(assertLevel, index >= 0 && index < cols());
return write(0, index); return write(0, index);
} }
else else
{ {
assert(index >= 0 && index < rows()); eigen_assert(assertLevel, index >= 0 && index < rows());
return write(index, 0); return write(index, 0);
} }
} }
Scalar& operator[](int index) { return write(index, UserDebugging); }
Eval<Derived> eval() const EIGEN_ALWAYS_INLINE; Eval<Derived> eval() const EIGEN_ALWAYS_INLINE;
}; };

View File

@ -26,6 +26,12 @@
#ifndef EIGEN_UTIL_H #ifndef EIGEN_UTIL_H
#define EIGEN_UTIL_H #define EIGEN_UTIL_H
#ifdef EIGEN_DONT_USE_UNROLLED_LOOPS
#define EIGEN_UNROLLED_LOOPS (false)
#else
#define EIGEN_UNROLLED_LOOPS (true)
#endif
#undef minor #undef minor
#define USING_EIGEN_DATA_TYPES \ #define USING_EIGEN_DATA_TYPES \
@ -33,11 +39,13 @@ EIGEN_USING_MATRIX_TYPEDEFS \
using Eigen::Matrix; using Eigen::Matrix;
#ifdef EIGEN_INTERNAL_DEBUGGING #ifdef EIGEN_INTERNAL_DEBUGGING
#define eigen_internal_assert(x) assert(x) #define EIGEN_ASSERT_LEVEL 2
#else #else
#define eigen_internal_assert(x) #define EIGEN_ASSERT_LEVEL 1
#endif #endif
#define eigen_assert(assertLevel, x) if(assertLevel <= EIGEN_ASSERT_LEVEL) assert(x);
#define EIGEN_UNUSED(x) (void)x #define EIGEN_UNUSED(x) (void)x
#ifdef NDEBUG #ifdef NDEBUG
@ -48,10 +56,8 @@ using Eigen::Matrix;
#ifdef __GNUC__ #ifdef __GNUC__
# define EIGEN_ALWAYS_INLINE __attribute__((always_inline)) # define EIGEN_ALWAYS_INLINE __attribute__((always_inline))
# define EIGEN_RESTRICT /*__restrict__*/
#else #else
# define EIGEN_ALWAYS_INLINE # define EIGEN_ALWAYS_INLINE
# define EIGEN_RESTRICT
#endif #endif
#define EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \ #define EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
@ -114,4 +120,10 @@ struct ForwardDecl<Matrix<_Scalar, _Rows, _Cols> >
const int Dynamic = -1; const int Dynamic = -1;
enum AssertLevel
{
UserDebugging = 1,
InternalDebugging = 2
};
#endif // EIGEN_UTIL_H #endif // EIGEN_UTIL_H