divsf3.c revision 222656
1214152Sed//===-- lib/divsf3.c - Single-precision division ------------------*- C -*-===// 2214152Sed// 3214152Sed// The LLVM Compiler Infrastructure 4214152Sed// 5222656Sed// This file is dual licensed under the MIT and the University of Illinois Open 6222656Sed// Source Licenses. See LICENSE.TXT for details. 7214152Sed// 8214152Sed//===----------------------------------------------------------------------===// 9214152Sed// 10214152Sed// This file implements single-precision soft-float division 11214152Sed// with the IEEE-754 default rounding (to nearest, ties to even). 12214152Sed// 13214152Sed// For simplicity, this implementation currently flushes denormals to zero. 14214152Sed// It should be a fairly straightforward exercise to implement gradual 15214152Sed// underflow with correct rounding. 16214152Sed// 17214152Sed//===----------------------------------------------------------------------===// 18222656Sed#include "abi.h" 19214152Sed 20214152Sed#define SINGLE_PRECISION 21214152Sed#include "fp_lib.h" 22214152Sed 23222656SedARM_EABI_FNALIAS(fdiv, divsf3); 24222656Sed 25214152Sedfp_t __divsf3(fp_t a, fp_t b) { 26214152Sed 27214152Sed const unsigned int aExponent = toRep(a) >> significandBits & maxExponent; 28214152Sed const unsigned int bExponent = toRep(b) >> significandBits & maxExponent; 29214152Sed const rep_t quotientSign = (toRep(a) ^ toRep(b)) & signBit; 30214152Sed 31214152Sed rep_t aSignificand = toRep(a) & significandMask; 32214152Sed rep_t bSignificand = toRep(b) & significandMask; 33214152Sed int scale = 0; 34214152Sed 35214152Sed // Detect if a or b is zero, denormal, infinity, or NaN. 36214152Sed if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) { 37214152Sed 38214152Sed const rep_t aAbs = toRep(a) & absMask; 39214152Sed const rep_t bAbs = toRep(b) & absMask; 40214152Sed 41214152Sed // NaN / anything = qNaN 42214152Sed if (aAbs > infRep) return fromRep(toRep(a) | quietBit); 43214152Sed // anything / NaN = qNaN 44214152Sed if (bAbs > infRep) return fromRep(toRep(b) | quietBit); 45214152Sed 46214152Sed if (aAbs == infRep) { 47214152Sed // infinity / infinity = NaN 48214152Sed if (bAbs == infRep) return fromRep(qnanRep); 49214152Sed // infinity / anything else = +/- infinity 50214152Sed else return fromRep(aAbs | quotientSign); 51214152Sed } 52214152Sed 53214152Sed // anything else / infinity = +/- 0 54214152Sed if (bAbs == infRep) return fromRep(quotientSign); 55214152Sed 56214152Sed if (!aAbs) { 57214152Sed // zero / zero = NaN 58214152Sed if (!bAbs) return fromRep(qnanRep); 59214152Sed // zero / anything else = +/- zero 60214152Sed else return fromRep(quotientSign); 61214152Sed } 62214152Sed // anything else / zero = +/- infinity 63214152Sed if (!bAbs) return fromRep(infRep | quotientSign); 64214152Sed 65214152Sed // one or both of a or b is denormal, the other (if applicable) is a 66214152Sed // normal number. Renormalize one or both of a and b, and set scale to 67214152Sed // include the necessary exponent adjustment. 68214152Sed if (aAbs < implicitBit) scale += normalize(&aSignificand); 69214152Sed if (bAbs < implicitBit) scale -= normalize(&bSignificand); 70214152Sed } 71214152Sed 72214152Sed // Or in the implicit significand bit. (If we fell through from the 73214152Sed // denormal path it was already set by normalize( ), but setting it twice 74214152Sed // won't hurt anything.) 75214152Sed aSignificand |= implicitBit; 76214152Sed bSignificand |= implicitBit; 77214152Sed int quotientExponent = aExponent - bExponent + scale; 78214152Sed 79214152Sed // Align the significand of b as a Q31 fixed-point number in the range 80214152Sed // [1, 2.0) and get a Q32 approximate reciprocal using a small minimax 81214152Sed // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2. This 82214152Sed // is accurate to about 3.5 binary digits. 83214152Sed uint32_t q31b = bSignificand << 8; 84214152Sed uint32_t reciprocal = UINT32_C(0x7504f333) - q31b; 85214152Sed 86214152Sed // Now refine the reciprocal estimate using a Newton-Raphson iteration: 87214152Sed // 88214152Sed // x1 = x0 * (2 - x0 * b) 89214152Sed // 90214152Sed // This doubles the number of correct binary digits in the approximation 91214152Sed // with each iteration, so after three iterations, we have about 28 binary 92214152Sed // digits of accuracy. 93214152Sed uint32_t correction; 94214152Sed correction = -((uint64_t)reciprocal * q31b >> 32); 95214152Sed reciprocal = (uint64_t)reciprocal * correction >> 31; 96214152Sed correction = -((uint64_t)reciprocal * q31b >> 32); 97214152Sed reciprocal = (uint64_t)reciprocal * correction >> 31; 98214152Sed correction = -((uint64_t)reciprocal * q31b >> 32); 99214152Sed reciprocal = (uint64_t)reciprocal * correction >> 31; 100214152Sed 101214152Sed // Exhaustive testing shows that the error in reciprocal after three steps 102214152Sed // is in the interval [-0x1.f58108p-31, 0x1.d0e48cp-29], in line with our 103214152Sed // expectations. We bump the reciprocal by a tiny value to force the error 104214152Sed // to be strictly positive (in the range [0x1.4fdfp-37,0x1.287246p-29], to 105214152Sed // be specific). This also causes 1/1 to give a sensible approximation 106214152Sed // instead of zero (due to overflow). 107214152Sed reciprocal -= 2; 108214152Sed 109214152Sed // The numerical reciprocal is accurate to within 2^-28, lies in the 110214152Sed // interval [0x1.000000eep-1, 0x1.fffffffcp-1], and is strictly smaller 111214152Sed // than the true reciprocal of b. Multiplying a by this reciprocal thus 112214152Sed // gives a numerical q = a/b in Q24 with the following properties: 113214152Sed // 114214152Sed // 1. q < a/b 115214152Sed // 2. q is in the interval [0x1.000000eep-1, 0x1.fffffffcp0) 116214152Sed // 3. the error in q is at most 2^-24 + 2^-27 -- the 2^24 term comes 117214152Sed // from the fact that we truncate the product, and the 2^27 term 118214152Sed // is the error in the reciprocal of b scaled by the maximum 119214152Sed // possible value of a. As a consequence of this error bound, 120214152Sed // either q or nextafter(q) is the correctly rounded 121214152Sed rep_t quotient = (uint64_t)reciprocal*(aSignificand << 1) >> 32; 122214152Sed 123214152Sed // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0). 124214152Sed // In either case, we are going to compute a residual of the form 125214152Sed // 126214152Sed // r = a - q*b 127214152Sed // 128214152Sed // We know from the construction of q that r satisfies: 129214152Sed // 130214152Sed // 0 <= r < ulp(q)*b 131214152Sed // 132214152Sed // if r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we 133214152Sed // already have the correct result. The exact halfway case cannot occur. 134214152Sed // We also take this time to right shift quotient if it falls in the [1,2) 135214152Sed // range and adjust the exponent accordingly. 136214152Sed rep_t residual; 137214152Sed if (quotient < (implicitBit << 1)) { 138214152Sed residual = (aSignificand << 24) - quotient * bSignificand; 139214152Sed quotientExponent--; 140214152Sed } else { 141214152Sed quotient >>= 1; 142214152Sed residual = (aSignificand << 23) - quotient * bSignificand; 143214152Sed } 144214152Sed 145214152Sed const int writtenExponent = quotientExponent + exponentBias; 146214152Sed 147214152Sed if (writtenExponent >= maxExponent) { 148214152Sed // If we have overflowed the exponent, return infinity. 149214152Sed return fromRep(infRep | quotientSign); 150214152Sed } 151214152Sed 152214152Sed else if (writtenExponent < 1) { 153214152Sed // Flush denormals to zero. In the future, it would be nice to add 154214152Sed // code to round them correctly. 155214152Sed return fromRep(quotientSign); 156214152Sed } 157214152Sed 158214152Sed else { 159214152Sed const bool round = (residual << 1) > bSignificand; 160214152Sed // Clear the implicit bit 161214152Sed rep_t absResult = quotient & significandMask; 162214152Sed // Insert the exponent 163214152Sed absResult |= (rep_t)writtenExponent << significandBits; 164214152Sed // Round 165214152Sed absResult += round; 166214152Sed // Insert the sign and return 167214152Sed return fromRep(absResult | quotientSign); 168214152Sed } 169214152Sed} 170