diff options
author | Reid Spencer <rspencer@reidspencer.com> | 2007-05-17 19:23:02 +0000 |
---|---|---|
committer | Reid Spencer <rspencer@reidspencer.com> | 2007-05-17 19:23:02 +0000 |
commit | fb0709a180e55325f8b13754df4c9d9671b7b285 (patch) | |
tree | 37a0fc7f97167f4763adc5d23e758a0dc9eb105a /lib/Support/APInt.cpp | |
parent | fd30eee2f0b413de504ca7ce394702b458ec9721 (diff) |
Get rid of leading zeros in the output of toString.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37175 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/APInt.cpp')
-rw-r--r-- | lib/Support/APInt.cpp | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp index 2ed9b07f1e..49945d8497 100644 --- a/lib/Support/APInt.cpp +++ b/lib/Support/APInt.cpp @@ -1938,14 +1938,33 @@ std::string APInt::toString(uint8_t radix, bool wantSigned) const { } if (radix != 10) { - uint64_t mask = radix - 1; - uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : 1); - uint32_t nibbles = APINT_BITS_PER_WORD / shift; - for (uint32_t i = 0; i < getNumWords(); ++i) { - uint64_t value = pVal[i]; - for (uint32_t j = 0; j < nibbles; ++j) { - result.insert(0, digits[ value & mask ]); - value >>= shift; + // For the 2, 8 and 16 bit cases, we can just shift instead of divide + // because the number of bits per digit (1,3 and 4 respectively) divides + // equaly. We just shift until there value is zero. + + // First, check for a zero value and just short circuit the logic below. + if (*this == 0) + result = "0"; + else { + APInt tmp(*this); + size_t insert_at = 0; + if (wantSigned && this->isNegative()) { + // They want to print the signed version and it is a negative value + // Flip the bits and add one to turn it into the equivalent positive + // value and put a '-' in the result. + tmp.flip(); + tmp++; + result = "-"; + insert_at = 1; + } + // Just shift tmp right for each digit width until it becomes zero + uint32_t shift = (radix == 16 ? 4 : (radix == 8 ? 3 : 1)); + uint64_t mask = radix - 1; + APInt zero(tmp.getBitWidth(), 0); + while (tmp.ne(zero)) { + unsigned digit = tmp.getZExtValue() & mask; + tmp = tmp.lshr(shift); + result.insert(insert_at, digits[digit]); } } return result; |