llvm.org GIT mirror llvm / cbc7cc6
Add a ceilLogBase2 function to APInt. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@83932 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
2 changed file(s) with 16 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
12331233 return BitWidth - 1 - countLeadingZeros();
12341234 }
12351235
1236 /// @returns the ceil log base 2 of this APInt.
1237 unsigned ceilLogBase2() const {
1238 return BitWidth - (*this - 1).countLeadingZeros();
1239 }
1240
12361241 /// @returns the log base 2 of this APInt if its an exact power of two, -1
12371242 /// otherwise
12381243 int32_t exactLogBase2() const {
314314 EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16));
315315 }
316316
317 TEST(APIntTest, Log2) {
318 EXPECT_EQ(APInt(15, 7).logBase2(), 2);
319 EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3);
320 EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1);
321 EXPECT_EQ(APInt(15, 8).logBase2(), 3);
322 EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3);
323 EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3);
324 EXPECT_EQ(APInt(15, 9).logBase2(), 3);
325 EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4);
326 EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
327 }
317328
318329 #ifdef GTEST_HAS_DEATH_TEST
319330 TEST(APIntTest, StringDeath) {