[llvm] r197272 - [block-freq] Add the method APInt::nearestLogBase2().
Michael Gottesman
mgottesman at apple.com
Fri Dec 13 12:47:37 PST 2013
Author: mgottesman
Date: Fri Dec 13 14:47:37 2013
New Revision: 197272
URL: http://llvm.org/viewvc/llvm-project?rev=197272&view=rev
Log:
[block-freq] Add the method APInt::nearestLogBase2().
Modified:
llvm/trunk/include/llvm/ADT/APInt.h
llvm/trunk/unittests/ADT/APIntTest.cpp
Modified: llvm/trunk/include/llvm/ADT/APInt.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APInt.h?rev=197272&r1=197271&r2=197272&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APInt.h (original)
+++ llvm/trunk/include/llvm/ADT/APInt.h Fri Dec 13 14:47:37 2013
@@ -1507,6 +1507,19 @@ public:
return BitWidth - (*this - 1).countLeadingZeros();
}
+ /// \returns the nearest log base 2 of this APInt. Ties round up.
+ unsigned nearestLogBase2() const {
+ // This is implemented by taking the normal log 2 of a number and adding 1
+ // to it if MSB - 1 is set.
+
+ // We follow the model from logBase2 that logBase2(0) == UINT32_MAX. This
+ // works since if we have 0, MSB will be 0. Then we subtract one yielding
+ // UINT32_MAX. Finally extractBit of MSB - 1 will be UINT32_MAX implying
+ // that we get BitWidth - 1.
+ unsigned lg = logBase2();
+ return lg + unsigned(extractBit(std::min(lg - 1, BitWidth - 1)));
+ }
+
/// \returns the log base 2 of this APInt if its an exact power of two, -1
/// otherwise
int32_t exactLogBase2() const {
Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=197272&r1=197271&r2=197272&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Fri Dec 13 14:47:37 2013
@@ -609,10 +609,10 @@ TEST(APIntTest, extractBit) {
// Multiword check.
integerPart E2[4] = {
- 0xeb6eb136591cba21ULL,
- 0x7b9358bd6a33f10aULL,
- 0x7e7ffa5eadd8846ULL,
- 0x305f341ca00b613dULL
+ 0xEB6EB136591CBA21ULL,
+ 0x7B9358BD6A33F10AULL,
+ 0x7E7FFA5EADD8846ULL,
+ 0x305F341CA00B613DULL
};
APInt A2(integerPartWidth*4, ArrayRef<integerPart>(E2, 4));
for (unsigned i = 0; i < 4; ++i) {
@@ -623,4 +623,40 @@ TEST(APIntTest, extractBit) {
}
}
+TEST(APIntTest, nearestLogBase2) {
+ // Single word check.
+
+ // Test round up.
+ uint64_t I1 = 0x1800001;
+ APInt A1(64, I1);
+ EXPECT_EQ(A1.nearestLogBase2(), A1.ceilLogBase2());
+
+ // Test round down.
+ uint64_t I2 = 0x1000011;
+ APInt A2(64, I2);
+ EXPECT_EQ(A2.nearestLogBase2(), A2.logBase2());
+
+ // Test ties round up.
+ uint64_t I3 = 0x1800000;
+ APInt A3(64, I3);
+ EXPECT_EQ(A3.nearestLogBase2(), A3.ceilLogBase2());
+
+ // Multiple word check.
+
+ // Test round up.
+ integerPart I4[4] = {0x0, 0xF, 0x18, 0x0};
+ APInt A4(integerPartWidth*4, ArrayRef<integerPart>(I4, 4));
+ EXPECT_EQ(A4.nearestLogBase2(), A4.ceilLogBase2());
+
+ // Test round down.
+ integerPart I5[4] = {0x0, 0xF, 0x10, 0x0};
+ APInt A5(integerPartWidth*4, ArrayRef<integerPart>(I5, 4));
+ EXPECT_EQ(A5.nearestLogBase2(), A5.logBase2());
+
+ // Test ties round up.
+ uint64_t I6[4] = {0x0, 0x0, 0x0, 0x18};
+ APInt A6(integerPartWidth*4, ArrayRef<integerPart>(I6, 4));
+ EXPECT_EQ(A6.nearestLogBase2(), A6.ceilLogBase2());
+}
+
}
More information about the llvm-commits
mailing list