2 * Borrowed from GCC 4.2.2 (which still was GPL v2+)
4 /* 128-bit long double support routines for Darwin.
5 Copyright (C) 1993, 2003, 2004, 2005, 2006, 2007
6 Free Software Foundation, Inc.
8 This file is part of GCC.
10 * SPDX-License-Identifier: GPL-2.0+
14 * Implementations of floating-point long double basic arithmetic
15 * functions called by the IBM C compiler when generating code for
16 * PowerPC platforms. In particular, the following functions are
17 * implemented: __gcc_qadd, __gcc_qsub, __gcc_qmul, and __gcc_qdiv.
18 * Double-double algorithms are based on the paper "Doubled-Precision
19 * IEEE Standard 754 Floating-Point Arithmetic" by W. Kahan, February 26,
20 * 1987. An alternative published reference is "Software for
21 * Doubled-Precision Floating-Point Computations", by Seppo Linnainmaa,
22 * ACM TOMS vol 7 no 3, September 1981, pages 272-283.
26 * Each long double is made up of two IEEE doubles. The value of the
27 * long double is the sum of the values of the two parts. The most
28 * significant part is required to be the value of the long double
29 * rounded to the nearest double, as specified by IEEE. For Inf
30 * values, the least significant part is required to be one of +0.0 or
31 * -0.0. No other requirements are made; so, for example, 1.0 may be
32 * represented as (1.0, +0.0) or (1.0, -0.0), and the low part of a
35 * This code currently assumes big-endian.
38 #define fabs(x) __builtin_fabs(x)
39 #define isless(x, y) __builtin_isless(x, y)
40 #define inf() __builtin_inf()
41 #define unlikely(x) __builtin_expect((x), 0)
42 #define nonfinite(a) unlikely(!isless(fabs(a), inf()))
49 /* Add two 'long double' values and return the result. */
50 long double __gcc_qadd(double a, double aa, double c, double cc)
61 x.dval[0] = z; /* Will always be DBL_MAX. */
63 if (fabs(a) > fabs(c))
64 x.dval[1] = a - z + c + zz;
66 x.dval[1] = c - z + a + zz;
69 zz = q + c + (a - (q + z)) + aa + cc;
80 x.dval[1] = z - xh + zz;
85 long double __gcc_qsub(double a, double b, double c, double d)
87 return __gcc_qadd(a, b, -c, -d);
90 long double __gcc_qmul(double a, double b, double c, double d)
93 double t, tau, u, v, w;
95 t = a * c; /* Highest order double term. */
97 if (unlikely(t == 0) /* Preserve -0. */
101 /* Sum terms of two highest orders. */
103 /* Use fused multiply-add to get low part of a * c. */
105 asm("fmsub %0,%1,%2,%3" : "=f"(tau) : "f"(a), "f"(c), "f"(t));
107 tau = fmsub(a, c, t);
111 tau += v + w; /* Add in other second-order terms. */
114 /* Construct long double result. */
118 z.dval[1] = (t - u) + tau;