]>
Commit | Line | Data |
---|---|---|
2b778ceb | 1 | /* Copyright (C) 2005-2021 Free Software Foundation, Inc. |
fea3f995 RM |
2 | This file is part of the GNU C Library. |
3 | Contributed by Richard Henderson <rth@redhat.com>, 2005. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
fea3f995 RM |
18 | |
19 | #include <stdbool.h> | |
20 | #include <math.h> | |
21 | #include <complex.h> | |
22 | ||
23 | attribute_hidden | |
24 | long double _Complex | |
25 | __divtc3 (long double a, long double b, long double c, long double d) | |
26 | { | |
27 | long double denom, ratio, x, y; | |
28 | ||
29 | /* ??? We can get better behavior from logarithmic scaling instead of | |
30 | the division. But that would mean starting to link libgcc against | |
31 | libm. We could implement something akin to ldexp/frexp as gcc builtins | |
32 | fairly easily... */ | |
33 | if (fabsl (c) < fabsl (d)) | |
34 | { | |
35 | ratio = c / d; | |
36 | denom = (c * ratio) + d; | |
37 | x = ((a * ratio) + b) / denom; | |
38 | y = ((b * ratio) - a) / denom; | |
39 | } | |
40 | else | |
41 | { | |
42 | ratio = d / c; | |
43 | denom = (d * ratio) + c; | |
44 | x = ((b * ratio) + a) / denom; | |
45 | y = (b - (a * ratio)) / denom; | |
46 | } | |
47 | ||
48 | /* Recover infinities and zeros that computed as NaN+iNaN; the only cases | |
49 | are nonzero/zero, infinite/finite, and finite/infinite. */ | |
50 | if (isnan (x) && isnan (y)) | |
51 | { | |
52 | if (denom == 0.0 && (!isnan (a) || !isnan (b))) | |
53 | { | |
81dca813 JM |
54 | x = copysignl (INFINITY, c) * a; |
55 | y = copysignl (INFINITY, c) * b; | |
fea3f995 | 56 | } |
fe8c2b33 | 57 | else if ((isinf (a) || isinf (b)) |
7edb55ce | 58 | && isfinite (c) && isfinite (d)) |
fea3f995 | 59 | { |
81dca813 JM |
60 | a = copysignl (isinf (a) ? 1 : 0, a); |
61 | b = copysignl (isinf (b) ? 1 : 0, b); | |
fea3f995 RM |
62 | x = INFINITY * (a * c + b * d); |
63 | y = INFINITY * (b * c - a * d); | |
64 | } | |
fe8c2b33 | 65 | else if ((isinf (c) || isinf (d)) |
7edb55ce | 66 | && isfinite (a) && isfinite (b)) |
fea3f995 | 67 | { |
81dca813 JM |
68 | c = copysignl (isinf (c) ? 1 : 0, c); |
69 | d = copysignl (isinf (d) ? 1 : 0, d); | |
fea3f995 RM |
70 | x = 0.0 * (a * c + b * d); |
71 | y = 0.0 * (b * c - a * d); | |
72 | } | |
73 | } | |
74 | ||
75 | return x + I * y; | |
76 | } |