2 conversion.c from the execute part of the gcc torture tests.
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
14 Note that this code is NOT intended for testing of accuracy of fp
16 #if 0 // Enable when SDCC supports K&R style
60 fnear (float x
, float y
)
63 return t
== 0 || x
/ t
> 1000000.0;
67 dnear (double x
, double y
)
70 return t
== 0 || x
/ t
> 100000000000000.0;
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 */
84 if (!fnear (u2f(~0U), (float) ~0U)) /* 0xffffffff */
86 if (!fnear (u2f((~0U) >> 1), (float) ((~0U) >> 1))) /* 0x7fffffff */
88 if (u2f(~((~0U) >> 1)) != (float) ~((~0U) >> 1)) /* 0x80000000 */
91 if (u2d(0U) != (double) 0U) /* 0 */
93 if (!dnear (u2d(~0U), (double) ~0U)) /* 0xffffffff */
95 if (!dnear (u2d((~0U) >> 1),(double) ((~0U) >> 1))) /* 0x7fffffff */
97 if (u2d(~((~0U) >> 1)) != (double) ~((~0U) >> 1)) /* 0x80000000 */
100 if (u2ld(0U) != (long double) 0U) /* 0 */
102 if (!ldnear (u2ld(~0U), (long double) ~0U)) /* 0xffffffff */
104 if (!ldnear (u2ld((~0U) >> 1),(long double) ((~0U) >> 1))) /* 0x7fffffff */
106 if (u2ld(~((~0U) >> 1)) != (long double) ~((~0U) >> 1)) /* 0x80000000 */
109 if (s2f(0) != (float) 0) /* 0 */
111 if (!fnear (s2f(~0), (float) ~0)) /* 0xffffffff */
113 if (!fnear (s2f((int)((~0U) >> 1)), (float)(int)((~0U) >> 1))) /* 0x7fffffff */
115 if (s2f((int)(~((~0U) >> 1))) != (float)(int)~((~0U) >> 1)) /* 0x80000000 */
118 if (s2d(0) != (double) 0) /* 0 */
120 if (!dnear (s2d(~0), (double) ~0)) /* 0xffffffff */
122 if (!dnear (s2d((int)((~0U) >> 1)), (double)(int)((~0U) >> 1))) /* 0x7fffffff */
124 if (s2d((int)~((~0U) >> 1)) != (double)(int)~((~0U) >> 1)) /* 0x80000000 */
127 if (s2ld(0) != (long double) 0) /* 0 */
129 if (!ldnear (s2ld(~0), (long double) ~0)) /* 0xffffffff */
131 if (!ldnear (s2ld((int)((~0U) >> 1)), (long double)(int)((~0U) >> 1))) /* 0x7fffffff */
133 if (s2ld((int)~((~0U) >> 1)) != (long double)(int)~((~0U) >> 1)) /* 0x80000000 */
140 unsigned long long int u
;
147 unsigned long long int u
;
154 unsigned long long int u
;
180 test_longlong_integer_to_float()
182 if (ull2f(0ULL) != (float) 0ULL) /* 0 */
184 if (ull2f(~0ULL) != (float) ~0ULL) /* 0xffffffff */
186 if (ull2f((~0ULL) >> 1) != (float) ((~0ULL) >> 1)) /* 0x7fffffff */
188 if (ull2f(~((~0ULL) >> 1)) != (float) ~((~0ULL) >> 1)) /* 0x80000000 */
191 if (ull2d(0ULL) != (double) 0ULL) /* 0 */
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 */
198 if (! dnear (ull2d((~0ULL) >> 1), (double) ((~0ULL) >> 1))) /* 0x7fffffff */
201 if (ull2d(~0ULL) != (double) ~0ULL) /* 0xffffffff */
203 if (ull2d((~0ULL) >> 1) != (double) ((~0ULL) >> 1)) /* 0x7fffffff */
206 if (ull2d(~((~0ULL) >> 1)) != (double) ~((~0ULL) >> 1)) /* 0x80000000 */
209 if (ull2ld(0ULL) != (long double) 0ULL) /* 0 */
211 if (ull2ld(~0ULL) != (long double) ~0ULL) /* 0xffffffff */
213 if (ull2ld((~0ULL) >> 1) != (long double) ((~0ULL) >> 1)) /* 0x7fffffff */
215 if (ull2ld(~((~0ULL) >> 1)) != (long double) ~((~0ULL) >> 1)) /* 0x80000000 */
218 if (sll2f(0LL) != (float) 0LL) /* 0 */
220 if (sll2f(~0LL) != (float) ~0LL) /* 0xffffffff */
222 if (! fnear (sll2f((long long int)((~0ULL) >> 1)), (float)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
224 if (sll2f((long long int)(~((~0ULL) >> 1))) != (float)(long long int)~((~0ULL) >> 1)) /* 0x80000000 */
227 if (sll2d(0LL) != (double) 0LL) /* 0 */
229 if (sll2d(~0LL) != (double) ~0LL) /* 0xffffffff */
231 if (!dnear (sll2d((long long int)((~0ULL) >> 1)), (double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
233 if (! dnear (sll2d((long long int)~((~0ULL) >> 1)), (double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
236 if (sll2ld(0LL) != (long double) 0LL) /* 0 */
238 if (sll2ld(~0LL) != (long double) ~0LL) /* 0xffffffff */
240 if (!ldnear (sll2ld((long long int)((~0ULL) >> 1)), (long double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
242 if (! ldnear (sll2ld((long long int)~((~0ULL) >> 1)), (long double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
283 test_float_to_integer()
294 /* SPU float rounds towards zero. */
295 if (f2u((float) ((~0U) >> 1)) != 0x7fffff80)
298 if (f2u((float) ((~0U) >> 1)) != (~0U) >> 1 && /* 0x7fffffff */
299 f2u((float) ((~0U) >> 1)) != ((~0U) >> 1) + 1)
302 if (f2u((float) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
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) {
316 if (d2u((double) (~0U)) != ~0U) /* 0xffffffff */
318 if (d2u((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
320 if (d2u((double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
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) {
329 if (ld2u(0.999) != 0)
335 if (ld2u((long double) (~0U)) != ~0U) /* 0xffffffff */
337 if (ld2u((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
339 if (ld2u((long double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
351 if (f2s(-0.999) != 0)
355 if (f2s(-1.99) != -1)
357 if (f2s((float)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
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) {
371 if (d2s(-0.999) != 0)
375 if (d2s(-1.99) != -1)
377 if (d2s((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
379 if (d2s((double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
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) {
388 if (ld2s(0.999) != 0)
394 if (ld2s(-0.999) != 0)
396 if (ld2s(-1.0) != -1)
398 if (ld2s(-1.99) != -1)
400 if (ld2s((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
402 if (ld2s((long double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
408 unsigned long long int
411 return (unsigned long long int) f
;
414 unsigned long long int
417 return (unsigned long long int) d
;
420 unsigned long long int
421 ld2ull(long double d
)
423 return (unsigned long long int) d
;
429 return (long long int) f
;
435 return (long long int) d
;
439 ld2sll(long double d
)
441 return (long long int) d
;
444 test_float_to_longlong_integer()
446 if (f2ull(0.0) != 0LL)
448 if (f2ull(0.999) != 0LL)
450 if (f2ull(1.0) != 1LL)
452 if (f2ull(1.99) != 1LL)
455 /* SPU float rounds towards zero. */
456 if (f2ull((float) ((~0ULL) >> 1)) != 0x7fffff8000000000ULL
)
459 if (f2ull((float) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
460 f2ull((float) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
463 if (f2ull((float) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
466 if (d2ull(0.0) != 0LL)
468 if (d2ull(0.999) != 0LL)
470 if (d2ull(1.0) != 1LL)
472 if (d2ull(1.99) != 1LL)
474 if (d2ull((double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
475 d2ull((double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
477 if (d2ull((double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
480 if (ld2ull(0.0) != 0LL)
482 if (ld2ull(0.999) != 0LL)
484 if (ld2ull(1.0) != 1LL)
486 if (ld2ull(1.99) != 1LL)
488 if (ld2ull((long double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
489 ld2ull((long double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
491 if (ld2ull((long double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
495 if (f2sll(0.0) != 0LL)
497 if (f2sll(0.999) != 0LL)
499 if (f2sll(1.0) != 1LL)
501 if (f2sll(1.99) != 1LL)
503 if (f2sll(-0.999) != 0LL)
505 if (f2sll(-1.0) != -1LL)
507 if (f2sll(-1.99) != -1LL)
509 if (f2sll((float)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
512 if (d2sll(0.0) != 0LL)
514 if (d2sll(0.999) != 0LL)
516 if (d2sll(1.0) != 1LL)
518 if (d2sll(1.99) != 1LL)
520 if (d2sll(-0.999) != 0LL)
522 if (d2sll(-1.0) != -1LL)
524 if (d2sll(-1.99) != -1LL)
526 if (d2sll((double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
529 if (ld2sll(0.0) != 0LL)
531 if (ld2sll(0.999) != 0LL)
533 if (ld2sll(1.0) != 1LL)
535 if (ld2sll(1.99) != 1LL)
537 if (ld2sll(-0.999) != 0LL)
539 if (ld2sll(-1.0) != -1LL)
541 if (ld2sll(-1.99) != -1LL)
543 if (ld2sll((long double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
549 testTortureExecute (void)
552 test_integer_to_float();
553 test_float_to_integer();
555 test_longlong_integer_to_float();
556 test_float_to_longlong_integer();