De-identifying per sabre review
[oota-llvm.git] / lib / Support / APFloat.cpp
index de333829dae7e6a3b31133b7bd8ccb578d3a18ee..d8d414d7ea5a166fd15dc343ba6d4539f32e59cc 100644 (file)
@@ -14,9 +14,8 @@
 
 #include "llvm/ADT/APFloat.h"
 #include "llvm/ADT/FoldingSet.h"
-#include <cassert>
-#include <cstring>
 #include "llvm/Support/MathExtras.h"
+#include <cstring>
 
 using namespace llvm;
 
@@ -24,6 +23,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);
 
 namespace llvm {
@@ -599,7 +599,8 @@ APFloat::copySignificand(const APFloat &rhs)
 }
 
 /* Make this number a NaN, with an arbitrary but deterministic value
-   for the significand.  */
+   for the significand.  If double or longer, this is a signalling NaN,
+   which may not be ideal. */
 void
 APFloat::makeNaN(void)
 {
@@ -695,7 +696,7 @@ APFloat::~APFloat()
 
 // Profile - This method 'profiles' an APFloat for use with FoldingSet.
 void APFloat::Profile(FoldingSetNodeID& ID) const {
-  ID.Add(convertToAPInt());
+  ID.Add(bitcastToAPInt());
 }
 
 unsigned int
@@ -788,6 +789,7 @@ APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
   integerPart scratch[4];
   integerPart *fullSignificand;
   lostFraction lost_fraction;
+  bool ignored;
 
   assert(semantics == rhs.semantics);
 
@@ -836,7 +838,7 @@ APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
     semantics = &extendedSemantics;
 
     APFloat extendedAddend(*addend);
-    status = extendedAddend.convert(extendedSemantics, rmTowardZero);
+    status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
     assert(status == opOK);
     lost_fraction = addOrSubtractSignificand(extendedAddend, false);
 
@@ -1404,6 +1406,42 @@ APFloat::divideSpecials(const APFloat &rhs)
   }
 }
 
+APFloat::opStatus
+APFloat::modSpecials(const APFloat &rhs)
+{
+  switch(convolve(category, rhs.category)) {
+  default:
+    assert(0);
+
+  case convolve(fcNaN, fcZero):
+  case convolve(fcNaN, fcNormal):
+  case convolve(fcNaN, fcInfinity):
+  case convolve(fcNaN, fcNaN):
+  case convolve(fcZero, fcInfinity):
+  case convolve(fcZero, fcNormal):
+  case convolve(fcNormal, fcInfinity):
+    return opOK;
+
+  case convolve(fcZero, fcNaN):
+  case convolve(fcNormal, fcNaN):
+  case convolve(fcInfinity, fcNaN):
+    category = fcNaN;
+    copySignificand(rhs);
+    return opOK;
+
+  case convolve(fcNormal, fcZero):
+  case convolve(fcInfinity, fcZero):
+  case convolve(fcInfinity, fcNormal):
+  case convolve(fcInfinity, fcInfinity):
+  case convolve(fcZero, fcZero):
+    makeNaN();
+    return opInvalidOp;
+
+  case convolve(fcNormal, fcNormal):
+    return opOK;
+  }
+}
+
 /* Change sign.  */
 void
 APFloat::changeSign()
@@ -1513,9 +1551,9 @@ APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
   return fs;
 }
 
-/* Normalized remainder.  This is not currently doing TRT.  */
+/* Normalized remainder.  This is not currently correct in all cases.  */
 APFloat::opStatus
-APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
+APFloat::remainder(const APFloat &rhs)
 {
   opStatus fs;
   APFloat V = *this;
@@ -1528,8 +1566,9 @@ APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
 
   int parts = partCount();
   integerPart *x = new integerPart[parts];
+  bool ignored;
   fs = V.convertToInteger(x, parts * integerPartWidth, true,
-                          rmNearestTiesToEven);
+                          rmNearestTiesToEven, &ignored);
   if (fs==opInvalidOp)
     return fs;
 
@@ -1537,10 +1576,10 @@ APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
                                         rmNearestTiesToEven);
   assert(fs==opOK);   // should always work
 
-  fs = V.multiply(rhs, rounding_mode);
+  fs = V.multiply(rhs, rmNearestTiesToEven);
   assert(fs==opOK || fs==opInexact);   // should not overflow or underflow
 
-  fs = subtract(V, rounding_mode);
+  fs = subtract(V, rmNearestTiesToEven);
   assert(fs==opOK || fs==opInexact);   // likewise
 
   if (isZero())
@@ -1549,6 +1588,48 @@ APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
   return fs;
 }
 
+/* Normalized llvm frem (C fmod).  
+   This is not currently correct in all cases.  */
+APFloat::opStatus
+APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
+{
+  opStatus fs;
+  assertArithmeticOK(*semantics);
+  fs = modSpecials(rhs);
+
+  if (category == fcNormal && rhs.category == fcNormal) {
+    APFloat V = *this;
+    unsigned int origSign = sign;
+
+    fs = V.divide(rhs, rmNearestTiesToEven);
+    if (fs == opDivByZero)
+      return fs;
+
+    int parts = partCount();
+    integerPart *x = new integerPart[parts];
+    bool ignored;
+    fs = V.convertToInteger(x, parts * integerPartWidth, true,
+                            rmTowardZero, &ignored);
+    if (fs==opInvalidOp)
+      return fs;
+
+    fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
+                                          rmNearestTiesToEven);
+    assert(fs==opOK);   // should always work
+
+    fs = V.multiply(rhs, rounding_mode);
+    assert(fs==opOK || fs==opInexact);   // should not overflow or underflow
+
+    fs = subtract(V, rounding_mode);
+    assert(fs==opOK || fs==opInexact);   // likewise
+
+    if (isZero())
+      sign = origSign;    // IEEE754 requires this
+    delete[] x;
+  }
+  return fs;
+}
+
 /* Normalized fused-multiply-add.  */
 APFloat::opStatus
 APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
@@ -1670,9 +1751,16 @@ APFloat::compare(const APFloat &rhs) const
   return result;
 }
 
+/// APFloat::convert - convert a value of one floating point type to another.
+/// The return value corresponds to the IEEE754 exceptions.  *losesInfo
+/// records whether the transformation lost information, i.e. whether
+/// converting the result back to the original type will produce the
+/// original value (this is almost the same as return value==fsOK, but there
+/// are edge cases where this is not so).
+
 APFloat::opStatus
 APFloat::convert(const fltSemantics &toSemantics,
-                 roundingMode rounding_mode)
+                 roundingMode rounding_mode, bool *losesInfo)
 {
   lostFraction lostFraction;
   unsigned int newPartCount, oldPartCount;
@@ -1718,15 +1806,32 @@ APFloat::convert(const fltSemantics &toSemantics,
     exponent += toSemantics.precision - semantics->precision;
     semantics = &toSemantics;
     fs = normalize(rounding_mode, lostFraction);
+    *losesInfo = (fs != opOK);
   } else if (category == fcNaN) {
     int shift = toSemantics.precision - semantics->precision;
     // Do this now so significandParts gets the right answer
+    const fltSemantics *oldSemantics = semantics;
     semantics = &toSemantics;
+    *losesInfo = false;
     // No normalization here, just truncate
     if (shift>0)
       APInt::tcShiftLeft(significandParts(), newPartCount, shift);
-    else if (shift < 0)
-      APInt::tcShiftRight(significandParts(), newPartCount, -shift);
+    else if (shift < 0) {
+      unsigned ushift = -shift;
+      // Figure out if we are losing information.  This happens
+      // if are shifting out something other than 0s, or if the x87 long
+      // double input did not have its integer bit set (pseudo-NaN), or if the
+      // x87 long double input did not have its QNan bit set (because the x87
+      // hardware sets this bit when converting a lower-precision NaN to
+      // x87 long double).
+      if (APInt::tcLSB(significandParts(), newPartCount) < ushift)
+        *losesInfo = true;
+      if (oldSemantics == &APFloat::x87DoubleExtended && 
+          (!(*significandParts() & 0x8000000000000000ULL) ||
+           !(*significandParts() & 0x4000000000000000ULL)))
+        *losesInfo = true;
+      APInt::tcShiftRight(significandParts(), newPartCount, ushift);
+    }
     // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
     // does not give you back the same bits.  This is dubious, and we
     // don't currently do it.  You're really supposed to get
@@ -1735,6 +1840,7 @@ APFloat::convert(const fltSemantics &toSemantics,
   } else {
     semantics = &toSemantics;
     fs = opOK;
+    *losesInfo = false;
   }
 
   return fs;
@@ -1753,7 +1859,8 @@ APFloat::convert(const fltSemantics &toSemantics,
 APFloat::opStatus
 APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
                                       bool isSigned,
-                                      roundingMode rounding_mode) const
+                                      roundingMode rounding_mode,
+                                      bool *isExact) const
 {
   lostFraction lost_fraction;
   const integerPart *src;
@@ -1761,6 +1868,8 @@ APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
 
   assertArithmeticOK(*semantics);
 
+  *isExact = false;
+
   /* Handle the three special cases first.  */
   if(category == fcInfinity || category == fcNaN)
     return opInvalidOp;
@@ -1769,6 +1878,8 @@ APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
 
   if(category == fcZero) {
     APInt::tcSet(parts, 0, dstPartsCount);
+    // Negative zero can't be represented as an int.
+    *isExact = !sign;
     return opOK;
   }
 
@@ -1779,7 +1890,9 @@ APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
   if (exponent < 0) {
     /* Our absolute value is less than one; truncate everything.  */
     APInt::tcSet(parts, 0, dstPartsCount);
-    truncatedBits = semantics->precision;
+    /* For exponent -1 the integer bit represents .5, look at that.
+       For smaller exponents leftmost truncated bit is 0. */
+    truncatedBits = semantics->precision -1U - exponent;
   } else {
     /* We want the most significant (exponent + 1) bits; the rest are
        truncated.  */
@@ -1841,24 +1954,31 @@ APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
       return opInvalidOp;
   }
 
-  if (lost_fraction == lfExactlyZero)
+  if (lost_fraction == lfExactlyZero) {
+    *isExact = true;
     return opOK;
-  else
+  else
     return opInexact;
 }
 
 /* Same as convertToSignExtendedInteger, except we provide
    deterministic values in case of an invalid operation exception,
    namely zero for NaNs and the minimal or maximal value respectively
-   for underflow or overflow.  */
+   for underflow or overflow.
+   The *isExact output tells whether the result is exact, in the sense
+   that converting it back to the original floating point type produces
+   the original value.  This is almost equivalent to result==opOK,
+   except for negative zeroes.
+*/
 APFloat::opStatus
 APFloat::convertToInteger(integerPart *parts, unsigned int width,
                           bool isSigned,
-                          roundingMode rounding_mode) const
+                          roundingMode rounding_mode, bool *isExact) const
 {
   opStatus fs;
 
-  fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode);
+  fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode, 
+                                    isExact);
 
   if (fs == opInvalidOp) {
     unsigned int bits, dstPartsCount;
@@ -2003,7 +2123,7 @@ APFloat::convertFromHexadecimalString(const char *p,
   firstSignificantDigit = p;
 
   for(;;) {
-    uint64_t hex_value;
+    integerPart hex_value;
 
     if(*p == '.') {
       assert(dot == 0);
@@ -2251,8 +2371,8 @@ APFloat::convertFromString(const char *p, roundingMode rounding_mode)
 
   if(p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
     return convertFromHexadecimalString(p + 2, rounding_mode);
-  else
-    return convertFromDecimalString(p, rounding_mode);
+
+  return convertFromDecimalString(p, rounding_mode);
 }
 
 /* Write out a hexadecimal representation of the floating point value
@@ -2601,7 +2721,7 @@ APFloat::convertFloatAPFloatToAPInt() const
 // and treating the result as a normal integer is unlikely to be useful.
 
 APInt
-APFloat::convertToAPInt() const
+APFloat::bitcastToAPInt() const
 {
   if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
     return convertFloatAPFloatToAPInt();
@@ -2621,7 +2741,7 @@ float
 APFloat::convertToFloat() const
 {
   assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
-  APInt api = convertToAPInt();
+  APInt api = bitcastToAPInt();
   return api.bitsToFloat();
 }
 
@@ -2629,15 +2749,17 @@ double
 APFloat::convertToDouble() const
 {
   assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
-  APInt api = convertToAPInt();
+  APInt api = bitcastToAPInt();
   return api.bitsToDouble();
 }
 
-/// Integer bit is explicit in this format.  Current Intel book does not
-/// define meaning of:
-///  exponent = all 1's, integer bit not set.
-///  exponent = 0, integer bit set. (formerly "psuedodenormals")
-///  exponent!=0 nor all 1's, integer bit not set. (formerly "unnormals")
+/// Integer bit is explicit in this format.  Intel hardware (387 and later)
+/// does not support these bit patterns:
+///  exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
+///  exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
+///  exponent = 0, integer bit 1 ("pseudodenormal")
+///  exponent!=0 nor all 1's, integer bit 0 ("unnormal")
+/// At the moment, the first two are treated as NaNs, the second two as Normal.
 void
 APFloat::initFromF80LongDoubleAPInt(const APInt &api)
 {