Make AsmPrinter::EmitLabelOffsetDifference a static helper and simplify.
[oota-llvm.git] / lib / Support / APFloat.cpp
index 4359061e4334960df987697b35ebebd0e236a354..f63d7748973c74262842cdc2809acde8290718a0 100644 (file)
@@ -35,8 +35,7 @@ using namespace llvm;
 
 /* Assumed in hexadecimal significand parsing, and conversion to
    hexadecimal strings.  */
-#define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
-COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
+static_assert(integerPartWidth % 4 == 0, "Part width must be divisible by 4!");
 
 namespace llvm {
 
@@ -212,15 +211,15 @@ skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end,
 {
   StringRef::iterator p = begin;
   *dot = end;
-  while (*p == '0' && p != end)
+  while (p != end && *p == '0')
     p++;
 
-  if (*p == '.') {
+  if (p != end && *p == '.') {
     *dot = p++;
 
     assert(end - begin != 1 && "Significand has no digits");
 
-    while (*p == '0' && p != end)
+    while (p != end && *p == '0')
       p++;
   }
 
@@ -955,7 +954,7 @@ APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
   // exponent accordingly.
   exponent += 1;
 
-  if (addend) {
+  if (addend && addend->isNonZero()) {
     // The intermediate result of the multiplication has "2 * precision" 
     // signicant bit; adjust the addend to be consistent with mul result.
     //
@@ -1722,8 +1721,7 @@ APFloat::remainder(const APFloat &rhs)
     return fs;
 
   int parts = partCount();
-  auto XOwner = make_unique<integerPart[]>(parts);
-  auto x = XOwner.get();
+  integerPart *x = new integerPart[parts];
   bool ignored;
   fs = V.convertToInteger(x, parts * integerPartWidth, true,
                           rmNearestTiesToEven, &ignored);
@@ -1742,6 +1740,7 @@ APFloat::remainder(const APFloat &rhs)
 
   if (isZero())
     sign = origSign;    // IEEE754 requires this
+  delete[] x;
   return fs;
 }
 
@@ -1762,8 +1761,7 @@ APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
       return fs;
 
     int parts = partCount();
-    auto XOwner = make_unique<integerPart[]>(parts);
-    auto x = XOwner.get();
+    integerPart *x = new integerPart[parts];
     bool ignored;
     fs = V.convertToInteger(x, parts * integerPartWidth, true,
                             rmTowardZero, &ignored);
@@ -1782,6 +1780,7 @@ APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
 
     if (isZero())
       sign = origSign;    // IEEE754 requires this
+    delete[] x;
   }
   return fs;
 }
@@ -1801,7 +1800,7 @@ APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
      extended-precision calculation.  */
   if (isFiniteNonZero() &&
       multiplicand.isFiniteNonZero() &&
-      addend.isFiniteNonZero()) {
+      addend.isFinite()) {
     lostFraction lost_fraction;
 
     lost_fraction = multiplySignificand(multiplicand, &addend);
@@ -2284,14 +2283,15 @@ APFloat::convertFromSignExtendedInteger(const integerPart *src,
 
   if (isSigned &&
       APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
-    auto C = make_unique<integerPart[]>(srcCount);
-    auto copy = C.get();
+    integerPart *copy;
 
     /* If we're signed and negative negate a copy.  */
     sign = true;
+    copy = new integerPart[srcCount];
     APInt::tcAssign(copy, src, srcCount);
     APInt::tcNegate(copy, srcCount);
     status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
+    delete [] copy;
   } else {
     sign = false;
     status = convertFromUnsignedParts(src, srcCount, rounding_mode);
@@ -2544,6 +2544,7 @@ APFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode)
     /* Overflow and round.  */
     fs = handleOverflow(rounding_mode);
   } else {
+    integerPart *decSignificand;
     unsigned int partCount;
 
     /* A tight upper bound on number of bits required to hold an
@@ -2552,8 +2553,7 @@ APFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode)
        tcMultiplyPart.  */
     partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
     partCount = partCountForBits(1 + 196 * partCount / 59);
-    auto DecSignificandOwner = make_unique<integerPart[]>(partCount + 1);
-    auto decSignificand = DecSignificandOwner.get();
+    decSignificand = new integerPart[partCount + 1];
     partCount = 0;
 
     /* Convert to binary efficiently - we do almost all multiplication
@@ -2594,6 +2594,8 @@ APFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode)
     category = fcNormal;
     fs = roundSignificandWithExponent(decSignificand, partCount,
                                       D.exponent, rounding_mode);
+
+    delete [] decSignificand;
   }
 
   return fs;
@@ -3374,7 +3376,9 @@ void APFloat::makeLargest(bool Negative) {
   // internal consistency.
   const unsigned NumUnusedHighBits =
     PartCount*integerPartWidth - semantics->precision;
-  significand[PartCount - 1] = ~integerPart(0) >> NumUnusedHighBits;
+  significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth)
+                                   ? (~integerPart(0) >> NumUnusedHighBits)
+                                   : 0;
 }
 
 /// Make this number the smallest magnitude denormal number in the given
@@ -3901,3 +3905,20 @@ APFloat::makeZero(bool Negative) {
   exponent = semantics->minExponent-1;
   APInt::tcSet(significandParts(), 0, partCount());  
 }
+
+APFloat llvm::scalbn(APFloat X, int Exp) {
+  if (X.isInfinity() || X.isZero() || X.isNaN())
+    return std::move(X);
+
+  auto MaxExp = X.getSemantics().maxExponent;
+  auto MinExp = X.getSemantics().minExponent;
+  if (Exp > (MaxExp - X.exponent))
+    // Overflow saturates to infinity.
+    return APFloat::getInf(X.getSemantics(), X.isNegative());
+  if (Exp < (MinExp - X.exponent))
+    // Underflow saturates to zero.
+    return APFloat::getZero(X.getSemantics(), X.isNegative());
+
+  X.exponent += Exp;
+  return std::move(X);
+}