1156cd587Sjoerg //===-- lib/divsf3.c - Single-precision division ------------------*- C -*-===//
2156cd587Sjoerg //
3156cd587Sjoerg // The LLVM Compiler Infrastructure
4156cd587Sjoerg //
5156cd587Sjoerg // This file is dual licensed under the MIT and the University of Illinois Open
6156cd587Sjoerg // Source Licenses. See LICENSE.TXT for details.
7156cd587Sjoerg //
8156cd587Sjoerg //===----------------------------------------------------------------------===//
9156cd587Sjoerg //
10156cd587Sjoerg // This file implements single-precision soft-float division
11156cd587Sjoerg // with the IEEE-754 default rounding (to nearest, ties to even).
12156cd587Sjoerg //
13156cd587Sjoerg // For simplicity, this implementation currently flushes denormals to zero.
14156cd587Sjoerg // It should be a fairly straightforward exercise to implement gradual
15156cd587Sjoerg // underflow with correct rounding.
16156cd587Sjoerg //
17156cd587Sjoerg //===----------------------------------------------------------------------===//
18156cd587Sjoerg
19156cd587Sjoerg #define SINGLE_PRECISION
20156cd587Sjoerg #include "fp_lib.h"
21156cd587Sjoerg
22f7f78b33Sjoerg COMPILER_RT_ABI fp_t
__divsf3(fp_t a,fp_t b)23f7f78b33Sjoerg __divsf3(fp_t a, fp_t b) {
24156cd587Sjoerg
25156cd587Sjoerg const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
26156cd587Sjoerg const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
27156cd587Sjoerg const rep_t quotientSign = (toRep(a) ^ toRep(b)) & signBit;
28156cd587Sjoerg
29156cd587Sjoerg rep_t aSignificand = toRep(a) & significandMask;
30156cd587Sjoerg rep_t bSignificand = toRep(b) & significandMask;
31156cd587Sjoerg int scale = 0;
32156cd587Sjoerg
33156cd587Sjoerg // Detect if a or b is zero, denormal, infinity, or NaN.
34156cd587Sjoerg if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
35156cd587Sjoerg
36156cd587Sjoerg const rep_t aAbs = toRep(a) & absMask;
37156cd587Sjoerg const rep_t bAbs = toRep(b) & absMask;
38156cd587Sjoerg
39156cd587Sjoerg // NaN / anything = qNaN
40156cd587Sjoerg if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
41156cd587Sjoerg // anything / NaN = qNaN
42156cd587Sjoerg if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
43156cd587Sjoerg
44156cd587Sjoerg if (aAbs == infRep) {
45156cd587Sjoerg // infinity / infinity = NaN
46156cd587Sjoerg if (bAbs == infRep) return fromRep(qnanRep);
47156cd587Sjoerg // infinity / anything else = +/- infinity
48156cd587Sjoerg else return fromRep(aAbs | quotientSign);
49156cd587Sjoerg }
50156cd587Sjoerg
51156cd587Sjoerg // anything else / infinity = +/- 0
52156cd587Sjoerg if (bAbs == infRep) return fromRep(quotientSign);
53156cd587Sjoerg
54156cd587Sjoerg if (!aAbs) {
55156cd587Sjoerg // zero / zero = NaN
56156cd587Sjoerg if (!bAbs) return fromRep(qnanRep);
57156cd587Sjoerg // zero / anything else = +/- zero
58156cd587Sjoerg else return fromRep(quotientSign);
59156cd587Sjoerg }
60156cd587Sjoerg // anything else / zero = +/- infinity
61156cd587Sjoerg if (!bAbs) return fromRep(infRep | quotientSign);
62156cd587Sjoerg
63156cd587Sjoerg // one or both of a or b is denormal, the other (if applicable) is a
64156cd587Sjoerg // normal number. Renormalize one or both of a and b, and set scale to
65156cd587Sjoerg // include the necessary exponent adjustment.
66156cd587Sjoerg if (aAbs < implicitBit) scale += normalize(&aSignificand);
67156cd587Sjoerg if (bAbs < implicitBit) scale -= normalize(&bSignificand);
68156cd587Sjoerg }
69156cd587Sjoerg
70156cd587Sjoerg // Or in the implicit significand bit. (If we fell through from the
71156cd587Sjoerg // denormal path it was already set by normalize( ), but setting it twice
72156cd587Sjoerg // won't hurt anything.)
73156cd587Sjoerg aSignificand |= implicitBit;
74156cd587Sjoerg bSignificand |= implicitBit;
75156cd587Sjoerg int quotientExponent = aExponent - bExponent + scale;
76156cd587Sjoerg
77156cd587Sjoerg // Align the significand of b as a Q31 fixed-point number in the range
78156cd587Sjoerg // [1, 2.0) and get a Q32 approximate reciprocal using a small minimax
79156cd587Sjoerg // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2. This
80156cd587Sjoerg // is accurate to about 3.5 binary digits.
81156cd587Sjoerg uint32_t q31b = bSignificand << 8;
82156cd587Sjoerg uint32_t reciprocal = UINT32_C(0x7504f333) - q31b;
83156cd587Sjoerg
84156cd587Sjoerg // Now refine the reciprocal estimate using a Newton-Raphson iteration:
85156cd587Sjoerg //
86156cd587Sjoerg // x1 = x0 * (2 - x0 * b)
87156cd587Sjoerg //
88156cd587Sjoerg // This doubles the number of correct binary digits in the approximation
89156cd587Sjoerg // with each iteration, so after three iterations, we have about 28 binary
90156cd587Sjoerg // digits of accuracy.
91156cd587Sjoerg uint32_t correction;
92156cd587Sjoerg correction = -((uint64_t)reciprocal * q31b >> 32);
93156cd587Sjoerg reciprocal = (uint64_t)reciprocal * correction >> 31;
94156cd587Sjoerg correction = -((uint64_t)reciprocal * q31b >> 32);
95156cd587Sjoerg reciprocal = (uint64_t)reciprocal * correction >> 31;
96156cd587Sjoerg correction = -((uint64_t)reciprocal * q31b >> 32);
97156cd587Sjoerg reciprocal = (uint64_t)reciprocal * correction >> 31;
98156cd587Sjoerg
99156cd587Sjoerg // Exhaustive testing shows that the error in reciprocal after three steps
100156cd587Sjoerg // is in the interval [-0x1.f58108p-31, 0x1.d0e48cp-29], in line with our
101156cd587Sjoerg // expectations. We bump the reciprocal by a tiny value to force the error
102156cd587Sjoerg // to be strictly positive (in the range [0x1.4fdfp-37,0x1.287246p-29], to
103156cd587Sjoerg // be specific). This also causes 1/1 to give a sensible approximation
104156cd587Sjoerg // instead of zero (due to overflow).
105156cd587Sjoerg reciprocal -= 2;
106156cd587Sjoerg
107156cd587Sjoerg // The numerical reciprocal is accurate to within 2^-28, lies in the
108156cd587Sjoerg // interval [0x1.000000eep-1, 0x1.fffffffcp-1], and is strictly smaller
109156cd587Sjoerg // than the true reciprocal of b. Multiplying a by this reciprocal thus
110156cd587Sjoerg // gives a numerical q = a/b in Q24 with the following properties:
111156cd587Sjoerg //
112156cd587Sjoerg // 1. q < a/b
113156cd587Sjoerg // 2. q is in the interval [0x1.000000eep-1, 0x1.fffffffcp0)
114156cd587Sjoerg // 3. the error in q is at most 2^-24 + 2^-27 -- the 2^24 term comes
115156cd587Sjoerg // from the fact that we truncate the product, and the 2^27 term
116156cd587Sjoerg // is the error in the reciprocal of b scaled by the maximum
117156cd587Sjoerg // possible value of a. As a consequence of this error bound,
118156cd587Sjoerg // either q or nextafter(q) is the correctly rounded
119156cd587Sjoerg rep_t quotient = (uint64_t)reciprocal*(aSignificand << 1) >> 32;
120156cd587Sjoerg
121156cd587Sjoerg // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
122156cd587Sjoerg // In either case, we are going to compute a residual of the form
123156cd587Sjoerg //
124156cd587Sjoerg // r = a - q*b
125156cd587Sjoerg //
126156cd587Sjoerg // We know from the construction of q that r satisfies:
127156cd587Sjoerg //
128156cd587Sjoerg // 0 <= r < ulp(q)*b
129156cd587Sjoerg //
130156cd587Sjoerg // if r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we
131156cd587Sjoerg // already have the correct result. The exact halfway case cannot occur.
132156cd587Sjoerg // We also take this time to right shift quotient if it falls in the [1,2)
133156cd587Sjoerg // range and adjust the exponent accordingly.
134156cd587Sjoerg rep_t residual;
135156cd587Sjoerg if (quotient < (implicitBit << 1)) {
136156cd587Sjoerg residual = (aSignificand << 24) - quotient * bSignificand;
137156cd587Sjoerg quotientExponent--;
138156cd587Sjoerg } else {
139156cd587Sjoerg quotient >>= 1;
140156cd587Sjoerg residual = (aSignificand << 23) - quotient * bSignificand;
141156cd587Sjoerg }
142156cd587Sjoerg
143156cd587Sjoerg const int writtenExponent = quotientExponent + exponentBias;
144156cd587Sjoerg
145156cd587Sjoerg if (writtenExponent >= maxExponent) {
146156cd587Sjoerg // If we have overflowed the exponent, return infinity.
147156cd587Sjoerg return fromRep(infRep | quotientSign);
148156cd587Sjoerg }
149156cd587Sjoerg
150156cd587Sjoerg else if (writtenExponent < 1) {
151156cd587Sjoerg // Flush denormals to zero. In the future, it would be nice to add
152156cd587Sjoerg // code to round them correctly.
153156cd587Sjoerg return fromRep(quotientSign);
154156cd587Sjoerg }
155156cd587Sjoerg
156156cd587Sjoerg else {
157156cd587Sjoerg const bool round = (residual << 1) > bSignificand;
158156cd587Sjoerg // Clear the implicit bit
159156cd587Sjoerg rep_t absResult = quotient & significandMask;
160156cd587Sjoerg // Insert the exponent
161156cd587Sjoerg absResult |= (rep_t)writtenExponent << significandBits;
162156cd587Sjoerg // Round
163156cd587Sjoerg absResult += round;
164156cd587Sjoerg // Insert the sign and return
165156cd587Sjoerg return fromRep(absResult | quotientSign);
166156cd587Sjoerg }
167156cd587Sjoerg }
1683044ee7eSrin
1693044ee7eSrin #if defined(__ARM_EABI__)
170*d3143459Srin #if defined(COMPILER_RT_ARMHF_TARGET)
__aeabi_fdiv(fp_t a,fp_t b)1713044ee7eSrin AEABI_RTABI fp_t __aeabi_fdiv(fp_t a, fp_t b) {
1723044ee7eSrin return __divsf3(a, b);
1733044ee7eSrin }
174*d3143459Srin #else
175*d3143459Srin AEABI_RTABI fp_t __aeabi_fdiv(fp_t a, fp_t b) COMPILER_RT_ALIAS(__divsf3);
1763044ee7eSrin #endif
177*d3143459Srin #endif
178