From: Erick Tryzelaar Date: Fri, 21 Aug 2009 03:15:14 +0000 (+0000) Subject: Allow '+' to appear in APInt strings, and add more unit tests. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=bb97531a5a13c9d5b2f04b3b714037b1eff7e9a9;p=oota-llvm.git Allow '+' to appear in APInt strings, and add more unit tests. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79592 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp index 4e9ee3402b3..6b637ba20cb 100644 --- a/lib/Support/APInt.cpp +++ b/lib/Support/APInt.cpp @@ -60,7 +60,7 @@ void APInt::initSlowCase(const APInt& that) { APInt::APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]) : BitWidth(numBits), VAL(0) { - assert(BitWidth && "bitwidth too small"); + assert(BitWidth && "Bitwidth too small"); assert(bigVal && "Null pointer detected!"); if (isSingleWord()) VAL = bigVal[0]; @@ -78,7 +78,7 @@ APInt::APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]) APInt::APInt(unsigned numbits, const StringRef& Str, uint8_t radix) : BitWidth(numbits), VAL(0) { - assert(BitWidth && "bitwidth too small"); + assert(BitWidth && "Bitwidth too small"); fromString(numbits, Str, radix); } @@ -589,12 +589,16 @@ APInt& APInt::flip(unsigned bitPosition) { unsigned APInt::getBitsNeeded(const StringRef& str, uint8_t radix) { assert(!str.empty() && "Invalid string length"); + assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && + "Radix should be 2, 8, 10, or 16!"); size_t slen = str.size(); // Each computation below needs to know if its negative + StringRef::iterator p = str.begin(); unsigned isNegative = str.front() == '-'; - if (isNegative) { + if (*p == '-' || *p == '+') { + p++; slen--; assert(slen && "string is only a minus!"); } @@ -619,7 +623,7 @@ unsigned APInt::getBitsNeeded(const StringRef& str, uint8_t radix) { unsigned sufficient = slen*64/18; // Convert to the actual binary value. - APInt tmp(sufficient, str.substr(isNegative), radix); + APInt tmp(sufficient, StringRef(p, slen), radix); // Compute how many bits are required. return isNegative + tmp.logBase2() + 1; @@ -2004,13 +2008,14 @@ void APInt::udivrem(const APInt &LHS, const APInt &RHS, void APInt::fromString(unsigned numbits, const StringRef& str, uint8_t radix) { // Check our assumptions here + assert(!str.empty() && "Invalid string length"); assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && "Radix should be 2, 8, 10, or 16!"); - assert(!str.empty() && "Invalid string length"); + StringRef::iterator p = str.begin(); size_t slen = str.size(); bool isNeg = *p == '-'; - if (isNeg) { + if (*p == '-' || *p == '+') { p++; slen--; assert(slen && "string is only a minus!"); diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp index dc8ba336359..8208af30cd0 100644 --- a/unittests/ADT/APIntTest.cpp +++ b/unittests/ADT/APIntTest.cpp @@ -164,18 +164,82 @@ TEST(APIntTest, i1) { } TEST(APIntTest, fromString) { - EXPECT_EQ(APInt(1, 0), APInt(1, "0", 10)); - EXPECT_EQ(APInt(1, 1), APInt(1, "1", 10)); - EXPECT_EQ(APInt(1, 1), APInt(1, "-1", 10)); - EXPECT_EQ(APInt(1, 1), APInt(1, "1", 2)); - EXPECT_EQ(APInt(1, 1), APInt(1, "1", 8)); - EXPECT_EQ(APInt(1, 1), APInt(1, "1", 16)); + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2)); + EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2)); + EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2)); + EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2)); + + EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2)); + EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2)); + EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2)); + EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2)); + EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2)); + + EXPECT_EQ(APInt(32, -0), APInt(32, "-0", 2)); + EXPECT_EQ(APInt(32, -1), APInt(32, "-1", 2)); + EXPECT_EQ(APInt(32, -2), APInt(32, "-10", 2)); + EXPECT_EQ(APInt(32, -3), APInt(32, "-11", 2)); + EXPECT_EQ(APInt(32, -4), APInt(32, "-100", 2)); + + + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8)); + EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8)); + EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8)); + EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8)); + EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8)); + + EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8)); + EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8)); + EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8)); + EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8)); + EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8)); + EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8)); + + EXPECT_EQ(APInt(32, -0), APInt(32, "-0", 8)); + EXPECT_EQ(APInt(32, -1), APInt(32, "-1", 8)); + EXPECT_EQ(APInt(32, -7), APInt(32, "-7", 8)); + EXPECT_EQ(APInt(32, -8), APInt(32, "-10", 8)); + EXPECT_EQ(APInt(32, -15), APInt(32, "-17", 8)); + EXPECT_EQ(APInt(32, -16), APInt(32, "-20", 8)); + + + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10)); + EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10)); + EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10)); + EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10)); + EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10)); + + EXPECT_EQ(APInt(32, -0), APInt(32, "-0", 10)); + EXPECT_EQ(APInt(32, -1), APInt(32, "-1", 10)); + EXPECT_EQ(APInt(32, -9), APInt(32, "-9", 10)); + EXPECT_EQ(APInt(32, -10), APInt(32, "-10", 10)); + EXPECT_EQ(APInt(32, -19), APInt(32, "-19", 10)); + EXPECT_EQ(APInt(32, -20), APInt(32, "-20", 10)); + + + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16)); + EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16)); + EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16)); + EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16)); + EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16)); + + EXPECT_EQ(APInt(32, -0), APInt(32, "-0", 16)); + EXPECT_EQ(APInt(32, -1), APInt(32, "-1", 16)); + EXPECT_EQ(APInt(32, -15), APInt(32, "-F", 16)); + EXPECT_EQ(APInt(32, -16), APInt(32, "-10", 16)); + EXPECT_EQ(APInt(32, -31), APInt(32, "-1F", 16)); + EXPECT_EQ(APInt(32, -32), APInt(32, "-20", 16)); } #ifdef GTEST_HAS_DEATH_TEST TEST(APIntTest, StringDeath) { - EXPECT_DEATH(APInt(0, "", 0), "bitwidth too small"); - EXPECT_DEATH(APInt(32, "", 0), "Radix should be 2, 8, 10, or 16!"); + EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small"); + EXPECT_DEATH(APInt(32, "", 0), "Invalid string length"); + EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, or 16!"); EXPECT_DEATH(APInt(32, "", 10), "Invalid string length"); EXPECT_DEATH(APInt(32, "-", 10), "string is only a minus!"); EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width");