APInt tmp(sufficient, StringRef(p, slen), radix);
// Compute how many bits are required. If the log is infinite, assume we need
- // just bit.
+ // just bit. If the log is exact and value is negative, then the value is
+ // MinSignedValue with (log + 1) bits.
unsigned log = tmp.logBase2();
if (log == (unsigned)-1) {
return isNegative + 1;
+ } else if (isNegative && tmp.isPowerOf2()) {
+ return isNegative + log;
} else {
return isNegative + log + 1;
}
EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10));
EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10));
- // TODO: INT_MIN cases need 1 less bit (PR40897)
- EXPECT_EQ(9U, APInt::getBitsNeeded("-128", 10));
+ EXPECT_EQ(1U, APInt::getBitsNeeded("-1", 10));
+ EXPECT_EQ(2U, APInt::getBitsNeeded("-2", 10));
+ EXPECT_EQ(3U, APInt::getBitsNeeded("-4", 10));
+ EXPECT_EQ(4U, APInt::getBitsNeeded("-8", 10));
+ EXPECT_EQ(5U, APInt::getBitsNeeded("-16", 10));
+ EXPECT_EQ(6U, APInt::getBitsNeeded("-23", 10));
+ EXPECT_EQ(6U, APInt::getBitsNeeded("-32", 10));
+ EXPECT_EQ(7U, APInt::getBitsNeeded("-64", 10));
+ EXPECT_EQ(8U, APInt::getBitsNeeded("-127", 10));
+ EXPECT_EQ(8U, APInt::getBitsNeeded("-128", 10));
+ EXPECT_EQ(9U, APInt::getBitsNeeded("-255", 10));
+ EXPECT_EQ(9U, APInt::getBitsNeeded("-256", 10));
+ EXPECT_EQ(10U, APInt::getBitsNeeded("-512", 10));
+ EXPECT_EQ(11U, APInt::getBitsNeeded("-1024", 10));
+ EXPECT_EQ(12U, APInt::getBitsNeeded("-1025", 10));
}
TEST(APIntTest, StringBitsNeeded16) {