This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: GCC optimizes integer overflow: bug or feature?
* Joseph S. Myers:
> On Tue, 19 Dec 2006, Florian Weimer wrote:
>
>> * Paolo Bonzini:
>>
>> > Interesting read. I agree with the proposed fix; however, note that
>> > GCC does not make the result of overflowing signed left-shifts
>> > undefined, exactly because in this case the overflow is relied upon by
>> > too many existing programs
>>
>> Is this documented somewhere? Without documentation, it could also be
>> an oversight (lack of optimization) from a programmer perspective.
>
> Certainly, in implement-c.texi:
>
> GCC does not use the latitude given in C99 only to treat certain
> aspects of signed @samp{<<} as undefined, but this is subject to
> change.
Thanks, I missed that paragraph. But I fail to see how this is
helpful in any way to a programmer. To me, it reads like "we've got
some predictable semantics in some cases, but we don't tell you what
they are, and if you rely on them by chance, your code may break with
the next compiler release, without notice".
Something like:
GCC does not use the latitude given in C99 only to treat
certain aspects of signed @samp{<<} as undefined: If the right
operand @var{n} is non-negative and less than the width of the
left operand @var{val}, the resulting value @code{@var{val} <<
@var{n}} is guaranteed to be equal to @var{val} times 2 to the
@var{n}th power under 2-complement arithmetic.
would probably settle the issue, but it's unwieldy.