aboutsummaryrefslogtreecommitdiff
path: root/lib/Support/APInt.cpp
diff options
context:
space:
mode:
authorErick Tryzelaar <idadesub@users.sourceforge.net>2009-08-21 03:15:28 +0000
committerErick Tryzelaar <idadesub@users.sourceforge.net>2009-08-21 03:15:28 +0000
commitae8f78d4de403965603ed2b61898d820db2449f9 (patch)
treeddc274606143a4aa9f246ba9e1b1498088696905 /lib/Support/APInt.cpp
parentbb97531a5a13c9d5b2f04b3b714037b1eff7e9a9 (diff)
Fix bug with APInt::getBitsNeeded with for base 10 numbers 0-9.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79593 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/APInt.cpp')
-rw-r--r--lib/Support/APInt.cpp72
1 files changed, 42 insertions, 30 deletions
diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp
index 6b637ba20c..b4e94c9184 100644
--- a/lib/Support/APInt.cpp
+++ b/lib/Support/APInt.cpp
@@ -44,6 +44,35 @@ inline static uint64_t* getMemory(unsigned numWords) {
return result;
}
+/// A utility function that converts a character to a digit.
+inline static unsigned getDigit(char cdigit, uint8_t radix) {
+ // Get a digit
+ unsigned digit = 0;
+ if (radix == 16) {
+ if (!isxdigit(cdigit))
+ llvm_unreachable("Invalid hex digit in string");
+ if (isdigit(cdigit))
+ digit = cdigit - '0';
+ else if (cdigit >= 'a')
+ digit = cdigit - 'a' + 10;
+ else if (cdigit >= 'A')
+ digit = cdigit - 'A' + 10;
+ else
+ llvm_unreachable("huh? we shouldn't get here");
+ } else if (isdigit(cdigit)) {
+ digit = cdigit - '0';
+ assert((radix == 10 ||
+ (radix == 8 && digit != 8 && digit != 9) ||
+ (radix == 2 && (digit == 0 || digit == 1))) &&
+ "Invalid digit in string for given radix");
+ } else {
+ llvm_unreachable("Invalid character in digit string");
+ }
+
+ return digit;
+}
+
+
void APInt::initSlowCase(unsigned numBits, uint64_t val, bool isSigned) {
pVal = getClearedMemory(getNumWords());
pVal[0] = val;
@@ -611,22 +640,27 @@ unsigned APInt::getBitsNeeded(const StringRef& str, uint8_t radix) {
if (radix == 16)
return slen * 4 + isNegative;
- // Otherwise it must be radix == 10, the hard case
- assert(radix == 10 && "Invalid radix");
-
// This is grossly inefficient but accurate. We could probably do something
// with a computation of roughly slen*64/20 and then adjust by the value of
// the first few digits. But, I'm not sure how accurate that could be.
// Compute a sufficient number of bits that is always large enough but might
- // be too large. This avoids the assertion in the constructor.
- unsigned sufficient = slen*64/18;
+ // be too large. This avoids the assertion in the constructor. This
+ // calculation doesn't work appropriately for the numbers 0-9, so just use 4
+ // bits in that case.
+ unsigned sufficient = slen == 1 ? 4 : slen * 64/18;
// Convert to the actual binary value.
APInt tmp(sufficient, StringRef(p, slen), radix);
- // Compute how many bits are required.
- return isNegative + tmp.logBase2() + 1;
+ // Compute how many bits are required. If the log is infinite, assume we need
+ // just bit.
+ unsigned log = tmp.logBase2();
+ if (log == (unsigned)-1) {
+ return isNegative + 1;
+ } else {
+ return isNegative + log + 1;
+ }
}
// From http://www.burtleburtle.net, byBob Jenkins.
@@ -2039,29 +2073,7 @@ void APInt::fromString(unsigned numbits, const StringRef& str, uint8_t radix) {
// Enter digit traversal loop
for (StringRef::iterator e = str.end(); p != e; ++p) {
- // Get a digit
- unsigned digit = 0;
- char cdigit = *p;
- if (radix == 16) {
- if (!isxdigit(cdigit))
- llvm_unreachable("Invalid hex digit in string");
- if (isdigit(cdigit))
- digit = cdigit - '0';
- else if (cdigit >= 'a')
- digit = cdigit - 'a' + 10;
- else if (cdigit >= 'A')
- digit = cdigit - 'A' + 10;
- else
- llvm_unreachable("huh? we shouldn't get here");
- } else if (isdigit(cdigit)) {
- digit = cdigit - '0';
- assert((radix == 10 ||
- (radix == 8 && digit != 8 && digit != 9) ||
- (radix == 2 && (digit == 0 || digit == 1))) &&
- "Invalid digit in string for given radix");
- } else {
- llvm_unreachable("Invalid character in digit string");
- }
+ unsigned digit = getDigit(*p, radix);
// Shift or multiply the value by the radix
if (slen > 1) {