Signed integer overflow undefined behavior or not?

Ola Fosheim Grøstad via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 13 01:18:10 PST 2015


On Friday, 13 November 2015 at 08:51:27 UTC, Ali Çehreli wrote:
> I understood Walter's response to be so.

That's my interpretation of what Walter has said before too. So a 
D compiler cannot prevent compilation of a statically detected 
wrapping (overflow). As a result D-integers are circular 
enumerations.

> On the other hand, in order to define the wrapping behavior at 
> all, one must speak of overflow first.

For educational purposes, probably :-)

> Wrapping is the solution for the condition of overflow, which D 
> must have to begin with, no? :)

For definition, not really. Signed integers are often defined as 
three functions (other definitions are possible):

Zero: 0
Successor of X: S
Predecessor of X: P

For a 2 bit signed modular arithmetics you would get the complete 
normalized set:

PP0, P0, 0, S0

with the defined rewrites

SPX = X
PSX = X
PPP0 = S0
SS0 = PP0

(implies PPPPX = X and SSSSX = X)

then you define the operators on the set (+,- etc) using 
relations such as PSX = X etc.



More information about the Digitalmars-d mailing list