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