Use brute-force algorithm for to_string. It doesn't have to be efficient

at this point, it just needs to work so we can test things reliably.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34262 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Spencer 2007-02-14 02:52:25 +00:00
parent b42b411f74
commit 879dfe1c9c

View File

@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/ADT/APInt.h" #include "llvm/ADT/APInt.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
#include <cstring> #include <cstring>
@ -814,36 +813,44 @@ APInt& APInt::flip(unsigned bitPosition) {
std::string APInt::to_string(uint8_t radix) const { std::string APInt::to_string(uint8_t radix) const {
assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
"Radix should be 2, 8, 10, or 16!"); "Radix should be 2, 8, 10, or 16!");
char *buf = 0; static const char *digits[] = {
unsigned n = getNumWords() * 64 - CountLeadingZeros(); "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
std::string format = radix == 8 ? };
"%0*llo" : (radix == 10 ? "%0*llu" : "%0*llx"); std::string result;
// If the radix is a power of 2, set the format of ostringstream, unsigned bits_used = getNumWords() * 64 - CountLeadingZeros();
// and output the value into buf. if (isSingleWord()) {
if ((radix & (radix - 1)) == 0) { char buf[65];
assert((buf = new char[n / Log2_32(radix) + 2]) && const char *format = (radix == 10 ? "%llu" :
"Memory allocation failed"); (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0)));
if (isSingleWord()) if (format) {
sprintf(buf, format.c_str(), 0, VAL); sprintf(buf, format, VAL);
else { } else {
unsigned offset = sprintf(buf, format.c_str(), 0, pVal[whichWord(n-1)]); memset(buf, 0, 65);
for (int i = whichWord(n-1) - 1; i >= 0; --i) uint64_t v = VAL;
offset += sprintf(buf + offset, format.c_str(), while (bits_used) {
64 / Log2_32(radix) + (64 % Log2_32(radix) ? 1 : 0), pVal[i]); unsigned bit = v & 1;
bits_used--;
buf[bits_used] = digits[bit][0];
v >>=1;
} }
} }
else { // If the radix = 10, need to translate the value into a result = buf;
// string. return result;
assert((buf = new char[(n / 64 + 1) * 20]) && "Memory allocation failed");
if (isSingleWord())
sprintf(buf, format.c_str(), 0, VAL);
else {
// FIXME: To be supported.
} }
APInt tmp(*this);
APInt divisor(radix,64);
if (tmp == 0)
result = "0";
else while (tmp != 0) {
APInt APdigit = APIntOps::URem(tmp,divisor);
unsigned digit = APdigit.getValue();
assert(digit < radix && "URem failed");
result.insert(0,digits[digit]);
tmp = APIntOps::UDiv(tmp, divisor);
} }
std::string retStr(buf);
delete[] buf; return result;
return retStr;
} }
/// getMaxValue - This function returns the largest value /// getMaxValue - This function returns the largest value