struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / gcc-torture-execute-conversion.c
blobb18a072efc7bc1aa06b25b69c2ff4d714574ef9d
1 /*
2 conversion.c from the execute part of the gcc torture tests.
3 */
5 #include <testfwk.h>
7 /* Test front-end conversions, optimizer conversions, and run-time
8 conversions between different arithmetic types.
10 Constants are specified in a non-obvious way to make them work for
11 any word size. Their value on a 32-bit machine is indicated in the
12 comments.
14 Note that this code is NOT intended for testing of accuracy of fp
15 conversions. */
16 #if 0 // Enable when SDCC supports K&R style
17 float
18 u2f(u)
19 unsigned int u;
21 return u;
24 double
25 u2d(u)
26 unsigned int u;
28 return u;
31 long double
32 u2ld(u)
33 unsigned int u;
35 return u;
38 float
39 s2f(s)
40 int s;
42 return s;
45 double
46 s2d(s)
47 int s;
49 return s;
52 long double
53 s2ld(s)
54 int s;
56 return s;
59 int
60 fnear (float x, float y)
62 float t = x - y;
63 return t == 0 || x / t > 1000000.0;
66 int
67 dnear (double x, double y)
69 double t = x - y;
70 return t == 0 || x / t > 100000000000000.0;
73 int
74 ldnear (long double x, long double y)
76 long double t = x - y;
77 return t == 0 || x / t > 100000000000000000000000000000000.0;
80 test_integer_to_float()
82 if (u2f(0U) != (float) 0U) /* 0 */
83 abort();
84 if (!fnear (u2f(~0U), (float) ~0U)) /* 0xffffffff */
85 abort();
86 if (!fnear (u2f((~0U) >> 1), (float) ((~0U) >> 1))) /* 0x7fffffff */
87 abort();
88 if (u2f(~((~0U) >> 1)) != (float) ~((~0U) >> 1)) /* 0x80000000 */
89 abort();
91 if (u2d(0U) != (double) 0U) /* 0 */
92 abort();
93 if (!dnear (u2d(~0U), (double) ~0U)) /* 0xffffffff */
94 abort();
95 if (!dnear (u2d((~0U) >> 1),(double) ((~0U) >> 1))) /* 0x7fffffff */
96 abort();
97 if (u2d(~((~0U) >> 1)) != (double) ~((~0U) >> 1)) /* 0x80000000 */
98 abort();
100 if (u2ld(0U) != (long double) 0U) /* 0 */
101 abort();
102 if (!ldnear (u2ld(~0U), (long double) ~0U)) /* 0xffffffff */
103 abort();
104 if (!ldnear (u2ld((~0U) >> 1),(long double) ((~0U) >> 1))) /* 0x7fffffff */
105 abort();
106 if (u2ld(~((~0U) >> 1)) != (long double) ~((~0U) >> 1)) /* 0x80000000 */
107 abort();
109 if (s2f(0) != (float) 0) /* 0 */
110 abort();
111 if (!fnear (s2f(~0), (float) ~0)) /* 0xffffffff */
112 abort();
113 if (!fnear (s2f((int)((~0U) >> 1)), (float)(int)((~0U) >> 1))) /* 0x7fffffff */
114 abort();
115 if (s2f((int)(~((~0U) >> 1))) != (float)(int)~((~0U) >> 1)) /* 0x80000000 */
116 abort();
118 if (s2d(0) != (double) 0) /* 0 */
119 abort();
120 if (!dnear (s2d(~0), (double) ~0)) /* 0xffffffff */
121 abort();
122 if (!dnear (s2d((int)((~0U) >> 1)), (double)(int)((~0U) >> 1))) /* 0x7fffffff */
123 abort();
124 if (s2d((int)~((~0U) >> 1)) != (double)(int)~((~0U) >> 1)) /* 0x80000000 */
125 abort();
127 if (s2ld(0) != (long double) 0) /* 0 */
128 abort();
129 if (!ldnear (s2ld(~0), (long double) ~0)) /* 0xffffffff */
130 abort();
131 if (!ldnear (s2ld((int)((~0U) >> 1)), (long double)(int)((~0U) >> 1))) /* 0x7fffffff */
132 abort();
133 if (s2ld((int)~((~0U) >> 1)) != (long double)(int)~((~0U) >> 1)) /* 0x80000000 */
134 abort();
137 #if __GNUC__
138 float
139 ull2f(u)
140 unsigned long long int u;
142 return u;
145 double
146 ull2d(u)
147 unsigned long long int u;
149 return u;
152 long double
153 ull2ld(u)
154 unsigned long long int u;
156 return u;
159 float
160 sll2f(s)
161 long long int s;
163 return s;
166 double
167 sll2d(s)
168 long long int s;
170 return s;
173 long double
174 sll2ld(s)
175 long long int s;
177 return s;
180 test_longlong_integer_to_float()
182 if (ull2f(0ULL) != (float) 0ULL) /* 0 */
183 abort();
184 if (ull2f(~0ULL) != (float) ~0ULL) /* 0xffffffff */
185 abort();
186 if (ull2f((~0ULL) >> 1) != (float) ((~0ULL) >> 1)) /* 0x7fffffff */
187 abort();
188 if (ull2f(~((~0ULL) >> 1)) != (float) ~((~0ULL) >> 1)) /* 0x80000000 */
189 abort();
191 if (ull2d(0ULL) != (double) 0ULL) /* 0 */
192 abort();
193 #if __HAVE_68881__
194 /* Some 68881 targets return values in fp0, with excess precision.
195 But the compile-time conversion to double works correctly. */
196 if (! dnear (ull2d(~0ULL), (double) ~0ULL)) /* 0xffffffff */
197 abort();
198 if (! dnear (ull2d((~0ULL) >> 1), (double) ((~0ULL) >> 1))) /* 0x7fffffff */
199 abort();
200 #else
201 if (ull2d(~0ULL) != (double) ~0ULL) /* 0xffffffff */
202 abort();
203 if (ull2d((~0ULL) >> 1) != (double) ((~0ULL) >> 1)) /* 0x7fffffff */
204 abort();
205 #endif
206 if (ull2d(~((~0ULL) >> 1)) != (double) ~((~0ULL) >> 1)) /* 0x80000000 */
207 abort();
209 if (ull2ld(0ULL) != (long double) 0ULL) /* 0 */
210 abort();
211 if (ull2ld(~0ULL) != (long double) ~0ULL) /* 0xffffffff */
212 abort();
213 if (ull2ld((~0ULL) >> 1) != (long double) ((~0ULL) >> 1)) /* 0x7fffffff */
214 abort();
215 if (ull2ld(~((~0ULL) >> 1)) != (long double) ~((~0ULL) >> 1)) /* 0x80000000 */
216 abort();
218 if (sll2f(0LL) != (float) 0LL) /* 0 */
219 abort();
220 if (sll2f(~0LL) != (float) ~0LL) /* 0xffffffff */
221 abort();
222 if (! fnear (sll2f((long long int)((~0ULL) >> 1)), (float)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
223 abort();
224 if (sll2f((long long int)(~((~0ULL) >> 1))) != (float)(long long int)~((~0ULL) >> 1)) /* 0x80000000 */
225 abort();
227 if (sll2d(0LL) != (double) 0LL) /* 0 */
228 abort();
229 if (sll2d(~0LL) != (double) ~0LL) /* 0xffffffff */
230 abort();
231 if (!dnear (sll2d((long long int)((~0ULL) >> 1)), (double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
232 abort();
233 if (! dnear (sll2d((long long int)~((~0ULL) >> 1)), (double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
234 abort();
236 if (sll2ld(0LL) != (long double) 0LL) /* 0 */
237 abort();
238 if (sll2ld(~0LL) != (long double) ~0LL) /* 0xffffffff */
239 abort();
240 if (!ldnear (sll2ld((long long int)((~0ULL) >> 1)), (long double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
241 abort();
242 if (! ldnear (sll2ld((long long int)~((~0ULL) >> 1)), (long double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
243 abort();
245 #endif
247 unsigned int
248 f2u(float f)
250 return (unsigned) f;
253 unsigned int
254 d2u(double d)
256 return (unsigned) d;
259 unsigned int
260 ld2u(long double d)
262 return (unsigned) d;
266 f2s(float f)
268 return (int) f;
272 d2s(double d)
274 return (int) d;
278 ld2s(long double d)
280 return (int) d;
283 test_float_to_integer()
285 if (f2u(0.0) != 0)
286 abort();
287 if (f2u(0.999) != 0)
288 abort();
289 if (f2u(1.0) != 1)
290 abort();
291 if (f2u(1.99) != 1)
292 abort();
293 #ifdef __SPU__
294 /* SPU float rounds towards zero. */
295 if (f2u((float) ((~0U) >> 1)) != 0x7fffff80)
296 abort();
297 #else
298 if (f2u((float) ((~0U) >> 1)) != (~0U) >> 1 && /* 0x7fffffff */
299 f2u((float) ((~0U) >> 1)) != ((~0U) >> 1) + 1)
300 abort();
301 #endif
302 if (f2u((float) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
303 abort();
305 /* These tests require double precision, so for hosts that don't offer
306 that much precision, just ignore these test. */
307 if (sizeof (double) >= 8) {
308 if (d2u(0.0) != 0)
309 abort();
310 if (d2u(0.999) != 0)
311 abort();
312 if (d2u(1.0) != 1)
313 abort();
314 if (d2u(1.99) != 1)
315 abort();
316 if (d2u((double) (~0U)) != ~0U) /* 0xffffffff */
317 abort();
318 if (d2u((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
319 abort();
320 if (d2u((double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
321 abort();
324 /* These tests require long double precision, so for hosts that don't offer
325 that much precision, just ignore these test. */
326 if (sizeof (long double) >= 8) {
327 if (ld2u(0.0) != 0)
328 abort();
329 if (ld2u(0.999) != 0)
330 abort();
331 if (ld2u(1.0) != 1)
332 abort();
333 if (ld2u(1.99) != 1)
334 abort();
335 if (ld2u((long double) (~0U)) != ~0U) /* 0xffffffff */
336 abort();
337 if (ld2u((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
338 abort();
339 if (ld2u((long double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
340 abort();
343 if (f2s(0.0) != 0)
344 abort();
345 if (f2s(0.999) != 0)
346 abort();
347 if (f2s(1.0) != 1)
348 abort();
349 if (f2s(1.99) != 1)
350 abort();
351 if (f2s(-0.999) != 0)
352 abort();
353 if (f2s(-1.0) != -1)
354 abort();
355 if (f2s(-1.99) != -1)
356 abort();
357 if (f2s((float)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
358 abort();
360 /* These tests require double precision, so for hosts that don't offer
361 that much precision, just ignore these test. */
362 if (sizeof (double) >= 8) {
363 if (d2s(0.0) != 0)
364 abort();
365 if (d2s(0.999) != 0)
366 abort();
367 if (d2s(1.0) != 1)
368 abort();
369 if (d2s(1.99) != 1)
370 abort();
371 if (d2s(-0.999) != 0)
372 abort();
373 if (d2s(-1.0) != -1)
374 abort();
375 if (d2s(-1.99) != -1)
376 abort();
377 if (d2s((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
378 abort();
379 if (d2s((double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
380 abort();
383 /* These tests require long double precision, so for hosts that don't offer
384 that much precision, just ignore these test. */
385 if (sizeof (long double) >= 8) {
386 if (ld2s(0.0) != 0)
387 abort();
388 if (ld2s(0.999) != 0)
389 abort();
390 if (ld2s(1.0) != 1)
391 abort();
392 if (ld2s(1.99) != 1)
393 abort();
394 if (ld2s(-0.999) != 0)
395 abort();
396 if (ld2s(-1.0) != -1)
397 abort();
398 if (ld2s(-1.99) != -1)
399 abort();
400 if (ld2s((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
401 abort();
402 if (ld2s((long double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
403 abort();
407 #if __GNUC__
408 unsigned long long int
409 f2ull(float f)
411 return (unsigned long long int) f;
414 unsigned long long int
415 d2ull(double d)
417 return (unsigned long long int) d;
420 unsigned long long int
421 ld2ull(long double d)
423 return (unsigned long long int) d;
426 long long int
427 f2sll(float f)
429 return (long long int) f;
432 long long int
433 d2sll(double d)
435 return (long long int) d;
438 long long int
439 ld2sll(long double d)
441 return (long long int) d;
444 test_float_to_longlong_integer()
446 if (f2ull(0.0) != 0LL)
447 abort();
448 if (f2ull(0.999) != 0LL)
449 abort();
450 if (f2ull(1.0) != 1LL)
451 abort();
452 if (f2ull(1.99) != 1LL)
453 abort();
454 #ifdef __SPU__
455 /* SPU float rounds towards zero. */
456 if (f2ull((float) ((~0ULL) >> 1)) != 0x7fffff8000000000ULL)
457 abort();
458 #else
459 if (f2ull((float) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
460 f2ull((float) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
461 abort();
462 #endif
463 if (f2ull((float) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
464 abort();
466 if (d2ull(0.0) != 0LL)
467 abort();
468 if (d2ull(0.999) != 0LL)
469 abort();
470 if (d2ull(1.0) != 1LL)
471 abort();
472 if (d2ull(1.99) != 1LL)
473 abort();
474 if (d2ull((double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
475 d2ull((double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
476 abort();
477 if (d2ull((double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
478 abort();
480 if (ld2ull(0.0) != 0LL)
481 abort();
482 if (ld2ull(0.999) != 0LL)
483 abort();
484 if (ld2ull(1.0) != 1LL)
485 abort();
486 if (ld2ull(1.99) != 1LL)
487 abort();
488 if (ld2ull((long double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
489 ld2ull((long double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
490 abort();
491 if (ld2ull((long double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
492 abort();
495 if (f2sll(0.0) != 0LL)
496 abort();
497 if (f2sll(0.999) != 0LL)
498 abort();
499 if (f2sll(1.0) != 1LL)
500 abort();
501 if (f2sll(1.99) != 1LL)
502 abort();
503 if (f2sll(-0.999) != 0LL)
504 abort();
505 if (f2sll(-1.0) != -1LL)
506 abort();
507 if (f2sll(-1.99) != -1LL)
508 abort();
509 if (f2sll((float)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
510 abort();
512 if (d2sll(0.0) != 0LL)
513 abort();
514 if (d2sll(0.999) != 0LL)
515 abort();
516 if (d2sll(1.0) != 1LL)
517 abort();
518 if (d2sll(1.99) != 1LL)
519 abort();
520 if (d2sll(-0.999) != 0LL)
521 abort();
522 if (d2sll(-1.0) != -1LL)
523 abort();
524 if (d2sll(-1.99) != -1LL)
525 abort();
526 if (d2sll((double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
527 abort();
529 if (ld2sll(0.0) != 0LL)
530 abort();
531 if (ld2sll(0.999) != 0LL)
532 abort();
533 if (ld2sll(1.0) != 1LL)
534 abort();
535 if (ld2sll(1.99) != 1LL)
536 abort();
537 if (ld2sll(-0.999) != 0LL)
538 abort();
539 if (ld2sll(-1.0) != -1LL)
540 abort();
541 if (ld2sll(-1.99) != -1LL)
542 abort();
543 if (ld2sll((long double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
544 abort();
546 #endif
547 #endif
548 void
549 testTortureExecute (void)
551 #if 0
552 test_integer_to_float();
553 test_float_to_integer();
554 #if __GNUC__
555 test_longlong_integer_to_float();
556 test_float_to_longlong_integer();
557 #endif
558 exit(0);
559 #endif