[PATCH] D30602: [APInt] Add getBitsSetFrom and setBitsFrom to set upper bits starting at a bit

Phabricator via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 6 19:10:33 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL297114: [APInt] Add getBitsSetFrom and setBitsFrom to set upper bits starting at a bit (authored by ctopper).

Changed prior to commit:
  https://reviews.llvm.org/D30602?vs=90595&id=90780#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D30602

Files:
  llvm/trunk/include/llvm/ADT/APInt.h
  llvm/trunk/unittests/ADT/APIntTest.cpp


Index: llvm/trunk/unittests/ADT/APIntTest.cpp
===================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp
+++ llvm/trunk/unittests/ADT/APIntTest.cpp
@@ -1584,6 +1584,16 @@
   EXPECT_EQ(32u, i64hi32.countPopulation());
 }
 
+TEST(APIntTest, getBitsSetFrom) {
+  APInt i64hi31 = APInt::getBitsSetFrom(64, 33);
+  EXPECT_EQ(31u, i64hi31.countLeadingOnes());
+  EXPECT_EQ(0u, i64hi31.countLeadingZeros());
+  EXPECT_EQ(64u, i64hi31.getActiveBits());
+  EXPECT_EQ(33u, i64hi31.countTrailingZeros());
+  EXPECT_EQ(0u, i64hi31.countTrailingOnes());
+  EXPECT_EQ(31u, i64hi31.countPopulation());
+}
+
 TEST(APIntTest, setLowBits) {
   APInt i64lo32(64, 0);
   i64lo32.setLowBits(32);
@@ -1704,3 +1714,14 @@
   EXPECT_EQ(0u, i32hi16.countTrailingOnes());
   EXPECT_EQ(16u, i32hi16.countPopulation());
 }
+
+TEST(APIntTest, setBitsFrom) {
+  APInt i64from63(64, 0);
+  i64from63.setBitsFrom(63);
+  EXPECT_EQ(1u, i64from63.countLeadingOnes());
+  EXPECT_EQ(0u, i64from63.countLeadingZeros());
+  EXPECT_EQ(64u, i64from63.getActiveBits());
+  EXPECT_EQ(63u, i64from63.countTrailingZeros());
+  EXPECT_EQ(0u, i64from63.countTrailingOnes());
+  EXPECT_EQ(1u, i64from63.countPopulation());
+}
Index: llvm/trunk/include/llvm/ADT/APInt.h
===================================================================
--- llvm/trunk/include/llvm/ADT/APInt.h
+++ llvm/trunk/include/llvm/ADT/APInt.h
@@ -510,6 +510,23 @@
     return Res;
   }
 
+  /// \brief Get a value with upper bits starting at loBit set.
+  ///
+  /// Constructs an APInt value that has a contiguous range of bits set. The
+  /// bits from loBit (inclusive) to numBits (exclusive) will be set. All other
+  /// bits will be zero. For example, with parameters(32, 12) you would get
+  /// 0xFFFFF000.
+  ///
+  /// \param numBits the intended bit width of the result
+  /// \param loBit the index of the lowest bit to set.
+  ///
+  /// \returns An APInt value with the requested bits set.
+  static APInt getBitsSetFrom(unsigned numBits, unsigned loBit) {
+    APInt Res(numBits, 0);
+    Res.setBitsFrom(loBit);
+    return Res;
+  }
+
   /// \brief Get a value with high bits set
   ///
   /// Constructs an APInt value that has the top hiBitsSet bits set.
@@ -1222,6 +1239,11 @@
     }
   }
 
+  /// Set the top bits starting from loBit.
+  void setBitsFrom(unsigned loBit) {
+    return setBits(loBit, BitWidth);
+  }
+
   /// Set the bottom loBits bits.
   void setLowBits(unsigned loBits) {
     return setBits(0, loBits);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30602.90780.patch
Type: text/x-patch
Size: 2522 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170307/9b77b386/attachment.bin>


More information about the llvm-commits mailing list