www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why isn't the overflow flag set?

reply =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
I'm creating a Checked!T struct for integral values. The version attached  
works
for the most part, but some parts confuse me, namely that the following
operations do not set the overflow flag:

uint.max + 1
ulong.max + 1

int.min - 1
uint.min - 1
long.min - 1
ulong.min - 1

uint.max << 1
long.max << 1
ulong.max << 1

int.min << 1
long.min << 1

int a = int.max; a *= 2
long a = long.max; a *= 2

The following feel wrong to me, but I guess there may be reasons behind  
them:

ubyte a = ubyte.min; --a
ushort a = ushort.min; --a
uint a = uint.min; --a
ulong a = ulong.min; --a

ubyte a = ubyte.max; ++a
ushort a = ushort.max; ++a
uint a = uint.max; ++a
ulong a = ulong.max; ++a

ubyte a = ubyte.max; a *= 2
ushort a = ushort.max; a *= 2
uint a = uint.max; a *= 2
ulong a = ulong.max; a *= 2

ubyte a = ubyte.min; a -= 1
ushort a = ushort.min; a -= 1
uint a = uint.min; a -= 1
ulong a = ulong.min; a -= 1

ubyte a = ubyte.max; a <<= 1
ushort a = ushort.max; a <<= 1
uint a = uint.max; a <<= 1
ulong a = ulong.max; a <<= 1

Are some of these supposed to work but don't?
Nov 10 2011
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 10 Nov 2011 12:24:30 -0500, Simen Kj=C3=A6r=C3=A5s <simen.kjaras=
 gmail.com>  =

wrote:

 I'm creating a Checked!T struct for integral values. The version attac=
hed
 works
 for the most part, but some parts confuse me, namely that the followin=
g
 operations do not set the overflow flag:

 uint.max + 1
 ulong.max + 1

 int.min - 1
 uint.min - 1
 long.min - 1
 ulong.min - 1

 uint.max << 1
 long.max << 1
 ulong.max << 1

 int.min << 1
 long.min << 1
These are likely folded into a single constant. I think it would be = inappropriate for these to set the flag (it would be something unnatural= = anyways).
 int a =3D int.max; a *=3D 2
 long a =3D long.max; a *=3D 2
These I would think could set the overflow flag, but not necessarily whe= n = optimized. I suspect in order to prevent optimization, you may have to do the = operations using asm. Which would be difficult to do (to say the least)= = using a template parameter. -Steve
Nov 10 2011
parent reply =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
On Thu, 10 Nov 2011 18:56:37 +0100, Steven Schveighoffer  =

<schveiguy yahoo.com> wrote:

 On Thu, 10 Nov 2011 12:24:30 -0500, Simen Kj=C3=A6r=C3=A5s  =
 <simen.kjaras gmail.com> wrote:

 I'm creating a Checked!T struct for integral values. The version  =
 attached
 works
 for the most part, but some parts confuse me, namely that the followi=
ng
 operations do not set the overflow flag:

 uint.max + 1
 ulong.max + 1

 int.min - 1
 uint.min - 1
 long.min - 1
 ulong.min - 1

 uint.max << 1
 long.max << 1
 ulong.max << 1

 int.min << 1
 long.min << 1
These are likely folded into a single constant. I think it would be =
 inappropriate for these to set the flag (it would be something unnatur=
al =
 anyways).
Sorry, my example was simplified. These are all on the form uint a =3D uint.max; a =3D a + 1; It works for all integral types not mentioned here.
 int a =3D int.max; a *=3D 2
 long a =3D long.max; a *=3D 2
These I would think could set the overflow flag, but not necessarily =
 when optimized.
No optimizations are on, and -debug is passed. I find it unlikely the compiler is doing much magic here. If it does, I am more than willing to call it a bug.
Nov 10 2011
parent reply Simon <s.d.hammett gmail.com> writes:
On 10/11/2011 18:46, Simen Kjærås wrote:
 On Thu, 10 Nov 2011 18:56:37 +0100, Steven Schveighoffer
 <schveiguy yahoo.com> wrote:

 On Thu, 10 Nov 2011 12:24:30 -0500, Simen Kjærås
 <simen.kjaras gmail.com> wrote:

 I'm creating a Checked!T struct for integral values. The version
 attached
 works
 for the most part, but some parts confuse me, namely that the following
 operations do not set the overflow flag:

 uint.max + 1
 ulong.max + 1

 int.min - 1
 uint.min - 1
 long.min - 1
 ulong.min - 1

 uint.max << 1
 long.max << 1
 ulong.max << 1

 int.min << 1
 long.min << 1
These are likely folded into a single constant. I think it would be inappropriate for these to set the flag (it would be something unnatural anyways).
Sorry, my example was simplified. These are all on the form uint a = uint.max; a = a + 1; It works for all integral types not mentioned here.
 int a = int.max; a *= 2
 long a = long.max; a *= 2
These I would think could set the overflow flag, but not necessarily when optimized.
No optimizations are on, and -debug is passed. I find it unlikely the compiler is doing much magic here. If it does, I am more than willing to call it a bug.
Don't know if it's changed but the last time I looked at the compiler, it still does constant folding even in debug mode. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Nov 11 2011
parent =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
On Fri, 11 Nov 2011 19:19:03 +0100, Simon <s.d.hammett gmail.com> wrote:=


 On 10/11/2011 18:46, Simen Kj=C3=A6r=C3=A5s wrote:
 On Thu, 10 Nov 2011 18:56:37 +0100, Steven Schveighoffer
 <schveiguy yahoo.com> wrote:

 On Thu, 10 Nov 2011 12:24:30 -0500, Simen Kj=C3=A6r=C3=A5s
 <simen.kjaras gmail.com> wrote:

 I'm creating a Checked!T struct for integral values. The version
 attached
 works
 for the most part, but some parts confuse me, namely that the  =
 following
 operations do not set the overflow flag:

 uint.max + 1
 ulong.max + 1

 int.min - 1
 uint.min - 1
 long.min - 1
 ulong.min - 1

 uint.max << 1
 long.max << 1
 ulong.max << 1

 int.min << 1
 long.min << 1
These are likely folded into a single constant. I think it would be inappropriate for these to set the flag (it would be something unnatural anyways).
Sorry, my example was simplified. These are all on the form uint a =3D uint.max; a =3D a + 1; It works for all integral types not mentioned here.
 int a =3D int.max; a *=3D 2
 long a =3D long.max; a *=3D 2
These I would think could set the overflow flag, but not necessarily=
 when optimized.
No optimizations are on, and -debug is passed. I find it unlikely the=
 compiler is doing much magic here. If it does, I am more than willing=
 to call it a bug.
Don't know if it's changed but the last time I looked at the compiler,=
=
 it still does constant folding even in debug mode.
That may be, but these are not folded, at least not for the types not mentioned above.
Nov 11 2011
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 11/10/2011 06:24 PM, Simen Kjærås wrote:
 I'm creating a Checked!T struct for integral values. The version
 attached works
 for the most part, but some parts confuse me, namely that the following
 operations do not set the overflow flag:

 uint.max + 1
 ulong.max + 1

 int.min - 1
 uint.min - 1
 long.min - 1
 ulong.min - 1

 uint.max << 1
 long.max << 1
 ulong.max << 1

 int.min << 1
 long.min << 1

 int a = int.max; a *= 2
 long a = long.max; a *= 2

 The following feel wrong to me, but I guess there may be reasons behind
 them:

 ubyte a = ubyte.min; --a
 ushort a = ushort.min; --a
 uint a = uint.min; --a
 ulong a = ulong.min; --a

 ubyte a = ubyte.max; ++a
 ushort a = ushort.max; ++a
 uint a = uint.max; ++a
 ulong a = ulong.max; ++a

 ubyte a = ubyte.max; a *= 2
 ushort a = ushort.max; a *= 2
 uint a = uint.max; a *= 2
 ulong a = ulong.max; a *= 2

 ubyte a = ubyte.min; a -= 1
 ushort a = ushort.min; a -= 1
 uint a = uint.min; a -= 1
 ulong a = ulong.min; a -= 1

 ubyte a = ubyte.max; a <<= 1
 ushort a = ushort.max; a <<= 1
 uint a = uint.max; a <<= 1
 ulong a = ulong.max; a <<= 1

 Are some of these supposed to work but don't?
The overflow flag indicates that a signed overflow has occurred. To test for unsigned overflow, check the carry flag with jc.
Nov 10 2011