OSDN Git Service

* config/rs6000/rs6000.c (rs6000_file_start): Use PPC405_ERRATUM77.
[pf3gnuchains/gcc-fork.git] / gcc / config / rs6000 / darwin-ldouble.c
1 /* 128-bit long double support routines for Darwin.
2    Copyright (C) 1993, 2003, 2004, 2005 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.)
19
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.
24
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, 51 Franklin Street, Fifth Floor, Boston, MA
28 02110-1301, USA.  */
29
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.  */
39
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.
48
49    This code currently assumes big-endian.  */
50
51 #if !_SOFT_FLOAT && (defined (__MACH__) || defined (__powerpc64__) || defined (_AIX))
52
53 #define fabs(x) __builtin_fabs(x)
54 #define isless(x, y) __builtin_isless (x, y)
55 #define inf() __builtin_inf()
56
57 #define unlikely(x) __builtin_expect ((x), 0)
58
59 #define nonfinite(a) unlikely (! isless (fabs (a), inf ()))
60
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.  */
64
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);
69
70 #if defined __ELF__ && defined SHARED
71 /* Provide definitions of the old symbol names to satisfy 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
86
87 typedef union
88 {
89   long double ldval;
90   double dval[2];
91 } longDblUnion;
92
93 /* Add two 'long double' values and return the result.  */
94 long double
95 __gcc_qadd (double a, double aa, double c, double cc)
96 {
97   longDblUnion x;
98   double z, q, zz, xh;
99
100   z = a + c;
101
102   if (nonfinite (z))
103     {
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;
113     }
114   else
115     {
116       q = a - z;
117       zz = q + c + (a - (q + z)) + aa + cc;
118       xh = z + zz;
119
120       if (nonfinite (xh))
121         return xh;
122
123       x.dval[0] = xh;
124       x.dval[1] = z - xh + zz;
125     }
126   return x.ldval;
127 }
128
129 long double
130 __gcc_qsub (double a, double b, double c, double d)
131 {
132   return __gcc_qadd (a, b, -c, -d);
133 }
134
135 long double
136 __gcc_qmul (double a, double b, double c, double d)
137 {
138   longDblUnion z;
139   double t, tau, u, v, w;
140   
141   t = a * c;                    /* Highest order double term.  */
142
143   if (unlikely (t == 0)         /* Preserve -0.  */
144       || nonfinite (t))
145     return t;
146
147   /* Sum terms of two highest orders. */
148   
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;
155
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;
162 }
163
164 long double
165 __gcc_qdiv (double a, double b, double c, double d)
166 {
167   longDblUnion z;
168   double s, sigma, t, tau, u, v, w;
169   
170   t = a / c;                    /* highest order double term */
171   
172   if (unlikely (t == 0)         /* Preserve -0.  */
173       || nonfinite (t))
174     return t;
175
176   /* Finite nonzero result requires corrections to the highest order term.  */
177
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.  */
181   
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;
185   
186   tau = ((v-sigma)+w)/c;   /* Correction to t.  */
187   u = t + tau;
188
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;
195 }
196
197 #endif