Why isn't the overflow flag set?

Simon s.d.hammett at gmail.com
Fri Nov 11 10:19:03 PST 2011


On 10/11/2011 18:46, Simen Kjærås wrote:
> On Thu, 10 Nov 2011 18:56:37 +0100, Steven Schveighoffer
> <schveiguy at yahoo.com> wrote:
>
>> 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).
>
> 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


More information about the Digitalmars-d-learn mailing list