1 /* Test front-end conversions, optimizer conversions, and run-time
2 conversions between different arithmetic types.
4 Constants are specified in a non-obvious way to make them work for
5 any word size. Their value on a 32-bit machine is indicated in the
8 Note that this code is NOT intended for testing of accuracy of fp
54 fnear (float x
, float y
)
57 return t
== 0 || x
/ t
> 1000000.0;
61 dnear (double x
, double y
)
64 return t
== 0 || x
/ t
> 100000000000000.0;
68 ldnear (long double x
, long double y
)
70 long double t
= x
- y
;
71 return t
== 0 || x
/ t
> 100000000000000000000000000000000.0;
74 test_integer_to_float()
76 if (u2f(0U) != (float) 0U) /* 0 */
78 if (!fnear (u2f(~0U), (float) ~0U)) /* 0xffffffff */
80 if (!fnear (u2f((~0U) >> 1), (float) ((~0U) >> 1))) /* 0x7fffffff */
82 if (u2f(~((~0U) >> 1)) != (float) ~((~0U) >> 1)) /* 0x80000000 */
85 if (u2d(0U) != (double) 0U) /* 0 */
87 if (!dnear (u2d(~0U), (double) ~0U)) /* 0xffffffff */
89 if (!dnear (u2d((~0U) >> 1),(double) ((~0U) >> 1))) /* 0x7fffffff */
91 if (u2d(~((~0U) >> 1)) != (double) ~((~0U) >> 1)) /* 0x80000000 */
94 if (u2ld(0U) != (long double) 0U) /* 0 */
96 if (!ldnear (u2ld(~0U), (long double) ~0U)) /* 0xffffffff */
98 if (!ldnear (u2ld((~0U) >> 1),(long double) ((~0U) >> 1))) /* 0x7fffffff */
100 if (u2ld(~((~0U) >> 1)) != (long double) ~((~0U) >> 1)) /* 0x80000000 */
103 if (s2f(0) != (float) 0) /* 0 */
105 if (!fnear (s2f(~0), (float) ~0)) /* 0xffffffff */
107 if (!fnear (s2f((int)((~0U) >> 1)), (float)(int)((~0U) >> 1))) /* 0x7fffffff */
109 if (s2f((int)(~((~0U) >> 1))) != (float)(int)~((~0U) >> 1)) /* 0x80000000 */
112 if (s2d(0) != (double) 0) /* 0 */
114 if (!dnear (s2d(~0), (double) ~0)) /* 0xffffffff */
116 if (!dnear (s2d((int)((~0U) >> 1)), (double)(int)((~0U) >> 1))) /* 0x7fffffff */
118 if (s2d((int)~((~0U) >> 1)) != (double)(int)~((~0U) >> 1)) /* 0x80000000 */
121 if (s2ld(0) != (long double) 0) /* 0 */
123 if (!ldnear (s2ld(~0), (long double) ~0)) /* 0xffffffff */
125 if (!ldnear (s2ld((int)((~0U) >> 1)), (long double)(int)((~0U) >> 1))) /* 0x7fffffff */
127 if (s2ld((int)~((~0U) >> 1)) != (long double)(int)~((~0U) >> 1)) /* 0x80000000 */
134 unsigned long long int u
;
141 unsigned long long int u
;
148 unsigned long long int u
;
174 test_longlong_integer_to_float()
176 if (ull2f(0ULL) != (float) 0ULL) /* 0 */
178 if (ull2f(~0ULL) != (float) ~0ULL) /* 0xffffffff */
180 if (ull2f((~0ULL) >> 1) != (float) ((~0ULL) >> 1)) /* 0x7fffffff */
182 if (ull2f(~((~0ULL) >> 1)) != (float) ~((~0ULL) >> 1)) /* 0x80000000 */
185 if (ull2d(0ULL) != (double) 0ULL) /* 0 */
188 /* Some 68881 targets return values in fp0, with excess precision.
189 But the compile-time conversion to double works correctly. */
190 if (! dnear (ull2d(~0ULL), (double) ~0ULL)) /* 0xffffffff */
192 if (! dnear (ull2d((~0ULL) >> 1), (double) ((~0ULL) >> 1))) /* 0x7fffffff */
195 if (ull2d(~0ULL) != (double) ~0ULL) /* 0xffffffff */
197 if (ull2d((~0ULL) >> 1) != (double) ((~0ULL) >> 1)) /* 0x7fffffff */
200 if (ull2d(~((~0ULL) >> 1)) != (double) ~((~0ULL) >> 1)) /* 0x80000000 */
203 if (ull2ld(0ULL) != (long double) 0ULL) /* 0 */
205 if (ull2ld(~0ULL) != (long double) ~0ULL) /* 0xffffffff */
207 if (ull2ld((~0ULL) >> 1) != (long double) ((~0ULL) >> 1)) /* 0x7fffffff */
209 if (ull2ld(~((~0ULL) >> 1)) != (long double) ~((~0ULL) >> 1)) /* 0x80000000 */
212 if (sll2f(0LL) != (float) 0LL) /* 0 */
214 if (sll2f(~0LL) != (float) ~0LL) /* 0xffffffff */
216 if (! fnear (sll2f((long long int)((~0ULL) >> 1)), (float)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
218 if (sll2f((long long int)(~((~0ULL) >> 1))) != (float)(long long int)~((~0ULL) >> 1)) /* 0x80000000 */
221 if (sll2d(0LL) != (double) 0LL) /* 0 */
223 if (sll2d(~0LL) != (double) ~0LL) /* 0xffffffff */
225 if (!dnear (sll2d((long long int)((~0ULL) >> 1)), (double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
227 if (! dnear (sll2d((long long int)~((~0ULL) >> 1)), (double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
230 if (sll2ld(0LL) != (long double) 0LL) /* 0 */
232 if (sll2ld(~0LL) != (long double) ~0LL) /* 0xffffffff */
234 if (!ldnear (sll2ld((long long int)((~0ULL) >> 1)), (long double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
236 if (! ldnear (sll2ld((long long int)~((~0ULL) >> 1)), (long double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
277 test_float_to_integer()
287 if (f2u((float) ((~0U) >> 1)) != (~0U) >> 1 && /* 0x7fffffff */
288 f2u((float) ((~0U) >> 1)) != ((~0U) >> 1) + 1)
290 if (f2u((float) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
293 /* These tests require double precision, so for hosts that don't offer
294 that much precision, just ignore these test. */
295 if (sizeof (double) >= 8) {
304 if (d2u((double) (~0U)) != ~0U) /* 0xffffffff */
306 if (d2u((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
308 if (d2u((double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
312 /* These tests require long double precision, so for hosts that don't offer
313 that much precision, just ignore these test. */
314 if (sizeof (long double) >= 8) {
317 if (ld2u(0.999) != 0)
323 if (ld2u((long double) (~0U)) != ~0U) /* 0xffffffff */
325 if (ld2u((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
327 if (ld2u((long double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
339 if (f2s(-0.999) != 0)
343 if (f2s(-1.99) != -1)
345 if (f2s((float)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
348 /* These tests require double precision, so for hosts that don't offer
349 that much precision, just ignore these test. */
350 if (sizeof (double) >= 8) {
359 if (d2s(-0.999) != 0)
363 if (d2s(-1.99) != -1)
365 if (d2s((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
367 if (d2s((double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
371 /* These tests require long double precision, so for hosts that don't offer
372 that much precision, just ignore these test. */
373 if (sizeof (long double) >= 8) {
376 if (ld2s(0.999) != 0)
382 if (ld2s(-0.999) != 0)
384 if (ld2s(-1.0) != -1)
386 if (ld2s(-1.99) != -1)
388 if (ld2s((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
390 if (ld2s((long double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
396 unsigned long long int
399 return (unsigned long long int) f
;
402 unsigned long long int
405 return (unsigned long long int) d
;
408 unsigned long long int
409 ld2ull(long double d
)
411 return (unsigned long long int) d
;
417 return (long long int) f
;
423 return (long long int) d
;
427 ld2sll(long double d
)
429 return (long long int) d
;
432 test_float_to_longlong_integer()
434 if (f2ull(0.0) != 0LL)
436 if (f2ull(0.999) != 0LL)
438 if (f2ull(1.0) != 1LL)
440 if (f2ull(1.99) != 1LL)
442 if (f2ull((float) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
443 f2ull((float) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
445 if (f2ull((float) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
448 if (d2ull(0.0) != 0LL)
450 if (d2ull(0.999) != 0LL)
452 if (d2ull(1.0) != 1LL)
454 if (d2ull(1.99) != 1LL)
456 if (d2ull((double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
457 d2ull((double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
459 if (d2ull((double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
462 if (ld2ull(0.0) != 0LL)
464 if (ld2ull(0.999) != 0LL)
466 if (ld2ull(1.0) != 1LL)
468 if (ld2ull(1.99) != 1LL)
470 if (ld2ull((long double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
471 ld2ull((long double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
473 if (ld2ull((long double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
477 if (f2sll(0.0) != 0LL)
479 if (f2sll(0.999) != 0LL)
481 if (f2sll(1.0) != 1LL)
483 if (f2sll(1.99) != 1LL)
485 if (f2sll(-0.999) != 0LL)
487 if (f2sll(-1.0) != -1LL)
489 if (f2sll(-1.99) != -1LL)
491 if (f2sll((float)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
494 if (d2sll(0.0) != 0LL)
496 if (d2sll(0.999) != 0LL)
498 if (d2sll(1.0) != 1LL)
500 if (d2sll(1.99) != 1LL)
502 if (d2sll(-0.999) != 0LL)
504 if (d2sll(-1.0) != -1LL)
506 if (d2sll(-1.99) != -1LL)
508 if (d2sll((double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
511 if (ld2sll(0.0) != 0LL)
513 if (ld2sll(0.999) != 0LL)
515 if (ld2sll(1.0) != 1LL)
517 if (ld2sll(1.99) != 1LL)
519 if (ld2sll(-0.999) != 0LL)
521 if (ld2sll(-1.0) != -1LL)
523 if (ld2sll(-1.99) != -1LL)
525 if (ld2sll((long double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
532 test_integer_to_float();
533 test_float_to_integer();
535 test_longlong_integer_to_float();
536 test_float_to_longlong_integer();