This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Efficient detection of signed overflow?


>> The test was, if I recall correctly
>>
>>   x = a + b;
>>   if ((x ^ a) & (x ^ b)) < 0)
>>
>> all you have to do is convert everything to unsigned values, then
>>
>>   ux = ua + ub;
>>   if ((ux ^ ua) & (ux ^ ub)) & (unsigned)INT_MIN))
>>     goto deal_with_overflow;
>>   // we now know there is no overflow
>>   x = ux;
>>
>> which is exactly the same test as before, but perfectly compliant.
>
> The comment is wrong.  The code checks for signed overflow, but the
> following assignment still overflwos when ux is larger than INT_MAX.

No, it doesn't.  This conversion is implementation-defined (6.3.1.3/3),
and GCC does the obvious two's complement thing.  This code is fine.


Segher


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]