APInt.h revision da347141ecb7ce44c532cdc51b000d5ab65db79f
1d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===-- llvm/Support/APInt.h - For Arbitrary Precision Integer -*- C++ -*--===// 2d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// 3d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// The LLVM Compiler Infrastructure 4d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// 5d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// This file was developed by Sheng Zhou and is distributed under the 6d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// University of Illinois Open Source License. See LICENSE.TXT for details. 7d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// 8d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===// 9d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// 10d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// This file implements a class to represent arbitrary precision integral 11d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// constant values. 12d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// 13d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===// 14d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 15d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#ifndef LLVM_APINT_H 16d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#define LLVM_APINT_H 17d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 18d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#include "llvm/Support/DataTypes.h" 1958a0d64fae7ace05b27dad94fd427991d853619bLauro Ramos Venancio#include <cassert> 20d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#include <string> 21d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 22d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Shengnamespace llvm { 23d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 240b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Forward declaration. 250b706b18bd0a7760d971727460a1f26bff8289b0Zhou Shengclass APInt; 260b706b18bd0a7760d971727460a1f26bff8289b0Zhou Shengnamespace APIntOps { 27e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt udiv(const APInt& LHS, const APInt& RHS); 28e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt urem(const APInt& LHS, const APInt& RHS); 290b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 300b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 31d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===// 32d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// APInt Class 33d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===// 34d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 35d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// APInt - This class represents arbitrary precision constant integral values. 36d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// It is a functional replacement for common case unsigned integer type like 37d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width 38e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// integer sizes and large integer value types such as 3-bits, 15-bits, or more 39d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// than 64-bits of precision. APInt provides a variety of arithmetic operators 40e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// and methods to manipulate integer values of any bit-width. It supports both 41e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// the typical integer arithmetic and comparison operations as well as bitwise 42e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// manipulation. 43d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// 44e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// The class has several invariants worth noting: 45e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// * All bit, byte, and word positions are zero-based. 46e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// * Once the bit width is set, it doesn't change except by the Truncate, 47e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// SignExtend, or ZeroExtend operations. 48e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// * All binary operators must be on APInt instances of the same bit width. 49e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// Attempting to use these operators on instances with different bit 50e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// widths will yield an assertion. 51e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// * The value is stored canonically as an unsigned value. For operations 52e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// where it makes a difference, there are both signed and unsigned variants 53e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// of the operation. For example, sdiv and udiv. However, because the bit 54e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// widths must be the same, operations such as Mul and Add produce the same 55e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// results regardless of whether the values are interpreted as signed or 56e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// not. 57e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// * In general, the class tries to follow the style of computation that LLVM 58e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// uses in its IR. This simplifies its use for LLVM. 597406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng/// 60e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// @brief Class for arbitrary precision integers. 61d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Shengclass APInt { 6220b1f5db1e93d28debc6449dcd56da6ca7aa6b67Reid Spencer 63a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t BitWidth; ///< The number of bits in this APInt. 64d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 65d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// This union is used to store the integer value. When the 667406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// integer bit-width <= 64, it uses VAL; 677406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// otherwise it uses the pVal. 68d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng union { 69d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng uint64_t VAL; ///< Used to store the <= 64 bits integer value. 70d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng uint64_t *pVal; ///< Used to store the >64 bits integer value. 71d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng }; 72d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 737406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// This enum is just used to hold a constant we needed for APInt. 74d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng enum { 75d81b0659501c66b2fec2009e8a999c3db6a1f95cReid Spencer APINT_BITS_PER_WORD = sizeof(uint64_t) * 8, 76d81b0659501c66b2fec2009e8a999c3db6a1f95cReid Spencer APINT_WORD_SIZE = sizeof(uint64_t) 77d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng }; 78d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 7920b1f5db1e93d28debc6449dcd56da6ca7aa6b67Reid Spencer // Fast internal constructor 8020b1f5db1e93d28debc6449dcd56da6ca7aa6b67Reid Spencer APInt(uint64_t* val, uint32_t bits) : BitWidth(bits), pVal(val) { } 8120b1f5db1e93d28debc6449dcd56da6ca7aa6b67Reid Spencer 82d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// Here one word's bitwidth equals to that of uint64_t. 837406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @returns the number of words to hold the integer value of this APInt. 847406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Get the number of words. 85a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t getNumWords() const { 86e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; 87d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng } 88d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 89d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns true if the number of bits <= 64, false otherwise. 90d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Determine if this APInt just has one word to store value. 91e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline bool isSingleWord() const { 92e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return BitWidth <= APINT_BITS_PER_WORD; 93e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 94d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 95d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the word position for the specified bit position. 96a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static inline uint32_t whichWord(uint32_t bitPosition) { 97e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return bitPosition / APINT_BITS_PER_WORD; 98e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 99d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 100d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the bit position in a word for the specified bit position 101d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// in APInt. 102a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static inline uint32_t whichBit(uint32_t bitPosition) { 103e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return bitPosition % APINT_BITS_PER_WORD; 104e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 105d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 106d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns a uint64_t type integer with just bit position at 107d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// "whichBit(bitPosition)" setting, others zero. 108a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static inline uint64_t maskBit(uint32_t bitPosition) { 109239e4021ce2dc1e57f31190b9e9737d0f2c507c1Reid Spencer return 1ULL << whichBit(bitPosition); 110e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 111d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 112e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This method is used internally to clear the to "N" bits that are not used 113d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// by the APInt. This is needed after the most significant word is assigned 114d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// a value to ensure that those bits are zero'd out. 115e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Clear high order bits 116c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer inline APInt& clearUnusedBits() { 117c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer // Compute how many bits are used in the final word 118c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD; 119c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer if (wordBits == 0) 120c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer // If all bits are used, we want to leave the value alone. This also 121c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer // avoids the undefined behavior of >> when the shfit is the same size as 122c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer // the word size (64). 123c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer return *this; 124c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer 125c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer // Mask out the hight bits. 126c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits); 127d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng if (isSingleWord()) 128c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer VAL &= mask; 129d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng else 130c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer pVal[getNumWords() - 1] &= mask; 131c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer return *this; 132d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng } 133d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 134d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the corresponding word for the specified bit position. 135d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// @brief Get the word corresponding to a bit position 136a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint64_t getWord(uint32_t bitPosition) const { 137e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; 138e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 139d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 140d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// This is used by the constructors that take string arguments. 141d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// @brief Converts a char array into an APInt 142a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer void fromString(uint32_t numBits, const char *StrStart, uint32_t slen, 143e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer uint8_t radix); 144f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 145d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// This is used by the toString method to divide by the radix. It simply 146c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// provides a more convenient form of divide for internal use since KnuthDiv 147c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// has specific constraints on its inputs. If those constraints are not met 148c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// then it provides a simpler form of divide. 149d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// @brief An internal division function for dividing APInts. 150d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer static void divide(const APInt LHS, uint32_t lhsWords, 151d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer const APInt &RHS, uint32_t rhsWords, 152d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer APInt *Quotient, APInt *Remainder); 153d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer 154f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer#ifndef NDEBUG 155f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer /// @brief debug method 156f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer void dump() const; 157f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer#endif 158f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer 159f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencerpublic: 160d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// @brief Create a new APInt of numBits width, initialized as val. 161a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, uint64_t val); 162d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 163d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// Note that numWords can be smaller or larger than the corresponding bit 164d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// width but any extraneous bits will be dropped. 165d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// @brief Create a new APInt of numBits width, initialized as bigVal[]. 166a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[]); 167d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1687406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Create a new APInt by translating the string represented 1697406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// integer value. 170a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, const std::string& Val, uint8_t radix); 171f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 172f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// @brief Create a new APInt by translating the char array represented 173f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// integer value. 174a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, const char StrStart[], uint32_t slen, uint8_t radix); 175d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1767406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Copy Constructor. 177d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt(const APInt& API); 178d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1797406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Destructor. 180d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng ~APInt(); 181d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1827406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Copy assignment operator. 183d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator=(const APInt& RHS); 184d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1857406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Assigns an integer value to the APInt. 1867406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Assignment operator. 187d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator=(uint64_t RHS); 188d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1897406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Increments the APInt by one. 1907406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Postfix increment operator. 191f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline const APInt operator++(int) { 192f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng APInt API(*this); 193b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng ++(*this); 194b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng return API; 195f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 196d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1977406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Increments the APInt by one. 1987406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Prefix increment operator. 199d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator++(); 200d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2017406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Decrements the APInt by one. 2027406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Postfix decrement operator. 203f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline const APInt operator--(int) { 204f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng APInt API(*this); 205b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng --(*this); 206b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng return API; 207f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 208d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2097406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Decrements the APInt by one. 2107406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Prefix decrement operator. 211d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator--(); 212d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2137406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise AND operation on this APInt and the given APInt& RHS, 2147406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2157406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise AND assignment operator. 216d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator&=(const APInt& RHS); 217d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2187406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise OR operation on this APInt and the given APInt& RHS, 2197406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2207406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise OR assignment operator. 221d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator|=(const APInt& RHS); 222d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2237406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise XOR operation on this APInt and the given APInt& RHS, 2247406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2257406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise XOR assignment operator. 226d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator^=(const APInt& RHS); 227d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2287406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs a bitwise complement operation on this APInt. 2297406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise complement operator. 230d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator~() const; 231d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2327406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Multiplies this APInt by the given APInt& RHS and 2337406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2347406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Multiplication assignment operator. 235d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator*=(const APInt& RHS); 236d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2377406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Adds this APInt by the given APInt& RHS and 2387406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2397406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Addition assignment operator. 240d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator+=(const APInt& RHS); 241d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2427406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Subtracts this APInt by the given APInt &RHS and 2437406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2447406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Subtraction assignment operator. 245d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator-=(const APInt& RHS); 246d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2477406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise AND operation on this APInt and 2487406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// the given APInt& RHS. 2497406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise AND operator. 250d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator&(const APInt& RHS) const; 251da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer APInt And(const APInt& RHS) const { 252da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer return this->operator&(RHS); 253da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer } 254d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2557406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise OR operation on this APInt and the given APInt& RHS. 2567406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise OR operator. 257d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator|(const APInt& RHS) const; 258da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer APInt Or(const APInt& RHS) const { 259da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer return this->operator|(RHS); 260da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer } 261d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2627406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise XOR operation on this APInt and the given APInt& RHS. 2637406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise XOR operator. 264d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator^(const APInt& RHS) const; 265da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer APInt Xor(const APInt& RHS) const { 266da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer return this->operator^(RHS); 267da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer } 268d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2697406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs logical negation operation on this APInt. 2707406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Logical negation operator. 271d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng bool operator !() const; 272d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2737406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Multiplies this APInt by the given APInt& RHS. 2747406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Multiplication operator. 275d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator*(const APInt& RHS) const; 276d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2777406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Adds this APInt by the given APInt& RHS. 2787406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Addition operator. 279d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator+(const APInt& RHS) const; 280f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer APInt operator+(uint64_t RHS) const { 281f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer return (*this) + APInt(BitWidth, RHS); 282f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer } 283f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer 284d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2857406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Subtracts this APInt by the given APInt& RHS 2867406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Subtraction operator. 287d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator-(const APInt& RHS) const; 288f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer APInt operator-(uint64_t RHS) const { 289f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer return (*this) - APInt(BitWidth, RHS); 290f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer } 291d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 292e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unary negation operator 2930b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng inline APInt operator-() const { 294cd6f2bfc268add3dd54cbdf98b2a2c4862fe4ba5Reid Spencer return APInt(BitWidth, 0) - (*this); 2950b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng } 296d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 297d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Array-indexing support. 298a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer bool operator[](uint32_t bitPosition) const; 299d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 3007406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Compare this APInt with the given APInt& RHS 301d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// for the validity of the equality relationship. 3027406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Equality operator. 303d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng bool operator==(const APInt& RHS) const; 304d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 305f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// Compare this APInt with the given uint64_t value 306f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// for the validity of the equality relationship. 307f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// @brief Equality operator. 308f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng bool operator==(uint64_t Val) const; 309f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 3107406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Compare this APInt with the given APInt& RHS 311d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// for the validity of the inequality relationship. 3127406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Inequality operator. 313f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline bool operator!=(const APInt& RHS) const { 314f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng return !((*this) == RHS); 315f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 316d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 317f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// Compare this APInt with the given uint64_t value 318f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// for the validity of the inequality relationship. 319f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// @brief Inequality operator. 320f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline bool operator!=(uint64_t Val) const { 321f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng return !((*this) == Val); 322f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 323f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 324e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Equality comparison 325e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool eq(const APInt &RHS) const { 326e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return (*this) == RHS; 327e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 328e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 329e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Inequality comparison 330e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ne(const APInt &RHS) const { 331e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !((*this) == RHS); 332e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 333e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 334e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned less than comparison 335e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ult(const APInt& RHS) const; 336e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 337e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed less than comparison 338e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool slt(const APInt& RHS) const; 339e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 340e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned less or equal comparison 341e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ule(const APInt& RHS) const { 342e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return ult(RHS) || eq(RHS); 343e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 344e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 345e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed less or equal comparison 346e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool sle(const APInt& RHS) const { 347e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return slt(RHS) || eq(RHS); 348e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 349e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 350e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned greather than comparison 351e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ugt(const APInt& RHS) const { 352e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !ult(RHS) && !eq(RHS); 353e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 354e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 355e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed greather than comparison 356e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool sgt(const APInt& RHS) const { 357e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !slt(RHS) && !eq(RHS); 358e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 359e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 360e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned greater or equal comparison 361e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool uge(const APInt& RHS) const { 362e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !ult(RHS); 363e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 364e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 365e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed greather or equal comparison 366e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool sge(const APInt& RHS) const { 367e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !slt(RHS); 368e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 369e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 370dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer /// This just tests the high bit of this APInt to determine if it is negative. 371dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer /// @returns true if this APInt is negative, false otherwise 372dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer /// @brief Determine sign of this APInt. 373ebf4ebd69155ea46c7937aec25df1f50dd61c136Reid Spencer bool isNegative() const { 374dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer return (*this)[BitWidth - 1]; 375dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer } 376dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer 377239e4021ce2dc1e57f31190b9e9737d0f2c507c1Reid Spencer /// This just tests the high bit of the APInt to determine if the value is 378239e4021ce2dc1e57f31190b9e9737d0f2c507c1Reid Spencer /// positove or not. 379239e4021ce2dc1e57f31190b9e9737d0f2c507c1Reid Spencer /// @brief Determine if this APInt Value is positive. 380239e4021ce2dc1e57f31190b9e9737d0f2c507c1Reid Spencer bool isPositive() const { 381239e4021ce2dc1e57f31190b9e9737d0f2c507c1Reid Spencer return !isNegative(); 382239e4021ce2dc1e57f31190b9e9737d0f2c507c1Reid Spencer } 383239e4021ce2dc1e57f31190b9e9737d0f2c507c1Reid Spencer 384e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Arithmetic right-shift this APInt by shiftAmt. 385e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Arithmetic right-shift function. 386a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt ashr(uint32_t shiftAmt) const; 387e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 388e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Logical right-shift this APInt by shiftAmt. 389e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Logical right-shift function. 390a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt lshr(uint32_t shiftAmt) const; 391e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 392e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Left-shift this APInt by shiftAmt. 393e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Left-shift function. 394a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt shl(uint32_t shiftAmt) const; 395e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 396e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Signed divide this APInt by APInt RHS. 397e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed division function for APInt. 398e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline APInt sdiv(const APInt& RHS) const { 39966ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer bool isNegativeLHS = isNegative(); 40066ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer bool isNegativeRHS = RHS.isNegative(); 401a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt Result = APIntOps::udiv( 402e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS); 403a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer return isNegativeLHS != isNegativeRHS ? -Result : Result; 404e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 405e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 406e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Unsigned divide this APInt by APInt RHS. 407e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned division function for APInt. 408e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt udiv(const APInt& RHS) const; 409e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 410e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Signed remainder operation on APInt. 411e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Function for signed remainder operation. 412e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline APInt srem(const APInt& RHS) const { 41366ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer bool isNegativeLHS = isNegative(); 41466ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer bool isNegativeRHS = RHS.isNegative(); 415a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt Result = APIntOps::urem( 416e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS); 417a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer return isNegativeLHS ? -Result : Result; 418e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 419e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 420e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Unsigned remainder operation on APInt. 421e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Function for unsigned remainder operation. 422e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt urem(const APInt& RHS) const; 423e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 424e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Truncate the APInt to a specified width. It is an error to specify a width 425e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// that is greater than or equal to the current width. 426e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Truncate to new width. 427f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer APInt &trunc(uint32_t width); 428e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 429e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This operation sign extends the APInt to a new width. If the high order 430e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// bit is set, the fill on the left will be done with 1 bits, otherwise zero. 431e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// It is an error to specify a width that is less than or equal to the 432e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// current width. 433e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Sign extend to a new width. 434f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer APInt &sext(uint32_t width); 435e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 436e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This operation zero extends the APInt to a new width. Thie high order bits 437e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// are filled with 0 bits. It is an error to specify a width that is less 438e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// than or equal to the current width. 439e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Zero extend to a new width. 440f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer APInt &zext(uint32_t width); 441e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 44268e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer /// Make this APInt have the bit width given by \p width. The value is sign 44368e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer /// extended, truncated, or left alone to make it that width. 44468e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer /// @brief Sign extend or truncate to width 44568e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer APInt &sextOrTrunc(uint32_t width); 44668e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer 44768e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer /// Make this APInt have the bit width given by \p width. The value is zero 44868e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer /// extended, truncated, or left alone to make it that width. 44968e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer /// @brief Zero extend or truncate to width 45068e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer APInt &zextOrTrunc(uint32_t width); 45168e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer 452e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set every bit to 1. 453e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt& set(); 454e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 455e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Set the given bit to 1 whose position is given as "bitPosition". 456e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set a given bit to 1. 457a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt& set(uint32_t bitPosition); 458e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 459e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set every bit to 0. 460e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt& clear(); 461e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 462e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Set the given bit to 0 whose position is given as "bitPosition". 463e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set a given bit to 0. 464a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt& clear(uint32_t bitPosition); 465e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 466e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Toggle every bit to its opposite value. 467e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt& flip(); 468e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 469e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Toggle a given bit to its opposite value whose position is given 470e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// as "bitPosition". 471e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Toggles a given bit to its opposite value. 472a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt& flip(uint32_t bitPosition); 473e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 474e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This function returns the number of active bits which is defined as the 475e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// bit width minus the number of leading zeros. This is used in several 476e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// computations to see how "wide" the value is. 477e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Compute the number of active bits in the value 478a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t getActiveBits() const { 479f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer return BitWidth - countLeadingZeros(); 480e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 481d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 4829d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// This function returns the number of active words in the value of this 4839d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// APInt. This is used in conjunction with getActiveData to extract the raw 4849d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// value of the APInt. 4859d3c51923392f69b63512e59e44f72affbe6136eReid Spencer inline uint32_t getActiveWords() const { 48684b4eeccc70b39f975a82ad098413d129d38a7d3Reid Spencer return whichWord(getActiveBits()-1) + 1; 4879d3c51923392f69b63512e59e44f72affbe6136eReid Spencer } 4889d3c51923392f69b63512e59e44f72affbe6136eReid Spencer 4899d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// This function returns a pointer to the internal storage of the APInt. 4909d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// This is useful for writing out the APInt in binary form without any 4919d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// conversions. 4929d3c51923392f69b63512e59e44f72affbe6136eReid Spencer inline const uint64_t* getRawData() const { 4939d3c51923392f69b63512e59e44f72affbe6136eReid Spencer if (isSingleWord()) 4949d3c51923392f69b63512e59e44f72affbe6136eReid Spencer return &VAL; 4959d3c51923392f69b63512e59e44f72affbe6136eReid Spencer return &pVal[0]; 4969d3c51923392f69b63512e59e44f72affbe6136eReid Spencer } 4979d3c51923392f69b63512e59e44f72affbe6136eReid Spencer 4989d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// Computes the minimum bit width for this APInt while considering it to be 4999d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// a signed (and probably negative) value. If the value is not negative, 5009d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// this function returns the same value as getActiveBits(). Otherwise, it 5019d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// returns the smallest bit width that will retain the negative value. For 5029d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so 5039d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// for -1, this function will always return 1. 5049d3c51923392f69b63512e59e44f72affbe6136eReid Spencer /// @brief Get the minimum bit size for this signed APInt 505681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer inline uint32_t getMinSignedBits() const { 506681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer if (isNegative()) 507681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer return BitWidth - countLeadingOnes() + 1; 508681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer return getActiveBits(); 509681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer } 510681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer 51131a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer /// This method attempts to return the value of this APInt as a zero extended 51231a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer /// uint64_t. The bitwidth must be <= 64 or the value must fit within a 51331a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer /// uint64_t. Otherwise an assertion will result. 51431a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer /// @brief Get zero extended value 51531a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer inline uint64_t getZExtValue() const { 516d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng if (isSingleWord()) 51731a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer return VAL; 51831a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer assert(getActiveBits() <= 64 && "Too many bits for uint64_t"); 51931a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer return pVal[0]; 52031a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer } 52131a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer 52231a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer /// This method attempts to return the value of this APInt as a sign extended 52331a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer /// int64_t. The bit width must be <= 64 or the value must fit within an 52431a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer /// int64_t. Otherwise an assertion will result. 52531a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer /// @brief Get sign extended value 52631a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer inline int64_t getSExtValue() const { 52731a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer if (isSingleWord()) 52831a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >> 52931a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer (APINT_BITS_PER_WORD - BitWidth); 53031a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer assert(getActiveBits() <= 64 && "Too many bits for int64_t"); 531946bca5bae2539ca55f8fb4700bff8b5e3f5a7ebReid Spencer return int64_t(pVal[0]); 532d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng } 533d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 53466ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// @brief Gets maximum unsigned value of APInt for specific bit width. 53566ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer static APInt getMaxValue(uint32_t numBits) { 53666ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return APInt(numBits, 0).set(); 53766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer } 53866ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer 53966ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// @brief Gets maximum signed value of APInt for a specific bit width. 54066ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer static APInt getSignedMaxValue(uint32_t numBits) { 54166ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return APInt(numBits, 0).set().clear(numBits - 1); 54266ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer } 54366ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer 54466ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// @brief Gets minimum unsigned value of APInt for a specific bit width. 54566ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer static APInt getMinValue(uint32_t numBits) { 54666ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return APInt(numBits, 0); 54766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer } 548d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 54966ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// @brief Gets minimum signed value of APInt for a specific bit width. 55066ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer static APInt getSignedMinValue(uint32_t numBits) { 55166ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return APInt(numBits, 0).set(numBits - 1); 55266ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer } 553d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 554d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the all-ones value for an APInt of the specified bit-width. 555d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Get the all-ones value. 55666ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer static APInt getAllOnesValue(uint32_t numBits) { 55766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return APInt(numBits, 0).set(); 55866ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer } 559d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 560db3faa64ee89d537ca1b1410dd841ef51e957540Reid Spencer /// @returns the '0' value for an APInt of the specified bit-width. 561db3faa64ee89d537ca1b1410dd841ef51e957540Reid Spencer /// @brief Get the '0' value. 56266ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer static APInt getNullValue(uint32_t numBits) { 56366ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return APInt(numBits, 0); 56466ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer } 565db3faa64ee89d537ca1b1410dd841ef51e957540Reid Spencer 56631a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer /// The hash value is computed as the sum of the words and the bit width. 56731a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer /// @returns A hash value computed from the sum of the APInt words. 56831a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer /// @brief Get a hash value based on this APInt 56931a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer uint64_t getHashValue() const; 57031a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer 571e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This converts the APInt to a boolean valy as a test against zero. 572e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Boolean conversion function. 573e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline bool getBoolValue() const { 574e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return countLeadingZeros() != BitWidth; 575e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 576d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 577f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// This checks to see if the value has all bits of the APInt are set or not. 578f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// @brief Determine if all bits are set 579f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer inline bool isAllOnesValue() const { 580f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer return countPopulation() == BitWidth; 581f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer } 582f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer 583f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// This checks to see if the value of this APInt is the maximum unsigned 584f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// value for the APInt's bit width. 585f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// @brief Determine if this is the largest unsigned value. 586f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer bool isMaxValue() const { 587f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer return countPopulation() == BitWidth; 588f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer } 589f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer 590f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// This checks to see if the value of this APInt is the maximum signed 591f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// value for the APInt's bit width. 592f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// @brief Determine if this is the largest signed value. 593f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer bool isMaxSignedValue() const { 594f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer return BitWidth == 1 ? VAL == 0 : 595f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer !isNegative() && countPopulation() == BitWidth - 1; 596f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer } 597f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer 598f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// This checks to see if the value of this APInt is the minimum signed 599f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// value for the APInt's bit width. 600f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// @brief Determine if this is the smallest unsigned value. 601f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer bool isMinValue() const { 602f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer return countPopulation() == 0; 603f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer } 604f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer 605f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// This checks to see if the value of this APInt is the minimum signed 606f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// value for the APInt's bit width. 607f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// @brief Determine if this is the smallest signed value. 608f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer bool isMinSignedValue() const { 609f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer return BitWidth == 1 ? VAL == 1 : 610f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer isNegative() && countPopulation() == 1; 611f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer } 612f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer 61366ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// This is used internally to convert an APInt to a string. 61466ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// @brief Converts an APInt to a std::string 61566ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer std::string toString(uint8_t radix, bool wantSigned) const; 61666ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer 61766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// Considers the APInt to be unsigned and converts it into a string in the 61866ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// radix given. The radix can be 2, 8, 10 or 16. 61966ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// @returns a character interpretation of the APInt 62066ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// @brief Convert unsigned APInt to string representation. 62166ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer inline std::string toString(uint8_t radix = 10) const { 62266ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return toString(radix, false); 62366ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer } 62466ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer 62566ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// Considers the APInt to be unsigned and converts it into a string in the 62666ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// radix given. The radix can be 2, 8, 10 or 16. 62766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// @returns a character interpretation of the APInt 62866ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// @brief Convert unsigned APInt to string representation. 62966ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer inline std::string toStringSigned(uint8_t radix = 10) const { 63066ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return toString(radix, true); 63166ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer } 632d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 633e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Get an APInt with the same BitWidth as this APInt, just zero mask 6347406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// the low bits and right shift to the least significant bit. 635d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the high "numBits" bits of this APInt. 636a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt getHiBits(uint32_t numBits) const; 637d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 638e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Get an APInt with the same BitWidth as this APInt, just zero mask 6397406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// the high bits. 640d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the low "numBits" bits of this APInt. 641a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt getLoBits(uint32_t numBits) const; 642d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 643d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns true if the argument APInt value is a power of two > 0. 644cd6f2bfc268add3dd54cbdf98b2a2c4862fe4ba5Reid Spencer bool isPowerOf2() const; 645d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 646c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countLeadingZeros - This function is an APInt version of the 647c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number 648c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// of zeros from the most significant bit to the first one bit. 649c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero. 650d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the number of zeros from the most significant bit to the first 651d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// one bits. 652681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer /// @brief Count the number of leading one bits. 653a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t countLeadingZeros() const; 654d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 655681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer /// countLeadingOnes - This function counts the number of contiguous 1 bits 656681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer /// in the high order bits. The count stops when the first 0 bit is reached. 657681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer /// @returns 0 if the high order bit is not set 658681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer /// @returns the number of 1 bits from the most significant to the least 659681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer /// @brief Count the number of leading one bits. 660681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer uint32_t countLeadingOnes() const; 661681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer 662c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countTrailingZeros - This function is an APInt version of the 663c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countTrailingZoers_{32,64} functions in MathExtras.h. It counts 664c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// the number of zeros from the least significant bit to the first one bit. 665c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero. 666d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the number of zeros from the least significant bit to the first 667d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// one bit. 668c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// @brief Count the number of trailing zero bits. 669a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t countTrailingZeros() const; 670d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 671c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countPopulation - This function is an APInt version of the 672c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countPopulation_{32,64} functions in MathExtras.h. It counts the number 673c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// of 1 bits in the APInt value. 674c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// @returns 0 if the value is zero. 675d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the number of set bits. 676c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// @brief Count the number of bits set. 677a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t countPopulation() const; 678d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 679d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the total number of bits. 680a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t getBitWidth() const { 681e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return BitWidth; 682e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 683d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 684ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng /// @brief Check if this APInt has a N-bits integer value. 685a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline bool isIntN(uint32_t N) const { 686b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng assert(N && "N == 0 ???"); 687ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng if (isSingleWord()) { 688b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng return VAL == (VAL & (~0ULL >> (64 - N))); 689ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } else { 690e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt Tmp(N, getNumWords(), pVal); 691ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return Tmp == (*this); 692ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } 693ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } 694ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 695ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng /// @returns a byte-swapped representation of this APInt Value. 696e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt byteSwap() const; 697ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 698ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng /// @returns the floor log base 2 of this APInt. 699a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t logBase2() const { 700e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return getNumWords() * APINT_BITS_PER_WORD - 1 - countLeadingZeros(); 701ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } 702ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 703d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng /// @brief Converts this APInt to a double value. 70466ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer double roundToDouble(bool isSigned) const; 70566ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer 70666ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// @brief Converts this unsigned APInt to a double value. 70766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer double roundToDouble() const { 70866ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return roundToDouble(false); 70966ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer } 710ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 71166ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer /// @brief Converts this signed APInt to a double value. 71266ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer double signedRoundToDouble() const { 71366ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return roundToDouble(true); 71466ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer } 715af8fb1984674db462bc6923ed54db0275c78b711Reid Spencer 716ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer /// The conversion does not do a translation from integer to double, it just 717ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer /// re-interprets the bits as a double. Note that it is valid to do this on 718ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer /// any bit width. Exactly 64 bits will be translated. 719ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer /// @brief Converts APInt bits to a double 720ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer double bitsToDouble() const { 721ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer union { 722ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer uint64_t I; 723ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer double D; 724ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer } T; 725ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer T.I = (isSingleWord() ? VAL : pVal[0]); 726ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer return T.D; 727ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer } 728ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer 729ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer /// The conversion does not do a translation from integer to float, it just 730ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer /// re-interprets the bits as a float. Note that it is valid to do this on 731ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer /// any bit width. Exactly 32 bits will be translated. 732ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer /// @brief Converts APInt bits to a double 733ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer float bitsToFloat() const { 734ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer union { 735ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer uint32_t I; 736ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer float F; 737ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer } T; 738ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer T.I = uint32_t((isSingleWord() ? VAL : pVal[0])); 739ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer return T.F; 740ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer } 741ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer 74253ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer /// The conversion does not do a translation from double to integer, it just 74353ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer /// re-interprets the bits of the double. Note that it is valid to do this on 74453ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer /// any bit width but bits from V may get truncated. 74553ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer /// @brief Converts a double to APInt bits. 74653ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer APInt& doubleToBits(double V) { 74753ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer union { 74853ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer uint64_t I; 74953ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer double D; 75053ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer } T; 75153ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer T.D = V; 75253ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer if (isSingleWord()) 75353ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer VAL = T.I; 75453ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer else 75553ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer pVal[0] = T.I; 75653ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer return clearUnusedBits(); 75753ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer } 75853ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer 75953ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer /// The conversion does not do a translation from float to integer, it just 76053ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer /// re-interprets the bits of the float. Note that it is valid to do this on 76153ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer /// any bit width but bits from V may get truncated. 76253ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer /// @brief Converts a float to APInt bits. 76353ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer APInt& floatToBits(float V) { 76453ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer union { 76553ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer uint32_t I; 76653ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer float F; 76753ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer } T; 76853ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer T.F = V; 76953ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer if (isSingleWord()) 77053ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer VAL = T.I; 77153ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer else 77253ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer pVal[0] = T.I; 77353ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer return clearUnusedBits(); 77453ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer } 77553ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer 776af8fb1984674db462bc6923ed54db0275c78b711Reid Spencer /// @brief Compute the square root 777af8fb1984674db462bc6923ed54db0275c78b711Reid Spencer APInt sqrt() const; 778b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer 779b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer /// If *this is < 0 then return -(*this), otherwise *this; 780b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer /// @brief Get the absolute value; 781b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer APInt abs() const { 782b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer if (isNegative()) 783b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer return -(*this); 784b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer return *this; 785b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer } 786d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng}; 787d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 7889d3c51923392f69b63512e59e44f72affbe6136eReid Spencerinline bool operator==(uint64_t V1, const APInt& V2) { 7899d3c51923392f69b63512e59e44f72affbe6136eReid Spencer return V2 == V1; 7909d3c51923392f69b63512e59e44f72affbe6136eReid Spencer} 7919d3c51923392f69b63512e59e44f72affbe6136eReid Spencer 7929d3c51923392f69b63512e59e44f72affbe6136eReid Spencerinline bool operator!=(uint64_t V1, const APInt& V2) { 7939d3c51923392f69b63512e59e44f72affbe6136eReid Spencer return V2 != V1; 7949d3c51923392f69b63512e59e44f72affbe6136eReid Spencer} 7959d3c51923392f69b63512e59e44f72affbe6136eReid Spencer 7960b706b18bd0a7760d971727460a1f26bff8289b0Zhou Shengnamespace APIntOps { 7970b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 798f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer/// @brief Determine the smaller of two APInts considered to be signed. 799f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencerinline APInt smin(const APInt &A, const APInt &B) { 800f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer return A.slt(B) ? A : B; 801f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer} 802f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer 803f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer/// @brief Determine the larger of two APInts considered to be signed. 804f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencerinline APInt smax(const APInt &A, const APInt &B) { 805f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer return A.sgt(B) ? A : B; 806f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer} 807f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer 808f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer/// @brief Determine the smaller of two APInts considered to be signed. 809f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencerinline APInt umin(const APInt &A, const APInt &B) { 810f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer return A.ult(B) ? A : B; 811f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer} 812f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer 813f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer/// @brief Determine the larger of two APInts considered to be unsigned. 814f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencerinline APInt umax(const APInt &A, const APInt &B) { 815f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer return A.ugt(B) ? A : B; 816f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer} 817f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer 818d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @brief Check if the specified APInt has a N-bits integer value. 819a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline bool isIntN(uint32_t N, const APInt& APIVal) { 820e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.isIntN(N); 821d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 822d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 823d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns true if the argument APInt value is a sequence of ones 824d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// starting at the least significant bit with the remainder zero. 825a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline const bool isMask(uint32_t numBits, const APInt& APIVal) { 826e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.getBoolValue() && ((APIVal + APInt(numBits,1)) & APIVal) == 0; 827d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 828d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 829d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns true if the argument APInt value contains a sequence of ones 830d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// with the remainder zero. 831a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline const bool isShiftedMask(uint32_t numBits, const APInt& APIVal) { 832e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal); 833d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 834d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 835d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns a byte-swapped representation of the specified APInt Value. 836e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt byteSwap(const APInt& APIVal) { 837e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.byteSwap(); 838ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 839d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 840d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns the floor log base 2 of the specified APInt value. 841a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline uint32_t logBase2(const APInt& APIVal) { 842e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.logBase2(); 843d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 844d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 845c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// GreatestCommonDivisor - This function returns the greatest common 846c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// divisor of the two APInt values using Enclid's algorithm. 847c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// @returns the greatest common divisor of Val1 and Val2 848c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// @brief Compute GCD of two APInt values. 849c68c2243dcb716bc117f41aa543ca8a41046df41Reid SpencerAPInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2); 850d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 85166ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer/// Treats the APInt as an unsigned value for conversion purposes. 85266ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer/// @brief Converts the given APInt to a double value. 85366ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencerinline double RoundAPIntToDouble(const APInt& APIVal) { 85466ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return APIVal.roundToDouble(); 85566ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer} 85666ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer 85766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer/// Treats the APInt as a signed value for conversion purposes. 858d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given APInt to a double value. 85966ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencerinline double RoundSignedAPIntToDouble(const APInt& APIVal) { 86066ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer return APIVal.signedRoundToDouble(); 861d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng} 862d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 863d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given APInt to a float vlalue. 864e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline float RoundAPIntToFloat(const APInt& APIVal) { 865e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return float(RoundAPIntToDouble(APIVal)); 866d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng} 867d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 868c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// RoundDoubleToAPInt - This function convert a double value to an APInt value. 869d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given double value into a APInt. 870409f092766e61ab317e46aec37b4a9234ead9b5aReid SpencerAPInt RoundDoubleToAPInt(double Double, uint32_t width = 64); 871d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 872c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// RoundFloatToAPInt - Converts a float value into an APInt value. 873c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// @brief Converts a float value into a APInt. 874e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt RoundFloatToAPInt(float Float) { 875e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return RoundDoubleToAPInt(double(Float)); 876d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng} 877d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 8780b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Arithmetic right-shift the APInt by shiftAmt. 8790b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Arithmetic right-shift function. 880a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline APInt ashr(const APInt& LHS, uint32_t shiftAmt) { 881e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.ashr(shiftAmt); 882ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 8830b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 8840b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Logical right-shift the APInt by shiftAmt. 8850b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Logical right-shift function. 886a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline APInt lshr(const APInt& LHS, uint32_t shiftAmt) { 887e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.lshr(shiftAmt); 888ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 8890b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 8900b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Left-shift the APInt by shiftAmt. 8910b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Left-shift function. 892a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline APInt shl(const APInt& LHS, uint32_t shiftAmt) { 893e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.shl(shiftAmt); 894ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 8950b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 8960b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Signed divide APInt LHS by APInt RHS. 8970b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Signed division function for APInt. 898e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt sdiv(const APInt& LHS, const APInt& RHS) { 899e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.sdiv(RHS); 9000b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 9010b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 9020b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Unsigned divide APInt LHS by APInt RHS. 9030b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Unsigned division function for APInt. 904e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt udiv(const APInt& LHS, const APInt& RHS) { 905e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.udiv(RHS); 906ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 9070b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 9080b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Signed remainder operation on APInt. 9090b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for signed remainder operation. 910e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt srem(const APInt& LHS, const APInt& RHS) { 911e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.srem(RHS); 9120b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 9130b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 9140b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Unsigned remainder operation on APInt. 9150b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for unsigned remainder operation. 916e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt urem(const APInt& LHS, const APInt& RHS) { 917e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.urem(RHS); 918ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 9190b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 9200b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs multiplication on APInt values. 9210b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for multiplication operation. 922e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt mul(const APInt& LHS, const APInt& RHS) { 9230b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng return LHS * RHS; 9240b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 9250b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 9260b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs addition on APInt values. 9270b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for addition operation. 928e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt add(const APInt& LHS, const APInt& RHS) { 9290b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng return LHS + RHS; 9300b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 9310b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 9320b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs subtraction on APInt values. 9330b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for subtraction operation. 934e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt sub(const APInt& LHS, const APInt& RHS) { 9350b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng return LHS - RHS; 9360b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 9370b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 938ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise AND operation on APInt LHS and 939ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// APInt RHS. 940ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise AND function for APInt. 941ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt And(const APInt& LHS, const APInt& RHS) { 942ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return LHS & RHS; 943ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 944ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 945ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise OR operation on APInt LHS and APInt RHS. 946ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise OR function for APInt. 947ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Or(const APInt& LHS, const APInt& RHS) { 948ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return LHS | RHS; 949ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 950ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 951ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise XOR operation on APInt. 952ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise XOR function for APInt. 953ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Xor(const APInt& LHS, const APInt& RHS) { 954ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return LHS ^ RHS; 955ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 956ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 957ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs a bitwise complement operation on APInt. 958ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise complement function. 959ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Not(const APInt& APIVal) { 960ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return ~APIVal; 961ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 962ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 9630b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} // End of APIntOps namespace 9640b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 965d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} // End of llvm namespace 966d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 967d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#endif 968