1*7c3d14c8STreehugger Robot //===-- lib/divsf3.c - Single-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 single-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 SINGLE_PRECISION
20*7c3d14c8STreehugger Robot #include "fp_lib.h"
21*7c3d14c8STreehugger Robot
ARM_EABI_FNALIAS(fdiv,divsf3)22*7c3d14c8STreehugger Robot ARM_EABI_FNALIAS(fdiv, divsf3)
23*7c3d14c8STreehugger Robot
24*7c3d14c8STreehugger Robot COMPILER_RT_ABI fp_t
25*7c3d14c8STreehugger Robot __divsf3(fp_t a, fp_t b) {
26*7c3d14c8STreehugger Robot
27*7c3d14c8STreehugger Robot const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
28*7c3d14c8STreehugger Robot const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
29*7c3d14c8STreehugger Robot const rep_t quotientSign = (toRep(a) ^ toRep(b)) & signBit;
30*7c3d14c8STreehugger Robot
31*7c3d14c8STreehugger Robot rep_t aSignificand = toRep(a) & significandMask;
32*7c3d14c8STreehugger Robot rep_t bSignificand = toRep(b) & significandMask;
33*7c3d14c8STreehugger Robot int scale = 0;
34*7c3d14c8STreehugger Robot
35*7c3d14c8STreehugger Robot // Detect if a or b is zero, denormal, infinity, or NaN.
36*7c3d14c8STreehugger Robot if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
37*7c3d14c8STreehugger Robot
38*7c3d14c8STreehugger Robot const rep_t aAbs = toRep(a) & absMask;
39*7c3d14c8STreehugger Robot const rep_t bAbs = toRep(b) & absMask;
40*7c3d14c8STreehugger Robot
41*7c3d14c8STreehugger Robot // NaN / anything = qNaN
42*7c3d14c8STreehugger Robot if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
43*7c3d14c8STreehugger Robot // anything / NaN = qNaN
44*7c3d14c8STreehugger Robot if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
45*7c3d14c8STreehugger Robot
46*7c3d14c8STreehugger Robot if (aAbs == infRep) {
47*7c3d14c8STreehugger Robot // infinity / infinity = NaN
48*7c3d14c8STreehugger Robot if (bAbs == infRep) return fromRep(qnanRep);
49*7c3d14c8STreehugger Robot // infinity / anything else = +/- infinity
50*7c3d14c8STreehugger Robot else return fromRep(aAbs | quotientSign);
51*7c3d14c8STreehugger Robot }
52*7c3d14c8STreehugger Robot
53*7c3d14c8STreehugger Robot // anything else / infinity = +/- 0
54*7c3d14c8STreehugger Robot if (bAbs == infRep) return fromRep(quotientSign);
55*7c3d14c8STreehugger Robot
56*7c3d14c8STreehugger Robot if (!aAbs) {
57*7c3d14c8STreehugger Robot // zero / zero = NaN
58*7c3d14c8STreehugger Robot if (!bAbs) return fromRep(qnanRep);
59*7c3d14c8STreehugger Robot // zero / anything else = +/- zero
60*7c3d14c8STreehugger Robot else return fromRep(quotientSign);
61*7c3d14c8STreehugger Robot }
62*7c3d14c8STreehugger Robot // anything else / zero = +/- infinity
63*7c3d14c8STreehugger Robot if (!bAbs) return fromRep(infRep | quotientSign);
64*7c3d14c8STreehugger Robot
65*7c3d14c8STreehugger Robot // one or both of a or b is denormal, the other (if applicable) is a
66*7c3d14c8STreehugger Robot // normal number. Renormalize one or both of a and b, and set scale to
67*7c3d14c8STreehugger Robot // include the necessary exponent adjustment.
68*7c3d14c8STreehugger Robot if (aAbs < implicitBit) scale += normalize(&aSignificand);
69*7c3d14c8STreehugger Robot if (bAbs < implicitBit) scale -= normalize(&bSignificand);
70*7c3d14c8STreehugger Robot }
71*7c3d14c8STreehugger Robot
72*7c3d14c8STreehugger Robot // Or in the implicit significand bit. (If we fell through from the
73*7c3d14c8STreehugger Robot // denormal path it was already set by normalize( ), but setting it twice
74*7c3d14c8STreehugger Robot // won't hurt anything.)
75*7c3d14c8STreehugger Robot aSignificand |= implicitBit;
76*7c3d14c8STreehugger Robot bSignificand |= implicitBit;
77*7c3d14c8STreehugger Robot int quotientExponent = aExponent - bExponent + scale;
78*7c3d14c8STreehugger Robot
79*7c3d14c8STreehugger Robot // Align the significand of b as a Q31 fixed-point number in the range
80*7c3d14c8STreehugger Robot // [1, 2.0) and get a Q32 approximate reciprocal using a small minimax
81*7c3d14c8STreehugger Robot // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2. This
82*7c3d14c8STreehugger Robot // is accurate to about 3.5 binary digits.
83*7c3d14c8STreehugger Robot uint32_t q31b = bSignificand << 8;
84*7c3d14c8STreehugger Robot uint32_t reciprocal = UINT32_C(0x7504f333) - q31b;
85*7c3d14c8STreehugger Robot
86*7c3d14c8STreehugger Robot // Now refine the reciprocal estimate using a Newton-Raphson iteration:
87*7c3d14c8STreehugger Robot //
88*7c3d14c8STreehugger Robot // x1 = x0 * (2 - x0 * b)
89*7c3d14c8STreehugger Robot //
90*7c3d14c8STreehugger Robot // This doubles the number of correct binary digits in the approximation
91*7c3d14c8STreehugger Robot // with each iteration, so after three iterations, we have about 28 binary
92*7c3d14c8STreehugger Robot // digits of accuracy.
93*7c3d14c8STreehugger Robot uint32_t correction;
94*7c3d14c8STreehugger Robot correction = -((uint64_t)reciprocal * q31b >> 32);
95*7c3d14c8STreehugger Robot reciprocal = (uint64_t)reciprocal * correction >> 31;
96*7c3d14c8STreehugger Robot correction = -((uint64_t)reciprocal * q31b >> 32);
97*7c3d14c8STreehugger Robot reciprocal = (uint64_t)reciprocal * correction >> 31;
98*7c3d14c8STreehugger Robot correction = -((uint64_t)reciprocal * q31b >> 32);
99*7c3d14c8STreehugger Robot reciprocal = (uint64_t)reciprocal * correction >> 31;
100*7c3d14c8STreehugger Robot
101*7c3d14c8STreehugger Robot // Exhaustive testing shows that the error in reciprocal after three steps
102*7c3d14c8STreehugger Robot // is in the interval [-0x1.f58108p-31, 0x1.d0e48cp-29], in line with our
103*7c3d14c8STreehugger Robot // expectations. We bump the reciprocal by a tiny value to force the error
104*7c3d14c8STreehugger Robot // to be strictly positive (in the range [0x1.4fdfp-37,0x1.287246p-29], to
105*7c3d14c8STreehugger Robot // be specific). This also causes 1/1 to give a sensible approximation
106*7c3d14c8STreehugger Robot // instead of zero (due to overflow).
107*7c3d14c8STreehugger Robot reciprocal -= 2;
108*7c3d14c8STreehugger Robot
109*7c3d14c8STreehugger Robot // The numerical reciprocal is accurate to within 2^-28, lies in the
110*7c3d14c8STreehugger Robot // interval [0x1.000000eep-1, 0x1.fffffffcp-1], and is strictly smaller
111*7c3d14c8STreehugger Robot // than the true reciprocal of b. Multiplying a by this reciprocal thus
112*7c3d14c8STreehugger Robot // gives a numerical q = a/b in Q24 with the following properties:
113*7c3d14c8STreehugger Robot //
114*7c3d14c8STreehugger Robot // 1. q < a/b
115*7c3d14c8STreehugger Robot // 2. q is in the interval [0x1.000000eep-1, 0x1.fffffffcp0)
116*7c3d14c8STreehugger Robot // 3. the error in q is at most 2^-24 + 2^-27 -- the 2^24 term comes
117*7c3d14c8STreehugger Robot // from the fact that we truncate the product, and the 2^27 term
118*7c3d14c8STreehugger Robot // is the error in the reciprocal of b scaled by the maximum
119*7c3d14c8STreehugger Robot // possible value of a. As a consequence of this error bound,
120*7c3d14c8STreehugger Robot // either q or nextafter(q) is the correctly rounded
121*7c3d14c8STreehugger Robot rep_t quotient = (uint64_t)reciprocal*(aSignificand << 1) >> 32;
122*7c3d14c8STreehugger Robot
123*7c3d14c8STreehugger Robot // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
124*7c3d14c8STreehugger Robot // In either case, we are going to compute a residual of the form
125*7c3d14c8STreehugger Robot //
126*7c3d14c8STreehugger Robot // r = a - q*b
127*7c3d14c8STreehugger Robot //
128*7c3d14c8STreehugger Robot // We know from the construction of q that r satisfies:
129*7c3d14c8STreehugger Robot //
130*7c3d14c8STreehugger Robot // 0 <= r < ulp(q)*b
131*7c3d14c8STreehugger Robot //
132*7c3d14c8STreehugger Robot // if r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we
133*7c3d14c8STreehugger Robot // already have the correct result. The exact halfway case cannot occur.
134*7c3d14c8STreehugger Robot // We also take this time to right shift quotient if it falls in the [1,2)
135*7c3d14c8STreehugger Robot // range and adjust the exponent accordingly.
136*7c3d14c8STreehugger Robot rep_t residual;
137*7c3d14c8STreehugger Robot if (quotient < (implicitBit << 1)) {
138*7c3d14c8STreehugger Robot residual = (aSignificand << 24) - quotient * bSignificand;
139*7c3d14c8STreehugger Robot quotientExponent--;
140*7c3d14c8STreehugger Robot } else {
141*7c3d14c8STreehugger Robot quotient >>= 1;
142*7c3d14c8STreehugger Robot residual = (aSignificand << 23) - quotient * bSignificand;
143*7c3d14c8STreehugger Robot }
144*7c3d14c8STreehugger Robot
145*7c3d14c8STreehugger Robot const int writtenExponent = quotientExponent + exponentBias;
146*7c3d14c8STreehugger Robot
147*7c3d14c8STreehugger Robot if (writtenExponent >= maxExponent) {
148*7c3d14c8STreehugger Robot // If we have overflowed the exponent, return infinity.
149*7c3d14c8STreehugger Robot return fromRep(infRep | quotientSign);
150*7c3d14c8STreehugger Robot }
151*7c3d14c8STreehugger Robot
152*7c3d14c8STreehugger Robot else if (writtenExponent < 1) {
153*7c3d14c8STreehugger Robot // Flush denormals to zero. In the future, it would be nice to add
154*7c3d14c8STreehugger Robot // code to round them correctly.
155*7c3d14c8STreehugger Robot return fromRep(quotientSign);
156*7c3d14c8STreehugger Robot }
157*7c3d14c8STreehugger Robot
158*7c3d14c8STreehugger Robot else {
159*7c3d14c8STreehugger Robot const bool round = (residual << 1) > bSignificand;
160*7c3d14c8STreehugger Robot // Clear the implicit bit
161*7c3d14c8STreehugger Robot rep_t absResult = quotient & significandMask;
162*7c3d14c8STreehugger Robot // Insert the exponent
163*7c3d14c8STreehugger Robot absResult |= (rep_t)writtenExponent << significandBits;
164*7c3d14c8STreehugger Robot // Round
165*7c3d14c8STreehugger Robot absResult += round;
166*7c3d14c8STreehugger Robot // Insert the sign and return
167*7c3d14c8STreehugger Robot return fromRep(absResult | quotientSign);
168*7c3d14c8STreehugger Robot }
169*7c3d14c8STreehugger Robot }
170