[block-freq] Add the APInt method extractBit.
authorMichael Gottesman <mgottesman@apple.com>
Fri, 13 Dec 2013 20:47:34 +0000 (20:47 +0000)
committerMichael Gottesman <mgottesman@apple.com>
Fri, 13 Dec 2013 20:47:34 +0000 (20:47 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197271 91177308-0d34-0410-b5e6-96231b3b80d8

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

index d494ad25351bd0db35091be7d24a6714cc9e8ad8..2073fa08cbf25c2f47e19abd8d2639ca7d2d163c 100644 (file)
@@ -1244,6 +1244,9 @@ public:
   /// as "bitPosition".
   void flipBit(unsigned bitPosition);
 
+  /// \brief Returns true if the bit in bitPosition is set.
+  bool extractBit(unsigned bitPosition) const;
+
   /// @}
   /// \name Value Characterization Functions
   /// @{
index 89f96bd5774002b3956d409e244bf4467c795e73..731c8cc9cae20e78e08059969e6196b5d3d7e6d3 100644 (file)
@@ -607,6 +607,14 @@ void APInt::flipBit(unsigned bitPosition) {
   else setBit(bitPosition);
 }
 
+bool APInt::extractBit(unsigned bitPosition) const {
+  assert(bitPosition < BitWidth && "Out of the bit-width range!");
+  if (isSingleWord())
+    return VAL & maskBit(bitPosition);
+  else
+    return pVal[whichWord(bitPosition)] & maskBit(bitPosition);
+}
+
 unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) {
   assert(!str.empty() && "Invalid string length");
   assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || 
index 3c0dfe1440447b2fd22128200d9479721d50dd2e..1d330f0d369cdeab6a67a1e11783f5a0f0b4b2c8 100644 (file)
@@ -597,4 +597,30 @@ TEST(APIntTest, tcDecrement) {
     EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
   }
 }
+
+TEST(APIntTest, extractBit) {
+  // Single word check.
+  uint64_t E1 = 0x2CA7F46BF6569915ULL;
+  APInt A1(64, E1);
+  for (unsigned i = 0, e = 64; i < e; ++i) {    
+    EXPECT_EQ(bool(E1 & (1ULL << i)),
+              A1.extractBit(i));
+  }
+
+  // Multiword check.
+  integerPart E2[4] = {
+    0xeb6eb136591cba21ULL,
+    0x7b9358bd6a33f10aULL,
+    0x7e7ffa5eadd8846ULL,
+    0x305f341ca00b613dULL
+  };
+  APInt A2(integerPartWidth*4, ArrayRef<integerPart>(E2, 4));
+  for (unsigned i = 0; i < 4; ++i) {
+    for (unsigned j = 0; j < integerPartWidth; ++j) {
+      EXPECT_EQ(bool(E2[i] & (1ULL << j)),
+                A2.extractBit(i*integerPartWidth + j));
+    }
+  }
+}
+
 }