[CLN-list] GCC 4.3 (experimental) -O2 dislikes intparam.h

Ralf Wildenhues Ralf.Wildenhues at gmx.de
Tue Dec 19 00:04:14 CET 2006


When compiled with optimization -O2 and higher, the SVN trunk version of
GCC miscompiles CLN's autoconf/intparam.c file.  I cannot reproduce this
with GCC-4.1.0 (no intermediate versions tested).  The consequence of
this is that the build fails very early due to #error's in the generated
intparam.h file.

I have reduced the difference to compiling the program below on
GNU/Linux x86:

$ gcc-4.3 -W -Wall -o foo foo.i
$ ./foo
/* Integers of type int have 32 bits. */
#define int_bitsize 32

$ gcc-4.3 -O2 -W -Wall -o foo foo.i
$ ./foo
#error "Integers of type int have no binary representation!!"

Now, I was about to report a compiler bug ... except that I think the
compiler is right here: integer overflow produces undefined behavior,
so I think intparam.c should be fixed instead.

With unsigned 'x', things are computed correctly.  I guess you could
just always use the respective unsigned variant for computing the
bitsize of a type.  (Using 'volatile int' helps with this GCC version,
but I don't think that is guaranteed by the standard.)

Next, please note that intparam.c uses exit but does not provide for a
prototype.  I recommend returning from main instead.

Furthermore, please note that the CLN headers cause many "might break
strict aliasing" warnings, and a few "will break strict aliasing"
warnings.  The latter are (at least) here:
  src/complex/ring/cl_C_ring.cc:131
  src/rational/ring/cl_RA_ring.cc:129
  src/real/ring/cl_R_ring.cc:133

Hope that helps.

Cheers,
Ralf

/* foo.i */
extern int printf (__const char *__restrict __format, ...);
static int int_bitsize;

int main(void)
{
  int x = 1;
  int bits = 0;
  while(1) {
    if (x==0)
      break;
    x = x+x;
    bits++;
    if (bits==1000) {
      bits = -1;
      break;
    }
  }
  int_bitsize = bits;
  if (int_bitsize >= 0) {
    printf("/* Integers of t%spe %s have %ld bits. */\n","y","int",(long)int_bitsize);
    if (!("int"[0] == 'u')) {
      printf("#define ");
      printf("int");
      printf("_bitsize %ld\n",(long)int_bitsize);
    }
    printf("\n");
  } else {
    printf("#error \"Integers of t%spe %s have no binary representation!!\"\n","y","int");
  }
  return 0;
}


More information about the CLN-list mailing list