OSDN Git Service

* builtins.c (fold_builtin_modf): New.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.dg / torture / builtin-minmax-1.c
1 /* Copyright (C) 2006  Free Software Foundation.
2
3    Verify that built-in math function folding of fmin/fmax is
4    correctly performed by the compiler.
5
6    Origin: Kaveh R. Ghazi,  November 13, 2006.  */
7
8 /* { dg-do link } */
9 /* { dg-options "-fno-math-errno" } */
10
11 /* All references to link_error should go away at compile-time.  */
12 extern void link_error(int);
13
14 #define DECLARE(FUNC) \
15   extern float FUNC##f (float); \
16   extern double FUNC (double); \
17   extern long double FUNC##l (long double)
18 #define DECLARE_L(FUNC) \
19   extern long FUNC##f (float); \
20   extern long FUNC (double); \
21   extern long FUNC##l (long double)
22 #define DECLARE2(FUNC) \
23   extern float FUNC##f (float, float); \
24   extern double FUNC (double, double); \
25   extern long double FUNC##l (long double, long double)
26
27 DECLARE2(fmin);
28 DECLARE2(fmax);
29 DECLARE_L(lround);
30 DECLARE_L(lrint);
31 DECLARE(sqrt);
32 DECLARE(fabs);
33 extern int pure(int) __attribute__ ((__pure__));
34
35 /* Test that FUNC(x,x) == x.  We cast to (long) so "!=" folds.  */
36 #define TEST_EQ(FUNC) do { \
37   if ((long)FUNC##f(xf,xf) != (long)xf) \
38     link_error(__LINE__); \
39   if ((long)FUNC(x,x) != (long)x) \
40     link_error(__LINE__); \
41   if ((long)FUNC##l(xl,xl) != (long)xl) \
42     link_error(__LINE__); \
43   } while (0)
44
45 /* Test that FUNC(purefn,purefn) == purefn.  We cast to (long) so "!=" folds.  */
46 #define TEST_EQ_PURE(FUNC) do { \
47   if ((long)FUNC##f(pure(i),pure(i)) != (long)FUNC##f(pure(i),pure(i))) \
48     link_error(__LINE__); \
49   if ((long)FUNC(pure(i),pure(i)) != (long)FUNC(pure(i),pure(i))) \
50     link_error(__LINE__); \
51   if ((long)FUNC##l(pure(i),pure(i)) != (long)FUNC##l(pure(i),pure(i))) \
52     link_error(__LINE__); \
53   } while (0)
54
55 /* Test that lround(FUNC(int,int)) == lrint(FUNC(int,int)), i.e. both
56    lround() and lrint() should be folded away.  */
57 #define TEST_NONNEG(FUNC) do { \
58   if (lroundf(FUNC##f(i,j)) != lrintf(FUNC##f(i,j))) \
59     link_error(__LINE__); \
60   if (lround(FUNC(i,j)) != lrint(FUNC(i,j))) \
61     link_error(__LINE__); \
62   if (lroundl(FUNC##l(i,j)) != lrintl(FUNC##l(i,j))) \
63     link_error(__LINE__); \
64   } while (0)
65
66 /* Test that (long)fabs(FUNC(fabs(x),fabs(y))) ==
67    (long)FUNC(fabs(x),fabs(y)).  We cast to (long) so "!=" folds.  */
68 #define TEST_INT(FUNC) do { \
69   if ((long)fabsf(FUNC##f(fabsf(xf),fabsf(yf))) != (long)FUNC##f(fabsf(xf),fabsf(yf))) \
70     link_error(__LINE__); \
71   if ((long)fabs(FUNC(fabs(x),fabs(y))) != (long)FUNC(fabs(x),fabs(y))) \
72     link_error(__LINE__); \
73   if ((long)fabsl(FUNC##l(fabsl(xl),fabsl(yl))) != (long)FUNC##l(fabsl(xl),fabsl(yl))) \
74     link_error(__LINE__); \
75   } while (0)
76
77 /* Test that FUNC(NaN,x) == x.  We cast to (long) so "!=" folds.  Set
78    parameter SIGNAL to `s' for testing signaling NaN.  */
79 #define TEST_NAN(FUNC,SIGNAL) do { \
80   if ((long)FUNC##f(__builtin_nan##SIGNAL##f(""),xf) != (long)xf) \
81     link_error(__LINE__); \
82   if ((long)FUNC##f(xf,__builtin_nan##SIGNAL##f("")) != (long)xf) \
83     link_error(__LINE__); \
84   if ((long)FUNC(__builtin_nan##SIGNAL(""),x) != (long)x) \
85     link_error(__LINE__); \
86   if ((long)FUNC(x,__builtin_nan##SIGNAL("")) != (long)x) \
87     link_error(__LINE__); \
88   if ((long)FUNC##l(__builtin_nan##SIGNAL##l(""),xl) != (long)xl) \
89     link_error(__LINE__); \
90   if ((long)FUNC##l(xl,__builtin_nan##SIGNAL##l("")) != (long)xl) \
91     link_error(__LINE__); \
92   } while (0)
93
94 void __attribute__ ((__noinline__))
95      foo (float xf, double x, long double xl,
96           float yf, double y, long double yl,
97           int i, int j)
98 {
99   TEST_EQ(fmin);
100   TEST_EQ(fmax);
101
102 #ifdef __OPTIMIZE__
103   TEST_EQ_PURE(fmin);
104   TEST_EQ_PURE(fmax);
105 #endif
106
107   TEST_INT(fmin);
108   TEST_INT(fmax);
109   
110   TEST_NONNEG(fmin);
111   TEST_NONNEG(fmax);
112
113   TEST_NAN(fmin,);
114   TEST_NAN(fmax,);
115   TEST_NAN(fmin,s);
116   TEST_NAN(fmax,s);
117 }
118
119 int main()
120 {
121   foo (1,1,1,1,1,1,1,1);
122   return 0;
123 }