Re:What Challenges and Trade-Offs Do Optimising Compilers Face?
by usefulcat on Thursday June 22, 2017 @11:41AM
(#14613003)
Assuming you're using gcc or something with similar options, you should have a look at -fwrapv if you haven't already.
Re:What Challenges and Trade-Offs Do Optimising Compilers Face?
by Animats on Thursday June 22, 2017 @02:02PM
(#14614073)
For C, the standard specifies that unsigned arithmetic wraps around, while integer arithmetic overflow is undefined.
I once suggested, back when there were machines that weren't byte-oriented or twos complement, that wraparound arithmetic should be requested with
unsigned int x;
x = (x + 1) % 0xffff;
or something similar. That way, you get the same result on all platforms, regardless of word length. If the hardware can truncate cheaply, the compiler can optimize "%" such expressions easily. Other than that, overflow should be an error.
I have used processors that have saturating arithmetic and I even used that feature, but even there compiler defaulted to the more common rollover behavior.