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