fp_mul_impl.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //===---- lib/fp_mul_impl.inc - floating point multiplication -----*- C -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements soft-float multiplication with the IEEE-754 default
  11. // rounding (to nearest, ties to even).
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "fp_lib.h"
  15. static __inline fp_t __mulXf3__(fp_t a, fp_t b) {
  16. const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
  17. const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
  18. const rep_t productSign = (toRep(a) ^ toRep(b)) & signBit;
  19. rep_t aSignificand = toRep(a) & significandMask;
  20. rep_t bSignificand = toRep(b) & significandMask;
  21. int scale = 0;
  22. // Detect if a or b is zero, denormal, infinity, or NaN.
  23. if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
  24. const rep_t aAbs = toRep(a) & absMask;
  25. const rep_t bAbs = toRep(b) & absMask;
  26. // NaN * anything = qNaN
  27. if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
  28. // anything * NaN = qNaN
  29. if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
  30. if (aAbs == infRep) {
  31. // infinity * non-zero = +/- infinity
  32. if (bAbs) return fromRep(aAbs | productSign);
  33. // infinity * zero = NaN
  34. else return fromRep(qnanRep);
  35. }
  36. if (bAbs == infRep) {
  37. //? non-zero * infinity = +/- infinity
  38. if (aAbs) return fromRep(bAbs | productSign);
  39. // zero * infinity = NaN
  40. else return fromRep(qnanRep);
  41. }
  42. // zero * anything = +/- zero
  43. if (!aAbs) return fromRep(productSign);
  44. // anything * zero = +/- zero
  45. if (!bAbs) return fromRep(productSign);
  46. // one or both of a or b is denormal, the other (if applicable) is a
  47. // normal number. Renormalize one or both of a and b, and set scale to
  48. // include the necessary exponent adjustment.
  49. if (aAbs < implicitBit) scale += normalize(&aSignificand);
  50. if (bAbs < implicitBit) scale += normalize(&bSignificand);
  51. }
  52. // Or in the implicit significand bit. (If we fell through from the
  53. // denormal path it was already set by normalize( ), but setting it twice
  54. // won't hurt anything.)
  55. aSignificand |= implicitBit;
  56. bSignificand |= implicitBit;
  57. // Get the significand of a*b. Before multiplying the significands, shift
  58. // one of them left to left-align it in the field. Thus, the product will
  59. // have (exponentBits + 2) integral digits, all but two of which must be
  60. // zero. Normalizing this result is just a conditional left-shift by one
  61. // and bumping the exponent accordingly.
  62. rep_t productHi, productLo;
  63. wideMultiply(aSignificand, bSignificand << exponentBits,
  64. &productHi, &productLo);
  65. int productExponent = aExponent + bExponent - exponentBias + scale;
  66. // Normalize the significand, adjust exponent if needed.
  67. if (productHi & implicitBit) productExponent++;
  68. else wideLeftShift(&productHi, &productLo, 1);
  69. // If we have overflowed the type, return +/- infinity.
  70. if (productExponent >= maxExponent) return fromRep(infRep | productSign);
  71. if (productExponent <= 0) {
  72. // Result is denormal before rounding
  73. //
  74. // If the result is so small that it just underflows to zero, return
  75. // a zero of the appropriate sign. Mathematically there is no need to
  76. // handle this case separately, but we make it a special case to
  77. // simplify the shift logic.
  78. const unsigned int shift = REP_C(1) - (unsigned int)productExponent;
  79. if (shift >= typeWidth) return fromRep(productSign);
  80. // Otherwise, shift the significand of the result so that the round
  81. // bit is the high bit of productLo.
  82. wideRightShiftWithSticky(&productHi, &productLo, shift);
  83. }
  84. else {
  85. // Result is normal before rounding; insert the exponent.
  86. productHi &= significandMask;
  87. productHi |= (rep_t)productExponent << significandBits;
  88. }
  89. // Insert the sign of the result:
  90. productHi |= productSign;
  91. // Final rounding. The final result may overflow to infinity, or underflow
  92. // to zero, but those are the correct results in those cases. We use the
  93. // default IEEE-754 round-to-nearest, ties-to-even rounding mode.
  94. if (productLo > signBit) productHi++;
  95. if (productLo == signBit) productHi += productHi & 1;
  96. return fromRep(productHi);
  97. }