FSF GCC merge 02/23/03
[official-gcc.git] / gcc / testsuite / gcc.c-torture / execute / bitfld-1.c
blobb7e86e8103e2d5f0bb93f3317237706abb9a708f
1 /* Copyright 2002 Free Software Foundation, Inc.
3 Tests correct signedness of operations on bitfields; in particular
4 that integer promotions are done correctly, including the case when
5 casts are present.
7 The C front end was eliding the cast of an unsigned bitfield to
8 unsigned as a no-op, when in fact it forces a conversion to a
9 full-width unsigned int. (At the time of writing, the C++ front end
10 has a different bug; it erroneously promotes the uncast unsigned
11 bitfield to an unsigned int).
13 Source: Neil Booth, 25 Jan 2002, based on PR 3325 (and 3326, which
14 is a different manifestation of the same bug).
17 extern void abort ();
19 int
20 main(int argc, char *argv[])
22 struct x { signed int i : 7; unsigned int u : 7; } bit;
24 unsigned int u;
25 int i;
26 unsigned int unsigned_result = -13U % 61;
27 int signed_result = -13 % 61;
29 bit.u = 61, u = 61;
30 bit.i = -13, i = -13;
32 if (i % u != unsigned_result)
33 abort ();
34 if (i % (unsigned int) u != unsigned_result)
35 abort ();
37 /* Somewhat counter-intuitively, bit.u is promoted to an int, making
38 the operands and result an int. */
39 if (i % bit.u != signed_result)
40 abort ();
42 if (bit.i % bit.u != signed_result)
43 abort ();
45 /* But with a cast to unsigned int, the unsigned int is promoted to
46 itself as a no-op, and the operands and result are unsigned. */
47 if (i % (unsigned int) bit.u != unsigned_result)
48 abort ();
50 if (bit.i % (unsigned int) bit.u != unsigned_result)
51 abort ();
53 return 0;