2011-03-21 Daniel Jacobowitz <dan@codesourcery.com>
[official-gcc.git] / gcc / config / rs6000 / darwin-ldouble.c
blobd76c1b1847d9fc3a24138f5554ceaebd1a35eeb3
1 /* 128-bit long double support routines for Darwin.
2 Copyright (C) 1993, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
27 /* Implementations of floating-point long double basic arithmetic
28 functions called by the IBM C compiler when generating code for
29 PowerPC platforms. In particular, the following functions are
30 implemented: __gcc_qadd, __gcc_qsub, __gcc_qmul, and __gcc_qdiv.
31 Double-double algorithms are based on the paper "Doubled-Precision
32 IEEE Standard 754 Floating-Point Arithmetic" by W. Kahan, February 26,
33 1987. An alternative published reference is "Software for
34 Doubled-Precision Floating-Point Computations", by Seppo Linnainmaa,
35 ACM TOMS vol 7 no 3, September 1981, pages 272-283. */
37 /* Each long double is made up of two IEEE doubles. The value of the
38 long double is the sum of the values of the two parts. The most
39 significant part is required to be the value of the long double
40 rounded to the nearest double, as specified by IEEE. For Inf
41 values, the least significant part is required to be one of +0.0 or
42 -0.0. No other requirements are made; so, for example, 1.0 may be
43 represented as (1.0, +0.0) or (1.0, -0.0), and the low part of a
44 NaN is don't-care.
46 This code currently assumes big-endian. */
48 #if (!defined (__LITTLE_ENDIAN__) \
49 && (defined (__MACH__) || defined (__powerpc__) || defined (_AIX)))
51 #define fabs(x) __builtin_fabs(x)
52 #define isless(x, y) __builtin_isless (x, y)
53 #define inf() __builtin_inf()
55 #define unlikely(x) __builtin_expect ((x), 0)
57 #define nonfinite(a) unlikely (! isless (fabs (a), inf ()))
59 /* Define ALIASNAME as a strong alias for NAME. */
60 # define strong_alias(name, aliasname) _strong_alias(name, aliasname)
61 # define _strong_alias(name, aliasname) \
62 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
64 /* All these routines actually take two long doubles as parameters,
65 but GCC currently generates poor code when a union is used to turn
66 a long double into a pair of doubles. */
68 long double __gcc_qadd (double, double, double, double);
69 long double __gcc_qsub (double, double, double, double);
70 long double __gcc_qmul (double, double, double, double);
71 long double __gcc_qdiv (double, double, double, double);
73 #if defined __ELF__ && defined SHARED \
74 && (defined __powerpc64__ || !(defined __linux__ || defined __gnu_hurd__))
75 /* Provide definitions of the old symbol names to satisfy apps and
76 shared libs built against an older libgcc. To access the _xlq
77 symbols an explicit version reference is needed, so these won't
78 satisfy an unadorned reference like _xlqadd. If dot symbols are
79 not needed, the assembler will remove the aliases from the symbol
80 table. */
81 __asm__ (".symver __gcc_qadd,_xlqadd@GCC_3.4\n\t"
82 ".symver __gcc_qsub,_xlqsub@GCC_3.4\n\t"
83 ".symver __gcc_qmul,_xlqmul@GCC_3.4\n\t"
84 ".symver __gcc_qdiv,_xlqdiv@GCC_3.4\n\t"
85 ".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");
89 #endif
91 typedef union
93 long double ldval;
94 double dval[2];
95 } longDblUnion;
97 /* Add two 'long double' values and return the result. */
98 long double
99 __gcc_qadd (double a, double aa, double c, double cc)
101 longDblUnion x;
102 double z, q, zz, xh;
104 z = a + c;
106 if (nonfinite (z))
108 z = cc + aa + c + a;
109 if (nonfinite (z))
110 return z;
111 x.dval[0] = z; /* Will always be DBL_MAX. */
112 zz = aa + cc;
113 if (fabs(a) > fabs(c))
114 x.dval[1] = a - z + c + zz;
115 else
116 x.dval[1] = c - z + a + zz;
118 else
120 q = a - z;
121 zz = q + c + (a - (q + z)) + aa + cc;
123 /* Keep -0 result. */
124 if (zz == 0.0)
125 return z;
127 xh = z + zz;
128 if (nonfinite (xh))
129 return xh;
131 x.dval[0] = xh;
132 x.dval[1] = z - xh + zz;
134 return x.ldval;
137 long double
138 __gcc_qsub (double a, double b, double c, double d)
140 return __gcc_qadd (a, b, -c, -d);
143 #ifdef __NO_FPRS__
144 static double fmsub (double, double, double);
145 #endif
147 long double
148 __gcc_qmul (double a, double b, double c, double d)
150 longDblUnion z;
151 double t, tau, u, v, w;
153 t = a * c; /* Highest order double term. */
155 if (unlikely (t == 0) /* Preserve -0. */
156 || nonfinite (t))
157 return t;
159 /* Sum terms of two highest orders. */
161 /* Use fused multiply-add to get low part of a * c. */
162 #ifndef __NO_FPRS__
163 asm ("fmsub %0,%1,%2,%3" : "=f"(tau) : "f"(a), "f"(c), "f"(t));
164 #else
165 tau = fmsub (a, c, t);
166 #endif
167 v = a*d;
168 w = b*c;
169 tau += v + w; /* Add in other second-order terms. */
170 u = t + tau;
172 /* Construct long double result. */
173 if (nonfinite (u))
174 return u;
175 z.dval[0] = u;
176 z.dval[1] = (t - u) + tau;
177 return z.ldval;
180 long double
181 __gcc_qdiv (double a, double b, double c, double d)
183 longDblUnion z;
184 double s, sigma, t, tau, u, v, w;
186 t = a / c; /* highest order double term */
188 if (unlikely (t == 0) /* Preserve -0. */
189 || nonfinite (t))
190 return t;
192 /* Finite nonzero result requires corrections to the highest order term. */
194 s = c * t; /* (s,sigma) = c*t exactly. */
195 w = -(-b + d * t); /* Written to get fnmsub for speed, but not
196 numerically necessary. */
198 /* Use fused multiply-add to get low part of c * t. */
199 #ifndef __NO_FPRS__
200 asm ("fmsub %0,%1,%2,%3" : "=f"(sigma) : "f"(c), "f"(t), "f"(s));
201 #else
202 sigma = fmsub (c, t, s);
203 #endif
204 v = a - s;
206 tau = ((v-sigma)+w)/c; /* Correction to t. */
207 u = t + tau;
209 /* Construct long double result. */
210 if (nonfinite (u))
211 return u;
212 z.dval[0] = u;
213 z.dval[1] = (t - u) + tau;
214 return z.ldval;
217 #if defined (_SOFT_DOUBLE) && defined (__LONG_DOUBLE_128__)
219 long double __gcc_qneg (double, double);
220 int __gcc_qeq (double, double, double, double);
221 int __gcc_qne (double, double, double, double);
222 int __gcc_qge (double, double, double, double);
223 int __gcc_qle (double, double, double, double);
224 long double __gcc_stoq (float);
225 long double __gcc_dtoq (double);
226 float __gcc_qtos (double, double);
227 double __gcc_qtod (double, double);
228 int __gcc_qtoi (double, double);
229 unsigned int __gcc_qtou (double, double);
230 long double __gcc_itoq (int);
231 long double __gcc_utoq (unsigned int);
233 extern int __eqdf2 (double, double);
234 extern int __ledf2 (double, double);
235 extern int __gedf2 (double, double);
237 /* Negate 'long double' value and return the result. */
238 long double
239 __gcc_qneg (double a, double aa)
241 longDblUnion x;
243 x.dval[0] = -a;
244 x.dval[1] = -aa;
245 return x.ldval;
248 /* Compare two 'long double' values for equality. */
250 __gcc_qeq (double a, double aa, double c, double cc)
252 if (__eqdf2 (a, c) == 0)
253 return __eqdf2 (aa, cc);
254 return 1;
257 strong_alias (__gcc_qeq, __gcc_qne);
259 /* Compare two 'long double' values for less than or equal. */
261 __gcc_qle (double a, double aa, double c, double cc)
263 if (__eqdf2 (a, c) == 0)
264 return __ledf2 (aa, cc);
265 return __ledf2 (a, c);
268 strong_alias (__gcc_qle, __gcc_qlt);
270 /* Compare two 'long double' values for greater than or equal. */
272 __gcc_qge (double a, double aa, double c, double cc)
274 if (__eqdf2 (a, c) == 0)
275 return __gedf2 (aa, cc);
276 return __gedf2 (a, c);
279 strong_alias (__gcc_qge, __gcc_qgt);
281 /* Convert single to long double. */
282 long double
283 __gcc_stoq (float a)
285 longDblUnion x;
287 x.dval[0] = (double) a;
288 x.dval[1] = 0.0;
290 return x.ldval;
293 /* Convert double to long double. */
294 long double
295 __gcc_dtoq (double a)
297 longDblUnion x;
299 x.dval[0] = a;
300 x.dval[1] = 0.0;
302 return x.ldval;
305 /* Convert long double to single. */
306 float
307 __gcc_qtos (double a, double aa __attribute__ ((__unused__)))
309 return (float) a;
312 /* Convert long double to double. */
313 double
314 __gcc_qtod (double a, double aa __attribute__ ((__unused__)))
316 return a;
319 /* Convert long double to int. */
321 __gcc_qtoi (double a, double aa)
323 double z = a + aa;
324 return (int) z;
327 /* Convert long double to unsigned int. */
328 unsigned int
329 __gcc_qtou (double a, double aa)
331 double z = a + aa;
332 return (unsigned int) z;
335 /* Convert int to long double. */
336 long double
337 __gcc_itoq (int a)
339 return __gcc_dtoq ((double) a);
342 /* Convert unsigned int to long double. */
343 long double
344 __gcc_utoq (unsigned int a)
346 return __gcc_dtoq ((double) a);
349 #endif
351 #ifdef __NO_FPRS__
353 int __gcc_qunord (double, double, double, double);
355 extern int __eqdf2 (double, double);
356 extern int __unorddf2 (double, double);
358 /* Compare two 'long double' values for unordered. */
360 __gcc_qunord (double a, double aa, double c, double cc)
362 if (__eqdf2 (a, c) == 0)
363 return __unorddf2 (aa, cc);
364 return __unorddf2 (a, c);
367 #include "config/soft-fp/soft-fp.h"
368 #include "config/soft-fp/double.h"
369 #include "config/soft-fp/quad.h"
371 /* Compute floating point multiply-subtract with higher (quad) precision. */
372 static double
373 fmsub (double a, double b, double c)
375 FP_DECL_EX;
376 FP_DECL_D(A);
377 FP_DECL_D(B);
378 FP_DECL_D(C);
379 FP_DECL_Q(X);
380 FP_DECL_Q(Y);
381 FP_DECL_Q(Z);
382 FP_DECL_Q(U);
383 FP_DECL_Q(V);
384 FP_DECL_D(R);
385 double r;
386 long double u, x, y, z;
388 FP_INIT_ROUNDMODE;
389 FP_UNPACK_RAW_D (A, a);
390 FP_UNPACK_RAW_D (B, b);
391 FP_UNPACK_RAW_D (C, c);
393 /* Extend double to quad. */
394 #if (2 * _FP_W_TYPE_SIZE) < _FP_FRACBITS_Q
395 FP_EXTEND(Q,D,4,2,X,A);
396 FP_EXTEND(Q,D,4,2,Y,B);
397 FP_EXTEND(Q,D,4,2,Z,C);
398 #else
399 FP_EXTEND(Q,D,2,1,X,A);
400 FP_EXTEND(Q,D,2,1,Y,B);
401 FP_EXTEND(Q,D,2,1,Z,C);
402 #endif
403 FP_PACK_RAW_Q(x,X);
404 FP_PACK_RAW_Q(y,Y);
405 FP_PACK_RAW_Q(z,Z);
406 FP_HANDLE_EXCEPTIONS;
408 /* Multiply. */
409 FP_INIT_ROUNDMODE;
410 FP_UNPACK_Q(X,x);
411 FP_UNPACK_Q(Y,y);
412 FP_MUL_Q(U,X,Y);
413 FP_PACK_Q(u,U);
414 FP_HANDLE_EXCEPTIONS;
416 /* Subtract. */
417 FP_INIT_ROUNDMODE;
418 FP_UNPACK_SEMIRAW_Q(U,u);
419 FP_UNPACK_SEMIRAW_Q(Z,z);
420 FP_SUB_Q(V,U,Z);
422 /* Truncate quad to double. */
423 #if (2 * _FP_W_TYPE_SIZE) < _FP_FRACBITS_Q
424 V_f[3] &= 0x0007ffff;
425 FP_TRUNC(D,Q,2,4,R,V);
426 #else
427 V_f1 &= 0x0007ffffffffffffL;
428 FP_TRUNC(D,Q,1,2,R,V);
429 #endif
430 FP_PACK_SEMIRAW_D(r,R);
431 FP_HANDLE_EXCEPTIONS;
433 return r;
436 #endif
438 #endif