APInt.h revision f99705e8985bbd4ff5786f5fc9d843d5de38a3d4
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 { 62a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerpublic: 6320b1f5db1e93d28debc6449dcd56da6ca7aa6b67Reid Spencer 64a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t BitWidth; ///< The number of bits in this APInt. 65d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 66d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// This union is used to store the integer value. When the 677406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// integer bit-width <= 64, it uses VAL; 687406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// otherwise it uses the pVal. 69d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng union { 70d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng uint64_t VAL; ///< Used to store the <= 64 bits integer value. 71d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng uint64_t *pVal; ///< Used to store the >64 bits integer value. 72d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng }; 73d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 747406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// This enum is just used to hold a constant we needed for APInt. 75d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng enum { 76d81b0659501c66b2fec2009e8a999c3db6a1f95cReid Spencer APINT_BITS_PER_WORD = sizeof(uint64_t) * 8, 77d81b0659501c66b2fec2009e8a999c3db6a1f95cReid Spencer APINT_WORD_SIZE = sizeof(uint64_t) 78d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng }; 79d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 8020b1f5db1e93d28debc6449dcd56da6ca7aa6b67Reid Spencer // Fast internal constructor 8120b1f5db1e93d28debc6449dcd56da6ca7aa6b67Reid Spencer APInt(uint64_t* val, uint32_t bits) : BitWidth(bits), pVal(val) { } 8220b1f5db1e93d28debc6449dcd56da6ca7aa6b67Reid Spencer 83d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// Here one word's bitwidth equals to that of uint64_t. 847406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @returns the number of words to hold the integer value of this APInt. 857406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Get the number of words. 86a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t getNumWords() const { 87e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; 88d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng } 89d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 90d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns true if the number of bits <= 64, false otherwise. 91d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Determine if this APInt just has one word to store value. 92e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline bool isSingleWord() const { 93e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return BitWidth <= APINT_BITS_PER_WORD; 94e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 95d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 96d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the word position for the specified bit position. 97a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static inline uint32_t whichWord(uint32_t bitPosition) { 98e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return bitPosition / APINT_BITS_PER_WORD; 99e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 100d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 101d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the bit position in a word for the specified bit position 102d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// in APInt. 103a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static inline uint32_t whichBit(uint32_t bitPosition) { 104e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return bitPosition % APINT_BITS_PER_WORD; 105e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 106d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 107d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns a uint64_t type integer with just bit position at 108d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// "whichBit(bitPosition)" setting, others zero. 109a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static inline uint64_t maskBit(uint32_t bitPosition) { 110e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return (static_cast<uint64_t>(1)) << whichBit(bitPosition); 111e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 112d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 113e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This method is used internally to clear the to "N" bits that are not used 114d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// by the APInt. This is needed after the most significant word is assigned 115d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// a value to ensure that those bits are zero'd out. 116e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Clear high order bits 117c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer inline APInt& clearUnusedBits() { 118c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer // Compute how many bits are used in the final word 119c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD; 120c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer if (wordBits == 0) 121c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer // If all bits are used, we want to leave the value alone. This also 122c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer // avoids the undefined behavior of >> when the shfit is the same size as 123c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer // the word size (64). 124c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer return *this; 125c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer 126c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer // Mask out the hight bits. 127c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits); 128d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng if (isSingleWord()) 129c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer VAL &= mask; 130d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng else 131c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer pVal[getNumWords() - 1] &= mask; 132c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer return *this; 133d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng } 134d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 135d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the corresponding word for the specified bit position. 136d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// @brief Get the word corresponding to a bit position 137a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint64_t getWord(uint32_t bitPosition) const { 138e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; 139e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 140d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 141d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// This is used by the constructors that take string arguments. 142d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// @brief Converts a char array into an APInt 143a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer void fromString(uint32_t numBits, const char *StrStart, uint32_t slen, 144e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer uint8_t radix); 145f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 146d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// This is used by the toString method to divide by the radix. It simply 147c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// provides a more convenient form of divide for internal use since KnuthDiv 148c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// has specific constraints on its inputs. If those constraints are not met 149c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// then it provides a simpler form of divide. 150d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// @brief An internal division function for dividing APInts. 151d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer static void divide(const APInt LHS, uint32_t lhsWords, 152d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer const APInt &RHS, uint32_t rhsWords, 153d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer APInt *Quotient, APInt *Remainder); 154d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer 155f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer#ifndef NDEBUG 156f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer /// @brief debug method 157f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer void dump() const; 158f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer#endif 159f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer 160f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencerpublic: 161d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// @brief Create a new APInt of numBits width, initialized as val. 162a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, uint64_t val); 163d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 164d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// Note that numWords can be smaller or larger than the corresponding bit 165d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// width but any extraneous bits will be dropped. 166d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer /// @brief Create a new APInt of numBits width, initialized as bigVal[]. 167a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[]); 168d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1697406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Create a new APInt by translating the string represented 1707406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// integer value. 171a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, const std::string& Val, uint8_t radix); 172f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 173f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// @brief Create a new APInt by translating the char array represented 174f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// integer value. 175a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt(uint32_t numBits, const char StrStart[], uint32_t slen, uint8_t radix); 176d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1777406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Copy Constructor. 178d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt(const APInt& API); 179d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1807406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Destructor. 181d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng ~APInt(); 182d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1837406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Copy assignment operator. 184d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator=(const APInt& RHS); 185d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1867406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Assigns an integer value to the APInt. 1877406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Assignment operator. 188d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator=(uint64_t RHS); 189d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1907406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Increments the APInt by one. 1917406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Postfix increment operator. 192f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline const APInt operator++(int) { 193f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng APInt API(*this); 194b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng ++(*this); 195b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng return API; 196f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 197d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 1987406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Increments the APInt by one. 1997406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Prefix increment operator. 200d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator++(); 201d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2027406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Decrements the APInt by one. 2037406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Postfix decrement operator. 204f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline const APInt operator--(int) { 205f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng APInt API(*this); 206b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng --(*this); 207b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng return API; 208f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 209d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2107406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Decrements the APInt by one. 2117406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Prefix decrement operator. 212d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator--(); 213d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2147406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise AND operation on this APInt and the given APInt& RHS, 2157406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2167406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise AND assignment operator. 217d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator&=(const APInt& RHS); 218d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2197406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise OR operation on this APInt and the given APInt& RHS, 2207406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2217406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise OR assignment operator. 222d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator|=(const APInt& RHS); 223d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2247406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise XOR operation on this APInt and the given APInt& RHS, 2257406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2267406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise XOR assignment operator. 227d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator^=(const APInt& RHS); 228d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2297406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs a bitwise complement operation on this APInt. 2307406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise complement operator. 231d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator~() const; 232d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2337406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Multiplies this APInt by the given APInt& RHS and 2347406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2357406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Multiplication assignment operator. 236d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator*=(const APInt& RHS); 237d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2387406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Adds this APInt by the given APInt& RHS and 2397406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2407406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Addition assignment operator. 241d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator+=(const APInt& RHS); 242d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2437406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Subtracts this APInt by the given APInt &RHS and 2447406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// assigns the result to this APInt. 2457406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Subtraction assignment operator. 246d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt& operator-=(const APInt& RHS); 247d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2487406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise AND operation on this APInt and 2497406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// the given APInt& RHS. 2507406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise AND operator. 251d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator&(const APInt& RHS) const; 252d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2537406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise OR operation on this APInt and the given APInt& RHS. 2547406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise OR operator. 255d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator|(const APInt& RHS) const; 256d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2577406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs bitwise XOR operation on this APInt and the given APInt& RHS. 2587406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Bitwise XOR operator. 259d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator^(const APInt& RHS) const; 260d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2617406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Performs logical negation operation on this APInt. 2627406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Logical negation operator. 263d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng bool operator !() const; 264d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2657406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Multiplies this APInt by the given APInt& RHS. 2667406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Multiplication operator. 267d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator*(const APInt& RHS) const; 268d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2697406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Adds this APInt by the given APInt& RHS. 2707406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Addition operator. 271d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator+(const APInt& RHS) const; 272d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2737406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Subtracts this APInt by the given APInt& RHS 2747406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Subtraction operator. 275d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng APInt operator-(const APInt& RHS) const; 276d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 277e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unary negation operator 2780b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng inline APInt operator-() const { 279cd6f2bfc268add3dd54cbdf98b2a2c4862fe4ba5Reid Spencer return APInt(BitWidth, 0) - (*this); 2800b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng } 281d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 282d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Array-indexing support. 283a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer bool operator[](uint32_t bitPosition) const; 284d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 2857406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Compare this APInt with the given APInt& RHS 286d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// for the validity of the equality relationship. 2877406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Equality operator. 288d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng bool operator==(const APInt& RHS) const; 289d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 290f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// Compare this APInt with the given uint64_t value 291f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// for the validity of the equality relationship. 292f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// @brief Equality operator. 293f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng bool operator==(uint64_t Val) const; 294f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 2957406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// Compare this APInt with the given APInt& RHS 296d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// for the validity of the inequality relationship. 2977406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// @brief Inequality operator. 298f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline bool operator!=(const APInt& RHS) const { 299f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng return !((*this) == RHS); 300f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 301d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 302f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// Compare this APInt with the given uint64_t value 303f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// for the validity of the inequality relationship. 304f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng /// @brief Inequality operator. 305f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng inline bool operator!=(uint64_t Val) const { 306f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng return !((*this) == Val); 307f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng } 308f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng 309e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Equality comparison 310e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool eq(const APInt &RHS) const { 311e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return (*this) == RHS; 312e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 313e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 314e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Inequality comparison 315e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ne(const APInt &RHS) const { 316e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !((*this) == RHS); 317e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 318e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 319e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned less than comparison 320e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ult(const APInt& RHS) const; 321e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 322e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed less than comparison 323e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool slt(const APInt& RHS) const; 324e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 325e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned less or equal comparison 326e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ule(const APInt& RHS) const { 327e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return ult(RHS) || eq(RHS); 328e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 329e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 330e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed less or equal comparison 331e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool sle(const APInt& RHS) const { 332e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return slt(RHS) || eq(RHS); 333e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 334e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 335e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned greather than comparison 336e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool ugt(const APInt& RHS) const { 337e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !ult(RHS) && !eq(RHS); 338e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 339e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 340e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed greather than comparison 341e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool sgt(const APInt& RHS) const { 342e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !slt(RHS) && !eq(RHS); 343e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 344e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 345e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned greater or equal comparison 346e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool uge(const APInt& RHS) const { 347e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !ult(RHS); 348e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 349e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 350e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed greather or equal comparison 351e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool sge(const APInt& RHS) const { 352e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return !slt(RHS); 353e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 354e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 355dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer /// This just tests the high bit of this APInt to determine if it is negative. 356dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer /// @returns true if this APInt is negative, false otherwise 357dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer /// @brief Determine sign of this APInt. 358ebf4ebd69155ea46c7937aec25df1f50dd61c136Reid Spencer bool isNegative() const { 359dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer return (*this)[BitWidth - 1]; 360dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer } 361dcffd5c3d90f295c90bc211a9c402e1fc18f8b12Reid Spencer 362e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Arithmetic right-shift this APInt by shiftAmt. 363e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Arithmetic right-shift function. 364a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt ashr(uint32_t shiftAmt) const; 365e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 366e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Logical right-shift this APInt by shiftAmt. 367e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Logical right-shift function. 368a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt lshr(uint32_t shiftAmt) const; 369e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 370e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Left-shift this APInt by shiftAmt. 371e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Left-shift function. 372a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt shl(uint32_t shiftAmt) const; 373e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 374e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Signed divide this APInt by APInt RHS. 375e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Signed division function for APInt. 376e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline APInt sdiv(const APInt& RHS) const { 377e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool isNegativeLHS = (*this)[BitWidth - 1]; 378e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool isNegativeRHS = RHS[RHS.BitWidth - 1]; 379a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt Result = APIntOps::udiv( 380e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS); 381a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer return isNegativeLHS != isNegativeRHS ? -Result : Result; 382e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 383e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 384e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Unsigned divide this APInt by APInt RHS. 385e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Unsigned division function for APInt. 386e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt udiv(const APInt& RHS) const; 387e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 388e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Signed remainder operation on APInt. 389e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Function for signed remainder operation. 390e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline APInt srem(const APInt& RHS) const { 391e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool isNegativeLHS = (*this)[BitWidth - 1]; 392e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer bool isNegativeRHS = RHS[RHS.BitWidth - 1]; 393a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt Result = APIntOps::urem( 394e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS); 395a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer return isNegativeLHS ? -Result : Result; 396e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 397e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 398e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Unsigned remainder operation on APInt. 399e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Function for unsigned remainder operation. 400e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt urem(const APInt& RHS) const; 401e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 402e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Truncate the APInt to a specified width. It is an error to specify a width 403e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// that is greater than or equal to the current width. 404e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Truncate to new width. 405a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer void trunc(uint32_t width); 406e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 407e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This operation sign extends the APInt to a new width. If the high order 408e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// bit is set, the fill on the left will be done with 1 bits, otherwise zero. 409e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// It is an error to specify a width that is less than or equal to the 410e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// current width. 411e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Sign extend to a new width. 412a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer void sext(uint32_t width); 413e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 414e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This operation zero extends the APInt to a new width. Thie high order bits 415e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// are filled with 0 bits. It is an error to specify a width that is less 416e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// than or equal to the current width. 417e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Zero extend to a new width. 418a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer void zext(uint32_t width); 419e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 420e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set every bit to 1. 421e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt& set(); 422e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 423e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Set the given bit to 1 whose position is given as "bitPosition". 424e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set a given bit to 1. 425a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt& set(uint32_t bitPosition); 426e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 427e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set every bit to 0. 428e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt& clear(); 429e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 430e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Set the given bit to 0 whose position is given as "bitPosition". 431e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Set a given bit to 0. 432a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt& clear(uint32_t bitPosition); 433e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 434e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Toggle every bit to its opposite value. 435e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt& flip(); 436e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 437e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Toggle a given bit to its opposite value whose position is given 438e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// as "bitPosition". 439e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Toggles a given bit to its opposite value. 440a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt& flip(uint32_t bitPosition); 441e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer 442e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This function returns the number of active bits which is defined as the 443e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// bit width minus the number of leading zeros. This is used in several 444e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// computations to see how "wide" the value is. 445e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Compute the number of active bits in the value 446a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t getActiveBits() const { 447f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer return BitWidth - countLeadingZeros(); 448e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 449d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 450d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns a uint64_t value from this APInt. If this APInt contains a single 451d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// word, just returns VAL, otherwise pVal[0]. 452b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng inline uint64_t getValue(bool isSigned = false) const { 453d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng if (isSingleWord()) 454e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return isSigned ? int64_t(VAL << (64 - BitWidth)) >> 455e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer (64 - BitWidth) : VAL; 456a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t n = getActiveBits(); 457d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng if (n <= 64) 458d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng return pVal[0]; 459f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng assert(0 && "This APInt's bitwidth > 64"); 460d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng } 461d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 462d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the largest value for an APInt of the specified bit-width and 463d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// if isSign == true, it should be largest signed value, otherwise largest 464d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// unsigned value. 465d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Gets max value of the APInt with bitwidth <= 64. 466f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer static APInt getMaxValue(uint32_t numBits, bool isSigned); 467d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 468d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the smallest value for an APInt of the given bit-width and 469d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// if isSign == true, it should be smallest signed value, otherwise zero. 470d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Gets min value of the APInt with bitwidth <= 64. 471f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer static APInt getMinValue(uint32_t numBits, bool isSigned); 472d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 473d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the all-ones value for an APInt of the specified bit-width. 474d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @brief Get the all-ones value. 475a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static APInt getAllOnesValue(uint32_t numBits); 476d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 477db3faa64ee89d537ca1b1410dd841ef51e957540Reid Spencer /// @returns the '0' value for an APInt of the specified bit-width. 478db3faa64ee89d537ca1b1410dd841ef51e957540Reid Spencer /// @brief Get the '0' value. 479a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer static APInt getNullValue(uint32_t numBits); 480db3faa64ee89d537ca1b1410dd841ef51e957540Reid Spencer 481e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// This converts the APInt to a boolean valy as a test against zero. 482e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// @brief Boolean conversion function. 483e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer inline bool getBoolValue() const { 484e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return countLeadingZeros() != BitWidth; 485e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 486d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 487f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// This checks to see if the value has all bits of the APInt are set or not. 488f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// @brief Determine if all bits are set 489f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer inline bool isAllOnesValue() const { 490f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer return countPopulation() == BitWidth; 491f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer } 492f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer 493f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// This checks to see if the value of this APInt is the maximum unsigned 494f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// value for the APInt's bit width. 495f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// @brief Determine if this is the largest unsigned value. 496f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer bool isMaxValue() const { 497f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer return countPopulation() == BitWidth; 498f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer } 499f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer 500f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// This checks to see if the value of this APInt is the maximum signed 501f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// value for the APInt's bit width. 502f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// @brief Determine if this is the largest signed value. 503f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer bool isMaxSignedValue() const { 504f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer return BitWidth == 1 ? VAL == 0 : 505f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer !isNegative() && countPopulation() == BitWidth - 1; 506f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer } 507f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer 508f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// This checks to see if the value of this APInt is the minimum signed 509f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// value for the APInt's bit width. 510f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// @brief Determine if this is the smallest unsigned value. 511f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer bool isMinValue() const { 512f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer return countPopulation() == 0; 513f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer } 514f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer 515f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// This checks to see if the value of this APInt is the minimum signed 516f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// value for the APInt's bit width. 517f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer /// @brief Determine if this is the smallest signed value. 518f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer bool isMinSignedValue() const { 519f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer return BitWidth == 1 ? VAL == 1 : 520f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer isNegative() && countPopulation() == 1; 521f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer } 522f99705e8985bbd4ff5786f5fc9d843d5de38a3d4Reid Spencer 523d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns a character interpretation of the APInt. 524443b570149f5756b298de6b63d13bbbf66b4f6fcReid Spencer std::string toString(uint8_t radix = 10, bool wantSigned = true) const; 525d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 526e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Get an APInt with the same BitWidth as this APInt, just zero mask 5277406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// the low bits and right shift to the least significant bit. 528d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the high "numBits" bits of this APInt. 529a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt getHiBits(uint32_t numBits) const; 530d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 531e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer /// Get an APInt with the same BitWidth as this APInt, just zero mask 5327406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng /// the high bits. 533d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the low "numBits" bits of this APInt. 534a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer APInt getLoBits(uint32_t numBits) const; 535d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 536d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns true if the argument APInt value is a power of two > 0. 537cd6f2bfc268add3dd54cbdf98b2a2c4862fe4ba5Reid Spencer bool isPowerOf2() const; 538d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 539c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countLeadingZeros - This function is an APInt version of the 540c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number 541c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// of zeros from the most significant bit to the first one bit. 542c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero. 543d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the number of zeros from the most significant bit to the first 544d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// one bits. 545c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// @brief Count the number of trailing one bits. 546a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t countLeadingZeros() const; 547d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 548c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countTrailingZeros - This function is an APInt version of the 549c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countTrailingZoers_{32,64} functions in MathExtras.h. It counts 550c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// the number of zeros from the least significant bit to the first one bit. 551c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero. 552d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the number of zeros from the least significant bit to the first 553d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// one bit. 554c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// @brief Count the number of trailing zero bits. 555a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t countTrailingZeros() const; 556d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 557c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countPopulation - This function is an APInt version of the 558c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// countPopulation_{32,64} functions in MathExtras.h. It counts the number 559c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// of 1 bits in the APInt value. 560c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// @returns 0 if the value is zero. 561d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the number of set bits. 562c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer /// @brief Count the number of bits set. 563a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer uint32_t countPopulation() const; 564d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 565d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng /// @returns the total number of bits. 566a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t getBitWidth() const { 567e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return BitWidth; 568e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer } 569d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 570ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng /// @brief Check if this APInt has a N-bits integer value. 571a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline bool isIntN(uint32_t N) const { 572b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng assert(N && "N == 0 ???"); 573ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng if (isSingleWord()) { 574b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng return VAL == (VAL & (~0ULL >> (64 - N))); 575ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } else { 576e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt Tmp(N, getNumWords(), pVal); 577ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return Tmp == (*this); 578ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } 579ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } 580ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 581ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng /// @returns a byte-swapped representation of this APInt Value. 582e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer APInt byteSwap() const; 583ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 584ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng /// @returns the floor log base 2 of this APInt. 585a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencer inline uint32_t logBase2() const { 586e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return getNumWords() * APINT_BITS_PER_WORD - 1 - countLeadingZeros(); 587ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng } 588ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 589d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng /// @brief Converts this APInt to a double value. 590e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer double roundToDouble(bool isSigned = false) const; 591ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 592d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng}; 593d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 5940b706b18bd0a7760d971727460a1f26bff8289b0Zhou Shengnamespace APIntOps { 5950b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 596d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @brief Check if the specified APInt has a N-bits integer value. 597a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline bool isIntN(uint32_t N, const APInt& APIVal) { 598e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.isIntN(N); 599d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 600d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 601d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns true if the argument APInt value is a sequence of ones 602d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// starting at the least significant bit with the remainder zero. 603a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline const bool isMask(uint32_t numBits, const APInt& APIVal) { 604e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.getBoolValue() && ((APIVal + APInt(numBits,1)) & APIVal) == 0; 605d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 606d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 607d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns true if the argument APInt value contains a sequence of ones 608d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// with the remainder zero. 609a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline const bool isShiftedMask(uint32_t numBits, const APInt& APIVal) { 610e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal); 611d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 612d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 613d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns a byte-swapped representation of the specified APInt Value. 614e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt byteSwap(const APInt& APIVal) { 615e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.byteSwap(); 616ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 617d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 618d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns the floor log base 2 of the specified APInt value. 619a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline uint32_t logBase2(const APInt& APIVal) { 620e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.logBase2(); 621d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} 622d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 623c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// GreatestCommonDivisor - This function returns the greatest common 624c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// divisor of the two APInt values using Enclid's algorithm. 625c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// @returns the greatest common divisor of Val1 and Val2 626c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// @brief Compute GCD of two APInt values. 627c68c2243dcb716bc117f41aa543ca8a41046df41Reid SpencerAPInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2); 628d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 629d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given APInt to a double value. 630e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline double RoundAPIntToDouble(const APInt& APIVal, bool isSigned = false) { 631e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return APIVal.roundToDouble(isSigned); 632d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng} 633d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 634d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given APInt to a float vlalue. 635e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline float RoundAPIntToFloat(const APInt& APIVal) { 636e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return float(RoundAPIntToDouble(APIVal)); 637d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng} 638d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 639c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// RoundDoubleToAPInt - This function convert a double value to an APInt value. 640d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given double value into a APInt. 641e81d2dad2c54014d36c73573307db5852c5caf8eReid SpencerAPInt RoundDoubleToAPInt(double Double); 642d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 643c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// RoundFloatToAPInt - Converts a float value into an APInt value. 644c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// @brief Converts a float value into a APInt. 645e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt RoundFloatToAPInt(float Float) { 646e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return RoundDoubleToAPInt(double(Float)); 647d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng} 648d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng 6490b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Arithmetic right-shift the APInt by shiftAmt. 6500b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Arithmetic right-shift function. 651a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline APInt ashr(const APInt& LHS, uint32_t shiftAmt) { 652e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.ashr(shiftAmt); 653ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 6540b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 6550b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Logical right-shift the APInt by shiftAmt. 6560b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Logical right-shift function. 657a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline APInt lshr(const APInt& LHS, uint32_t shiftAmt) { 658e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.lshr(shiftAmt); 659ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 6600b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 6610b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Left-shift the APInt by shiftAmt. 6620b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Left-shift function. 663a932e3f799cbf2f689e4987fea543d3f8661e7a8Reid Spencerinline APInt shl(const APInt& LHS, uint32_t shiftAmt) { 664e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.shl(shiftAmt); 665ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 6660b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 6670b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Signed divide APInt LHS by APInt RHS. 6680b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Signed division function for APInt. 669e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt sdiv(const APInt& LHS, const APInt& RHS) { 670e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.sdiv(RHS); 6710b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 6720b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 6730b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Unsigned divide APInt LHS by APInt RHS. 6740b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Unsigned division function for APInt. 675e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt udiv(const APInt& LHS, const APInt& RHS) { 676e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.udiv(RHS); 677ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 6780b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 6790b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Signed remainder operation on APInt. 6800b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for signed remainder operation. 681e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt srem(const APInt& LHS, const APInt& RHS) { 682e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.srem(RHS); 6830b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 6840b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 6850b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Unsigned remainder operation on APInt. 6860b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for unsigned remainder operation. 687e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt urem(const APInt& LHS, const APInt& RHS) { 688e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer return LHS.urem(RHS); 689ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 6900b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 6910b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs multiplication on APInt values. 6920b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for multiplication operation. 693e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt mul(const APInt& LHS, const APInt& RHS) { 6940b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng return LHS * RHS; 6950b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 6960b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 6970b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs addition on APInt values. 6980b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for addition operation. 699e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt add(const APInt& LHS, const APInt& RHS) { 7000b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng return LHS + RHS; 7010b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 7020b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 7030b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs subtraction on APInt values. 7040b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for subtraction operation. 705e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt sub(const APInt& LHS, const APInt& RHS) { 7060b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng return LHS - RHS; 7070b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} 7080b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 709ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise AND operation on APInt LHS and 710ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// APInt RHS. 711ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise AND function for APInt. 712ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt And(const APInt& LHS, const APInt& RHS) { 713ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return LHS & RHS; 714ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 715ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 716ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise OR operation on APInt LHS and APInt RHS. 717ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise OR function for APInt. 718ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Or(const APInt& LHS, const APInt& RHS) { 719ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return LHS | RHS; 720ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 721ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 722ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise XOR operation on APInt. 723ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise XOR function for APInt. 724ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Xor(const APInt& LHS, const APInt& RHS) { 725ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return LHS ^ RHS; 726ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 727ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 728ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs a bitwise complement operation on APInt. 729ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise complement function. 730ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Not(const APInt& APIVal) { 731ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng return ~APIVal; 732ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng} 733ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng 7340b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} // End of APIntOps namespace 7350b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng 736d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} // End of llvm namespace 737d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng 738d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#endif 739