-O flag ; automatic cast in a bitshift

Guillaume Lathoud gsub at glat.info
Thu Sep 20 11:02:02 UTC 2018


Hello,

the code below behaves differently when compiled with or without 
the -O flag (both DMD and LDC2).

Two questions:

(1) does the D language explicitly specifies what the following 
expression should do? If yes, where?

  '<ulong> |= <ubyte> << <ubyte>'

In the example below, there seems to be a cast to 32 bits first, 
then a shift, then a cast to 64 bits.

(2) why the different result when putting the -O flag?

Best regards,
Guillaume Lathoud
.

import std.stdio;

void main()
{
   ubyte b = 84;
   ulong l = 0;
   ubyte shift = 50;

   l |= b << shift;

   writefln( "%064b", l );

   /*
     DMD64 D Compiler v2.080.1

     dmd cast_question.d ; cast_question
     
0000000000000000000000000000000000000001010100000000000000000000

     dmd -O cast_question.d ; cast_question
     
0000000000000000000000000000000000000000000000000000000000000000
   */

   /*
     LDC - the LLVM D compiler (1.10.0):
     based on DMD v2.080.1 and LLVM 6.0.0

     ldmd2 cast_question.d ; cast_question
     
0000000000000000000000000000000000000001010100000000000000000000

     ldmd2 -O cast_question.d ; cast_question
     
0000000000000000000000000000000000000000000000000000000000000000
    */
}



More information about the Digitalmars-d-learn mailing list