1156cd587Sjoerg //===-- lib/divdf3.c - Double-precision division ------------------*- C -*-===//
2156cd587Sjoerg //
3156cd587Sjoerg // The LLVM Compiler Infrastructure
4156cd587Sjoerg //
5156cd587Sjoerg // This file is dual licensed under the MIT and the University of Illinois Open
6156cd587Sjoerg // Source Licenses. See LICENSE.TXT for details.
7156cd587Sjoerg //
8156cd587Sjoerg //===----------------------------------------------------------------------===//
9156cd587Sjoerg //
10156cd587Sjoerg // This file implements double-precision soft-float division
11156cd587Sjoerg // with the IEEE-754 default rounding (to nearest, ties to even).
12156cd587Sjoerg //
13156cd587Sjoerg // For simplicity, this implementation currently flushes denormals to zero.
14156cd587Sjoerg // It should be a fairly straightforward exercise to implement gradual
15156cd587Sjoerg // underflow with correct rounding.
16156cd587Sjoerg //
17156cd587Sjoerg //===----------------------------------------------------------------------===//
18156cd587Sjoerg
19156cd587Sjoerg #define DOUBLE_PRECISION
20156cd587Sjoerg #include "fp_lib.h"
21156cd587Sjoerg
22f7f78b33Sjoerg COMPILER_RT_ABI fp_t
__divdf3(fp_t a,fp_t b)23f7f78b33Sjoerg __divdf3(fp_t a, fp_t b) {
24156cd587Sjoerg
25156cd587Sjoerg const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
26156cd587Sjoerg const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
27156cd587Sjoerg const rep_t quotientSign = (toRep(a) ^ toRep(b)) & signBit;
28156cd587Sjoerg
29156cd587Sjoerg rep_t aSignificand = toRep(a) & significandMask;
30156cd587Sjoerg rep_t bSignificand = toRep(b) & significandMask;
31156cd587Sjoerg int scale = 0;
32156cd587Sjoerg
33156cd587Sjoerg // Detect if a or b is zero, denormal, infinity, or NaN.
34156cd587Sjoerg if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
35156cd587Sjoerg
36156cd587Sjoerg const rep_t aAbs = toRep(a) & absMask;
37156cd587Sjoerg const rep_t bAbs = toRep(b) & absMask;
38156cd587Sjoerg
39156cd587Sjoerg // NaN / anything = qNaN
40156cd587Sjoerg if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
41156cd587Sjoerg // anything / NaN = qNaN
42156cd587Sjoerg if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
43156cd587Sjoerg
44156cd587Sjoerg if (aAbs == infRep) {
45156cd587Sjoerg // infinity / infinity = NaN
46156cd587Sjoerg if (bAbs == infRep) return fromRep(qnanRep);
47156cd587Sjoerg // infinity / anything else = +/- infinity
48156cd587Sjoerg else return fromRep(aAbs | quotientSign);
49156cd587Sjoerg }
50156cd587Sjoerg
51156cd587Sjoerg // anything else / infinity = +/- 0
52156cd587Sjoerg if (bAbs == infRep) return fromRep(quotientSign);
53156cd587Sjoerg
54156cd587Sjoerg if (!aAbs) {
55156cd587Sjoerg // zero / zero = NaN
56156cd587Sjoerg if (!bAbs) return fromRep(qnanRep);
57156cd587Sjoerg // zero / anything else = +/- zero
58156cd587Sjoerg else return fromRep(quotientSign);
59156cd587Sjoerg }
60156cd587Sjoerg // anything else / zero = +/- infinity
61156cd587Sjoerg if (!bAbs) return fromRep(infRep | quotientSign);
62156cd587Sjoerg
63156cd587Sjoerg // one or both of a or b is denormal, the other (if applicable) is a
64156cd587Sjoerg // normal number. Renormalize one or both of a and b, and set scale to
65156cd587Sjoerg // include the necessary exponent adjustment.
66156cd587Sjoerg if (aAbs < implicitBit) scale += normalize(&aSignificand);
67156cd587Sjoerg if (bAbs < implicitBit) scale -= normalize(&bSignificand);
68156cd587Sjoerg }
69156cd587Sjoerg
70156cd587Sjoerg // Or in the implicit significand bit. (If we fell through from the
71156cd587Sjoerg // denormal path it was already set by normalize( ), but setting it twice
72156cd587Sjoerg // won't hurt anything.)
73156cd587Sjoerg aSignificand |= implicitBit;
74156cd587Sjoerg bSignificand |= implicitBit;
75156cd587Sjoerg int quotientExponent = aExponent - bExponent + scale;
76156cd587Sjoerg
77156cd587Sjoerg // Align the significand of b as a Q31 fixed-point number in the range
78156cd587Sjoerg // [1, 2.0) and get a Q32 approximate reciprocal using a small minimax
79156cd587Sjoerg // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2. This
80156cd587Sjoerg // is accurate to about 3.5 binary digits.
81156cd587Sjoerg const uint32_t q31b = bSignificand >> 21;
82156cd587Sjoerg uint32_t recip32 = UINT32_C(0x7504f333) - q31b;
83156cd587Sjoerg
84156cd587Sjoerg // Now refine the reciprocal estimate using a Newton-Raphson iteration:
85156cd587Sjoerg //
86156cd587Sjoerg // x1 = x0 * (2 - x0 * b)
87156cd587Sjoerg //
88156cd587Sjoerg // This doubles the number of correct binary digits in the approximation
89156cd587Sjoerg // with each iteration, so after three iterations, we have about 28 binary
90156cd587Sjoerg // digits of accuracy.
91156cd587Sjoerg uint32_t correction32;
92156cd587Sjoerg correction32 = -((uint64_t)recip32 * q31b >> 32);
93156cd587Sjoerg recip32 = (uint64_t)recip32 * correction32 >> 31;
94156cd587Sjoerg correction32 = -((uint64_t)recip32 * q31b >> 32);
95156cd587Sjoerg recip32 = (uint64_t)recip32 * correction32 >> 31;
96156cd587Sjoerg correction32 = -((uint64_t)recip32 * q31b >> 32);
97156cd587Sjoerg recip32 = (uint64_t)recip32 * correction32 >> 31;
98156cd587Sjoerg
9930308f42Sjoerg // recip32 might have overflowed to exactly zero in the preceding
100156cd587Sjoerg // computation if the high word of b is exactly 1.0. This would sabotage
101156cd587Sjoerg // the full-width final stage of the computation that follows, so we adjust
102156cd587Sjoerg // recip32 downward by one bit.
103156cd587Sjoerg recip32--;
104156cd587Sjoerg
105156cd587Sjoerg // We need to perform one more iteration to get us to 56 binary digits;
106156cd587Sjoerg // The last iteration needs to happen with extra precision.
107156cd587Sjoerg const uint32_t q63blo = bSignificand << 11;
108156cd587Sjoerg uint64_t correction, reciprocal;
109156cd587Sjoerg correction = -((uint64_t)recip32*q31b + ((uint64_t)recip32*q63blo >> 32));
110156cd587Sjoerg uint32_t cHi = correction >> 32;
111156cd587Sjoerg uint32_t cLo = correction;
112156cd587Sjoerg reciprocal = (uint64_t)recip32*cHi + ((uint64_t)recip32*cLo >> 32);
113156cd587Sjoerg
114156cd587Sjoerg // We already adjusted the 32-bit estimate, now we need to adjust the final
115156cd587Sjoerg // 64-bit reciprocal estimate downward to ensure that it is strictly smaller
116156cd587Sjoerg // than the infinitely precise exact reciprocal. Because the computation
117156cd587Sjoerg // of the Newton-Raphson step is truncating at every step, this adjustment
118156cd587Sjoerg // is small; most of the work is already done.
119156cd587Sjoerg reciprocal -= 2;
120156cd587Sjoerg
121156cd587Sjoerg // The numerical reciprocal is accurate to within 2^-56, lies in the
122156cd587Sjoerg // interval [0.5, 1.0), and is strictly smaller than the true reciprocal
123156cd587Sjoerg // of b. Multiplying a by this reciprocal thus gives a numerical q = a/b
124156cd587Sjoerg // in Q53 with the following properties:
125156cd587Sjoerg //
126156cd587Sjoerg // 1. q < a/b
127156cd587Sjoerg // 2. q is in the interval [0.5, 2.0)
128156cd587Sjoerg // 3. the error in q is bounded away from 2^-53 (actually, we have a
129156cd587Sjoerg // couple of bits to spare, but this is all we need).
130156cd587Sjoerg
131156cd587Sjoerg // We need a 64 x 64 multiply high to compute q, which isn't a basic
132156cd587Sjoerg // operation in C, so we need to be a little bit fussy.
133156cd587Sjoerg rep_t quotient, quotientLo;
134156cd587Sjoerg wideMultiply(aSignificand << 2, reciprocal, "ient, "ientLo);
135156cd587Sjoerg
136156cd587Sjoerg // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
137156cd587Sjoerg // In either case, we are going to compute a residual of the form
138156cd587Sjoerg //
139156cd587Sjoerg // r = a - q*b
140156cd587Sjoerg //
141156cd587Sjoerg // We know from the construction of q that r satisfies:
142156cd587Sjoerg //
143156cd587Sjoerg // 0 <= r < ulp(q)*b
144156cd587Sjoerg //
145156cd587Sjoerg // if r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we
146156cd587Sjoerg // already have the correct result. The exact halfway case cannot occur.
147156cd587Sjoerg // We also take this time to right shift quotient if it falls in the [1,2)
148156cd587Sjoerg // range and adjust the exponent accordingly.
149156cd587Sjoerg rep_t residual;
150156cd587Sjoerg if (quotient < (implicitBit << 1)) {
151156cd587Sjoerg residual = (aSignificand << 53) - quotient * bSignificand;
152156cd587Sjoerg quotientExponent--;
153156cd587Sjoerg } else {
154156cd587Sjoerg quotient >>= 1;
155156cd587Sjoerg residual = (aSignificand << 52) - quotient * bSignificand;
156156cd587Sjoerg }
157156cd587Sjoerg
158156cd587Sjoerg const int writtenExponent = quotientExponent + exponentBias;
159156cd587Sjoerg
160156cd587Sjoerg if (writtenExponent >= maxExponent) {
161156cd587Sjoerg // If we have overflowed the exponent, return infinity.
162156cd587Sjoerg return fromRep(infRep | quotientSign);
163156cd587Sjoerg }
164156cd587Sjoerg
165156cd587Sjoerg else if (writtenExponent < 1) {
166156cd587Sjoerg // Flush denormals to zero. In the future, it would be nice to add
167156cd587Sjoerg // code to round them correctly.
168156cd587Sjoerg return fromRep(quotientSign);
169156cd587Sjoerg }
170156cd587Sjoerg
171156cd587Sjoerg else {
172156cd587Sjoerg const bool round = (residual << 1) > bSignificand;
173156cd587Sjoerg // Clear the implicit bit
174156cd587Sjoerg rep_t absResult = quotient & significandMask;
175156cd587Sjoerg // Insert the exponent
176156cd587Sjoerg absResult |= (rep_t)writtenExponent << significandBits;
177156cd587Sjoerg // Round
178156cd587Sjoerg absResult += round;
179156cd587Sjoerg // Insert the sign and return
180156cd587Sjoerg const double result = fromRep(absResult | quotientSign);
181156cd587Sjoerg return result;
182156cd587Sjoerg }
183156cd587Sjoerg }
1843044ee7eSrin
1853044ee7eSrin #if defined(__ARM_EABI__)
186*d3143459Srin #if defined(COMPILER_RT_ARMHF_TARGET)
__aeabi_ddiv(fp_t a,fp_t b)1873044ee7eSrin AEABI_RTABI fp_t __aeabi_ddiv(fp_t a, fp_t b) {
1883044ee7eSrin return __divdf3(a, b);
1893044ee7eSrin }
190*d3143459Srin #else
191*d3143459Srin AEABI_RTABI fp_t __aeabi_ddiv(fp_t a, fp_t b) COMPILER_RT_ALIAS(__divdf3);
1923044ee7eSrin #endif
193*d3143459Srin #endif
194