fp_add_impl.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //===----- lib/fp_add_impl.inc - floaing point addition -----------*- C -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements soft-float addition with the IEEE-754 default rounding
  10. // (to nearest, ties to even).
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "fp_lib.h"
  14. #include "fp_mode.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)
  25. return fromRep(toRep(a) | quietBit);
  26. // anything + NaN = qNaN
  27. if (bAbs > infRep)
  28. return fromRep(toRep(b) | quietBit);
  29. if (aAbs == infRep) {
  30. // +/-infinity + -/+infinity = qNaN
  31. if ((toRep(a) ^ toRep(b)) == signBit)
  32. return fromRep(qnanRep);
  33. // +/-infinity + anything remaining = +/- infinity
  34. else
  35. return a;
  36. }
  37. // anything remaining + +/-infinity = +/-infinity
  38. if (bAbs == infRep)
  39. return b;
  40. // zero + anything = anything
  41. if (!aAbs) {
  42. // We need to get the sign right for zero + zero.
  43. if (!bAbs)
  44. return fromRep(toRep(a) & toRep(b));
  45. else
  46. return b;
  47. }
  48. // anything + zero = anything
  49. if (!bAbs)
  50. return a;
  51. }
  52. // Swap a and b if necessary so that a has the larger absolute value.
  53. if (bAbs > aAbs) {
  54. const rep_t temp = aRep;
  55. aRep = bRep;
  56. bRep = temp;
  57. }
  58. // Extract the exponent and significand from the (possibly swapped) a and b.
  59. int aExponent = aRep >> significandBits & maxExponent;
  60. int bExponent = bRep >> significandBits & maxExponent;
  61. rep_t aSignificand = aRep & significandMask;
  62. rep_t bSignificand = bRep & significandMask;
  63. // Normalize any denormals, and adjust the exponent accordingly.
  64. if (aExponent == 0)
  65. aExponent = normalize(&aSignificand);
  66. if (bExponent == 0)
  67. bExponent = normalize(&bSignificand);
  68. // The sign of the result is the sign of the larger operand, a. If they
  69. // have opposite signs, we are performing a subtraction. Otherwise, we
  70. // perform addition.
  71. const rep_t resultSign = aRep & signBit;
  72. const bool subtraction = (aRep ^ bRep) & signBit;
  73. // Shift the significands to give us round, guard and sticky, and set the
  74. // implicit significand bit. If we fell through from the denormal path it
  75. // was already set by normalize( ), but setting it twice won't hurt
  76. // anything.
  77. aSignificand = (aSignificand | implicitBit) << 3;
  78. bSignificand = (bSignificand | implicitBit) << 3;
  79. // Shift the significand of b by the difference in exponents, with a sticky
  80. // bottom bit to get rounding correct.
  81. const unsigned int align = (unsigned int)(aExponent - bExponent);
  82. if (align) {
  83. if (align < typeWidth) {
  84. const bool sticky = (bSignificand << (typeWidth - align)) != 0;
  85. bSignificand = bSignificand >> align | sticky;
  86. } else {
  87. bSignificand = 1; // Set the sticky bit. b is known to be non-zero.
  88. }
  89. }
  90. if (subtraction) {
  91. aSignificand -= bSignificand;
  92. // If a == -b, return +zero.
  93. if (aSignificand == 0)
  94. return fromRep(0);
  95. // If partial cancellation occured, we need to left-shift the result
  96. // and adjust the exponent.
  97. if (aSignificand < implicitBit << 3) {
  98. const int shift = rep_clz(aSignificand) - rep_clz(implicitBit << 3);
  99. aSignificand <<= shift;
  100. aExponent -= shift;
  101. }
  102. } else /* addition */ {
  103. aSignificand += bSignificand;
  104. // If the addition carried up, we need to right-shift the result and
  105. // adjust the exponent.
  106. if (aSignificand & implicitBit << 4) {
  107. const bool sticky = aSignificand & 1;
  108. aSignificand = aSignificand >> 1 | sticky;
  109. aExponent += 1;
  110. }
  111. }
  112. // If we have overflowed the type, return +/- infinity.
  113. if (aExponent >= maxExponent)
  114. return fromRep(infRep | resultSign);
  115. if (aExponent <= 0) {
  116. // The result is denormal before rounding. The exponent is zero and we
  117. // need to shift the significand.
  118. const int shift = 1 - aExponent;
  119. const bool sticky = (aSignificand << (typeWidth - shift)) != 0;
  120. aSignificand = aSignificand >> shift | sticky;
  121. aExponent = 0;
  122. }
  123. // Low three bits are round, guard, and sticky.
  124. const int roundGuardSticky = aSignificand & 0x7;
  125. // Shift the significand into place, and mask off the implicit bit.
  126. rep_t result = aSignificand >> 3 & significandMask;
  127. // Insert the exponent and sign.
  128. result |= (rep_t)aExponent << significandBits;
  129. result |= resultSign;
  130. // Perform the final rounding. The result may overflow to infinity, but
  131. // that is the correct result in that case.
  132. switch (__fe_getround()) {
  133. case CRT_FE_TONEAREST:
  134. if (roundGuardSticky > 0x4)
  135. result++;
  136. if (roundGuardSticky == 0x4)
  137. result += result & 1;
  138. break;
  139. case CRT_FE_DOWNWARD:
  140. if (resultSign && roundGuardSticky) result++;
  141. break;
  142. case CRT_FE_UPWARD:
  143. if (!resultSign && roundGuardSticky) result++;
  144. break;
  145. case CRT_FE_TOWARDZERO:
  146. break;
  147. }
  148. if (roundGuardSticky)
  149. __fe_raise_inexact();
  150. return fromRep(result);
  151. }