xref: /minix3/sys/external/bsd/compiler_rt/dist/test/Unit/muldc3_test.c (revision 4684ddb6aab0b36791c8099bc705d6140b3d05d0)
1*4684ddb6SLionel Sambuc //===-- muldc3_test.c - Test __muldc3 -------------------------------------===//
2*4684ddb6SLionel Sambuc //
3*4684ddb6SLionel Sambuc //                     The LLVM Compiler Infrastructure
4*4684ddb6SLionel Sambuc //
5*4684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*4684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*4684ddb6SLionel Sambuc //
8*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
9*4684ddb6SLionel Sambuc //
10*4684ddb6SLionel Sambuc // This file tests __muldc3 for the compiler_rt library.
11*4684ddb6SLionel Sambuc //
12*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
13*4684ddb6SLionel Sambuc 
14*4684ddb6SLionel Sambuc #include "int_lib.h"
15*4684ddb6SLionel Sambuc #include <math.h>
16*4684ddb6SLionel Sambuc #include <complex.h>
17*4684ddb6SLionel Sambuc #include <stdio.h>
18*4684ddb6SLionel Sambuc 
19*4684ddb6SLionel Sambuc // Returns: the product of a + ib and c + id
20*4684ddb6SLionel Sambuc 
21*4684ddb6SLionel Sambuc double _Complex __muldc3(double __a, double __b, double __c, double __d);
22*4684ddb6SLionel Sambuc 
23*4684ddb6SLionel Sambuc enum {zero, non_zero, inf, NaN, non_zero_nan};
24*4684ddb6SLionel Sambuc 
25*4684ddb6SLionel Sambuc int
classify(double _Complex x)26*4684ddb6SLionel Sambuc classify(double _Complex x)
27*4684ddb6SLionel Sambuc {
28*4684ddb6SLionel Sambuc     if (x == 0)
29*4684ddb6SLionel Sambuc         return zero;
30*4684ddb6SLionel Sambuc     if (isinf(creal(x)) || isinf(cimag(x)))
31*4684ddb6SLionel Sambuc         return inf;
32*4684ddb6SLionel Sambuc     if (isnan(creal(x)) && isnan(cimag(x)))
33*4684ddb6SLionel Sambuc         return NaN;
34*4684ddb6SLionel Sambuc     if (isnan(creal(x)))
35*4684ddb6SLionel Sambuc     {
36*4684ddb6SLionel Sambuc         if (cimag(x) == 0)
37*4684ddb6SLionel Sambuc             return NaN;
38*4684ddb6SLionel Sambuc         return non_zero_nan;
39*4684ddb6SLionel Sambuc     }
40*4684ddb6SLionel Sambuc     if (isnan(cimag(x)))
41*4684ddb6SLionel Sambuc     {
42*4684ddb6SLionel Sambuc         if (creal(x) == 0)
43*4684ddb6SLionel Sambuc             return NaN;
44*4684ddb6SLionel Sambuc         return non_zero_nan;
45*4684ddb6SLionel Sambuc     }
46*4684ddb6SLionel Sambuc     return non_zero;
47*4684ddb6SLionel Sambuc }
48*4684ddb6SLionel Sambuc 
test__muldc3(double a,double b,double c,double d)49*4684ddb6SLionel Sambuc int test__muldc3(double a, double b, double c, double d)
50*4684ddb6SLionel Sambuc {
51*4684ddb6SLionel Sambuc     double _Complex r = __muldc3(a, b, c, d);
52*4684ddb6SLionel Sambuc //     printf("test__muldc3(%f, %f, %f, %f) = %f + I%f\n",
53*4684ddb6SLionel Sambuc //             a, b, c, d, creal(r), cimag(r));
54*4684ddb6SLionel Sambuc 	double _Complex dividend;
55*4684ddb6SLionel Sambuc 	double _Complex divisor;
56*4684ddb6SLionel Sambuc 
57*4684ddb6SLionel Sambuc 	__real__ dividend = a;
58*4684ddb6SLionel Sambuc 	__imag__ dividend = b;
59*4684ddb6SLionel Sambuc 	__real__ divisor = c;
60*4684ddb6SLionel Sambuc 	__imag__ divisor = d;
61*4684ddb6SLionel Sambuc 
62*4684ddb6SLionel Sambuc     switch (classify(dividend))
63*4684ddb6SLionel Sambuc     {
64*4684ddb6SLionel Sambuc     case zero:
65*4684ddb6SLionel Sambuc         switch (classify(divisor))
66*4684ddb6SLionel Sambuc         {
67*4684ddb6SLionel Sambuc         case zero:
68*4684ddb6SLionel Sambuc             if (classify(r) != zero)
69*4684ddb6SLionel Sambuc                 return 1;
70*4684ddb6SLionel Sambuc             break;
71*4684ddb6SLionel Sambuc         case non_zero:
72*4684ddb6SLionel Sambuc             if (classify(r) != zero)
73*4684ddb6SLionel Sambuc                 return 1;
74*4684ddb6SLionel Sambuc             break;
75*4684ddb6SLionel Sambuc         case inf:
76*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
77*4684ddb6SLionel Sambuc                 return 1;
78*4684ddb6SLionel Sambuc             break;
79*4684ddb6SLionel Sambuc         case NaN:
80*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
81*4684ddb6SLionel Sambuc                 return 1;
82*4684ddb6SLionel Sambuc             break;
83*4684ddb6SLionel Sambuc         case non_zero_nan:
84*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
85*4684ddb6SLionel Sambuc                 return 1;
86*4684ddb6SLionel Sambuc             break;
87*4684ddb6SLionel Sambuc         }
88*4684ddb6SLionel Sambuc         break;
89*4684ddb6SLionel Sambuc     case non_zero:
90*4684ddb6SLionel Sambuc         switch (classify(divisor))
91*4684ddb6SLionel Sambuc         {
92*4684ddb6SLionel Sambuc         case zero:
93*4684ddb6SLionel Sambuc             if (classify(r) != zero)
94*4684ddb6SLionel Sambuc                 return 1;
95*4684ddb6SLionel Sambuc             break;
96*4684ddb6SLionel Sambuc         case non_zero:
97*4684ddb6SLionel Sambuc             if (classify(r) != non_zero)
98*4684ddb6SLionel Sambuc                 return 1;
99*4684ddb6SLionel Sambuc             if (r != a * c - b * d + _Complex_I*(a * d + b * c))
100*4684ddb6SLionel Sambuc                 return 1;
101*4684ddb6SLionel Sambuc             break;
102*4684ddb6SLionel Sambuc         case inf:
103*4684ddb6SLionel Sambuc             if (classify(r) != inf)
104*4684ddb6SLionel Sambuc                 return 1;
105*4684ddb6SLionel Sambuc             break;
106*4684ddb6SLionel Sambuc         case NaN:
107*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
108*4684ddb6SLionel Sambuc                 return 1;
109*4684ddb6SLionel Sambuc             break;
110*4684ddb6SLionel Sambuc         case non_zero_nan:
111*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
112*4684ddb6SLionel Sambuc                 return 1;
113*4684ddb6SLionel Sambuc             break;
114*4684ddb6SLionel Sambuc         }
115*4684ddb6SLionel Sambuc         break;
116*4684ddb6SLionel Sambuc     case inf:
117*4684ddb6SLionel Sambuc         switch (classify(divisor))
118*4684ddb6SLionel Sambuc         {
119*4684ddb6SLionel Sambuc         case zero:
120*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
121*4684ddb6SLionel Sambuc                 return 1;
122*4684ddb6SLionel Sambuc             break;
123*4684ddb6SLionel Sambuc         case non_zero:
124*4684ddb6SLionel Sambuc             if (classify(r) != inf)
125*4684ddb6SLionel Sambuc                 return 1;
126*4684ddb6SLionel Sambuc             break;
127*4684ddb6SLionel Sambuc         case inf:
128*4684ddb6SLionel Sambuc             if (classify(r) != inf)
129*4684ddb6SLionel Sambuc                 return 1;
130*4684ddb6SLionel Sambuc             break;
131*4684ddb6SLionel Sambuc         case NaN:
132*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
133*4684ddb6SLionel Sambuc                 return 1;
134*4684ddb6SLionel Sambuc             break;
135*4684ddb6SLionel Sambuc         case non_zero_nan:
136*4684ddb6SLionel Sambuc             if (classify(r) != inf)
137*4684ddb6SLionel Sambuc                 return 1;
138*4684ddb6SLionel Sambuc             break;
139*4684ddb6SLionel Sambuc         }
140*4684ddb6SLionel Sambuc         break;
141*4684ddb6SLionel Sambuc     case NaN:
142*4684ddb6SLionel Sambuc         switch (classify(divisor))
143*4684ddb6SLionel Sambuc         {
144*4684ddb6SLionel Sambuc         case zero:
145*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
146*4684ddb6SLionel Sambuc                 return 1;
147*4684ddb6SLionel Sambuc             break;
148*4684ddb6SLionel Sambuc         case non_zero:
149*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
150*4684ddb6SLionel Sambuc                 return 1;
151*4684ddb6SLionel Sambuc             break;
152*4684ddb6SLionel Sambuc         case inf:
153*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
154*4684ddb6SLionel Sambuc                 return 1;
155*4684ddb6SLionel Sambuc             break;
156*4684ddb6SLionel Sambuc         case NaN:
157*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
158*4684ddb6SLionel Sambuc                 return 1;
159*4684ddb6SLionel Sambuc             break;
160*4684ddb6SLionel Sambuc         case non_zero_nan:
161*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
162*4684ddb6SLionel Sambuc                 return 1;
163*4684ddb6SLionel Sambuc             break;
164*4684ddb6SLionel Sambuc         }
165*4684ddb6SLionel Sambuc         break;
166*4684ddb6SLionel Sambuc     case non_zero_nan:
167*4684ddb6SLionel Sambuc         switch (classify(divisor))
168*4684ddb6SLionel Sambuc         {
169*4684ddb6SLionel Sambuc         case zero:
170*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
171*4684ddb6SLionel Sambuc                 return 1;
172*4684ddb6SLionel Sambuc             break;
173*4684ddb6SLionel Sambuc         case non_zero:
174*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
175*4684ddb6SLionel Sambuc                 return 1;
176*4684ddb6SLionel Sambuc             break;
177*4684ddb6SLionel Sambuc         case inf:
178*4684ddb6SLionel Sambuc             if (classify(r) != inf)
179*4684ddb6SLionel Sambuc                 return 1;
180*4684ddb6SLionel Sambuc             break;
181*4684ddb6SLionel Sambuc         case NaN:
182*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
183*4684ddb6SLionel Sambuc                 return 1;
184*4684ddb6SLionel Sambuc             break;
185*4684ddb6SLionel Sambuc         case non_zero_nan:
186*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
187*4684ddb6SLionel Sambuc                 return 1;
188*4684ddb6SLionel Sambuc             break;
189*4684ddb6SLionel Sambuc         }
190*4684ddb6SLionel Sambuc         break;
191*4684ddb6SLionel Sambuc     }
192*4684ddb6SLionel Sambuc 
193*4684ddb6SLionel Sambuc     return 0;
194*4684ddb6SLionel Sambuc }
195*4684ddb6SLionel Sambuc 
196*4684ddb6SLionel Sambuc double x[][2] =
197*4684ddb6SLionel Sambuc {
198*4684ddb6SLionel Sambuc     { 1.e-6,  1.e-6},
199*4684ddb6SLionel Sambuc     {-1.e-6,  1.e-6},
200*4684ddb6SLionel Sambuc     {-1.e-6, -1.e-6},
201*4684ddb6SLionel Sambuc     { 1.e-6, -1.e-6},
202*4684ddb6SLionel Sambuc 
203*4684ddb6SLionel Sambuc     { 1.e+6,  1.e-6},
204*4684ddb6SLionel Sambuc     {-1.e+6,  1.e-6},
205*4684ddb6SLionel Sambuc     {-1.e+6, -1.e-6},
206*4684ddb6SLionel Sambuc     { 1.e+6, -1.e-6},
207*4684ddb6SLionel Sambuc 
208*4684ddb6SLionel Sambuc     { 1.e-6,  1.e+6},
209*4684ddb6SLionel Sambuc     {-1.e-6,  1.e+6},
210*4684ddb6SLionel Sambuc     {-1.e-6, -1.e+6},
211*4684ddb6SLionel Sambuc     { 1.e-6, -1.e+6},
212*4684ddb6SLionel Sambuc 
213*4684ddb6SLionel Sambuc     { 1.e+6,  1.e+6},
214*4684ddb6SLionel Sambuc     {-1.e+6,  1.e+6},
215*4684ddb6SLionel Sambuc     {-1.e+6, -1.e+6},
216*4684ddb6SLionel Sambuc     { 1.e+6, -1.e+6},
217*4684ddb6SLionel Sambuc 
218*4684ddb6SLionel Sambuc     {NAN, NAN},
219*4684ddb6SLionel Sambuc     {-INFINITY, NAN},
220*4684ddb6SLionel Sambuc     {-2, NAN},
221*4684ddb6SLionel Sambuc     {-1, NAN},
222*4684ddb6SLionel Sambuc     {-0.5, NAN},
223*4684ddb6SLionel Sambuc     {-0., NAN},
224*4684ddb6SLionel Sambuc     {+0., NAN},
225*4684ddb6SLionel Sambuc     {0.5, NAN},
226*4684ddb6SLionel Sambuc     {1, NAN},
227*4684ddb6SLionel Sambuc     {2, NAN},
228*4684ddb6SLionel Sambuc     {INFINITY, NAN},
229*4684ddb6SLionel Sambuc 
230*4684ddb6SLionel Sambuc     {NAN, -INFINITY},
231*4684ddb6SLionel Sambuc     {-INFINITY, -INFINITY},
232*4684ddb6SLionel Sambuc     {-2, -INFINITY},
233*4684ddb6SLionel Sambuc     {-1, -INFINITY},
234*4684ddb6SLionel Sambuc     {-0.5, -INFINITY},
235*4684ddb6SLionel Sambuc     {-0., -INFINITY},
236*4684ddb6SLionel Sambuc     {+0., -INFINITY},
237*4684ddb6SLionel Sambuc     {0.5, -INFINITY},
238*4684ddb6SLionel Sambuc     {1, -INFINITY},
239*4684ddb6SLionel Sambuc     {2, -INFINITY},
240*4684ddb6SLionel Sambuc     {INFINITY, -INFINITY},
241*4684ddb6SLionel Sambuc 
242*4684ddb6SLionel Sambuc     {NAN, -2},
243*4684ddb6SLionel Sambuc     {-INFINITY, -2},
244*4684ddb6SLionel Sambuc     {-2, -2},
245*4684ddb6SLionel Sambuc     {-1, -2},
246*4684ddb6SLionel Sambuc     {-0.5, -2},
247*4684ddb6SLionel Sambuc     {-0., -2},
248*4684ddb6SLionel Sambuc     {+0., -2},
249*4684ddb6SLionel Sambuc     {0.5, -2},
250*4684ddb6SLionel Sambuc     {1, -2},
251*4684ddb6SLionel Sambuc     {2, -2},
252*4684ddb6SLionel Sambuc     {INFINITY, -2},
253*4684ddb6SLionel Sambuc 
254*4684ddb6SLionel Sambuc     {NAN, -1},
255*4684ddb6SLionel Sambuc     {-INFINITY, -1},
256*4684ddb6SLionel Sambuc     {-2, -1},
257*4684ddb6SLionel Sambuc     {-1, -1},
258*4684ddb6SLionel Sambuc     {-0.5, -1},
259*4684ddb6SLionel Sambuc     {-0., -1},
260*4684ddb6SLionel Sambuc     {+0., -1},
261*4684ddb6SLionel Sambuc     {0.5, -1},
262*4684ddb6SLionel Sambuc     {1, -1},
263*4684ddb6SLionel Sambuc     {2, -1},
264*4684ddb6SLionel Sambuc     {INFINITY, -1},
265*4684ddb6SLionel Sambuc 
266*4684ddb6SLionel Sambuc     {NAN, -0.5},
267*4684ddb6SLionel Sambuc     {-INFINITY, -0.5},
268*4684ddb6SLionel Sambuc     {-2, -0.5},
269*4684ddb6SLionel Sambuc     {-1, -0.5},
270*4684ddb6SLionel Sambuc     {-0.5, -0.5},
271*4684ddb6SLionel Sambuc     {-0., -0.5},
272*4684ddb6SLionel Sambuc     {+0., -0.5},
273*4684ddb6SLionel Sambuc     {0.5, -0.5},
274*4684ddb6SLionel Sambuc     {1, -0.5},
275*4684ddb6SLionel Sambuc     {2, -0.5},
276*4684ddb6SLionel Sambuc     {INFINITY, -0.5},
277*4684ddb6SLionel Sambuc 
278*4684ddb6SLionel Sambuc     {NAN, -0.},
279*4684ddb6SLionel Sambuc     {-INFINITY, -0.},
280*4684ddb6SLionel Sambuc     {-2, -0.},
281*4684ddb6SLionel Sambuc     {-1, -0.},
282*4684ddb6SLionel Sambuc     {-0.5, -0.},
283*4684ddb6SLionel Sambuc     {-0., -0.},
284*4684ddb6SLionel Sambuc     {+0., -0.},
285*4684ddb6SLionel Sambuc     {0.5, -0.},
286*4684ddb6SLionel Sambuc     {1, -0.},
287*4684ddb6SLionel Sambuc     {2, -0.},
288*4684ddb6SLionel Sambuc     {INFINITY, -0.},
289*4684ddb6SLionel Sambuc 
290*4684ddb6SLionel Sambuc     {NAN, 0.},
291*4684ddb6SLionel Sambuc     {-INFINITY, 0.},
292*4684ddb6SLionel Sambuc     {-2, 0.},
293*4684ddb6SLionel Sambuc     {-1, 0.},
294*4684ddb6SLionel Sambuc     {-0.5, 0.},
295*4684ddb6SLionel Sambuc     {-0., 0.},
296*4684ddb6SLionel Sambuc     {+0., 0.},
297*4684ddb6SLionel Sambuc     {0.5, 0.},
298*4684ddb6SLionel Sambuc     {1, 0.},
299*4684ddb6SLionel Sambuc     {2, 0.},
300*4684ddb6SLionel Sambuc     {INFINITY, 0.},
301*4684ddb6SLionel Sambuc 
302*4684ddb6SLionel Sambuc     {NAN, 0.5},
303*4684ddb6SLionel Sambuc     {-INFINITY, 0.5},
304*4684ddb6SLionel Sambuc     {-2, 0.5},
305*4684ddb6SLionel Sambuc     {-1, 0.5},
306*4684ddb6SLionel Sambuc     {-0.5, 0.5},
307*4684ddb6SLionel Sambuc     {-0., 0.5},
308*4684ddb6SLionel Sambuc     {+0., 0.5},
309*4684ddb6SLionel Sambuc     {0.5, 0.5},
310*4684ddb6SLionel Sambuc     {1, 0.5},
311*4684ddb6SLionel Sambuc     {2, 0.5},
312*4684ddb6SLionel Sambuc     {INFINITY, 0.5},
313*4684ddb6SLionel Sambuc 
314*4684ddb6SLionel Sambuc     {NAN, 1},
315*4684ddb6SLionel Sambuc     {-INFINITY, 1},
316*4684ddb6SLionel Sambuc     {-2, 1},
317*4684ddb6SLionel Sambuc     {-1, 1},
318*4684ddb6SLionel Sambuc     {-0.5, 1},
319*4684ddb6SLionel Sambuc     {-0., 1},
320*4684ddb6SLionel Sambuc     {+0., 1},
321*4684ddb6SLionel Sambuc     {0.5, 1},
322*4684ddb6SLionel Sambuc     {1, 1},
323*4684ddb6SLionel Sambuc     {2, 1},
324*4684ddb6SLionel Sambuc     {INFINITY, 1},
325*4684ddb6SLionel Sambuc 
326*4684ddb6SLionel Sambuc     {NAN, 2},
327*4684ddb6SLionel Sambuc     {-INFINITY, 2},
328*4684ddb6SLionel Sambuc     {-2, 2},
329*4684ddb6SLionel Sambuc     {-1, 2},
330*4684ddb6SLionel Sambuc     {-0.5, 2},
331*4684ddb6SLionel Sambuc     {-0., 2},
332*4684ddb6SLionel Sambuc     {+0., 2},
333*4684ddb6SLionel Sambuc     {0.5, 2},
334*4684ddb6SLionel Sambuc     {1, 2},
335*4684ddb6SLionel Sambuc     {2, 2},
336*4684ddb6SLionel Sambuc     {INFINITY, 2},
337*4684ddb6SLionel Sambuc 
338*4684ddb6SLionel Sambuc     {NAN, INFINITY},
339*4684ddb6SLionel Sambuc     {-INFINITY, INFINITY},
340*4684ddb6SLionel Sambuc     {-2, INFINITY},
341*4684ddb6SLionel Sambuc     {-1, INFINITY},
342*4684ddb6SLionel Sambuc     {-0.5, INFINITY},
343*4684ddb6SLionel Sambuc     {-0., INFINITY},
344*4684ddb6SLionel Sambuc     {+0., INFINITY},
345*4684ddb6SLionel Sambuc     {0.5, INFINITY},
346*4684ddb6SLionel Sambuc     {1, INFINITY},
347*4684ddb6SLionel Sambuc     {2, INFINITY},
348*4684ddb6SLionel Sambuc     {INFINITY, INFINITY}
349*4684ddb6SLionel Sambuc 
350*4684ddb6SLionel Sambuc };
351*4684ddb6SLionel Sambuc 
main()352*4684ddb6SLionel Sambuc int main()
353*4684ddb6SLionel Sambuc {
354*4684ddb6SLionel Sambuc     const unsigned N = sizeof(x) / sizeof(x[0]);
355*4684ddb6SLionel Sambuc     unsigned i, j;
356*4684ddb6SLionel Sambuc     for (i = 0; i < N; ++i)
357*4684ddb6SLionel Sambuc     {
358*4684ddb6SLionel Sambuc         for (j = 0; j < N; ++j)
359*4684ddb6SLionel Sambuc         {
360*4684ddb6SLionel Sambuc             if (test__muldc3(x[i][0], x[i][1], x[j][0], x[j][1]))
361*4684ddb6SLionel Sambuc                 return 1;
362*4684ddb6SLionel Sambuc         }
363*4684ddb6SLionel Sambuc     }
364*4684ddb6SLionel Sambuc 
365*4684ddb6SLionel Sambuc     return 0;
366*4684ddb6SLionel Sambuc }
367