[llvm-commits] CVS: llvm/include/llvm/ADT/APInt.h

Reid Spencer reid at x10sys.com
Tue Feb 27 12:24:49 PST 2007



Changes in directory llvm/include/llvm/ADT:

APInt.h updated: 1.25 -> 1.26
---
Log message:

Improve APInt interface:
1. Add unsigned and signed versions of methods so a "bool" argument doesn't
   need to be passed in.
2. Make the various getMin/getMax functions all be inline since they are
   so simple.
3. Simplify sdiv and srem code.


---
Diffs of the changes:  (+67 -22)

 APInt.h |   89 ++++++++++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 67 insertions(+), 22 deletions(-)


Index: llvm/include/llvm/ADT/APInt.h
diff -u llvm/include/llvm/ADT/APInt.h:1.25 llvm/include/llvm/ADT/APInt.h:1.26
--- llvm/include/llvm/ADT/APInt.h:1.25	Tue Feb 27 12:22:31 2007
+++ llvm/include/llvm/ADT/APInt.h	Tue Feb 27 14:24:31 2007
@@ -59,7 +59,6 @@
 ///
 /// @brief Class for arbitrary precision integers.
 class APInt {
-public:
 
   uint32_t BitWidth;      ///< The number of bits in this APInt.
 
@@ -374,8 +373,8 @@
   /// Signed divide this APInt by APInt RHS.
   /// @brief Signed division function for APInt.
   inline APInt sdiv(const APInt& RHS) const {
-    bool isNegativeLHS = (*this)[BitWidth - 1];
-    bool isNegativeRHS = RHS[RHS.BitWidth - 1];
+    bool isNegativeLHS = isNegative();
+    bool isNegativeRHS = RHS.isNegative();
     APInt Result = APIntOps::udiv(
         isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
     return isNegativeLHS != isNegativeRHS ? -Result : Result;
@@ -388,8 +387,8 @@
   /// Signed remainder operation on APInt.
   /// @brief Function for signed remainder operation.
   inline APInt srem(const APInt& RHS) const {
-    bool isNegativeLHS = (*this)[BitWidth - 1];
-    bool isNegativeRHS = RHS[RHS.BitWidth - 1];
+    bool isNegativeLHS = isNegative();
+    bool isNegativeRHS = RHS.isNegative();
     APInt Result = APIntOps::urem(
         isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
     return isNegativeLHS ? -Result : Result;
@@ -470,24 +469,37 @@
     return int64_t(pVal[0]);
   }
 
-  /// @returns the largest value for an APInt of the specified bit-width and 
-  /// if isSign == true, it should be largest signed value, otherwise largest
-  /// unsigned value.
-  /// @brief Gets max value of the APInt with bitwidth <= 64.
-  static APInt getMaxValue(uint32_t numBits, bool isSigned);
-
-  /// @returns the smallest value for an APInt of the given bit-width and
-  /// if isSign == true, it should be smallest signed value, otherwise zero.
-  /// @brief Gets min value of the APInt with bitwidth <= 64.
-  static APInt getMinValue(uint32_t numBits, bool isSigned);
+  /// @brief Gets maximum unsigned value of APInt for specific bit width.
+  static APInt getMaxValue(uint32_t numBits) {
+    return APInt(numBits, 0).set();
+  }
+
+  /// @brief Gets maximum signed value of APInt for a specific bit width.
+  static APInt getSignedMaxValue(uint32_t numBits) {
+    return APInt(numBits, 0).set().clear(numBits - 1);
+  }
+
+  /// @brief Gets minimum unsigned value of APInt for a specific bit width.
+  static APInt getMinValue(uint32_t numBits) {
+    return APInt(numBits, 0);
+  }
+
+  /// @brief Gets minimum signed value of APInt for a specific bit width.
+  static APInt getSignedMinValue(uint32_t numBits) {
+    return APInt(numBits, 0).set(numBits - 1);
+  }
 
   /// @returns the all-ones value for an APInt of the specified bit-width.
   /// @brief Get the all-ones value.
-  static APInt getAllOnesValue(uint32_t numBits);
+  static APInt getAllOnesValue(uint32_t numBits) {
+    return APInt(numBits, 0).set();
+  }
 
   /// @returns the '0' value for an APInt of the specified bit-width.
   /// @brief Get the '0' value.
-  static APInt getNullValue(uint32_t numBits);
+  static APInt getNullValue(uint32_t numBits) {
+    return APInt(numBits, 0);
+  }
 
   /// The hash value is computed as the sum of the words and the bit width.
   /// @returns A hash value computed from the sum of the APInt words.
@@ -536,8 +548,25 @@
                            isNegative() && countPopulation() == 1;
   }
 
-  /// @returns a character interpretation of the APInt.
-  std::string toString(uint8_t radix = 10, bool wantSigned = true) const;
+  /// This is used internally to convert an APInt to a string.
+  /// @brief Converts an APInt to a std::string
+  std::string toString(uint8_t radix, bool wantSigned) const;
+
+  /// Considers the APInt to be unsigned and converts it into a string in the
+  /// radix given. The radix can be 2, 8, 10 or 16.
+  /// @returns a character interpretation of the APInt
+  /// @brief Convert unsigned APInt to string representation.
+  inline std::string toString(uint8_t radix = 10) const {
+    return toString(radix, false);
+  }
+
+  /// Considers the APInt to be unsigned and converts it into a string in the
+  /// radix given. The radix can be 2, 8, 10 or 16.
+  /// @returns a character interpretation of the APInt
+  /// @brief Convert unsigned APInt to string representation.
+  inline std::string toStringSigned(uint8_t radix = 10) const {
+    return toString(radix, true);
+  }
 
   /// Get an APInt with the same BitWidth as this APInt, just zero mask
   /// the low bits and right shift to the least significant bit.
@@ -603,8 +632,17 @@
   }
 
   /// @brief Converts this APInt to a double value.
-  double roundToDouble(bool isSigned = false) const;
+  double roundToDouble(bool isSigned) const;
+
+  /// @brief Converts this unsigned APInt to a double value.
+  double roundToDouble() const {
+    return roundToDouble(false);
+  }
 
+  /// @brief Converts this signed APInt to a double value.
+  double signedRoundToDouble() const {
+    return roundToDouble(true);
+  }
 };
 
 namespace APIntOps {
@@ -642,9 +680,16 @@
 /// @brief Compute GCD of two APInt values.
 APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
 
+/// Treats the APInt as an unsigned value for conversion purposes.
+/// @brief Converts the given APInt to a double value.
+inline double RoundAPIntToDouble(const APInt& APIVal) {
+  return APIVal.roundToDouble();
+}
+
+/// Treats the APInt as a signed value for conversion purposes.
 /// @brief Converts the given APInt to a double value.
-inline double RoundAPIntToDouble(const APInt& APIVal, bool isSigned = false) {
-  return APIVal.roundToDouble(isSigned);
+inline double RoundSignedAPIntToDouble(const APInt& APIVal) {
+  return APIVal.signedRoundToDouble();
 }
 
 /// @brief Converts the given APInt to a float vlalue.






More information about the llvm-commits mailing list