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-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);
58 if (cos(x
) != cos(-x
))
61 if (cos(x
) != cos(fabs(x
)))
64 if (cos(x
) != cos(-fabs(x
)))
67 if (cos(tan(x
)) != cos(tan(-fabs(x
))))
70 if (sin(x
)/cos(x
) != tan(x
))
73 if (cos(x
)/sin(x
) != 1.0/tan(x
))
76 if (tan(x
)*cos(x
) != sin(x
))
79 if (cos(x
)*tan(x
) != sin(x
))
82 if (sin(x
)/tan(x
) != cos(x
))
85 if (tan(x
)/sin(x
) != 1.0/cos(x
))
89 void test2(double x
, double y
)
91 if (-tan(x
-y
) != tan(y
-x
))
94 if (-sin(x
-y
) != sin(y
-x
))
97 if (cos(-x
*y
) != cos(x
*y
))
100 if (cos(x
*-y
) != cos(x
*y
))
103 if (cos(-x
/y
) != cos(x
/y
))
106 if (cos(x
/-y
) != cos(x
/y
))
109 if (cos(-fabs(tan(x
/-y
))) != cos(tan(x
/y
)))
112 if (cos(y
<10 ? -x
: y
) != cos(y
<10 ? x
: y
))
115 if (cos(y
<10 ? x
: -y
) != cos(y
<10 ? x
: y
))
118 if (cos(y
<10 ? -fabs(x
) : tan(x
<20 ? -x
: -fabs(y
)))
119 != cos(y
<10 ? x
: tan(x
<20 ? x
: y
)))
122 if (cos((y
*=3, -x
)) != cos((y
*=3,x
)))
125 if (cos(-fabs(tan(x
/-y
))) != cos(tan(x
/y
)))
128 if (cos(copysign(x
,y
)) != cos(x
))
131 if (cos(copysign(-fabs(x
),y
*=2)) != cos((y
*=2,x
)))
134 if (hypot (x
, 0) != fabs(x
))
137 if (hypot (0, x
) != fabs(x
))
140 if (hypot (x
, x
) != fabs(x
) * __builtin_sqrt(2))
143 if (hypot (-x
, y
) != hypot (x
, y
))
146 if (hypot (x
, -y
) != hypot (x
, y
))
149 if (hypot (-x
, -y
) != hypot (x
, y
))
152 if (hypot (fabs(x
), y
) != hypot (x
, y
))
155 if (hypot (x
, fabs(y
)) != hypot (x
, y
))
158 if (hypot (fabs(x
), fabs(y
)) != hypot (x
, y
))
161 if (hypot (-fabs(-x
), -fabs(fabs(fabs(-y
)))) != hypot (x
, y
))
164 if (hypot (-x
, 0) != fabs(x
))
167 if (hypot (-x
, x
) != fabs(x
) * __builtin_sqrt(2))
170 if (hypot (pure(x
), -pure(x
)) != fabs(pure(x
)) * __builtin_sqrt(2))
173 if (hypot (tan(-x
), tan(-fabs(y
))) != hypot (tan(x
), tan(y
)))
176 if (fmin (fmax(x
,y
),y
) != y
)
179 if (fmin (fmax(y
,x
),y
) != y
)
182 if (fmin (x
,fmax(x
,y
)) != x
)
185 if (fmin (x
,fmax(y
,x
)) != x
)
188 if (fmax (fmin(x
,y
),y
) != y
)
191 if (fmax (fmin(y
,x
),y
) != y
)
194 if (fmax (x
,fmin(x
,y
)) != x
)
197 if (fmax (x
,fmin(y
,x
)) != x
)
200 if ((__complex__
double) x
!= -(__complex__
double) (-x
))
203 if (x
*1i
!= -(-x
*1i
))
206 if (x
+(x
-y
)*1i
!= -(-x
+(y
-x
)*1i
))
209 if (x
+(x
-y
)*1i
!= -(-x
-(x
-y
)*1i
))
212 if (ccos(tan(x
)+sin(y
)*1i
) != ccos(-tan(-x
)+-sin(-y
)*1i
))
215 if (ccos(tan(x
)+sin(x
-y
)*1i
) != ccos(-tan(-x
)-sin(y
-x
)*1i
))
218 if (-5+x
*1i
!= -~(5+x
*1i
))
221 if (tan(x
)+tan(y
)*1i
!= -~(tan(-x
)+tan(y
)*1i
))
225 void test3(__complex__
double x
, __complex__
double y
, int i
)
227 if (carg(x
) != atan2(__imag__ x
, __real__ x
))
230 if (ccos(x
) != ccos(-x
))
233 if (ccos(ctan(x
)) != ccos(ctan(-x
)))
236 if (ctan(x
-y
) != -ctan(y
-x
))
239 if (ccos(x
/y
) != ccos(-x
/y
))
242 if (ccos(x
/y
) != ccos(x
/-y
))
245 if (ccos(x
/ctan(y
)) != ccos(-x
/ctan(-y
)))
248 if (ccos(x
*y
) != ccos(-x
*y
))
251 if (ccos(x
*y
) != ccos(x
*-y
))
254 if (ccos(ctan(x
)*y
) != ccos(ctan(-x
)*-y
))
257 if (ccos(ctan(x
/y
)) != ccos(-ctan(x
/-y
)))
260 if (ccos(i
? x
: y
) != ccos(i
? -x
: y
))
263 if (ccos(i
? x
: y
) != ccos(i
? x
: -y
))
266 if (ccos(i
? x
: ctan(y
/x
)) != ccos(i
? -x
: -ctan(-y
/x
)))
272 if (ccos(~x
) != ccos(-~-x
))
275 if (ctan(~(x
-y
)) != -ctan(~(y
-x
)))
278 if (ctan(~(x
/y
)) != -ctan(~(x
/-y
)))
284 if (cosf(x
) != cosf(-x
))
287 if (cosf(x
) != cosf(fabsf(x
)))
290 if (cosf(x
) != cosf(-fabsf(x
)))
293 if (cosf(tanf(x
)) != cosf(tanf(-fabsf(x
))))
296 #ifdef HAVE_C99_RUNTIME
297 if (sinf(x
)/cosf(x
) != tanf(x
))
300 if (cosf(x
)/sinf(x
) != 1.0f
/tanf(x
))
303 if (tanf(x
)*cosf(x
) != sinf(x
))
306 if (cosf(x
)*tanf(x
) != sinf(x
))
309 if (sinf(x
)/tanf(x
) != cosf(x
))
312 if (tanf(x
)/sinf(x
) != 1.0f
/cosf(x
))
317 void test2f(float x
, float y
)
319 if (-tanf(x
-y
) != tanf(y
-x
))
322 if (-sinf(x
-y
) != sinf(y
-x
))
325 if (cosf(-x
*y
) != cosf(x
*y
))
328 if (cosf(x
*-y
) != cosf(x
*y
))
331 if (cosf(-x
/y
) != cosf(x
/y
))
334 if (cosf(x
/-y
) != cosf(x
/y
))
337 if (cosf(-fabsf(tanf(x
/-y
))) != cosf(tanf(x
/y
)))
340 if (cosf(y
<10 ? -x
: y
) != cosf(y
<10 ? x
: y
))
343 if (cosf(y
<10 ? x
: -y
) != cosf(y
<10 ? x
: y
))
346 if (cosf(y
<10 ? -fabsf(x
) : tanf(x
<20 ? -x
: -fabsf(y
)))
347 != cosf(y
<10 ? x
: tanf(x
<20 ? x
: y
)))
350 if (cosf((y
*=3, -x
)) != cosf((y
*=3,x
)))
353 if (cosf(-fabsf(tanf(x
/-y
))) != cosf(tanf(x
/y
)))
356 if (cosf(copysignf(x
,y
)) != cosf(x
))
359 if (cosf(copysignf(-fabsf(x
),y
*=2)) != cosf((y
*=2,x
)))
362 if (hypotf (x
, 0) != fabsf(x
))
365 if (hypotf (0, x
) != fabsf(x
))
368 if (hypotf (x
, x
) != fabsf(x
) * __builtin_sqrtf(2))
371 if (hypotf (-x
, y
) != hypotf (x
, y
))
374 if (hypotf (x
, -y
) != hypotf (x
, y
))
377 if (hypotf (-x
, -y
) != hypotf (x
, y
))
380 if (hypotf (fabsf(x
), y
) != hypotf (x
, y
))
383 if (hypotf (x
, fabsf(y
)) != hypotf (x
, y
))
386 if (hypotf (fabsf(x
), fabsf(y
)) != hypotf (x
, y
))
389 if (hypotf (-fabsf(-x
), -fabsf(fabsf(fabsf(-y
)))) != hypotf (x
, y
))
392 if (hypotf (-x
, 0) != fabsf(x
))
395 if (hypotf (-x
, x
) != fabsf(x
) * __builtin_sqrtf(2))
398 if (hypotf (puref(x
), -puref(x
)) != fabsf(puref(x
)) * __builtin_sqrtf(2))
401 if (hypotf (tanf(-x
), tanf(-fabsf(y
))) != hypotf (tanf(x
), tanf(y
)))
404 if (fminf (fmaxf(x
,y
),y
) != y
)
407 if (fminf (fmaxf(y
,x
),y
) != y
)
410 if (fminf (x
,fmaxf(x
,y
)) != x
)
413 if (fminf (x
,fmaxf(y
,x
)) != x
)
416 if (fmaxf (fminf(x
,y
),y
) != y
)
419 if (fmaxf (fminf(y
,x
),y
) != y
)
422 if (fmaxf (x
,fminf(x
,y
)) != x
)
425 if (fmaxf (x
,fminf(y
,x
)) != x
)
428 if ((__complex__
float) x
!= -(__complex__
float) (-x
))
431 if (x
+(x
-y
)*1i
!= -(-x
+(y
-x
)*1i
))
434 if (x
+(x
-y
)*1i
!= -(-x
-(x
-y
)*1i
))
437 if (ccosf(tanf(x
)+sinf(y
)*1i
) != ccosf(-tanf(-x
)+-sinf(-y
)*1i
))
440 if (ccosf(tanf(x
)+sinf(x
-y
)*1i
) != ccosf(-tanf(-x
)-sinf(y
-x
)*1i
))
443 if (-5+x
*1i
!= -~(5+x
*1i
))
446 if (tanf(x
)+tanf(y
)*1i
!= -~(tanf(-x
)+tanf(y
)*1i
))
450 void test3f(__complex__
float x
, __complex__
float y
, int i
)
452 if (ccosf(x
) != ccosf(-x
))
455 if (ccosf(ctanf(x
)) != ccosf(ctanf(-x
)))
458 if (ctanf(x
-y
) != -ctanf(y
-x
))
461 if (ccosf(x
/y
) != ccosf(-x
/y
))
464 if (ccosf(x
/y
) != ccosf(x
/-y
))
467 if (ccosf(x
/ctanf(y
)) != ccosf(-x
/ctanf(-y
)))
470 if (ccosf(x
*y
) != ccosf(-x
*y
))
473 if (ccosf(x
*y
) != ccosf(x
*-y
))
476 if (ccosf(ctanf(x
)*y
) != ccosf(ctanf(-x
)*-y
))
479 if (ccosf(ctanf(x
/y
)) != ccosf(-ctanf(x
/-y
)))
482 if (ccosf(i
? x
: y
) != ccosf(i
? -x
: y
))
485 if (ccosf(i
? x
: y
) != ccosf(i
? x
: -y
))
488 if (ccosf(i
? x
: ctanf(y
/x
)) != ccosf(i
? -x
: -ctanf(-y
/x
)))
494 if (ccosf(~x
) != ccosf(-~-x
))
497 if (ctanf(~(x
-y
)) != -ctanf(~(y
-x
)))
500 if (ctanf(~(x
/y
)) != -ctanf(~(x
/-y
)))
503 #ifdef HAVE_C99_RUNTIME
504 if (cargf(x
) != atan2f(__imag__ x
, __real__ x
))
509 void test1l(long double x
)
511 if (cosl(x
) != cosl(-x
))
514 if (cosl(x
) != cosl(fabsl(x
)))
517 if (cosl(x
) != cosl(-fabsl(x
)))
520 if (cosl(tanl(x
)) != cosl(tanl(-fabsl(x
))))
523 #ifdef HAVE_C99_RUNTIME
524 if (sinl(x
)/cosl(x
) != tanl(x
))
527 if (cosl(x
)/sinl(x
) != 1.0l/tanl(x
))
530 if (tanl(x
)*cosl(x
) != sinl(x
))
533 if (cosl(x
)*tanl(x
) != sinl(x
))
536 if (sinl(x
)/tanl(x
) != cosl(x
))
539 if (tanl(x
)/sinl(x
) != 1.0l/cosl(x
))
544 void test2l(long double x
, long double y
)
546 if (-tanl(x
-y
) != tanl(y
-x
))
549 if (-sinl(x
-y
) != sinl(y
-x
))
552 if (cosl(-x
*y
) != cosl(x
*y
))
555 if (cosl(x
*-y
) != cosl(x
*y
))
558 if (cosl(-x
/y
) != cosl(x
/y
))
561 if (cosl(x
/-y
) != cosl(x
/y
))
564 if (cosl(-fabsl(tanl(x
/-y
))) != cosl(tanl(x
/y
)))
567 if (cosl(y
<10 ? -x
: y
) != cosl(y
<10 ? x
: y
))
570 if (cosl(y
<10 ? x
: -y
) != cosl(y
<10 ? x
: y
))
573 if (cosl(y
<10 ? -fabsl(x
) : tanl(x
<20 ? -x
: -fabsl(y
)))
574 != cosl(y
<10 ? x
: tanl(x
<20 ? x
: y
)))
577 if (cosl((y
*=3, -x
)) != cosl((y
*=3,x
)))
580 if (cosl(-fabsl(tanl(x
/-y
))) != cosl(tanl(x
/y
)))
583 if (cosl(copysignl(x
,y
)) != cosl(x
))
586 if (cosl(copysignl(-fabsl(x
),y
*=2)) != cosl((y
*=2,x
)))
589 if (hypotl (x
, 0) != fabsl(x
))
592 if (hypotl (0, x
) != fabsl(x
))
595 if (hypotl (x
, x
) != fabsl(x
) * __builtin_sqrtl(2))
598 if (hypotl (-x
, y
) != hypotl (x
, y
))
601 if (hypotl (x
, -y
) != hypotl (x
, y
))
604 if (hypotl (-x
, -y
) != hypotl (x
, y
))
607 if (hypotl (fabsl(x
), y
) != hypotl (x
, y
))
610 if (hypotl (x
, fabsl(y
)) != hypotl (x
, y
))
613 if (hypotl (fabsl(x
), fabsl(y
)) != hypotl (x
, y
))
616 if (hypotl (-fabsl(-x
), -fabsl(fabsl(fabsl(-y
)))) != hypotl (x
, y
))
619 if (hypotl (-x
, 0) != fabsl(x
))
622 if (hypotl (-x
, x
) != fabsl(x
) * __builtin_sqrtl(2))
625 if (hypotl (purel(x
), -purel(x
)) != fabsl(purel(x
)) * __builtin_sqrtl(2))
628 if (hypotl (tanl(-x
), tanl(-fabsl(y
))) != hypotl (tanl(x
), tanl(y
)))
631 if (fminl (fmaxl(x
,y
),y
) != y
)
634 if (fminl (fmaxl(y
,x
),y
) != y
)
637 if (fminl (x
,fmaxl(x
,y
)) != x
)
640 if (fminl (x
,fmaxl(y
,x
)) != x
)
643 if (fmaxl (fminl(x
,y
),y
) != y
)
646 if (fmaxl (fminl(y
,x
),y
) != y
)
649 if (fmaxl (x
,fminl(x
,y
)) != x
)
652 if (fmaxl (x
,fminl(y
,x
)) != x
)
655 if ((__complex__
long double) x
!= -(__complex__
long double) (-x
))
658 if (x
+(x
-y
)*1i
!= -(-x
+(y
-x
)*1i
))
661 if (x
+(x
-y
)*1i
!= -(-x
-(x
-y
)*1i
))
664 if (ccosl(tanl(x
)+sinl(y
)*1i
) != ccosl(-tanl(-x
)+-sinl(-y
)*1i
))
667 if (ccosl(tanl(x
)+sinl(x
-y
)*1i
) != ccosl(-tanl(-x
)-sinl(y
-x
)*1i
))
670 if (-5+x
*1i
!= -~(5+x
*1i
))
673 if (tanl(x
)+tanl(y
)*1i
!= -~(tanl(-x
)+tanl(y
)*1i
))
677 void test3l(__complex__
long double x
, __complex__
long double y
, int i
)
679 if (ccosl(x
) != ccosl(-x
))
682 if (ccosl(ctanl(x
)) != ccosl(ctanl(-x
)))
685 if (ctanl(x
-y
) != -ctanl(y
-x
))
688 if (ccosl(x
/y
) != ccosl(-x
/y
))
691 if (ccosl(x
/y
) != ccosl(x
/-y
))
694 if (ccosl(x
/ctanl(y
)) != ccosl(-x
/ctanl(-y
)))
697 if (ccosl(x
*y
) != ccosl(-x
*y
))
700 if (ccosl(x
*y
) != ccosl(x
*-y
))
703 if (ccosl(ctanl(x
)*y
) != ccosl(ctanl(-x
)*-y
))
706 if (ccosl(ctanl(x
/y
)) != ccosl(-ctanl(x
/-y
)))
709 if (ccosl(i
? x
: y
) != ccosl(i
? -x
: y
))
712 if (ccosl(i
? x
: y
) != ccosl(i
? x
: -y
))
715 if (ccosl(i
? x
: ctanl(y
/x
)) != ccosl(i
? -x
: -ctanl(-y
/x
)))
721 if (ccosl(~x
) != ccosl(-~-x
))
724 if (ctanl(~(x
-y
)) != -ctanl(~(y
-x
)))
727 if (ctanl(~(x
/y
)) != -ctanl(~(x
/-y
)))
730 #ifdef HAVE_C99_RUNTIME
731 if (cargl(x
) != atan2l(__imag__ x
, __real__ x
))