Debian Codegraph @

gcc/testsuite/gcc.c-torture/execute/conversion.c

raw
1 /* Test front-end conversions, optimizer conversions, and run-time
2 conversions between different arithmetic types.
3
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.
7
8 Note that this code is NOT intended for testing of accuracy of fp
9 conversions. */
10
11 void abort (void);
12 void exit (int);
13
14 float
15 u2f(u)
16 unsigned int u;
17 {
18 return u;
19 }
20
21 double
22 u2d(u)
23 unsigned int u;
24 {
25 return u;
26 }
27
28 long double
29 u2ld(u)
30 unsigned int u;
31 {
32 return u;
33 }
34
35 float
36 s2f(s)
37 int s;
38 {
39 return s;
40 }
41
42 double
43 s2d(s)
44 int s;
45 {
46 return s;
47 }
48
49 long double
50 s2ld(s)
51 int s;
52 {
53 return s;
54 }
55
56 int
57 fnear (float x, float y)
58 {
59 float t = x - y;
60 return t == 0 || x / t > 1000000.0;
61 }
62
63 int
64 dnear (double x, double y)
65 {
66 double t = x - y;
67 return t == 0 || x / t > 100000000000000.0;
68 }
69
70 int
71 ldnear (long double x, long double y)
72 {
73 long double t = x - y;
74 return t == 0 || x / t > 100000000000000000000000000000000.0;
75 }
76
77 void
78 test_integer_to_float()
79 {
80 if (u2f(0U) != (float) 0U) /* 0 */
81 abort();
82 if (!fnear (u2f(~0U), (float) ~0U)) /* 0xffffffff */
83 abort();
84 if (!fnear (u2f((~0U) >> 1), (float) ((~0U) >> 1))) /* 0x7fffffff */
85 abort();
86 if (u2f(~((~0U) >> 1)) != (float) ~((~0U) >> 1)) /* 0x80000000 */
87 abort();
88
89 if (u2d(0U) != (double) 0U) /* 0 */
90 abort();
91 if (!dnear (u2d(~0U), (double) ~0U)) /* 0xffffffff */
92 abort();
93 if (!dnear (u2d((~0U) >> 1),(double) ((~0U) >> 1))) /* 0x7fffffff */
94 abort();
95 if (u2d(~((~0U) >> 1)) != (double) ~((~0U) >> 1)) /* 0x80000000 */
96 abort();
97
98 if (u2ld(0U) != (long double) 0U) /* 0 */
99 abort();
100 if (!ldnear (u2ld(~0U), (long double) ~0U)) /* 0xffffffff */
101 abort();
102 if (!ldnear (u2ld((~0U) >> 1),(long double) ((~0U) >> 1))) /* 0x7fffffff */
103 abort();
104 if (u2ld(~((~0U) >> 1)) != (long double) ~((~0U) >> 1)) /* 0x80000000 */
105 abort();
106
107 if (s2f(0) != (float) 0) /* 0 */
108 abort();
109 if (!fnear (s2f(~0), (float) ~0)) /* 0xffffffff */
110 abort();
111 if (!fnear (s2f((int)((~0U) >> 1)), (float)(int)((~0U) >> 1))) /* 0x7fffffff */
112 abort();
113 if (s2f((int)(~((~0U) >> 1))) != (float)(int)~((~0U) >> 1)) /* 0x80000000 */
114 abort();
115
116 if (s2d(0) != (double) 0) /* 0 */
117 abort();
118 if (!dnear (s2d(~0), (double) ~0)) /* 0xffffffff */
119 abort();
120 if (!dnear (s2d((int)((~0U) >> 1)), (double)(int)((~0U) >> 1))) /* 0x7fffffff */
121 abort();
122 if (s2d((int)~((~0U) >> 1)) != (double)(int)~((~0U) >> 1)) /* 0x80000000 */
123 abort();
124
125 if (s2ld(0) != (long double) 0) /* 0 */
126 abort();
127 if (!ldnear (s2ld(~0), (long double) ~0)) /* 0xffffffff */
128 abort();
129 if (!ldnear (s2ld((int)((~0U) >> 1)), (long double)(int)((~0U) >> 1))) /* 0x7fffffff */
130 abort();
131 if (s2ld((int)~((~0U) >> 1)) != (long double)(int)~((~0U) >> 1)) /* 0x80000000 */
132 abort();
133 }
134
135 #if __GNUC__
136 float
137 ull2f(u)
138 unsigned long long int u;
139 {
140 return u;
141 }
142
143 double
144 ull2d(u)
145 unsigned long long int u;
146 {
147 return u;
148 }
149
150 long double
151 ull2ld(u)
152 unsigned long long int u;
153 {
154 return u;
155 }
156
157 float
158 sll2f(s)
159 long long int s;
160 {
161 return s;
162 }
163
164 double
165 sll2d(s)
166 long long int s;
167 {
168 return s;
169 }
170
171 long double
172 sll2ld(s)
173 long long int s;
174 {
175 return s;
176 }
177
178 void
179 test_longlong_integer_to_float()
180 {
181 if (ull2f(0ULL) != (float) 0ULL) /* 0 */
182 abort();
183 if (ull2f(~0ULL) != (float) ~0ULL) /* 0xffffffff */
184 abort();
185 if (ull2f((~0ULL) >> 1) != (float) ((~0ULL) >> 1)) /* 0x7fffffff */
186 abort();
187 if (ull2f(~((~0ULL) >> 1)) != (float) ~((~0ULL) >> 1)) /* 0x80000000 */
188 abort();
189
190 if (ull2d(0ULL) != (double) 0ULL) /* 0 */
191 abort();
192 #if __HAVE_68881__
193 /* Some 68881 targets return values in fp0, with excess precision.
194 But the compile-time conversion to double works correctly. */
195 if (! dnear (ull2d(~0ULL), (double) ~0ULL)) /* 0xffffffff */
196 abort();
197 if (! dnear (ull2d((~0ULL) >> 1), (double) ((~0ULL) >> 1))) /* 0x7fffffff */
198 abort();
199 #else
200 if (ull2d(~0ULL) != (double) ~0ULL) /* 0xffffffff */
201 abort();
202 if (ull2d((~0ULL) >> 1) != (double) ((~0ULL) >> 1)) /* 0x7fffffff */
203 abort();
204 #endif
205 if (ull2d(~((~0ULL) >> 1)) != (double) ~((~0ULL) >> 1)) /* 0x80000000 */
206 abort();
207
208 if (ull2ld(0ULL) != (long double) 0ULL) /* 0 */
209 abort();
210 if (ull2ld(~0ULL) != (long double) ~0ULL) /* 0xffffffff */
211 abort();
212 if (ull2ld((~0ULL) >> 1) != (long double) ((~0ULL) >> 1)) /* 0x7fffffff */
213 abort();
214 if (ull2ld(~((~0ULL) >> 1)) != (long double) ~((~0ULL) >> 1)) /* 0x80000000 */
215 abort();
216
217 if (sll2f(0LL) != (float) 0LL) /* 0 */
218 abort();
219 if (sll2f(~0LL) != (float) ~0LL) /* 0xffffffff */
220 abort();
221 if (! fnear (sll2f((long long int)((~0ULL) >> 1)), (float)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
222 abort();
223 if (sll2f((long long int)(~((~0ULL) >> 1))) != (float)(long long int)~((~0ULL) >> 1)) /* 0x80000000 */
224 abort();
225
226 if (sll2d(0LL) != (double) 0LL) /* 0 */
227 abort();
228 if (sll2d(~0LL) != (double) ~0LL) /* 0xffffffff */
229 abort();
230 if (!dnear (sll2d((long long int)((~0ULL) >> 1)), (double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
231 abort();
232 if (! dnear (sll2d((long long int)~((~0ULL) >> 1)), (double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
233 abort();
234
235 if (sll2ld(0LL) != (long double) 0LL) /* 0 */
236 abort();
237 if (sll2ld(~0LL) != (long double) ~0LL) /* 0xffffffff */
238 abort();
239 if (!ldnear (sll2ld((long long int)((~0ULL) >> 1)), (long double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
240 abort();
241 if (! ldnear (sll2ld((long long int)~((~0ULL) >> 1)), (long double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
242 abort();
243 }
244 #endif
245
246 unsigned int
247 f2u(float f)
248 {
249 return (unsigned) f;
250 }
251
252 unsigned int
253 d2u(double d)
254 {
255 return (unsigned) d;
256 }
257
258 unsigned int
259 ld2u(long double d)
260 {
261 return (unsigned) d;
262 }
263
264 int
265 f2s(float f)
266 {
267 return (int) f;
268 }
269
270 int
271 d2s(double d)
272 {
273 return (int) d;
274 }
275
276 int
277 ld2s(long double d)
278 {
279 return (int) d;
280 }
281
282 void
283 test_float_to_integer()
284 {
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 if (f2u((float) ((~0U) >> 1)) != (~0U) >> 1 && /* 0x7fffffff */
294 f2u((float) ((~0U) >> 1)) != ((~0U) >> 1) + 1)
295 abort();
296 if (f2u((float) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
297 abort();
298
299 /* These tests require double precision, so for hosts that don't offer
300 that much precision, just ignore these test. */
301 if (sizeof (double) >= 8) {
302 if (d2u(0.0) != 0)
303 abort();
304 if (d2u(0.999) != 0)
305 abort();
306 if (d2u(1.0) != 1)
307 abort();
308 if (d2u(1.99) != 1)
309 abort();
310 if (d2u((double) (~0U)) != ~0U) /* 0xffffffff */
311 abort();
312 if (d2u((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
313 abort();
314 if (d2u((double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
315 abort();
316 }
317
318 /* These tests require long double precision, so for hosts that don't offer
319 that much precision, just ignore these test. */
320 if (sizeof (long double) >= 8) {
321 if (ld2u(0.0) != 0)
322 abort();
323 if (ld2u(0.999) != 0)
324 abort();
325 if (ld2u(1.0) != 1)
326 abort();
327 if (ld2u(1.99) != 1)
328 abort();
329 if (ld2u((long double) (~0U)) != ~0U) /* 0xffffffff */
330 abort();
331 if (ld2u((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
332 abort();
333 if (ld2u((long double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */
334 abort();
335 }
336
337 if (f2s(0.0) != 0)
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(-0.999) != 0)
346 abort();
347 if (f2s(-1.0) != -1)
348 abort();
349 if (f2s(-1.99) != -1)
350 abort();
351 if (f2s((float)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
352 abort();
353
354 /* These tests require double precision, so for hosts that don't offer
355 that much precision, just ignore these test. */
356 if (sizeof (double) >= 8) {
357 if (d2s(0.0) != 0)
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(-0.999) != 0)
366 abort();
367 if (d2s(-1.0) != -1)
368 abort();
369 if (d2s(-1.99) != -1)
370 abort();
371 if (d2s((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
372 abort();
373 if (d2s((double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
374 abort();
375 }
376
377 /* These tests require long double precision, so for hosts that don't offer
378 that much precision, just ignore these test. */
379 if (sizeof (long double) >= 8) {
380 if (ld2s(0.0) != 0)
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(-0.999) != 0)
389 abort();
390 if (ld2s(-1.0) != -1)
391 abort();
392 if (ld2s(-1.99) != -1)
393 abort();
394 if (ld2s((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */
395 abort();
396 if (ld2s((long double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
397 abort();
398 }
399 }
400
401 #if __GNUC__
402 unsigned long long int
403 f2ull(float f)
404 {
405 return (unsigned long long int) f;
406 }
407
408 unsigned long long int
409 d2ull(double d)
410 {
411 return (unsigned long long int) d;
412 }
413
414 unsigned long long int
415 ld2ull(long double d)
416 {
417 return (unsigned long long int) d;
418 }
419
420 long long int
421 f2sll(float f)
422 {
423 return (long long int) f;
424 }
425
426 long long int
427 d2sll(double d)
428 {
429 return (long long int) d;
430 }
431
432 long long int
433 ld2sll(long double d)
434 {
435 return (long long int) d;
436 }
437
438 void
439 test_float_to_longlong_integer()
440 {
441 if (f2ull(0.0) != 0LL)
442 abort();
443 if (f2ull(0.999) != 0LL)
444 abort();
445 if (f2ull(1.0) != 1LL)
446 abort();
447 if (f2ull(1.99) != 1LL)
448 abort();
449 if (f2ull((float) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
450 f2ull((float) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
451 abort();
452 if (f2ull((float) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
453 abort();
454
455 if (d2ull(0.0) != 0LL)
456 abort();
457 if (d2ull(0.999) != 0LL)
458 abort();
459 if (d2ull(1.0) != 1LL)
460 abort();
461 if (d2ull(1.99) != 1LL)
462 abort();
463 if (d2ull((double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
464 d2ull((double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
465 abort();
466 if (d2ull((double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
467 abort();
468
469 if (ld2ull(0.0) != 0LL)
470 abort();
471 if (ld2ull(0.999) != 0LL)
472 abort();
473 if (ld2ull(1.0) != 1LL)
474 abort();
475 if (ld2ull(1.99) != 1LL)
476 abort();
477 if (ld2ull((long double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
478 ld2ull((long double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
479 abort();
480 if (ld2ull((long double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
481 abort();
482
483
484 if (f2sll(0.0) != 0LL)
485 abort();
486 if (f2sll(0.999) != 0LL)
487 abort();
488 if (f2sll(1.0) != 1LL)
489 abort();
490 if (f2sll(1.99) != 1LL)
491 abort();
492 if (f2sll(-0.999) != 0LL)
493 abort();
494 if (f2sll(-1.0) != -1LL)
495 abort();
496 if (f2sll(-1.99) != -1LL)
497 abort();
498 if (f2sll((float)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
499 abort();
500
501 if (d2sll(0.0) != 0LL)
502 abort();
503 if (d2sll(0.999) != 0LL)
504 abort();
505 if (d2sll(1.0) != 1LL)
506 abort();
507 if (d2sll(1.99) != 1LL)
508 abort();
509 if (d2sll(-0.999) != 0LL)
510 abort();
511 if (d2sll(-1.0) != -1LL)
512 abort();
513 if (d2sll(-1.99) != -1LL)
514 abort();
515 if (d2sll((double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
516 abort();
517
518 if (ld2sll(0.0) != 0LL)
519 abort();
520 if (ld2sll(0.999) != 0LL)
521 abort();
522 if (ld2sll(1.0) != 1LL)
523 abort();
524 if (ld2sll(1.99) != 1LL)
525 abort();
526 if (ld2sll(-0.999) != 0LL)
527 abort();
528 if (ld2sll(-1.0) != -1LL)
529 abort();
530 if (ld2sll(-1.99) != -1LL)
531 abort();
532 if (ld2sll((long double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
533 abort();
534 }
535 #endif
536
537 int
538 main(void)
539 {
540 test_integer_to_float();
541 test_float_to_integer();
542 #if __GNUC__
543 test_longlong_integer_to_float();
544 test_float_to_longlong_integer();
545 #endif
546 exit(0);
547 }