1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_divdc3
3 // REQUIRES: c99-complex
11 // Returns: the quotient of (a + ib) / (c + id)
13 COMPILER_RT_ABI
double _Complex
14 __divdc3(double __a
, double __b
, double __c
, double __d
);
16 enum {zero
, non_zero
, inf
, NaN
, non_zero_nan
};
19 classify(double _Complex x
)
23 if (isinf(creal(x
)) || isinf(cimag(x
)))
25 if (isnan(creal(x
)) && isnan(cimag(x
)))
42 int test__divdc3(double a
, double b
, double c
, double d
)
44 double _Complex r
= __divdc3(a
, b
, c
, d
);
45 // printf("test__divdc3(%f, %f, %f, %f) = %f + I%f\n",
46 // a, b, c, d, creal(r), cimag(r));
47 double _Complex dividend
;
48 double _Complex divisor
;
50 __real__ dividend
= a
;
51 __imag__ dividend
= b
;
55 switch (classify(dividend
))
58 switch (classify(divisor
))
61 if (classify(r
) != NaN
)
65 if (classify(r
) != zero
)
69 if (classify(r
) != zero
)
73 if (classify(r
) != NaN
)
77 if (classify(r
) != NaN
)
83 switch (classify(divisor
))
86 if (classify(r
) != inf
)
90 if (classify(r
) != non_zero
)
93 double _Complex z
= (a
* c
+ b
* d
) / (c
* c
+ d
* d
)
94 + (b
* c
- a
* d
) / (c
* c
+ d
* d
) * _Complex_I
;
95 if (cabs((r
-z
)/r
) > 1.e
-6)
100 if (classify(r
) != zero
)
104 if (classify(r
) != NaN
)
108 if (classify(r
) != NaN
)
114 switch (classify(divisor
))
117 if (classify(r
) != inf
)
121 if (classify(r
) != inf
)
125 if (classify(r
) != NaN
)
129 if (classify(r
) != NaN
)
133 if (classify(r
) != NaN
)
139 switch (classify(divisor
))
142 if (classify(r
) != NaN
)
146 if (classify(r
) != NaN
)
150 if (classify(r
) != NaN
)
154 if (classify(r
) != NaN
)
158 if (classify(r
) != NaN
)
164 switch (classify(divisor
))
167 if (classify(r
) != inf
)
171 if (classify(r
) != NaN
)
175 if (classify(r
) != NaN
)
179 if (classify(r
) != NaN
)
183 if (classify(r
) != NaN
)
228 {-INFINITY
, -INFINITY
},
237 {INFINITY
, -INFINITY
},
336 {-INFINITY
, INFINITY
},
351 const unsigned N
= sizeof(x
) / sizeof(x
[0]);
353 for (i
= 0; i
< N
; ++i
)
355 for (j
= 0; j
< N
; ++j
)
357 if (test__divdc3(x
[i
][0], x
[i
][1], x
[j
][0], x
[j
][1]))