]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgcc/config/rs6000/_divkc3.c
Update copyright years.
[thirdparty/gcc.git] / libgcc / config / rs6000 / _divkc3.c
1 /* Copyright (C) 1989-2019 Free Software Foundation, Inc.
2
3 This file is part of GCC.
4
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
8 version.
9
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 for more details.
14
15 Under Section 7 of GPL version 3, you are granted additional
16 permissions described in the GCC Runtime Library Exception, version
17 3.1, as published by the Free Software Foundation.
18
19 You should have received a copy of the GNU General Public License and
20 a copy of the GCC Runtime Library Exception along with this program;
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 <http://www.gnu.org/licenses/>. */
23
24 /* This is a temporary specialization of code from libgcc/libgcc2.c. */
25
26 #include "soft-fp.h"
27 #include "quad-float128.h"
28
29 #define COPYSIGN(x,y) __builtin_copysignf128 (x, y)
30 #define INFINITY __builtin_inff128 ()
31 #define FABS __builtin_fabsf128
32 #define isnan __builtin_isnan
33 #define isinf __builtin_isinf
34 #define isfinite __builtin_isfinite
35
36 #if defined(FLOAT128_HW_INSNS) && !defined(__divkc3)
37 #define __divkc3 __divkc3_sw
38 #endif
39
40 TCtype
41 __divkc3 (TFtype a, TFtype b, TFtype c, TFtype d)
42 {
43 TFtype denom, ratio, x, y;
44 TCtype res;
45
46 /* ??? We can get better behavior from logarithmic scaling instead of
47 the division. But that would mean starting to link libgcc against
48 libm. We could implement something akin to ldexp/frexp as gcc builtins
49 fairly easily... */
50 if (FABS (c) < FABS (d))
51 {
52 ratio = c / d;
53 denom = (c * ratio) + d;
54 x = ((a * ratio) + b) / denom;
55 y = ((b * ratio) - a) / denom;
56 }
57 else
58 {
59 ratio = d / c;
60 denom = (d * ratio) + c;
61 x = ((b * ratio) + a) / denom;
62 y = (b - (a * ratio)) / denom;
63 }
64
65 /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
66 are nonzero/zero, infinite/finite, and finite/infinite. */
67 if (isnan (x) && isnan (y))
68 {
69 if (c == 0.0 && d == 0.0 && (!isnan (a) || !isnan (b)))
70 {
71 x = COPYSIGN (INFINITY, c) * a;
72 y = COPYSIGN (INFINITY, c) * b;
73 }
74 else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
75 {
76 a = COPYSIGN (isinf (a) ? 1 : 0, a);
77 b = COPYSIGN (isinf (b) ? 1 : 0, b);
78 x = INFINITY * (a * c + b * d);
79 y = INFINITY * (b * c - a * d);
80 }
81 else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
82 {
83 c = COPYSIGN (isinf (c) ? 1 : 0, c);
84 d = COPYSIGN (isinf (d) ? 1 : 0, d);
85 x = 0.0 * (a * c + b * d);
86 y = 0.0 * (b * c - a * d);
87 }
88 }
89
90 __real__ res = x;
91 __imag__ res = y;
92 return res;
93 }