From: Dmitry Venikov Date: Sat, 29 Jun 2019 11:38:12 +0000 (+0000) Subject: [APInt] Fix getBitsNeeded for INT_MIN values X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9e9eb62f9fd75706377ac6cf3abffad1328d0d4f;p=platform%2Fupstream%2Fllvm.git [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 llvm-svn: 364710 --- diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp index d008055..3b40d9b 100644 --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -482,10 +482,13 @@ unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) { 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; } diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp index f5f6594..6456066 100644 --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -1263,8 +1263,21 @@ TEST(APIntTest, StringBitsNeeded10) { 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) {