Why isn't the overflow flag set?

Steven Schveighoffer schveiguy at yahoo.com
Thu Nov 10 09:56:37 PST 2011


On Thu, 10 Nov 2011 12:24:30 -0500, Simen Kjærås <simen.kjaras at 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).

>
> 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.

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


More information about the Digitalmars-d-learn mailing list