User:UplinkCoder/VRP

From D Wiki
Revision as of 17:30, 13 November 2014 by UplinkCoder (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Value Range Propagation / safe truncation

What I am going to write about is an important feature of D.

Safe implicit truncation.

One thing C is criticized for is it's relatively lax type system.

You can do things like : char c = 400;

With luck you will get a ,

warning: overflow in implicit conversion.

On modern compilers.

This is not compatible with D's phlisophy, "If it compiles, it's most likely correct."

That is why DMD will not compile code with narrowing conversions.

Execpt if it can proof that this conversion is safe.

To do that it needs meta-information about expressions.

This meta-information is the Range of Values this expression can have.

A ubyte for example can have the Values from 0 to 255 or [0,255]

That means the Expression u-2 with the Variable u of type ubyte has [-2,253]

the formula for minus is :

result.max = lhs.max - rhs.min

result.min = lhs.min - rhs.max