struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / longlong.c.in
blobf0d1c6ff10cb9e2d9f33c63e770291efac709380
1 /** Simple long long tests.
2 test: mul, div, bit, shift
4 */
5 #include <testfwk.h>
7 #ifdef __SDCC
8 #pragma std_sdcc99
9 #endif
11 #define TEST_{test}
13 #if !(defined(__SDCC_mcs51) && !defined(__SDCC_STACK_AUTO) && defined(__SDCC_MODEL_SMALL) ) && !defined(__SDCC_pic14) && !defined(__SDCC_pic16) && !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
14 long long x;
15 unsigned long long y;
16 int i;
18 #if defined(TEST_mul)
20 long long g(void)
22 int y = i + 1;
23 return (y);
26 long long h(void)
28 return (x);
31 long long c(void)
33 return (12ll);
36 long long d(int i)
38 return (i);
41 long long (*gp)(void) = &g;
43 static unsigned long long mulLL(unsigned long long a, unsigned long long b)
45 return a * b;
48 void
49 LongLong_mul (void)
51 volatile unsigned long tmp;
53 i = 42;
54 ASSERT (g() == 43);
55 i = 23;
56 ASSERT ((*gp)() == 24);
57 ASSERT (c() == 12);
58 x = 42;
59 ASSERT (h() == x);
60 ASSERT (d(12) == 12ll);
61 ASSERT ((x >> 1) == 21);
62 ASSERT ((x << 1) == 84);
63 ASSERT (!(x >> 17));
64 ASSERT ((x << 17) == (42l << 17));
65 y = x;
66 ASSERT (y == 42ull);
67 ASSERT ((y >> 1) == 21);
68 ASSERT ((y << 1) == 84);
69 ASSERT ((y >> 17) == 0);
70 ASSERT ((y << 17) == (42ul << 17));
71 ASSERT ((y << 16) == (42ul << 16));
73 tmp = 0xaaffaafful;
74 y = tmp;
75 ASSERT (c() == 12);
76 ASSERT ((unsigned char)y == (unsigned char)tmp);
77 ASSERT ((unsigned int)y == (unsigned int)tmp);
78 ASSERT ((unsigned long)y == (unsigned long)tmp);
79 ASSERT (y == tmp);
80 ASSERT ((y >> 8) == (tmp >>= 8));
81 ASSERT ((y >> 12) == (tmp >>= 4));
82 ASSERT ((y >> 16) == (tmp >>= 4));
83 ASSERT ((y >> 24) == (tmp >>= 8));
84 ASSERT ((y >> 32) == 0x0ul);
86 tmp = 1;
87 y = tmp;
88 ASSERT (c() == 12);
89 ASSERT ((y << 1) == 2);
90 ASSERT ((y << 16) == (tmp << 16));
91 ASSERT ((y << 23) == (tmp << 23));
92 y += 2;
93 y <<= 31;
94 ASSERT (c() == 12);
95 y >>= 31;
96 ASSERT (y == 3);
98 tmp = 23;
99 y = 42;
100 ASSERT (y + tmp == 42 + 23);
101 ASSERT (y - tmp == 42 - 23);
102 ASSERT (y * tmp == 42 * 23);
103 ASSERT (y / tmp == 42 / 23);
104 ASSERT (y % tmp == 42 % 23);
106 tmp = 42;
107 x = 42ll << 23;
108 ASSERT (x + y == (42ll << 23) + 42);
109 ASSERT (x - y == (42ll << 23) - 42);
110 ASSERT (x * y == (42ll << 23) * 42);
111 ASSERT (x / tmp == (42ll << 23) / 42);
112 ASSERT (x % tmp == (42ll << 23) % 42);
114 x = 0x1122334455667788ll;
115 y = 0x9988776655443322ull;
116 ASSERT (y + x == 0x9988776655443322ull + 0x1122334455667788ll);
117 ASSERT (y - x == 0x9988776655443322ull - 0x1122334455667788ll);
119 ASSERT (mulLL (1ull, 1ull) == 1ull * 1ull);
121 y = 0x55667788ull;
122 ASSERT (y * y == 0x55667788ull * 0x55667788ull); // this test is optimized by constant propagation
123 ASSERT (mulLL (y, y) == 0x55667788ull * 0x55667788ull); // this test is not
124 y = 0x55667788ull;
125 x = 0x55667788ll;
126 ASSERT (y * x == 0x55667788ull * 0x55667788ll); // this test is optimized by constant propagation
127 ASSERT (mulLL (y, x) == 0x55667788ull * 0x55667788ll); // this test is not
129 y = 0xa5667788ull;
130 ASSERT (y * y == 0xa5667788ull * 0xa5667788ull); // this test is optimized by constant propagation
131 ASSERT (mulLL (y, y) == 0xa5667788ull * 0xa5667788ull); // this test is not
132 y = 0xa5667788ull;
133 x = 0xa5667788ll;
134 ASSERT (y * x == 0xa5667788ull * 0xa5667788ll); // this test is optimized by constant propagation
135 ASSERT (mulLL (y, x) == 0xa5667788ull * 0xa5667788ull); // this test is not
137 y = 0xa5667788ccddull;
138 x = 0x0788ll;
139 ASSERT (y * x == 0xa5667788ccddull * 0x0788ll); // this test is optimized by constant propagation
140 ASSERT (mulLL (y, x) == 0xa5667788ccddull * 0x0788ll); // this test is not
142 y = 0x1122334455667700ull;
143 x = 0x2ll;
144 ASSERT (y * x == 0x1122334455667700ull * 0x2ll); // this test is optimized by constant propagation
145 ASSERT (mulLL (y, x) == 0x1122334455667700ull * 0x2ll); // this test is not
147 c(); // Unused long long return value requires special handling in register allocation.
150 #elif defined(TEST_div)
152 static unsigned long long divULL(unsigned long long a, unsigned long long b)
154 return a / b;
157 static unsigned long long modULL(unsigned long long a, unsigned long long b)
159 return a % b;
162 static long long divLL(long long a, long long b)
164 return a / b;
167 static long long modLL(long long a, long long b)
169 return a % b;
172 void
173 LongLong_div (void)
175 y = 0x1122334455667700ull;
176 x = 0x7ll;
177 ASSERT (y / x == 0x1122334455667700ull / 0x7ll); // this test is optimized by constant propagation
178 ASSERT (divULL (y, x) == 0x1122334455667700ull / 0x7ll); // this test is not
179 ASSERT (y % x == 0x1122334455667700ull % 0x7ll); // this test is optimized by constant propagation
180 ASSERT (modULL (y, x) == 0x1122334455667700ull % 0x7ll); // this test is not
181 x = 0x1122334455667700ll;
182 ASSERT (x / 0x7ll == 0x1122334455667700ll / 0x7ll); // this test is optimized by constant propagation
183 ASSERT (divLL (x, 0x7ll) == 0x1122334455667700ll / 0x7ll); // this test is not
184 ASSERT (x % 0x7ll == 0x1122334455667700ll % 0x7ll); // this test is optimized by constant propagation
185 ASSERT (modLL (x, 0x7ll) == 0x1122334455667700ll % 0x7ll); // this test is not
188 #elif defined(TEST_bit)
190 static int compareLL(unsigned long long a, unsigned long long b)
192 if (a > b)
193 return 1;
194 else if (a < b)
195 return -1;
196 else
197 return 0;
200 static long long leftShiftLL(long long a)
202 return a << 8;
205 static long long rightShiftLL(long long a)
207 return a >> 8;
210 static unsigned long long rightShiftULL(unsigned long long a)
212 return a >> 8;
215 static unsigned long long leftShiftULL(unsigned long long a)
217 return a << 8;
220 static unsigned long long bitAndULL(unsigned long long a, unsigned long long b)
222 return a & b;
225 static unsigned long long bitOrULL(unsigned long long a, unsigned long long b)
227 return a | b;
230 static unsigned long long bitXorULL(unsigned long long a, unsigned long long b)
232 return a ^ b;
235 static unsigned long long bitNotULL(unsigned long long a)
237 return ~a;
240 void
241 LongLong_bit (void)
243 y = 0x44556677aabbccddull;
244 x = 0x7766554433221100ull;
245 ASSERT (y < x);
246 ASSERT (x > y);
247 ASSERT (compareLL (y, x) == -1);
248 ASSERT (compareLL (x, y) == 1);
250 y = 0x5566778899aabbccull;
251 x = 0xaabbccdd11223344ll;
252 ASSERT ((y << 8) == 0x66778899aabbcc00ull);
253 ASSERT (leftShiftULL (y) == 0x66778899aabbcc00ull);
254 ASSERT ((y >> 8) == 0x005566778899aabbull);
255 ASSERT (rightShiftULL (y) == 0x005566778899aabbull);
256 ASSERT ((x << 8) == 0xbbccdd1122334400ll);
257 ASSERT (leftShiftLL (x) == 0xbbccdd1122334400ll);
258 ASSERT ((x >> 8) == 0xffaabbccdd112233ll);
259 ASSERT (rightShiftLL (x) == 0xffaabbccdd112233ll);
261 x = 0x44556677aabbccddll;
262 ASSERT (++x == 0x44556677aabbccdell);
263 y = 0x99556677aabbccddull;
264 ASSERT (--y == 0x99556677aabbccdcull);
266 y = 0x69aaaaaaaaaa55aaull;
267 x = 0x69555555555555aall;
268 ASSERT ((y & x) == (0x69aaaaaaaaaa55aaull & 0x69555555555555aall));
269 ASSERT (bitAndULL (y, x) == (0x69aaaaaaaaaa55aaull & 0x69555555555555aall));
270 ASSERT ((y | x) == (0x69aaaaaaaaaa55aaull | 0x69555555555555aall));
271 ASSERT (bitOrULL (y, x) == (0x69aaaaaaaaaa55aaull | 0x69555555555555aall));
272 ASSERT ((y ^ x) == (0x69aaaaaaaaaa55aaull ^ 0x69555555555555aall));
273 ASSERT (bitXorULL (y, x) == (0x69aaaaaaaaaa55aaull ^ 0x69555555555555aall));
274 ASSERT ((~y) == (~0x69aaaaaaaaaa55aaull));
275 ASSERT (bitNotULL (y) == (~0x69aaaaaaaaaa55aaull));
278 #elif defined(TEST_shift)
280 void
281 LongLong_shift (void)
283 unsigned char i,j,expected,match;
284 for (i=0;i<64;i++) {
285 y = 1ull << i;
286 match=0;
287 for (j=0;j<64;j++) {
288 expected = (j==i);
289 if ((unsigned char)(y & 1) == expected)
290 match++;
291 y >>= 1;
293 ASSERT (match==64);
296 for (i=0;i<64;i++) {
297 y = 0x8000000000000000ull >> i;
298 match=0;
299 for (j=0;j<64;j++) {
300 expected = (j==i);
301 if ((y & 0x8000000000000000ull) ? expected : !expected)
302 match++;
303 y <<= 1;
305 ASSERT (match==64);
308 for (i=0;i<64;i++) {
309 x = (signed long long)0x8000000000000000ll >> i;
310 match=0;
311 for (j=0;j<64;j++) {
312 expected = (j<=i);
313 if ((x & 0x8000000000000000ll) ? expected : !expected)
314 match++;
315 x <<= 1;
317 ASSERT (match==64);
322 #endif //TEST_mul/div/bit/shift
324 #endif //!mcs51-small
326 void
327 testLongLong (void)
329 #if !(defined(__SDCC_mcs51) && !defined(__SDCC_STACK_AUTO) && defined(__SDCC_MODEL_SMALL) ) && !defined(__SDCC_pic14) && !defined(__SDCC_pic16) && !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
330 LongLong_{test}();
331 #endif