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
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
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
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
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");
97 /* Add two 'long double' values and return the result. */
99 __gcc_qadd (double a
, double aa
, double c
, double cc
)
111 x
.dval
[0] = z
; /* Will always be DBL_MAX. */
113 if (fabs(a
) > fabs(c
))
114 x
.dval
[1] = a
- z
+ c
+ zz
;
116 x
.dval
[1] = c
- z
+ a
+ zz
;
121 zz
= q
+ c
+ (a
- (q
+ z
)) + aa
+ cc
;
123 /* Keep -0 result. */
132 x
.dval
[1] = z
- xh
+ zz
;
138 __gcc_qsub (double a
, double b
, double c
, double d
)
140 return __gcc_qadd (a
, b
, -c
, -d
);
144 static double fmsub (double, double, double);
148 __gcc_qmul (double a
, double b
, double c
, double d
)
151 double t
, tau
, u
, v
, w
;
153 t
= a
* c
; /* Highest order double term. */
155 if (unlikely (t
== 0) /* Preserve -0. */
159 /* Sum terms of two highest orders. */
161 /* Use fused multiply-add to get low part of a * c. */
163 asm ("fmsub %0,%1,%2,%3" : "=f"(tau
) : "f"(a
), "f"(c
), "f"(t
));
165 tau
= fmsub (a
, c
, t
);
169 tau
+= v
+ w
; /* Add in other second-order terms. */
172 /* Construct long double result. */
176 z
.dval
[1] = (t
- u
) + tau
;
181 __gcc_qdiv (double a
, double b
, double c
, double d
)
184 double s
, sigma
, t
, tau
, u
, v
, w
;
186 t
= a
/ c
; /* highest order double term */
188 if (unlikely (t
== 0) /* Preserve -0. */
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. */
200 asm ("fmsub %0,%1,%2,%3" : "=f"(sigma
) : "f"(c
), "f"(t
), "f"(s
));
202 sigma
= fmsub (c
, t
, s
);
206 tau
= ((v
-sigma
)+w
)/c
; /* Correction to t. */
209 /* Construct long double result. */
213 z
.dval
[1] = (t
- u
) + tau
;
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. */
239 __gcc_qneg (double a
, double aa
)
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
);
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. */
287 x
.dval
[0] = (double) a
;
293 /* Convert double to long double. */
295 __gcc_dtoq (double a
)
305 /* Convert long double to single. */
307 __gcc_qtos (double a
, double aa
__attribute__ ((__unused__
)))
312 /* Convert long double to double. */
314 __gcc_qtod (double a
, double aa
__attribute__ ((__unused__
)))
319 /* Convert long double to int. */
321 __gcc_qtoi (double a
, double aa
)
327 /* Convert long double to unsigned int. */
329 __gcc_qtou (double a
, double aa
)
332 return (unsigned int) z
;
335 /* Convert int to long double. */
339 return __gcc_dtoq ((double) a
);
342 /* Convert unsigned int to long double. */
344 __gcc_utoq (unsigned int a
)
346 return __gcc_dtoq ((double) a
);
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. */
373 fmsub (double a
, double b
, double c
)
386 long double u
, x
, y
, z
;
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
);
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
);
406 FP_HANDLE_EXCEPTIONS
;
414 FP_HANDLE_EXCEPTIONS
;
418 FP_UNPACK_SEMIRAW_Q(U
,u
);
419 FP_UNPACK_SEMIRAW_Q(Z
,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
);
427 V_f1
&= 0x0007ffffffffffffL
;
428 FP_TRUNC(D
,Q
,1,2,R
,V
);
430 FP_PACK_SEMIRAW_D(r
,R
);
431 FP_HANDLE_EXCEPTIONS
;