1214152Sed//===-- lib/divdf3.c - Double-precision division ------------------*- C -*-===//
2214152Sed//
3214152Sed//                     The LLVM Compiler Infrastructure
4214152Sed//
5222656Sed// This file is dual licensed under the MIT and the University of Illinois Open
6222656Sed// Source Licenses. See LICENSE.TXT for details.
7214152Sed//
8214152Sed//===----------------------------------------------------------------------===//
9214152Sed//
10214152Sed// This file implements double-precision soft-float division
11214152Sed// with the IEEE-754 default rounding (to nearest, ties to even).
12214152Sed//
13214152Sed// For simplicity, this implementation currently flushes denormals to zero.
14214152Sed// It should be a fairly straightforward exercise to implement gradual
15214152Sed// underflow with correct rounding.
16214152Sed//
17214152Sed//===----------------------------------------------------------------------===//
18214152Sed
19214152Sed#define DOUBLE_PRECISION
20214152Sed#include "fp_lib.h"
21214152Sed
22239138SandrewARM_EABI_FNALIAS(ddiv, divdf3)
23222656Sed
24214152Sedfp_t __divdf3(fp_t a, fp_t b) {
25214152Sed
26214152Sed    const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
27214152Sed    const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
28214152Sed    const rep_t quotientSign = (toRep(a) ^ toRep(b)) & signBit;
29214152Sed
30214152Sed    rep_t aSignificand = toRep(a) & significandMask;
31214152Sed    rep_t bSignificand = toRep(b) & significandMask;
32214152Sed    int scale = 0;
33214152Sed
34214152Sed    // Detect if a or b is zero, denormal, infinity, or NaN.
35214152Sed    if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
36214152Sed
37214152Sed        const rep_t aAbs = toRep(a) & absMask;
38214152Sed        const rep_t bAbs = toRep(b) & absMask;
39214152Sed
40214152Sed        // NaN / anything = qNaN
41214152Sed        if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
42214152Sed        // anything / NaN = qNaN
43214152Sed        if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
44214152Sed
45214152Sed        if (aAbs == infRep) {
46214152Sed            // infinity / infinity = NaN
47214152Sed            if (bAbs == infRep) return fromRep(qnanRep);
48214152Sed            // infinity / anything else = +/- infinity
49214152Sed            else return fromRep(aAbs | quotientSign);
50214152Sed        }
51214152Sed
52214152Sed        // anything else / infinity = +/- 0
53214152Sed        if (bAbs == infRep) return fromRep(quotientSign);
54214152Sed
55214152Sed        if (!aAbs) {
56214152Sed            // zero / zero = NaN
57214152Sed            if (!bAbs) return fromRep(qnanRep);
58214152Sed            // zero / anything else = +/- zero
59214152Sed            else return fromRep(quotientSign);
60214152Sed        }
61214152Sed        // anything else / zero = +/- infinity
62214152Sed        if (!bAbs) return fromRep(infRep | quotientSign);
63214152Sed
64214152Sed        // one or both of a or b is denormal, the other (if applicable) is a
65214152Sed        // normal number.  Renormalize one or both of a and b, and set scale to
66214152Sed        // include the necessary exponent adjustment.
67214152Sed        if (aAbs < implicitBit) scale += normalize(&aSignificand);
68214152Sed        if (bAbs < implicitBit) scale -= normalize(&bSignificand);
69214152Sed    }
70214152Sed
71214152Sed    // Or in the implicit significand bit.  (If we fell through from the
72214152Sed    // denormal path it was already set by normalize( ), but setting it twice
73214152Sed    // won't hurt anything.)
74214152Sed    aSignificand |= implicitBit;
75214152Sed    bSignificand |= implicitBit;
76214152Sed    int quotientExponent = aExponent - bExponent + scale;
77214152Sed
78214152Sed    // Align the significand of b as a Q31 fixed-point number in the range
79214152Sed    // [1, 2.0) and get a Q32 approximate reciprocal using a small minimax
80214152Sed    // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2.  This
81214152Sed    // is accurate to about 3.5 binary digits.
82214152Sed    const uint32_t q31b = bSignificand >> 21;
83214152Sed    uint32_t recip32 = UINT32_C(0x7504f333) - q31b;
84214152Sed
85214152Sed    // Now refine the reciprocal estimate using a Newton-Raphson iteration:
86214152Sed    //
87214152Sed    //     x1 = x0 * (2 - x0 * b)
88214152Sed    //
89214152Sed    // This doubles the number of correct binary digits in the approximation
90214152Sed    // with each iteration, so after three iterations, we have about 28 binary
91214152Sed    // digits of accuracy.
92214152Sed    uint32_t correction32;
93214152Sed    correction32 = -((uint64_t)recip32 * q31b >> 32);
94214152Sed    recip32 = (uint64_t)recip32 * correction32 >> 31;
95214152Sed    correction32 = -((uint64_t)recip32 * q31b >> 32);
96214152Sed    recip32 = (uint64_t)recip32 * correction32 >> 31;
97214152Sed    correction32 = -((uint64_t)recip32 * q31b >> 32);
98214152Sed    recip32 = (uint64_t)recip32 * correction32 >> 31;
99214152Sed
100214152Sed    // recip32 might have overflowed to exactly zero in the preceeding
101214152Sed    // computation if the high word of b is exactly 1.0.  This would sabotage
102214152Sed    // the full-width final stage of the computation that follows, so we adjust
103214152Sed    // recip32 downward by one bit.
104214152Sed    recip32--;
105214152Sed
106214152Sed    // We need to perform one more iteration to get us to 56 binary digits;
107214152Sed    // The last iteration needs to happen with extra precision.
108214152Sed    const uint32_t q63blo = bSignificand << 11;
109214152Sed    uint64_t correction, reciprocal;
110214152Sed    correction = -((uint64_t)recip32*q31b + ((uint64_t)recip32*q63blo >> 32));
111214152Sed    uint32_t cHi = correction >> 32;
112214152Sed    uint32_t cLo = correction;
113214152Sed    reciprocal = (uint64_t)recip32*cHi + ((uint64_t)recip32*cLo >> 32);
114214152Sed
115214152Sed    // We already adjusted the 32-bit estimate, now we need to adjust the final
116214152Sed    // 64-bit reciprocal estimate downward to ensure that it is strictly smaller
117214152Sed    // than the infinitely precise exact reciprocal.  Because the computation
118214152Sed    // of the Newton-Raphson step is truncating at every step, this adjustment
119214152Sed    // is small; most of the work is already done.
120214152Sed    reciprocal -= 2;
121214152Sed
122214152Sed    // The numerical reciprocal is accurate to within 2^-56, lies in the
123214152Sed    // interval [0.5, 1.0), and is strictly smaller than the true reciprocal
124214152Sed    // of b.  Multiplying a by this reciprocal thus gives a numerical q = a/b
125214152Sed    // in Q53 with the following properties:
126214152Sed    //
127214152Sed    //    1. q < a/b
128214152Sed    //    2. q is in the interval [0.5, 2.0)
129214152Sed    //    3. the error in q is bounded away from 2^-53 (actually, we have a
130214152Sed    //       couple of bits to spare, but this is all we need).
131214152Sed
132214152Sed    // We need a 64 x 64 multiply high to compute q, which isn't a basic
133214152Sed    // operation in C, so we need to be a little bit fussy.
134214152Sed    rep_t quotient, quotientLo;
135214152Sed    wideMultiply(aSignificand << 2, reciprocal, &quotient, &quotientLo);
136214152Sed
137214152Sed    // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
138214152Sed    // In either case, we are going to compute a residual of the form
139214152Sed    //
140214152Sed    //     r = a - q*b
141214152Sed    //
142214152Sed    // We know from the construction of q that r satisfies:
143214152Sed    //
144214152Sed    //     0 <= r < ulp(q)*b
145214152Sed    //
146214152Sed    // if r is greater than 1/2 ulp(q)*b, then q rounds up.  Otherwise, we
147214152Sed    // already have the correct result.  The exact halfway case cannot occur.
148214152Sed    // We also take this time to right shift quotient if it falls in the [1,2)
149214152Sed    // range and adjust the exponent accordingly.
150214152Sed    rep_t residual;
151214152Sed    if (quotient < (implicitBit << 1)) {
152214152Sed        residual = (aSignificand << 53) - quotient * bSignificand;
153214152Sed        quotientExponent--;
154214152Sed    } else {
155214152Sed        quotient >>= 1;
156214152Sed        residual = (aSignificand << 52) - quotient * bSignificand;
157214152Sed    }
158214152Sed
159214152Sed    const int writtenExponent = quotientExponent + exponentBias;
160214152Sed
161214152Sed    if (writtenExponent >= maxExponent) {
162214152Sed        // If we have overflowed the exponent, return infinity.
163214152Sed        return fromRep(infRep | quotientSign);
164214152Sed    }
165214152Sed
166214152Sed    else if (writtenExponent < 1) {
167214152Sed        // Flush denormals to zero.  In the future, it would be nice to add
168214152Sed        // code to round them correctly.
169214152Sed        return fromRep(quotientSign);
170214152Sed    }
171214152Sed
172214152Sed    else {
173214152Sed        const bool round = (residual << 1) > bSignificand;
174214152Sed        // Clear the implicit bit
175214152Sed        rep_t absResult = quotient & significandMask;
176214152Sed        // Insert the exponent
177214152Sed        absResult |= (rep_t)writtenExponent << significandBits;
178214152Sed        // Round
179214152Sed        absResult += round;
180214152Sed        // Insert the sign and return
181214152Sed        const double result = fromRep(absResult | quotientSign);
182214152Sed        return result;
183214152Sed    }
184214152Sed}
185