struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / bitintarith.c.in
blob90e7e1b7703b8924c65a057d3e0b1ff734de0495
1 /** bit-precise integers
3 width: 2, 4, 7, 8, 9, 15, 16, 17, 24, 32, 33, 40, 48, 63, 64, 65
4 */
6 #include <testfwk.h>
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
12 #endif
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;
23 volatile char c = 1;
25 #endif
26 #endif
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
34 a = -1, b = 1;
35 ASSERT(a + b == 0);
36 ua = 1, ub = 1;
37 ASSERT(ua + ub == 2);
39 // From here on, we do the computation on the left at runtime, the one on the right at compile time, to check both.
40 a = 3, ua = 3;
41 #if {width} >= 4
42 ASSERT(a << 1 == (bitinttype)(3) << 1);
43 ASSERT(ua << 1 == (ubitinttype)(3) << 1);
44 ASSERT(ua << 3 == (ubitinttype)(3) << 3);
45 #endif
46 ASSERT(a >> 1 == (bitinttype)(3) >> 1);
47 ASSERT(ua >> 1 == (ubitinttype)(3) >> 1);
48 ua = 0xffffffff;
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);
54 a = 23, b = -42;
55 ua = 23, ub = -42;
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));
60 #endif
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));
70 #endif
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));
75 #endif
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)));
86 #endif
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.
93 ua = 1;
94 ASSERT(--ua == 0);
95 ASSERT(--ua == (ubitinttype)0xffffffffffffffffull);
96 ASSERT(++ua == 0);
97 #endif
98 #endif
99 #endif
100 #endif