LLVM API Documentation

llvm::APInt Class Reference

Class for arbitrary precision integers. More...

#include <APInt.h>

Inheritance diagram for llvm::APInt:

Inheritance graph
[legend]
Collaboration diagram for llvm::APInt:

Collaboration graph
[legend]

List of all members.

Value Generators

Gets maximum unsigned value of APInt for specific bit width.

APInt getHiBits (uint32_t numBits) const
 HiBits - This function returns the high "numBits" bits of this APInt.
APInt getLoBits (uint32_t numBits) const
 LoBits - This function returns the low "numBits" bits of this APInt.
uint64_t getHashValue () const
 Get a hash value based on this APInt.
const uint64_t * getRawData () const
static APInt getMaxValue (uint32_t numBits)
static APInt getSignedMaxValue (uint32_t numBits)
 Gets maximum signed value of APInt for a specific bit width.
static APInt getMinValue (uint32_t numBits)
 Gets minimum unsigned value of APInt for a specific bit width.
static APInt getSignedMinValue (uint32_t numBits)
 Gets minimum signed value of APInt for a specific bit width.
static APInt getSignBit (uint32_t BitWidth)
 Get the SignBit for a specific bit width.
static APInt getAllOnesValue (uint32_t numBits)
 Get the all-ones value.
static APInt getNullValue (uint32_t numBits)
 Get the '0' value.
static APInt getBitsSet (uint32_t numBits, uint32_t loBit, uint32_t hiBit)
 Get a value with a block of bits set.
static APInt getHighBitsSet (uint32_t numBits, uint32_t hiBitsSet)
 Get a value with high bits set.
static APInt getLowBitsSet (uint32_t numBits, uint32_t loBitsSet)
 Get a value with low bits set.

Binary Operators

Bitwise AND operator.

Performs a bitwise AND operation on *this and RHS.

Returns:
An APInt value representing the bitwise AND of *this and RHS.


APInt operator & (const APInt &RHS) const
APInt And (const APInt &RHS) const
APInt operator| (const APInt &RHS) const
 Bitwise OR operator.
APInt Or (const APInt &RHS) const
APInt operator^ (const APInt &RHS) const
 Bitwise XOR operator.
APInt Xor (const APInt &RHS) const
APInt operator* (const APInt &RHS) const
 Multiplication operator.
APInt operator+ (const APInt &RHS) const
 Addition operator.
APInt operator+ (uint64_t RHS) const
APInt operator- (const APInt &RHS) const
 Subtraction operator.
APInt operator- (uint64_t RHS) const
APInt operator<< (unsigned Bits) const
APInt operator<< (const APInt &Bits) const
APInt ashr (uint32_t shiftAmt) const
 Arithmetic right-shift function.
APInt lshr (uint32_t shiftAmt) const
 Logical right-shift function.
APInt shl (uint32_t shiftAmt) const
 Left-shift function.
APInt rotl (uint32_t rotateAmt) const
 Rotate left by rotateAmt.
APInt rotr (uint32_t rotateAmt) const
 Rotate right by rotateAmt.
APInt ashr (const APInt &shiftAmt) const
 Arithmetic right-shift function.
APInt lshr (const APInt &shiftAmt) const
 Logical right-shift function.
APInt shl (const APInt &shiftAmt) const
 Left-shift function.
APInt rotl (const APInt &rotateAmt) const
 Rotate left by rotateAmt.
APInt rotr (const APInt &rotateAmt) const
 Rotate right by rotateAmt.
APInt udiv (const APInt &RHS) const
 Unsigned division operation.
APInt sdiv (const APInt &RHS) const
 Signed division function for APInt.
APInt urem (const APInt &RHS) const
 Unsigned remainder operation.
APInt srem (const APInt &RHS) const
 Function for signed remainder operation.
bool operator[] (uint32_t bitPosition) const
 Array-indexing support.
static void udivrem (const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
 Dual division/remainder interface.
static void sdivrem (const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)

Value Characterization Functions

uint32_t getBitWidth () const
uint32_t getNumWords () const
 Get the number of words.
uint32_t getActiveBits () const
 Compute the number of active bits in the value.
uint32_t getActiveWords () const
uint32_t getMinSignedBits () const
 Get the minimum bit size for this signed APInt.
uint64_t getZExtValue () const
 Get zero extended value.
int64_t getSExtValue () const
 Get sign extended value.
uint32_t countLeadingZeros () const
uint32_t countLeadingOnes () const
 Count the number of leading one bits.
uint32_t countTrailingZeros () const
 Count the number of trailing zero bits.
uint32_t countTrailingOnes () const
 Count the number of trailing one bits.
uint32_t countPopulation () const
 Count the number of bits set.
static uint32_t getBitsNeeded (const char *str, uint32_t slen, uint8_t radix)
 Get bits required for string value.

Building-block Operations for APInt and APFloat

void dump () const
 debug method
*static void tcSet (integerPart *, integerPart, unsigned int)
static void tcAssign (integerPart *, const integerPart *, unsigned int)
 Assign one bignum to another.
static bool tcIsZero (const integerPart *, unsigned int)
 Returns true if a bignum is zero, false otherwise.
static int tcExtractBit (const integerPart *, unsigned int bit)
 Extract the given bit of a bignum; returns 0 or 1. Zero-based.
static void tcExtract (integerPart *, unsigned int dstCount, const integerPart *, unsigned int srcBits, unsigned int srcLSB)
static void tcSetBit (integerPart *, unsigned int bit)
 Set the given bit of a bignum. Zero-based.
static unsigned int tcLSB (const integerPart *, unsigned int)
static unsigned int tcMSB (const integerPart *parts, unsigned int n)
static void tcNegate (integerPart *, unsigned int)
 Negate a bignum in-place.
static integerPart tcAdd (integerPart *, const integerPart *, integerPart carry, unsigned)
static integerPart tcSubtract (integerPart *, const integerPart *, integerPart carry, unsigned)
static int tcMultiplyPart (integerPart *dst, const integerPart *src, integerPart multiplier, integerPart carry, unsigned int srcParts, unsigned int dstParts, bool add)
static int tcMultiply (integerPart *, const integerPart *, const integerPart *, unsigned)
static unsigned int tcFullMultiply (integerPart *, const integerPart *, const integerPart *, unsigned, unsigned)
static int tcDivide (integerPart *lhs, const integerPart *rhs, integerPart *remainder, integerPart *scratch, unsigned int parts)
static void tcShiftLeft (integerPart *, unsigned int parts, unsigned int count)
static void tcShiftRight (integerPart *, unsigned int parts, unsigned int count)
static void tcAnd (integerPart *, const integerPart *, unsigned int)
 The obvious AND, OR and XOR and complement operations.
static void tcOr (integerPart *, const integerPart *, unsigned int)
static void tcXor (integerPart *, const integerPart *, unsigned int)
static void tcComplement (integerPart *, unsigned int)
static int tcCompare (const integerPart *, const integerPart *, unsigned int)
 Comparison (unsigned) of two bignums.
static integerPart tcIncrement (integerPart *, unsigned int)
 Increment a bignum in-place. Return the carry flag.
static void tcSetLeastSignificantBits (integerPart *, unsigned int, unsigned int bits)
 Set the least significant BITS and clear the rest.

Public Member Functions

Constructors
Create a new APInt of numBits width, initialized as val.

If isSigned is true then val is treated as if it were a signed value (i.e. as an int64_t) and the appropriate sign extension to the bit width will be done. Otherwise, no sign extension occurs (high order bits beyond the range of val are zero filled).

Parameters:
numBits the bit width of the constructed APInt
val the initial value of the APInt
isSigned how to treat signedness of val


 APInt (uint32_t numBits, uint64_t val, bool isSigned=false)
 APInt (uint32_t numBits, uint32_t numWords, const uint64_t bigVal[])
 Construct an APInt of numBits width, initialized as bigVal[].
 APInt (uint32_t numBits, const char strStart[], uint32_t slen, uint8_t radix)
 Construct an APInt from a string representation.
 APInt (const APInt &that)
 Copy Constructor.
 ~APInt ()
 Destructor.
 APInt ()
void Profile (FoldingSetNodeID &id) const
 Profile - This method 'profiles' an APInt for use with FoldingSet.
void Emit (Serializer &S) const
 Used by the Bitcode serializer to emit APInts to Bitcode.
void Read (Deserializer &D)
 Used by the Bitcode deserializer to deserialize APInts.
Value Tests
Determine sign of this APInt.

This tests the high bit of this APInt to determine if it is set.

Returns:
true if this APInt is negative, false otherwise


bool isNegative () const
bool isNonNegative () const
 Determine if this APInt Value is non-negative (>= 0).
bool isStrictlyPositive () const
 Determine if this APInt Value is positive.
bool isAllOnesValue () const
 Determine if all bits are set.
bool isMaxValue () const
 Determine if this is the largest unsigned value.
bool isMaxSignedValue () const
 Determine if this is the largest signed value.
bool isMinValue () const
 Determine if this is the smallest unsigned value.
bool isMinSignedValue () const
 Determine if this is the smallest signed value.
bool isIntN (uint32_t N) const
 Check if this APInt has an N-bits unsigned integer value.
bool isSignedIntN (uint32_t N) const
 Check if this APInt has an N-bits signed integer value.
bool isPowerOf2 () const
bool isSignBit () const
 isSignBit - Return true if this is the value returned by getSignBit.
bool getBoolValue () const
 Boolean conversion function.
uint64_t getLimitedValue (uint64_t Limit=~0ULL) const
Unary Operators
Postfix increment operator.

Returns:
a new APInt value representing *this incremented by one


const APInt operator++ (int)
APIntoperator++ ()
 Prefix increment operator.
const APInt operator-- (int)
 Postfix decrement operator.
APIntoperator-- ()
 Prefix decrement operator.
APInt operator~ () const
 Unary bitwise complement operator.
APInt operator- () const
 Unary negation operator.
bool operator! () const
 Logical negation operator.
Assignment Operators
Copy assignment operator.

Returns:
*this after assignment of RHS.


APIntoperator= (const APInt &RHS)
APIntoperator= (uint64_t RHS)
 Assignment operator.
APIntoperator &= (const APInt &RHS)
 Bitwise AND assignment operator.
APIntoperator|= (const APInt &RHS)
 Bitwise OR assignment operator.
APIntoperator^= (const APInt &RHS)
 Bitwise XOR assignment operator.
APIntoperator*= (const APInt &RHS)
 Multiplication assignment operator.
APIntoperator+= (const APInt &RHS)
 Addition assignment operator.
APIntoperator-= (const APInt &RHS)
 Subtraction assignment operator.
APIntoperator<<= (uint32_t shiftAmt)
 Left-shift assignment function.
Comparison Operators
Equality operator.

Compares this APInt with RHS for the validity of the equality relationship.

bool operator== (const APInt &RHS) const
bool operator== (uint64_t Val) const
 Equality operator.
bool eq (const APInt &RHS) const
 Equality comparison.
bool operator!= (const APInt &RHS) const
 Inequality operator.
bool operator!= (uint64_t Val) const
 Inequality operator.
bool ne (const APInt &RHS) const
 Inequality comparison.
bool ult (const APInt &RHS) const
 Unsigned less than comparison.
bool slt (const APInt &RHS) const
 Signed less than comparison.
bool ule (const APInt &RHS) const
 Unsigned less or equal comparison.
bool sle (const APInt &RHS) const
 Signed less or equal comparison.
bool ugt (const APInt &RHS) const
 Unsigned greather than comparison.
bool sgt (const APInt &RHS) const
 Signed greather than comparison.
bool uge (const APInt &RHS) const
 Unsigned greater or equal comparison.
bool sge (const APInt &RHS) const
 Signed greather or equal comparison.
bool intersects (const APInt &RHS) const
Resizing Operators
Truncate to new width.

Truncate the APInt to a specified width. It is an error to specify a width that is greater than or equal to the current width.

APInttrunc (uint32_t width)
APIntsext (uint32_t width)
 Sign extend to a new width.
APIntzext (uint32_t width)
 Zero extend to a new width.
APIntsextOrTrunc (uint32_t width)
 Sign extend or truncate to width.
APIntzextOrTrunc (uint32_t width)
 Zero extend or truncate to width.
Bit Manipulation Operators
Set every bit to 1.

APIntset ()
APIntset (uint32_t bitPosition)
 Set a given bit to 1.
APIntclear ()
 Set every bit to 0.
APIntclear (uint32_t bitPosition)
 Set a given bit to 0.
APIntflip ()
 Toggle every bit to its opposite value.
APIntflip (uint32_t bitPosition)
 Toggles a given bit to its opposite value.
Conversion Functions
void print (raw_ostream &OS, bool isSigned) const
void toString (SmallVectorImpl< char > &Str, unsigned Radix, bool Signed) const
void toStringUnsigned (SmallVectorImpl< char > &Str, unsigned Radix=10) const
void toStringSigned (SmallVectorImpl< char > &Str, unsigned Radix=10) const
std::string toString (unsigned Radix, bool Signed) const
APInt byteSwap () const
double roundToDouble (bool isSigned) const
 Converts this APInt to a double value.
double roundToDouble () const
 Converts this unsigned APInt to a double value.
double signedRoundToDouble () const
 Converts this signed APInt to a double value.
double bitsToDouble () const
 Converts APInt bits to a double.
float bitsToFloat () const
 Converts APInt bits to a double.
APIntdoubleToBits (double V)
 Converts a double to APInt bits.
APIntfloatToBits (float V)
 Converts a float to APInt bits.
Mathematics Operations
uint32_t logBase2 () const
int32_t exactLogBase2 () const
APInt sqrt () const
 Compute the square root.
APInt abs () const
 Get the absolute value;.
APInt multiplicativeInverse (const APInt &modulo) const


Detailed Description

Class for arbitrary precision integers.

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.

Definition at line 71 of file APInt.h.


Constructor & Destructor Documentation

llvm::APInt::APInt ( uint32_t  numBits,
uint64_t  val,
bool  isSigned = false 
) [inline]

Definition at line 212 of file APInt.h.

References VAL.

APInt::APInt ( uint32_t  numBits,
uint32_t  numWords,
const uint64_t  bigVal[] 
)

Construct an APInt of numBits width, initialized as bigVal[].

Note that numWords can be smaller or larger than the corresponding bit width but any extraneous bits will be dropped.

Parameters:
numBits the bit width of the constructed APInt
numWords the number of words in bigVal
bigVal a sequence of words to form the initial value of the APInt

Definition at line 53 of file APInt.cpp.

References getClearedMemory(), getNumWords(), pVal, and VAL.

APInt::APInt ( uint32_t  numBits,
const char  strStart[],
uint32_t  slen,
uint8_t  radix 
)

Construct an APInt from a string representation.

This constructor interprets the slen characters starting at StrStart as a string in the given radix. The interpretation stops when the first character that is not suitable for the radix is encountered. Acceptable radix values are 2, 8, 10 and 16. It is an error for the value implied by the string to require more bits than numBits.

Parameters:
numBits the bit width of the constructed APInt
strStart the start of the string to be interpreted
slen the maximum number of characters to interpret
radix the radix to use for the conversion

Definition at line 71 of file APInt.cpp.

llvm::APInt::APInt ( const APInt that  )  [inline]

Copy Constructor.

Simply makes *this a copy of that.

Definition at line 244 of file APInt.h.

References VAL.

llvm::APInt::~APInt (  )  [inline]

Destructor.

Definition at line 254 of file APInt.h.

References pVal.

llvm::APInt::APInt (  )  [inline, explicit]

Default constructor that creates an uninitialized APInt. This is useful for object deserialization (pair this with the static method Read).

Definition at line 261 of file APInt.h.

Referenced by ashr(), byteSwap(), getAllOnesValue(), getHighBitsSet(), getLowBitsSet(), getMaxValue(), getMinValue(), getNullValue(), getSignedMaxValue(), getSignedMinValue(), isPowerOf2(), lshr(), multiplicativeInverse(), operator &(), operator*(), operator+(), operator-(), operator^(), operator|(), shl(), sqrt(), udiv(), udivrem(), and urem().


Member Function Documentation

void APInt::Profile ( FoldingSetNodeID id  )  const

Profile - This method 'profiles' an APInt for use with FoldingSet.

Profile - Used to insert APInt objects, or objects that contain APInt objects, into FoldingSets.

Reimplemented in llvm::APSInt.

Definition at line 127 of file APInt.cpp.

References llvm::FoldingSetNodeID::AddInteger(), getNumWords(), and pVal.

Referenced by llvm::APSInt::Profile().

void APInt::Emit ( Serializer S  )  const

Used by the Bitcode serializer to emit APInts to Bitcode.

Definition at line 20 of file SerializeAPInt.cpp.

References llvm::Serializer::EmitInt(), getNumWords(), pVal, and VAL.

void APInt::Read ( Deserializer D  ) 

Used by the Bitcode deserializer to deserialize APInts.

Definition at line 20 of file DeserializeAPInt.cpp.

References pVal, llvm::Deserializer::ReadInt(), and VAL.

bool llvm::APInt::isNegative (  )  const [inline]

bool llvm::APInt::isNonNegative (  )  const [inline]

Determine if this APInt Value is non-negative (>= 0).

This tests the high bit of the APInt to determine if it is unset.

Definition at line 285 of file APInt.h.

References isNegative().

Referenced by isStrictlyPositive().

bool llvm::APInt::isStrictlyPositive (  )  const [inline]

Determine if this APInt Value is positive.

This tests if the value of this APInt is positive (> 0). Note that 0 is not a positive value.

Returns:
true if this APInt is positive.

Definition at line 293 of file APInt.h.

References isNonNegative().

Referenced by llvm::ComputeMaskedBits(), and llvm::SelectionDAG::ComputeMaskedBits().

bool llvm::APInt::isAllOnesValue (  )  const [inline]

Determine if all bits are set.

This checks to see if the value has all bits of the APInt are set or not.

Definition at line 299 of file APInt.h.

References countPopulation().

Referenced by llvm::ConstantInt::isAllOnesValue(), and llvm::TargetLowering::SimplifyDemandedBits().

bool llvm::APInt::isMaxValue (  )  const [inline]

Determine if this is the largest unsigned value.

This checks to see if the value of this APInt is the maximum unsigned value for the APInt's bit width.

Definition at line 306 of file APInt.h.

References countPopulation().

Referenced by llvm::ConstantRange::ConstantRange(), llvm::ConstantRange::isFullSet(), and llvm::ConstantInt::isMaxValue().

bool llvm::APInt::isMaxSignedValue (  )  const [inline]

Determine if this is the largest signed value.

This checks to see if the value of this APInt is the maximum signed value for the APInt's bit width.

Definition at line 313 of file APInt.h.

References countPopulation(), isNegative(), and VAL.

Referenced by llvm::ConstantInt::isMaxValue().

bool llvm::APInt::isMinValue (  )  const [inline]

Determine if this is the smallest unsigned value.

This checks to see if the value of this APInt is the minimum unsigned value for the APInt's bit width.

Definition at line 321 of file APInt.h.

References countPopulation().

Referenced by llvm::ConstantRange::ConstantRange(), llvm::ConstantRange::isEmptySet(), and llvm::ConstantInt::isMinValue().

bool llvm::APInt::isMinSignedValue (  )  const [inline]

Determine if this is the smallest signed value.

This checks to see if the value of this APInt is the minimum signed value for the APInt's bit width.

Definition at line 328 of file APInt.h.

References countPopulation(), isNegative(), and VAL.

Referenced by llvm::ConstantFoldBinaryInstruction(), llvm::ConstantInt::isMinValue(), and isSignBit().

bool llvm::APInt::isIntN ( uint32_t  N  )  const [inline]

Check if this APInt has an N-bits unsigned integer value.

Definition at line 334 of file APInt.h.

References getNumWords(), pVal, and VAL.

Referenced by llvm::APIntOps::isIntN().

bool llvm::APInt::isSignedIntN ( uint32_t  N  )  const [inline]

Check if this APInt has an N-bits signed integer value.

Definition at line 345 of file APInt.h.

References getMinSignedBits().

Referenced by llvm::APIntOps::isSignedIntN().

bool APInt::isPowerOf2 (  )  const

Returns:
true if the argument APInt value is a power of two > 0.

Definition at line 650 of file APInt.cpp.

References APInt().

Referenced by llvm::ComputeMaskedBits(), llvm::SelectionDAG::ComputeMaskedBits(), exactLogBase2(), isOneBitSet(), and llvm::SCEVExpander::visitUDivExpr().

bool llvm::APInt::isSignBit (  )  const [inline]

isSignBit - Return true if this is the value returned by getSignBit.

Definition at line 354 of file APInt.h.

References isMinSignedValue().

Referenced by isSignBitCheck().

bool llvm::APInt::getBoolValue (  )  const [inline]

Boolean conversion function.

This converts the APInt to a boolean value as a test against zero.

Definition at line 358 of file APInt.h.

Referenced by llvm::SelectionDAG::ComputeMaskedBits(), and llvm::PPCTargetLowering::SelectAddressRegReg().

uint64_t llvm::APInt::getLimitedValue ( uint64_t  Limit = ~0ULL  )  const [inline]

getLimitedValue - If this value is smaller than the specified limit, return it, otherwise return the limit value. This causes the value to saturate to the limit.

Definition at line 365 of file APInt.h.

References getActiveBits(), and getZExtValue().

Referenced by ashr(), llvm::ConstantInt::getLimitedValue(), lshr(), rotl(), rotr(), and shl().

static APInt llvm::APInt::getMaxValue ( uint32_t  numBits  )  [inline, static]

static APInt llvm::APInt::getSignedMaxValue ( uint32_t  numBits  )  [inline, static]

Gets maximum signed value of APInt for a specific bit width.

Definition at line 379 of file APInt.h.

References APInt().

Referenced by llvm::APSInt::getMaxValue(), llvm::ConstantRange::getSignedMax(), and llvm::TargetLowering::SimplifySetCC().

static APInt llvm::APInt::getMinValue ( uint32_t  numBits  )  [inline, static]

Gets minimum unsigned value of APInt for a specific bit width.

Definition at line 384 of file APInt.h.

References APInt().

Referenced by llvm::ConstantRange::ConstantRange(), llvm::APSInt::getMinValue(), llvm::ConstantRange::getUnsignedMin(), llvm::ICmpInst::makeConstantRange(), and llvm::TargetLowering::SimplifySetCC().

static APInt llvm::APInt::getSignedMinValue ( uint32_t  numBits  )  [inline, static]

Gets minimum signed value of APInt for a specific bit width.

Definition at line 389 of file APInt.h.

References APInt().

Referenced by BinomialCoefficient(), llvm::APSInt::getMinValue(), getSignBit(), llvm::ConstantRange::getSignedMin(), llvm::ICmpInst::makeConstantRange(), and llvm::TargetLowering::SimplifySetCC().

static APInt llvm::APInt::getSignBit ( uint32_t  BitWidth  )  [inline, static]

Get the SignBit for a specific bit width.

getSignBit - This is just a wrapper function of getSignedMinValue(), and it helps code readability when we want to get a SignBit.

Definition at line 396 of file APInt.h.

References getSignedMinValue().

Referenced by llvm::ComputeMaskedBits(), llvm::SelectionDAG::ComputeMaskedBits(), llvm::SelectionDAG::SignBitIsZero(), and llvm::TargetLowering::SimplifyDemandedBits().

static APInt llvm::APInt::getAllOnesValue ( uint32_t  numBits  )  [inline, static]

Get the all-ones value.

Returns:
the all-ones value for an APInt of th