summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Gohman <gohman@apple.com>2009-10-13 01:49:02 +0000
committerDan Gohman <gohman@apple.com>2009-10-13 01:49:02 +0000
commitc4e367b85430cfc4a0919ac8f16abb6884f720ea (patch)
treea22c6861bea9e10e4ef7967572cacb4043998afd
parent70e8505eb1a5379d884f3bfd8cc7f9f252edcaec (diff)
downloadbcm5719-llvm-c4e367b85430cfc4a0919ac8f16abb6884f720ea.tar.gz
bcm5719-llvm-c4e367b85430cfc4a0919ac8f16abb6884f720ea.zip
Add a ceilLogBase2 function to APInt.
llvm-svn: 83932
-rw-r--r--llvm/include/llvm/ADT/APInt.h5
-rw-r--r--llvm/unittests/ADT/APIntTest.cpp11
2 files changed, 16 insertions, 0 deletions
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 6c418bda62d..88aa9956d93 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -1234,6 +1234,11 @@ public:
return BitWidth - 1 - countLeadingZeros();
}
+ /// @returns the ceil log base 2 of this APInt.
+ unsigned ceilLogBase2() const {
+ return BitWidth - (*this - 1).countLeadingZeros();
+ }
+
/// @returns the log base 2 of this APInt if its an exact power of two, -1
/// otherwise
int32_t exactLogBase2() const {
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 24a3d9b4ade..7b03d0f00c8 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -315,6 +315,17 @@ TEST(APIntTest, StringBitsNeeded16) {
EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16));
}
+TEST(APIntTest, Log2) {
+ EXPECT_EQ(APInt(15, 7).logBase2(), 2);
+ EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3);
+ EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1);
+ EXPECT_EQ(APInt(15, 8).logBase2(), 3);
+ EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3);
+ EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3);
+ EXPECT_EQ(APInt(15, 9).logBase2(), 3);
+ EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4);
+ EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
+}
#ifdef GTEST_HAS_DEATH_TEST
TEST(APIntTest, StringDeath) {
OpenPOWER on IntegriCloud