www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13231] New: Safe left shift for checkedint


          Issue ID: 13231
           Summary: Safe left shift for checkedint
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: bearophile_hugs eml.cc

The purpose of checkedint functions/intrinsics is to detect overflows and bugs.

When you have a number x of type int/uint/long/ulong where one or more of the
most n significant bits is set to 1, and you shift x on left by n, you lose
some information, so you have an overflow.

So I suggest to add to the checkedint.d module the leftShift functions that
perform a checked left shifting. They should also warn against too much large

leftShift(in uint x, in uint n, ref bool overflow)
leftShift(in ulong x, in uint n, ref bool overflow)

They check that:
1) n is in the appropriate range [0, sizeof(x) * 8]. Otherwise they set
overflow to true.
2) None of the n most significant bits of x is 1 before the shifting.

Jul 31 2014