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. */
9 /* { dg-options "-O2 -ffast-math" } */
10 /* { dg-options "-O2 -ffast-math -mmacosx-version-min=10.3" { target powerpc-*-darwin* } } */
11 /* { dg-options "-O2 -ffast-math -std=c99" { target *-*-solaris2* } } */
13 #include "builtins-config.h"
15 extern double cos (double);
16 extern double sin (double);
17 extern double tan (double);
18 extern double fabs (double);
19 extern double atan2 (double, double);
20 extern double copysign (double, double);
21 extern double fmin (double, double);
22 extern double fmax (double, double);
23 extern double hypot (double, double);
24 extern double pure (double) __attribute__ ((__pure__));
25 extern double carg (__complex__ double);
26 extern __complex__ double ccos (__complex__ double);
27 extern __complex__ double ctan (__complex__ double);
28 extern float cosf (float);
29 extern float sinf (float);
30 extern float tanf (float);
31 extern float fabsf (float);
32 extern float atan2f (float, float);
33 extern float copysignf (float, float);
34 extern float fminf (float, float);
35 extern float fmaxf (float, float);
36 extern float hypotf (float, float);
37 extern float puref (float) __attribute__ ((__pure__));
38 extern float cargf (__complex__ float);
39 extern __complex__ float ccosf (__complex__ float);
40 extern __complex__ float ctanf (__complex__ float);
41 extern long double cosl (long double);
42 extern long double sinl (long double);
43 extern long double tanl (long double);
44 extern long double fabsl (long double);
45 extern long double atan2l (long double, long double);
46 extern long double copysignl (long double, long double);
47 extern long double fminl (long double, long double);
48 extern long double fmaxl (long double, long double);
49 extern long double hypotl (long double, long double);
50 extern long double purel (long double) __attribute__ ((__pure__));
51 extern long double cargl (__complex__ long double);
52 extern __complex__ long double ccosl (__complex__ long double);
53 extern __complex__ long double ctanl (__complex__ long double);
55 extern void link_error(void);
59 if (cos(x) != cos(-x))
62 if (cos(x) != cos(fabs(x)))
65 if (cos(x) != cos(-fabs(x)))
68 if (cos(tan(x)) != cos(tan(-fabs(x))))
71 if (sin(x)/cos(x) != tan(x))
74 if (cos(x)/sin(x) != 1.0/tan(x))
77 if (tan(x)*cos(x) != sin(x))
80 if (cos(x)*tan(x) != sin(x))
83 if (sin(x)/tan(x) != cos(x))
86 if (tan(x)/sin(x) != 1.0/cos(x))
90 void test2(double x, double y)
92 if (-tan(x-y) != tan(y-x))
95 if (-sin(x-y) != sin(y-x))
98 if (cos(-x*y) != cos(x*y))
101 if (cos(x*-y) != cos(x*y))
104 if (cos(-x/y) != cos(x/y))
107 if (cos(x/-y) != cos(x/y))
110 if (cos(-fabs(tan(x/-y))) != cos(tan(x/y)))
113 if (cos(y<10 ? -x : y) != cos(y<10 ? x : y))
116 if (cos(y<10 ? x : -y) != cos(y<10 ? x : y))
119 if (cos(y<10 ? -fabs(x) : tan(x<20 ? -x : -fabs(y)))
120 != cos(y<10 ? x : tan(x<20 ? x : y)))
123 if (cos((y*=3, -x)) != cos((y*=3,x)))
126 if (cos((y*=2, -fabs(tan(x/-y)))) != cos((y*=2,tan(x/y))))
129 if (cos(copysign(x,y)) != cos(x))
132 if (cos(copysign(-fabs(x),y*=2)) != cos((y*=2,x)))
135 if (hypot (x, 0) != fabs(x))
138 if (hypot (0, x) != fabs(x))
141 if (hypot (x, x) != fabs(x) * __builtin_sqrt(2))
144 if (hypot (-x, y) != hypot (x, y))
147 if (hypot (x, -y) != hypot (x, y))
150 if (hypot (-x, -y) != hypot (x, y))
153 if (hypot (fabs(x), y) != hypot (x, y))
156 if (hypot (x, fabs(y)) != hypot (x, y))
159 if (hypot (fabs(x), fabs(y)) != hypot (x, y))
162 if (hypot (-fabs(-x), -fabs(fabs(fabs(-y)))) != hypot (x, y))
165 if (hypot (-x, 0) != fabs(x))
168 if (hypot (-x, x) != fabs(x) * __builtin_sqrt(2))
171 if (hypot (pure(x), -pure(x)) != fabs(pure(x)) * __builtin_sqrt(2))
174 if (hypot (tan(-x), tan(-fabs(y))) != hypot (tan(x), tan(y)))
177 if (fmin (fmax(x,y),y) != y)
180 if (fmin (fmax(y,x),y) != y)
183 if (fmin (x,fmax(x,y)) != x)
186 if (fmin (x,fmax(y,x)) != x)
189 if (fmax (fmin(x,y),y) != y)
192 if (fmax (fmin(y,x),y) != y)
195 if (fmax (x,fmin(x,y)) != x)
198 if (fmax (x,fmin(y,x)) != x)
201 if ((__complex__ double) x != -(__complex__ double) (-x))
204 if (x*1i != -(-x*1i))
207 if (x+(x-y)*1i != -(-x+(y-x)*1i))
210 if (x+(x-y)*1i != -(-x-(x-y)*1i))
213 if (ccos(tan(x)+sin(y)*1i) != ccos(-tan(-x)+-sin(-y)*1i))
216 if (ccos(tan(x)+sin(x-y)*1i) != ccos(-tan(-x)-sin(y-x)*1i))
219 if (-5+x*1i != -~(5+x*1i))
222 if (tan(x)+tan(y)*1i != -~(tan(-x)+tan(y)*1i))
226 void test3(__complex__ double x, __complex__ double y, int i)
228 if (carg(x) != atan2(__imag__ x, __real__ x))
231 if (ccos(x) != ccos(-x))
234 if (ccos(ctan(x)) != ccos(ctan(-x)))
237 if (ctan(x-y) != -ctan(y-x))
240 if (ccos(x/y) != ccos(-x/y))
243 if (ccos(x/y) != ccos(x/-y))
246 if (ccos(x/ctan(y)) != ccos(-x/ctan(-y)))
249 if (ccos(x*y) != ccos(-x*y))
252 if (ccos(x*y) != ccos(x*-y))
255 if (ccos(ctan(x)*y) != ccos(ctan(-x)*-y))
258 if (ccos(ctan(x/y)) != ccos(-ctan(x/-y)))
261 if (ccos(i ? x : y) != ccos(i ? -x : y))
264 if (ccos(i ? x : y) != ccos(i ? x : -y))
267 if (ccos(i ? x : ctan(y/x)) != ccos(i ? -x : -ctan(-y/x)))
273 if (ccos(~x) != ccos(-~-x))
276 if (ctan(~(x-y)) != -ctan(~(y-x)))
279 if (ctan(~(x/y)) != -ctan(~(x/-y)))
285 if (cosf(x) != cosf(-x))
288 if (cosf(x) != cosf(fabsf(x)))
291 if (cosf(x) != cosf(-fabsf(x)))
294 if (cosf(tanf(x)) != cosf(tanf(-fabsf(x))))
297 #ifdef HAVE_C99_RUNTIME
298 if (sinf(x)/cosf(x) != tanf(x))
301 if (cosf(x)/sinf(x) != 1.0f/tanf(x))
304 if (tanf(x)*cosf(x) != sinf(x))
307 if (cosf(x)*tanf(x) != sinf(x))
310 if (sinf(x)/tanf(x) != cosf(x))
313 if (tanf(x)/sinf(x) != 1.0f/cosf(x))
318 void test2f(float x, float y)
320 if (-tanf(x-y) != tanf(y-x))
323 if (-sinf(x-y) != sinf(y-x))
326 if (cosf(-x*y) != cosf(x*y))
329 if (cosf(x*-y) != cosf(x*y))
332 if (cosf(-x/y) != cosf(x/y))
335 if (cosf(x/-y) != cosf(x/y))
338 if (cosf(-fabsf(tanf(x/-y))) != cosf(tanf(x/y)))
341 if (cosf(y<10 ? -x : y) != cosf(y<10 ? x : y))
344 if (cosf(y<10 ? x : -y) != cosf(y<10 ? x : y))
347 if (cosf(y<10 ? -fabsf(x) : tanf(x<20 ? -x : -fabsf(y)))
348 != cosf(y<10 ? x : tanf(x<20 ? x : y)))
351 if (cosf((y*=3, -x)) != cosf((y*=3,x)))
354 if (cosf((y*=2, -fabsf(tanf(x/-y)))) != cosf((y*=2,tanf(x/y))))
357 if (cosf(copysignf(x,y)) != cosf(x))
360 if (cosf(copysignf(-fabsf(x),y*=2)) != cosf((y*=2,x)))
363 if (hypotf (x, 0) != fabsf(x))
366 if (hypotf (0, x) != fabsf(x))
369 if (hypotf (x, x) != fabsf(x) * __builtin_sqrtf(2))
372 if (hypotf (-x, y) != hypotf (x, y))
375 if (hypotf (x, -y) != hypotf (x, y))
378 if (hypotf (-x, -y) != hypotf (x, y))
381 if (hypotf (fabsf(x), y) != hypotf (x, y))
384 if (hypotf (x, fabsf(y)) != hypotf (x, y))
387 if (hypotf (fabsf(x), fabsf(y)) != hypotf (x, y))
390 if (hypotf (-fabsf(-x), -fabsf(fabsf(fabsf(-y)))) != hypotf (x, y))
393 if (hypotf (-x, 0) != fabsf(x))
396 if (hypotf (-x, x) != fabsf(x) * __builtin_sqrtf(2))
399 if (hypotf (puref(x), -puref(x)) != fabsf(puref(x)) * __builtin_sqrtf(2))
402 if (hypotf (tanf(-x), tanf(-fabsf(y))) != hypotf (tanf(x), tanf(y)))
405 if (fminf (fmaxf(x,y),y) != y)
408 if (fminf (fmaxf(y,x),y) != y)
411 if (fminf (x,fmaxf(x,y)) != x)
414 if (fminf (x,fmaxf(y,x)) != x)
417 if (fmaxf (fminf(x,y),y) != y)
420 if (fmaxf (fminf(y,x),y) != y)
423 if (fmaxf (x,fminf(x,y)) != x)
426 if (fmaxf (x,fminf(y,x)) != x)
429 if ((__complex__ float) x != -(__complex__ float) (-x))
432 if (x+(x-y)*1i != -(-x+(y-x)*1i))
435 if (x+(x-y)*1i != -(-x-(x-y)*1i))
438 if (ccosf(tanf(x)+sinf(y)*1i) != ccosf(-tanf(-x)+-sinf(-y)*1i))
441 if (ccosf(tanf(x)+sinf(x-y)*1i) != ccosf(-tanf(-x)-sinf(y-x)*1i))
444 if (-5+x*1i != -~(5+x*1i))
447 if (tanf(x)+tanf(y)*1i != -~(tanf(-x)+tanf(y)*1i))
451 void test3f(__complex__ float x, __complex__ float y, int i)
453 if (cargf(x) != atan2f(__imag__ x, __real__ x))
456 if (ccosf(x) != ccosf(-x))
459 if (ccosf(ctanf(x)) != ccosf(ctanf(-x)))
462 if (ctanf(x-y) != -ctanf(y-x))
465 if (ccosf(x/y) != ccosf(-x/y))
468 if (ccosf(x/y) != ccosf(x/-y))
471 if (ccosf(x/ctanf(y)) != ccosf(-x/ctanf(-y)))
474 if (ccosf(x*y) != ccosf(-x*y))
477 if (ccosf(x*y) != ccosf(x*-y))
480 if (ccosf(ctanf(x)*y) != ccosf(ctanf(-x)*-y))
483 if (ccosf(ctanf(x/y)) != ccosf(-ctanf(x/-y)))
486 if (ccosf(i ? x : y) != ccosf(i ? -x : y))
489 if (ccosf(i ? x : y) != ccosf(i ? x : -y))
492 if (ccosf(i ? x : ctanf(y/x)) != ccosf(i ? -x : -ctanf(-y/x)))
498 if (ccosf(~x) != ccosf(-~-x))
501 if (ctanf(~(x-y)) != -ctanf(~(y-x)))
504 if (ctanf(~(x/y)) != -ctanf(~(x/-y)))
508 void test1l(long double x)
510 if (cosl(x) != cosl(-x))
513 if (cosl(x) != cosl(fabsl(x)))
516 if (cosl(x) != cosl(-fabsl(x)))
519 if (cosl(tanl(x)) != cosl(tanl(-fabsl(x))))
522 #ifdef HAVE_C99_RUNTIME
523 if (sinl(x)/cosl(x) != tanl(x))
526 if (cosl(x)/sinl(x) != 1.0l/tanl(x))
529 if (tanl(x)*cosl(x) != sinl(x))
532 if (cosl(x)*tanl(x) != sinl(x))
535 if (sinl(x)/tanl(x) != cosl(x))
538 if (tanl(x)/sinl(x) != 1.0l/cosl(x))
543 void test2l(long double x, long double y)
545 if (-tanl(x-y) != tanl(y-x))
548 if (-sinl(x-y) != sinl(y-x))
551 if (cosl(-x*y) != cosl(x*y))
554 if (cosl(x*-y) != cosl(x*y))
557 if (cosl(-x/y) != cosl(x/y))
560 if (cosl(x/-y) != cosl(x/y))
563 if (cosl(-fabsl(tanl(x/-y))) != cosl(tanl(x/y)))
566 if (cosl(y<10 ? -x : y) != cosl(y<10 ? x : y))
569 if (cosl(y<10 ? x : -y) != cosl(y<10 ? x : y))
572 if (cosl(y<10 ? -fabsl(x) : tanl(x<20 ? -x : -fabsl(y)))
573 != cosl(y<10 ? x : tanl(x<20 ? x : y)))
576 if (cosl((y*=3, -x)) != cosl((y*=3,x)))
579 if (cosl((y*=2, -fabsl(tanl(x/-y)))) != cosl((y*=2,tanl(x/y))))
582 if (cosl(copysignl(x,y)) != cosl(x))
585 if (cosl(copysignl(-fabsl(x),y*=2)) != cosl((y*=2,x)))
588 if (hypotl (x, 0) != fabsl(x))
591 if (hypotl (0, x) != fabsl(x))
594 if (hypotl (x, x) != fabsl(x) * __builtin_sqrtl(2))
597 if (hypotl (-x, y) != hypotl (x, y))
600 if (hypotl (x, -y) != hypotl (x, y))
603 if (hypotl (-x, -y) != hypotl (x, y))
606 if (hypotl (fabsl(x), y) != hypotl (x, y))
609 if (hypotl (x, fabsl(y)) != hypotl (x, y))
612 if (hypotl (fabsl(x), fabsl(y)) != hypotl (x, y))
615 if (hypotl (-fabsl(-x), -fabsl(fabsl(fabsl(-y)))) != hypotl (x, y))
618 if (hypotl (-x, 0) != fabsl(x))
621 if (hypotl (-x, x) != fabsl(x) * __builtin_sqrtl(2))
624 if (hypotl (purel(x), -purel(x)) != fabsl(purel(x)) * __builtin_sqrtl(2))
627 if (hypotl (tanl(-x), tanl(-fabsl(y))) != hypotl (tanl(x), tanl(y)))
630 if (fminl (fmaxl(x,y),y) != y)
633 if (fminl (fmaxl(y,x),y) != y)
636 if (fminl (x,fmaxl(x,y)) != x)
639 if (fminl (x,fmaxl(y,x)) != x)
642 if (fmaxl (fminl(x,y),y) != y)
645 if (fmaxl (fminl(y,x),y) != y)
648 if (fmaxl (x,fminl(x,y)) != x)
651 if (fmaxl (x,fminl(y,x)) != x)
654 if ((__complex__ long double) x != -(__complex__ long double) (-x))
657 if (x+(x-y)*1i != -(-x+(y-x)*1i))
660 if (x+(x-y)*1i != -(-x-(x-y)*1i))
663 if (ccosl(tanl(x)+sinl(y)*1i) != ccosl(-tanl(-x)+-sinl(-y)*1i))
666 if (ccosl(tanl(x)+sinl(x-y)*1i) != ccosl(-tanl(-x)-sinl(y-x)*1i))
669 if (-5+x*1i != -~(5+x*1i))
672 if (tanl(x)+tanl(y)*1i != -~(tanl(-x)+tanl(y)*1i))
676 void test3l(__complex__ long double x, __complex__ long double y, int i)
678 if (cargl(x) != atan2l(__imag__ x, __real__ x))
681 if (ccosl(x) != ccosl(-x))
684 if (ccosl(ctanl(x)) != ccosl(ctanl(-x)))
687 if (ctanl(x-y) != -ctanl(y-x))
690 if (ccosl(x/y) != ccosl(-x/y))
693 if (ccosl(x/y) != ccosl(x/-y))
696 if (ccosl(x/ctanl(y)) != ccosl(-x/ctanl(-y)))
699 if (ccosl(x*y) != ccosl(-x*y))
702 if (ccosl(x*y) != ccosl(x*-y))
705 if (ccosl(ctanl(x)*y) != ccosl(ctanl(-x)*-y))
708 if (ccosl(ctanl(x/y)) != ccosl(-ctanl(x/-y)))
711 if (ccosl(i ? x : y) != ccosl(i ? -x : y))
714 if (ccosl(i ? x : y) != ccosl(i ? x : -y))
717 if (ccosl(i ? x : ctanl(y/x)) != ccosl(i ? -x : -ctanl(-y/x)))
723 if (ccosl(~x) != ccosl(-~-x))
726 if (ctanl(~(x-y)) != -ctanl(~(y-x)))
729 if (ctanl(~(x/y)) != -ctanl(~(x/-y)))