This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Bogus testcase?
- From: Joe Buck <Joe dot Buck at synopsys dot COM>
- To: Jeffrey A Law <law at redhat dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Tue, 15 Nov 2005 13:31:05 -0800
- Subject: Re: Bogus testcase?
- References: <1132089344.484.56.camel@slowpoke>
On Tue, Nov 15, 2005 at 02:15:44PM -0700, Jeffrey A Law wrote:
>
> So, is it just me or does execute/930529-1.c invoke undefined or
> implementation defined behavior due to its reliance upon overflow
> behavior for signed types?
>
> In particular look at the control for the second loop:
>
> int i;
> [ ... ]
>
> for (i = ((unsigned) ~0 >> 1) - 3; i <= ((unsigned) ~0 >> 1) + 3; i++)
>
>
> i <= ((unsigned)~0>>1) + 3
>
> Seems like it overflows to me, or would cause "i" to have to
> overflow to terminate the loop.
There is no overflow in the test; the RHS is an unsigned expression
(mix signed and unsigned, you get unsigned). This means that the
termination test is equivalent to
unsigned(i) <= ((unsigned)~0>>1)+3
or
unsigned(i) <= (~0U>>1U)+3U
However, the test does rely on the autoincrement wrapping around;
specifically that i++ sets i to (int)(((unsigned)i)+1U).
Perhaps the test could be rewritten as
unsigned u;
for (u = (~0U >> 1) - 3U; u <= (~0U >> 1) + 3U; u++)
{
int i = (int)u;
...
}