xref: /minix3/sys/external/bsd/compiler_rt/dist/test/Unit/mulsc3_test.c (revision 4684ddb6aab0b36791c8099bc705d6140b3d05d0)
1*4684ddb6SLionel Sambuc //===-- mulsc3_test.c - Test __mulsc3 -------------------------------------===//
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 __mulsc3 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 float _Complex __mulsc3(float __a, float __b, float __c, float __d);
22*4684ddb6SLionel Sambuc 
23*4684ddb6SLionel Sambuc enum {zero, non_zero, inf, NaN, non_zero_nan};
24*4684ddb6SLionel Sambuc 
25*4684ddb6SLionel Sambuc int
classify(float _Complex x)26*4684ddb6SLionel Sambuc classify(float _Complex x)
27*4684ddb6SLionel Sambuc {
28*4684ddb6SLionel Sambuc     if (x == 0)
29*4684ddb6SLionel Sambuc         return zero;
30*4684ddb6SLionel Sambuc     if (isinf(crealf(x)) || isinf(cimagf(x)))
31*4684ddb6SLionel Sambuc         return inf;
32*4684ddb6SLionel Sambuc     if (isnan(crealf(x)) && isnan(cimagf(x)))
33*4684ddb6SLionel Sambuc         return NaN;
34*4684ddb6SLionel Sambuc     if (isnan(crealf(x)))
35*4684ddb6SLionel Sambuc     {
36*4684ddb6SLionel Sambuc         if (cimagf(x) == 0)
37*4684ddb6SLionel Sambuc             return NaN;
38*4684ddb6SLionel Sambuc         return non_zero_nan;
39*4684ddb6SLionel Sambuc     }
40*4684ddb6SLionel Sambuc     if (isnan(cimagf(x)))
41*4684ddb6SLionel Sambuc     {
42*4684ddb6SLionel Sambuc         if (crealf(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__mulsc3(float a,float b,float c,float d)49*4684ddb6SLionel Sambuc int test__mulsc3(float a, float b, float c, float d)
50*4684ddb6SLionel Sambuc {
51*4684ddb6SLionel Sambuc     float _Complex r = __mulsc3(a, b, c, d);
52*4684ddb6SLionel Sambuc //     printf("test__mulsc3(%f, %f, %f, %f) = %f + I%f\n",
53*4684ddb6SLionel Sambuc //             a, b, c, d, crealf(r), cimagf(r));
54*4684ddb6SLionel Sambuc 	float _Complex dividend;
55*4684ddb6SLionel Sambuc 	float _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             {
100*4684ddb6SLionel Sambuc             float _Complex z = a * c - b * d + _Complex_I*(a * d + b * c);
101*4684ddb6SLionel Sambuc             // relaxed tolerance to arbitrary (1.e-6) amount.
102*4684ddb6SLionel Sambuc             if (cabsf((r-z)/r) > 1.e-6)
103*4684ddb6SLionel Sambuc                 return 1;
104*4684ddb6SLionel Sambuc             }
105*4684ddb6SLionel Sambuc             break;
106*4684ddb6SLionel Sambuc         case inf:
107*4684ddb6SLionel Sambuc             if (classify(r) != inf)
108*4684ddb6SLionel Sambuc                 return 1;
109*4684ddb6SLionel Sambuc             break;
110*4684ddb6SLionel Sambuc         case NaN:
111*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
112*4684ddb6SLionel Sambuc                 return 1;
113*4684ddb6SLionel Sambuc             break;
114*4684ddb6SLionel Sambuc         case non_zero_nan:
115*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
116*4684ddb6SLionel Sambuc                 return 1;
117*4684ddb6SLionel Sambuc             break;
118*4684ddb6SLionel Sambuc         }
119*4684ddb6SLionel Sambuc         break;
120*4684ddb6SLionel Sambuc     case inf:
121*4684ddb6SLionel Sambuc         switch (classify(divisor))
122*4684ddb6SLionel Sambuc         {
123*4684ddb6SLionel Sambuc         case zero:
124*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
125*4684ddb6SLionel Sambuc                 return 1;
126*4684ddb6SLionel Sambuc             break;
127*4684ddb6SLionel Sambuc         case non_zero:
128*4684ddb6SLionel Sambuc             if (classify(r) != inf)
129*4684ddb6SLionel Sambuc                 return 1;
130*4684ddb6SLionel Sambuc             break;
131*4684ddb6SLionel Sambuc         case inf:
132*4684ddb6SLionel Sambuc             if (classify(r) != inf)
133*4684ddb6SLionel Sambuc                 return 1;
134*4684ddb6SLionel Sambuc             break;
135*4684ddb6SLionel Sambuc         case NaN:
136*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
137*4684ddb6SLionel Sambuc                 return 1;
138*4684ddb6SLionel Sambuc             break;
139*4684ddb6SLionel Sambuc         case non_zero_nan:
140*4684ddb6SLionel Sambuc             if (classify(r) != inf)
141*4684ddb6SLionel Sambuc                 return 1;
142*4684ddb6SLionel Sambuc             break;
143*4684ddb6SLionel Sambuc         }
144*4684ddb6SLionel Sambuc         break;
145*4684ddb6SLionel Sambuc     case NaN:
146*4684ddb6SLionel Sambuc         switch (classify(divisor))
147*4684ddb6SLionel Sambuc         {
148*4684ddb6SLionel Sambuc         case zero:
149*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
150*4684ddb6SLionel Sambuc                 return 1;
151*4684ddb6SLionel Sambuc             break;
152*4684ddb6SLionel Sambuc         case non_zero:
153*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
154*4684ddb6SLionel Sambuc                 return 1;
155*4684ddb6SLionel Sambuc             break;
156*4684ddb6SLionel Sambuc         case inf:
157*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
158*4684ddb6SLionel Sambuc                 return 1;
159*4684ddb6SLionel Sambuc             break;
160*4684ddb6SLionel Sambuc         case NaN:
161*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
162*4684ddb6SLionel Sambuc                 return 1;
163*4684ddb6SLionel Sambuc             break;
164*4684ddb6SLionel Sambuc         case non_zero_nan:
165*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
166*4684ddb6SLionel Sambuc                 return 1;
167*4684ddb6SLionel Sambuc             break;
168*4684ddb6SLionel Sambuc         }
169*4684ddb6SLionel Sambuc         break;
170*4684ddb6SLionel Sambuc     case non_zero_nan:
171*4684ddb6SLionel Sambuc         switch (classify(divisor))
172*4684ddb6SLionel Sambuc         {
173*4684ddb6SLionel Sambuc         case zero:
174*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
175*4684ddb6SLionel Sambuc                 return 1;
176*4684ddb6SLionel Sambuc             break;
177*4684ddb6SLionel Sambuc         case non_zero:
178*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
179*4684ddb6SLionel Sambuc                 return 1;
180*4684ddb6SLionel Sambuc             break;
181*4684ddb6SLionel Sambuc         case inf:
182*4684ddb6SLionel Sambuc             if (classify(r) != inf)
183*4684ddb6SLionel Sambuc                 return 1;
184*4684ddb6SLionel Sambuc             break;
185*4684ddb6SLionel Sambuc         case NaN:
186*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
187*4684ddb6SLionel Sambuc                 return 1;
188*4684ddb6SLionel Sambuc             break;
189*4684ddb6SLionel Sambuc         case non_zero_nan:
190*4684ddb6SLionel Sambuc             if (classify(r) != NaN)
191*4684ddb6SLionel Sambuc                 return 1;
192*4684ddb6SLionel Sambuc             break;
193*4684ddb6SLionel Sambuc         }
194*4684ddb6SLionel Sambuc         break;
195*4684ddb6SLionel Sambuc     }
196*4684ddb6SLionel Sambuc 
197*4684ddb6SLionel Sambuc     return 0;
198*4684ddb6SLionel Sambuc }
199*4684ddb6SLionel Sambuc 
200*4684ddb6SLionel Sambuc float x[][2] =
201*4684ddb6SLionel Sambuc {
202*4684ddb6SLionel Sambuc     { 1.e-6,  1.e-6},
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 
207*4684ddb6SLionel Sambuc     { 1.e+6,  1.e-6},
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 
212*4684ddb6SLionel Sambuc     { 1.e-6,  1.e+6},
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 
217*4684ddb6SLionel Sambuc     { 1.e+6,  1.e+6},
218*4684ddb6SLionel Sambuc     {-1.e+6,  1.e+6},
219*4684ddb6SLionel Sambuc     {-1.e+6, -1.e+6},
220*4684ddb6SLionel Sambuc     { 1.e+6, -1.e+6},
221*4684ddb6SLionel Sambuc 
222*4684ddb6SLionel Sambuc     {NAN, NAN},
223*4684ddb6SLionel Sambuc     {-INFINITY, NAN},
224*4684ddb6SLionel Sambuc     {-2, NAN},
225*4684ddb6SLionel Sambuc     {-1, NAN},
226*4684ddb6SLionel Sambuc     {-0.5, NAN},
227*4684ddb6SLionel Sambuc     {-0., NAN},
228*4684ddb6SLionel Sambuc     {+0., NAN},
229*4684ddb6SLionel Sambuc     {0.5, NAN},
230*4684ddb6SLionel Sambuc     {1, NAN},
231*4684ddb6SLionel Sambuc     {2, NAN},
232*4684ddb6SLionel Sambuc     {INFINITY, NAN},
233*4684ddb6SLionel Sambuc 
234*4684ddb6SLionel Sambuc     {NAN, -INFINITY},
235*4684ddb6SLionel Sambuc     {-INFINITY, -INFINITY},
236*4684ddb6SLionel Sambuc     {-2, -INFINITY},
237*4684ddb6SLionel Sambuc     {-1, -INFINITY},
238*4684ddb6SLionel Sambuc     {-0.5, -INFINITY},
239*4684ddb6SLionel Sambuc     {-0., -INFINITY},
240*4684ddb6SLionel Sambuc     {+0., -INFINITY},
241*4684ddb6SLionel Sambuc     {0.5, -INFINITY},
242*4684ddb6SLionel Sambuc     {1, -INFINITY},
243*4684ddb6SLionel Sambuc     {2, -INFINITY},
244*4684ddb6SLionel Sambuc     {INFINITY, -INFINITY},
245*4684ddb6SLionel Sambuc 
246*4684ddb6SLionel Sambuc     {NAN, -2},
247*4684ddb6SLionel Sambuc     {-INFINITY, -2},
248*4684ddb6SLionel Sambuc     {-2, -2},
249*4684ddb6SLionel Sambuc     {-1, -2},
250*4684ddb6SLionel Sambuc     {-0.5, -2},
251*4684ddb6SLionel Sambuc     {-0., -2},
252*4684ddb6SLionel Sambuc     {+0., -2},
253*4684ddb6SLionel Sambuc     {0.5, -2},
254*4684ddb6SLionel Sambuc     {1, -2},
255*4684ddb6SLionel Sambuc     {2, -2},
256*4684ddb6SLionel Sambuc     {INFINITY, -2},
257*4684ddb6SLionel Sambuc 
258*4684ddb6SLionel Sambuc     {NAN, -1},
259*4684ddb6SLionel Sambuc     {-INFINITY, -1},
260*4684ddb6SLionel Sambuc     {-2, -1},
261*4684ddb6SLionel Sambuc     {-1, -1},
262*4684ddb6SLionel Sambuc     {-0.5, -1},
263*4684ddb6SLionel Sambuc     {-0., -1},
264*4684ddb6SLionel Sambuc     {+0., -1},
265*4684ddb6SLionel Sambuc     {0.5, -1},
266*4684ddb6SLionel Sambuc     {1, -1},
267*4684ddb6SLionel Sambuc     {2, -1},
268*4684ddb6SLionel Sambuc     {INFINITY, -1},
269*4684ddb6SLionel Sambuc 
270*4684ddb6SLionel Sambuc     {NAN, -0.5},
271*4684ddb6SLionel Sambuc     {-INFINITY, -0.5},
272*4684ddb6SLionel Sambuc     {-2, -0.5},
273*4684ddb6SLionel Sambuc     {-1, -0.5},
274*4684ddb6SLionel Sambuc     {-0.5, -0.5},
275*4684ddb6SLionel Sambuc     {-0., -0.5},
276*4684ddb6SLionel Sambuc     {+0., -0.5},
277*4684ddb6SLionel Sambuc     {0.5, -0.5},
278*4684ddb6SLionel Sambuc     {1, -0.5},
279*4684ddb6SLionel Sambuc     {2, -0.5},
280*4684ddb6SLionel Sambuc     {INFINITY, -0.5},
281*4684ddb6SLionel Sambuc 
282*4684ddb6SLionel Sambuc     {NAN, -0.},
283*4684ddb6SLionel Sambuc     {-INFINITY, -0.},
284*4684ddb6SLionel Sambuc     {-2, -0.},
285*4684ddb6SLionel Sambuc     {-1, -0.},
286*4684ddb6SLionel Sambuc     {-0.5, -0.},
287*4684ddb6SLionel Sambuc     {-0., -0.},
288*4684ddb6SLionel Sambuc     {+0., -0.},
289*4684ddb6SLionel Sambuc     {0.5, -0.},
290*4684ddb6SLionel Sambuc     {1, -0.},
291*4684ddb6SLionel Sambuc     {2, -0.},
292*4684ddb6SLionel Sambuc     {INFINITY, -0.},
293*4684ddb6SLionel Sambuc 
294*4684ddb6SLionel Sambuc     {NAN, 0.},
295*4684ddb6SLionel Sambuc     {-INFINITY, 0.},
296*4684ddb6SLionel Sambuc     {-2, 0.},
297*4684ddb6SLionel Sambuc     {-1, 0.},
298*4684ddb6SLionel Sambuc     {-0.5, 0.},
299*4684ddb6SLionel Sambuc     {-0., 0.},
300*4684ddb6SLionel Sambuc     {+0., 0.},
301*4684ddb6SLionel Sambuc     {0.5, 0.},
302*4684ddb6SLionel Sambuc     {1, 0.},
303*4684ddb6SLionel Sambuc     {2, 0.},
304*4684ddb6SLionel Sambuc     {INFINITY, 0.},
305*4684ddb6SLionel Sambuc 
306*4684ddb6SLionel Sambuc     {NAN, 0.5},
307*4684ddb6SLionel Sambuc     {-INFINITY, 0.5},
308*4684ddb6SLionel Sambuc     {-2, 0.5},
309*4684ddb6SLionel Sambuc     {-1, 0.5},
310*4684ddb6SLionel Sambuc     {-0.5, 0.5},
311*4684ddb6SLionel Sambuc     {-0., 0.5},
312*4684ddb6SLionel Sambuc     {+0., 0.5},
313*4684ddb6SLionel Sambuc     {0.5, 0.5},
314*4684ddb6SLionel Sambuc     {1, 0.5},
315*4684ddb6SLionel Sambuc     {2, 0.5},
316*4684ddb6SLionel Sambuc     {INFINITY, 0.5},
317*4684ddb6SLionel Sambuc 
318*4684ddb6SLionel Sambuc     {NAN, 1},
319*4684ddb6SLionel Sambuc     {-INFINITY, 1},
320*4684ddb6SLionel Sambuc     {-2, 1},
321*4684ddb6SLionel Sambuc     {-1, 1},
322*4684ddb6SLionel Sambuc     {-0.5, 1},
323*4684ddb6SLionel Sambuc     {-0., 1},
324*4684ddb6SLionel Sambuc     {+0., 1},
325*4684ddb6SLionel Sambuc     {0.5, 1},
326*4684ddb6SLionel Sambuc     {1, 1},
327*4684ddb6SLionel Sambuc     {2, 1},
328*4684ddb6SLionel Sambuc     {INFINITY, 1},
329*4684ddb6SLionel Sambuc 
330*4684ddb6SLionel Sambuc     {NAN, 2},
331*4684ddb6SLionel Sambuc     {-INFINITY, 2},
332*4684ddb6SLionel Sambuc     {-2, 2},
333*4684ddb6SLionel Sambuc     {-1, 2},
334*4684ddb6SLionel Sambuc     {-0.5, 2},
335*4684ddb6SLionel Sambuc     {-0., 2},
336*4684ddb6SLionel Sambuc     {+0., 2},
337*4684ddb6SLionel Sambuc     {0.5, 2},
338*4684ddb6SLionel Sambuc     {1, 2},
339*4684ddb6SLionel Sambuc     {2, 2},
340*4684ddb6SLionel Sambuc     {INFINITY, 2},
341*4684ddb6SLionel Sambuc 
342*4684ddb6SLionel Sambuc     {NAN, INFINITY},
343*4684ddb6SLionel Sambuc     {-INFINITY, INFINITY},
344*4684ddb6SLionel Sambuc     {-2, INFINITY},
345*4684ddb6SLionel Sambuc     {-1, INFINITY},
346*4684ddb6SLionel Sambuc     {-0.5, INFINITY},
347*4684ddb6SLionel Sambuc     {-0., INFINITY},
348*4684ddb6SLionel Sambuc     {+0., INFINITY},
349*4684ddb6SLionel Sambuc     {0.5, INFINITY},
350*4684ddb6SLionel Sambuc     {1, INFINITY},
351*4684ddb6SLionel Sambuc     {2, INFINITY},
352*4684ddb6SLionel Sambuc     {INFINITY, INFINITY}
353*4684ddb6SLionel Sambuc 
354*4684ddb6SLionel Sambuc };
355*4684ddb6SLionel Sambuc 
main()356*4684ddb6SLionel Sambuc int main()
357*4684ddb6SLionel Sambuc {
358*4684ddb6SLionel Sambuc     const unsigned N = sizeof(x) / sizeof(x[0]);
359*4684ddb6SLionel Sambuc     unsigned i, j;
360*4684ddb6SLionel Sambuc     for (i = 0; i < N; ++i)
361*4684ddb6SLionel Sambuc     {
362*4684ddb6SLionel Sambuc         for (j = 0; j < N; ++j)
363*4684ddb6SLionel Sambuc         {
364*4684ddb6SLionel Sambuc             if (test__mulsc3(x[i][0], x[i][1], x[j][0], x[j][1]))
365*4684ddb6SLionel Sambuc                 return 1;
366*4684ddb6SLionel Sambuc         }
367*4684ddb6SLionel Sambuc     }
368*4684ddb6SLionel Sambuc 
369*4684ddb6SLionel Sambuc     return 0;
370*4684ddb6SLionel Sambuc }
371