[llvm-commits] CVS: llvm/lib/Support/APInt.cpp
Reid Spencer
reid at x10sys.com
Thu May 17 12:23:20 PDT 2007
Changes in directory llvm/lib/Support:
APInt.cpp updated: 1.84 -> 1.85
---
Log message:
Get rid of leading zeros in the output of toString.
---
Diffs of the changes: (+27 -8)
APInt.cpp | 35 +++++++++++++++++++++++++++--------
1 files changed, 27 insertions(+), 8 deletions(-)
Index: llvm/lib/Support/APInt.cpp
diff -u llvm/lib/Support/APInt.cpp:1.84 llvm/lib/Support/APInt.cpp:1.85
--- llvm/lib/Support/APInt.cpp:1.84 Thu May 17 01:26:29 2007
+++ llvm/lib/Support/APInt.cpp Thu May 17 14:23:02 2007
@@ -1938,14 +1938,33 @@
}
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;
More information about the llvm-commits
mailing list