This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

apparent integer promotions bug


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]