OT (partially): about promotion of integers

Timon Gehr timon.gehr at gmx.ch
Wed Dec 12 15:29:46 PST 2012


On 12/12/2012 10:25 PM, Walter Bright wrote:
> On 12/12/2012 4:51 AM, Araq wrote:
>> ...
>> So how does D improve on C's model? If signed integers are required to
>> wrap
>> around in D (no undefined behaviour), you also prevent some otherwise
>> possible
>> optimizations (there is a reason it's still undefined behaviour in C).
>
> D requires 2's complement arithmetic, it does not support 1's complement
> as C does.

I think what he is talking about is that in C, if after a few steps of 
inlining and constant propagation you end up with something like:

int x;
// ...
if(x>x+1) {
   // lots and lots of code
}else return 0;

Then a C compiler will assume that the addition does not overflow and 
reduce the code to 'return 0;', whereas a D compiler will not apply this 
optimization as it might change the semantics of valid D programs.


More information about the Digitalmars-d mailing list