| Stephen Canon | e508632 | 2010-07-01 15:52:42 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * The LLVM Compiler Infrastructure |
| 3 | * |
| 4 | * This file is distributed under the University of Illinois Open Source |
| 5 | * License. See LICENSE.TXT for details. |
| 6 | */ |
| 7 | |
| 8 | #define DOUBLE_PRECISION |
| 9 | #include "fp_lib.h" |
| 10 | |
| 11 | // This file implements double-precision soft-float multiplication with the |
| 12 | // IEEE-754 default rounding (to nearest, ties to even). |
| 13 | |
| 14 | #define loWord(a) (a & 0xffffffffU) |
| 15 | #define hiWord(a) (a >> 32) |
| 16 | |
| 17 | // 64x64 -> 128 wide multiply for platforms that don't have such an operation; |
| 18 | // some 64-bit platforms have this operation, but they tend to have hardware |
| 19 | // floating-point, so we don't bother with a special case for them here. |
| 20 | static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { |
| 21 | // Each of the component 32x32 -> 64 products |
| 22 | const uint64_t plolo = loWord(a) * loWord(b); |
| 23 | const uint64_t plohi = loWord(a) * hiWord(b); |
| 24 | const uint64_t philo = hiWord(a) * loWord(b); |
| 25 | const uint64_t phihi = hiWord(a) * hiWord(b); |
| 26 | // Sum terms that compute to lo in a way that allows us to get the carry |
| 27 | const uint64_t r0 = loWord(plolo); |
| 28 | const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo); |
| 29 | *lo = r0 + (r1 << 32); |
| 30 | // Sum terms contributing to hi with the carry from lo |
| 31 | *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi; |
| 32 | } |
| 33 | |
| 34 | fp_t __muldf3(fp_t a, fp_t b) { |
| 35 | |
| 36 | const unsigned int aExponent = toRep(a) >> significandBits & maxExponent; |
| 37 | const unsigned int bExponent = toRep(b) >> significandBits & maxExponent; |
| 38 | const rep_t productSign = (toRep(a) ^ toRep(b)) & signBit; |
| 39 | |
| 40 | rep_t aSignificand = toRep(a) & significandMask; |
| 41 | rep_t bSignificand = toRep(b) & significandMask; |
| 42 | int scale = 0; |
| 43 | |
| 44 | // Detect if a or b is zero, denormal, infinity, or NaN. |
| 45 | if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) { |
| 46 | |
| 47 | const rep_t aAbs = toRep(a) & absMask; |
| 48 | const rep_t bAbs = toRep(b) & absMask; |
| 49 | |
| 50 | // NaN * anything = qNaN |
| 51 | if (aAbs > infRep) return fromRep(toRep(a) | quietBit); |
| 52 | // anything * NaN = qNaN |
| 53 | if (bAbs > infRep) return fromRep(toRep(b) | quietBit); |
| 54 | |
| 55 | if (aAbs == infRep) { |
| 56 | // infinity * non-zero = +/- infinity |
| 57 | if (bAbs) return fromRep(aAbs | productSign); |
| 58 | // infinity * zero = NaN |
| 59 | else return fromRep(qnanRep); |
| 60 | } |
| 61 | |
| 62 | if (bAbs == infRep) { |
| 63 | // non-zero * infinity = +/- infinity |
| 64 | if (aAbs) return fromRep(bAbs | productSign); |
| 65 | // zero * infinity = NaN |
| 66 | else return fromRep(qnanRep); |
| 67 | } |
| 68 | |
| 69 | // zero * anything = +/- zero |
| 70 | if (!aAbs) return fromRep(productSign); |
| 71 | // anything * zero = +/- zero |
| 72 | if (!bAbs) return fromRep(productSign); |
| 73 | |
| 74 | // one or both of a or b is denormal, the other (if applicable) is a |
| 75 | // normal number. Renormalize one or both of a and b, and set scale to |
| 76 | // include the necessary exponent adjustment. |
| 77 | if (aAbs < implicitBit) scale += normalize(&aSignificand); |
| 78 | if (bAbs < implicitBit) scale += normalize(&bSignificand); |
| 79 | } |
| 80 | |
| 81 | // Or in the implicit significand bit. (If we fell through from the |
| 82 | // denormal path it was already set by normalize( ), but setting it twice |
| 83 | // won't hurt anything.) |
| 84 | aSignificand |= implicitBit; |
| 85 | bSignificand |= implicitBit; |
| 86 | |
| 87 | // Get the significand of a*b. Before multiplying the significands, shift |
| 88 | // one of them left to left-align it in the field. Thus, the product will |
| 89 | // have (exponentBits + 2) integral digits, all but two of which must be |
| 90 | // zero. Normalizing this result is just a conditional left-shift by one |
| 91 | // and bumping the exponent accordingly. |
| 92 | rep_t productHi, productLo; |
| 93 | wideMultiply(aSignificand, bSignificand << exponentBits, |
| 94 | &productHi, &productLo); |
| 95 | |
| 96 | int productExponent = aExponent + bExponent - exponentBias + scale; |
| 97 | |
| 98 | // Normalize the significand, adjust exponent if needed. |
| 99 | if (productHi & implicitBit) productExponent++; |
| 100 | else wideLeftShift(&productHi, &productLo, 1); |
| 101 | |
| 102 | // If we have overflowed the type, return +/- infinity. |
| 103 | if (productExponent >= maxExponent) return fromRep(infRep | productSign); |
| 104 | |
| 105 | if (productExponent <= 0) { |
| 106 | // Result is denormal before rounding |
| 107 | // |
| 108 | // If the result is so small that it just underflows to zero, return |
| 109 | // a zero of the appropriate sign. Mathematically there is no need to |
| 110 | // handle this case separately, but we make it a special case to |
| 111 | // simplify the shift logic. |
| 112 | const int shift = 1 - productExponent; |
| 113 | if (shift >= typeWidth) return fromRep(productSign); |
| 114 | |
| 115 | // Otherwise, shift the significand of the result so that the round |
| 116 | // bit is the high bit of productLo. |
| 117 | wideRightShiftWithSticky(&productHi, &productLo, shift); |
| 118 | } |
| 119 | |
| 120 | else { |
| 121 | // Result is normal before rounding; insert the exponent. |
| 122 | productHi &= significandMask; |
| 123 | productHi |= (rep_t)productExponent << significandBits; |
| 124 | } |
| 125 | |
| 126 | // Insert the sign of the result: |
| 127 | productHi |= productSign; |
| 128 | |
| 129 | // Final rounding. The final result may overflow to infinity, or underflow |
| 130 | // to zero, but those are the correct results in those cases. We use the |
| 131 | // default IEEE-754 round-to-nearest, ties-to-even rounding mode. |
| 132 | if (productLo > signBit) productHi++; |
| 133 | if (productLo == signBit) productHi += productHi & 1; |
| 134 | return fromRep(productHi); |
| 135 | } |