OT (partially): about promotion of integers

Walter Bright newshound2 at digitalmars.com
Wed Dec 12 15:57:21 PST 2012


On 12/12/2012 3:29 PM, Timon Gehr wrote:
> 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.

You're right in that the D optimizer does not take advantage of C "undefined 
behavior" in its optimizations. The article mentioned that many bugs were caused 
not by the actual wraparound behavior, but by aggressive C optimizers that 
interpreted "undefined behavior" as not having to account for those cases.


More information about the Digitalmars-d mailing list