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