From: Reid Spencer Date: Thu, 17 May 2007 19:23:02 +0000 (+0000) Subject: Get rid of leading zeros in the output of toString. X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=fb0709a180e55325f8b13754df4c9d9671b7b285 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 --- diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp index 2ed9b07f1ea..49945d84978 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;