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