Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / regression / tests / bitintshift.c.in
blobe803df7e366a232a0393127d6045f282712702f9
1 /** shifts of bit-precise integers.
3 width: 7, 8, 9, 16, 24, 32, 33, 40, 48, 63, 64
4 count: 1, 4, 5, 7, 8, 9, 16, 20, 33, 40, 47, 48
5 varstorage: auto, static
6 countstorage: auto, static
7 direction: 0, 1
8 */
10 #include <testfwk.h>
12 // clang 11 supports bit-precise types, but deviates a bit from C23.
13 #if __clang_major__ == 11
14 #define __SDCC_BITINT_MAXWIDTH 128
15 #define _BitInt _ExtInt
16 #endif
18 #if __SDCC_BITINT_MAXWIDTH >= {width} // TODO: When we can regression-test in --std-c23 mode, use the standard macro from limits.h instead!
19 typedef unsigned _BitInt({width}) bitinttype;
20 typedef unsigned _BitInt(8) counttype;
21 #else
22 #if {width} <= 32
23 typedef unsigned int bitinttype;
24 #else
25 typedef unsigned long bitinttype;
26 #endif
27 typedef unsigned char counttype;
28 #endif
30 #define OPERAND0 ((bitinttype)0xa5b6c7d8e9fa)
31 #define OPERAND1 ((bitinttype)0x08192a3b4c5d6e7f)
33 // Lack of memory on pdk and mcs51
34 #if defined (__SDCC_pdk14) || defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)
35 #define MAXWIDTH 32
36 #elif defined (__SDCC_pdk15)
37 #define MAXWIDTH 40
38 #elif defined (__SDCC_mcs51) && defined(__SDCC_MODEL_SMALL)
39 #define MAXWIDTH 48
40 #else
41 #define MAXWDITH 64
42 #endif
44 bitinttype setoperand0(void)
46 volatile bitinttype tmp = OPERAND0;
47 return(tmp);
50 bitinttype setoperand1(void)
52 volatile bitinttype tmp = OPERAND1;
53 return(tmp);
56 counttype setcount(void)
58 volatile counttype tmp = {count};
59 return(tmp);
62 void testleftshift(void)
64 #if {width} <= MAXWIDTH && {count} < {width} && {direction} == 0
65 {varstorage} bitinttype operand, result;
66 {countstorage} counttype count;
68 count = setcount();
70 operand = setoperand0();
71 result = operand << count;
72 ASSERT (result == (OPERAND0 << {count}));
74 operand = setoperand1();
75 result = operand << count;
76 ASSERT (result == (OPERAND1 << {count}));
77 #endif
80 void testrightshift(void)
82 #if {width} <= MAXWIDTH && {count} < {width} && {direction} == 1
83 {varstorage} bitinttype operand, result;
84 {countstorage} counttype count;
86 count = setcount();
88 operand = setoperand0();
89 result = operand >> count;
90 ASSERT (result == (OPERAND0 >> {count}));
92 operand = setoperand1();
93 result = operand >> count;
94 ASSERT (result == (OPERAND1 >> {count}));
95 #endif