Skip to content

Commit

Permalink
LeastSquaresSolver: Fix nasty GCC compile optimization error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MaEtUgR authored and jkflying committed Sep 18, 2019
1 parent 5844b0e commit 3747232
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions matrix/LeastSquaresSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,21 @@ class LeastSquaresSolver
Vector<Type, M> qtbv = qtb(b);
Vector<Type, N> 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<int>(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;
}
Expand Down

0 comments on commit 3747232

Please sign in to comment.