1 /* 128-bit long double support routines for Darwin.
2 Copyright (C) 1993-2014 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 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
26 /* Implementations of floating-point long double basic arithmetic
27 functions called by the IBM C compiler when generating code for
28 PowerPC platforms. In particular, the following functions are
29 implemented: __gcc_qadd, __gcc_qsub, __gcc_qmul, and __gcc_qdiv.
30 Double-double algorithms are based on the paper "Doubled-Precision
31 IEEE Standard 754 Floating-Point Arithmetic" by W. Kahan, February 26,
32 1987. An alternative published reference is "Software for
33 Doubled-Precision Floating-Point Computations", by Seppo Linnainmaa,
34 ACM TOMS vol 7 no 3, September 1981, pages 272-283. */
36 /* Each long double is made up of two IEEE doubles. The value of the
37 long double is the sum of the values of the two parts. The most
38 significant part is required to be the value of the long double
39 rounded to the nearest double, as specified by IEEE. For Inf
40 values, the least significant part is required to be one of +0.0 or
41 -0.0. No other requirements are made; so, for example, 1.0 may be
42 represented as (1.0, +0.0) or (1.0, -0.0), and the low part of a
45 This code currently assumes the most significant double is in
46 the lower numbered register or lower addressed memory. */
48 #if defined (__MACH__) || defined (__powerpc__) || defined (_AIX)
50 #define fabs(x) __builtin_fabs(x)
51 #define isless(x, y) __builtin_isless (x, y)
52 #define inf() __builtin_inf()
54 #define unlikely(x) __builtin_expect ((x), 0)
56 #define nonfinite(a) unlikely (! isless (fabs (a), inf ()))
58 /* Define ALIASNAME as a strong alias for NAME. */
59 # define strong_alias(name, aliasname) _strong_alias(name, aliasname)
60 # define _strong_alias(name, aliasname) \
61 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
63 /* All these routines actually take two long doubles as parameters,
64 but GCC currently generates poor code when a union is used to turn
65 a long double into a pair of doubles. */
67 long double __gcc_qadd (double, double, double, double);
68 long double __gcc_qsub (double, double, double, double);
69 long double __gcc_qmul (double, double, double, double);
70 long double __gcc_qdiv (double, double, double, double);
72 #if defined __ELF__ && defined SHARED \
73 && (defined __powerpc64__ || !(defined __linux__ || defined __gnu_hurd__))
74 /* Provide definitions of the old symbol names to satisfy apps and
75 shared libs built against an older libgcc. To access the _xlq
76 symbols an explicit version reference is needed, so these won't
77 satisfy an unadorned reference like _xlqadd. If dot symbols are
78 not needed, the assembler will remove the aliases from the symbol
80 __asm__ (".symver __gcc_qadd,_xlqadd@GCC_3.4\n\t"
81 ".symver __gcc_qsub,_xlqsub@GCC_3.4\n\t"
82 ".symver __gcc_qmul,_xlqmul@GCC_3.4\n\t"
83 ".symver __gcc_qdiv,_xlqdiv@GCC_3.4\n\t"
84 ".symver .__gcc_qadd,._xlqadd@GCC_3.4\n\t"
85 ".symver .__gcc_qsub,._xlqsub@GCC_3.4\n\t"
86 ".symver .__gcc_qmul,._xlqmul@GCC_3.4\n\t"
87 ".symver .__gcc_qdiv,._xlqdiv@GCC_3.4");
90 /* Combine two 'double' values into one 'long double' and return the result. */
91 static inline long double
92 pack_ldouble (double dh
, double dl
)
94 #if defined (__LONG_DOUBLE_128__) \
95 && !(defined (_SOFT_FLOAT) || defined (__NO_FPRS__))
96 return __builtin_pack_longdouble (dh
, dl
);
109 /* Add two 'long double' values and return the result. */
111 __gcc_qadd (double a
, double aa
, double c
, double cc
)
113 double xh
, xl
, z
, q
, zz
;
119 if (fabs (z
) != inf())
124 xh
= z
; /* Will always be DBL_MAX. */
126 if (fabs(a
) > fabs(c
))
134 zz
= q
+ c
+ (a
- (q
+ z
)) + aa
+ cc
;
136 /* Keep -0 result. */
146 return pack_ldouble (xh
, xl
);
150 __gcc_qsub (double a
, double b
, double c
, double d
)
152 return __gcc_qadd (a
, b
, -c
, -d
);
156 static double fmsub (double, double, double);
160 __gcc_qmul (double a
, double b
, double c
, double d
)
162 double xh
, xl
, t
, tau
, u
, v
, w
;
164 t
= a
* c
; /* Highest order double term. */
166 if (unlikely (t
== 0) /* Preserve -0. */
170 /* Sum terms of two highest orders. */
172 /* Use fused multiply-add to get low part of a * c. */
174 asm ("fmsub %0,%1,%2,%3" : "=f"(tau
) : "f"(a
), "f"(c
), "f"(t
));
176 tau
= fmsub (a
, c
, t
);
180 tau
+= v
+ w
; /* Add in other second-order terms. */
183 /* Construct long double result. */
188 return pack_ldouble (xh
, xl
);
192 __gcc_qdiv (double a
, double b
, double c
, double d
)
194 double xh
, xl
, s
, sigma
, t
, tau
, u
, v
, w
;
196 t
= a
/ c
; /* highest order double term */
198 if (unlikely (t
== 0) /* Preserve -0. */
202 /* Finite nonzero result requires corrections to the highest order
203 term. These corrections require the low part of c * t to be
204 exactly represented in double. */
205 if (fabs (a
) <= 0x1p
-969)
213 s
= c
* t
; /* (s,sigma) = c*t exactly. */
214 w
= -(-b
+ d
* t
); /* Written to get fnmsub for speed, but not
215 numerically necessary. */
217 /* Use fused multiply-add to get low part of c * t. */
219 asm ("fmsub %0,%1,%2,%3" : "=f"(sigma
) : "f"(c
), "f"(t
), "f"(s
));
221 sigma
= fmsub (c
, t
, s
);
225 tau
= ((v
-sigma
)+w
)/c
; /* Correction to t. */
228 /* Construct long double result. */
233 return pack_ldouble (xh
, xl
);
236 #if defined (_SOFT_DOUBLE) && defined (__LONG_DOUBLE_128__)
238 long double __gcc_qneg (double, double);
239 int __gcc_qeq (double, double, double, double);
240 int __gcc_qne (double, double, double, double);
241 int __gcc_qge (double, double, double, double);
242 int __gcc_qle (double, double, double, double);
243 long double __gcc_stoq (float);
244 long double __gcc_dtoq (double);
245 float __gcc_qtos (double, double);
246 double __gcc_qtod (double, double);
247 int __gcc_qtoi (double, double);
248 unsigned int __gcc_qtou (double, double);
249 long double __gcc_itoq (int);
250 long double __gcc_utoq (unsigned int);
252 extern int __eqdf2 (double, double);
253 extern int __ledf2 (double, double);
254 extern int __gedf2 (double, double);
256 /* Negate 'long double' value and return the result. */
258 __gcc_qneg (double a
, double aa
)
260 return pack_ldouble (-a
, -aa
);
263 /* Compare two 'long double' values for equality. */
265 __gcc_qeq (double a
, double aa
, double c
, double cc
)
267 if (__eqdf2 (a
, c
) == 0)
268 return __eqdf2 (aa
, cc
);
272 strong_alias (__gcc_qeq
, __gcc_qne
);
274 /* Compare two 'long double' values for less than or equal. */
276 __gcc_qle (double a
, double aa
, double c
, double cc
)
278 if (__eqdf2 (a
, c
) == 0)
279 return __ledf2 (aa
, cc
);
280 return __ledf2 (a
, c
);
283 strong_alias (__gcc_qle
, __gcc_qlt
);
285 /* Compare two 'long double' values for greater than or equal. */
287 __gcc_qge (double a
, double aa
, double c
, double cc
)
289 if (__eqdf2 (a
, c
) == 0)
290 return __gedf2 (aa
, cc
);
291 return __gedf2 (a
, c
);
294 strong_alias (__gcc_qge
, __gcc_qgt
);
296 /* Convert single to long double. */
300 return pack_ldouble ((double) a
, 0.0);
303 /* Convert double to long double. */
305 __gcc_dtoq (double a
)
307 return pack_ldouble (a
, 0.0);
310 /* Convert long double to single. */
312 __gcc_qtos (double a
, double aa
__attribute__ ((__unused__
)))
317 /* Convert long double to double. */
319 __gcc_qtod (double a
, double aa
__attribute__ ((__unused__
)))
324 /* Convert long double to int. */
326 __gcc_qtoi (double a
, double aa
)
332 /* Convert long double to unsigned int. */
334 __gcc_qtou (double a
, double aa
)
337 return (unsigned int) z
;
340 /* Convert int to long double. */
344 return __gcc_dtoq ((double) a
);
347 /* Convert unsigned int to long double. */
349 __gcc_utoq (unsigned int a
)
351 return __gcc_dtoq ((double) a
);
358 int __gcc_qunord (double, double, double, double);
360 extern int __eqdf2 (double, double);
361 extern int __unorddf2 (double, double);
363 /* Compare two 'long double' values for unordered. */
365 __gcc_qunord (double a
, double aa
, double c
, double cc
)
367 if (__eqdf2 (a
, c
) == 0)
368 return __unorddf2 (aa
, cc
);
369 return __unorddf2 (a
, c
);
372 #include "soft-fp/soft-fp.h"
373 #include "soft-fp/double.h"
374 #include "soft-fp/quad.h"
376 /* Compute floating point multiply-subtract with higher (quad) precision. */
378 fmsub (double a
, double b
, double c
)
391 long double u
, x
, y
, z
;
394 FP_UNPACK_RAW_D (A
, a
);
395 FP_UNPACK_RAW_D (B
, b
);
396 FP_UNPACK_RAW_D (C
, c
);
398 /* Extend double to quad. */
399 #if (2 * _FP_W_TYPE_SIZE) < _FP_FRACBITS_Q
400 FP_EXTEND(Q
,D
,4,2,X
,A
);
401 FP_EXTEND(Q
,D
,4,2,Y
,B
);
402 FP_EXTEND(Q
,D
,4,2,Z
,C
);
404 FP_EXTEND(Q
,D
,2,1,X
,A
);
405 FP_EXTEND(Q
,D
,2,1,Y
,B
);
406 FP_EXTEND(Q
,D
,2,1,Z
,C
);
411 FP_HANDLE_EXCEPTIONS
;
419 FP_HANDLE_EXCEPTIONS
;
423 FP_UNPACK_SEMIRAW_Q(U
,u
);
424 FP_UNPACK_SEMIRAW_Q(Z
,z
);
427 /* Truncate quad to double. */
428 #if (2 * _FP_W_TYPE_SIZE) < _FP_FRACBITS_Q
429 V_f
[3] &= 0x0007ffff;
430 FP_TRUNC(D
,Q
,2,4,R
,V
);
432 V_f1
&= 0x0007ffffffffffffL
;
433 FP_TRUNC(D
,Q
,1,2,R
,V
);
435 FP_PACK_SEMIRAW_D(r
,R
);
436 FP_HANDLE_EXCEPTIONS
;