llvm.org GIT mirror llvm / 78409ab
[APInt] Fix getBitsNeeded for INT_MIN values Summary: This patch fixes behaviour of APInt::getBitsNeeded for INT_MIN 10 bits values. Reviewers: regehr, RKSimon Reviewed By: RKSimon Subscribers: grandinj, dexonsmith, kristina, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63691 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@364710 91177308-0d34-0410-b5e6-96231b3b80d8 Dmitry Venikov a month ago
2 changed file(s) with 19 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
481481 APInt tmp(sufficient, StringRef(p, slen), radix);
482482
483483 // Compute how many bits are required. If the log is infinite, assume we need
484 // just bit.
484 // just bit. If the log is exact and value is negative, then the value is
485 // MinSignedValue with (log + 1) bits.
485486 unsigned log = tmp.logBase2();
486487 if (log == (unsigned)-1) {
487488 return isNegative + 1;
489 } else if (isNegative && tmp.isPowerOf2()) {
490 return isNegative + log;
488491 } else {
489492 return isNegative + log + 1;
490493 }
12621262 EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10));
12631263 EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10));
12641264
1265 // TODO: INT_MIN cases need 1 less bit (PR40897)
1266 EXPECT_EQ(9U, APInt::getBitsNeeded("-128", 10));
1265 EXPECT_EQ(1U, APInt::getBitsNeeded("-1", 10));
1266 EXPECT_EQ(2U, APInt::getBitsNeeded("-2", 10));
1267 EXPECT_EQ(3U, APInt::getBitsNeeded("-4", 10));
1268 EXPECT_EQ(4U, APInt::getBitsNeeded("-8", 10));
1269 EXPECT_EQ(5U, APInt::getBitsNeeded("-16", 10));
1270 EXPECT_EQ(6U, APInt::getBitsNeeded("-23", 10));
1271 EXPECT_EQ(6U, APInt::getBitsNeeded("-32", 10));
1272 EXPECT_EQ(7U, APInt::getBitsNeeded("-64", 10));
1273 EXPECT_EQ(8U, APInt::getBitsNeeded("-127", 10));
1274 EXPECT_EQ(8U, APInt::getBitsNeeded("-128", 10));
1275 EXPECT_EQ(9U, APInt::getBitsNeeded("-255", 10));
1276 EXPECT_EQ(9U, APInt::getBitsNeeded("-256", 10));
1277 EXPECT_EQ(10U, APInt::getBitsNeeded("-512", 10));
1278 EXPECT_EQ(11U, APInt::getBitsNeeded("-1024", 10));
1279 EXPECT_EQ(12U, APInt::getBitsNeeded("-1025", 10));
12671280 }
12681281
12691282 TEST(APIntTest, StringBitsNeeded16) {