[PATCH] D63691: [APInt] Fix getBitsNeeded for INT_MIN values
Dmitry Venikov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 23 02:42:20 PDT 2019
Quolyk created this revision.
Quolyk added reviewers: regehr, RKSimon.
Herald added subscribers: llvm-commits, kristina, dexonsmith.
Herald added a project: LLVM.
This patch fixes behaviour of APInt::getBitsNeeded for INT_MIN 10 bits values.
Repository:
rL LLVM
https://reviews.llvm.org/D63691
Files:
lib/Support/APInt.cpp
unittests/ADT/APIntTest.cpp
Index: unittests/ADT/APIntTest.cpp
===================================================================
--- unittests/ADT/APIntTest.cpp
+++ unittests/ADT/APIntTest.cpp
@@ -1263,8 +1263,10 @@
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(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));
}
TEST(APIntTest, StringBitsNeeded16) {
Index: lib/Support/APInt.cpp
===================================================================
--- lib/Support/APInt.cpp
+++ lib/Support/APInt.cpp
@@ -482,10 +482,14 @@
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();
+ int32_t exactLog = tmp.exactLogBase2();
if (log == (unsigned)-1) {
return isNegative + 1;
+ } else if (isNegative && exactLog > 0) {
+ return isNegative + log;
} else {
return isNegative + log + 1;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63691.206134.patch
Type: text/x-patch
Size: 1352 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190623/69983f26/attachment.bin>
More information about the llvm-commits
mailing list