Re:A bit of background on compilers exploiting signed overflow
by detrino on Sunday May 08, 2016 @02:15PM
(#11655035)
Your loop:
void TestSigned(int min, int max, int step) {
for (int i = max; i >= min; i -= step) F(i);
}
Becomes this with unsigned: void TestUnsigned(unsigned min, unsigned max, unsigned step) {
while (true) {
F(max);
if (max < min + step) break;
max -= step;
}
}
Now if I want to transform my loop to operate on pointers or iterators, the transformation is trivial: void TestUnsigned(unsigned *min, unsigned *max, unsigned step) {
while (true) {
F(*max);
if (max < min + step) break;
max -= step;
}
}
Quick: Do the same for yours.
Re:A bit of background on compilers exploiting signed overflow
by dsfuoi on Sunday May 08, 2016 @03:05PM
(#11655271)
It looks easy but it isn't.
The second example causes unexpected behavior (and probably ub later) if: min + step > MIN_TYPE_MAX
The last example causes undefined behavior if: max - min < step - 1.
Re:A bit of background on compilers exploiting signed overflow
by pbsd on Sunday May 08, 2016 @12:49PM
(#11654679)
Something like for(size_t i = 0; i <= n; i += k) { work_with(n - i); }?
This sort of loop always comes up in these discussions, and I was never convinced that signed integers were worth it for that alone.
Re:A bit of background on compilers exploiting signed overflow
by Sean1708 on Monday May 09, 2016 @10:02AM
(#11659890)
To be honest I think there are good arguments to both sides, and I don't think there is really a one size fits all solution here. If you use unsigned you've got to pay attention to certain corner cases, if you use signed you've got to pay attention to different corner cases, it's up to you to decide which is more common in your code.
Re:A bit of background on compilers exploiting signed overflow
by tzs on Sunday May 08, 2016 @12:40PM
(#11654646)
If n is not within k of the maximum unsigned integer:
i = n+k;
do {
i -= k;
...
} while (i >= k);
If n is close enough to the maximum unsigned integer for n+k to overflow: i = n;
goto skip_sub;
do {
i -= k;
skip_sub:
...
} while (i >= k);
I just use a signed pointer-size type for all sizes and counters, and stay happy...