fp_add_impl.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //===----- lib/fp_add_impl.inc - floaing point addition -----------*- 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 addition with the IEEE-754 default rounding
  11. // (to nearest, ties to even).
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "fp_lib.h"
  15. static __inline fp_t __addXf3__(fp_t a, fp_t b) {
  16. rep_t aRep = toRep(a);
  17. rep_t bRep = toRep(b);
  18. const rep_t aAbs = aRep & absMask;
  19. const rep_t bAbs = bRep & absMask;
  20. // Detect if a or b is zero, infinity, or NaN.
  21. if (aAbs - REP_C(1) >= infRep - REP_C(1) ||
  22. bAbs - REP_C(1) >= infRep - REP_C(1)) {
  23. // NaN + anything = qNaN
  24. if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
  25. // anything + NaN = qNaN
  26. if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
  27. if (aAbs == infRep) {
  28. // +/-infinity + -/+infinity = qNaN
  29. if ((toRep(a) ^ toRep(b)) == signBit) return fromRep(qnanRep);
  30. // +/-infinity + anything remaining = +/- infinity
  31. else return a;
  32. }
  33. // anything remaining + +/-infinity = +/-infinity
  34. if (bAbs == infRep) return b;
  35. // zero + anything = anything
  36. if (!aAbs) {
  37. // but we need to get the sign right for zero + zero
  38. if (!bAbs) return fromRep(toRep(a) & toRep(b));
  39. else return b;
  40. }
  41. // anything + zero = anything
  42. if (!bAbs) return a;
  43. }
  44. // Swap a and b if necessary so that a has the larger absolute value.
  45. if (bAbs > aAbs) {
  46. const rep_t temp = aRep;
  47. aRep = bRep;
  48. bRep = temp;
  49. }
  50. // Extract the exponent and significand from the (possibly swapped) a and b.
  51. int aExponent = aRep >> significandBits & maxExponent;
  52. int bExponent = bRep >> significandBits & maxExponent;
  53. rep_t aSignificand = aRep & significandMask;
  54. rep_t bSignificand = bRep & significandMask;
  55. // Normalize any denormals, and adjust the exponent accordingly.
  56. if (aExponent == 0) aExponent = normalize(&aSignificand);
  57. if (bExponent == 0) bExponent = normalize(&bSignificand);
  58. // The sign of the result is the sign of the larger operand, a. If they
  59. // have opposite signs, we are performing a subtraction; otherwise addition.
  60. const rep_t resultSign = aRep & signBit;
  61. const bool subtraction = (aRep ^ bRep) & signBit;
  62. // Shift the significands to give us round, guard and sticky, and or in the
  63. // implicit significand bit. (If we fell through from the denormal path it
  64. // was already set by normalize( ), but setting it twice won't hurt
  65. // anything.)
  66. aSignificand = (aSignificand | implicitBit) << 3;
  67. bSignificand = (bSignificand | implicitBit) << 3;
  68. // Shift the significand of b by the difference in exponents, with a sticky
  69. // bottom bit to get rounding correct.
  70. const unsigned int align = aExponent - bExponent;
  71. if (align) {
  72. if (align < typeWidth) {
  73. const bool sticky = bSignificand << (typeWidth - align);
  74. bSignificand = bSignificand >> align | sticky;
  75. } else {
  76. bSignificand = 1; // sticky; b is known to be non-zero.
  77. }
  78. }
  79. if (subtraction) {
  80. aSignificand -= bSignificand;
  81. // If a == -b, return +zero.
  82. if (aSignificand == 0) return fromRep(0);
  83. // If partial cancellation occured, we need to left-shift the result
  84. // and adjust the exponent:
  85. if (aSignificand < implicitBit << 3) {
  86. const int shift = rep_clz(aSignificand) - rep_clz(implicitBit << 3);
  87. aSignificand <<= shift;
  88. aExponent -= shift;
  89. }
  90. }
  91. else /* addition */ {
  92. aSignificand += bSignificand;
  93. // If the addition carried up, we need to right-shift the result and
  94. // adjust the exponent:
  95. if (aSignificand & implicitBit << 4) {
  96. const bool sticky = aSignificand & 1;
  97. aSignificand = aSignificand >> 1 | sticky;
  98. aExponent += 1;
  99. }
  100. }
  101. // If we have overflowed the type, return +/- infinity:
  102. if (aExponent >= maxExponent) return fromRep(infRep | resultSign);
  103. if (aExponent <= 0) {
  104. // Result is denormal before rounding; the exponent is zero and we
  105. // need to shift the significand.
  106. const int shift = 1 - aExponent;
  107. const bool sticky = aSignificand << (typeWidth - shift);
  108. aSignificand = aSignificand >> shift | sticky;
  109. aExponent = 0;
  110. }
  111. // Low three bits are round, guard, and sticky.
  112. const int roundGuardSticky = aSignificand & 0x7;
  113. // Shift the significand into place, and mask off the implicit bit.
  114. rep_t result = aSignificand >> 3 & significandMask;
  115. // Insert the exponent and sign.
  116. result |= (rep_t)aExponent << significandBits;
  117. result |= resultSign;
  118. // Final rounding. The result may overflow to infinity, but that is the
  119. // correct result in that case.
  120. if (roundGuardSticky > 0x4) result++;
  121. if (roundGuardSticky == 0x4) result += result & 1;
  122. return fromRep(result);
  123. }