//===-- 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 <strings.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdlib>
using namespace llvm;
APInt::APInt(uint64_t val, unsigned numBits, bool sign)
: bitsnum(numBits), isSigned(sign) {
assert(bitsnum >= IntegerType::MIN_INT_BITS && "bitwidth too small");
assert(bitsnum <= IntegerType::MAX_INT_BITS && "bitwidth too large");
if (isSingleWord())
VAL = val & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - bitsnum));
else {
// Memory allocation and check if successful.
assert((pVal = new uint64_t[numWords()]) &&
"APInt memory allocation fails!");
bzero(pVal, numWords() * 8);
pVal[0] = val;
}
}
APInt::APInt(unsigned numBits, uint64_t bigVal[], bool sign)
: bitsnum(numBits), isSigned(sign) {
assert(bitsnum >= IntegerType::MIN_INT_BITS && "bitwidth too small");
assert(bitsnum <= IntegerType::MAX_INT_BITS && "bitwidth too large");
assert(bigVal && "Null pointer detected!");
if (isSingleWord())
VAL = bigVal[0] & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - bitsnum));
else {
// Memory allocation and check if successful.
assert((pVal = new uint64_t[numWords()]) &&
"APInt memory allocation fails!");
// Calculate the actual length of bigVal[].
unsigned n = sizeof(*bigVal) / sizeof(bigVal[0]);
unsigned maxN = std::max<unsigned>(n, numWords());
unsigned minN = std::min<unsigned>(n, numWords());
memcpy(pVal, bigVal, (minN - 1) * 8);
pVal[minN-1] = bigVal[minN-1] & (~uint64_t(0ULL) >> (64 - bitsnum % 64));
if (maxN == numWords())
bzero(pVal+n, (numWords() - n) * 8);
}
}
APInt::APInt(std::string& Val, uint8_t radix, bool sign)
: isSigned(sign) {
assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
"Radix should be 2, 8, 10, or 16!");
assert(!Val.empty() && "String empty?");
unsigned slen = Val.size();
unsigned size = 0;
// If the radix is a power of 2, read the input
// from most significant to least significant.
if ((radix & (radix - 1)) == 0) {
unsigned nextBitPos = 0, bits_per_digit = radix / 8 + 2;
uint64_t resDigit = 0;
bitsnum = slen * bits_per_digit;
if (numWords() > 1)
assert((pVal = new uint64_t[numWords()]) &&
"APInt memory allocation fails!");
for (int i = slen