1 /* Copyright 2002 Free Software Foundation, Inc.
2 
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.
6 
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).
12 
13    Source: Neil Booth, 25 Jan 2002, based on PR 3325 (and 3326, which
14    is a different manifestation of the same bug).
15 */
16 
17 extern void abort ();
18 
19 int
main(int argc,char * argv[])20 main(int argc, char *argv[])
21 {
22   struct x { signed int i : 7; unsigned int u : 7; } bit;
23 
24   unsigned int u;
25   int i;
26   unsigned int unsigned_result = -13U % 61;
27   int signed_result = -13 % 61;
28 
29   bit.u = 61, u = 61;
30   bit.i = -13, i = -13;
31 
32   if (i % u != unsigned_result)
33     abort ();
34   if (i % (unsigned int) u != unsigned_result)
35     abort ();
36 
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 ();
41 
42   if (bit.i % bit.u != signed_result)
43     abort ();
44 
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 ();
49 
50   if (bit.i % (unsigned int) bit.u != unsigned_result)
51     abort ();
52 
53   return 0;
54 }
55