1 /* Copyright (C) 2012-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 /* Adapted from gcc.dg/torture/builtin-complex-1.c test from GCC
19    testsuite written by Joseph S. Myers.  */
20 
21 #include <complex.h>
22 
23 static int result;
24 
25 #define COMPARE_BODY(A, B, TYPE, COPYSIGN)				\
26   do {									\
27     TYPE s1 = COPYSIGN ((TYPE) 1.0, A);					\
28     TYPE s2 = COPYSIGN ((TYPE) 1.0, B);					\
29     if (s1 != s2)							\
30       result |= 1;							\
31     if ((__builtin_isnan (A) != 0) != (__builtin_isnan (B) != 0))	\
32       result |= 1;							\
33     if ((A != B) != (__builtin_isnan (A) != 0))				\
34       result |= 1;							\
35   } while (0)
36 
37 #ifdef CMPLX
38 
39 static void
comparef(float a,float b)40 comparef (float a, float b)
41 {
42   COMPARE_BODY (a, b, float, __builtin_copysignf);
43 }
44 
45 static void
compare(double a,double b)46 compare (double a, double b)
47 {
48   COMPARE_BODY (a, b, double, __builtin_copysign);
49 }
50 
51 static void
comparel(long double a,long double b)52 comparel (long double a, long double b)
53 {
54   COMPARE_BODY (a, b, long double, __builtin_copysignl);
55 }
56 
57 static void
comparecf(_Complex float a,float r,float i)58 comparecf (_Complex float a, float r, float i)
59 {
60   comparef (__real__ a, r);
61   comparef (__imag__ a, i);
62 }
63 
64 static void
comparec(_Complex double a,double r,double i)65 comparec (_Complex double a, double r, double i)
66 {
67   compare (__real__ a, r);
68   compare (__imag__ a, i);
69 }
70 
71 static void
comparecl(_Complex long double a,long double r,long double i)72 comparecl (_Complex long double a, long double r, long double i)
73 {
74   comparel (__real__ a, r);
75   comparel (__imag__ a, i);
76 }
77 
78 #define VERIFY(A, B, TYPE, COMPARE, CL)			\
79   do {							\
80     TYPE a = A;						\
81     TYPE b = B;						\
82     _Complex TYPE cr = CL (a, b);			\
83     static _Complex TYPE cs = CL (A, B);		\
84     COMPARE (cr, A, B);					\
85     COMPARE (cs, A, B);					\
86   } while (0)
87 
88 #define ALL_CHECKS(PZ, NZ, NAN, INF, TYPE, COMPARE, CL)	\
89   do {							\
90     VERIFY (PZ, PZ, TYPE, COMPARE, CL);			\
91     VERIFY (PZ, NZ, TYPE, COMPARE, CL);			\
92     VERIFY (PZ, NAN, TYPE, COMPARE, CL);		\
93     VERIFY (PZ, INF, TYPE, COMPARE, CL);		\
94     VERIFY (NZ, PZ, TYPE, COMPARE, CL);			\
95     VERIFY (NZ, NZ, TYPE, COMPARE, CL);			\
96     VERIFY (NZ, NAN, TYPE, COMPARE, CL);		\
97     VERIFY (NZ, INF, TYPE, COMPARE, CL);		\
98     VERIFY (NAN, PZ, TYPE, COMPARE, CL);		\
99     VERIFY (NAN, NZ, TYPE, COMPARE, CL);		\
100     VERIFY (NAN, NAN, TYPE, COMPARE, CL);		\
101     VERIFY (NAN, INF, TYPE, COMPARE, CL);		\
102     VERIFY (INF, PZ, TYPE, COMPARE,CL);			\
103     VERIFY (INF, NZ, TYPE, COMPARE, CL);		\
104     VERIFY (INF, NAN, TYPE, COMPARE, CL);		\
105     VERIFY (INF, INF, TYPE, COMPARE, CL);		\
106   } while (0)
107 
108 static void
check_float(void)109 check_float (void)
110 {
111   ALL_CHECKS (0.0f, -0.0f, __builtin_nanf (""), __builtin_inff (),
112 	      float, comparecf, CMPLXF);
113 }
114 
115 static void
check_double(void)116 check_double (void)
117 {
118   ALL_CHECKS (0.0, -0.0, __builtin_nan (""), __builtin_inf (),
119 	      double, comparec, CMPLX);
120 }
121 
122 static void
check_long_double(void)123 check_long_double (void)
124 {
125   ALL_CHECKS (0.0l, -0.0l, __builtin_nanl (""), __builtin_infl (),
126 	      long double, comparecl, CMPLXL);
127 }
128 #endif
129 
130 static int
do_test(void)131 do_test (void)
132 {
133 #ifdef CMPLX
134   check_float ();
135   check_double ();
136   check_long_double ();
137 #endif
138 
139   return result;
140 }
141 
142 #define TEST_FUNCTION do_test ()
143 #include "../test-skeleton.c"
144