[Issue 13231] New: Safe left shift for checkedint

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Jul 31 13:13:57 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=13231

          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 at puremagic.com
          Reporter: bearophile_hugs at 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
shifts:

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.

--


More information about the Digitalmars-d-bugs mailing list