1 /* 128-bit long double support routines for Darwin.
2 Copyright (C) 1993, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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
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
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
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
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. */
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
50 This code currently assumes big-endian. */
52 #if (!defined (__LITTLE_ENDIAN__) \
53 && (defined (__MACH__) || defined (__powerpc__) || defined (_AIX)))
55 #define fabs(x) __builtin_fabs(x)
56 #define isless(x, y) __builtin_isless (x, y)
57 #define inf() __builtin_inf()
59 #define unlikely(x) __builtin_expect ((x), 0)
61 #define nonfinite(a) unlikely (! isless (fabs (a), inf ()))
63 /* Define ALIASNAME as a strong alias for NAME. */
64 # define strong_alias(name, aliasname) _strong_alias(name, aliasname)
65 # define _strong_alias(name, aliasname) \
66 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
68 /* All these routines actually take two long doubles as parameters,
69 but GCC currently generates poor code when a union is used to turn
70 a long double into a pair of doubles. */
72 long double __gcc_qadd (double, double, double, double);
73 long double __gcc_qsub (double, double, double, double);
74 long double __gcc_qmul (double, double, double, double);
75 long double __gcc_qdiv (double, double, double, double);
77 #if defined __ELF__ && defined SHARED \
78 && (defined __powerpc64__ || !(defined __linux__ || defined __gnu_hurd__))
79 /* Provide definitions of the old symbol names to satisfy apps and
80 shared libs built against an older libgcc. To access the _xlq
81 symbols an explicit version reference is needed, so these won't
82 satisfy an unadorned reference like _xlqadd. If dot symbols are
83 not needed, the assembler will remove the aliases from the symbol
85 __asm__ (".symver __gcc_qadd,_xlqadd@GCC_3.4\n\t"
86 ".symver __gcc_qsub,_xlqsub@GCC_3.4\n\t"
87 ".symver __gcc_qmul,_xlqmul@GCC_3.4\n\t"
88 ".symver __gcc_qdiv,_xlqdiv@GCC_3.4\n\t"
89 ".symver .__gcc_qadd,._xlqadd@GCC_3.4\n\t"
90 ".symver .__gcc_qsub,._xlqsub@GCC_3.4\n\t"
91 ".symver .__gcc_qmul,._xlqmul@GCC_3.4\n\t"
92 ".symver .__gcc_qdiv,._xlqdiv@GCC_3.4");
101 /* Add two 'long double' values and return the result. */
103 __gcc_qadd (double a, double aa, double c, double cc)
115 x.dval[0] = z; /* Will always be DBL_MAX. */
117 if (fabs(a) > fabs(c))
118 x.dval[1] = a - z + c + zz;
120 x.dval[1] = c - z + a + zz;
125 zz = q + c + (a - (q + z)) + aa + cc;
127 /* Keep -0 result. */
136 x.dval[1] = z - xh + zz;
142 __gcc_qsub (double a, double b, double c, double d)
144 return __gcc_qadd (a, b, -c, -d);
148 static double fmsub (double, double, double);
152 __gcc_qmul (double a, double b, double c, double d)
155 double t, tau, u, v, w;
157 t = a * c; /* Highest order double term. */
159 if (unlikely (t == 0) /* Preserve -0. */
163 /* Sum terms of two highest orders. */
165 /* Use fused multiply-add to get low part of a * c. */
167 asm ("fmsub %0,%1,%2,%3" : "=f"(tau) : "f"(a), "f"(c), "f"(t));
169 tau = fmsub (a, c, t);
173 tau += v + w; /* Add in other second-order terms. */
176 /* Construct long double result. */
180 z.dval[1] = (t - u) + tau;
185 __gcc_qdiv (double a, double b, double c, double d)
188 double s, sigma, t, tau, u, v, w;
190 t = a / c; /* highest order double term */
192 if (unlikely (t == 0) /* Preserve -0. */
196 /* Finite nonzero result requires corrections to the highest order term. */
198 s = c * t; /* (s,sigma) = c*t exactly. */
199 w = -(-b + d * t); /* Written to get fnmsub for speed, but not
200 numerically necessary. */
202 /* Use fused multiply-add to get low part of c * t. */
204 asm ("fmsub %0,%1,%2,%3" : "=f"(sigma) : "f"(c), "f"(t), "f"(s));
206 sigma = fmsub (c, t, s);
210 tau = ((v-sigma)+w)/c; /* Correction to t. */
213 /* Construct long double result. */
217 z.dval[1] = (t - u) + tau;
221 #if defined (_SOFT_DOUBLE) && defined (__LONG_DOUBLE_128__)
223 long double __gcc_qneg (double, double);
224 int __gcc_qeq (double, double, double, double);
225 int __gcc_qne (double, double, double, double);
226 int __gcc_qge (double, double, double, double);
227 int __gcc_qle (double, double, double, double);
228 long double __gcc_stoq (float);
229 long double __gcc_dtoq (double);
230 float __gcc_qtos (double, double);
231 double __gcc_qtod (double, double);
232 int __gcc_qtoi (double, double);
233 unsigned int __gcc_qtou (double, double);
234 long double __gcc_itoq (int);
235 long double __gcc_utoq (unsigned int);
237 extern int __eqdf2 (double, double);
238 extern int __ledf2 (double, double);
239 extern int __gedf2 (double, double);
241 /* Negate 'long double' value and return the result. */
243 __gcc_qneg (double a, double aa)
252 /* Compare two 'long double' values for equality. */
254 __gcc_qeq (double a, double aa, double c, double cc)
256 if (__eqdf2 (a, c) == 0)
257 return __eqdf2 (aa, cc);
261 strong_alias (__gcc_qeq, __gcc_qne);
263 /* Compare two 'long double' values for less than or equal. */
265 __gcc_qle (double a, double aa, double c, double cc)
267 if (__eqdf2 (a, c) == 0)
268 return __ledf2 (aa, cc);
269 return __ledf2 (a, c);
272 strong_alias (__gcc_qle, __gcc_qlt);
274 /* Compare two 'long double' values for greater than or equal. */
276 __gcc_qge (double a, double aa, double c, double cc)
278 if (__eqdf2 (a, c) == 0)
279 return __gedf2 (aa, cc);
280 return __gedf2 (a, c);
283 strong_alias (__gcc_qge, __gcc_qgt);
285 /* Convert single to long double. */
291 x.dval[0] = (double) a;
297 /* Convert double to long double. */
299 __gcc_dtoq (double a)
309 /* Convert long double to single. */
311 __gcc_qtos (double a, double aa __attribute__ ((__unused__)))
316 /* Convert long double to double. */
318 __gcc_qtod (double a, double aa __attribute__ ((__unused__)))
323 /* Convert long double to int. */
325 __gcc_qtoi (double a, double aa)
331 /* Convert long double to unsigned int. */
333 __gcc_qtou (double a, double aa)
336 return (unsigned int) z;
339 /* Convert int to long double. */
343 return __gcc_dtoq ((double) a);
346 /* Convert unsigned int to long double. */
348 __gcc_utoq (unsigned int a)
350 return __gcc_dtoq ((double) a);
357 int __gcc_qunord (double, double, double, double);
359 extern int __eqdf2 (double, double);
360 extern int __unorddf2 (double, double);
362 /* Compare two 'long double' values for unordered. */
364 __gcc_qunord (double a, double aa, double c, double cc)
366 if (__eqdf2 (a, c) == 0)
367 return __unorddf2 (aa, cc);
368 return __unorddf2 (a, c);
371 #include "config/soft-fp/soft-fp.h"
372 #include "config/soft-fp/double.h"
373 #include "config/soft-fp/quad.h"
375 /* Compute floating point multiply-subtract with higher (quad) precision. */
377 fmsub (double a, double b, double c)
390 long double u, v, x, y, z;
393 FP_UNPACK_RAW_D (A, a);
394 FP_UNPACK_RAW_D (B, b);
395 FP_UNPACK_RAW_D (C, c);
397 /* Extend double to quad. */
398 #if (2 * _FP_W_TYPE_SIZE) < _FP_FRACBITS_Q
399 FP_EXTEND(Q,D,4,2,X,A);
400 FP_EXTEND(Q,D,4,2,Y,B);
401 FP_EXTEND(Q,D,4,2,Z,C);
403 FP_EXTEND(Q,D,2,1,X,A);
404 FP_EXTEND(Q,D,2,1,Y,B);
405 FP_EXTEND(Q,D,2,1,Z,C);
410 FP_HANDLE_EXCEPTIONS;
418 FP_HANDLE_EXCEPTIONS;
422 FP_UNPACK_SEMIRAW_Q(U,u);
423 FP_UNPACK_SEMIRAW_Q(Z,z);
426 /* Truncate quad to double. */
427 #if (2 * _FP_W_TYPE_SIZE) < _FP_FRACBITS_Q
428 V_f[3] &= 0x0007ffff;
429 FP_TRUNC(D,Q,2,4,R,V);
431 V_f1 &= 0x0007ffffffffffffL;
432 FP_TRUNC(D,Q,1,2,R,V);
434 FP_PACK_SEMIRAW_D(r,R);
435 FP_HANDLE_EXCEPTIONS;