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