Merge from mainline
[official-gcc.git] / gcc / config / rs6000 / darwin-ldouble.c
blob08ff351d52c0a29fe5457bde7831249a1c6d57ed
1 /* 128-bit long double support routines for Darwin.
2 Copyright (C) 1993, 2003, 2004, 2005, 2006
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
10 version.
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.)
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.
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. */
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
48 NaN is don't-care.
50 This code currently assumes big-endian. */
52 #if !_SOFT_FLOAT && (defined (__MACH__) || defined (__powerpc64__) || defined (__powerpc__) || defined (_AIX))
54 #define fabs(x) __builtin_fabs(x)
55 #define isless(x, y) __builtin_isless (x, y)
56 #define inf() __builtin_inf()
58 #define unlikely(x) __builtin_expect ((x), 0)
60 #define nonfinite(a) unlikely (! isless (fabs (a), inf ()))
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. */
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);
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
89 typedef union
91 long double ldval;
92 double dval[2];
93 } longDblUnion;
95 /* Add two 'long double' values and return the result. */
96 long double
97 __gcc_qadd (double a, double aa, double c, double cc)
99 longDblUnion x;
100 double z, q, zz, xh;
102 z = a + c;
104 if (nonfinite (z))
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;
116 else
118 q = a - z;
119 zz = q + c + (a - (q + z)) + aa + cc;
121 /* Keep -0 result. */
122 if (zz == 0.0)
123 return z;
125 xh = z + zz;
126 if (nonfinite (xh))
127 return xh;
129 x.dval[0] = xh;
130 x.dval[1] = z - xh + zz;
132 return x.ldval;
135 long double
136 __gcc_qsub (double a, double b, double c, double d)
138 return __gcc_qadd (a, b, -c, -d);
141 long double
142 __gcc_qmul (double a, double b, double c, double d)
144 longDblUnion z;
145 double t, tau, u, v, w;
147 t = a * c; /* Highest order double term. */
149 if (unlikely (t == 0) /* Preserve -0. */
150 || nonfinite (t))
151 return t;
153 /* Sum terms of two highest orders. */
155 /* Use fused multiply-add to get low part of a * c. */
156 asm ("fmsub %0,%1,%2,%3" : "=f"(tau) : "f"(a), "f"(c), "f"(t));
157 v = a*d;
158 w = b*c;
159 tau += v + w; /* Add in other second-order terms. */
160 u = t + tau;
162 /* Construct long double result. */
163 if (nonfinite (u))
164 return u;
165 z.dval[0] = u;
166 z.dval[1] = (t - u) + tau;
167 return z.ldval;
170 long double
171 __gcc_qdiv (double a, double b, double c, double d)
173 longDblUnion z;
174 double s, sigma, t, tau, u, v, w;
176 t = a / c; /* highest order double term */
178 if (unlikely (t == 0) /* Preserve -0. */
179 || nonfinite (t))
180 return t;
182 /* Finite nonzero result requires corrections to the highest order term. */
184 s = c * t; /* (s,sigma) = c*t exactly. */
185 w = -(-b + d * t); /* Written to get fnmsub for speed, but not
186 numerically necessary. */
188 /* Use fused multiply-add to get low part of c * t. */
189 asm ("fmsub %0,%1,%2,%3" : "=f"(sigma) : "f"(c), "f"(t), "f"(s));
190 v = a - s;
192 tau = ((v-sigma)+w)/c; /* Correction to t. */
193 u = t + tau;
195 /* Construct long double result. */
196 if (nonfinite (u))
197 return u;
198 z.dval[0] = u;
199 z.dval[1] = (t - u) + tau;
200 return z.ldval;
203 #endif