* config/rs6000/ibm-ldouble.c (typedef union longDblUnion): Delete.
[official-gcc.git] / libgcc / config / rs6000 / ibm-ldouble.c
blob51d58cdae9d33cd5e9d7a8e5c2f5b4dd412b39cc
1 /* 128-bit long double support routines for Darwin.
2 Copyright (C) 1993-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
26 /* Implementations of floating-point long double basic arithmetic
27 functions called by the IBM C compiler when generating code for
28 PowerPC platforms. In particular, the following functions are
29 implemented: __gcc_qadd, __gcc_qsub, __gcc_qmul, and __gcc_qdiv.
30 Double-double algorithms are based on the paper "Doubled-Precision
31 IEEE Standard 754 Floating-Point Arithmetic" by W. Kahan, February 26,
32 1987. An alternative published reference is "Software for
33 Doubled-Precision Floating-Point Computations", by Seppo Linnainmaa,
34 ACM TOMS vol 7 no 3, September 1981, pages 272-283. */
36 /* Each long double is made up of two IEEE doubles. The value of the
37 long double is the sum of the values of the two parts. The most
38 significant part is required to be the value of the long double
39 rounded to the nearest double, as specified by IEEE. For Inf
40 values, the least significant part is required to be one of +0.0 or
41 -0.0. No other requirements are made; so, for example, 1.0 may be
42 represented as (1.0, +0.0) or (1.0, -0.0), and the low part of a
43 NaN is don't-care.
45 This code currently assumes the most significant double is in
46 the lower numbered register or lower addressed memory. */
48 #if defined (__MACH__) || defined (__powerpc__) || defined (_AIX)
50 #define fabs(x) __builtin_fabs(x)
51 #define isless(x, y) __builtin_isless (x, y)
52 #define inf() __builtin_inf()
54 #define unlikely(x) __builtin_expect ((x), 0)
56 #define nonfinite(a) unlikely (! isless (fabs (a), inf ()))
58 /* Define ALIASNAME as a strong alias for NAME. */
59 # define strong_alias(name, aliasname) _strong_alias(name, aliasname)
60 # define _strong_alias(name, aliasname) \
61 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
63 /* All these routines actually take two long doubles as parameters,
64 but GCC currently generates poor code when a union is used to turn
65 a long double into a pair of doubles. */
67 long double __gcc_qadd (double, double, double, double);
68 long double __gcc_qsub (double, double, double, double);
69 long double __gcc_qmul (double, double, double, double);
70 long double __gcc_qdiv (double, double, double, double);
72 #if defined __ELF__ && defined SHARED \
73 && (defined __powerpc64__ || !(defined __linux__ || defined __gnu_hurd__))
74 /* Provide definitions of the old symbol names to satisfy apps and
75 shared libs built against an older libgcc. To access the _xlq
76 symbols an explicit version reference is needed, so these won't
77 satisfy an unadorned reference like _xlqadd. If dot symbols are
78 not needed, the assembler will remove the aliases from the symbol
79 table. */
80 __asm__ (".symver __gcc_qadd,_xlqadd@GCC_3.4\n\t"
81 ".symver __gcc_qsub,_xlqsub@GCC_3.4\n\t"
82 ".symver __gcc_qmul,_xlqmul@GCC_3.4\n\t"
83 ".symver __gcc_qdiv,_xlqdiv@GCC_3.4\n\t"
84 ".symver .__gcc_qadd,._xlqadd@GCC_3.4\n\t"
85 ".symver .__gcc_qsub,._xlqsub@GCC_3.4\n\t"
86 ".symver .__gcc_qmul,._xlqmul@GCC_3.4\n\t"
87 ".symver .__gcc_qdiv,._xlqdiv@GCC_3.4");
88 #endif
90 /* Combine two 'double' values into one 'long double' and return the result. */
91 static inline long double
92 pack_ldouble (double dh, double dl)
94 #if defined (__LONG_DOUBLE_128__) \
95 && !(defined (_SOFT_FLOAT) || defined (__NO_FPRS__))
96 return __builtin_pack_longdouble (dh, dl);
97 #else
98 union
100 long double ldval;
101 double dval[2];
102 } x;
103 x.dval[0] = dh;
104 x.dval[1] = dl;
105 return x.ldval;
106 #endif
109 /* Add two 'long double' values and return the result. */
110 long double
111 __gcc_qadd (double a, double aa, double c, double cc)
113 double xh, xl, z, q, zz;
115 z = a + c;
117 if (nonfinite (z))
119 if (fabs (z) != inf())
120 return z;
121 z = cc + aa + c + a;
122 if (nonfinite (z))
123 return z;
124 xh = z; /* Will always be DBL_MAX. */
125 zz = aa + cc;
126 if (fabs(a) > fabs(c))
127 xl = a - z + c + zz;
128 else
129 xl = c - z + a + zz;
131 else
133 q = a - z;
134 zz = q + c + (a - (q + z)) + aa + cc;
136 /* Keep -0 result. */
137 if (zz == 0.0)
138 return z;
140 xh = z + zz;
141 if (nonfinite (xh))
142 return xh;
144 xl = z - xh + zz;
146 return pack_ldouble (xh, xl);
149 long double
150 __gcc_qsub (double a, double b, double c, double d)
152 return __gcc_qadd (a, b, -c, -d);
155 #ifdef __NO_FPRS__
156 static double fmsub (double, double, double);
157 #endif
159 long double
160 __gcc_qmul (double a, double b, double c, double d)
162 double xh, xl, t, tau, u, v, w;
164 t = a * c; /* Highest order double term. */
166 if (unlikely (t == 0) /* Preserve -0. */
167 || nonfinite (t))
168 return t;
170 /* Sum terms of two highest orders. */
172 /* Use fused multiply-add to get low part of a * c. */
173 #ifndef __NO_FPRS__
174 asm ("fmsub %0,%1,%2,%3" : "=f"(tau) : "f"(a), "f"(c), "f"(t));
175 #else
176 tau = fmsub (a, c, t);
177 #endif
178 v = a*d;
179 w = b*c;
180 tau += v + w; /* Add in other second-order terms. */
181 u = t + tau;
183 /* Construct long double result. */
184 if (nonfinite (u))
185 return u;
186 xh = u;
187 xl = (t - u) + tau;
188 return pack_ldouble (xh, xl);
191 long double
192 __gcc_qdiv (double a, double b, double c, double d)
194 double xh, xl, s, sigma, t, tau, u, v, w;
196 t = a / c; /* highest order double term */
198 if (unlikely (t == 0) /* Preserve -0. */
199 || nonfinite (t))
200 return t;
202 /* Finite nonzero result requires corrections to the highest order
203 term. These corrections require the low part of c * t to be
204 exactly represented in double. */
205 if (fabs (a) <= 0x1p-969)
207 a *= 0x1p106;
208 b *= 0x1p106;
209 c *= 0x1p106;
210 d *= 0x1p106;
213 s = c * t; /* (s,sigma) = c*t exactly. */
214 w = -(-b + d * t); /* Written to get fnmsub for speed, but not
215 numerically necessary. */
217 /* Use fused multiply-add to get low part of c * t. */
218 #ifndef __NO_FPRS__
219 asm ("fmsub %0,%1,%2,%3" : "=f"(sigma) : "f"(c), "f"(t), "f"(s));
220 #else
221 sigma = fmsub (c, t, s);
222 #endif
223 v = a - s;
225 tau = ((v-sigma)+w)/c; /* Correction to t. */
226 u = t + tau;
228 /* Construct long double result. */
229 if (nonfinite (u))
230 return u;
231 xh = u;
232 xl = (t - u) + tau;
233 return pack_ldouble (xh, xl);
236 #if defined (_SOFT_DOUBLE) && defined (__LONG_DOUBLE_128__)
238 long double __gcc_qneg (double, double);
239 int __gcc_qeq (double, double, double, double);
240 int __gcc_qne (double, double, double, double);
241 int __gcc_qge (double, double, double, double);
242 int __gcc_qle (double, double, double, double);
243 long double __gcc_stoq (float);
244 long double __gcc_dtoq (double);
245 float __gcc_qtos (double, double);
246 double __gcc_qtod (double, double);
247 int __gcc_qtoi (double, double);
248 unsigned int __gcc_qtou (double, double);
249 long double __gcc_itoq (int);
250 long double __gcc_utoq (unsigned int);
252 extern int __eqdf2 (double, double);
253 extern int __ledf2 (double, double);
254 extern int __gedf2 (double, double);
256 /* Negate 'long double' value and return the result. */
257 long double
258 __gcc_qneg (double a, double aa)
260 return pack_ldouble (-a, -aa);
263 /* Compare two 'long double' values for equality. */
265 __gcc_qeq (double a, double aa, double c, double cc)
267 if (__eqdf2 (a, c) == 0)
268 return __eqdf2 (aa, cc);
269 return 1;
272 strong_alias (__gcc_qeq, __gcc_qne);
274 /* Compare two 'long double' values for less than or equal. */
276 __gcc_qle (double a, double aa, double c, double cc)
278 if (__eqdf2 (a, c) == 0)
279 return __ledf2 (aa, cc);
280 return __ledf2 (a, c);
283 strong_alias (__gcc_qle, __gcc_qlt);
285 /* Compare two 'long double' values for greater than or equal. */
287 __gcc_qge (double a, double aa, double c, double cc)
289 if (__eqdf2 (a, c) == 0)
290 return __gedf2 (aa, cc);
291 return __gedf2 (a, c);
294 strong_alias (__gcc_qge, __gcc_qgt);
296 /* Convert single to long double. */
297 long double
298 __gcc_stoq (float a)
300 return pack_ldouble ((double) a, 0.0);
303 /* Convert double to long double. */
304 long double
305 __gcc_dtoq (double a)
307 return pack_ldouble (a, 0.0);
310 /* Convert long double to single. */
311 float
312 __gcc_qtos (double a, double aa __attribute__ ((__unused__)))
314 return (float) a;
317 /* Convert long double to double. */
318 double
319 __gcc_qtod (double a, double aa __attribute__ ((__unused__)))
321 return a;
324 /* Convert long double to int. */
326 __gcc_qtoi (double a, double aa)
328 double z = a + aa;
329 return (int) z;
332 /* Convert long double to unsigned int. */
333 unsigned int
334 __gcc_qtou (double a, double aa)
336 double z = a + aa;
337 return (unsigned int) z;
340 /* Convert int to long double. */
341 long double
342 __gcc_itoq (int a)
344 return __gcc_dtoq ((double) a);
347 /* Convert unsigned int to long double. */
348 long double
349 __gcc_utoq (unsigned int a)
351 return __gcc_dtoq ((double) a);
354 #endif
356 #ifdef __NO_FPRS__
358 int __gcc_qunord (double, double, double, double);
360 extern int __eqdf2 (double, double);
361 extern int __unorddf2 (double, double);
363 /* Compare two 'long double' values for unordered. */
365 __gcc_qunord (double a, double aa, double c, double cc)
367 if (__eqdf2 (a, c) == 0)
368 return __unorddf2 (aa, cc);
369 return __unorddf2 (a, c);
372 #include "soft-fp/soft-fp.h"
373 #include "soft-fp/double.h"
374 #include "soft-fp/quad.h"
376 /* Compute floating point multiply-subtract with higher (quad) precision. */
377 static double
378 fmsub (double a, double b, double c)
380 FP_DECL_EX;
381 FP_DECL_D(A);
382 FP_DECL_D(B);
383 FP_DECL_D(C);
384 FP_DECL_Q(X);
385 FP_DECL_Q(Y);
386 FP_DECL_Q(Z);
387 FP_DECL_Q(U);
388 FP_DECL_Q(V);
389 FP_DECL_D(R);
390 double r;
391 long double u, x, y, z;
393 FP_INIT_ROUNDMODE;
394 FP_UNPACK_RAW_D (A, a);
395 FP_UNPACK_RAW_D (B, b);
396 FP_UNPACK_RAW_D (C, c);
398 /* Extend double to quad. */
399 #if (2 * _FP_W_TYPE_SIZE) < _FP_FRACBITS_Q
400 FP_EXTEND(Q,D,4,2,X,A);
401 FP_EXTEND(Q,D,4,2,Y,B);
402 FP_EXTEND(Q,D,4,2,Z,C);
403 #else
404 FP_EXTEND(Q,D,2,1,X,A);
405 FP_EXTEND(Q,D,2,1,Y,B);
406 FP_EXTEND(Q,D,2,1,Z,C);
407 #endif
408 FP_PACK_RAW_Q(x,X);
409 FP_PACK_RAW_Q(y,Y);
410 FP_PACK_RAW_Q(z,Z);
411 FP_HANDLE_EXCEPTIONS;
413 /* Multiply. */
414 FP_INIT_ROUNDMODE;
415 FP_UNPACK_Q(X,x);
416 FP_UNPACK_Q(Y,y);
417 FP_MUL_Q(U,X,Y);
418 FP_PACK_Q(u,U);
419 FP_HANDLE_EXCEPTIONS;
421 /* Subtract. */
422 FP_INIT_ROUNDMODE;
423 FP_UNPACK_SEMIRAW_Q(U,u);
424 FP_UNPACK_SEMIRAW_Q(Z,z);
425 FP_SUB_Q(V,U,Z);
427 /* Truncate quad to double. */
428 #if (2 * _FP_W_TYPE_SIZE) < _FP_FRACBITS_Q
429 V_f[3] &= 0x0007ffff;
430 FP_TRUNC(D,Q,2,4,R,V);
431 #else
432 V_f1 &= 0x0007ffffffffffffL;
433 FP_TRUNC(D,Q,1,2,R,V);
434 #endif
435 FP_PACK_SEMIRAW_D(r,R);
436 FP_HANDLE_EXCEPTIONS;
438 return r;
441 #endif
443 #endif