| 1 |
/* |
| 2 |
This test checks promotion of bitfields. Bitfields should be promoted |
| 3 |
very much like chars and shorts: |
| 4 |
|
| 5 |
Bitfields (signed or unsigned) should be promoted to signed int if their |
| 6 |
value will fit in a signed int, otherwise to an unsigned int if their |
| 7 |
value will fit in an unsigned int, otherwise we don't promote them (ANSI/ISO |
| 8 |
does not specify the behavior of bitfields larger than an unsigned int). |
| 9 |
|
| 10 |
We test the behavior by subtracting two from the promoted value: this will |
| 11 |
result in a negitive value for signed types, a positive value for unsigned |
| 12 |
types. This test (of course) assumes that the compiler is correctly |
| 13 |
implementing signed and unsigned arithmetic. |
| 14 |
*/ |
| 15 |
|
| 16 |
void abort (void); |
| 17 |
void exit (int); |
| 18 |
|
| 19 |
struct X { |
| 20 |
unsigned int u3:3; |
| 21 |
signed long int s31:31; |
| 22 |
signed long int s32:32; |
| 23 |
unsigned long int u31:31; |
| 24 |
unsigned long int u32:32; |
| 25 |
unsigned long long ull3 :3; |
| 26 |
unsigned long long ull35:35; |
| 27 |
unsigned u15:15; |
| 28 |
}; |
| 29 |
|
| 30 |
struct X x; |
| 31 |
|
| 32 |
int |
| 33 |
main (void) |
| 34 |
{ |
| 35 |
if ((x.u3 - 2) >= 0) /* promoted value should be signed */ |
| 36 |
abort (); |
| 37 |
|
| 38 |
if ((x.s31 - 2) >= 0) /* promoted value should be signed */ |
| 39 |
abort (); |
| 40 |
|
| 41 |
if ((x.s32 - 2) >= 0) /* promoted value should be signed */ |
| 42 |
abort (); |
| 43 |
|
| 44 |
if ((x.u15 - 2) >= 0) /* promoted value should be signed */ |
| 45 |
abort (); |
| 46 |
|
| 47 |
/* Conditionalize check on whether integers are 4 bytes or larger, i.e. |
| 48 |
larger than a 31 bit bitfield. */ |
| 49 |
if (sizeof (int) >= 4) |
| 50 |
{ |
| 51 |
if ((x.u31 - 2) >= 0) /* promoted value should be signed */ |
| 52 |
abort (); |
| 53 |
} |
| 54 |
else |
| 55 |
{ |
| 56 |
if ((x.u31 - 2) < 0) /* promoted value should be UNsigned */ |
| 57 |
abort (); |
| 58 |
} |
| 59 |
|
| 60 |
if ((x.u32 - 2) < 0) /* promoted value should be UNsigned */ |
| 61 |
abort (); |
| 62 |
|
| 63 |
if ((x.ull3 - 2) >= 0) /* promoted value should be signed */ |
| 64 |
abort (); |
| 65 |
|
| 66 |
if ((x.ull35 - 2) < 0) /* promoted value should be UNsigned */ |
| 67 |
abort (); |
| 68 |
|
| 69 |
exit (0); |
| 70 |
} |