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