//===-- 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;
// A utility function for allocating memory, checking for allocation failures,
// and ensuring the contents is zeroed.
inline static uint64_t* getClearedMemory(uint32_t numWords) {
uint64_t * result = new uint64_t[numWords];
assert(result && "APInt memory allocation fails!");
memset(result, 0, numWords * sizeof(uint64_t));
return result;
}
// A utility function for allocating memory and checking for allocation failure.
inline static uint64_t* getMemory(uint32_t numWords) {
uint64_t * result = new uint64_t[numWords];
assert(result && "APInt memory allocation fails!");
return result;
}
APInt::APInt(uint32_t 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 {
pVal = getClearedMemory(getNumWords());
pVal[0] = val;
}
}
APInt::APInt(uint32_t numBits, uint32_t 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 {
pVal = getMemory(getNumWords());
// Calculate the actual length of bigVal[].
uint32_t maxN = std::max<uint32_t>(numWords, getNumWords());
uint32_t minN = std::min<uint32_t>(numWords, getNumWords());
memcpy(pVal, bigVal, (minN - 1) * APINT_WORD_SIZE);
pVal[minN-1] = bigVal[minN-1]