From 374723272485ed065464c816772dbd672f3a2a1e Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Tue, 17 Sep 2019 06:49:36 +0200 Subject: [PATCH] LeastSquaresSolver: Fix nasty GCC compile optimization error The original implementation with no wrapping on size_t is more readable but the compiler errors with: internal compiler error: in trunc_int_for_mode, at explow.c:55 I read up and it's apparently a loop optimization problem. Inspired by https://stackoverflow.com/a/27224697/6326048 I used a far less readable implementation that works fine and wrote a comment to explain it. --- matrix/LeastSquaresSolver.hpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/matrix/LeastSquaresSolver.hpp b/matrix/LeastSquaresSolver.hpp index 2d812d7fda..1f518b4d69 100644 --- a/matrix/LeastSquaresSolver.hpp +++ b/matrix/LeastSquaresSolver.hpp @@ -116,20 +116,21 @@ public: Vector qtbv = qtb(b); Vector x; - for (size_t l = N; l > 0 ; l--) { - size_t i = l - 1; + // size_t is unsigned and wraps i = 0 - 1 to i > N + for (size_t i = N - 1; i < N; i--) { + printf("i %d\n", static_cast(i)); x(i) = qtbv(i); for (size_t r = i+1; r < N; r++) { x(i) -= _A(i,r) * x(r); } // divide by zero, return vector of zeros - if (fabs(_A(i,i)) < Type(1e-8)) { + if (isEqualF(_A(i,i), Type(0), Type(1e-8))) { for (size_t z = 0; z < N; z++) { x(z) = Type(0); } break; } - x(i) = x(i) / _A(i,i); + x(i) /= _A(i,i); } return x; }