1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20: #include <stdbool.h>
21: #include <math.h>
22: #include <complex.h>
23:
24: attribute_hidden
25: long double _Complex
26: __divtc3 (long double a, long double b, long double c, long double d)
27: {
28: long double denom, ratio, x, y;
29:
30:
31:
32:
33:
34: if (fabsl (c) < fabsl (d))
35: {
36: ratio = c / d;
37: denom = (c * ratio) + d;
38: x = ((a * ratio) + b) / denom;
39: y = ((b * ratio) - a) / denom;
40: }
41: else
42: {
43: ratio = d / c;
44: denom = (d * ratio) + c;
45: x = ((b * ratio) + a) / denom;
46: y = (b - (a * ratio)) / denom;
47: }
48:
49:
50:
51: if (isnan (x) && isnan (y))
52: {
53: if (denom == 0.0 && (!isnan (a) || !isnan (b)))
54: {
55: x = __copysignl (INFINITY, c) * a;
56: y = __copysignl (INFINITY, c) * b;
57: }
58: else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
59: {
60: a = __copysignl (isinf (a) ? 1 : 0, a);
61: b = __copysignl (isinf (b) ? 1 : 0, b);
62: x = INFINITY * (a * c + b * d);
63: y = INFINITY * (b * c - a * d);
64: }
65: else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
66: {
67: c = __copysignl (isinf (c) ? 1 : 0, c);
68: d = __copysignl (isinf (d) ? 1 : 0, d);
69: x = 0.0 * (a * c + b * d);
70: y = 0.0 * (b * c - a * d);
71: }
72: }
73:
74: return x + I * y;
75: }