From 114de952002b9b47701bd690fbe891d9b7e16a9b Mon Sep 17 00:00:00 2001 From: Davis King Date: Tue, 22 Oct 2013 21:07:23 -0400 Subject: [PATCH] Refactored code slightly to avoid getting this warning from gcc when certain optimizations are enabled "warning: assuming signed overflow does not occur when assuming that (X + c) < X is always false" --- dlib/matrix/matrix_subexp.h | 61 +++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/dlib/matrix/matrix_subexp.h b/dlib/matrix/matrix_subexp.h index fb27c3665..fe0bc5603 100644 --- a/dlib/matrix/matrix_subexp.h +++ b/dlib/matrix/matrix_subexp.h @@ -533,15 +533,18 @@ namespace dlib assignable_sub_matrix( matrix& m_, - const rectangle& rect_ - ) : m(m_), rect(rect_) {} + long top_, + long left_, + long height_, + long width_ + ) : m(m_), left(left_), top(top_), width(width_), height(height_) {} T& operator() ( long r, long c ) { - return m(r+rect.top(),c+rect.left()); + return m(r+top,c+left); } const T& operator() ( @@ -549,24 +552,24 @@ namespace dlib long c ) const { - return m(r+rect.top(),c+rect.left()); + return m(r+top,c+left); } - long nr() const { return rect.height(); } - long nc() const { return rect.width(); } + long nr() const { return height; } + long nc() const { return width; } template assignable_sub_matrix& operator= ( const matrix_exp& exp ) { - DLIB_ASSERT( exp.nr() == (long)rect.height() && exp.nc() == (long)rect.width(), + DLIB_ASSERT( exp.nr() == height && exp.nc() == width, "\tassignable_matrix_expression set_subm()" << "\n\tYou have tried to assign to this object using a matrix that isn't the right size" << "\n\texp.nr() (source matrix): " << exp.nr() << "\n\texp.nc() (source matrix): " << exp.nc() - << "\n\trect.width() (target matrix): " << rect.width() - << "\n\trect.height() (target matrix): " << rect.height() + << "\n\twidth (target matrix): " << width + << "\n\theight (target matrix): " << height ); if (exp.destructively_aliases(m) == false) @@ -588,18 +591,18 @@ namespace dlib const matrix_exp& exp ) { - DLIB_ASSERT( exp.nr() == (long)rect.height() && exp.nc() == (long)rect.width(), + DLIB_ASSERT( exp.nr() == height && exp.nc() == width, "\tassignable_matrix_expression set_subm()" << "\n\tYou have tried to assign to this object using a matrix that isn't the right size" << "\n\texp.nr() (source matrix): " << exp.nr() << "\n\texp.nc() (source matrix): " << exp.nc() - << "\n\trect.width() (target matrix): " << rect.width() - << "\n\trect.height() (target matrix): " << rect.height() + << "\n\twidth (target matrix): " << width + << "\n\theight (target matrix): " << height ); if (exp.destructively_aliases(m) == false) { - matrix_assign(*this, subm(m,rect)+exp); + matrix_assign(*this, subm(m,top,left,height,width)+exp); } else { @@ -616,18 +619,18 @@ namespace dlib const matrix_exp& exp ) { - DLIB_ASSERT( exp.nr() == (long)rect.height() && exp.nc() == (long)rect.width(), + DLIB_ASSERT( exp.nr() == height && exp.nc() == width, "\tassignable_matrix_expression set_subm()" << "\n\tYou have tried to assign to this object using a matrix that isn't the right size" << "\n\texp.nr() (source matrix): " << exp.nr() << "\n\texp.nc() (source matrix): " << exp.nc() - << "\n\trect.width() (target matrix): " << rect.width() - << "\n\trect.height() (target matrix): " << rect.height() + << "\n\twidth (target matrix): " << width + << "\n\theight (target matrix): " << height ); if (exp.destructively_aliases(m) == false) { - matrix_assign(*this, subm(m,rect)-exp); + matrix_assign(*this, subm(m,top,left,height,width)-exp); } else { @@ -643,9 +646,11 @@ namespace dlib const T& value ) { - for (long r = rect.top(); r <= rect.bottom(); ++r) + const long bottom = top+height-1; + const long right = left+width-1; + for (long r = top; r <= bottom; ++r) { - for (long c = rect.left(); c <= rect.right(); ++c) + for (long c = left; c <= right; ++c) { m(r,c) = value; } @@ -658,9 +663,11 @@ namespace dlib const T& value ) { - for (long r = rect.top(); r <= rect.bottom(); ++r) + const long bottom = top+height-1; + const long right = left+width-1; + for (long r = top; r <= bottom; ++r) { - for (long c = rect.left(); c <= rect.right(); ++c) + for (long c = left; c <= right; ++c) { m(r,c) += value; } @@ -673,9 +680,11 @@ namespace dlib const T& value ) { - for (long r = rect.top(); r <= rect.bottom(); ++r) + const long bottom = top+height-1; + const long right = left+width-1; + for (long r = top; r <= bottom; ++r) { - for (long c = rect.left(); c <= rect.right(); ++c) + for (long c = left; c <= right; ++c) { m(r,c) -= value; } @@ -686,7 +695,7 @@ namespace dlib matrix& m; - const rectangle rect; + const long left, top, width, height; }; @@ -708,7 +717,7 @@ namespace dlib ); - return assignable_sub_matrix(m,rect); + return assignable_sub_matrix(m,rect.top(), rect.left(), rect.height(), rect.width()); } @@ -732,7 +741,7 @@ namespace dlib << "\n\tnc: " << nc ); - return assignable_sub_matrix(m,rectangle(c,r, c+nc-1, r+nr-1)); + return assignable_sub_matrix(m,r,c, nr, nc); } // ----------------------------------------------------------------------------------------