3 ms·
Undefined signed overflow isn’t great (and it doesn’t help that C’s implicit integer promotion rules are bad), but it’s important because it allows loop optimiz
by astrange 5y ago
Undefined signed overflow isn’t great (and it doesn’t help that C’s implicit integer promotion rules are bad), but it’s important because it allows loop optimizations, which really are important cross-platform, eg Intel is fine with loops counting up but PPC would like them to count down.
The problem causing this is that C loops are overspecified. If there was a “for…in” loop that didn’t make you declare and increment a variable, then manual increments could trap on overflow without causing so many problems.
- hramrach 5y agoI don't understand. If you can turn the loop from upward to downward then you do have a bound for it, and then you can tell if it overflows or not. Also why is signed overflow a problem and not unsigned? Surely you want unsigned loop optimized also?
- astrange 5y agoUnsigned overflow is not UB (it wraps) so it has to be preserved more often, which means you have a loop bounds less often, which means those loops can't be optimized. Typically not a problem for i=0...n loops but eg `for (i=0;i<n;i+=2)` could overflow.