]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/rs6000/darwin-ldouble.c
[multiple changes]
[thirdparty/gcc.git] / gcc / config / rs6000 / darwin-ldouble.c
1 /* 128-bit long double support routines for Darwin.
2 Copyright (C) 1993, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
20
21 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with GCC; see the file COPYING. If not, write to the Free
28 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 02110-1301, USA. */
30
31 /* Implementations of floating-point long double basic arithmetic
32 functions called by the IBM C compiler when generating code for
33 PowerPC platforms. In particular, the following functions are
34 implemented: __gcc_qadd, __gcc_qsub, __gcc_qmul, and __gcc_qdiv.
35 Double-double algorithms are based on the paper "Doubled-Precision
36 IEEE Standard 754 Floating-Point Arithmetic" by W. Kahan, February 26,
37 1987. An alternative published reference is "Software for
38 Doubled-Precision Floating-Point Computations", by Seppo Linnainmaa,
39 ACM TOMS vol 7 no 3, September 1981, pages 272-283. */
40
41 /* Each long double is made up of two IEEE doubles. The value of the
42 long double is the sum of the values of the two parts. The most
43 significant part is required to be the value of the long double
44 rounded to the nearest double, as specified by IEEE. For Inf
45 values, the least significant part is required to be one of +0.0 or
46 -0.0. No other requirements are made; so, for example, 1.0 may be
47 represented as (1.0, +0.0) or (1.0, -0.0), and the low part of a
48 NaN is don't-care.
49
50 This code currently assumes big-endian. */
51
52 #if !_SOFT_FLOAT && (defined (__MACH__) || defined (__powerpc64__) || defined (__powerpc__) || defined (_AIX))
53
54 #define fabs(x) __builtin_fabs(x)
55 #define isless(x, y) __builtin_isless (x, y)
56 #define inf() __builtin_inf()
57
58 #define unlikely(x) __builtin_expect ((x), 0)
59
60 #define nonfinite(a) unlikely (! isless (fabs (a), inf ()))
61
62 /* All these routines actually take two long doubles as parameters,
63 but GCC currently generates poor code when a union is used to turn
64 a long double into a pair of doubles. */
65
66 extern long double __gcc_qadd (double, double, double, double);
67 extern long double __gcc_qsub (double, double, double, double);
68 extern long double __gcc_qmul (double, double, double, double);
69 extern long double __gcc_qdiv (double, double, double, double);
70
71 #if defined __ELF__ && defined SHARED \
72 && (defined __powerpc64__ || !(defined __linux__ || defined __gnu_hurd__))
73 /* Provide definitions of the old symbol names to satisfy apps and
74 shared libs built against an older libgcc. To access the _xlq
75 symbols an explicit version reference is needed, so these won't
76 satisfy an unadorned reference like _xlqadd. If dot symbols are
77 not needed, the assembler will remove the aliases from the symbol
78 table. */
79 __asm__ (".symver __gcc_qadd,_xlqadd@GCC_3.4\n\t"
80 ".symver __gcc_qsub,_xlqsub@GCC_3.4\n\t"
81 ".symver __gcc_qmul,_xlqmul@GCC_3.4\n\t"
82 ".symver __gcc_qdiv,_xlqdiv@GCC_3.4\n\t"
83 ".symver .__gcc_qadd,._xlqadd@GCC_3.4\n\t"
84 ".symver .__gcc_qsub,._xlqsub@GCC_3.4\n\t"
85 ".symver .__gcc_qmul,._xlqmul@GCC_3.4\n\t"
86 ".symver .__gcc_qdiv,._xlqdiv@GCC_3.4");
87 #endif
88
89 typedef union
90 {
91 long double ldval;
92 double dval[2];
93 } longDblUnion;
94
95 /* Add two 'long double' values and return the result. */
96 long double
97 __gcc_qadd (double a, double aa, double c, double cc)
98 {
99 longDblUnion x;
100 double z, q, zz, xh;
101
102 z = a + c;
103
104 if (nonfinite (z))
105 {
106 z = cc + aa + c + a;
107 if (nonfinite (z))
108 return z;
109 x.dval[0] = z; /* Will always be DBL_MAX. */
110 zz = aa + cc;
111 if (fabs(a) > fabs(c))
112 x.dval[1] = a - z + c + zz;
113 else
114 x.dval[1] = c - z + a + zz;
115 }
116 else
117 {
118 q = a - z;
119 zz = q + c + (a - (q + z)) + aa + cc;
120 xh = z + zz;
121
122 if (nonfinite (xh))
123 return xh;
124
125 x.dval[0] = xh;
126 x.dval[1] = z - xh + zz;
127 }
128 return x.ldval;
129 }
130
131 long double
132 __gcc_qsub (double a, double b, double c, double d)
133 {
134 return __gcc_qadd (a, b, -c, -d);
135 }
136
137 long double
138 __gcc_qmul (double a, double b, double c, double d)
139 {
140 longDblUnion z;
141 double t, tau, u, v, w;
142
143 t = a * c; /* Highest order double term. */
144
145 if (unlikely (t == 0) /* Preserve -0. */
146 || nonfinite (t))
147 return t;
148
149 /* Sum terms of two highest orders. */
150
151 /* Use fused multiply-add to get low part of a * c. */
152 asm ("fmsub %0,%1,%2,%3" : "=f"(tau) : "f"(a), "f"(c), "f"(t));
153 v = a*d;
154 w = b*c;
155 tau += v + w; /* Add in other second-order terms. */
156 u = t + tau;
157
158 /* Construct long double result. */
159 if (nonfinite (u))
160 return u;
161 z.dval[0] = u;
162 z.dval[1] = (t - u) + tau;
163 return z.ldval;
164 }
165
166 long double
167 __gcc_qdiv (double a, double b, double c, double d)
168 {
169 longDblUnion z;
170 double s, sigma, t, tau, u, v, w;
171
172 t = a / c; /* highest order double term */
173
174 if (unlikely (t == 0) /* Preserve -0. */
175 || nonfinite (t))
176 return t;
177
178 /* Finite nonzero result requires corrections to the highest order term. */
179
180 s = c * t; /* (s,sigma) = c*t exactly. */
181 w = -(-b + d * t); /* Written to get fnmsub for speed, but not
182 numerically necessary. */
183
184 /* Use fused multiply-add to get low part of c * t. */
185 asm ("fmsub %0,%1,%2,%3" : "=f"(sigma) : "f"(c), "f"(t), "f"(s));
186 v = a - s;
187
188 tau = ((v-sigma)+w)/c; /* Correction to t. */
189 u = t + tau;
190
191 /* Construct long double result. */
192 if (nonfinite (u))
193 return u;
194 z.dval[0] = u;
195 z.dval[1] = (t - u) + tau;
196 return z.ldval;
197 }
198
199 #endif