[llvm-commits] [llvm] r83932 - in /llvm/trunk: include/llvm/ADT/APInt.h unittests/ADT/APIntTest.cpp

Dan Gohman gohman at apple.com
Mon Oct 12 18:49:02 PDT 2009


Author: djg
Date: Mon Oct 12 20:49:02 2009
New Revision: 83932

URL: http://llvm.org/viewvc/llvm-project?rev=83932&view=rev
Log:
Add a ceilLogBase2 function to APInt.

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=83932&r1=83931&r2=83932&view=diff

==============================================================================
--- llvm/trunk/include/llvm/ADT/APInt.h (original)
+++ llvm/trunk/include/llvm/ADT/APInt.h Mon Oct 12 20:49:02 2009
@@ -1234,6 +1234,11 @@
     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 {

Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=83932&r1=83931&r2=83932&view=diff

==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Mon Oct 12 20:49:02 2009
@@ -315,6 +315,17 @@
   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) {





More information about the llvm-commits mailing list