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