[testsuite] loop failure

Geoff Keating geoffk@cygnus.com
Wed Jun 21 12:24:00 GMT 2000


> Date: Wed, 21 Jun 2000 09:35:57 +0200
> From: Jakub Jelinek <jakub@redhat.com>

> Do you mean that
> 
> main()
> {
>   char c, d;
>   int nbits;
>   if (sizeof(int) < 4)
>     exit(0);
>   c = -1;
>   for (nbits = 1 ; nbits < 10; nbits++) {
>     d = (1 << nbits) - 1;
>     if (d == c)
>       break;
>   }
>   if (nbits != 10)
>     abort();
>   exit(0);
> }
> 
> should have defined behaviour and never call abort()?

No, because assigning 255 to a signed char is overflow, and if char is
unsigned then the test will stop with nbits==8.

> It aborts on sparc with -O0, -O1, works with -O2 and above.

Hmmm.  Interesting.

> Similarly,
> 
> main()
> {
>   char c;
>   unsigned char d;
>   int nbits;
>   if (sizeof(int) < 4)
>     exit(0);
>   c = -1;
>   for (nbits = 1 ; nbits < 10; nbits++) {
>     d = (1 << nbits) - 1;
>     if (d == c)
>       break;
>   }
>   if (nbits == 10)
>     abort();
>   exit(0);
> }
> 
> aborts on sparc with -O0, -O1 and works with -O2 and above.
> (note unsigned char and == instead of !=).

Now, _this_ is close to the right test.  It should always abort,
because no unsigned value should ever be equal to -1 (remember, both
'd' and 'c' get promoted to 'int' in the comparison, but only c is
sign-extended).

You don't have to check sizeof(int) because the testcase is valid even
if int is 16 bits, which is the minimum.

You probably should change 'char c' to 'signed char c', though.

> Unless all of this is undefined behaviour, there is some bug in gcc related
> to it.

Oh, I don't doubt that there's a bug somewhere, almost certainly in loop.

-- 
- Geoffrey Keating <geoffk@cygnus.com>


More information about the Gcc-patches mailing list