//===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===//
//
// The LLVM Compiler Infrastructure
//
// This file 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 and operations on them.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_APINT_H
#define LLVM_APINT_H
#include "llvm/Support/DataTypes.h"
#include <cassert>
#include <string>
#define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
namespace llvm {
class Serializer;
class Deserializer;
class FoldingSetNodeID;
/* An unsigned host type used as a single part of a multi-part
bignum. */
typedef uint64_t integerPart;
const unsigned int host_char_bit = 8;
const unsigned int integerPartWidth = host_char_bit * sizeof(integerPart);
//===----------------------------------------------------------------------===//
// APInt Class
//===----------------------------------------------------------------------===//
/// APInt - This class represents arbitrary precision constant integral values.
/// It is a functional replacement for common case unsigned integer type like
/// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
/// integer sizes and large integer value types such as 3-bits, 15-bits, or more
/// than 64-bits of precision. APInt provides a variety of arithmetic operators
/// and methods to manipulate integer values of any bit-width. It supports both
/// the typical integer arithmetic and comparison operations as well as bitwise
/// manipulation.
///
/// The class has several invariants worth noting:
/// * All bit, byte, and word positions are zero-based.
/// * Once the bit width is set, it doesn't change except by the Truncate,
/// SignExtend, or ZeroExtend operations.
/// * All binary operators must be on APInt instances of the same bit width.
/// Attempting to use these operators on instances with different bit
/// widths will yield an assertion.
/// * The value is stored canonically as an unsigned value. For operations
/// where it makes a difference, there are both signed and unsigned variants
/// of the operation. For example, sdiv and udiv. However, because the bit
/// widths must be the same, operations such as Mul and Add produce the same
/// results regardless of whether the values are interpreted as signed or
/// not.
/// * In general, the class tries to follow the style of computation that LLVM
/// uses in its IR. This simplifies its use for LLVM.
///
/// @brief Class for arbitrary precision integers.
class APInt {
uint32_t BitWidth; ///< The number of bits in this APInt.
/// This union is used to store the integer value. When the
/// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
union {
uint64_t VAL; ///< Used to store the <= 64 bits integer value.
uint64_t *pVal; ///< Used to store the >64 bits integer value.
};
/// This enum is used to hold the constants we needed for APInt.
enum {
APINT_BITS_PER_WORD = sizeof(uint64_t) * 8, ///< Bits in a word
APINT_WORD_SIZE = sizeof(uint64_t) ///< Byte size of a word
};
/// This constructor is used only internally for speed of construction of
/// temporaries. It is unsafe for general use so it is not public.
/// @brief Fast internal constructor
APInt(uint64_t* val, uint32_t bits) :