2008-03-21 Janne Blomqvist <jb@gcc.gnu.org>
[official-gcc.git] / gcc / testsuite / gcc.dg / builtins-20.c
blob43aa71b271a23115563200c0b60cb6dc2ba571cd
1 /* Copyright (C) 2003 Free Software Foundation.
3 Verify that built-in math function constant folding doesn't break
4 anything and produces the expected results.
6 Written by Roger Sayle, 8th June 2003. */
8 /* { dg-do link } */
9 /* { dg-options "-O2 -ffast-math" } */
10 /* { dg-add-options c99_runtime } */
12 #include "builtins-config.h"
14 extern double cos (double);
15 extern double sin (double);
16 extern double tan (double);
17 extern double fabs (double);
18 extern double atan2 (double, double);
19 extern double copysign (double, double);
20 extern double fmin (double, double);
21 extern double fmax (double, double);
22 extern double hypot (double, double);
23 extern double pure (double) __attribute__ ((__pure__));
24 extern double carg (__complex__ double);
25 extern __complex__ double ccos (__complex__ double);
26 extern __complex__ double ctan (__complex__ double);
27 extern float cosf (float);
28 extern float sinf (float);
29 extern float tanf (float);
30 extern float fabsf (float);
31 extern float atan2f (float, float);
32 extern float copysignf (float, float);
33 extern float fminf (float, float);
34 extern float fmaxf (float, float);
35 extern float hypotf (float, float);
36 extern float puref (float) __attribute__ ((__pure__));
37 extern float cargf (__complex__ float);
38 extern __complex__ float ccosf (__complex__ float);
39 extern __complex__ float ctanf (__complex__ float);
40 extern long double cosl (long double);
41 extern long double sinl (long double);
42 extern long double tanl (long double);
43 extern long double fabsl (long double);
44 extern long double atan2l (long double, long double);
45 extern long double copysignl (long double, long double);
46 extern long double fminl (long double, long double);
47 extern long double fmaxl (long double, long double);
48 extern long double hypotl (long double, long double);
49 extern long double purel (long double) __attribute__ ((__pure__));
50 extern long double cargl (__complex__ long double);
51 extern __complex__ long double ccosl (__complex__ long double);
52 extern __complex__ long double ctanl (__complex__ long double);
54 extern void link_error(void);
56 void test1(double x)
58 if (cos(x) != cos(-x))
59 link_error ();
61 if (cos(x) != cos(fabs(x)))
62 link_error ();
64 if (cos(x) != cos(-fabs(x)))
65 link_error ();
67 if (cos(tan(x)) != cos(tan(-fabs(x))))
68 link_error ();
70 if (sin(x)/cos(x) != tan(x))
71 link_error ();
73 if (cos(x)/sin(x) != 1.0/tan(x))
74 link_error ();
76 if (tan(x)*cos(x) != sin(x))
77 link_error ();
79 if (cos(x)*tan(x) != sin(x))
80 link_error ();
82 if (sin(x)/tan(x) != cos(x))
83 link_error ();
85 if (tan(x)/sin(x) != 1.0/cos(x))
86 link_error ();
89 void test2(double x, double y)
91 if (-tan(x-y) != tan(y-x))
92 link_error ();
94 if (-sin(x-y) != sin(y-x))
95 link_error ();
97 if (cos(-x*y) != cos(x*y))
98 link_error ();
100 if (cos(x*-y) != cos(x*y))
101 link_error ();
103 if (cos(-x/y) != cos(x/y))
104 link_error ();
106 if (cos(x/-y) != cos(x/y))
107 link_error ();
109 if (cos(-fabs(tan(x/-y))) != cos(tan(x/y)))
110 link_error ();
112 if (cos(y<10 ? -x : y) != cos(y<10 ? x : y))
113 link_error ();
115 if (cos(y<10 ? x : -y) != cos(y<10 ? x : y))
116 link_error ();
118 if (cos(y<10 ? -fabs(x) : tan(x<20 ? -x : -fabs(y)))
119 != cos(y<10 ? x : tan(x<20 ? x : y)))
120 link_error ();
122 if (cos((y*=3, -x)) != cos((y*=3,x)))
123 link_error ();
125 if (cos((y*=2, -fabs(tan(x/-y)))) != cos((y*=2,tan(x/y))))
126 link_error ();
128 if (cos(copysign(x,y)) != cos(x))
129 link_error ();
131 if (cos(copysign(-fabs(x),y*=2)) != cos((y*=2,x)))
132 link_error ();
134 if (hypot (x, 0) != fabs(x))
135 link_error ();
137 if (hypot (0, x) != fabs(x))
138 link_error ();
140 if (hypot (x, x) != fabs(x) * __builtin_sqrt(2))
141 link_error ();
143 if (hypot (-x, y) != hypot (x, y))
144 link_error ();
146 if (hypot (x, -y) != hypot (x, y))
147 link_error ();
149 if (hypot (-x, -y) != hypot (x, y))
150 link_error ();
152 if (hypot (fabs(x), y) != hypot (x, y))
153 link_error ();
155 if (hypot (x, fabs(y)) != hypot (x, y))
156 link_error ();
158 if (hypot (fabs(x), fabs(y)) != hypot (x, y))
159 link_error ();
161 if (hypot (-fabs(-x), -fabs(fabs(fabs(-y)))) != hypot (x, y))
162 link_error ();
164 if (hypot (-x, 0) != fabs(x))
165 link_error ();
167 if (hypot (-x, x) != fabs(x) * __builtin_sqrt(2))
168 link_error ();
170 if (hypot (pure(x), -pure(x)) != fabs(pure(x)) * __builtin_sqrt(2))
171 link_error ();
173 if (hypot (tan(-x), tan(-fabs(y))) != hypot (tan(x), tan(y)))
174 link_error ();
176 if (fmin (fmax(x,y),y) != y)
177 link_error ();
179 if (fmin (fmax(y,x),y) != y)
180 link_error ();
182 if (fmin (x,fmax(x,y)) != x)
183 link_error ();
185 if (fmin (x,fmax(y,x)) != x)
186 link_error ();
188 if (fmax (fmin(x,y),y) != y)
189 link_error ();
191 if (fmax (fmin(y,x),y) != y)
192 link_error ();
194 if (fmax (x,fmin(x,y)) != x)
195 link_error ();
197 if (fmax (x,fmin(y,x)) != x)
198 link_error ();
200 if ((__complex__ double) x != -(__complex__ double) (-x))
201 link_error ();
203 if (x*1i != -(-x*1i))
204 link_error ();
206 if (x+(x-y)*1i != -(-x+(y-x)*1i))
207 link_error ();
209 if (x+(x-y)*1i != -(-x-(x-y)*1i))
210 link_error ();
212 if (ccos(tan(x)+sin(y)*1i) != ccos(-tan(-x)+-sin(-y)*1i))
213 link_error ();
215 if (ccos(tan(x)+sin(x-y)*1i) != ccos(-tan(-x)-sin(y-x)*1i))
216 link_error ();
218 if (-5+x*1i != -~(5+x*1i))
219 link_error ();
221 if (tan(x)+tan(y)*1i != -~(tan(-x)+tan(y)*1i))
222 link_error ();
225 void test3(__complex__ double x, __complex__ double y, int i)
227 if (carg(x) != atan2(__imag__ x, __real__ x))
228 link_error ();
230 if (ccos(x) != ccos(-x))
231 link_error();
233 if (ccos(ctan(x)) != ccos(ctan(-x)))
234 link_error();
236 if (ctan(x-y) != -ctan(y-x))
237 link_error();
239 if (ccos(x/y) != ccos(-x/y))
240 link_error();
242 if (ccos(x/y) != ccos(x/-y))
243 link_error();
245 if (ccos(x/ctan(y)) != ccos(-x/ctan(-y)))
246 link_error();
248 if (ccos(x*y) != ccos(-x*y))
249 link_error();
251 if (ccos(x*y) != ccos(x*-y))
252 link_error();
254 if (ccos(ctan(x)*y) != ccos(ctan(-x)*-y))
255 link_error();
257 if (ccos(ctan(x/y)) != ccos(-ctan(x/-y)))
258 link_error();
260 if (ccos(i ? x : y) != ccos(i ? -x : y))
261 link_error();
263 if (ccos(i ? x : y) != ccos(i ? x : -y))
264 link_error();
266 if (ccos(i ? x : ctan(y/x)) != ccos(i ? -x : -ctan(-y/x)))
267 link_error();
269 if (~x != -~-x)
270 link_error();
272 if (ccos(~x) != ccos(-~-x))
273 link_error();
275 if (ctan(~(x-y)) != -ctan(~(y-x)))
276 link_error();
278 if (ctan(~(x/y)) != -ctan(~(x/-y)))
279 link_error();
282 void test1f(float x)
284 if (cosf(x) != cosf(-x))
285 link_error ();
287 if (cosf(x) != cosf(fabsf(x)))
288 link_error ();
290 if (cosf(x) != cosf(-fabsf(x)))
291 link_error ();
293 if (cosf(tanf(x)) != cosf(tanf(-fabsf(x))))
294 link_error ();
296 #ifdef HAVE_C99_RUNTIME
297 if (sinf(x)/cosf(x) != tanf(x))
298 link_error ();
300 if (cosf(x)/sinf(x) != 1.0f/tanf(x))
301 link_error ();
303 if (tanf(x)*cosf(x) != sinf(x))
304 link_error ();
306 if (cosf(x)*tanf(x) != sinf(x))
307 link_error ();
309 if (sinf(x)/tanf(x) != cosf(x))
310 link_error ();
312 if (tanf(x)/sinf(x) != 1.0f/cosf(x))
313 link_error ();
314 #endif
317 void test2f(float x, float y)
319 if (-tanf(x-y) != tanf(y-x))
320 link_error ();
322 if (-sinf(x-y) != sinf(y-x))
323 link_error ();
325 if (cosf(-x*y) != cosf(x*y))
326 link_error ();
328 if (cosf(x*-y) != cosf(x*y))
329 link_error ();
331 if (cosf(-x/y) != cosf(x/y))
332 link_error ();
334 if (cosf(x/-y) != cosf(x/y))
335 link_error ();
337 if (cosf(-fabsf(tanf(x/-y))) != cosf(tanf(x/y)))
338 link_error ();
340 if (cosf(y<10 ? -x : y) != cosf(y<10 ? x : y))
341 link_error ();
343 if (cosf(y<10 ? x : -y) != cosf(y<10 ? x : y))
344 link_error ();
346 if (cosf(y<10 ? -fabsf(x) : tanf(x<20 ? -x : -fabsf(y)))
347 != cosf(y<10 ? x : tanf(x<20 ? x : y)))
348 link_error ();
350 if (cosf((y*=3, -x)) != cosf((y*=3,x)))
351 link_error ();
353 if (cosf((y*=2, -fabsf(tanf(x/-y)))) != cosf((y*=2,tanf(x/y))))
354 link_error ();
356 if (cosf(copysignf(x,y)) != cosf(x))
357 link_error ();
359 if (cosf(copysignf(-fabsf(x),y*=2)) != cosf((y*=2,x)))
360 link_error ();
362 if (hypotf (x, 0) != fabsf(x))
363 link_error ();
365 if (hypotf (0, x) != fabsf(x))
366 link_error ();
368 if (hypotf (x, x) != fabsf(x) * __builtin_sqrtf(2))
369 link_error ();
371 if (hypotf (-x, y) != hypotf (x, y))
372 link_error ();
374 if (hypotf (x, -y) != hypotf (x, y))
375 link_error ();
377 if (hypotf (-x, -y) != hypotf (x, y))
378 link_error ();
380 if (hypotf (fabsf(x), y) != hypotf (x, y))
381 link_error ();
383 if (hypotf (x, fabsf(y)) != hypotf (x, y))
384 link_error ();
386 if (hypotf (fabsf(x), fabsf(y)) != hypotf (x, y))
387 link_error ();
389 if (hypotf (-fabsf(-x), -fabsf(fabsf(fabsf(-y)))) != hypotf (x, y))
390 link_error ();
392 if (hypotf (-x, 0) != fabsf(x))
393 link_error ();
395 if (hypotf (-x, x) != fabsf(x) * __builtin_sqrtf(2))
396 link_error ();
398 if (hypotf (puref(x), -puref(x)) != fabsf(puref(x)) * __builtin_sqrtf(2))
399 link_error ();
401 if (hypotf (tanf(-x), tanf(-fabsf(y))) != hypotf (tanf(x), tanf(y)))
402 link_error ();
404 if (fminf (fmaxf(x,y),y) != y)
405 link_error ();
407 if (fminf (fmaxf(y,x),y) != y)
408 link_error ();
410 if (fminf (x,fmaxf(x,y)) != x)
411 link_error ();
413 if (fminf (x,fmaxf(y,x)) != x)
414 link_error ();
416 if (fmaxf (fminf(x,y),y) != y)
417 link_error ();
419 if (fmaxf (fminf(y,x),y) != y)
420 link_error ();
422 if (fmaxf (x,fminf(x,y)) != x)
423 link_error ();
425 if (fmaxf (x,fminf(y,x)) != x)
426 link_error ();
428 if ((__complex__ float) x != -(__complex__ float) (-x))
429 link_error ();
431 if (x+(x-y)*1i != -(-x+(y-x)*1i))
432 link_error ();
434 if (x+(x-y)*1i != -(-x-(x-y)*1i))
435 link_error ();
437 if (ccosf(tanf(x)+sinf(y)*1i) != ccosf(-tanf(-x)+-sinf(-y)*1i))
438 link_error ();
440 if (ccosf(tanf(x)+sinf(x-y)*1i) != ccosf(-tanf(-x)-sinf(y-x)*1i))
441 link_error ();
443 if (-5+x*1i != -~(5+x*1i))
444 link_error ();
446 if (tanf(x)+tanf(y)*1i != -~(tanf(-x)+tanf(y)*1i))
447 link_error ();
450 void test3f(__complex__ float x, __complex__ float y, int i)
452 if (ccosf(x) != ccosf(-x))
453 link_error();
455 if (ccosf(ctanf(x)) != ccosf(ctanf(-x)))
456 link_error();
458 if (ctanf(x-y) != -ctanf(y-x))
459 link_error();
461 if (ccosf(x/y) != ccosf(-x/y))
462 link_error();
464 if (ccosf(x/y) != ccosf(x/-y))
465 link_error();
467 if (ccosf(x/ctanf(y)) != ccosf(-x/ctanf(-y)))
468 link_error();
470 if (ccosf(x*y) != ccosf(-x*y))
471 link_error();
473 if (ccosf(x*y) != ccosf(x*-y))
474 link_error();
476 if (ccosf(ctanf(x)*y) != ccosf(ctanf(-x)*-y))
477 link_error();
479 if (ccosf(ctanf(x/y)) != ccosf(-ctanf(x/-y)))
480 link_error();
482 if (ccosf(i ? x : y) != ccosf(i ? -x : y))
483 link_error();
485 if (ccosf(i ? x : y) != ccosf(i ? x : -y))
486 link_error();
488 if (ccosf(i ? x : ctanf(y/x)) != ccosf(i ? -x : -ctanf(-y/x)))
489 link_error();
491 if (~x != -~-x)
492 link_error();
494 if (ccosf(~x) != ccosf(-~-x))
495 link_error();
497 if (ctanf(~(x-y)) != -ctanf(~(y-x)))
498 link_error();
500 if (ctanf(~(x/y)) != -ctanf(~(x/-y)))
501 link_error();
503 #ifdef HAVE_C99_RUNTIME
504 if (cargf(x) != atan2f(__imag__ x, __real__ x))
505 link_error ();
506 #endif
509 void test1l(long double x)
511 if (cosl(x) != cosl(-x))
512 link_error ();
514 if (cosl(x) != cosl(fabsl(x)))
515 link_error ();
517 if (cosl(x) != cosl(-fabsl(x)))
518 link_error ();
520 if (cosl(tanl(x)) != cosl(tanl(-fabsl(x))))
521 link_error ();
523 #ifdef HAVE_C99_RUNTIME
524 if (sinl(x)/cosl(x) != tanl(x))
525 link_error ();
527 if (cosl(x)/sinl(x) != 1.0l/tanl(x))
528 link_error ();
530 if (tanl(x)*cosl(x) != sinl(x))
531 link_error ();
533 if (cosl(x)*tanl(x) != sinl(x))
534 link_error ();
536 if (sinl(x)/tanl(x) != cosl(x))
537 link_error ();
539 if (tanl(x)/sinl(x) != 1.0l/cosl(x))
540 link_error ();
541 #endif
544 void test2l(long double x, long double y)
546 if (-tanl(x-y) != tanl(y-x))
547 link_error ();
549 if (-sinl(x-y) != sinl(y-x))
550 link_error ();
552 if (cosl(-x*y) != cosl(x*y))
553 link_error ();
555 if (cosl(x*-y) != cosl(x*y))
556 link_error ();
558 if (cosl(-x/y) != cosl(x/y))
559 link_error ();
561 if (cosl(x/-y) != cosl(x/y))
562 link_error ();
564 if (cosl(-fabsl(tanl(x/-y))) != cosl(tanl(x/y)))
565 link_error ();
567 if (cosl(y<10 ? -x : y) != cosl(y<10 ? x : y))
568 link_error ();
570 if (cosl(y<10 ? x : -y) != cosl(y<10 ? x : y))
571 link_error ();
573 if (cosl(y<10 ? -fabsl(x) : tanl(x<20 ? -x : -fabsl(y)))
574 != cosl(y<10 ? x : tanl(x<20 ? x : y)))
575 link_error ();
577 if (cosl((y*=3, -x)) != cosl((y*=3,x)))
578 link_error ();
580 if (cosl((y*=2, -fabsl(tanl(x/-y)))) != cosl((y*=2,tanl(x/y))))
581 link_error ();
583 if (cosl(copysignl(x,y)) != cosl(x))
584 link_error ();
586 if (cosl(copysignl(-fabsl(x),y*=2)) != cosl((y*=2,x)))
587 link_error ();
589 if (hypotl (x, 0) != fabsl(x))
590 link_error ();
592 if (hypotl (0, x) != fabsl(x))
593 link_error ();
595 if (hypotl (x, x) != fabsl(x) * __builtin_sqrtl(2))
596 link_error ();
598 if (hypotl (-x, y) != hypotl (x, y))
599 link_error ();
601 if (hypotl (x, -y) != hypotl (x, y))
602 link_error ();
604 if (hypotl (-x, -y) != hypotl (x, y))
605 link_error ();
607 if (hypotl (fabsl(x), y) != hypotl (x, y))
608 link_error ();
610 if (hypotl (x, fabsl(y)) != hypotl (x, y))
611 link_error ();
613 if (hypotl (fabsl(x), fabsl(y)) != hypotl (x, y))
614 link_error ();
616 if (hypotl (-fabsl(-x), -fabsl(fabsl(fabsl(-y)))) != hypotl (x, y))
617 link_error ();
619 if (hypotl (-x, 0) != fabsl(x))
620 link_error ();
622 if (hypotl (-x, x) != fabsl(x) * __builtin_sqrtl(2))
623 link_error ();
625 if (hypotl (purel(x), -purel(x)) != fabsl(purel(x)) * __builtin_sqrtl(2))
626 link_error ();
628 if (hypotl (tanl(-x), tanl(-fabsl(y))) != hypotl (tanl(x), tanl(y)))
629 link_error ();
631 if (fminl (fmaxl(x,y),y) != y)
632 link_error ();
634 if (fminl (fmaxl(y,x),y) != y)
635 link_error ();
637 if (fminl (x,fmaxl(x,y)) != x)
638 link_error ();
640 if (fminl (x,fmaxl(y,x)) != x)
641 link_error ();
643 if (fmaxl (fminl(x,y),y) != y)
644 link_error ();
646 if (fmaxl (fminl(y,x),y) != y)
647 link_error ();
649 if (fmaxl (x,fminl(x,y)) != x)
650 link_error ();
652 if (fmaxl (x,fminl(y,x)) != x)
653 link_error ();
655 if ((__complex__ long double) x != -(__complex__ long double) (-x))
656 link_error ();
658 if (x+(x-y)*1i != -(-x+(y-x)*1i))
659 link_error ();
661 if (x+(x-y)*1i != -(-x-(x-y)*1i))
662 link_error ();
664 if (ccosl(tanl(x)+sinl(y)*1i) != ccosl(-tanl(-x)+-sinl(-y)*1i))
665 link_error ();
667 if (ccosl(tanl(x)+sinl(x-y)*1i) != ccosl(-tanl(-x)-sinl(y-x)*1i))
668 link_error ();
670 if (-5+x*1i != -~(5+x*1i))
671 link_error ();
673 if (tanl(x)+tanl(y)*1i != -~(tanl(-x)+tanl(y)*1i))
674 link_error ();
677 void test3l(__complex__ long double x, __complex__ long double y, int i)
679 if (ccosl(x) != ccosl(-x))
680 link_error();
682 if (ccosl(ctanl(x)) != ccosl(ctanl(-x)))
683 link_error();
685 if (ctanl(x-y) != -ctanl(y-x))
686 link_error();
688 if (ccosl(x/y) != ccosl(-x/y))
689 link_error();
691 if (ccosl(x/y) != ccosl(x/-y))
692 link_error();
694 if (ccosl(x/ctanl(y)) != ccosl(-x/ctanl(-y)))
695 link_error();
697 if (ccosl(x*y) != ccosl(-x*y))
698 link_error();
700 if (ccosl(x*y) != ccosl(x*-y))
701 link_error();
703 if (ccosl(ctanl(x)*y) != ccosl(ctanl(-x)*-y))
704 link_error();
706 if (ccosl(ctanl(x/y)) != ccosl(-ctanl(x/-y)))
707 link_error();
709 if (ccosl(i ? x : y) != ccosl(i ? -x : y))
710 link_error();
712 if (ccosl(i ? x : y) != ccosl(i ? x : -y))
713 link_error();
715 if (ccosl(i ? x : ctanl(y/x)) != ccosl(i ? -x : -ctanl(-y/x)))
716 link_error();
718 if (~x != -~-x)
719 link_error();
721 if (ccosl(~x) != ccosl(-~-x))
722 link_error();
724 if (ctanl(~(x-y)) != -ctanl(~(y-x)))
725 link_error();
727 if (ctanl(~(x/y)) != -ctanl(~(x/-y)))
728 link_error();
730 #ifdef HAVE_C99_RUNTIME
731 if (cargl(x) != atan2l(__imag__ x, __real__ x))
732 link_error ();
733 #endif
736 int main()
738 test1 (1.0);
739 test2 (1.0, 2.0);
741 test1f (1.0f);
742 test2f (1.0f, 2.0f);
744 test1l (1.0l);
745 test2l (1.0l, 2.0l);
747 return 0;