1 /* Copyright (C) 2009 Free Software Foundation.
3 Verify that folding of complex mul and div work correctly.
5 Origin: Kaveh R. Ghazi, August 13, 2009. */
8 /* { dg-require-effective-target mpc_pow } */
9 /* { dg-add-options ieee } */
11 extern void link_error(int);
13 /* Evaluate this expression at compile-time. */
14 #define COMPILETIME_TESTIT(TYPE,X,OP,Y,RES) do { \
15 if ((_Complex TYPE)(X) OP (_Complex TYPE)(Y) != (_Complex TYPE)(RES)) \
16 link_error(__LINE__); \
19 /* Use this error function for cases which only evaluate at
20 compile-time when optimizing. */
22 # define ERROR_FUNC(X) link_error(X)
24 # define ERROR_FUNC(X) __builtin_abort()
27 /* Evaluate this expression at compile-time using static initializers. */
28 #define STATICINIT_TESTIT(TYPE,X,OP,Y,RES) do { \
29 static const _Complex TYPE foo = (_Complex TYPE)(X) OP (_Complex TYPE)(Y); \
30 if (foo != (_Complex TYPE)(RES)) \
31 ERROR_FUNC (__LINE__); \
34 /* Evaluate this expression at runtime. */
35 #define RUNTIME_TESTIT(TYPE,X,OP,Y,RES) do { \
36 volatile _Complex TYPE foo; \
37 foo = (_Complex TYPE)(X); \
38 foo OP##= (_Complex TYPE)(Y); \
39 if (foo != (_Complex TYPE)(RES)) \
43 /* Evaluate this expression at compile-time and runtime. */
44 #define TESTIT(TYPE,X,OP,Y,RES) do { \
45 STATICINIT_TESTIT(TYPE,X,OP,Y,RES); \
46 COMPILETIME_TESTIT(TYPE,X,OP,Y,RES); \
47 RUNTIME_TESTIT(TYPE,X,OP,Y,RES); \
50 /* Either the real or imaginary parts should be infinity. */
51 #define TEST_ONE_PART_INF(VAL) do { \
52 static const _Complex double foo = (VAL); \
53 if (! __builtin_isinf(__real foo) && ! __builtin_isinf(__imag foo)) \
54 ERROR_FUNC (__LINE__); \
55 if (! __builtin_isinf(__real (VAL)) && ! __builtin_isinf(__imag (VAL))) \
61 /* Test some regular finite values. */
62 TESTIT (double, 3.+4.i, *, 2, 6+8i);
63 TESTIT (double, 3.+4.i, /, 2, 1.5+2i);
64 TESTIT (int, 3+4i, *, 2, 6+8i);
65 TESTIT (int, 3+4i, /, 2, 1+2i);
67 TESTIT (double, 3.+4.i, *, 2+5i, -14+23i);
68 TESTIT (double, 3.+4.i, /, 5i, .8-.6i);
69 TESTIT (int, 3+4i, *, 2+5i, -14+23i);
70 TESTIT (int, 30+40i, /, 5i, 8-6i);
71 TESTIT (int, 14+6i, /, 7+3i, 2);
72 TESTIT (int, 8+24i, /, 4+12i, 2);
74 /* Test that we don't overflow. */
76 (__DBL_MAX__ * 0.5 + __DBL_MAX__ * 0.5i),
78 (__DBL_MAX__ * 0.25 + __DBL_MAX__ * 0.25i),
81 /* Test for accuracy. */
82 COMPILETIME_TESTIT (double,
83 (1 + __DBL_EPSILON__ + 1i),
85 (1 - __DBL_EPSILON__ + 1i),
86 -4.93038065763132378382330353301741393545754021943139377981e-32+2i);
88 /* This becomes (NaN + iInf). */
89 #define VAL1 ((_Complex double)__builtin_inf() * 1i)
91 /* Test some C99 Annex G special cases. */
92 TEST_ONE_PART_INF ((VAL1) * (VAL1));
93 TEST_ONE_PART_INF ((_Complex double)1 / (_Complex double)0);
94 TEST_ONE_PART_INF ((VAL1) / (_Complex double)1);
96 RUNTIME_TESTIT (double, 1, /, VAL1, 0);
97 STATICINIT_TESTIT (double, 1, /, VAL1, 0);