On 08/21/2013 03:49 PM, Andy Falanga (afalanga) wrote: >> -----Original Message----- >> From: gcc-help-owner@xxxxxxxxxxx [mailto:gcc-help-owner@xxxxxxxxxxx] On >> Behalf Of Andrew Haley >> Sent: Tuesday, August 20, 2013 1:06 PM >> To: Brian Budge >> Cc: GCC-help >> Subject: Re: bad optimization >> >> On 08/20/2013 07:34 PM, Brian Budge wrote: >>> Is this an example of undefined behavior, or should I file a bug? >> >> It's an example of undefined behavior. No bug. > > For the ignorant (me), can you explain why it's undefined? Signed integer overflow is always undefined in C++: (5/5 C++03, 5/4 C++11) If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. (3.9.1/4) Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2^n where n is the number of bits in the value representation of that particular size of integer. This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type. Andrew.