Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 1 | //===-- lib/divsf3.c - Single-precision division ------------------*- C -*-===// |
| 2 | // |
Chandler Carruth | 7a739a0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements single-precision soft-float division |
| 10 | // with the IEEE-754 default rounding (to nearest, ties to even). |
| 11 | // |
| 12 | // For simplicity, this implementation currently flushes denormals to zero. |
| 13 | // It should be a fairly straightforward exercise to implement gradual |
| 14 | // underflow with correct rounding. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #define SINGLE_PRECISION |
| 19 | #include "fp_lib.h" |
| 20 | |
Joerg Sonnenberger | bfbb8bb | 2014-03-01 15:30:50 +0000 | [diff] [blame] | 21 | COMPILER_RT_ABI fp_t |
| 22 | __divsf3(fp_t a, fp_t b) { |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 23 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 24 | const unsigned int aExponent = toRep(a) >> significandBits & maxExponent; |
| 25 | const unsigned int bExponent = toRep(b) >> significandBits & maxExponent; |
| 26 | const rep_t quotientSign = (toRep(a) ^ toRep(b)) & signBit; |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 27 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 28 | rep_t aSignificand = toRep(a) & significandMask; |
| 29 | rep_t bSignificand = toRep(b) & significandMask; |
| 30 | int scale = 0; |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 31 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 32 | // Detect if a or b is zero, denormal, infinity, or NaN. |
| 33 | if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) { |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 34 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 35 | const rep_t aAbs = toRep(a) & absMask; |
| 36 | const rep_t bAbs = toRep(b) & absMask; |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 37 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 38 | // NaN / anything = qNaN |
| 39 | if (aAbs > infRep) return fromRep(toRep(a) | quietBit); |
| 40 | // anything / NaN = qNaN |
| 41 | if (bAbs > infRep) return fromRep(toRep(b) | quietBit); |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 42 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 43 | if (aAbs == infRep) { |
| 44 | // infinity / infinity = NaN |
| 45 | if (bAbs == infRep) return fromRep(qnanRep); |
| 46 | // infinity / anything else = +/- infinity |
| 47 | else return fromRep(aAbs | quotientSign); |
| 48 | } |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 49 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 50 | // anything else / infinity = +/- 0 |
| 51 | if (bAbs == infRep) return fromRep(quotientSign); |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 52 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 53 | if (!aAbs) { |
| 54 | // zero / zero = NaN |
| 55 | if (!bAbs) return fromRep(qnanRep); |
| 56 | // zero / anything else = +/- zero |
| 57 | else return fromRep(quotientSign); |
| 58 | } |
| 59 | // anything else / zero = +/- infinity |
| 60 | if (!bAbs) return fromRep(infRep | quotientSign); |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 61 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 62 | // one or both of a or b is denormal, the other (if applicable) is a |
| 63 | // normal number. Renormalize one or both of a and b, and set scale to |
| 64 | // include the necessary exponent adjustment. |
| 65 | if (aAbs < implicitBit) scale += normalize(&aSignificand); |
| 66 | if (bAbs < implicitBit) scale -= normalize(&bSignificand); |
| 67 | } |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 68 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 69 | // Or in the implicit significand bit. (If we fell through from the |
| 70 | // denormal path it was already set by normalize( ), but setting it twice |
| 71 | // won't hurt anything.) |
| 72 | aSignificand |= implicitBit; |
| 73 | bSignificand |= implicitBit; |
| 74 | int quotientExponent = aExponent - bExponent + scale; |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 75 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 76 | // Align the significand of b as a Q31 fixed-point number in the range |
| 77 | // [1, 2.0) and get a Q32 approximate reciprocal using a small minimax |
| 78 | // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2. This |
| 79 | // is accurate to about 3.5 binary digits. |
| 80 | uint32_t q31b = bSignificand << 8; |
| 81 | uint32_t reciprocal = UINT32_C(0x7504f333) - q31b; |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 82 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 83 | // Now refine the reciprocal estimate using a Newton-Raphson iteration: |
| 84 | // |
| 85 | // x1 = x0 * (2 - x0 * b) |
| 86 | // |
| 87 | // This doubles the number of correct binary digits in the approximation |
| 88 | // with each iteration, so after three iterations, we have about 28 binary |
| 89 | // digits of accuracy. |
| 90 | uint32_t correction; |
| 91 | correction = -((uint64_t)reciprocal * q31b >> 32); |
| 92 | reciprocal = (uint64_t)reciprocal * correction >> 31; |
| 93 | correction = -((uint64_t)reciprocal * q31b >> 32); |
| 94 | reciprocal = (uint64_t)reciprocal * correction >> 31; |
| 95 | correction = -((uint64_t)reciprocal * q31b >> 32); |
| 96 | reciprocal = (uint64_t)reciprocal * correction >> 31; |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 97 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 98 | // Exhaustive testing shows that the error in reciprocal after three steps |
| 99 | // is in the interval [-0x1.f58108p-31, 0x1.d0e48cp-29], in line with our |
| 100 | // expectations. We bump the reciprocal by a tiny value to force the error |
| 101 | // to be strictly positive (in the range [0x1.4fdfp-37,0x1.287246p-29], to |
| 102 | // be specific). This also causes 1/1 to give a sensible approximation |
| 103 | // instead of zero (due to overflow). |
| 104 | reciprocal -= 2; |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 105 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 106 | // The numerical reciprocal is accurate to within 2^-28, lies in the |
| 107 | // interval [0x1.000000eep-1, 0x1.fffffffcp-1], and is strictly smaller |
| 108 | // than the true reciprocal of b. Multiplying a by this reciprocal thus |
| 109 | // gives a numerical q = a/b in Q24 with the following properties: |
| 110 | // |
| 111 | // 1. q < a/b |
| 112 | // 2. q is in the interval [0x1.000000eep-1, 0x1.fffffffcp0) |
| 113 | // 3. the error in q is at most 2^-24 + 2^-27 -- the 2^24 term comes |
| 114 | // from the fact that we truncate the product, and the 2^27 term |
| 115 | // is the error in the reciprocal of b scaled by the maximum |
| 116 | // possible value of a. As a consequence of this error bound, |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 117 | // either q or nextafter(q) is the correctly rounded |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 118 | rep_t quotient = (uint64_t)reciprocal*(aSignificand << 1) >> 32; |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 119 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 120 | // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0). |
| 121 | // In either case, we are going to compute a residual of the form |
| 122 | // |
| 123 | // r = a - q*b |
| 124 | // |
| 125 | // We know from the construction of q that r satisfies: |
| 126 | // |
| 127 | // 0 <= r < ulp(q)*b |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 128 | // |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 129 | // if r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we |
| 130 | // already have the correct result. The exact halfway case cannot occur. |
| 131 | // We also take this time to right shift quotient if it falls in the [1,2) |
| 132 | // range and adjust the exponent accordingly. |
| 133 | rep_t residual; |
| 134 | if (quotient < (implicitBit << 1)) { |
| 135 | residual = (aSignificand << 24) - quotient * bSignificand; |
| 136 | quotientExponent--; |
| 137 | } else { |
| 138 | quotient >>= 1; |
| 139 | residual = (aSignificand << 23) - quotient * bSignificand; |
| 140 | } |
| 141 | |
| 142 | const int writtenExponent = quotientExponent + exponentBias; |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 143 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 144 | if (writtenExponent >= maxExponent) { |
| 145 | // If we have overflowed the exponent, return infinity. |
| 146 | return fromRep(infRep | quotientSign); |
| 147 | } |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 148 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 149 | else if (writtenExponent < 1) { |
| 150 | // Flush denormals to zero. In the future, it would be nice to add |
| 151 | // code to round them correctly. |
| 152 | return fromRep(quotientSign); |
| 153 | } |
Yi Kong | e527a51 | 2018-12-10 22:52:59 +0000 | [diff] [blame] | 154 | |
Stephen Canon | c9d2b05 | 2010-07-04 06:15:44 +0000 | [diff] [blame] | 155 | else { |
| 156 | const bool round = (residual << 1) > bSignificand; |
| 157 | // Clear the implicit bit |
| 158 | rep_t absResult = quotient & significandMask; |
| 159 | // Insert the exponent |
| 160 | absResult |= (rep_t)writtenExponent << significandBits; |
| 161 | // Round |
| 162 | absResult += round; |
| 163 | // Insert the sign and return |
| 164 | return fromRep(absResult | quotientSign); |
| 165 | } |
| 166 | } |
Saleem Abdulrasool | 99e2e66 | 2017-05-16 16:41:37 +0000 | [diff] [blame] | 167 | |
| 168 | #if defined(__ARM_EABI__) |
Eli Friedman | 83774b4 | 2017-10-03 21:25:07 +0000 | [diff] [blame] | 169 | #if defined(COMPILER_RT_ARMHF_TARGET) |
Saleem Abdulrasool | 99e2e66 | 2017-05-16 16:41:37 +0000 | [diff] [blame] | 170 | AEABI_RTABI fp_t __aeabi_fdiv(fp_t a, fp_t b) { |
| 171 | return __divsf3(a, b); |
| 172 | } |
Eli Friedman | 83774b4 | 2017-10-03 21:25:07 +0000 | [diff] [blame] | 173 | #else |
| 174 | AEABI_RTABI fp_t __aeabi_fdiv(fp_t a, fp_t b) COMPILER_RT_ALIAS(__divsf3); |
Saleem Abdulrasool | 99e2e66 | 2017-05-16 16:41:37 +0000 | [diff] [blame] | 175 | #endif |
Eli Friedman | 83774b4 | 2017-10-03 21:25:07 +0000 | [diff] [blame] | 176 | #endif |