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