OSDN Git Service

* config/darwin.c, config/darwin.h, config/freebsd-spec.h,
[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 1961, 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 #define fabs(x) __builtin_fabs(x)
52
53 #define unlikely(x) __builtin_expect ((x), 0)
54
55 /* All these routines actually take two long doubles as parameters,
56    but GCC currently generates poor code when a union is used to turn
57    a long double into a pair of doubles.  */
58
59 extern long double _xlqadd (double, double, double, double);
60 extern long double _xlqsub (double, double, double, double);
61 extern long double _xlqmul (double, double, double, double);
62 extern long double _xlqdiv (double, double, double, double);
63
64 typedef union
65 {
66   long double ldval;
67   double dval[2];
68 } longDblUnion;
69
70 static const double FPKINF = 1.0/0.0;
71
72 /* Add two 'long double' values and return the result.  */
73 long double
74 _xlqadd (double a, double b, double c, double d)
75 {
76   longDblUnion z;
77   double t, tau, u, FPR_zero, FPR_PosInf;
78
79   FPR_zero = 0.0;
80   FPR_PosInf = FPKINF;
81
82   if (unlikely (a != a) || unlikely (c != c)) 
83     return a + c;  /* NaN result.  */
84
85   /* Ordered operands are arranged in order of their magnitudes.  */
86
87   /* Switch inputs if |(c,d)| > |(a,b)|. */
88   if (fabs (c) > fabs (a))
89     {
90       t = a;
91       tau = b;
92       a = c;
93       b = d;
94       c = t;
95       d = tau;
96     }
97
98   /* b <- second largest magnitude double.  */
99   if (fabs (c) > fabs (b))
100     {
101       t = b;
102       b = c;
103       c = t;
104     }
105
106   /* Thanks to commutativity, sum is invariant w.r.t. the next
107      conditional exchange.  */
108   tau = d + c;
109
110   /* Order the smallest magnitude doubles.  */
111   if (fabs (d) > fabs (c))
112     {
113       t = c;
114       c = d;
115       d = t;
116     }
117
118   t = (tau + b) + a;         /* Sum values in ascending magnitude order.  */
119
120   /* Infinite or zero result.  */
121   if (unlikely (t == FPR_zero) || unlikely (fabs (t) == FPR_PosInf))
122     return t;
123
124   /* Usual case.  */
125   tau = (((a-t) + b) + c) + d;
126   u = t + tau;
127   z.dval[0] = u;               /* Final fixup for long double result.  */
128   z.dval[1] = (t - u) + tau;
129   return z.ldval;
130 }
131
132 long double
133 _xlqsub (double a, double b, double c, double d)
134 {
135   return _xlqadd (a, b, -c, -d);
136 }
137
138 long double
139 _xlqmul (double a, double b, double c, double d)
140 {
141   longDblUnion z;
142   double t, tau, u, v, w, FPR_zero, FPR_PosInf;
143   
144   FPR_zero = 0.0;
145   FPR_PosInf = FPKINF;
146
147   t = a * c;                    /* Highest order double term.  */
148
149   if (unlikely (t != t) || unlikely (t == FPR_zero) 
150       || unlikely (fabs (t) == FPR_PosInf))
151     return t;
152
153   /* Finite nonzero result requires summing of terms of two highest
154      orders.    */
155   
156   /* Use fused multiply-add to get low part of a * c.    */
157   asm ("fmsub %0,%1,%2,%3" : "=f"(tau) : "f"(a), "f"(c), "f"(t));
158   v = a*d;
159   w = b*c;
160   tau += v + w;     /* Add in other second-order terms.  */
161   u = t + tau;
162
163   /* Construct long double result.  */
164   z.dval[0] = u;
165   z.dval[1] = (t - u) + tau;
166   return z.ldval;
167 }
168
169 long double
170 _xlqdiv (double a, double b, double c, double d)
171 {
172   longDblUnion z;
173   double s, sigma, t, tau, u, v, w, FPR_zero, FPR_PosInf;
174   
175   FPR_zero = 0.0;
176   FPR_PosInf = FPKINF;
177   
178   t = a / c;                    /* highest order double term */
179   
180   if (unlikely (t != t) || unlikely (t == FPR_zero) 
181       || unlikely (fabs (t) == FPR_PosInf))
182     return t;
183
184   /* Finite nonzero result requires corrections to the highest order term.  */
185
186   s = c * t;                    /* (s,sigma) = c*t exactly.  */
187   w = -(-b + d * t);    /* Written to get fnmsub for speed, but not
188                            numerically necessary.  */
189   
190   /* Use fused multiply-add to get low part of c * t.    */
191   asm ("fmsub %0,%1,%2,%3" : "=f"(sigma) : "f"(c), "f"(t), "f"(s));
192   v = a - s;
193   
194   tau = ((v-sigma)+w)/c;   /* Correction to t.  */
195   u = t + tau;
196
197   /* Construct long double result.  */
198   z.dval[0] = u;
199   z.dval[1] = (t - u) + tau;
200   return z.ldval;
201 }