1 /* Copyright (C) 2009  Free Software Foundation.
2 
3    Verify that folding of complex mul and div work correctly.
4 
5    Origin: Kaveh R. Ghazi,  August 13, 2009.  */
6 
7 /* { dg-do run } */
8 /* C6X fails due to -freciprocal-math default.  */
9 /* { dg-skip-if "" { tic6x-*-* } } */
10 /* { dg-require-effective-target inf } */
11 /* { dg-add-options ieee } */
12 /* { dg-require-effective-target large_double } */
13 
14 extern void link_error(int);
15 
16 /* Evaluate this expression at compile-time.  */
17 #define COMPILETIME_TESTIT(TYPE,X,OP,Y,RES) do { \
18   if ((_Complex TYPE)(X) OP (_Complex TYPE)(Y) != (_Complex TYPE)(RES)) \
19     link_error(__LINE__); \
20 } while (0)
21 
22 /* Use this error function for cases which only evaluate at
23    compile-time when optimizing.  */
24 #ifdef __OPTIMIZE__
25 # define ERROR_FUNC(X) link_error(X)
26 #else
27 # define ERROR_FUNC(X) __builtin_abort()
28 #endif
29 
30 /* Evaluate this expression at compile-time using static initializers.  */
31 #define STATICINIT_TESTIT(TYPE,X,OP,Y,RES) do { \
32   static const _Complex TYPE foo = (_Complex TYPE)(X) OP (_Complex TYPE)(Y); \
33   if (foo != (_Complex TYPE)(RES)) \
34     ERROR_FUNC (__LINE__); \
35 } while (0)
36 
37 /* Evaluate this expression at runtime.  */
38 #define RUNTIME_TESTIT(TYPE,X,OP,Y,RES) do { \
39   volatile _Complex TYPE foo; \
40   foo = (_Complex TYPE)(X); \
41   foo OP##= (_Complex TYPE)(Y); \
42   if (foo != (_Complex TYPE)(RES)) \
43     __builtin_abort(); \
44 } while (0)
45 
46 /* Evaluate this expression at compile-time and runtime.  */
47 #define TESTIT(TYPE,X,OP,Y,RES) do { \
48   STATICINIT_TESTIT(TYPE,X,OP,Y,RES); \
49   COMPILETIME_TESTIT(TYPE,X,OP,Y,RES); \
50   RUNTIME_TESTIT(TYPE,X,OP,Y,RES); \
51 } while (0)
52 
53 /* Either the real or imaginary parts should be infinity.  */
54 #define TEST_ONE_PART_INF(VAL) do { \
55   static const _Complex double foo = (VAL); \
56   if (! __builtin_isinf(__real foo) && ! __builtin_isinf(__imag foo)) \
57     ERROR_FUNC (__LINE__); \
58   if (! __builtin_isinf(__real (VAL)) && ! __builtin_isinf(__imag (VAL))) \
59     __builtin_abort(); \
60 } while (0)
61 
main()62 int main()
63 {
64   /* Test some regular finite values.  */
65   TESTIT (double, 3.+4.i, *, 2, 6+8i);
66   TESTIT (double, 3.+4.i, /, 2, 1.5+2i);
67   TESTIT (int, 3+4i, *, 2, 6+8i);
68   TESTIT (int, 3+4i, /, 2, 1+2i);
69 
70   TESTIT (double, 3.+4.i, *, 2+5i, -14+23i);
71   TESTIT (double, 3.+4.i, /, 5i, .8-.6i);
72   TESTIT (int, 3+4i, *, 2+5i, -14+23i);
73   TESTIT (int, 30+40i, /, 5i, 8-6i);
74   TESTIT (int, 14+6i, /, 7+3i, 2);
75   TESTIT (int, 8+24i, /, 4+12i, 2);
76 
77   /* Test that we don't overflow.  */
78   TESTIT (double,
79 	  (__DBL_MAX__ * 0.5 + __DBL_MAX__ * 0.5i),
80 	  /,
81 	  (__DBL_MAX__ * 0.25 + __DBL_MAX__ * 0.25i),
82 	  2);
83 
84   /* Test for accuracy.  */
85   COMPILETIME_TESTIT (double,
86 		      (1 + __DBL_EPSILON__ + 1i),
87 		      *,
88 		      (1 - __DBL_EPSILON__ + 1i),
89 		      -4.93038065763132378382330353301741393545754021943139377981e-32+2i);
90 
91   /* This becomes (NaN + iInf).  */
92 #define VAL1 ((_Complex double)__builtin_inf() * 1i)
93 
94   /* Test some C99 Annex G special cases.  */
95   TEST_ONE_PART_INF ((VAL1) * (VAL1));
96   TEST_ONE_PART_INF ((_Complex double)1 / (_Complex double)0);
97   TEST_ONE_PART_INF ((VAL1) / (_Complex double)1);
98 
99   RUNTIME_TESTIT (double, 1, /, VAL1, 0);
100   STATICINIT_TESTIT (double, 1, /, VAL1, 0);
101 
102   return 0;
103 }
104