1 /** bit-precise integers
3 width: 2, 4, 7, 8, 9, 15, 16, 17, 24, 32, 33, 40, 48, 63, 64, 65
8 // clang 11 supports bit-precise types, but deviates a bit from C23.
9 #if __clang_major__ == 11
10 #define __SDCC_BITINT_MAXWIDTH 128
11 #define _BitInt _ExtInt
14 #if __SDCC_BITINT_MAXWIDTH >= {width} // TODO: When we can regression-test in --std-c23 mode, use the standard macro from limits.h instead!
15 #if {width} <= 40 || !defined (__SDCC_pdk14) // Lack of memory
17 typedef _BitInt({width
}) bitinttype
;
19 typedef unsigned _BitInt({width
}) ubitinttype
;
21 volatile bitinttype a
, b
;
22 volatile ubitinttype ua
, ub
;
28 void testBitIntArith(void)
30 #ifndef __clang_major__ // clang 11 doesn't support == between _BitInt and int
31 #if __SDCC_BITINT_MAXWIDTH >= {width} // TODO: When we can regression-test in --std-c23 mode, use the standard macro from limits.h instead!
32 #if ({width} <= 40 || !defined (__SDCC_pdk14)) && ({width} <= 48 || !defined (__SDCC_pdk15)) // Lack of memory
39 // From here on, we do the computation on the left at runtime, the one on the right at compile time, to check both.
42 ASSERT(a
<< 1 == (bitinttype
)(3) << 1);
43 ASSERT(ua
<< 1 == (ubitinttype
)(3) << 1);
44 ASSERT(ua
<< 3 == (ubitinttype
)(3) << 3);
46 ASSERT(a
>> 1 == (bitinttype
)(3) >> 1);
47 ASSERT(ua
>> 1 == (ubitinttype
)(3) >> 1);
49 ASSERT(ua
<< 1 == (ubitinttype
)(0xffffffff) << 1);
50 ASSERT(ua
>> 1 == (ubitinttype
)(0xffffffff) >> 1);
51 ASSERT(ua
<< c
== (ubitinttype
)(0xffffffff) << 1);
52 ASSERT(ua
>> c
== (ubitinttype
)(0xffffffff) >> 1);
57 #if {width} >= 8 // Signed overflow is undefined behaviour
58 ASSERT(a
+ b
== (bitinttype
)(23) + (bitinttype
)(-42));
59 ASSERT(a
- b
== (bitinttype
)(23) - (bitinttype
)(-42));
62 // Wrap on unsigned overflow
63 ASSERT(ua
+ ub
== (ubitinttype
)(23) + (ubitinttype
)(-42));
64 ASSERT(ua
- ub
== (ubitinttype
)(23) - (ubitinttype
)(-42));
66 #if !defined(__SDCC_pdk13) && !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
68 #if {width} >= 11 // Signed overflow is undefined behaviour
69 ASSERT(a
* b
== (bitinttype
)(23) * (bitinttype
)(-42));
72 #if {width} >= 8 // Signed overflow is undefined behaviour
73 ASSERT(a
% b
== (bitinttype
)(23) % (bitinttype
)(-42));
74 ASSERT(a
/ b
== (bitinttype
)(23) / (bitinttype
)(-42));
77 ASSERT(ua
* ub
== (ubitinttype
)(23) * (ubitinttype
)(-42));
78 ASSERT(ua
% ub
== (ubitinttype
)(23) % (ubitinttype
)(-42));
79 ASSERT(ua
/ ub
== (ubitinttype
)(23) / (ubitinttype
)(-42));
81 #if !defined(__SDCC_mcs51) && !defined(__SDCC_ds390) // Bug #3355
82 ASSERT((a
< b
) == ((bitinttype
)(23) < (bitinttype
)(-42)));
83 ASSERT((a
<= b
) == ((bitinttype
)(23) <= (bitinttype
)(-42)));
84 ASSERT((ua
< ub
) == ((ubitinttype
)(23) < (ubitinttype
)(-42)));
85 ASSERT((ua
<= ub
) == ((ubitinttype
)(23) <= (ubitinttype
)(-42)));
88 ASSERT((ua
& ub
) == ((ubitinttype
)(23) & (ubitinttype
)(-42)));
89 ASSERT((ua
| ub
) == ((ubitinttype
)(23) | (ubitinttype
)(-42)));
90 ASSERT((ua
^ ub
) == ((ubitinttype
)(23) ^ (ubitinttype
)(-42)));
92 // Test increment / decrement and it wraparound for unsigned _BitInt.
95 ASSERT(--ua
== (ubitinttype
)0xffffffffffffffffull
);