From d50ce24dd9631f6bb53b4f19667500efe6e23cca Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Tue, 11 Dec 2007 10:06:43 +0000 Subject: [PATCH] rework asserts system so as to minimize the impact of debugging code on performance --- src/Core/MatrixBase.h | 58 ++++++++++++++++--------------------------- src/Core/Util.h | 20 ++++++++++++--- 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/Core/MatrixBase.h b/src/Core/MatrixBase.h index 665229766..1fd6e3971 100644 --- a/src/Core/MatrixBase.h +++ b/src/Core/MatrixBase.h @@ -42,7 +42,7 @@ template class MatrixBase typedef typename ForwardDecl::Ref Ref; typedef typename NumTraits::Real RealScalar; - + int rows() const { return static_cast(this)->_rows(); } int cols() const { return static_cast(this)->_cols(); } int size() const { return rows() * cols(); } @@ -50,24 +50,6 @@ template class MatrixBase Ref ref() const { return static_cast(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(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(this)->_read(row, col); - } - template Derived& operator=(const MatrixBase& other) { @@ -132,7 +114,7 @@ template class MatrixBase ) const; template - Product + const Product lazyProduct(const MatrixBase& other) const EIGEN_ALWAYS_INLINE; Opposite operator-() const; @@ -156,49 +138,53 @@ template class MatrixBase Derived& operator/=(const std::complex& other); Derived& operator/=(const std::complex& other); - Scalar operator()(int row, int col) const + Scalar read(int row, int col, AssertLevel assertLevel = InternalDebugging) const { - assert(row >= 0 && row < rows() - && col >= 0 && col < cols()); - return read(row, col); + eigen_assert(assertLevel, row >= 0 && row < rows() + && col >= 0 && col < cols()); + return static_cast(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() - && col >= 0 && col < cols()); - return write(row, col); + eigen_assert(assertLevel, row >= 0 && row < rows() + && col >= 0 && col < cols()); + return static_cast(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) { - assert(index >= 0 && index < cols()); + eigen_assert(assertLevel, index >= 0 && index < cols()); return read(0, index); } else { - assert(index >= 0 && index < rows()); + eigen_assert(assertLevel, index >= 0 && index < rows()); 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) { - assert(index >= 0 && index < cols()); + eigen_assert(assertLevel, index >= 0 && index < cols()); return write(0, index); } else { - assert(index >= 0 && index < rows()); + eigen_assert(assertLevel, index >= 0 && index < rows()); return write(index, 0); } } + Scalar& operator[](int index) { return write(index, UserDebugging); } Eval eval() const EIGEN_ALWAYS_INLINE; }; diff --git a/src/Core/Util.h b/src/Core/Util.h index 9820deb8f..e55192ba1 100644 --- a/src/Core/Util.h +++ b/src/Core/Util.h @@ -26,6 +26,12 @@ #ifndef 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 #define USING_EIGEN_DATA_TYPES \ @@ -33,11 +39,13 @@ EIGEN_USING_MATRIX_TYPEDEFS \ using Eigen::Matrix; #ifdef EIGEN_INTERNAL_DEBUGGING -#define eigen_internal_assert(x) assert(x) +#define EIGEN_ASSERT_LEVEL 2 #else -#define eigen_internal_assert(x) +#define EIGEN_ASSERT_LEVEL 1 #endif +#define eigen_assert(assertLevel, x) if(assertLevel <= EIGEN_ASSERT_LEVEL) assert(x); + #define EIGEN_UNUSED(x) (void)x #ifdef NDEBUG @@ -48,10 +56,8 @@ using Eigen::Matrix; #ifdef __GNUC__ # define EIGEN_ALWAYS_INLINE __attribute__((always_inline)) -# define EIGEN_RESTRICT /*__restrict__*/ #else # define EIGEN_ALWAYS_INLINE -# define EIGEN_RESTRICT #endif #define EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \ @@ -114,4 +120,10 @@ struct ForwardDecl > const int Dynamic = -1; +enum AssertLevel +{ + UserDebugging = 1, + InternalDebugging = 2 +}; + #endif // EIGEN_UTIL_H