From 1084447730888f6cf0b1bcafbab122aad0257c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 20 Apr 2012 12:12:35 +0200 Subject: [PATCH] Using custom operator instead of [][] in Matrix internals. GCC does some heavy magic optimizations in -O2 (-O1 works) and it somewhat breaks them. It should be safe to use them outside of Matrix, though (e.g. when not used in loops through all elements). --- src/Math/Matrix.h | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Math/Matrix.h b/src/Math/Matrix.h index 779c90f9f..d41c819ab 100644 --- a/src/Math/Matrix.h +++ b/src/Math/Matrix.h @@ -46,6 +46,8 @@ namespace Implementation { * @todo first col, then row (cache adjacency) */ template class Matrix { + friend class Matrix; /* for ij() */ + public: /** * @brief %Matrix from array @@ -160,7 +162,7 @@ template class Matrix { for(size_t row = 0; row != size; ++row) for(size_t col = 0; col != size; ++col) for(size_t pos = 0; pos != size; ++pos) - out[col][row] += (*this)[pos][row]*other[col][pos]; + out(col, row) += (*this)(pos, row)*other(col, pos); return out; } @@ -176,7 +178,7 @@ template class Matrix { for(size_t row = 0; row != size; ++row) for(size_t pos = 0; pos != size; ++pos) - out[row] += (*this)[pos][row]*other[pos]; + out[row] += (*this)(pos, row)*other[pos]; return out; } @@ -187,7 +189,7 @@ template class Matrix { for(size_t row = 0; row != size; ++row) for(size_t col = 0; col != size; ++col) - out[row][col] = (*this)[col][row]; + out(row, col) = (*this)(col, row); return out; } @@ -198,8 +200,8 @@ template class Matrix { for(size_t row = 0; row != size-1; ++row) for(size_t col = 0; col != size-1; ++col) - out[col][row] = (*this)[col + (col >= skipCol)] - [row + (row >= skipRow)]; + out(col, row) = (*this)(col + (col >= skipCol), + row + (row >= skipRow)); return out; } @@ -223,7 +225,7 @@ template class Matrix { for(size_t row = 0; row != size; ++row) for(size_t col = 0; col != size; ++col) - out[col][row] = (((row+col) & 1) ? -1 : 1)*ij(row, col).determinant()/_determinant; + out(col, row) = (((row+col) & 1) ? -1 : 1)*ij(row, col).determinant()/_determinant; return out; } @@ -237,6 +239,15 @@ template class Matrix { return Matrix(first, next...); } + /* Used internally instead of [][], because GCC does some heavy + optimalization in release mode which breaks it */ + inline T& operator()(size_t col, size_t row) { + return _data[col*size+row]; + } + inline constexpr const T& operator()(size_t col, size_t row) const { + return _data[col*size+row]; + } + T _data[size*size]; };