fix a critical bug in smallvector, where it would destroy elements that are
[oota-llvm.git] / include / llvm / ADT / APInt.h
index 8cfb1d566bfdad6d5f570109a512e886e1a4b0bb..7c100a28351e55a7c2634963b6356a5028d48d17 100644 (file)
 #define LLVM_APINT_H
 
 #include "llvm/Support/DataTypes.h"
+#include <cassert>
 #include <string>
 
 namespace llvm {
 
+/// Forward declaration.
+class APInt;
+namespace APIntOps {
+  APInt udiv(const APInt& LHS, const APInt& RHS);
+  APInt urem(const APInt& LHS, const APInt& RHS);
+}
+
 //===----------------------------------------------------------------------===//
 //                              APInt Class
 //===----------------------------------------------------------------------===//
@@ -37,16 +45,7 @@ namespace llvm {
 /// Note: In this class, all bit/byte/word positions are zero-based.
 ///
 class APInt {
-  /// Friend Functions of APInt declared here. For detailed comments,
-  /// see bottom of this file.
-  friend bool isIntN(unsigned N, const APInt& APIVal);
-  friend APInt ByteSwap(const APInt& APIVal);
-  friend APInt LogBase2(const APInt& APIVal);
-  friend double APIntToDouble(const APInt& APIVal);
-  friend float APIntToFloat(const APInt& APIVal);
-
-  unsigned bitsnum;      ///< The number of bits.
-  bool isSigned;         ///< The sign flag for this APInt.
+  unsigned BitsNum;      ///< The number of bits.
 
   /// This union is used to store the integer value. When the
   /// integer bit-width <= 64, it uses VAL; 
@@ -64,22 +63,22 @@ class APInt {
   /// Here one word's bitwidth equals to that of uint64_t.
   /// @returns the number of words to hold the integer value of this APInt.
   /// @brief Get the number of words.
-  inline unsigned numWords() const {
-    return bitsnum < 1 ? 0 : (bitsnum + APINT_BITS_PER_WORD - 1) /
-                             APINT_BITS_PER_WORD;
+  inline unsigned getNumWords() const {
+    return (BitsNum + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
   }
 
   /// @returns true if the number of bits <= 64, false otherwise.
   /// @brief Determine if this APInt just has one word to store value.
   inline bool isSingleWord() const
-  { return bitsnum <= APINT_BITS_PER_WORD; }
+  { return BitsNum <= APINT_BITS_PER_WORD; }
 
   /// @returns the word position for the specified bit position.
   static inline unsigned whichWord(unsigned bitPosition)
   { return bitPosition / APINT_BITS_PER_WORD; }
 
   /// @returns the byte position for the specified bit position.
-  static inline unsigned whichByte(unsigned bitPosition);
+  static inline unsigned whichByte(unsigned bitPosition)
+  { return (bitPosition % APINT_BITS_PER_WORD) / 8; }
 
   /// @returns the bit position in a word for the specified bit position 
   /// in APInt.
@@ -93,10 +92,10 @@ class APInt {
 
   inline void TruncToBits() {
     if (isSingleWord())
-      VAL &= ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - bitsnum);
+      VAL &= ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitsNum);
     else
-      pVal[numWords() - 1] &= ~uint64_t(0ULL) >> 
-        (APINT_BITS_PER_WORD - (whichBit(bitsnum - 1) + 1));
+      pVal[getNumWords() - 1] &= ~uint64_t(0ULL) >> 
+        (APINT_BITS_PER_WORD - (whichBit(BitsNum - 1) + 1));
   }
 
   /// @returns the corresponding word for the specified bit position.
@@ -108,18 +107,24 @@ class APInt {
   inline uint64_t getWord(unsigned bitPosition) const
   { return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; }
 
+  /// @brief Converts a char array into an integer.
+  void StrToAPInt(const char *StrStart, unsigned slen, uint8_t radix);
+
 public:
   /// @brief Create a new APInt of numBits bit-width, and initialized as val.
-  APInt(uint64_t val = 0, unsigned numBits = APINT_BITS_PER_WORD, 
-        bool sign = false);
+  APInt(uint64_t val = 0, unsigned numBits = APINT_BITS_PER_WORD);
 
   /// @brief Create a new APInt of numBits bit-width, and initialized as 
   /// bigVal[].
-  APInt(unsigned numBits, uint64_t bigVal[], bool sign = false);
+  APInt(unsigned numBits, uint64_t bigVal[]);
 
   /// @brief Create a new APInt by translating the string represented 
   /// integer value.
-  APInt(std::string& Val, uint8_t radix = 10, bool sign = false);
+  APInt(const std::string& Val, uint8_t radix = 10);
+
+  /// @brief Create a new APInt by translating the char array represented
+  /// integer value.
+  APInt(const char StrStart[], unsigned slen, uint8_t radix);
 
   /// @brief Copy Constructor.
   APInt(const APInt& API);
@@ -136,7 +141,10 @@ public:
 
   /// Increments the APInt by one.
   /// @brief Postfix increment operator.
-  const APInt operator++(int);
+  inline const APInt operator++(int) {
+    APInt API(*this);
+    return ++API;
+  }
 
   /// Increments the APInt by one.
   /// @brief Prefix increment operator.
@@ -144,7 +152,10 @@ public:
 
   /// Decrements the APInt by one.
   /// @brief Postfix decrement operator. 
-  const APInt operator--(int);
+  inline const APInt operator--(int) {
+    APInt API(*this);
+    return --API;
+  }
 
   /// Decrements the APInt by one.
   /// @brief Prefix decrement operator. 
@@ -165,14 +176,6 @@ public:
   /// @brief Bitwise XOR assignment operator. 
   APInt& operator^=(const APInt& RHS);
 
-  /// Left-shift the APInt by shiftAmt and assigns the result to this APInt.
-  /// @brief Left-shift assignment operator. 
-  APInt& operator<<=(unsigned shiftAmt);
-
-  /// Right-shift the APInt by shiftAmt and assigns the result to this APInt.
-  /// @brief Right-shift assignment operator. 
-  APInt& operator>>=(unsigned shiftAmt);
-
   /// Performs a bitwise complement operation on this APInt.
   /// @brief Bitwise complement operator. 
   APInt operator~() const;
@@ -182,11 +185,6 @@ public:
   /// @brief Multiplication assignment operator. 
   APInt& operator*=(const APInt& RHS);
 
-  /// Divides this APInt by the given APInt &RHS and 
-  /// assigns the result to this APInt.
-  /// @brief Division assignment operator. 
-  APInt& operator/=(const APInt& RHS);
-
   /// Adds this APInt by the given APInt& RHS and 
   /// assigns the result to this APInt.
   /// @brief Addition assignment operator. 
@@ -197,11 +195,6 @@ public:
   /// @brief Subtraction assignment operator. 
   APInt& operator-=(const APInt& RHS);
 
-  /// Yields the remainder from the division of this APInt by 
-  /// the given APInt& RHS and assigns the remainder to this APInt.
-  /// @brief Remainder assignment operator. 
-  APInt& operator%=(const APInt& RHS);
-
   /// Performs bitwise AND operation on this APInt and 
   /// the given APInt& RHS.
   /// @brief Bitwise AND operator. 
@@ -231,15 +224,6 @@ public:
   /// @brief Multiplication operator. 
   APInt operator*(const APInt& RHS) const;
 
-  /// Divides this APInt by the given APInt& RHS.
-  /// @brief Division operator. 
-  APInt operator/(const APInt& RHS) const;
-
-  /// Yields the remainder from the division of 
-  /// this APInt and the given APInt& RHS.
-  /// @brief Remainder operator. 
-  APInt operator%(const APInt& RHS) const;
-
   /// Adds this APInt by the given APInt& RHS.
   /// @brief Addition operator. 
   APInt operator+(const APInt& RHS) const;
@@ -248,13 +232,10 @@ public:
   /// @brief Subtraction operator. 
   APInt operator-(const APInt& RHS) const;
 
-  /// Left-shift the APInt by shiftAmt.
-  /// @brief Left-shift operator. 
-  APInt operator<<(unsigned shiftAmt) const;
-
-  /// Right-shift the APInt by shiftAmt.
-  /// @brief Right-shift operator. 
-  APInt operator>>(unsigned shiftAmt) const;
+  ///
+  inline APInt operator-() const {
+    return APInt(0, BitsNum) - (*this);
+  }
 
   /// @brief Array-indexing support.
   bool operator[](unsigned bitPosition) const;
@@ -264,11 +245,25 @@ public:
   /// @brief Equality operator. 
   bool operator==(const APInt& RHS) const;
 
+  /// Compare this APInt with the given uint64_t value
+  /// for the validity of the equality relationship.
+  /// @brief Equality operator.
+  bool operator==(uint64_t Val) const;
+
   /// Compare this APInt with the given APInt& RHS 
   /// for the validity of the inequality relationship.
   /// @brief Inequality operator. 
-  bool operator!=(const APInt& RHS) const;
+  inline bool operator!=(const APInt& RHS) const {
+    return !((*this) == RHS);
+  }
 
+  /// Compare this APInt with the given uint64_t value 
+  /// for the validity of the inequality relationship.
+  /// @brief Inequality operator. 
+  inline bool operator!=(uint64_t Val) const {
+    return !((*this) == Val);
+  }
+  
   /// Compare this APInt with the given APInt& RHS for 
   /// the validity of the less-than relationship.
   /// @brief Less-than operator. 
@@ -293,11 +288,11 @@ public:
   /// word, just returns VAL, otherwise pVal[0].
   inline uint64_t getValue() {
     if (isSingleWord())
-      return isSigned ? ((int64_t(VAL) << (APINT_BITS_PER_WORD - bitsnum)) >> 
-                         (APINT_BITS_PER_WORD - bitsnum)) :
-                        VAL;
-    else
+      return VAL;
+    unsigned n = getNumWords() * 64 - CountLeadingZeros();
+    if (n <= 64)
       return pVal[0];
+    assert(0 && "This APInt's bitwidth > 64");
   }
 
   /// @returns the largest value for an APInt of the specified bit-width and 
@@ -344,12 +339,12 @@ public:
   /// @returns a character interpretation of the APInt.
   std::string to_string(uint8_t radix = 10) const;
 
-  /// Get an APInt with the same bitsnum as this APInt, just zero mask
+  /// Get an APInt with the same BitsNum as this APInt, just zero mask
   /// the low bits and right shift to the least significant bit.
   /// @returns the high "numBits" bits of this APInt.
   APInt HiBits(unsigned numBits) const;
 
-  /// Get an APInt with the same bitsnum as this APInt, just zero mask
+  /// Get an APInt with the same BitsNum as this APInt, just zero mask
   /// the high bits.
   /// @returns the low "numBits" bits of this APInt.
   APInt LoBits(unsigned numBits) const;
@@ -372,19 +367,73 @@ public:
 
   /// @returns the total number of bits.
   inline unsigned getNumBits() const
-  { return bitsnum; }
+  { return BitsNum; }
+
+  /// @brief Check if this APInt has a N-bits integer value.
+  inline bool isIntN(unsigned N) const {
+    if (isSingleWord()) {
+      return VAL == VAL & (~uint64_t(0ULL) >> (64 - N));
+    } else {
+      APInt Tmp(N, pVal);
+      return Tmp == (*this);
+    }
+  }
+
+  /// @returns a byte-swapped representation of this APInt Value.
+  APInt ByteSwap() const;
+
+  /// @returns the floor log base 2 of this APInt.
+  inline unsigned LogBase2() const {
+    return getNumWords() * APINT_BITS_PER_WORD - 
+           CountLeadingZeros();
+  }
+
+  /// @brief Converts this APInt to a double value.
+  double APIntRoundToDouble(bool isSigned = false) const;
+
+  /// Arithmetic right-shift this APInt by shiftAmt.
+  /// @brief Arithmetic right-shift function.
+  APInt ashr(unsigned shiftAmt) const;
+
+  /// Logical right-shift this APInt by shiftAmt.
+  /// @brief Logical right-shift function.
+  APInt lshr(unsigned shiftAmt) const;
+
+  /// Left-shift this APInt by shiftAmt.
+  /// @brief Left-shift function.
+  APInt shl(unsigned shiftAmt) const;
+
+  /// Signed divide this APInt by APInt RHS.
+  /// @brief Signed division function for APInt.
+  inline APInt sdiv(const APInt& RHS) const {
+    bool isSignedLHS = (*this)[BitsNum - 1], isSignedRHS = RHS[RHS.BitsNum - 1];
+    APInt API = APIntOps::udiv(isSignedLHS ? -(*this) : (*this), isSignedRHS ? -RHS : RHS);
+    return isSignedLHS != isSignedRHS ? -API : API;;
+  }
+
+  /// Unsigned divide this APInt by APInt RHS.
+  /// @brief Unsigned division function for APInt.
+  APInt udiv(const APInt& RHS) const;
+
+  /// Signed remainder operation on APInt.
+  /// @brief Function for signed remainder operation.
+  inline APInt srem(const APInt& RHS) const {
+    bool isSignedLHS = (*this)[BitsNum - 1], isSignedRHS = RHS[RHS.BitsNum - 1];
+    APInt API = APIntOps::urem(isSignedLHS ? -(*this) : (*this), isSignedRHS ? -RHS : RHS);
+    return isSignedLHS ? -API : API;
+  }
+
+  /// Unsigned remainder operation on APInt.
+  /// @brief Function for unsigned remainder operation.
+  APInt urem(const APInt& RHS) const;
 
 };
 
+namespace APIntOps {
+
 /// @brief Check if the specified APInt has a N-bits integer value.
 inline bool isIntN(unsigned N, const APInt& APIVal) {
-  if (APIVal.isSingleWord()) {
-    APInt Tmp(N, APIVal.VAL);
-    return Tmp == APIVal;
-  } else {
-    APInt Tmp(N, APIVal.pVal);
-    return Tmp == APIVal;
-  }
+  return APIVal.isIntN(N);
 }
 
 /// @returns true if the argument APInt value is a sequence of ones
@@ -400,18 +449,124 @@ inline const bool isShiftedMask(unsigned numBits, const APInt& APIVal) {
 }
 
 /// @returns a byte-swapped representation of the specified APInt Value.
-APInt ByteSwap(const APInt& APIVal);
+inline APInt ByteSwap(const APInt& APIVal) {
+  return APIVal.ByteSwap();
+}
 
 /// @returns the floor log base 2 of the specified APInt value.
-inline APInt LogBase2(const APInt& APIVal) {
-  return APIVal.numWords() * APInt::APINT_BITS_PER_WORD - 
-         APIVal.CountLeadingZeros();
+inline unsigned LogBase2(const APInt& APIVal) {
+  return APIVal.LogBase2(); 
 }
 
 /// @returns the greatest common divisor of the two values 
 /// using Euclid's algorithm.
 APInt GreatestCommonDivisor(const APInt& API1, const APInt& API2);
 
+/// @brief Converts the given APInt to a double value.
+inline double APIntRoundToDouble(const APInt& APIVal, bool isSigned = false) {
+  return APIVal.APIntRoundToDouble(isSigned);
+}
+
+/// @brief Converts the given APInt to a float vlalue.
+inline float APIntRoundToFloat(const APInt& APIVal) {
+  return float(APIntRoundToDouble(APIVal));
+}
+
+/// @brief Converts the given double value into a APInt.
+APInt DoubleRoundToAPInt(double Double);
+
+/// @brief Converts the given float value into a APInt.
+inline APInt FloatRoundToAPInt(float Float) {
+  return DoubleRoundToAPInt(double(Float));
+}
+
+/// Arithmetic right-shift the APInt by shiftAmt.
+/// @brief Arithmetic right-shift function.
+inline APInt ashr(const APInt& LHS, unsigned shiftAmt) {
+  return LHS.ashr(shiftAmt);
+}
+
+/// Logical right-shift the APInt by shiftAmt.
+/// @brief Logical right-shift function.
+inline APInt lshr(const APInt& LHS, unsigned shiftAmt) {
+  return LHS.lshr(shiftAmt);
+}
+
+/// Left-shift the APInt by shiftAmt.
+/// @brief Left-shift function.
+inline APInt shl(const APInt& LHS, unsigned shiftAmt) {
+  return LHS.shl(shiftAmt);
+}
+
+/// Signed divide APInt LHS by APInt RHS.
+/// @brief Signed division function for APInt.
+inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
+  return LHS.sdiv(RHS);
+}
+
+/// Unsigned divide APInt LHS by APInt RHS.
+/// @brief Unsigned division function for APInt.
+inline APInt udiv(const APInt& LHS, const APInt& RHS) {
+  return LHS.udiv(RHS);
+}
+
+/// Signed remainder operation on APInt.
+/// @brief Function for signed remainder operation.
+inline APInt srem(const APInt& LHS, const APInt& RHS) {
+  return LHS.srem(RHS);
+}
+
+/// Unsigned remainder operation on APInt.
+/// @brief Function for unsigned remainder operation.
+inline APInt urem(const APInt& LHS, const APInt& RHS) {
+  return LHS.urem(RHS);
+}
+
+/// Performs multiplication on APInt values.
+/// @brief Function for multiplication operation.
+inline APInt mul(const APInt& LHS, const APInt& RHS) {
+  return LHS * RHS;
+}
+
+/// Performs addition on APInt values.
+/// @brief Function for addition operation.
+inline APInt add(const APInt& LHS, const APInt& RHS) {
+  return LHS + RHS;
+}
+
+/// Performs subtraction on APInt values.
+/// @brief Function for subtraction operation.
+inline APInt sub(const APInt& LHS, const APInt& RHS) {
+  return LHS - RHS;
+}
+
+/// Performs bitwise AND operation on APInt LHS and 
+/// APInt RHS.
+/// @brief Bitwise AND function for APInt.
+inline APInt And(const APInt& LHS, const APInt& RHS) {
+  return LHS & RHS;
+}
+
+/// Performs bitwise OR operation on APInt LHS and APInt RHS.
+/// @brief Bitwise OR function for APInt. 
+inline APInt Or(const APInt& LHS, const APInt& RHS) {
+  return LHS | RHS;
+}
+
+/// Performs bitwise XOR operation on APInt.
+/// @brief Bitwise XOR function for APInt.
+inline APInt Xor(const APInt& LHS, const APInt& RHS) {
+  return LHS ^ RHS;
+} 
+
+/// Performs a bitwise complement operation on APInt.
+/// @brief Bitwise complement function. 
+inline APInt Not(const APInt& APIVal) {
+  return ~APIVal;
+}
+
+} // End of APIntOps namespace
+
 } // End of llvm namespace
 
 #endif