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