OSDN Git Service

2010-03-31 Martin Jambor <mjambor@suse.cz>
[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 DECLARE2(FUNC) \
19   extern float FUNC##f (float, float); \
20   extern double FUNC (double, double); \
21   extern long double FUNC##l (long double, long double)
22
23 DECLARE2(fmin);
24 DECLARE2(fmax);
25 DECLARE(fabs);
26 extern int pure(int) __attribute__ ((__pure__));
27
28 /* Test that FUNC(x,x) == x.  We cast to (long) so "!=" folds.  */
29 #define TEST_EQ(FUNC) do { \
30   if ((long)FUNC##f(xf,xf) != (long)xf) \
31     link_error(__LINE__); \
32   if ((long)FUNC(x,x) != (long)x) \
33     link_error(__LINE__); \
34   if ((long)FUNC##l(xl,xl) != (long)xl) \
35     link_error(__LINE__); \
36   } while (0)
37
38 /* Test that FUNC(purefn,purefn) == purefn.  We cast to (long) so "!=" folds.  */
39 #define TEST_EQ_PURE(FUNC) do { \
40   if ((long)FUNC##f(pure(i),pure(i)) != (long)FUNC##f(pure(i),pure(i))) \
41     link_error(__LINE__); \
42   if ((long)FUNC(pure(i),pure(i)) != (long)FUNC(pure(i),pure(i))) \
43     link_error(__LINE__); \
44   if ((long)FUNC##l(pure(i),pure(i)) != (long)FUNC##l(pure(i),pure(i))) \
45     link_error(__LINE__); \
46   } while (0)
47
48 /* Test that FIXFUNC(FUNC(int1,int2)) == (TYPE)FUNC(int1,int2),
49    i.e. FIXFUNC should be folded away and replaced with a cast.  */
50 #define TEST_FIXFUNC(FUNC,FIXFUNC,TYPE) do { \
51   if (FIXFUNC##f(FUNC##f(i,j)) != (TYPE)FUNC##f(i,j)) \
52     link_error(__LINE__); \
53   if (FIXFUNC(FUNC(i,j)) != (TYPE)FUNC(i,j)) \
54     link_error(__LINE__); \
55   if (FIXFUNC##l(FUNC##l(i,j)) != (TYPE)FUNC##l(i,j)) \
56     link_error(__LINE__); \
57   } while (0)
58
59 /* Test that FUNC(int1,int2) has an integer return type.  */
60 #define TEST_INT(FUNC) do { \
61   TEST_FIXFUNC(FUNC,__builtin_lround,long); \
62   TEST_FIXFUNC(FUNC,__builtin_llround,long long); \
63   TEST_FIXFUNC(FUNC,__builtin_lrint,long); \
64   TEST_FIXFUNC(FUNC,__builtin_llrint,long long); \
65   TEST_FIXFUNC(FUNC,__builtin_lceil,long); \
66   TEST_FIXFUNC(FUNC,__builtin_llceil,long long); \
67   TEST_FIXFUNC(FUNC,__builtin_lfloor,long); \
68   TEST_FIXFUNC(FUNC,__builtin_llfloor,long long); \
69   } while (0)
70
71 /* Test that (long)fabs(FUNC(fabs(x),fabs(y))) ==
72    (long)FUNC(fabs(x),fabs(y)).  We cast to (long) so "!=" folds.  */
73 #define TEST_NONNEG(FUNC) do { \
74   if ((long)fabsf(FUNC##f(fabsf(xf),fabsf(yf))) != (long)FUNC##f(fabsf(xf),fabsf(yf))) \
75     link_error(__LINE__); \
76   if ((long)fabs(FUNC(fabs(x),fabs(y))) != (long)FUNC(fabs(x),fabs(y))) \
77     link_error(__LINE__); \
78   if ((long)fabsl(FUNC##l(fabsl(xl),fabsl(yl))) != (long)FUNC##l(fabsl(xl),fabsl(yl))) \
79     link_error(__LINE__); \
80   } while (0)
81
82 /* Test that FUNC(NaN,x) == x.  We cast to (long) so "!=" folds.  Set
83    parameter SIGNAL to `s' for testing signaling NaN.  */
84 #define TEST_NAN(FUNC,SIGNAL) do { \
85   if ((long)FUNC##f(__builtin_nan##SIGNAL##f(""),xf) != (long)xf) \
86     link_error(__LINE__); \
87   if ((long)FUNC##f(xf,__builtin_nan##SIGNAL##f("")) != (long)xf) \
88     link_error(__LINE__); \
89   if ((long)FUNC(__builtin_nan##SIGNAL(""),x) != (long)x) \
90     link_error(__LINE__); \
91   if ((long)FUNC(x,__builtin_nan##SIGNAL("")) != (long)x) \
92     link_error(__LINE__); \
93   if ((long)FUNC##l(__builtin_nan##SIGNAL##l(""),xl) != (long)xl) \
94     link_error(__LINE__); \
95   if ((long)FUNC##l(xl,__builtin_nan##SIGNAL##l("")) != (long)xl) \
96     link_error(__LINE__); \
97   } while (0)
98
99 void __attribute__ ((__noinline__))
100      foo (float xf, double x, long double xl,
101           float yf, double y, long double yl,
102           int i, int j)
103 {
104   TEST_EQ(fmin);
105   TEST_EQ(fmax);
106
107 #ifdef __OPTIMIZE__
108   TEST_EQ_PURE(fmin);
109   TEST_EQ_PURE(fmax);
110 #endif
111
112   TEST_INT(fmin);
113   TEST_INT(fmax);
114   
115   TEST_NONNEG(fmin);
116   TEST_NONNEG(fmax);
117
118   TEST_NAN(fmin,);
119   TEST_NAN(fmax,);
120   TEST_NAN(fmin,s);
121   TEST_NAN(fmax,s);
122 }
123
124 int main()
125 {
126   foo (1,1,1,1,1,1,1,1);
127   return 0;
128 }