Update copyright notices with scripts/update-copyrights
[glibc.git] / math / divtc3.c
blob48309e42dc6de5f2e4e5b575cd8c30ec25b18ffe
1 /* Copyright (C) 2005-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Richard Henderson <rth@redhat.com>, 2005.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdbool.h>
20 #include <math.h>
21 #include <complex.h>
23 attribute_hidden
24 long double _Complex
25 __divtc3 (long double a, long double b, long double c, long double d)
27 long double denom, ratio, x, y;
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))
35 ratio = c / d;
36 denom = (c * ratio) + d;
37 x = ((a * ratio) + b) / denom;
38 y = ((b * ratio) - a) / denom;
40 else
42 ratio = d / c;
43 denom = (d * ratio) + c;
44 x = ((b * ratio) + a) / denom;
45 y = (b - (a * ratio)) / denom;
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))
52 if (denom == 0.0 && (!isnan (a) || !isnan (b)))
54 x = __copysignl (INFINITY, c) * a;
55 y = __copysignl (INFINITY, c) * b;
57 else if ((__isinf_nsl (a) || __isinf_nsl (b))
58 && isfinite (c) && isfinite (d))
60 a = __copysignl (__isinf_nsl (a) ? 1 : 0, a);
61 b = __copysignl (__isinf_nsl (b) ? 1 : 0, b);
62 x = INFINITY * (a * c + b * d);
63 y = INFINITY * (b * c - a * d);
65 else if ((__isinf_nsl (c) || __isinf_nsl (d))
66 && isfinite (a) && isfinite (b))
68 c = __copysignl (__isinf_nsl (c) ? 1 : 0, c);
69 d = __copysignl (__isinf_nsl (d) ? 1 : 0, d);
70 x = 0.0 * (a * c + b * d);
71 y = 0.0 * (b * c - a * d);
75 return x + I * y;