Numeric limits tracking
John Colvin
john.loughran.colvin at gmail.com
Sun May 12 17:18:05 PDT 2013
On Monday, 13 May 2013 at 00:00:54 UTC, Manu wrote:
> So, here's an issue that constantly drives me nuts, and an
> elegant solution
> seems so do-able.
>
> void func(int x)
> {
> x &= 0xFFFF;
> short s = x; // Error! (but we know x is 0 .. 65535)
>
> if(x < 256)
> {
> byte b = x; // Error! (we also know x is 0 .. 255)
> }
> }
>
> It would be really nice if the compiler would carry around the
> known
> possible range of values, and refer to that information when
> performing
> down-casting assignments.
> It would also be very useful information for the back end, it
> can choose
> more efficient types if it knows the range, or produce jump
> tables rather
> than branch sequences.
>
> I think the compiler would need a min, max, and mask for any
> integers, and
> update them as it works.
>
> There are many sources of this information.
> Comparisons effectively limit the range:
> if(x > 0)
> {
> // x = 0 .. int.max
> }
> else
> {
> // x = int.min .. -1
> }
>
> Masks, obviously:
> x &= 15;
>
> Also contracts are a really great source of seeding this
> information on
> function entry.
>
> Has this been discussed? It seems simple, and it's invisible to
> the
> language. It would just reduce some boilerplate (tedious
> casts), and also
> offer some great optimisation opportunities.
I've been thinking about this for a while from an optimisation
point of view. Is this not a common practice for an optimising
compiler?
More information about the Digitalmars-d
mailing list