2007-02-05 17:29:16 +00:00
|
|
|
//===-- llvm/Support/APInt.h - For Arbitrary Precision Integer -*- C++ -*--===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_APINT_H
|
|
|
|
#define LLVM_APINT_H
|
|
|
|
|
|
|
|
#include "llvm/Support/DataTypes.h"
|
2007-02-07 16:59:17 +00:00
|
|
|
#include <cassert>
|
2007-02-05 17:29:16 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2007-02-08 14:35:19 +00:00
|
|
|
/// Forward declaration.
|
|
|
|
class APInt;
|
|
|
|
namespace APIntOps {
|
2007-02-16 22:36:51 +00:00
|
|
|
APInt udiv(const APInt& LHS, const APInt& RHS);
|
|
|
|
APInt urem(const APInt& LHS, const APInt& RHS);
|
2007-02-08 14:35:19 +00:00
|
|
|
}
|
|
|
|
|
2007-02-05 17:29:16 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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
|
2007-02-16 22:36:51 +00:00
|
|
|
/// integer sizes and large integer value types such as 3-bits, 15-bits, or more
|
2007-02-05 17:29:16 +00:00
|
|
|
/// than 64-bits of precision. APInt provides a variety of arithmetic operators
|
2007-02-16 22:36:51 +00:00
|
|
|
/// 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.
|
2007-02-05 17:29:16 +00:00
|
|
|
///
|
2007-02-16 22:36:51 +00:00
|
|
|
/// 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.
|
2007-02-06 05:59:47 +00:00
|
|
|
///
|
2007-02-16 22:36:51 +00:00
|
|
|
/// @brief Class for arbitrary precision integers.
|
2007-02-05 17:29:16 +00:00
|
|
|
class APInt {
|
2007-02-25 07:29:03 +00:00
|
|
|
|
2007-02-18 18:42:35 +00:00
|
|
|
uint32_t BitWidth; ///< The number of bits in this APInt.
|
2007-02-05 17:29:16 +00:00
|
|
|
|
|
|
|
/// This union is used to store the integer value. When the
|
2007-02-06 05:59:47 +00:00
|
|
|
/// integer bit-width <= 64, it uses VAL;
|
|
|
|
/// otherwise it uses the pVal.
|
2007-02-05 17:29:16 +00:00
|
|
|
union {
|
|
|
|
uint64_t VAL; ///< Used to store the <= 64 bits integer value.
|
|
|
|
uint64_t *pVal; ///< Used to store the >64 bits integer value.
|
|
|
|
};
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// This enum is just used to hold a constant we needed for APInt.
|
2007-02-05 17:29:16 +00:00
|
|
|
enum {
|
2007-02-19 03:18:22 +00:00
|
|
|
APINT_BITS_PER_WORD = sizeof(uint64_t) * 8,
|
|
|
|
APINT_WORD_SIZE = sizeof(uint64_t)
|
2007-02-05 17:29:16 +00:00
|
|
|
};
|
|
|
|
|
2007-02-25 07:29:03 +00:00
|
|
|
// Fast internal constructor
|
|
|
|
APInt(uint64_t* val, uint32_t bits) : BitWidth(bits), pVal(val) { }
|
|
|
|
|
2007-02-05 17:29:16 +00:00
|
|
|
/// @returns true if the number of bits <= 64, false otherwise.
|
|
|
|
/// @brief Determine if this APInt just has one word to store value.
|
2007-02-16 22:36:51 +00:00
|
|
|
inline bool isSingleWord() const {
|
|
|
|
return BitWidth <= APINT_BITS_PER_WORD;
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
|
|
|
/// @returns the word position for the specified bit position.
|
2007-02-18 18:42:35 +00:00
|
|
|
static inline uint32_t whichWord(uint32_t bitPosition) {
|
2007-02-16 22:36:51 +00:00
|
|
|
return bitPosition / APINT_BITS_PER_WORD;
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
|
|
|
/// @returns the bit position in a word for the specified bit position
|
|
|
|
/// in APInt.
|
2007-02-18 18:42:35 +00:00
|
|
|
static inline uint32_t whichBit(uint32_t bitPosition) {
|
2007-02-16 22:36:51 +00:00
|
|
|
return bitPosition % APINT_BITS_PER_WORD;
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
|
|
|
/// @returns a uint64_t type integer with just bit position at
|
|
|
|
/// "whichBit(bitPosition)" setting, others zero.
|
2007-02-18 18:42:35 +00:00
|
|
|
static inline uint64_t maskBit(uint32_t bitPosition) {
|
2007-03-03 00:24:39 +00:00
|
|
|
return 1ULL << whichBit(bitPosition);
|
2007-02-16 22:36:51 +00:00
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-16 22:36:51 +00:00
|
|
|
/// This method is used internally to clear the to "N" bits that are not used
|
2007-02-24 09:50:13 +00:00
|
|
|
/// by the APInt. This is needed after the most significant word is assigned
|
|
|
|
/// a value to ensure that those bits are zero'd out.
|
2007-02-16 22:36:51 +00:00
|
|
|
/// @brief Clear high order bits
|
2007-02-25 19:26:01 +00:00
|
|
|
inline APInt& clearUnusedBits() {
|
|
|
|
// Compute how many bits are used in the final word
|
|
|
|
uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
|
|
|
|
if (wordBits == 0)
|
|
|
|
// If all bits are used, we want to leave the value alone. This also
|
|
|
|
// avoids the undefined behavior of >> when the shfit is the same size as
|
|
|
|
// the word size (64).
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
// Mask out the hight bits.
|
|
|
|
uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
|
2007-02-05 17:29:16 +00:00
|
|
|
if (isSingleWord())
|
2007-02-25 19:26:01 +00:00
|
|
|
VAL &= mask;
|
2007-02-05 17:29:16 +00:00
|
|
|
else
|
2007-02-25 19:26:01 +00:00
|
|
|
pVal[getNumWords() - 1] &= mask;
|
|
|
|
return *this;
|
2007-02-05 17:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns the corresponding word for the specified bit position.
|
2007-02-24 09:50:13 +00:00
|
|
|
/// @brief Get the word corresponding to a bit position
|
2007-02-18 18:42:35 +00:00
|
|
|
inline uint64_t getWord(uint32_t bitPosition) const {
|
2007-02-16 22:36:51 +00:00
|
|
|
return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-24 09:50:13 +00:00
|
|
|
/// This is used by the constructors that take string arguments.
|
|
|
|
/// @brief Converts a char array into an APInt
|
2007-02-18 18:42:35 +00:00
|
|
|
void fromString(uint32_t numBits, const char *StrStart, uint32_t slen,
|
2007-02-16 22:36:51 +00:00
|
|
|
uint8_t radix);
|
2007-02-07 05:58:38 +00:00
|
|
|
|
2007-02-24 09:50:13 +00:00
|
|
|
/// This is used by the toString method to divide by the radix. It simply
|
2007-02-25 19:26:01 +00:00
|
|
|
/// provides a more convenient form of divide for internal use since KnuthDiv
|
|
|
|
/// has specific constraints on its inputs. If those constraints are not met
|
|
|
|
/// then it provides a simpler form of divide.
|
2007-02-24 09:50:13 +00:00
|
|
|
/// @brief An internal division function for dividing APInts.
|
|
|
|
static void divide(const APInt LHS, uint32_t lhsWords,
|
|
|
|
const APInt &RHS, uint32_t rhsWords,
|
|
|
|
APInt *Quotient, APInt *Remainder);
|
|
|
|
|
2007-02-21 03:56:12 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
/// @brief debug method
|
|
|
|
void dump() const;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
2007-02-24 09:50:13 +00:00
|
|
|
/// @brief Create a new APInt of numBits width, initialized as val.
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt(uint32_t numBits, uint64_t val);
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-24 09:50:13 +00:00
|
|
|
/// Note that numWords can be smaller or larger than the corresponding bit
|
|
|
|
/// width but any extraneous bits will be dropped.
|
|
|
|
/// @brief Create a new APInt of numBits width, initialized as bigVal[].
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[]);
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// @brief Create a new APInt by translating the string represented
|
|
|
|
/// integer value.
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt(uint32_t numBits, const std::string& Val, uint8_t radix);
|
2007-02-07 05:58:38 +00:00
|
|
|
|
|
|
|
/// @brief Create a new APInt by translating the char array represented
|
|
|
|
/// integer value.
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt(uint32_t numBits, const char StrStart[], uint32_t slen, uint8_t radix);
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// @brief Copy Constructor.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt(const APInt& API);
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// @brief Destructor.
|
2007-02-05 17:29:16 +00:00
|
|
|
~APInt();
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// @brief Copy assignment operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt& operator=(const APInt& RHS);
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Assigns an integer value to the APInt.
|
|
|
|
/// @brief Assignment operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt& operator=(uint64_t RHS);
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Increments the APInt by one.
|
|
|
|
/// @brief Postfix increment operator.
|
2007-02-07 05:58:38 +00:00
|
|
|
inline const APInt operator++(int) {
|
|
|
|
APInt API(*this);
|
2007-02-15 06:36:31 +00:00
|
|
|
++(*this);
|
|
|
|
return API;
|
2007-02-07 05:58:38 +00:00
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Increments the APInt by one.
|
|
|
|
/// @brief Prefix increment operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt& operator++();
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Decrements the APInt by one.
|
|
|
|
/// @brief Postfix decrement operator.
|
2007-02-07 05:58:38 +00:00
|
|
|
inline const APInt operator--(int) {
|
|
|
|
APInt API(*this);
|
2007-02-15 06:36:31 +00:00
|
|
|
--(*this);
|
|
|
|
return API;
|
2007-02-07 05:58:38 +00:00
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Decrements the APInt by one.
|
|
|
|
/// @brief Prefix decrement operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt& operator--();
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Performs bitwise AND operation on this APInt and the given APInt& RHS,
|
|
|
|
/// assigns the result to this APInt.
|
|
|
|
/// @brief Bitwise AND assignment operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt& operator&=(const APInt& RHS);
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Performs bitwise OR operation on this APInt and the given APInt& RHS,
|
|
|
|
/// assigns the result to this APInt.
|
|
|
|
/// @brief Bitwise OR assignment operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt& operator|=(const APInt& RHS);
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Performs bitwise XOR operation on this APInt and the given APInt& RHS,
|
|
|
|
/// assigns the result to this APInt.
|
|
|
|
/// @brief Bitwise XOR assignment operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt& operator^=(const APInt& RHS);
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Performs a bitwise complement operation on this APInt.
|
|
|
|
/// @brief Bitwise complement operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt operator~() const;
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Multiplies this APInt by the given APInt& RHS and
|
|
|
|
/// assigns the result to this APInt.
|
|
|
|
/// @brief Multiplication assignment operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt& operator*=(const APInt& RHS);
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Adds this APInt by the given APInt& RHS and
|
|
|
|
/// assigns the result to this APInt.
|
|
|
|
/// @brief Addition assignment operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt& operator+=(const APInt& RHS);
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Subtracts this APInt by the given APInt &RHS and
|
|
|
|
/// assigns the result to this APInt.
|
|
|
|
/// @brief Subtraction assignment operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt& operator-=(const APInt& RHS);
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Performs bitwise AND operation on this APInt and
|
|
|
|
/// the given APInt& RHS.
|
|
|
|
/// @brief Bitwise AND operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt operator&(const APInt& RHS) const;
|
2007-03-03 05:37:23 +00:00
|
|
|
APInt And(const APInt& RHS) const {
|
|
|
|
return this->operator&(RHS);
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Performs bitwise OR operation on this APInt and the given APInt& RHS.
|
|
|
|
/// @brief Bitwise OR operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt operator|(const APInt& RHS) const;
|
2007-03-03 05:37:23 +00:00
|
|
|
APInt Or(const APInt& RHS) const {
|
|
|
|
return this->operator|(RHS);
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Performs bitwise XOR operation on this APInt and the given APInt& RHS.
|
|
|
|
/// @brief Bitwise XOR operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt operator^(const APInt& RHS) const;
|
2007-03-03 05:37:23 +00:00
|
|
|
APInt Xor(const APInt& RHS) const {
|
|
|
|
return this->operator^(RHS);
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Performs logical negation operation on this APInt.
|
|
|
|
/// @brief Logical negation operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
bool operator !() const;
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Multiplies this APInt by the given APInt& RHS.
|
|
|
|
/// @brief Multiplication operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt operator*(const APInt& RHS) const;
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Adds this APInt by the given APInt& RHS.
|
|
|
|
/// @brief Addition operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt operator+(const APInt& RHS) const;
|
2007-02-28 17:33:36 +00:00
|
|
|
APInt operator+(uint64_t RHS) const {
|
|
|
|
return (*this) + APInt(BitWidth, RHS);
|
|
|
|
}
|
|
|
|
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Subtracts this APInt by the given APInt& RHS
|
|
|
|
/// @brief Subtraction operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
APInt operator-(const APInt& RHS) const;
|
2007-02-28 17:33:36 +00:00
|
|
|
APInt operator-(uint64_t RHS) const {
|
|
|
|
return (*this) - APInt(BitWidth, RHS);
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-16 22:36:51 +00:00
|
|
|
/// @brief Unary negation operator
|
2007-02-08 14:35:19 +00:00
|
|
|
inline APInt operator-() const {
|
2007-02-17 00:18:01 +00:00
|
|
|
return APInt(BitWidth, 0) - (*this);
|
2007-02-08 14:35:19 +00:00
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
|
|
|
/// @brief Array-indexing support.
|
2007-02-18 18:42:35 +00:00
|
|
|
bool operator[](uint32_t bitPosition) const;
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Compare this APInt with the given APInt& RHS
|
2007-02-05 17:29:16 +00:00
|
|
|
/// for the validity of the equality relationship.
|
2007-02-06 05:59:47 +00:00
|
|
|
/// @brief Equality operator.
|
2007-02-05 17:29:16 +00:00
|
|
|
bool operator==(const APInt& RHS) const;
|
|
|
|
|
2007-02-07 05:58:38 +00:00
|
|
|
/// Compare this APInt with the given uint64_t value
|
|
|
|
/// for the validity of the equality relationship.
|
|
|
|
/// @brief Equality operator.
|
|
|
|
bool operator==(uint64_t Val) const;
|
|
|
|
|
2007-02-06 05:59:47 +00:00
|
|
|
/// Compare this APInt with the given APInt& RHS
|
2007-02-05 17:29:16 +00:00
|
|
|
/// for the validity of the inequality relationship.
|
2007-02-06 05:59:47 +00:00
|
|
|
/// @brief Inequality operator.
|
2007-02-07 05:58:38 +00:00
|
|
|
inline bool operator!=(const APInt& RHS) const {
|
|
|
|
return !((*this) == RHS);
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-07 05:58:38 +00:00
|
|
|
/// Compare this APInt with the given uint64_t value
|
|
|
|
/// for the validity of the inequality relationship.
|
|
|
|
/// @brief Inequality operator.
|
|
|
|
inline bool operator!=(uint64_t Val) const {
|
|
|
|
return !((*this) == Val);
|
|
|
|
}
|
|
|
|
|
2007-02-16 22:36:51 +00:00
|
|
|
/// @brief Equality comparison
|
|
|
|
bool eq(const APInt &RHS) const {
|
|
|
|
return (*this) == RHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Inequality comparison
|
|
|
|
bool ne(const APInt &RHS) const {
|
|
|
|
return !((*this) == RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Unsigned less than comparison
|
|
|
|
bool ult(const APInt& RHS) const;
|
|
|
|
|
|
|
|
/// @brief Signed less than comparison
|
|
|
|
bool slt(const APInt& RHS) const;
|
|
|
|
|
|
|
|
/// @brief Unsigned less or equal comparison
|
|
|
|
bool ule(const APInt& RHS) const {
|
|
|
|
return ult(RHS) || eq(RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Signed less or equal comparison
|
|
|
|
bool sle(const APInt& RHS) const {
|
|
|
|
return slt(RHS) || eq(RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Unsigned greather than comparison
|
|
|
|
bool ugt(const APInt& RHS) const {
|
|
|
|
return !ult(RHS) && !eq(RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Signed greather than comparison
|
|
|
|
bool sgt(const APInt& RHS) const {
|
|
|
|
return !slt(RHS) && !eq(RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Unsigned greater or equal comparison
|
|
|
|
bool uge(const APInt& RHS) const {
|
|
|
|
return !ult(RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Signed greather or equal comparison
|
|
|
|
bool sge(const APInt& RHS) const {
|
|
|
|
return !slt(RHS);
|
|
|
|
}
|
|
|
|
|
2007-02-26 01:20:59 +00:00
|
|
|
/// This just tests the high bit of this APInt to determine if it is negative.
|
|
|
|
/// @returns true if this APInt is negative, false otherwise
|
|
|
|
/// @brief Determine sign of this APInt.
|
2007-02-26 07:45:40 +00:00
|
|
|
bool isNegative() const {
|
2007-02-26 01:20:59 +00:00
|
|
|
return (*this)[BitWidth - 1];
|
|
|
|
}
|
|
|
|
|
2007-03-03 00:24:39 +00:00
|
|
|
/// This just tests the high bit of the APInt to determine if the value is
|
|
|
|
/// positove or not.
|
|
|
|
/// @brief Determine if this APInt Value is positive.
|
|
|
|
bool isPositive() const {
|
|
|
|
return !isNegative();
|
|
|
|
}
|
|
|
|
|
2007-02-16 22:36:51 +00:00
|
|
|
/// Arithmetic right-shift this APInt by shiftAmt.
|
|
|
|
/// @brief Arithmetic right-shift function.
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt ashr(uint32_t shiftAmt) const;
|
2007-02-16 22:36:51 +00:00
|
|
|
|
|
|
|
/// Logical right-shift this APInt by shiftAmt.
|
|
|
|
/// @brief Logical right-shift function.
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt lshr(uint32_t shiftAmt) const;
|
2007-02-16 22:36:51 +00:00
|
|
|
|
|
|
|
/// Left-shift this APInt by shiftAmt.
|
|
|
|
/// @brief Left-shift function.
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt shl(uint32_t shiftAmt) const;
|
2007-02-16 22:36:51 +00:00
|
|
|
|
2007-03-11 07:16:10 +00:00
|
|
|
/// Left-shift this APInt by shiftAmt and
|
|
|
|
/// assigns the result to this APInt.
|
|
|
|
/// @brief Lef-shift assignment function.
|
|
|
|
inline APInt& operator<<=(uint32_t shiftAmt) {
|
|
|
|
*this = shl(shiftAmt);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2007-02-16 22:36:51 +00:00
|
|
|
/// Signed divide this APInt by APInt RHS.
|
|
|
|
/// @brief Signed division function for APInt.
|
|
|
|
inline APInt sdiv(const APInt& RHS) const {
|
2007-02-27 20:24:31 +00:00
|
|
|
bool isNegativeLHS = isNegative();
|
|
|
|
bool isNegativeRHS = RHS.isNegative();
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt Result = APIntOps::udiv(
|
2007-02-16 22:36:51 +00:00
|
|
|
isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
|
2007-02-18 18:42:35 +00:00
|
|
|
return isNegativeLHS != isNegativeRHS ? -Result : Result;
|
2007-02-16 22:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unsigned divide this APInt by APInt RHS.
|
|
|
|
/// @brief Unsigned division function for APInt.
|
|
|
|
APInt udiv(const APInt& RHS) const;
|
|
|
|
|
|
|
|
/// Signed remainder operation on APInt.
|
|
|
|
/// @brief Function for signed remainder operation.
|
|
|
|
inline APInt srem(const APInt& RHS) const {
|
2007-02-27 20:24:31 +00:00
|
|
|
bool isNegativeLHS = isNegative();
|
|
|
|
bool isNegativeRHS = RHS.isNegative();
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt Result = APIntOps::urem(
|
2007-02-16 22:36:51 +00:00
|
|
|
isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
|
2007-02-18 18:42:35 +00:00
|
|
|
return isNegativeLHS ? -Result : Result;
|
2007-02-16 22:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unsigned remainder operation on APInt.
|
|
|
|
/// @brief Function for unsigned remainder operation.
|
|
|
|
APInt urem(const APInt& RHS) const;
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
/// @brief Truncate to new width.
|
2007-02-28 17:33:36 +00:00
|
|
|
APInt &trunc(uint32_t width);
|
2007-02-16 22:36:51 +00:00
|
|
|
|
|
|
|
/// This operation sign extends the APInt to a new width. If the high order
|
|
|
|
/// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
|
|
|
|
/// It is an error to specify a width that is less than or equal to the
|
|
|
|
/// current width.
|
|
|
|
/// @brief Sign extend to a new width.
|
2007-02-28 17:33:36 +00:00
|
|
|
APInt &sext(uint32_t width);
|
2007-02-16 22:36:51 +00:00
|
|
|
|
|
|
|
/// This operation zero extends the APInt to a new width. Thie high order bits
|
|
|
|
/// are filled with 0 bits. It is an error to specify a width that is less
|
|
|
|
/// than or equal to the current width.
|
|
|
|
/// @brief Zero extend to a new width.
|
2007-02-28 17:33:36 +00:00
|
|
|
APInt &zext(uint32_t width);
|
2007-02-16 22:36:51 +00:00
|
|
|
|
2007-03-01 17:15:32 +00:00
|
|
|
/// Make this APInt have the bit width given by \p width. The value is sign
|
|
|
|
/// extended, truncated, or left alone to make it that width.
|
|
|
|
/// @brief Sign extend or truncate to width
|
|
|
|
APInt &sextOrTrunc(uint32_t width);
|
|
|
|
|
|
|
|
/// Make this APInt have the bit width given by \p width. The value is zero
|
|
|
|
/// extended, truncated, or left alone to make it that width.
|
|
|
|
/// @brief Zero extend or truncate to width
|
|
|
|
APInt &zextOrTrunc(uint32_t width);
|
|
|
|
|
2007-02-16 22:36:51 +00:00
|
|
|
/// @brief Set every bit to 1.
|
|
|
|
APInt& set();
|
|
|
|
|
|
|
|
/// Set the given bit to 1 whose position is given as "bitPosition".
|
|
|
|
/// @brief Set a given bit to 1.
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt& set(uint32_t bitPosition);
|
2007-02-16 22:36:51 +00:00
|
|
|
|
|
|
|
/// @brief Set every bit to 0.
|
|
|
|
APInt& clear();
|
|
|
|
|
|
|
|
/// Set the given bit to 0 whose position is given as "bitPosition".
|
|
|
|
/// @brief Set a given bit to 0.
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt& clear(uint32_t bitPosition);
|
2007-02-16 22:36:51 +00:00
|
|
|
|
|
|
|
/// @brief Toggle every bit to its opposite value.
|
|
|
|
APInt& flip();
|
|
|
|
|
|
|
|
/// Toggle a given bit to its opposite value whose position is given
|
|
|
|
/// as "bitPosition".
|
|
|
|
/// @brief Toggles a given bit to its opposite value.
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt& flip(uint32_t bitPosition);
|
2007-02-16 22:36:51 +00:00
|
|
|
|
2007-03-11 07:16:10 +00:00
|
|
|
inline void setWordToValue(uint32_t idx, uint64_t Val) {
|
|
|
|
assert(idx < getNumWords() && "Invalid word array index");
|
|
|
|
if (isSingleWord())
|
|
|
|
VAL = Val;
|
|
|
|
else
|
|
|
|
pVal[idx] = Val;
|
|
|
|
}
|
|
|
|
|
2007-02-16 22:36:51 +00:00
|
|
|
/// This function returns the number of active bits which is defined as the
|
|
|
|
/// bit width minus the number of leading zeros. This is used in several
|
|
|
|
/// computations to see how "wide" the value is.
|
|
|
|
/// @brief Compute the number of active bits in the value
|
2007-02-18 18:42:35 +00:00
|
|
|
inline uint32_t getActiveBits() const {
|
2007-02-21 03:56:12 +00:00
|
|
|
return BitWidth - countLeadingZeros();
|
2007-02-16 22:36:51 +00:00
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-27 23:47:33 +00:00
|
|
|
/// This function returns the number of active words in the value of this
|
|
|
|
/// APInt. This is used in conjunction with getActiveData to extract the raw
|
|
|
|
/// value of the APInt.
|
|
|
|
inline uint32_t getActiveWords() const {
|
2007-02-28 02:20:49 +00:00
|
|
|
return whichWord(getActiveBits()-1) + 1;
|
2007-02-27 23:47:33 +00:00
|
|
|
}
|
|
|
|
|
2007-03-03 06:17:23 +00:00
|
|
|
/// Here one word's bitwidth equals to that of uint64_t.
|
|
|
|
/// @returns the number of words to hold the integer value of this APInt.
|
|
|
|
/// @brief Get the number of words.
|
|
|
|
inline uint32_t getNumWords() const {
|
|
|
|
return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
|
|
|
|
}
|
|
|
|
|
2007-02-27 23:47:33 +00:00
|
|
|
/// This function returns a pointer to the internal storage of the APInt.
|
|
|
|
/// This is useful for writing out the APInt in binary form without any
|
|
|
|
/// conversions.
|
|
|
|
inline const uint64_t* getRawData() const {
|
|
|
|
if (isSingleWord())
|
|
|
|
return &VAL;
|
|
|
|
return &pVal[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the minimum bit width for this APInt while considering it to be
|
|
|
|
/// a signed (and probably negative) value. If the value is not negative,
|
|
|
|
/// this function returns the same value as getActiveBits(). Otherwise, it
|
|
|
|
/// returns the smallest bit width that will retain the negative value. For
|
|
|
|
/// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
|
|
|
|
/// for -1, this function will always return 1.
|
|
|
|
/// @brief Get the minimum bit size for this signed APInt
|
2007-02-27 21:59:26 +00:00
|
|
|
inline uint32_t getMinSignedBits() const {
|
|
|
|
if (isNegative())
|
|
|
|
return BitWidth - countLeadingOnes() + 1;
|
|
|
|
return getActiveBits();
|
|
|
|
}
|
|
|
|
|
2007-02-26 20:57:12 +00:00
|
|
|
/// This method attempts to return the value of this APInt as a zero extended
|
|
|
|
/// uint64_t. The bitwidth must be <= 64 or the value must fit within a
|
|
|
|
/// uint64_t. Otherwise an assertion will result.
|
|
|
|
/// @brief Get zero extended value
|
|
|
|
inline uint64_t getZExtValue() const {
|
2007-02-05 17:29:16 +00:00
|
|
|
if (isSingleWord())
|
2007-02-26 20:57:12 +00:00
|
|
|
return VAL;
|
|
|
|
assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
|
|
|
|
return pVal[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method attempts to return the value of this APInt as a sign extended
|
|
|
|
/// int64_t. The bit width must be <= 64 or the value must fit within an
|
|
|
|
/// int64_t. Otherwise an assertion will result.
|
|
|
|
/// @brief Get sign extended value
|
|
|
|
inline int64_t getSExtValue() const {
|
|
|
|
if (isSingleWord())
|
|
|
|
return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >>
|
|
|
|
(APINT_BITS_PER_WORD - BitWidth);
|
|
|
|
assert(getActiveBits() <= 64 && "Too many bits for int64_t");
|
2007-02-26 21:06:05 +00:00
|
|
|
return int64_t(pVal[0]);
|
2007-02-05 17:29:16 +00:00
|
|
|
}
|
|
|
|
|
2007-02-27 20:24:31 +00:00
|
|
|
/// @brief Gets maximum unsigned value of APInt for specific bit width.
|
|
|
|
static APInt getMaxValue(uint32_t numBits) {
|
|
|
|
return APInt(numBits, 0).set();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Gets maximum signed value of APInt for a specific bit width.
|
|
|
|
static APInt getSignedMaxValue(uint32_t numBits) {
|
|
|
|
return APInt(numBits, 0).set().clear(numBits - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Gets minimum unsigned value of APInt for a specific bit width.
|
|
|
|
static APInt getMinValue(uint32_t numBits) {
|
|
|
|
return APInt(numBits, 0);
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-27 20:24:31 +00:00
|
|
|
/// @brief Gets minimum signed value of APInt for a specific bit width.
|
|
|
|
static APInt getSignedMinValue(uint32_t numBits) {
|
|
|
|
return APInt(numBits, 0).set(numBits - 1);
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-03-11 07:16:10 +00:00
|
|
|
/// getSignBit - This is just a wrapper function of getSignedMinValue(), and
|
|
|
|
/// it helps code readability when we want to get a SignBit.
|
|
|
|
/// @brief Get the SignBit for a specific bit width.
|
|
|
|
inline static APInt getSignBit(uint32_t BitWidth) {
|
|
|
|
return getSignedMinValue(BitWidth);
|
|
|
|
}
|
|
|
|
|
2007-02-05 17:29:16 +00:00
|
|
|
/// @returns the all-ones value for an APInt of the specified bit-width.
|
|
|
|
/// @brief Get the all-ones value.
|
2007-02-27 20:24:31 +00:00
|
|
|
static APInt getAllOnesValue(uint32_t numBits) {
|
|
|
|
return APInt(numBits, 0).set();
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-13 22:41:58 +00:00
|
|
|
/// @returns the '0' value for an APInt of the specified bit-width.
|
|
|
|
/// @brief Get the '0' value.
|
2007-02-27 20:24:31 +00:00
|
|
|
static APInt getNullValue(uint32_t numBits) {
|
|
|
|
return APInt(numBits, 0);
|
|
|
|
}
|
2007-02-13 22:41:58 +00:00
|
|
|
|
2007-02-26 20:57:12 +00:00
|
|
|
/// The hash value is computed as the sum of the words and the bit width.
|
|
|
|
/// @returns A hash value computed from the sum of the APInt words.
|
|
|
|
/// @brief Get a hash value based on this APInt
|
|
|
|
uint64_t getHashValue() const;
|
|
|
|
|
2007-02-16 22:36:51 +00:00
|
|
|
/// This converts the APInt to a boolean valy as a test against zero.
|
|
|
|
/// @brief Boolean conversion function.
|
|
|
|
inline bool getBoolValue() const {
|
|
|
|
return countLeadingZeros() != BitWidth;
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-26 17:50:32 +00:00
|
|
|
/// This checks to see if the value has all bits of the APInt are set or not.
|
|
|
|
/// @brief Determine if all bits are set
|
|
|
|
inline bool isAllOnesValue() const {
|
|
|
|
return countPopulation() == BitWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This checks to see if the value of this APInt is the maximum unsigned
|
|
|
|
/// value for the APInt's bit width.
|
|
|
|
/// @brief Determine if this is the largest unsigned value.
|
|
|
|
bool isMaxValue() const {
|
|
|
|
return countPopulation() == BitWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This checks to see if the value of this APInt is the maximum signed
|
|
|
|
/// value for the APInt's bit width.
|
|
|
|
/// @brief Determine if this is the largest signed value.
|
|
|
|
bool isMaxSignedValue() const {
|
|
|
|
return BitWidth == 1 ? VAL == 0 :
|
|
|
|
!isNegative() && countPopulation() == BitWidth - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This checks to see if the value of this APInt is the minimum signed
|
|
|
|
/// value for the APInt's bit width.
|
|
|
|
/// @brief Determine if this is the smallest unsigned value.
|
|
|
|
bool isMinValue() const {
|
|
|
|
return countPopulation() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This checks to see if the value of this APInt is the minimum signed
|
|
|
|
/// value for the APInt's bit width.
|
|
|
|
/// @brief Determine if this is the smallest signed value.
|
|
|
|
bool isMinSignedValue() const {
|
|
|
|
return BitWidth == 1 ? VAL == 1 :
|
|
|
|
isNegative() && countPopulation() == 1;
|
|
|
|
}
|
|
|
|
|
2007-02-27 20:24:31 +00:00
|
|
|
/// This is used internally to convert an APInt to a string.
|
|
|
|
/// @brief Converts an APInt to a std::string
|
|
|
|
std::string toString(uint8_t radix, bool wantSigned) const;
|
|
|
|
|
|
|
|
/// Considers the APInt to be unsigned and converts it into a string in the
|
|
|
|
/// radix given. The radix can be 2, 8, 10 or 16.
|
|
|
|
/// @returns a character interpretation of the APInt
|
|
|
|
/// @brief Convert unsigned APInt to string representation.
|
|
|
|
inline std::string toString(uint8_t radix = 10) const {
|
|
|
|
return toString(radix, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Considers the APInt to be unsigned and converts it into a string in the
|
|
|
|
/// radix given. The radix can be 2, 8, 10 or 16.
|
|
|
|
/// @returns a character interpretation of the APInt
|
|
|
|
/// @brief Convert unsigned APInt to string representation.
|
|
|
|
inline std::string toStringSigned(uint8_t radix = 10) const {
|
|
|
|
return toString(radix, true);
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-16 22:36:51 +00:00
|
|
|
/// Get an APInt with the same BitWidth as this APInt, just zero mask
|
2007-02-06 05:59:47 +00:00
|
|
|
/// the low bits and right shift to the least significant bit.
|
2007-02-05 17:29:16 +00:00
|
|
|
/// @returns the high "numBits" bits of this APInt.
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt getHiBits(uint32_t numBits) const;
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-16 22:36:51 +00:00
|
|
|
/// Get an APInt with the same BitWidth as this APInt, just zero mask
|
2007-02-06 05:59:47 +00:00
|
|
|
/// the high bits.
|
2007-02-05 17:29:16 +00:00
|
|
|
/// @returns the low "numBits" bits of this APInt.
|
2007-02-18 18:42:35 +00:00
|
|
|
APInt getLoBits(uint32_t numBits) const;
|
2007-02-05 17:29:16 +00:00
|
|
|
|
|
|
|
/// @returns true if the argument APInt value is a power of two > 0.
|
2007-02-17 00:18:01 +00:00
|
|
|
bool isPowerOf2() const;
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-25 19:26:01 +00:00
|
|
|
/// countLeadingZeros - This function is an APInt version of the
|
|
|
|
/// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
|
|
|
|
/// of zeros from the most significant bit to the first one bit.
|
|
|
|
/// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
|
2007-02-05 17:29:16 +00:00
|
|
|
/// @returns the number of zeros from the most significant bit to the first
|
|
|
|
/// one bits.
|
2007-02-27 21:59:26 +00:00
|
|
|
/// @brief Count the number of leading one bits.
|
2007-02-18 18:42:35 +00:00
|
|
|
uint32_t countLeadingZeros() const;
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-27 21:59:26 +00:00
|
|
|
/// countLeadingOnes - This function counts the number of contiguous 1 bits
|
|
|
|
/// in the high order bits. The count stops when the first 0 bit is reached.
|
|
|
|
/// @returns 0 if the high order bit is not set
|
|
|
|
/// @returns the number of 1 bits from the most significant to the least
|
|
|
|
/// @brief Count the number of leading one bits.
|
|
|
|
uint32_t countLeadingOnes() const;
|
|
|
|
|
2007-02-25 19:26:01 +00:00
|
|
|
/// countTrailingZeros - This function is an APInt version of the
|
|
|
|
/// countTrailingZoers_{32,64} functions in MathExtras.h. It counts
|
|
|
|
/// the number of zeros from the least significant bit to the first one bit.
|
|
|
|
/// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
|
2007-02-05 17:29:16 +00:00
|
|
|
/// @returns the number of zeros from the least significant bit to the first
|
|
|
|
/// one bit.
|
2007-02-25 19:26:01 +00:00
|
|
|
/// @brief Count the number of trailing zero bits.
|
2007-02-18 18:42:35 +00:00
|
|
|
uint32_t countTrailingZeros() const;
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-25 19:26:01 +00:00
|
|
|
/// countPopulation - This function is an APInt version of the
|
|
|
|
/// countPopulation_{32,64} functions in MathExtras.h. It counts the number
|
|
|
|
/// of 1 bits in the APInt value.
|
|
|
|
/// @returns 0 if the value is zero.
|
2007-02-05 17:29:16 +00:00
|
|
|
/// @returns the number of set bits.
|
2007-02-25 19:26:01 +00:00
|
|
|
/// @brief Count the number of bits set.
|
2007-02-18 18:42:35 +00:00
|
|
|
uint32_t countPopulation() const;
|
2007-02-05 17:29:16 +00:00
|
|
|
|
|
|
|
/// @returns the total number of bits.
|
2007-02-18 18:42:35 +00:00
|
|
|
inline uint32_t getBitWidth() const {
|
2007-02-16 22:36:51 +00:00
|
|
|
return BitWidth;
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-09 07:48:24 +00:00
|
|
|
/// @brief Check if this APInt has a N-bits integer value.
|
2007-02-18 18:42:35 +00:00
|
|
|
inline bool isIntN(uint32_t N) const {
|
2007-02-15 06:36:31 +00:00
|
|
|
assert(N && "N == 0 ???");
|
2007-02-09 07:48:24 +00:00
|
|
|
if (isSingleWord()) {
|
2007-02-15 06:36:31 +00:00
|
|
|
return VAL == (VAL & (~0ULL >> (64 - N)));
|
2007-02-09 07:48:24 +00:00
|
|
|
} else {
|
2007-02-16 22:36:51 +00:00
|
|
|
APInt Tmp(N, getNumWords(), pVal);
|
2007-02-09 07:48:24 +00:00
|
|
|
return Tmp == (*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns a byte-swapped representation of this APInt Value.
|
2007-02-16 22:36:51 +00:00
|
|
|
APInt byteSwap() const;
|
2007-02-09 07:48:24 +00:00
|
|
|
|
|
|
|
/// @returns the floor log base 2 of this APInt.
|
2007-02-18 18:42:35 +00:00
|
|
|
inline uint32_t logBase2() const {
|
2007-03-05 16:42:58 +00:00
|
|
|
return BitWidth - 1 - countLeadingZeros();
|
2007-02-09 07:48:24 +00:00
|
|
|
}
|
|
|
|
|
2007-02-12 20:02:55 +00:00
|
|
|
/// @brief Converts this APInt to a double value.
|
2007-02-27 20:24:31 +00:00
|
|
|
double roundToDouble(bool isSigned) const;
|
|
|
|
|
|
|
|
/// @brief Converts this unsigned APInt to a double value.
|
|
|
|
double roundToDouble() const {
|
|
|
|
return roundToDouble(false);
|
|
|
|
}
|
2007-02-09 07:48:24 +00:00
|
|
|
|
2007-02-27 20:24:31 +00:00
|
|
|
/// @brief Converts this signed APInt to a double value.
|
|
|
|
double signedRoundToDouble() const {
|
|
|
|
return roundToDouble(true);
|
|
|
|
}
|
2007-03-01 05:39:56 +00:00
|
|
|
|
2007-03-01 20:06:51 +00:00
|
|
|
/// The conversion does not do a translation from integer to double, it just
|
|
|
|
/// re-interprets the bits as a double. Note that it is valid to do this on
|
|
|
|
/// any bit width. Exactly 64 bits will be translated.
|
|
|
|
/// @brief Converts APInt bits to a double
|
|
|
|
double bitsToDouble() const {
|
|
|
|
union {
|
|
|
|
uint64_t I;
|
|
|
|
double D;
|
|
|
|
} T;
|
|
|
|
T.I = (isSingleWord() ? VAL : pVal[0]);
|
|
|
|
return T.D;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The conversion does not do a translation from integer to float, it just
|
|
|
|
/// re-interprets the bits as a float. Note that it is valid to do this on
|
|
|
|
/// any bit width. Exactly 32 bits will be translated.
|
|
|
|
/// @brief Converts APInt bits to a double
|
|
|
|
float bitsToFloat() const {
|
|
|
|
union {
|
|
|
|
uint32_t I;
|
|
|
|
float F;
|
|
|
|
} T;
|
|
|
|
T.I = uint32_t((isSingleWord() ? VAL : pVal[0]));
|
|
|
|
return T.F;
|
|
|
|
}
|
|
|
|
|
2007-03-01 20:39:01 +00:00
|
|
|
/// The conversion does not do a translation from double to integer, it just
|
|
|
|
/// re-interprets the bits of the double. Note that it is valid to do this on
|
|
|
|
/// any bit width but bits from V may get truncated.
|
|
|
|
/// @brief Converts a double to APInt bits.
|
|
|
|
APInt& doubleToBits(double V) {
|
|
|
|
union {
|
|
|
|
uint64_t I;
|
|
|
|
double D;
|
|
|
|
} T;
|
|
|
|
T.D = V;
|
|
|
|
if (isSingleWord())
|
|
|
|
VAL = T.I;
|
|
|
|
else
|
|
|
|
pVal[0] = T.I;
|
|
|
|
return clearUnusedBits();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The conversion does not do a translation from float to integer, it just
|
|
|
|
/// re-interprets the bits of the float. Note that it is valid to do this on
|
|
|
|
/// any bit width but bits from V may get truncated.
|
|
|
|
/// @brief Converts a float to APInt bits.
|
|
|
|
APInt& floatToBits(float V) {
|
|
|
|
union {
|
|
|
|
uint32_t I;
|
|
|
|
float F;
|
|
|
|
} T;
|
|
|
|
T.F = V;
|
|
|
|
if (isSingleWord())
|
|
|
|
VAL = T.I;
|
|
|
|
else
|
|
|
|
pVal[0] = T.I;
|
|
|
|
return clearUnusedBits();
|
|
|
|
}
|
|
|
|
|
2007-03-01 05:39:56 +00:00
|
|
|
/// @brief Compute the square root
|
|
|
|
APInt sqrt() const;
|
2007-03-01 23:37:09 +00:00
|
|
|
|
|
|
|
/// If *this is < 0 then return -(*this), otherwise *this;
|
|
|
|
/// @brief Get the absolute value;
|
|
|
|
APInt abs() const {
|
|
|
|
if (isNegative())
|
|
|
|
return -(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
};
|
|
|
|
|
2007-02-27 23:47:33 +00:00
|
|
|
inline bool operator==(uint64_t V1, const APInt& V2) {
|
|
|
|
return V2 == V1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(uint64_t V1, const APInt& V2) {
|
|
|
|
return V2 != V1;
|
|
|
|
}
|
|
|
|
|
2007-02-08 14:35:19 +00:00
|
|
|
namespace APIntOps {
|
|
|
|
|
2007-02-28 17:33:36 +00:00
|
|
|
/// @brief Determine the smaller of two APInts considered to be signed.
|
|
|
|
inline APInt smin(const APInt &A, const APInt &B) {
|
|
|
|
return A.slt(B) ? A : B;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Determine the larger of two APInts considered to be signed.
|
|
|
|
inline APInt smax(const APInt &A, const APInt &B) {
|
|
|
|
return A.sgt(B) ? A : B;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Determine the smaller of two APInts considered to be signed.
|
|
|
|
inline APInt umin(const APInt &A, const APInt &B) {
|
|
|
|
return A.ult(B) ? A : B;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Determine the larger of two APInts considered to be unsigned.
|
|
|
|
inline APInt umax(const APInt &A, const APInt &B) {
|
|
|
|
return A.ugt(B) ? A : B;
|
|
|
|
}
|
|
|
|
|
2007-02-05 17:29:16 +00:00
|
|
|
/// @brief Check if the specified APInt has a N-bits integer value.
|
2007-02-18 18:42:35 +00:00
|
|
|
inline bool isIntN(uint32_t N, const APInt& APIVal) {
|
2007-02-16 22:36:51 +00:00
|
|
|
return APIVal.isIntN(N);
|
2007-02-05 17:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns true if the argument APInt value is a sequence of ones
|
|
|
|
/// starting at the least significant bit with the remainder zero.
|
2007-02-18 18:42:35 +00:00
|
|
|
inline const bool isMask(uint32_t numBits, const APInt& APIVal) {
|
2007-02-16 22:36:51 +00:00
|
|
|
return APIVal.getBoolValue() && ((APIVal + APInt(numBits,1)) & APIVal) == 0;
|
2007-02-05 17:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns true if the argument APInt value contains a sequence of ones
|
|
|
|
/// with the remainder zero.
|
2007-02-18 18:42:35 +00:00
|
|
|
inline const bool isShiftedMask(uint32_t numBits, const APInt& APIVal) {
|
2007-02-16 22:36:51 +00:00
|
|
|
return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
|
2007-02-05 17:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns a byte-swapped representation of the specified APInt Value.
|
2007-02-16 22:36:51 +00:00
|
|
|
inline APInt byteSwap(const APInt& APIVal) {
|
|
|
|
return APIVal.byteSwap();
|
2007-02-09 07:48:24 +00:00
|
|
|
}
|
2007-02-05 17:29:16 +00:00
|
|
|
|
|
|
|
/// @returns the floor log base 2 of the specified APInt value.
|
2007-02-18 18:42:35 +00:00
|
|
|
inline uint32_t logBase2(const APInt& APIVal) {
|
2007-02-16 22:36:51 +00:00
|
|
|
return APIVal.logBase2();
|
2007-02-05 17:29:16 +00:00
|
|
|
}
|
|
|
|
|
2007-02-25 19:26:01 +00:00
|
|
|
/// GreatestCommonDivisor - This function returns the greatest common
|
|
|
|
/// divisor of the two APInt values using Enclid's algorithm.
|
|
|
|
/// @returns the greatest common divisor of Val1 and Val2
|
|
|
|
/// @brief Compute GCD of two APInt values.
|
|
|
|
APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
|
2007-02-05 17:29:16 +00:00
|
|
|
|
2007-02-27 20:24:31 +00:00
|
|
|
/// Treats the APInt as an unsigned value for conversion purposes.
|
|
|
|
/// @brief Converts the given APInt to a double value.
|
|
|
|
inline double RoundAPIntToDouble(const APInt& APIVal) {
|
|
|
|
return APIVal.roundToDouble();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Treats the APInt as a signed value for conversion purposes.
|
2007-02-12 20:02:55 +00:00
|
|
|
/// @brief Converts the given APInt to a double value.
|
2007-02-27 20:24:31 +00:00
|
|
|
inline double RoundSignedAPIntToDouble(const APInt& APIVal) {
|
|
|
|
return APIVal.signedRoundToDouble();
|
2007-02-12 20:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Converts the given APInt to a float vlalue.
|
2007-02-16 22:36:51 +00:00
|
|
|
inline float RoundAPIntToFloat(const APInt& APIVal) {
|
|
|
|
return float(RoundAPIntToDouble(APIVal));
|
2007-02-12 20:02:55 +00:00
|
|
|
}
|
|
|
|
|
2007-03-03 08:34:02 +00:00
|
|
|
/// Treast the APInt as a signed value for conversion purposes.
|
|
|
|
/// @brief Converts the given APInt to a float value.
|
|
|
|
inline float RoundSignedAPIntToFloat(const APInt& APIVal) {
|
|
|
|
return float(APIVal.signedRoundToDouble());
|
|
|
|
}
|
|
|
|
|
2007-02-25 19:26:01 +00:00
|
|
|
/// RoundDoubleToAPInt - This function convert a double value to an APInt value.
|
2007-02-12 20:02:55 +00:00
|
|
|
/// @brief Converts the given double value into a APInt.
|
2007-03-03 08:34:02 +00:00
|
|
|
APInt RoundDoubleToAPInt(double Double, uint32_t width);
|
2007-02-12 20:02:55 +00:00
|
|
|
|
2007-02-25 19:26:01 +00:00
|
|
|
/// RoundFloatToAPInt - Converts a float value into an APInt value.
|
|
|
|
/// @brief Converts a float value into a APInt.
|
2007-03-03 08:34:02 +00:00
|
|
|
inline APInt RoundFloatToAPInt(float Float, uint32_t width) {
|
|
|
|
return RoundDoubleToAPInt(double(Float), width);
|
2007-02-12 20:02:55 +00:00
|
|
|
}
|
|
|
|
|
2007-02-08 14:35:19 +00:00
|
|
|
/// Arithmetic right-shift the APInt by shiftAmt.
|
|
|
|
/// @brief Arithmetic right-shift function.
|
2007-02-18 18:42:35 +00:00
|
|
|
inline APInt ashr(const APInt& LHS, uint32_t shiftAmt) {
|
2007-02-16 22:36:51 +00:00
|
|
|
return LHS.ashr(shiftAmt);
|
2007-02-09 07:48:24 +00:00
|
|
|
}
|
2007-02-08 14:35:19 +00:00
|
|
|
|
|
|
|
/// Logical right-shift the APInt by shiftAmt.
|
|
|
|
/// @brief Logical right-shift function.
|
2007-02-18 18:42:35 +00:00
|
|
|
inline APInt lshr(const APInt& LHS, uint32_t shiftAmt) {
|
2007-02-16 22:36:51 +00:00
|
|
|
return LHS.lshr(shiftAmt);
|
2007-02-09 07:48:24 +00:00
|
|
|
}
|
2007-02-08 14:35:19 +00:00
|
|
|
|
|
|
|
/// Left-shift the APInt by shiftAmt.
|
|
|
|
/// @brief Left-shift function.
|
2007-02-18 18:42:35 +00:00
|
|
|
inline APInt shl(const APInt& LHS, uint32_t shiftAmt) {
|
2007-02-16 22:36:51 +00:00
|
|
|
return LHS.shl(shiftAmt);
|
2007-02-09 07:48:24 +00:00
|
|
|
}
|
2007-02-08 14:35:19 +00:00
|
|
|
|
|
|
|
/// Signed divide APInt LHS by APInt RHS.
|
|
|
|
/// @brief Signed division function for APInt.
|
2007-02-16 22:36:51 +00:00
|
|
|
inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
|
|
|
|
return LHS.sdiv(RHS);
|
2007-02-08 14:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unsigned divide APInt LHS by APInt RHS.
|
|
|
|
/// @brief Unsigned division function for APInt.
|
2007-02-16 22:36:51 +00:00
|
|
|
inline APInt udiv(const APInt& LHS, const APInt& RHS) {
|
|
|
|
return LHS.udiv(RHS);
|
2007-02-09 07:48:24 +00:00
|
|
|
}
|
2007-02-08 14:35:19 +00:00
|
|
|
|
|
|
|
/// Signed remainder operation on APInt.
|
|
|
|
/// @brief Function for signed remainder operation.
|
2007-02-16 22:36:51 +00:00
|
|
|
inline APInt srem(const APInt& LHS, const APInt& RHS) {
|
|
|
|
return LHS.srem(RHS);
|
2007-02-08 14:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unsigned remainder operation on APInt.
|
|
|
|
/// @brief Function for unsigned remainder operation.
|
2007-02-16 22:36:51 +00:00
|
|
|
inline APInt urem(const APInt& LHS, const APInt& RHS) {
|
|
|
|
return LHS.urem(RHS);
|
2007-02-09 07:48:24 +00:00
|
|
|
}
|
2007-02-08 14:35:19 +00:00
|
|
|
|
|
|
|
/// Performs multiplication on APInt values.
|
|
|
|
/// @brief Function for multiplication operation.
|
2007-02-16 22:36:51 +00:00
|
|
|
inline APInt mul(const APInt& LHS, const APInt& RHS) {
|
2007-02-08 14:35:19 +00:00
|
|
|
return LHS * RHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs addition on APInt values.
|
|
|
|
/// @brief Function for addition operation.
|
2007-02-16 22:36:51 +00:00
|
|
|
inline APInt add(const APInt& LHS, const APInt& RHS) {
|
2007-02-08 14:35:19 +00:00
|
|
|
return LHS + RHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs subtraction on APInt values.
|
|
|
|
/// @brief Function for subtraction operation.
|
2007-02-16 22:36:51 +00:00
|
|
|
inline APInt sub(const APInt& LHS, const APInt& RHS) {
|
2007-02-08 14:35:19 +00:00
|
|
|
return LHS - RHS;
|
|
|
|
}
|
|
|
|
|
2007-02-09 07:48:24 +00:00
|
|
|
/// Performs bitwise AND operation on APInt LHS and
|
|
|
|
/// APInt RHS.
|
|
|
|
/// @brief Bitwise AND function for APInt.
|
|
|
|
inline APInt And(const APInt& LHS, const APInt& RHS) {
|
|
|
|
return LHS & RHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs bitwise OR operation on APInt LHS and APInt RHS.
|
|
|
|
/// @brief Bitwise OR function for APInt.
|
|
|
|
inline APInt Or(const APInt& LHS, const APInt& RHS) {
|
|
|
|
return LHS | RHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs bitwise XOR operation on APInt.
|
|
|
|
/// @brief Bitwise XOR function for APInt.
|
|
|
|
inline APInt Xor(const APInt& LHS, const APInt& RHS) {
|
|
|
|
return LHS ^ RHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs a bitwise complement operation on APInt.
|
|
|
|
/// @brief Bitwise complement function.
|
|
|
|
inline APInt Not(const APInt& APIVal) {
|
|
|
|
return ~APIVal;
|
|
|
|
}
|
|
|
|
|
2007-02-08 14:35:19 +00:00
|
|
|
} // End of APIntOps namespace
|
|
|
|
|
2007-02-05 17:29:16 +00:00
|
|
|
} // End of llvm namespace
|
|
|
|
|
|
|
|
#endif
|