xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/divtf3.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===-- lib/divtf3.c - Quad-precision division --------------------*- C -*-===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // This file implements quad-precision soft-float division
11*0a6a1f1dSLionel Sambuc // with the IEEE-754 default rounding (to nearest, ties to even).
12*0a6a1f1dSLionel Sambuc //
13*0a6a1f1dSLionel Sambuc // For simplicity, this implementation currently flushes denormals to zero.
14*0a6a1f1dSLionel Sambuc // It should be a fairly straightforward exercise to implement gradual
15*0a6a1f1dSLionel Sambuc // underflow with correct rounding.
16*0a6a1f1dSLionel Sambuc //
17*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc #define QUAD_PRECISION
20*0a6a1f1dSLionel Sambuc #include "fp_lib.h"
21*0a6a1f1dSLionel Sambuc 
22*0a6a1f1dSLionel Sambuc #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
__divtf3(fp_t a,fp_t b)23*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI fp_t __divtf3(fp_t a, fp_t b) {
24*0a6a1f1dSLionel Sambuc 
25*0a6a1f1dSLionel Sambuc     const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
26*0a6a1f1dSLionel Sambuc     const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
27*0a6a1f1dSLionel Sambuc     const rep_t quotientSign = (toRep(a) ^ toRep(b)) & signBit;
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc     rep_t aSignificand = toRep(a) & significandMask;
30*0a6a1f1dSLionel Sambuc     rep_t bSignificand = toRep(b) & significandMask;
31*0a6a1f1dSLionel Sambuc     int scale = 0;
32*0a6a1f1dSLionel Sambuc 
33*0a6a1f1dSLionel Sambuc     // Detect if a or b is zero, denormal, infinity, or NaN.
34*0a6a1f1dSLionel Sambuc     if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
35*0a6a1f1dSLionel Sambuc 
36*0a6a1f1dSLionel Sambuc         const rep_t aAbs = toRep(a) & absMask;
37*0a6a1f1dSLionel Sambuc         const rep_t bAbs = toRep(b) & absMask;
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc         // NaN / anything = qNaN
40*0a6a1f1dSLionel Sambuc         if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
41*0a6a1f1dSLionel Sambuc         // anything / NaN = qNaN
42*0a6a1f1dSLionel Sambuc         if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc         if (aAbs == infRep) {
45*0a6a1f1dSLionel Sambuc             // infinity / infinity = NaN
46*0a6a1f1dSLionel Sambuc             if (bAbs == infRep) return fromRep(qnanRep);
47*0a6a1f1dSLionel Sambuc             // infinity / anything else = +/- infinity
48*0a6a1f1dSLionel Sambuc             else return fromRep(aAbs | quotientSign);
49*0a6a1f1dSLionel Sambuc         }
50*0a6a1f1dSLionel Sambuc 
51*0a6a1f1dSLionel Sambuc         // anything else / infinity = +/- 0
52*0a6a1f1dSLionel Sambuc         if (bAbs == infRep) return fromRep(quotientSign);
53*0a6a1f1dSLionel Sambuc 
54*0a6a1f1dSLionel Sambuc         if (!aAbs) {
55*0a6a1f1dSLionel Sambuc             // zero / zero = NaN
56*0a6a1f1dSLionel Sambuc             if (!bAbs) return fromRep(qnanRep);
57*0a6a1f1dSLionel Sambuc             // zero / anything else = +/- zero
58*0a6a1f1dSLionel Sambuc             else return fromRep(quotientSign);
59*0a6a1f1dSLionel Sambuc         }
60*0a6a1f1dSLionel Sambuc         // anything else / zero = +/- infinity
61*0a6a1f1dSLionel Sambuc         if (!bAbs) return fromRep(infRep | quotientSign);
62*0a6a1f1dSLionel Sambuc 
63*0a6a1f1dSLionel Sambuc         // one or both of a or b is denormal, the other (if applicable) is a
64*0a6a1f1dSLionel Sambuc         // normal number.  Renormalize one or both of a and b, and set scale to
65*0a6a1f1dSLionel Sambuc         // include the necessary exponent adjustment.
66*0a6a1f1dSLionel Sambuc         if (aAbs < implicitBit) scale += normalize(&aSignificand);
67*0a6a1f1dSLionel Sambuc         if (bAbs < implicitBit) scale -= normalize(&bSignificand);
68*0a6a1f1dSLionel Sambuc     }
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc     // Or in the implicit significand bit.  (If we fell through from the
71*0a6a1f1dSLionel Sambuc     // denormal path it was already set by normalize( ), but setting it twice
72*0a6a1f1dSLionel Sambuc     // won't hurt anything.)
73*0a6a1f1dSLionel Sambuc     aSignificand |= implicitBit;
74*0a6a1f1dSLionel Sambuc     bSignificand |= implicitBit;
75*0a6a1f1dSLionel Sambuc     int quotientExponent = aExponent - bExponent + scale;
76*0a6a1f1dSLionel Sambuc 
77*0a6a1f1dSLionel Sambuc     // Align the significand of b as a Q63 fixed-point number in the range
78*0a6a1f1dSLionel Sambuc     // [1, 2.0) and get a Q64 approximate reciprocal using a small minimax
79*0a6a1f1dSLionel Sambuc     // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2.  This
80*0a6a1f1dSLionel Sambuc     // is accurate to about 3.5 binary digits.
81*0a6a1f1dSLionel Sambuc     const uint64_t q63b = bSignificand >> 49;
82*0a6a1f1dSLionel Sambuc     uint64_t recip64 = UINT64_C(0x7504f333F9DE6484) - q63b;
83*0a6a1f1dSLionel Sambuc     // 0x7504f333F9DE6484 / 2^64 + 1 = 3/4 + 1/sqrt(2)
84*0a6a1f1dSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc     // Now refine the reciprocal estimate using a Newton-Raphson iteration:
86*0a6a1f1dSLionel Sambuc     //
87*0a6a1f1dSLionel Sambuc     //     x1 = x0 * (2 - x0 * b)
88*0a6a1f1dSLionel Sambuc     //
89*0a6a1f1dSLionel Sambuc     // This doubles the number of correct binary digits in the approximation
90*0a6a1f1dSLionel Sambuc     // with each iteration.
91*0a6a1f1dSLionel Sambuc     uint64_t correction64;
92*0a6a1f1dSLionel Sambuc     correction64 = -((rep_t)recip64 * q63b >> 64);
93*0a6a1f1dSLionel Sambuc     recip64 = (rep_t)recip64 * correction64 >> 63;
94*0a6a1f1dSLionel Sambuc     correction64 = -((rep_t)recip64 * q63b >> 64);
95*0a6a1f1dSLionel Sambuc     recip64 = (rep_t)recip64 * correction64 >> 63;
96*0a6a1f1dSLionel Sambuc     correction64 = -((rep_t)recip64 * q63b >> 64);
97*0a6a1f1dSLionel Sambuc     recip64 = (rep_t)recip64 * correction64 >> 63;
98*0a6a1f1dSLionel Sambuc     correction64 = -((rep_t)recip64 * q63b >> 64);
99*0a6a1f1dSLionel Sambuc     recip64 = (rep_t)recip64 * correction64 >> 63;
100*0a6a1f1dSLionel Sambuc     correction64 = -((rep_t)recip64 * q63b >> 64);
101*0a6a1f1dSLionel Sambuc     recip64 = (rep_t)recip64 * correction64 >> 63;
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc     // recip64 might have overflowed to exactly zero in the preceeding
104*0a6a1f1dSLionel Sambuc     // computation if the high word of b is exactly 1.0.  This would sabotage
105*0a6a1f1dSLionel Sambuc     // the full-width final stage of the computation that follows, so we adjust
106*0a6a1f1dSLionel Sambuc     // recip64 downward by one bit.
107*0a6a1f1dSLionel Sambuc     recip64--;
108*0a6a1f1dSLionel Sambuc 
109*0a6a1f1dSLionel Sambuc     // We need to perform one more iteration to get us to 112 binary digits;
110*0a6a1f1dSLionel Sambuc     // The last iteration needs to happen with extra precision.
111*0a6a1f1dSLionel Sambuc     const uint64_t q127blo = bSignificand << 15;
112*0a6a1f1dSLionel Sambuc     rep_t correction, reciprocal;
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc     // NOTE: This operation is equivalent to __multi3, which is not implemented
115*0a6a1f1dSLionel Sambuc     //       in some architechure
116*0a6a1f1dSLionel Sambuc     rep_t r64q63, r64q127, r64cH, r64cL, dummy;
117*0a6a1f1dSLionel Sambuc     wideMultiply((rep_t)recip64, (rep_t)q63b, &dummy, &r64q63);
118*0a6a1f1dSLionel Sambuc     wideMultiply((rep_t)recip64, (rep_t)q127blo, &dummy, &r64q127);
119*0a6a1f1dSLionel Sambuc 
120*0a6a1f1dSLionel Sambuc     correction = -(r64q63 + (r64q127 >> 64));
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc     uint64_t cHi = correction >> 64;
123*0a6a1f1dSLionel Sambuc     uint64_t cLo = correction;
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc     wideMultiply((rep_t)recip64, (rep_t)cHi, &dummy, &r64cH);
126*0a6a1f1dSLionel Sambuc     wideMultiply((rep_t)recip64, (rep_t)cLo, &dummy, &r64cL);
127*0a6a1f1dSLionel Sambuc 
128*0a6a1f1dSLionel Sambuc     reciprocal = r64cH + (r64cL >> 64);
129*0a6a1f1dSLionel Sambuc 
130*0a6a1f1dSLionel Sambuc     // We already adjusted the 64-bit estimate, now we need to adjust the final
131*0a6a1f1dSLionel Sambuc     // 128-bit reciprocal estimate downward to ensure that it is strictly smaller
132*0a6a1f1dSLionel Sambuc     // than the infinitely precise exact reciprocal.  Because the computation
133*0a6a1f1dSLionel Sambuc     // of the Newton-Raphson step is truncating at every step, this adjustment
134*0a6a1f1dSLionel Sambuc     // is small; most of the work is already done.
135*0a6a1f1dSLionel Sambuc     reciprocal -= 2;
136*0a6a1f1dSLionel Sambuc 
137*0a6a1f1dSLionel Sambuc     // The numerical reciprocal is accurate to within 2^-112, lies in the
138*0a6a1f1dSLionel Sambuc     // interval [0.5, 1.0), and is strictly smaller than the true reciprocal
139*0a6a1f1dSLionel Sambuc     // of b.  Multiplying a by this reciprocal thus gives a numerical q = a/b
140*0a6a1f1dSLionel Sambuc     // in Q127 with the following properties:
141*0a6a1f1dSLionel Sambuc     //
142*0a6a1f1dSLionel Sambuc     //    1. q < a/b
143*0a6a1f1dSLionel Sambuc     //    2. q is in the interval [0.5, 2.0)
144*0a6a1f1dSLionel Sambuc     //    3. the error in q is bounded away from 2^-113 (actually, we have a
145*0a6a1f1dSLionel Sambuc     //       couple of bits to spare, but this is all we need).
146*0a6a1f1dSLionel Sambuc 
147*0a6a1f1dSLionel Sambuc     // We need a 128 x 128 multiply high to compute q, which isn't a basic
148*0a6a1f1dSLionel Sambuc     // operation in C, so we need to be a little bit fussy.
149*0a6a1f1dSLionel Sambuc     rep_t quotient, quotientLo;
150*0a6a1f1dSLionel Sambuc     wideMultiply(aSignificand << 2, reciprocal, &quotient, &quotientLo);
151*0a6a1f1dSLionel Sambuc 
152*0a6a1f1dSLionel Sambuc     // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
153*0a6a1f1dSLionel Sambuc     // In either case, we are going to compute a residual of the form
154*0a6a1f1dSLionel Sambuc     //
155*0a6a1f1dSLionel Sambuc     //     r = a - q*b
156*0a6a1f1dSLionel Sambuc     //
157*0a6a1f1dSLionel Sambuc     // We know from the construction of q that r satisfies:
158*0a6a1f1dSLionel Sambuc     //
159*0a6a1f1dSLionel Sambuc     //     0 <= r < ulp(q)*b
160*0a6a1f1dSLionel Sambuc     //
161*0a6a1f1dSLionel Sambuc     // if r is greater than 1/2 ulp(q)*b, then q rounds up.  Otherwise, we
162*0a6a1f1dSLionel Sambuc     // already have the correct result.  The exact halfway case cannot occur.
163*0a6a1f1dSLionel Sambuc     // We also take this time to right shift quotient if it falls in the [1,2)
164*0a6a1f1dSLionel Sambuc     // range and adjust the exponent accordingly.
165*0a6a1f1dSLionel Sambuc     rep_t residual;
166*0a6a1f1dSLionel Sambuc     rep_t qb;
167*0a6a1f1dSLionel Sambuc 
168*0a6a1f1dSLionel Sambuc     if (quotient < (implicitBit << 1)) {
169*0a6a1f1dSLionel Sambuc         wideMultiply(quotient, bSignificand, &dummy, &qb);
170*0a6a1f1dSLionel Sambuc         residual = (aSignificand << 113) - qb;
171*0a6a1f1dSLionel Sambuc         quotientExponent--;
172*0a6a1f1dSLionel Sambuc     } else {
173*0a6a1f1dSLionel Sambuc         quotient >>= 1;
174*0a6a1f1dSLionel Sambuc         wideMultiply(quotient, bSignificand, &dummy, &qb);
175*0a6a1f1dSLionel Sambuc         residual = (aSignificand << 112) - qb;
176*0a6a1f1dSLionel Sambuc     }
177*0a6a1f1dSLionel Sambuc 
178*0a6a1f1dSLionel Sambuc     const int writtenExponent = quotientExponent + exponentBias;
179*0a6a1f1dSLionel Sambuc 
180*0a6a1f1dSLionel Sambuc     if (writtenExponent >= maxExponent) {
181*0a6a1f1dSLionel Sambuc         // If we have overflowed the exponent, return infinity.
182*0a6a1f1dSLionel Sambuc         return fromRep(infRep | quotientSign);
183*0a6a1f1dSLionel Sambuc     }
184*0a6a1f1dSLionel Sambuc     else if (writtenExponent < 1) {
185*0a6a1f1dSLionel Sambuc         // Flush denormals to zero.  In the future, it would be nice to add
186*0a6a1f1dSLionel Sambuc         // code to round them correctly.
187*0a6a1f1dSLionel Sambuc         return fromRep(quotientSign);
188*0a6a1f1dSLionel Sambuc     }
189*0a6a1f1dSLionel Sambuc     else {
190*0a6a1f1dSLionel Sambuc         const bool round = (residual << 1) >= bSignificand;
191*0a6a1f1dSLionel Sambuc         // Clear the implicit bit
192*0a6a1f1dSLionel Sambuc         rep_t absResult = quotient & significandMask;
193*0a6a1f1dSLionel Sambuc         // Insert the exponent
194*0a6a1f1dSLionel Sambuc         absResult |= (rep_t)writtenExponent << significandBits;
195*0a6a1f1dSLionel Sambuc         // Round
196*0a6a1f1dSLionel Sambuc         absResult += round;
197*0a6a1f1dSLionel Sambuc         // Insert the sign and return
198*0a6a1f1dSLionel Sambuc         const long double result = fromRep(absResult | quotientSign);
199*0a6a1f1dSLionel Sambuc         return result;
200*0a6a1f1dSLionel Sambuc     }
201*0a6a1f1dSLionel Sambuc }
202*0a6a1f1dSLionel Sambuc 
203*0a6a1f1dSLionel Sambuc #endif
204