<p dir="ltr">This name doesn't feel quite right to me... testBit or bitTest maybe? Meh. Not sure.</p>
<div class="gmail_quote">On Dec 13, 2013 12:54 PM, "Michael Gottesman" <<a href="mailto:mgottesman@apple.com">mgottesman@apple.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: mgottesman<br>
Date: Fri Dec 13 14:47:34 2013<br>
New Revision: 197271<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=197271&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=197271&view=rev</a><br>
Log:<br>
[block-freq] Add the APInt method extractBit.<br>
<br>
Modified:<br>
  Â  llvm/trunk/include/llvm/ADT/APInt.h<br>
  Â  llvm/trunk/lib/Support/APInt.cpp<br>
  Â  llvm/trunk/unittests/ADT/APIntTest.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/ADT/APInt.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APInt.h?rev=197271&r1=197270&r2=197271&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APInt.h?rev=197271&r1=197270&r2=197271&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/include/llvm/ADT/APInt.h (original)<br>
+++ llvm/trunk/include/llvm/ADT/APInt.h Fri Dec 13 14:47:34 2013<br>
@@ -1244,6 +1244,9 @@ public:<br>
  Â /// as "bitPosition".<br>
  Â void flipBit(unsigned bitPosition);<br>
<br>
+ Â /// \brief Returns true if the bit in bitPosition is set.<br>
+ Â bool extractBit(unsigned bitPosition) const;<br>
+<br>
  Â /// @}<br>
  Â /// \name Value Characterization Functions<br>
  Â /// @{<br>
<br>
Modified: llvm/trunk/lib/Support/APInt.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APInt.cpp?rev=197271&r1=197270&r2=197271&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APInt.cpp?rev=197271&r1=197270&r2=197271&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/lib/Support/APInt.cpp (original)<br>
+++ llvm/trunk/lib/Support/APInt.cpp Fri Dec 13 14:47:34 2013<br>
@@ -607,6 +607,14 @@ void APInt::flipBit(unsigned bitPosition<br>
  Â else setBit(bitPosition);<br>
 }<br>
<br>
+bool APInt::extractBit(unsigned bitPosition) const {<br>
+ Â assert(bitPosition < BitWidth && "Out of the bit-width range!");<br>
+ Â if (isSingleWord())<br>
+ Â  Â return VAL & maskBit(bitPosition);<br>
+ Â else<br>
+ Â  Â return pVal[whichWord(bitPosition)] & maskBit(bitPosition);<br>
+}<br>
+<br>
 unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) {<br>
  Â assert(!str.empty() && "Invalid string length");<br>
  Â assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||<br>
<br>
Modified: llvm/trunk/unittests/ADT/APIntTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=197271&r1=197270&r2=197271&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=197271&r1=197270&r2=197271&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)<br>
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Fri Dec 13 14:47:34 2013<br>
@@ -597,4 +597,30 @@ TEST(APIntTest, tcDecrement) {<br>
  Â  Â EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);<br>
  Â }<br>
 }<br>
+<br>
+TEST(APIntTest, extractBit) {<br>
+ Â // Single word check.<br>
+ Â uint64_t E1 = 0x2CA7F46BF6569915ULL;<br>
+ Â APInt A1(64, E1);<br>
+ Â for (unsigned i = 0, e = 64; i < e; ++i) {<br>
+ Â  Â EXPECT_EQ(bool(E1 & (1ULL << i)),<br>
+ Â  Â  Â  Â  Â  Â  Â A1.extractBit(i));<br>
+ Â }<br>
+<br>
+ Â // Multiword check.<br>
+ Â integerPart E2[4] = {<br>
+ Â  Â 0xeb6eb136591cba21ULL,<br>
+ Â  Â 0x7b9358bd6a33f10aULL,<br>
+ Â  Â 0x7e7ffa5eadd8846ULL,<br>
+ Â  Â 0x305f341ca00b613dULL<br>
+ Â };<br>
+ Â APInt A2(integerPartWidth*4, ArrayRef<integerPart>(E2, 4));<br>
+ Â for (unsigned i = 0; i < 4; ++i) {<br>
+ Â  Â for (unsigned j = 0; j < integerPartWidth; ++j) {<br>
+ Â  Â  Â EXPECT_EQ(bool(E2[i] & (1ULL << j)),<br>
+ Â  Â  Â  Â  Â  Â  Â  Â A2.extractBit(i*integerPartWidth + j));<br>
+ Â  Â }<br>
+ Â }<br>
+}<br>
+<br>
 }<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>