apparent integer promotions bug

David Holland dholland@eecs.harvard.edu
Fri Mar 29 14:42:00 GMT 2002


It appears that left-shifting an unsigned value is generating a signed
value, but only when a bitfield is involved.

Test case follows.


/*
 * test.c
 * Compile with gcc -W -c test.c
 *
 * Generates "comparison between signed and unsigned" warning which
 * I believe to be incorrect.
 *
 * Observed with:
 *   gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release)
 *   (NetBSD 1.5W current i386 gcc)
 *
 * and also with
 *   gcc version 3.0.3
 *   built with --target=mips-linux, --nfp, and --disable-shared, running
 *      on i386-netbsd.
 */

struct foo {
   unsigned field:29;
};

int bar(struct foo *f, unsigned val) {
   // doesn't warn
   //unsigned tmp = (unsigned)f->field;
   //return ((tmp) << 3U) < val;

   // doesn't warn
   //return ((unsigned)(((unsigned)f->field) << 3U)) < val;

   // doesn't warn
   //return (((unsigned)f->field) * 8U) < val;

   // warns (correctly)
   //return (((unsigned)f->field) * 8) < val;

   // warns (incorrectly)
   return (((unsigned)f->field) << 3U) < val;
}

-- 
   - David A. Holland / dholland [at] eecs.harvard.edu



More information about the Gcc-bugs mailing list