Correct types of fields in libm-test.inc structures.
[jlayton/glibc.git] / math / divtc3.c
1 /* Copyright (C) 2005-2013 Free Software Foundation, Inc.
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
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
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         {
54           x = __copysignl (INFINITY, c) * a;
55           y = __copysignl (INFINITY, c) * b;
56         }
57       else if ((__isinf_nsl (a) || __isinf_nsl (b))
58                && isfinite (c) && isfinite (d))
59         {
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);
64         }
65       else if ((__isinf_nsl (c) || __isinf_nsl (d))
66                && isfinite (a) && isfinite (b))
67         {
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);
72         }
73     }
74
75   return x + I * y;
76 }