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