OSDN Git Service

* config/rs6000/darwin.h (SUBTARGET_OPTIONS): Move from here, to...
[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 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, 59 Temple Place - Suite 330, Boston, MA
28 02111-1307, 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: _xlqadd, _xlqsub, _xlqmul, and _xlqdiv.  Double-double
34    algorithms are based on the paper "Doubled-Precision IEEE Standard
35    754 Floating-Point Arithmetic" by W. Kahan, February 26, 1987.  An
36    alternative published reference is "Software for Doubled-Precision
37    Floating-Point Computations", by Seppo Linnainmaa, ACM TOMS vol 7
38    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__))
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 _xlqadd (double, double, double, double);
66 extern long double _xlqsub (double, double, double, double);
67 extern long double _xlqmul (double, double, double, double);
68 extern long double _xlqdiv (double, double, double, double);
69
70 typedef union
71 {
72   long double ldval;
73   double dval[2];
74 } longDblUnion;
75
76 /* Add two 'long double' values and return the result.  */
77 long double
78 _xlqadd (double a, double aa, double c, double cc)
79 {
80   longDblUnion x;
81   double z, q, zz, xh;
82
83   z = a + c;
84
85   if (nonfinite (z))
86     {
87       z = cc + aa + c + a;
88       if (nonfinite (z))
89         return z;
90       x.dval[0] = z;  /* Will always be DBL_MAX.  */
91       zz = aa + cc;
92       if (fabs(a) > fabs(c))
93         x.dval[1] = a - z + c + zz;
94       else
95         x.dval[1] = c - z + a + zz;
96     }
97   else
98     {
99       q = a - z;
100       zz = q + c + (a - (q + z)) + aa + cc;
101       xh = z + zz;
102
103       if (nonfinite (xh))
104         return xh;
105
106       x.dval[0] = xh;
107       x.dval[1] = z - xh + zz;
108     }
109   return x.ldval;
110 }
111
112 long double
113 _xlqsub (double a, double b, double c, double d)
114 {
115   return _xlqadd (a, b, -c, -d);
116 }
117
118 long double
119 _xlqmul (double a, double b, double c, double d)
120 {
121   longDblUnion z;
122   double t, tau, u, v, w;
123   
124   t = a * c;                    /* Highest order double term.  */
125
126   if (unlikely (t == 0)         /* Preserve -0.  */
127       || nonfinite (t))
128     return t;
129
130   /* Sum terms of two highest orders. */
131   
132   /* Use fused multiply-add to get low part of a * c.  */
133   asm ("fmsub %0,%1,%2,%3" : "=f"(tau) : "f"(a), "f"(c), "f"(t));
134   v = a*d;
135   w = b*c;
136   tau += v + w;     /* Add in other second-order terms.  */
137   u = t + tau;
138
139   /* Construct long double result.  */
140   if (nonfinite (u))
141     return u;
142   z.dval[0] = u;
143   z.dval[1] = (t - u) + tau;
144   return z.ldval;
145 }
146
147 long double
148 _xlqdiv (double a, double b, double c, double d)
149 {
150   longDblUnion z;
151   double s, sigma, t, tau, u, v, w;
152   
153   t = a / c;                    /* highest order double term */
154   
155   if (unlikely (t == 0)         /* Preserve -0.  */
156       || nonfinite (t))
157     return t;
158
159   /* Finite nonzero result requires corrections to the highest order term.  */
160
161   s = c * t;                    /* (s,sigma) = c*t exactly.  */
162   w = -(-b + d * t);    /* Written to get fnmsub for speed, but not
163                            numerically necessary.  */
164   
165   /* Use fused multiply-add to get low part of c * t.    */
166   asm ("fmsub %0,%1,%2,%3" : "=f"(sigma) : "f"(c), "f"(t), "f"(s));
167   v = a - s;
168   
169   tau = ((v-sigma)+w)/c;   /* Correction to t.  */
170   u = t + tau;
171
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;
178 }
179
180 #endif