Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / regression / tests / rotate2.c.in
blob33084ffe07b69db5c7646462d7fd059cb8a06e56
1 /** Tests covering RRC, RLC & SWAP operations
3 size: 8, 16, 32
4 andCase: 0, 1 , 2
5 xorLiteral: 0, 1
6 rotateLeft: 1, -1, 0
7 structVar: 0, 1
9 size. 32
10 andCase. 0
11 xorLiteral. 1
12 rotateLeft. -1, 1, 0
13 structVar. 0, 1
16 #include <testfwk.h>
17 #ifdef __sun__
18 #include <inttypes.h>
19 #else
20 #include <stdint.h>
21 #endif
23 #define SIZE ({size})
24 #define AND_CASE ({andCase})
25 #define XOR_LITERAL ({xorLiteral})
26 #define ROTATE_LEFT ({rotateLeft})
27 #define STRUCT_VAR ({structVar})
29 #if SIZE == 8
30 #define TYPE uint8_t
31 #define TEST_VALUE 0x5B
32 #if AND_CASE == 2
33 #define AND_VALUE 0xFE
34 #elif AND_CASE == 1
35 #define AND_VALUE 0xEF
36 #else
37 #undef AND_VALUE
38 #endif
39 #endif
41 #if SIZE == 16
42 #define TYPE uint16_t
43 #define TEST_VALUE 0x3579
44 #if AND_CASE == 2
45 #define AND_VALUE 0xFEFF
46 #elif AND_CASE == 1
47 #define AND_VALUE 0xFFEF
48 #else
49 #undef AND_VALUE
50 #endif
51 #endif
53 #if SIZE == 32
54 #define TYPE uint32_t
55 #define TEST_VALUE 0x13579BDF
56 #if AND_CASE == 2
57 #define AND_VALUE 0xFEFFFEFF
58 #elif AND_CASE == 1
59 #define AND_VALUE 0xFFEFFFEF
60 #else
61 #undef AND_VALUE
62 #endif
63 #endif
65 #if ROTATE_LEFT == 1
66 #define SHIFT_R (SIZE - 1)
67 #define SHIFT_L 1
69 #elif ROTATE_LEFT == -1
70 #define SHIFT_R 1
71 #define SHIFT_L (SIZE - 1)
72 #elif ROTATE_LEFT == 0
73 #define SHIFT_R (SIZE / 2)
74 #define SHIFT_L (SIZE / 2)
75 #else
76 #error "UNKNOWN SHIFT CASE"
77 #endif
79 #ifdef AND_VALUE
80 #define AND_OPERATION & AND_VALUE
81 #else
82 #define AND_OPERATION
83 #endif
85 #define XOR_VALUE 0x24
87 #if XOR_LITERAL
88 #define rotate_test_value_xor XOR_VALUE
89 #else
90 volatile TYPE rotate_test_value_xor = XOR_VALUE;
91 #endif
93 #define ROTATE_RESULT ((TYPE)(((TEST_VALUE << SHIFT_L) | (TEST_VALUE >> SHIFT_R)) AND_OPERATION))
94 #define ROTATE_RESULT_XOR ((TYPE)((((TEST_VALUE ^ XOR_VALUE) << SHIFT_L) | ((TEST_VALUE ^ XOR_VALUE) >> SHIFT_R)) AND_OPERATION))
96 #if STRUCT_VAR
97 typedef struct
99 TYPE rotate_test_value1;
100 TYPE rotate_test_value2;
101 }rotate_test_struct_;
103 rotate_test_struct_ rotate_test_struct;
104 #define rotate_test_value rotate_test_struct.rotate_test_value2
105 #else
106 TYPE rotate_test_value;
107 #endif
109 TYPE rotate_test_1(TYPE value)
111 return ((value << SHIFT_L) | (value >> SHIFT_R)) AND_OPERATION;
114 TYPE rotate_test_1_xor1(TYPE value)
116 value = value ^ rotate_test_value_xor;
117 return ((value << SHIFT_L) | (value >> SHIFT_R)) AND_OPERATION;
120 TYPE rotate_test_1_xor2(TYPE value)
122 return (((value ^ rotate_test_value_xor) << SHIFT_L) | ((value ^ rotate_test_value_xor) >> SHIFT_R)) AND_OPERATION;
125 #if !(defined(__SDCC_pdk14) || defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of memory
127 TYPE rotate_test_2(TYPE value)
129 TYPE value2 = value;
130 return ((value2 << SHIFT_L) | (value2 >> SHIFT_R)) AND_OPERATION;
133 TYPE rotate_test_2_xor1(TYPE value)
135 TYPE value2 = value ^ rotate_test_value_xor;
136 return ((value2 << SHIFT_L) | (value2 >> SHIFT_R)) AND_OPERATION;
139 TYPE rotate_test_2_xor2(TYPE value)
141 TYPE value2 = value;
142 return (((value2 ^ rotate_test_value_xor) << SHIFT_L) | ((value2 ^ rotate_test_value_xor) >> SHIFT_R)) AND_OPERATION;
145 TYPE rotate_test_3(TYPE value)
147 volatile TYPE value2 = value;
148 value2 = ((value2 << SHIFT_L) | (value2 >> SHIFT_R)) AND_OPERATION;
149 return value2;
152 TYPE rotate_test_3_xor1(TYPE value)
154 volatile TYPE value2 = value ^ rotate_test_value_xor;
155 value2 = ((value2 << SHIFT_L) | (value2 >> SHIFT_R)) AND_OPERATION;
156 return value2;
159 TYPE rotate_test_3_xor2(TYPE value)
161 volatile TYPE value2 = value;
162 value2 = (((value2 ^ rotate_test_value_xor) << SHIFT_L) | ((value2 ^ rotate_test_value_xor) >> SHIFT_R)) AND_OPERATION;
163 return value2;
166 #ifndef __SDCC_pdk15 // Lack of memory
168 TYPE rotate_test_4(TYPE value)
170 rotate_test_value = value;
171 rotate_test_value = ((rotate_test_value << SHIFT_L) | (rotate_test_value >> SHIFT_R)) AND_OPERATION;
172 return rotate_test_value;
175 TYPE rotate_test_4_xor1(TYPE value)
177 rotate_test_value = value ^ rotate_test_value_xor;
178 rotate_test_value = ((rotate_test_value << SHIFT_L) | (rotate_test_value >> SHIFT_R)) AND_OPERATION;
179 return rotate_test_value;
182 TYPE rotate_test_4_xor2(TYPE value)
184 rotate_test_value = value;
185 rotate_test_value = (((rotate_test_value ^ rotate_test_value_xor) << SHIFT_L) | ((rotate_test_value ^ rotate_test_value_xor) >> SHIFT_R)) AND_OPERATION;
186 return rotate_test_value;
189 void rotate_test_load(TYPE value)
191 rotate_test_value = value;
194 TYPE rotate_test_5(TYPE value)
196 rotate_test_load (value);
197 rotate_test_value = ((rotate_test_value << SHIFT_L) | (rotate_test_value >> SHIFT_R)) AND_OPERATION;
198 return rotate_test_value;
201 TYPE rotate_test_5_xor1(TYPE value)
203 rotate_test_load (value ^ rotate_test_value_xor);
204 rotate_test_value = ((rotate_test_value << SHIFT_L) | (rotate_test_value >> SHIFT_R)) AND_OPERATION;
205 return rotate_test_value;
208 TYPE rotate_test_5_xor2(TYPE value)
210 rotate_test_load (value);
211 rotate_test_value = (((rotate_test_value ^ rotate_test_value_xor) << SHIFT_L) | ((rotate_test_value ^ rotate_test_value_xor) >> SHIFT_R)) AND_OPERATION;
212 return rotate_test_value;
214 #endif
215 #endif
217 static void
218 testSwaps(void)
220 #if !(defined(__SDCC_pdk14) || defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of memory
221 volatile TYPE t = TEST_VALUE;
222 TYPE u;
224 u = t;
226 ASSERT( rotate_test_1(u) == ROTATE_RESULT);
227 ASSERT( rotate_test_1_xor1(u) == ROTATE_RESULT_XOR);
228 ASSERT( rotate_test_1_xor2(u) == ROTATE_RESULT_XOR);
230 ASSERT( rotate_test_2(u) == ROTATE_RESULT);
231 ASSERT( rotate_test_2_xor1(u) == ROTATE_RESULT_XOR);
232 ASSERT( rotate_test_2_xor2(u) == ROTATE_RESULT_XOR);
234 ASSERT( rotate_test_3(u) == ROTATE_RESULT);
235 ASSERT( rotate_test_3_xor1(u) == ROTATE_RESULT_XOR);
236 ASSERT( rotate_test_3_xor2(u) == ROTATE_RESULT_XOR);
238 #ifndef __SDCC_pdk15 // Lack of memory
239 ASSERT( rotate_test_4(u) == ROTATE_RESULT);
240 ASSERT( rotate_test_4_xor1(u) == ROTATE_RESULT_XOR);
241 ASSERT( rotate_test_4_xor2(u) == ROTATE_RESULT_XOR);
243 ASSERT( rotate_test_5(u) == ROTATE_RESULT);
244 ASSERT( rotate_test_5_xor1(u) == ROTATE_RESULT_XOR);
245 ASSERT( rotate_test_5_xor2(u) == ROTATE_RESULT_XOR);
246 #endif
247 #endif