OSDN Git Service

* gcc.c-torture/execute/20000412-1.c: Reduce some more.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.c-torture / execute / conversion.c
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 float
12 u2f(u)
13      unsigned int u;
14 {
15   return u;
16 }
17
18 double
19 u2d(u)
20      unsigned int u;
21 {
22   return u;
23 }
24
25 float
26 s2f(s)
27      int s;
28 {
29   return s;
30 }
31
32 double
33 s2d(s)
34      int s;
35 {
36   return s;
37 }
38
39 int
40 fnear (float x, float y)
41 {
42   float t = x - y;
43   return t == 0 || x / t > 1000000.0;
44 }
45
46 int
47 dnear (double x, double y)
48 {
49   double t = x - y;
50   return t == 0 || x / t > 100000000000000.0;
51 }
52
53 test_integer_to_float()
54 {
55   if (u2f(0U) != (float) 0U)                            /* 0 */
56     abort();
57   if (!fnear (u2f(~0U), (float) ~0U))                   /* 0xffffffff */
58     abort();
59   if (!fnear (u2f((~0U) >> 1), (float) ((~0U) >> 1)))   /* 0x7fffffff */
60     abort();
61   if (u2f(~((~0U) >> 1)) != (float) ~((~0U) >> 1))      /* 0x80000000 */
62     abort();
63
64   if (u2d(0U) != (double) 0U)                           /* 0 */
65     abort();
66   if (!dnear (u2d(~0U), (double) ~0U))                  /* 0xffffffff */
67     abort();
68   if (!dnear (u2d((~0U) >> 1),(double) ((~0U) >> 1)))   /* 0x7fffffff */
69     abort();
70   if (u2d(~((~0U) >> 1)) != (double) ~((~0U) >> 1))     /* 0x80000000 */
71     abort();
72
73   if (s2f(0) != (float) 0)                              /* 0 */
74     abort();
75   if (!fnear (s2f(~0), (float) ~0))                     /* 0xffffffff */
76     abort();
77   if (!fnear (s2f((int)((~0U) >> 1)), (float)(int)((~0U) >> 1))) /* 0x7fffffff */
78     abort();
79   if (s2f((int)(~((~0U) >> 1))) != (float)(int)~((~0U) >> 1)) /* 0x80000000 */
80     abort();
81
82   if (s2d(0) != (double) 0)                             /* 0 */
83     abort();
84   if (!dnear (s2d(~0), (double) ~0))                    /* 0xffffffff */
85     abort();
86   if (!dnear (s2d((int)((~0U) >> 1)), (double)(int)((~0U) >> 1))) /* 0x7fffffff */
87     abort();
88   if (s2d((int)~((~0U) >> 1)) != (double)(int)~((~0U) >> 1)) /* 0x80000000 */
89     abort();
90 }
91
92 #if __GNUC__
93 float
94 ull2f(u)
95      unsigned long long int u;
96 {
97   return u;
98 }
99
100 double
101 ull2d(u)
102      unsigned long long int u;
103 {
104   return u;
105 }
106
107 float
108 sll2f(s)
109      long long int s;
110 {
111   return s;
112 }
113
114 double
115 sll2d(s)
116      long long int s;
117 {
118   return s;
119 }
120
121 test_longlong_integer_to_float()
122 {
123   if (ull2f(0ULL) != (float) 0ULL)                      /* 0 */
124     abort();
125   if (ull2f(~0ULL) != (float) ~0ULL)                    /* 0xffffffff */
126     abort();
127   if (ull2f((~0ULL) >> 1) != (float) ((~0ULL) >> 1))    /* 0x7fffffff */
128     abort();
129   if (ull2f(~((~0ULL) >> 1)) != (float) ~((~0ULL) >> 1)) /* 0x80000000 */
130     abort();
131
132   if (ull2d(0ULL) != (double) 0ULL)                     /* 0 */
133     abort();
134 #if __HAVE_68881__
135   /* Some 68881 targets return values in fp0, with excess precision.
136      But the compile-time conversion to double works correctly.  */
137   if (! dnear (ull2d(~0ULL), (double) ~0ULL))           /* 0xffffffff */
138     abort();
139   if (! dnear (ull2d((~0ULL) >> 1), (double) ((~0ULL) >> 1))) /* 0x7fffffff */
140     abort();
141 #else
142   if (ull2d(~0ULL) != (double) ~0ULL)                   /* 0xffffffff */
143     abort();
144   if (ull2d((~0ULL) >> 1) != (double) ((~0ULL) >> 1))   /* 0x7fffffff */
145     abort();
146 #endif
147   if (ull2d(~((~0ULL) >> 1)) != (double) ~((~0ULL) >> 1)) /* 0x80000000 */
148     abort();
149
150   if (sll2f(0LL) != (float) 0LL)                        /* 0 */
151     abort();
152   if (sll2f(~0LL) != (float) ~0LL)                      /* 0xffffffff */
153     abort();
154   if (! fnear (sll2f((long long int)((~0ULL) >> 1)), (float)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
155     abort();
156   if (sll2f((long long int)(~((~0ULL) >> 1))) != (float)(long long int)~((~0ULL) >> 1)) /* 0x80000000 */
157     abort();
158
159   if (sll2d(0LL) != (double) 0LL)                       /* 0 */
160     abort();
161   if (sll2d(~0LL) != (double) ~0LL)                     /* 0xffffffff */
162     abort();
163   if (!dnear (sll2d((long long int)((~0ULL) >> 1)), (double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
164     abort();
165   if (! dnear (sll2d((long long int)~((~0ULL) >> 1)), (double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
166     abort();
167 }
168 #endif
169
170 unsigned int
171 f2u(float f)
172 {
173   return (unsigned) f;
174 }
175
176 unsigned int
177 d2u(double d)
178 {
179   return (unsigned) d;
180 }
181
182 int
183 f2s(float f)
184 {
185   return (int) f;
186 }
187
188 int
189 d2s(double d)
190 {
191   return (int) d;
192 }
193
194 test_float_to_integer()
195 {
196   if (f2u(0.0) != 0)
197     abort();
198   if (f2u(0.999) != 0)
199     abort();
200   if (f2u(1.0) != 1)
201     abort();
202   if (f2u(1.99) != 1)
203     abort();
204   if (f2u((float) ((~0U) >> 1)) != (~0U) >> 1 &&        /* 0x7fffffff */
205       f2u((float) ((~0U) >> 1)) != ((~0U) >> 1) + 1)
206     abort();
207   if (f2u((float) ~((~0U) >> 1)) != ~((~0U) >> 1))      /* 0x80000000 */
208     abort();
209
210  /* CYGNUS LOCAL -- amylaar/32bit doubles */
211  /* These tests require double precision, so for hosts that don't offer
212     that much precision, just ignore these test.  */
213  if (sizeof (double) >= 8) {
214  /* END CYGNUS LOCAL -- amylaar/32bit doubles */
215   if (d2u(0.0) != 0)
216     abort();
217   if (d2u(0.999) != 0)
218     abort();
219   if (d2u(1.0) != 1)
220     abort();
221   if (d2u(1.99) != 1)
222     abort();
223   if (d2u((double) (~0U)) != ~0U)                       /* 0xffffffff */
224     abort();
225   if (d2u((double) ((~0U) >> 1)) != (~0U) >> 1)         /* 0x7fffffff */
226     abort();
227   if (d2u((double) ~((~0U) >> 1)) != ~((~0U) >> 1))     /* 0x80000000 */
228     abort();
229  /* CYGNUS LOCAL -- amylaar/32bit doubles */
230  }
231  /* END CYGNUS LOCAL -- amylaar/32bit doubles */
232
233
234   if (f2s(0.0) != 0)
235     abort();
236   if (f2s(0.999) != 0)
237     abort();
238   if (f2s(1.0) != 1)
239     abort();
240   if (f2s(1.99) != 1)
241     abort();
242   if (f2s(-0.999) != 0)
243     abort();
244   if (f2s(-1.0) != -1)
245     abort();
246   if (f2s(-1.99) != -1)
247     abort();
248   if (f2s((float)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
249     abort();
250
251  /* CYGNUS LOCAL -- amylaar/32bit doubles */
252  /* These tests require double precision, so for hosts that don't offer
253     that much precision, just ignore these test.  */
254  if (sizeof (double) >= 8) {
255  /* END CYGNUS LOCAL -- amylaar/32bit doubles */
256   if (d2s(0.0) != 0)
257     abort();
258   if (d2s(0.999) != 0)
259     abort();
260   if (d2s(1.0) != 1)
261     abort();
262   if (d2s(1.99) != 1)
263     abort();
264   if (d2s(-0.999) != 0)
265     abort();
266   if (d2s(-1.0) != -1)
267     abort();
268   if (d2s(-1.99) != -1)
269     abort();
270   if (d2s((double) ((~0U) >> 1)) != (~0U) >> 1)         /* 0x7fffffff */
271     abort();
272   if (d2s((double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
273     abort();
274  /* CYGNUS LOCAL -- amylaar/32bit doubles */
275  }
276  /* END CYGNUS LOCAL -- amylaar/32bit doubles */
277 }
278
279 #if __GNUC__
280 unsigned long long int
281 f2ull(float f)
282 {
283   return (unsigned long long int) f;
284 }
285
286 unsigned long long int
287 d2ull(double d)
288 {
289   return (unsigned long long int) d;
290 }
291
292 long long int
293 f2sll(float f)
294 {
295   return (long long int) f;
296 }
297
298 long long int
299 d2sll(double d)
300 {
301   return (long long int) d;
302 }
303
304 test_float_to_longlong_integer()
305 {
306   if (f2ull(0.0) != 0LL)
307     abort();
308   if (f2ull(0.999) != 0LL)
309     abort();
310   if (f2ull(1.0) != 1LL)
311     abort();
312   if (f2ull(1.99) != 1LL)
313     abort();
314   if (f2ull((float) ((~0ULL) >> 1)) != (~0ULL) >> 1 &&  /* 0x7fffffff */
315       f2ull((float) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
316     abort();
317   if (f2ull((float) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
318     abort();
319
320   if (d2ull(0.0) != 0LL)
321     abort();
322   if (d2ull(0.999) != 0LL)
323     abort();
324   if (d2ull(1.0) != 1LL)
325     abort();
326   if (d2ull(1.99) != 1LL)
327     abort();
328   if (d2ull((double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */
329       d2ull((double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
330     abort();
331   if (d2ull((double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
332     abort();
333
334
335   if (f2sll(0.0) != 0LL)
336     abort();
337   if (f2sll(0.999) != 0LL)
338     abort();
339   if (f2sll(1.0) != 1LL)
340     abort();
341   if (f2sll(1.99) != 1LL)
342     abort();
343   if (f2sll(-0.999) != 0LL)
344     abort();
345   if (f2sll(-1.0) != -1LL)
346     abort();
347   if (f2sll(-1.99) != -1LL)
348     abort();
349   if (f2sll((float)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
350     abort();
351
352   if (d2sll(0.0) != 0LL)
353     abort();
354   if (d2sll(0.999) != 0LL)
355     abort();
356   if (d2sll(1.0) != 1LL)
357     abort();
358   if (d2sll(1.99) != 1LL)
359     abort();
360   if (d2sll(-0.999) != 0LL)
361     abort();
362   if (d2sll(-1.0) != -1LL)
363     abort();
364   if (d2sll(-1.99) != -1LL)
365     abort();
366   if (d2sll((double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
367     abort();
368 }
369 #endif
370
371 main()
372 {
373   test_integer_to_float();
374   test_float_to_integer();
375 #if __GNUC__
376   test_longlong_integer_to_float();
377   test_float_to_longlong_integer();
378 #endif
379   exit(0);
380 }