struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / widebitintbitfield.c.in
bloba39e8450acf5a347ba0197cdc6e5067828edff48
1 /** bit-precise integer types allow wider bit-fields than int.
3 width: 7, 17, 24, 32, 33, 40, 48, 63, 64, 65
4 sign: unsigned, signed
5 */
7 #include <testfwk.h>
9 #include <stdbool.h>
11 // clang 11 supports bit-precise types, but deviates a bit from C23.
12 #if __clang_major__ == 11
13 #define __SDCC_BITINT_MAXWIDTH 128
14 #define _BitInt _ExtInt
15 #endif
17 #if __SDCC_BITINT_MAXWIDTH >= {width} // TODO: When we can regression-test in --std-c23 mode, use the standard macro from limits.h instead!
18 typedef {sign} _BitInt({width}) bitinttype;
20 struct {
21 bitinttype bw : {width};
22 unsigned _BitInt(4) b4 : 4;
23 bool b : 1;
24 } bf = {5, 6, false};
26 struct {
27 bitinttype bw : {width};
28 unsigned _BitInt(4) b4 : 4;
29 bitinttype bw_1 : {width} - 1;
30 } bf2 = {2, 2, 5};
31 #endif
33 void testWideBitIntBitField(void)
35 #if __SDCC_BITINT_MAXWIDTH >= {width} // TODO: When we can regression-test in --std-c23 mode, use the standard macro from limits.h instead!
36 #ifndef __clang_major__ // clang 11 doesn't support == between _BitInt and int, and does weird stuff when promoting bit-fields.
37 ASSERT (_Generic(+bf2.bw, bitinttype: 1, default: 0) == 1);
38 ASSERT (_Generic(+bf2.bw_1, bitinttype: 1, default: 0) == 1);
40 ASSERT (bf.bw == 5);
41 ASSERT (bf.b4 == 6);
42 ASSERT (bf.b == false);
44 ASSERT (bf2.bw == 2);
45 ASSERT (bf2.b4 == 2);
46 ASSERT (bf2.bw_1 == 5);
48 bf.bw = 0x2a;
49 bf2.bw_1 = 0x15;
50 ASSERT (bf.bw == 0x2a);
51 ASSERT (bf2.bw_1 == 0x15);
52 #endif
53 #endif