2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / gcc.c-torture / execute / conversion.c
blob9e62acfd2ada3efebe54687d356f9fa835af73d6
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
6 comments.
8 Note that this code is NOT intended for testing of accuracy of fp
9 conversions. */
11 float
12 u2f(u)
13 unsigned int u;
15 return u;
18 double
19 u2d(u)
20 unsigned int u;
22 return u;
25 long double
26 u2ld(u)
27 unsigned int u;
29 return u;
32 float
33 s2f(s)
34 int s;
36 return s;
39 double
40 s2d(s)
41 int s;
43 return s;
46 long double
47 s2ld(s)
48 int s;
50 return s;
53 int
54 fnear (float x, float y)
56 float t = x - y;
57 return t == 0 || x / t > 1000000.0;
60 int
61 dnear (double x, double y)
63 double t = x - y;
64 return t == 0 || x / t > 100000000000000.0;
67 int
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 */
77 abort();
78 if (!fnear (u2f(~0U), (float) ~0U)) /* 0xffffffff */
79 abort();
80 if (!fnear (u2f((~0U) >> 1), (float) ((~0U) >> 1))) /* 0x7fffffff */
81 abort();
82 if (u2f(~((~0U) >> 1)) != (float) ~((~0U) >> 1)) /* 0x80000000 */
83 abort();
85 if (u2d(0U) != (double) 0U) /* 0 */
86 abort();
87 if (!dnear (u2d(~0U), (double) ~0U)) /* 0xffffffff */
88 abort();
89 if (!dnear (u2d((~0U) >> 1),(double) ((~0U) >> 1))) /* 0x7fffffff */
90 abort();
91 if (u2d(~((~0U) >> 1)) != (double) ~((~0U) >> 1)) /* 0x80000000 */
92 abort();
94 if (u2ld(0U) != (long double) 0U) /* 0 */
95 abort();
96 if (!ldnear (u2ld(~0U), (long double) ~0U)) /* 0xffffffff */
97 abort();
98 if (!ldnear (u2ld((~0U) >> 1),(long double) ((~0U) >> 1))) /* 0x7fffffff */
99 abort();
100 if (u2ld(~((~0U) >> 1)) != (long double) ~((~0U) >> 1)) /* 0x80000000 */
101 abort();
103 if (s2f(0) != (float) 0) /* 0 */
104 abort();
105 if (!fnear (s2f(~0), (float) ~0)) /* 0xffffffff */
106 abort();
107 if (!fnear (s2f((int)((~0U) >> 1)), (float)(int)((~0U) >> 1))) /* 0x7fffffff */
108 abort();
109 if (s2f((int)(~((~0U) >> 1))) != (float)(int)~((~0U) >> 1)) /* 0x80000000 */
110 abort();
112 if (s2d(0) != (double) 0) /* 0 */
113 abort();
114 if (!dnear (s2d(~0), (double) ~0)) /* 0xffffffff */
115 abort();
116 if (!dnear (s2d((int)((~0U) >> 1)), (double)(int)((~0U) >> 1))) /* 0x7fffffff */
117 abort();
118 if (s2d((int)~((~0U) >> 1)) != (double)(int)~((~0U) >> 1)) /* 0x80000000 */
119 abort();
121 if (s2ld(0) != (long double) 0) /* 0 */
122 abort();
123 if (!ldnear (s2ld(~0), (long double) ~0)) /* 0xffffffff */
124 abort();
125 if (!ldnear (s2ld((int)((~0U) >> 1)), (long double)(int)((~0U) >> 1))) /* 0x7fffffff */
126 abort();
127 if (s2ld((int)~((~0U) >> 1)) != (long double)(int)~((~0U) >> 1)) /* 0x80000000 */
128 abort();
131 #if __GNUC__
132 float
133 ull2f(u)
134 unsigned long long int u;
136 return u;
139 double
140 ull2d(u)
141 unsigned long long int u;
143 return u;
146 long double
147 ull2ld(u)
148 unsigned long long int u;
150 return u;
153 float
154 sll2f(s)
155 long long int s;
157 return s;
160 double
161 sll2d(s)
162 long long int s;
164 return s;
167 long double
168 sll2ld(s)
169 long long int s;
171 return s;
174 test_longlong_integer_to_float()
176 if (ull2f(0ULL) != (float) 0ULL) /* 0 */
177 abort();
178 if (ull2f(~0ULL) != (float) ~0ULL) /* 0xffffffff */
179 abort();
180 if (ull2f((~0ULL) >> 1) != (float) ((~0ULL) >> 1)) /* 0x7fffffff */
181 abort();
182 if (ull2f(~((~0ULL) >> 1)) != (float) ~((~0ULL) >> 1)) /* 0x80000000 */
183 abort();
185 if (ull2d(0ULL) != (double) 0ULL) /* 0 */
186 abort();
187 #if __HAVE_68881__
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 */
191 abort();
192 if (! dnear (ull2d((~0ULL) >> 1), (double) ((~0ULL) >> 1))) /* 0x7fffffff */
193 abort();
194 #else
195 if (ull2d(~0ULL) != (double) ~0ULL) /* 0xffffffff */
196 abort();
197 if (ull2d((~0ULL) >> 1) != (double) ((~0ULL) >> 1)) /* 0x7fffffff */
198 abort();
199 #endif
200 if (ull2d(~((~0ULL) >> 1)) != (double) ~((~0ULL) >> 1)) /* 0x80000000 */
201 abort();
203 if (ull2ld(0ULL) != (long double) 0ULL) /* 0 */
204 abort();
205 if (ull2ld(~0ULL) != (long double) ~0ULL) /* 0xffffffff */
206 abort();
207 if (ull2ld((~0ULL) >> 1) != (long double) ((~0ULL) >> 1)) /* 0x7fffffff */
208 abort();
209 if (ull2ld(~((~0ULL) >> 1)) != (long double) ~((~0ULL) >> 1)) /* 0x80000000 */
210 abort();
212 if (sll2f(0LL) != (float) 0LL) /* 0 */
213 abort();
214 if (sll2f(~0LL) != (float) ~0LL) /* 0xffffffff */
215 abort();
216 if (! fnear (sll2f((long long int)((~0ULL) >> 1)), (float)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
217 abort();
218 if (sll2f((long long int)(~((~0ULL) >> 1))) != (float)(long long int)~((~0ULL) >> 1)) /* 0x80000000 */
219 abort();
221 if (sll2d(0LL) != (double) 0LL) /* 0 */
222 abort();
223 if (sll2d(~0LL) != (double) ~0LL) /* 0xffffffff */
224 abort();
225 if (!dnear (sll2d((long long int)((~0ULL) >> 1)), (double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
226 abort();
227 if (! dnear (sll2d((long long int)~((~0ULL) >> 1)), (double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
228 abort();
230 if (sll2ld(0LL) != (long double) 0LL) /* 0 */
231 abort();
232 if (sll2ld(~0LL) != (long double) ~0LL) /* 0xffffffff */
233 abort();
234 if (!ldnear (sll2ld((long long int)((~0ULL) >> 1)), (long double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
235 abort();
236 if (! ldnear (sll2ld((long long int)~((~0ULL) >> 1)), (long double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
237 abort();
239 #endif
241 unsigned int
242 f2u(float f)
244 return (unsigned) f;
247 unsigned int
248 d2u(double d)
250 return (unsigned) d;
253 unsigned int
254 ld2u(long double d)
256 return (unsigned) d;
260 f2s(float f)
262 return (int) f;
266 d2s(double d)
268 return (int) d;
272 ld2s(long double d)
274 return (int) d;
277 test_float_to_integer()
279 if (f2u(0.0) != 0)
280 abort();
281 if (f2u(0.999) != 0)
282 abort();
283 if (f2u(1.0) != 1)
284 abort();
285 if (f2u(1.99) != 1)
286 abort();
287 if (f2u((float) ((~0U) >> 1)) != (~0U) >> 1 && /* 0x7fffffff */
288 f2u((float) ((~0U) >> 1)) != ((~0U) >> 1) + 1)
289 abort();
290 if (f2u((float) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
291 abort();
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) {
296 if (d2u(0.0) != 0)
297 abort();
298 if (d2u(0.999) != 0)
299 abort();
300 if (d2u(1.0) != 1)
301 abort();
302 if (d2u(1.99) != 1)
303 abort();
304 if (d2u((double) (~0U)) != ~0U) /* 0xffffffff */
305 abort();
306 if (d2u((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
307 abort();
308 if (d2u((double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
309 abort();
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) {
315 if (ld2u(0.0) != 0)
316 abort();
317 if (ld2u(0.999) != 0)
318 abort();
319 if (ld2u(1.0) != 1)
320 abort();
321 if (ld2u(1.99) != 1)
322 abort();
323 if (ld2u((long double) (~0U)) != ~0U) /* 0xffffffff */
324 abort();
325 if (ld2u((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
326 abort();
327 if (ld2u((long double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
328 abort();
331 if (f2s(0.0) != 0)
332 abort();
333 if (f2s(0.999) != 0)
334 abort();
335 if (f2s(1.0) != 1)
336 abort();
337 if (f2s(1.99) != 1)
338 abort();
339 if (f2s(-0.999) != 0)
340 abort();
341 if (f2s(-1.0) != -1)
342 abort();
343 if (f2s(-1.99) != -1)
344 abort();
345 if (f2s((float)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
346 abort();
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) {
351 if (d2s(0.0) != 0)
352 abort();
353 if (d2s(0.999) != 0)
354 abort();
355 if (d2s(1.0) != 1)
356 abort();
357 if (d2s(1.99) != 1)
358 abort();
359 if (d2s(-0.999) != 0)
360 abort();
361 if (d2s(-1.0) != -1)
362 abort();
363 if (d2s(-1.99) != -1)
364 abort();
365 if (d2s((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
366 abort();
367 if (d2s((double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
368 abort();
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) {
374 if (ld2s(0.0) != 0)
375 abort();
376 if (ld2s(0.999) != 0)
377 abort();
378 if (ld2s(1.0) != 1)
379 abort();
380 if (ld2s(1.99) != 1)
381 abort();
382 if (ld2s(-0.999) != 0)
383 abort();
384 if (ld2s(-1.0) != -1)
385 abort();
386 if (ld2s(-1.99) != -1)
387 abort();
388 if (ld2s((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
389 abort();
390 if (ld2s((long double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
391 abort();
395 #if __GNUC__
396 unsigned long long int
397 f2ull(float f)
399 return (unsigned long long int) f;
402 unsigned long long int
403 d2ull(double d)
405 return (unsigned long long int) d;
408 unsigned long long int
409 ld2ull(long double d)
411 return (unsigned long long int) d;
414 long long int
415 f2sll(float f)
417 return (long long int) f;
420 long long int
421 d2sll(double d)
423 return (long long int) d;
426 long long int
427 ld2sll(long double d)
429 return (long long int) d;
432 test_float_to_longlong_integer()
434 if (f2ull(0.0) != 0LL)
435 abort();
436 if (f2ull(0.999) != 0LL)
437 abort();
438 if (f2ull(1.0) != 1LL)
439 abort();
440 if (f2ull(1.99) != 1LL)
441 abort();
442 if (f2ull((float) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
443 f2ull((float) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
444 abort();
445 if (f2ull((float) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
446 abort();
448 if (d2ull(0.0) != 0LL)
449 abort();
450 if (d2ull(0.999) != 0LL)
451 abort();
452 if (d2ull(1.0) != 1LL)
453 abort();
454 if (d2ull(1.99) != 1LL)
455 abort();
456 if (d2ull((double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
457 d2ull((double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
458 abort();
459 if (d2ull((double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
460 abort();
462 if (ld2ull(0.0) != 0LL)
463 abort();
464 if (ld2ull(0.999) != 0LL)
465 abort();
466 if (ld2ull(1.0) != 1LL)
467 abort();
468 if (ld2ull(1.99) != 1LL)
469 abort();
470 if (ld2ull((long double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
471 ld2ull((long double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
472 abort();
473 if (ld2ull((long double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
474 abort();
477 if (f2sll(0.0) != 0LL)
478 abort();
479 if (f2sll(0.999) != 0LL)
480 abort();
481 if (f2sll(1.0) != 1LL)
482 abort();
483 if (f2sll(1.99) != 1LL)
484 abort();
485 if (f2sll(-0.999) != 0LL)
486 abort();
487 if (f2sll(-1.0) != -1LL)
488 abort();
489 if (f2sll(-1.99) != -1LL)
490 abort();
491 if (f2sll((float)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
492 abort();
494 if (d2sll(0.0) != 0LL)
495 abort();
496 if (d2sll(0.999) != 0LL)
497 abort();
498 if (d2sll(1.0) != 1LL)
499 abort();
500 if (d2sll(1.99) != 1LL)
501 abort();
502 if (d2sll(-0.999) != 0LL)
503 abort();
504 if (d2sll(-1.0) != -1LL)
505 abort();
506 if (d2sll(-1.99) != -1LL)
507 abort();
508 if (d2sll((double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
509 abort();
511 if (ld2sll(0.0) != 0LL)
512 abort();
513 if (ld2sll(0.999) != 0LL)
514 abort();
515 if (ld2sll(1.0) != 1LL)
516 abort();
517 if (ld2sll(1.99) != 1LL)
518 abort();
519 if (ld2sll(-0.999) != 0LL)
520 abort();
521 if (ld2sll(-1.0) != -1LL)
522 abort();
523 if (ld2sll(-1.99) != -1LL)
524 abort();
525 if (ld2sll((long double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
526 abort();
528 #endif
530 main()
532 test_integer_to_float();
533 test_float_to_integer();
534 #if __GNUC__
535 test_longlong_integer_to_float();
536 test_float_to_longlong_integer();
537 #endif
538 exit(0);