//===-- APInt.cpp - Implement APInt class ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Sheng Zhou and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements a class to represent arbitrary precision integral
// constant values.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/APInt.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Support/MathExtras.h"
#include <cstring>
#include <cstdlib>
using namespace llvm;
#if 0
/// lshift - This function shift x[0:len-1] left by shiftAmt bits, and
/// store the len least significant words of the result in
/// dest[d_offset:d_offset+len-1]. It returns the bits shifted out from
/// the most significant digit.
static uint64_t lshift(uint64_t dest[], unsigned d_offset,
uint64_t x[], unsigned len, unsigned shiftAmt) {
unsigned count = 64 - shiftAmt;
int i = len - 1;
uint64_t high_word = x[i], retVal = high_word >> count;
++d_offset;
while (--i >= 0) {
uint64_t low_word = x[i];
dest[d_offset+i] = (high_word << shiftAmt) | (low_word >> count);
high_word = low_word;
}
dest[d_offset+i] = high_word << shiftAmt;
return retVal;
}
#endif
APInt::APInt(unsigned numBits, uint64_t val)
: BitWidth(numBits) {
assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small");
assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large");
if (isSingleWord())
VAL = val & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth));
else {
// Memory allocation and check if successful.
assert((pVal = new uint64_t[getNumWords()]) &&
"APInt memory allocation fails!");
memset(pVal, 0, getNumWords() * 8);
pVal[0] = val;
}
}
APInt::APInt(unsigned numBits, unsigned numWords, uint64_t bigVal[])
: BitWidth(numBits) {
assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small");
assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large");
assert(bigVal && "Null pointer detected!");
if (isSingleWord())
VAL = bigVal[0] & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth));
else {
// Memory allocation and check if successful.
assert((pVal = new uint64_t[getNumWords()]) &&
"APInt memory allocation fails!");
// Calculate the actual length of bigVal[].
unsigned maxN = std::max<unsigned>(numWords, getNumWords());
unsigned minN = std::min<unsigned>(numWords, getNumWords());
memcpy(pVal, bigVal, (minN - 1) * 8);
pVal[minN-1] = bigVal[minN-1] & (~uint64_t(0ULL) >> (64 - BitWidth % 64));
if (maxN == getNumWords())
memset(pVal+numWords, 0, (getNumWords() - numWords) * 8);
}
}
/// @brief Create a new APInt by translating the char array represented
/// integer value.
APInt::APInt(unsigned numbits, const char StrStart[], unsigned slen,
uint8_t radix