112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon//===-- lib/divdf3.c - Double-precision division ------------------*- C -*-===//
212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon//
312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon//                     The LLVM Compiler Infrastructure
412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon//
59ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant// This file is dual licensed under the MIT and the University of Illinois Open
69ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant// Source Licenses. See LICENSE.TXT for details.
712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon//
812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon//===----------------------------------------------------------------------===//
912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon//
1012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon// This file implements double-precision soft-float division
1112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon// with the IEEE-754 default rounding (to nearest, ties to even).
1212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon//
1312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon// For simplicity, this implementation currently flushes denormals to zero.
1412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon// It should be a fairly straightforward exercise to implement gradual
1512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon// underflow with correct rounding.
1612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon//
1712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon//===----------------------------------------------------------------------===//
1812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
1912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon#define DOUBLE_PRECISION
2012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon#include "fp_lib.h"
2112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
220193b74976719b8aea4cb8874ba36b75836a8d6eChandler CarruthARM_EABI_FNALIAS(ddiv, divdf3)
2337b97d1cf4501b94347e0b4e880f4b25825a289fAnton Korobeynikov
242d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen HinesCOMPILER_RT_ABI fp_t
252d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines__divdf3(fp_t a, fp_t b) {
2612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
2712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
2812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
2912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    const rep_t quotientSign = (toRep(a) ^ toRep(b)) & signBit;
3012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
3112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    rep_t aSignificand = toRep(a) & significandMask;
3212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    rep_t bSignificand = toRep(b) & significandMask;
3312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    int scale = 0;
3412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
3512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // Detect if a or b is zero, denormal, infinity, or NaN.
3612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
3712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
3812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        const rep_t aAbs = toRep(a) & absMask;
3912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        const rep_t bAbs = toRep(b) & absMask;
4012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
4112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // NaN / anything = qNaN
4212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
4312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // anything / NaN = qNaN
4412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
4512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
4612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        if (aAbs == infRep) {
4712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon            // infinity / infinity = NaN
4812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon            if (bAbs == infRep) return fromRep(qnanRep);
4912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon            // infinity / anything else = +/- infinity
5012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon            else return fromRep(aAbs | quotientSign);
5112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        }
5212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
5312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // anything else / infinity = +/- 0
5412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        if (bAbs == infRep) return fromRep(quotientSign);
5512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
5612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        if (!aAbs) {
5712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon            // zero / zero = NaN
5812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon            if (!bAbs) return fromRep(qnanRep);
5912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon            // zero / anything else = +/- zero
6012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon            else return fromRep(quotientSign);
6112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        }
6212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // anything else / zero = +/- infinity
6312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        if (!bAbs) return fromRep(infRep | quotientSign);
6412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
6512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // one or both of a or b is denormal, the other (if applicable) is a
6612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // normal number.  Renormalize one or both of a and b, and set scale to
6712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // include the necessary exponent adjustment.
6812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        if (aAbs < implicitBit) scale += normalize(&aSignificand);
6912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        if (bAbs < implicitBit) scale -= normalize(&bSignificand);
7012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    }
7112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
7212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // Or in the implicit significand bit.  (If we fell through from the
7312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // denormal path it was already set by normalize( ), but setting it twice
7412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // won't hurt anything.)
7512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    aSignificand |= implicitBit;
7612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    bSignificand |= implicitBit;
7712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    int quotientExponent = aExponent - bExponent + scale;
7812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
7912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // Align the significand of b as a Q31 fixed-point number in the range
8012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // [1, 2.0) and get a Q32 approximate reciprocal using a small minimax
8112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2.  This
8212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // is accurate to about 3.5 binary digits.
8312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    const uint32_t q31b = bSignificand >> 21;
8412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    uint32_t recip32 = UINT32_C(0x7504f333) - q31b;
8512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
8612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // Now refine the reciprocal estimate using a Newton-Raphson iteration:
8712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //
8812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //     x1 = x0 * (2 - x0 * b)
8912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //
9012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // This doubles the number of correct binary digits in the approximation
9112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // with each iteration, so after three iterations, we have about 28 binary
9212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // digits of accuracy.
9312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    uint32_t correction32;
9412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    correction32 = -((uint64_t)recip32 * q31b >> 32);
9512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    recip32 = (uint64_t)recip32 * correction32 >> 31;
9612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    correction32 = -((uint64_t)recip32 * q31b >> 32);
9712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    recip32 = (uint64_t)recip32 * correction32 >> 31;
9812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    correction32 = -((uint64_t)recip32 * q31b >> 32);
9912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    recip32 = (uint64_t)recip32 * correction32 >> 31;
10012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
1012d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines    // recip32 might have overflowed to exactly zero in the preceding
10212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // computation if the high word of b is exactly 1.0.  This would sabotage
10312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // the full-width final stage of the computation that follows, so we adjust
10412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // recip32 downward by one bit.
10512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    recip32--;
10612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
10712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // We need to perform one more iteration to get us to 56 binary digits;
10812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // The last iteration needs to happen with extra precision.
10912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    const uint32_t q63blo = bSignificand << 11;
11012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    uint64_t correction, reciprocal;
11112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    correction = -((uint64_t)recip32*q31b + ((uint64_t)recip32*q63blo >> 32));
11212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    uint32_t cHi = correction >> 32;
11312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    uint32_t cLo = correction;
11412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    reciprocal = (uint64_t)recip32*cHi + ((uint64_t)recip32*cLo >> 32);
11512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
11612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // We already adjusted the 32-bit estimate, now we need to adjust the final
11712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // 64-bit reciprocal estimate downward to ensure that it is strictly smaller
11812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // than the infinitely precise exact reciprocal.  Because the computation
11912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // of the Newton-Raphson step is truncating at every step, this adjustment
12012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // is small; most of the work is already done.
12112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    reciprocal -= 2;
12212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
12312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // The numerical reciprocal is accurate to within 2^-56, lies in the
12412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // interval [0.5, 1.0), and is strictly smaller than the true reciprocal
12512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // of b.  Multiplying a by this reciprocal thus gives a numerical q = a/b
12612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // in Q53 with the following properties:
12712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //
12812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //    1. q < a/b
12912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //    2. q is in the interval [0.5, 2.0)
13012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //    3. the error in q is bounded away from 2^-53 (actually, we have a
13112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //       couple of bits to spare, but this is all we need).
13212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
13312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // We need a 64 x 64 multiply high to compute q, which isn't a basic
13412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // operation in C, so we need to be a little bit fussy.
13512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    rep_t quotient, quotientLo;
13612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    wideMultiply(aSignificand << 2, reciprocal, &quotient, &quotientLo);
13712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
13812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
13912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // In either case, we are going to compute a residual of the form
14012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //
14112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //     r = a - q*b
14212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //
14312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // We know from the construction of q that r satisfies:
14412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //
14512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //     0 <= r < ulp(q)*b
14612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    //
14712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // if r is greater than 1/2 ulp(q)*b, then q rounds up.  Otherwise, we
14812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // already have the correct result.  The exact halfway case cannot occur.
14912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // We also take this time to right shift quotient if it falls in the [1,2)
15012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    // range and adjust the exponent accordingly.
15112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    rep_t residual;
15212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    if (quotient < (implicitBit << 1)) {
15312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        residual = (aSignificand << 53) - quotient * bSignificand;
15412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        quotientExponent--;
15512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    } else {
15612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        quotient >>= 1;
15712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        residual = (aSignificand << 52) - quotient * bSignificand;
15812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    }
15912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
16012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    const int writtenExponent = quotientExponent + exponentBias;
16112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
16212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    if (writtenExponent >= maxExponent) {
16312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // If we have overflowed the exponent, return infinity.
16412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        return fromRep(infRep | quotientSign);
16512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    }
16612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
16712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    else if (writtenExponent < 1) {
16812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // Flush denormals to zero.  In the future, it would be nice to add
16912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // code to round them correctly.
17012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        return fromRep(quotientSign);
17112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    }
17212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon
17312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    else {
17412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        const bool round = (residual << 1) > bSignificand;
17512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // Clear the implicit bit
17612a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        rep_t absResult = quotient & significandMask;
17712a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // Insert the exponent
17812a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        absResult |= (rep_t)writtenExponent << significandBits;
17912a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // Round
18012a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        absResult += round;
18112a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        // Insert the sign and return
18212a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        const double result = fromRep(absResult | quotientSign);
18312a7d094b185795f478308f4fc27b43abebdde07Stephen Canon        return result;
18412a7d094b185795f478308f4fc27b43abebdde07Stephen Canon    }
18512a7d094b185795f478308f4fc27b43abebdde07Stephen Canon}
186