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