diff options
author | Erick Tryzelaar <idadesub@users.sourceforge.net> | 2009-08-21 03:15:14 +0000 |
---|---|---|
committer | Erick Tryzelaar <idadesub@users.sourceforge.net> | 2009-08-21 03:15:14 +0000 |
commit | bb97531a5a13c9d5b2f04b3b714037b1eff7e9a9 (patch) | |
tree | d4a7c4e117e9fd01c7ef11b8c63c5a03266e9018 /lib/Support/APInt.cpp | |
parent | d4b4cf524b8afc342b618254d69f48f214b60093 (diff) | |
download | llvm-bb97531a5a13c9d5b2f04b3b714037b1eff7e9a9.tar.gz llvm-bb97531a5a13c9d5b2f04b3b714037b1eff7e9a9.tar.bz2 llvm-bb97531a5a13c9d5b2f04b3b714037b1eff7e9a9.tar.xz |
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
Diffstat (limited to 'lib/Support/APInt.cpp')
-rw-r--r-- | lib/Support/APInt.cpp | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp index 4e9ee3402b..6b637ba20c 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!"); |