Add methods for bit width modification: sextOrTrunc, zextOrTrunc.
authorReid Spencer <rspencer@reidspencer.com>
Thu, 1 Mar 2007 17:15:32 +0000 (17:15 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Thu, 1 Mar 2007 17:15:32 +0000 (17:15 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34789 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/APInt.h
lib/Support/APInt.cpp

index 0e127b8c44d73a640c36a7bf01a91555b3e15eb4..9043227f0b77fc1397dae074a15063296b21ddd2 100644 (file)
@@ -423,6 +423,16 @@ public:
   /// @brief Zero extend to a new width.
   APInt &zext(uint32_t width);
 
+  /// Make this APInt have the bit width given by \p width. The value is sign
+  /// extended, truncated, or left alone to make it that width.
+  /// @brief Sign extend or truncate to width
+  APInt &sextOrTrunc(uint32_t width);
+
+  /// Make this APInt have the bit width given by \p width. The value is zero
+  /// extended, truncated, or left alone to make it that width.
+  /// @brief Zero extend or truncate to width
+  APInt &zextOrTrunc(uint32_t width);
+
   /// @brief Set every bit to 1.
   APInt& set();
 
index 93442f344c11bdc9f51743b31d813e176fdca086..50b0dc34fea0576c527375a1c489b8062c3ecd19 100644 (file)
@@ -985,6 +985,22 @@ APInt &APInt::zext(uint32_t width) {
   return *this;
 }
 
+APInt &APInt::zextOrTrunc(uint32_t width) {
+  if (BitWidth < width)
+    return zext(width);
+  if (BitWidth > width)
+    return trunc(width);
+  return *this;
+}
+
+APInt &APInt::sextOrTrunc(uint32_t width) {
+  if (BitWidth < width)
+    return sext(width);
+  if (BitWidth > width)
+    return trunc(width);
+  return *this;
+}
+
 /// Arithmetic right-shift this APInt by shiftAmt.
 /// @brief Arithmetic right-shift function.
 APInt APInt::ashr(uint32_t shiftAmt) const {