LLVM API Documentation

APInt.h
Go to the documentation of this file.
00001 //===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief This file implements a class to represent arbitrary precision
00012 /// integral constant values and operations on them.
00013 ///
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_ADT_APINT_H
00017 #define LLVM_ADT_APINT_H
00018 
00019 #include "llvm/ADT/ArrayRef.h"
00020 #include "llvm/Support/Compiler.h"
00021 #include "llvm/Support/MathExtras.h"
00022 #include <cassert>
00023 #include <climits>
00024 #include <cstring>
00025 #include <string>
00026 
00027 namespace llvm {
00028 class Deserializer;
00029 class FoldingSetNodeID;
00030 class Serializer;
00031 class StringRef;
00032 class hash_code;
00033 class raw_ostream;
00034 
00035 template <typename T> class SmallVectorImpl;
00036 
00037 // An unsigned host type used as a single part of a multi-part
00038 // bignum.
00039 typedef uint64_t integerPart;
00040 
00041 const unsigned int host_char_bit = 8;
00042 const unsigned int integerPartWidth =
00043     host_char_bit * static_cast<unsigned int>(sizeof(integerPart));
00044 
00045 //===----------------------------------------------------------------------===//
00046 //                              APInt Class
00047 //===----------------------------------------------------------------------===//
00048 
00049 /// \brief Class for arbitrary precision integers.
00050 ///
00051 /// APInt is a functional replacement for common case unsigned integer type like
00052 /// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
00053 /// integer sizes and large integer value types such as 3-bits, 15-bits, or more
00054 /// than 64-bits of precision. APInt provides a variety of arithmetic operators
00055 /// and methods to manipulate integer values of any bit-width. It supports both
00056 /// the typical integer arithmetic and comparison operations as well as bitwise
00057 /// manipulation.
00058 ///
00059 /// The class has several invariants worth noting:
00060 ///   * All bit, byte, and word positions are zero-based.
00061 ///   * Once the bit width is set, it doesn't change except by the Truncate,
00062 ///     SignExtend, or ZeroExtend operations.
00063 ///   * All binary operators must be on APInt instances of the same bit width.
00064 ///     Attempting to use these operators on instances with different bit
00065 ///     widths will yield an assertion.
00066 ///   * The value is stored canonically as an unsigned value. For operations
00067 ///     where it makes a difference, there are both signed and unsigned variants
00068 ///     of the operation. For example, sdiv and udiv. However, because the bit
00069 ///     widths must be the same, operations such as Mul and Add produce the same
00070 ///     results regardless of whether the values are interpreted as signed or
00071 ///     not.
00072 ///   * In general, the class tries to follow the style of computation that LLVM
00073 ///     uses in its IR. This simplifies its use for LLVM.
00074 ///
00075 class APInt {
00076   unsigned BitWidth; ///< The number of bits in this APInt.
00077 
00078   /// This union is used to store the integer value. When the
00079   /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
00080   union {
00081     uint64_t VAL;   ///< Used to store the <= 64 bits integer value.
00082     uint64_t *pVal; ///< Used to store the >64 bits integer value.
00083   };
00084 
00085   /// This enum is used to hold the constants we needed for APInt.
00086   enum {
00087     /// Bits in a word
00088     APINT_BITS_PER_WORD =
00089         static_cast<unsigned int>(sizeof(uint64_t)) * CHAR_BIT,
00090     /// Byte size of a word
00091     APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
00092   };
00093 
00094   /// \brief Fast internal constructor
00095   ///
00096   /// This constructor is used only internally for speed of construction of
00097   /// temporaries. It is unsafe for general use so it is not public.
00098   APInt(uint64_t *val, unsigned bits) : BitWidth(bits), pVal(val) {}
00099 
00100   /// \brief Determine if this APInt just has one word to store value.
00101   ///
00102   /// \returns true if the number of bits <= 64, false otherwise.
00103   bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; }
00104 
00105   /// \brief Determine which word a bit is in.
00106   ///
00107   /// \returns the word position for the specified bit position.
00108   static unsigned whichWord(unsigned bitPosition) {
00109     return bitPosition / APINT_BITS_PER_WORD;
00110   }
00111 
00112   /// \brief Determine which bit in a word a bit is in.
00113   ///
00114   /// \returns the bit position in a word for the specified bit position
00115   /// in the APInt.
00116   static unsigned whichBit(unsigned bitPosition) {
00117     return bitPosition % APINT_BITS_PER_WORD;
00118   }
00119 
00120   /// \brief Get a single bit mask.
00121   ///
00122   /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set
00123   /// This method generates and returns a uint64_t (word) mask for a single
00124   /// bit at a specific bit position. This is used to mask the bit in the
00125   /// corresponding word.
00126   static uint64_t maskBit(unsigned bitPosition) {
00127     return 1ULL << whichBit(bitPosition);
00128   }
00129 
00130   /// \brief Clear unused high order bits
00131   ///
00132   /// This method is used internally to clear the to "N" bits in the high order
00133   /// word that are not used by the APInt. This is needed after the most
00134   /// significant word is assigned a value to ensure that those bits are
00135   /// zero'd out.
00136   APInt &clearUnusedBits() {
00137     // Compute how many bits are used in the final word
00138     unsigned wordBits = BitWidth % APINT_BITS_PER_WORD;
00139     if (wordBits == 0)
00140       // If all bits are used, we want to leave the value alone. This also
00141       // avoids the undefined behavior of >> when the shift is the same size as
00142       // the word size (64).
00143       return *this;
00144 
00145     // Mask out the high bits.
00146     uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
00147     if (isSingleWord())
00148       VAL &= mask;
00149     else
00150       pVal[getNumWords() - 1] &= mask;
00151     return *this;
00152   }
00153 
00154   /// \brief Get the word corresponding to a bit position
00155   /// \returns the corresponding word for the specified bit position.
00156   uint64_t getWord(unsigned bitPosition) const {
00157     return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
00158   }
00159 
00160   /// \brief Convert a char array into an APInt
00161   ///
00162   /// \param radix 2, 8, 10, 16, or 36
00163   /// Converts a string into a number.  The string must be non-empty
00164   /// and well-formed as a number of the given base. The bit-width
00165   /// must be sufficient to hold the result.
00166   ///
00167   /// This is used by the constructors that take string arguments.
00168   ///
00169   /// StringRef::getAsInteger is superficially similar but (1) does
00170   /// not assume that the string is well-formed and (2) grows the
00171   /// result to hold the input.
00172   void fromString(unsigned numBits, StringRef str, uint8_t radix);
00173 
00174   /// \brief An internal division function for dividing APInts.
00175   ///
00176   /// This is used by the toString method to divide by the radix. It simply
00177   /// provides a more convenient form of divide for internal use since KnuthDiv
00178   /// has specific constraints on its inputs. If those constraints are not met
00179   /// then it provides a simpler form of divide.
00180   static void divide(const APInt LHS, unsigned lhsWords, const APInt &RHS,
00181                      unsigned rhsWords, APInt *Quotient, APInt *Remainder);
00182 
00183   /// out-of-line slow case for inline constructor
00184   void initSlowCase(unsigned numBits, uint64_t val, bool isSigned);
00185 
00186   /// shared code between two array constructors
00187   void initFromArray(ArrayRef<uint64_t> array);
00188 
00189   /// out-of-line slow case for inline copy constructor
00190   void initSlowCase(const APInt &that);
00191 
00192   /// out-of-line slow case for shl
00193   APInt shlSlowCase(unsigned shiftAmt) const;
00194 
00195   /// out-of-line slow case for operator&
00196   APInt AndSlowCase(const APInt &RHS) const;
00197 
00198   /// out-of-line slow case for operator|
00199   APInt OrSlowCase(const APInt &RHS) const;
00200 
00201   /// out-of-line slow case for operator^
00202   APInt XorSlowCase(const APInt &RHS) const;
00203 
00204   /// out-of-line slow case for operator=
00205   APInt &AssignSlowCase(const APInt &RHS);
00206 
00207   /// out-of-line slow case for operator==
00208   bool EqualSlowCase(const APInt &RHS) const;
00209 
00210   /// out-of-line slow case for operator==
00211   bool EqualSlowCase(uint64_t Val) const;
00212 
00213   /// out-of-line slow case for countLeadingZeros
00214   unsigned countLeadingZerosSlowCase() const;
00215 
00216   /// out-of-line slow case for countTrailingOnes
00217   unsigned countTrailingOnesSlowCase() const;
00218 
00219   /// out-of-line slow case for countPopulation
00220   unsigned countPopulationSlowCase() const;
00221 
00222 public:
00223   /// \name Constructors
00224   /// @{
00225 
00226   /// \brief Create a new APInt of numBits width, initialized as val.
00227   ///
00228   /// If isSigned is true then val is treated as if it were a signed value
00229   /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
00230   /// will be done. Otherwise, no sign extension occurs (high order bits beyond
00231   /// the range of val are zero filled).
00232   ///
00233   /// \param numBits the bit width of the constructed APInt
00234   /// \param val the initial value of the APInt
00235   /// \param isSigned how to treat signedness of val
00236   APInt(unsigned numBits, uint64_t val, bool isSigned = false)
00237       : BitWidth(numBits), VAL(0) {
00238     assert(BitWidth && "bitwidth too small");
00239     if (isSingleWord())
00240       VAL = val;
00241     else
00242       initSlowCase(numBits, val, isSigned);
00243     clearUnusedBits();
00244   }
00245 
00246   /// \brief Construct an APInt of numBits width, initialized as bigVal[].
00247   ///
00248   /// Note that bigVal.size() can be smaller or larger than the corresponding
00249   /// bit width but any extraneous bits will be dropped.
00250   ///
00251   /// \param numBits the bit width of the constructed APInt
00252   /// \param bigVal a sequence of words to form the initial value of the APInt
00253   APInt(unsigned numBits, ArrayRef<uint64_t> bigVal);
00254 
00255   /// Equivalent to APInt(numBits, ArrayRef<uint64_t>(bigVal, numWords)), but
00256   /// deprecated because this constructor is prone to ambiguity with the
00257   /// APInt(unsigned, uint64_t, bool) constructor.
00258   ///
00259   /// If this overload is ever deleted, care should be taken to prevent calls
00260   /// from being incorrectly captured by the APInt(unsigned, uint64_t, bool)
00261   /// constructor.
00262   APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
00263 
00264   /// \brief Construct an APInt from a string representation.
00265   ///
00266   /// This constructor interprets the string \p str in the given radix. The
00267   /// interpretation stops when the first character that is not suitable for the
00268   /// radix is encountered, or the end of the string. Acceptable radix values
00269   /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the
00270   /// string to require more bits than numBits.
00271   ///
00272   /// \param numBits the bit width of the constructed APInt
00273   /// \param str the string to be interpreted
00274   /// \param radix the radix to use for the conversion
00275   APInt(unsigned numBits, StringRef str, uint8_t radix);
00276 
00277   /// Simply makes *this a copy of that.
00278   /// @brief Copy Constructor.
00279   APInt(const APInt &that) : BitWidth(that.BitWidth), VAL(0) {
00280     assert(BitWidth && "bitwidth too small");
00281     if (isSingleWord())
00282       VAL = that.VAL;
00283     else
00284       initSlowCase(that);
00285   }
00286 
00287   /// \brief Move Constructor.
00288   APInt(APInt &&that) : BitWidth(that.BitWidth), VAL(that.VAL) {
00289     that.BitWidth = 0;
00290   }
00291 
00292   /// \brief Destructor.
00293   ~APInt() {
00294     if (needsCleanup())
00295       delete[] pVal;
00296   }
00297 
00298   /// \brief Default constructor that creates an uninitialized APInt.
00299   ///
00300   /// This is useful for object deserialization (pair this with the static
00301   ///  method Read).
00302   explicit APInt() : BitWidth(1) {}
00303 
00304   /// \brief Returns whether this instance allocated memory.
00305   bool needsCleanup() const { return !isSingleWord(); }
00306 
00307   /// Used to insert APInt objects, or objects that contain APInt objects, into
00308   ///  FoldingSets.
00309   void Profile(FoldingSetNodeID &id) const;
00310 
00311   /// @}
00312   /// \name Value Tests
00313   /// @{
00314 
00315   /// \brief Determine sign of this APInt.
00316   ///
00317   /// This tests the high bit of this APInt to determine if it is set.
00318   ///
00319   /// \returns true if this APInt is negative, false otherwise
00320   bool isNegative() const { return (*this)[BitWidth - 1]; }
00321 
00322   /// \brief Determine if this APInt Value is non-negative (>= 0)
00323   ///
00324   /// This tests the high bit of the APInt to determine if it is unset.
00325   bool isNonNegative() const { return !isNegative(); }
00326 
00327   /// \brief Determine if this APInt Value is positive.
00328   ///
00329   /// This tests if the value of this APInt is positive (> 0). Note
00330   /// that 0 is not a positive value.
00331   ///
00332   /// \returns true if this APInt is positive.
00333   bool isStrictlyPositive() const { return isNonNegative() && !!*this; }
00334 
00335   /// \brief Determine if all bits are set
00336   ///
00337   /// This checks to see if the value has all bits of the APInt are set or not.
00338   bool isAllOnesValue() const {
00339     if (isSingleWord())
00340       return VAL == ~integerPart(0) >> (APINT_BITS_PER_WORD - BitWidth);
00341     return countPopulationSlowCase() == BitWidth;
00342   }
00343 
00344   /// \brief Determine if this is the largest unsigned value.
00345   ///
00346   /// This checks to see if the value of this APInt is the maximum unsigned
00347   /// value for the APInt's bit width.
00348   bool isMaxValue() const { return isAllOnesValue(); }
00349 
00350   /// \brief Determine if this is the largest signed value.
00351   ///
00352   /// This checks to see if the value of this APInt is the maximum signed
00353   /// value for the APInt's bit width.
00354   bool isMaxSignedValue() const {
00355     return BitWidth == 1 ? VAL == 0
00356                          : !isNegative() && countPopulation() == BitWidth - 1;
00357   }
00358 
00359   /// \brief Determine if this is the smallest unsigned value.
00360   ///
00361   /// This checks to see if the value of this APInt is the minimum unsigned
00362   /// value for the APInt's bit width.
00363   bool isMinValue() const { return !*this; }
00364 
00365   /// \brief Determine if this is the smallest signed value.
00366   ///
00367   /// This checks to see if the value of this APInt is the minimum signed
00368   /// value for the APInt's bit width.
00369   bool isMinSignedValue() const {
00370     return BitWidth == 1 ? VAL == 1 : isNegative() && isPowerOf2();
00371   }
00372 
00373   /// \brief Check if this APInt has an N-bits unsigned integer value.
00374   bool isIntN(unsigned N) const {
00375     assert(N && "N == 0 ???");
00376     return getActiveBits() <= N;
00377   }
00378 
00379   /// \brief Check if this APInt has an N-bits signed integer value.
00380   bool isSignedIntN(unsigned N) const {
00381     assert(N && "N == 0 ???");
00382     return getMinSignedBits() <= N;
00383   }
00384 
00385   /// \brief Check if this APInt's value is a power of two greater than zero.
00386   ///
00387   /// \returns true if the argument APInt value is a power of two > 0.
00388   bool isPowerOf2() const {
00389     if (isSingleWord())
00390       return isPowerOf2_64(VAL);
00391     return countPopulationSlowCase() == 1;
00392   }
00393 
00394   /// \brief Check if the APInt's value is returned by getSignBit.
00395   ///
00396   /// \returns true if this is the value returned by getSignBit.
00397   bool isSignBit() const { return isMinSignedValue(); }
00398 
00399   /// \brief Convert APInt to a boolean value.
00400   ///
00401   /// This converts the APInt to a boolean value as a test against zero.
00402   bool getBoolValue() const { return !!*this; }
00403 
00404   /// If this value is smaller than the specified limit, return it, otherwise
00405   /// return the limit value.  This causes the value to saturate to the limit.
00406   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
00407     return (getActiveBits() > 64 || getZExtValue() > Limit) ? Limit
00408                                                             : getZExtValue();
00409   }
00410 
00411   /// @}
00412   /// \name Value Generators
00413   /// @{
00414 
00415   /// \brief Gets maximum unsigned value of APInt for specific bit width.
00416   static APInt getMaxValue(unsigned numBits) {
00417     return getAllOnesValue(numBits);
00418   }
00419 
00420   /// \brief Gets maximum signed value of APInt for a specific bit width.
00421   static APInt getSignedMaxValue(unsigned numBits) {
00422     APInt API = getAllOnesValue(numBits);
00423     API.clearBit(numBits - 1);
00424     return API;
00425   }
00426 
00427   /// \brief Gets minimum unsigned value of APInt for a specific bit width.
00428   static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); }
00429 
00430   /// \brief Gets minimum signed value of APInt for a specific bit width.
00431   static APInt getSignedMinValue(unsigned numBits) {
00432     APInt API(numBits, 0);
00433     API.setBit(numBits - 1);
00434     return API;
00435   }
00436 
00437   /// \brief Get the SignBit for a specific bit width.
00438   ///
00439   /// This is just a wrapper function of getSignedMinValue(), and it helps code
00440   /// readability when we want to get a SignBit.
00441   static APInt getSignBit(unsigned BitWidth) {
00442     return getSignedMinValue(BitWidth);
00443   }
00444 
00445   /// \brief Get the all-ones value.
00446   ///
00447   /// \returns the all-ones value for an APInt of the specified bit-width.
00448   static APInt getAllOnesValue(unsigned numBits) {
00449     return APInt(numBits, UINT64_MAX, true);
00450   }
00451 
00452   /// \brief Get the '0' value.
00453   ///
00454   /// \returns the '0' value for an APInt of the specified bit-width.
00455   static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); }
00456 
00457   /// \brief Compute an APInt containing numBits highbits from this APInt.
00458   ///
00459   /// Get an APInt with the same BitWidth as this APInt, just zero mask
00460   /// the low bits and right shift to the least significant bit.
00461   ///
00462   /// \returns the high "numBits" bits of this APInt.
00463   APInt getHiBits(unsigned numBits) const;
00464 
00465   /// \brief Compute an APInt containing numBits lowbits from this APInt.
00466   ///
00467   /// Get an APInt with the same BitWidth as this APInt, just zero mask
00468   /// the high bits.
00469   ///
00470   /// \returns the low "numBits" bits of this APInt.
00471   APInt getLoBits(unsigned numBits) const;
00472 
00473   /// \brief Return an APInt with exactly one bit set in the result.
00474   static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
00475     APInt Res(numBits, 0);
00476     Res.setBit(BitNo);
00477     return Res;
00478   }
00479 
00480   /// \brief Get a value with a block of bits set.
00481   ///
00482   /// Constructs an APInt value that has a contiguous range of bits set. The
00483   /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
00484   /// bits will be zero. For example, with parameters(32, 0, 16) you would get
00485   /// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For
00486   /// example, with parameters (32, 28, 4), you would get 0xF000000F.
00487   ///
00488   /// \param numBits the intended bit width of the result
00489   /// \param loBit the index of the lowest bit set.
00490   /// \param hiBit the index of the highest bit set.
00491   ///
00492   /// \returns An APInt value with the requested bits set.
00493   static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
00494     assert(hiBit <= numBits && "hiBit out of range");
00495     assert(loBit < numBits && "loBit out of range");
00496     if (hiBit < loBit)
00497       return getLowBitsSet(numBits, hiBit) |
00498              getHighBitsSet(numBits, numBits - loBit);
00499     return getLowBitsSet(numBits, hiBit - loBit).shl(loBit);
00500   }
00501 
00502   /// \brief Get a value with high bits set
00503   ///
00504   /// Constructs an APInt value that has the top hiBitsSet bits set.
00505   ///
00506   /// \param numBits the bitwidth of the result
00507   /// \param hiBitsSet the number of high-order bits set in the result.
00508   static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
00509     assert(hiBitsSet <= numBits && "Too many bits to set!");
00510     // Handle a degenerate case, to avoid shifting by word size
00511     if (hiBitsSet == 0)
00512       return APInt(numBits, 0);
00513     unsigned shiftAmt = numBits - hiBitsSet;
00514     // For small values, return quickly
00515     if (numBits <= APINT_BITS_PER_WORD)
00516       return APInt(numBits, ~0ULL << shiftAmt);
00517     return getAllOnesValue(numBits).shl(shiftAmt);
00518   }
00519 
00520   /// \brief Get a value with low bits set
00521   ///
00522   /// Constructs an APInt value that has the bottom loBitsSet bits set.
00523   ///
00524   /// \param numBits the bitwidth of the result
00525   /// \param loBitsSet the number of low-order bits set in the result.
00526   static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) {
00527     assert(loBitsSet <= numBits && "Too many bits to set!");
00528     // Handle a degenerate case, to avoid shifting by word size
00529     if (loBitsSet == 0)
00530       return APInt(numBits, 0);
00531     if (loBitsSet == APINT_BITS_PER_WORD)
00532       return APInt(numBits, UINT64_MAX);
00533     // For small values, return quickly.
00534     if (loBitsSet <= APINT_BITS_PER_WORD)
00535       return APInt(numBits, UINT64_MAX >> (APINT_BITS_PER_WORD - loBitsSet));
00536     return getAllOnesValue(numBits).lshr(numBits - loBitsSet);
00537   }
00538 
00539   /// \brief Return a value containing V broadcasted over NewLen bits.
00540   static APInt getSplat(unsigned NewLen, const APInt &V) {
00541     assert(NewLen >= V.getBitWidth() && "Can't splat to smaller bit width!");
00542 
00543     APInt Val = V.zextOrSelf(NewLen);
00544     for (unsigned I = V.getBitWidth(); I < NewLen; I <<= 1)
00545       Val |= Val << I;
00546 
00547     return Val;
00548   }
00549 
00550   /// \brief Determine if two APInts have the same value, after zero-extending
00551   /// one of them (if needed!) to ensure that the bit-widths match.
00552   static bool isSameValue(const APInt &I1, const APInt &I2) {
00553     if (I1.getBitWidth() == I2.getBitWidth())
00554       return I1 == I2;
00555 
00556     if (I1.getBitWidth() > I2.getBitWidth())
00557       return I1 == I2.zext(I1.getBitWidth());
00558 
00559     return I1.zext(I2.getBitWidth()) == I2;
00560   }
00561 
00562   /// \brief Overload to compute a hash_code for an APInt value.
00563   friend hash_code hash_value(const APInt &Arg);
00564 
00565   /// This function returns a pointer to the internal storage of the APInt.
00566   /// This is useful for writing out the APInt in binary form without any
00567   /// conversions.
00568   const uint64_t *getRawData() const {
00569     if (isSingleWord())
00570       return &VAL;
00571     return &pVal[0];
00572   }
00573 
00574   /// @}
00575   /// \name Unary Operators
00576   /// @{
00577 
00578   /// \brief Postfix increment operator.
00579   ///
00580   /// \returns a new APInt value representing *this incremented by one
00581   const APInt operator++(int) {
00582     APInt API(*this);
00583     ++(*this);
00584     return API;
00585   }
00586 
00587   /// \brief Prefix increment operator.
00588   ///
00589   /// \returns *this incremented by one
00590   APInt &operator++();
00591 
00592   /// \brief Postfix decrement operator.
00593   ///
00594   /// \returns a new APInt representing *this decremented by one.
00595   const APInt operator--(int) {
00596     APInt API(*this);
00597     --(*this);
00598     return API;
00599   }
00600 
00601   /// \brief Prefix decrement operator.
00602   ///
00603   /// \returns *this decremented by one.
00604   APInt &operator--();
00605 
00606   /// \brief Unary bitwise complement operator.
00607   ///
00608   /// Performs a bitwise complement operation on this APInt.
00609   ///
00610   /// \returns an APInt that is the bitwise complement of *this
00611   APInt operator~() const {
00612     APInt Result(*this);
00613     Result.flipAllBits();
00614     return Result;
00615   }
00616 
00617   /// \brief Unary negation operator
00618   ///
00619   /// Negates *this using two's complement logic.
00620   ///
00621   /// \returns An APInt value representing the negation of *this.
00622   APInt operator-() const { return APInt(BitWidth, 0) - (*this); }
00623 
00624   /// \brief Logical negation operator.
00625   ///
00626   /// Performs logical negation operation on this APInt.
00627   ///
00628   /// \returns true if *this is zero, false otherwise.
00629   bool operator!() const {
00630     if (isSingleWord())
00631       return !VAL;
00632 
00633     for (unsigned i = 0; i != getNumWords(); ++i)
00634       if (pVal[i])
00635         return false;
00636     return true;
00637   }
00638 
00639   /// @}
00640   /// \name Assignment Operators
00641   /// @{
00642 
00643   /// \brief Copy assignment operator.
00644   ///
00645   /// \returns *this after assignment of RHS.
00646   APInt &operator=(const APInt &RHS) {
00647     // If the bitwidths are the same, we can avoid mucking with memory
00648     if (isSingleWord() && RHS.isSingleWord()) {
00649       VAL = RHS.VAL;
00650       BitWidth = RHS.BitWidth;
00651       return clearUnusedBits();
00652     }
00653 
00654     return AssignSlowCase(RHS);
00655   }
00656 
00657   /// @brief Move assignment operator.
00658   APInt &operator=(APInt &&that) {
00659     if (!isSingleWord()) {
00660       // The MSVC STL shipped in 2013 requires that self move assignment be a
00661       // no-op.  Otherwise algorithms like stable_sort will produce answers
00662       // where half of the output is left in a moved-from state.
00663       if (this == &that)
00664         return *this;
00665       delete[] pVal;
00666     }
00667 
00668     // Use memcpy so that type based alias analysis sees both VAL and pVal
00669     // as modified.
00670     memcpy(&VAL, &that.VAL, sizeof(uint64_t));
00671 
00672     // If 'this == &that', avoid zeroing our own bitwidth by storing to 'that'
00673     // first.
00674     unsigned ThatBitWidth = that.BitWidth;
00675     that.BitWidth = 0;
00676     BitWidth = ThatBitWidth;
00677 
00678     return *this;
00679   }
00680 
00681   /// \brief Assignment operator.
00682   ///
00683   /// The RHS value is assigned to *this. If the significant bits in RHS exceed
00684   /// the bit width, the excess bits are truncated. If the bit width is larger
00685   /// than 64, the value is zero filled in the unspecified high order bits.
00686   ///
00687   /// \returns *this after assignment of RHS value.
00688   APInt &operator=(uint64_t RHS);
00689 
00690   /// \brief Bitwise AND assignment operator.
00691   ///
00692   /// Performs a bitwise AND operation on this APInt and RHS. The result is
00693   /// assigned to *this.
00694   ///
00695   /// \returns *this after ANDing with RHS.
00696   APInt &operator&=(const APInt &RHS);
00697 
00698   /// \brief Bitwise OR assignment operator.
00699   ///
00700   /// Performs a bitwise OR operation on this APInt and RHS. The result is
00701   /// assigned *this;
00702   ///
00703   /// \returns *this after ORing with RHS.
00704   APInt &operator|=(const APInt &RHS);
00705 
00706   /// \brief Bitwise OR assignment operator.
00707   ///
00708   /// Performs a bitwise OR operation on this APInt and RHS. RHS is
00709   /// logically zero-extended or truncated to match the bit-width of
00710   /// the LHS.
00711   APInt &operator|=(uint64_t RHS) {
00712     if (isSingleWord()) {
00713       VAL |= RHS;
00714       clearUnusedBits();
00715     } else {
00716       pVal[0] |= RHS;
00717     }
00718     return *this;
00719   }
00720 
00721   /// \brief Bitwise XOR assignment operator.
00722   ///
00723   /// Performs a bitwise XOR operation on this APInt and RHS. The result is
00724   /// assigned to *this.
00725   ///
00726   /// \returns *this after XORing with RHS.
00727   APInt &operator^=(const APInt &RHS);
00728 
00729   /// \brief Multiplication assignment operator.
00730   ///
00731   /// Multiplies this APInt by RHS and assigns the result to *this.
00732   ///
00733   /// \returns *this
00734   APInt &operator*=(const APInt &RHS);
00735 
00736   /// \brief Addition assignment operator.
00737   ///
00738   /// Adds RHS to *this and assigns the result to *this.
00739   ///
00740   /// \returns *this
00741   APInt &operator+=(const APInt &RHS);
00742 
00743   /// \brief Subtraction assignment operator.
00744   ///
00745   /// Subtracts RHS from *this and assigns the result to *this.
00746   ///
00747   /// \returns *this
00748   APInt &operator-=(const APInt &RHS);
00749 
00750   /// \brief Left-shift assignment function.
00751   ///
00752   /// Shifts *this left by shiftAmt and assigns the result to *this.
00753   ///
00754   /// \returns *this after shifting left by shiftAmt
00755   APInt &operator<<=(unsigned shiftAmt) {
00756     *this = shl(shiftAmt);
00757     return *this;
00758   }
00759 
00760   /// @}
00761   /// \name Binary Operators
00762   /// @{
00763 
00764   /// \brief Bitwise AND operator.
00765   ///
00766   /// Performs a bitwise AND operation on *this and RHS.
00767   ///
00768   /// \returns An APInt value representing the bitwise AND of *this and RHS.
00769   APInt operator&(const APInt &RHS) const {
00770     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
00771     if (isSingleWord())
00772       return APInt(getBitWidth(), VAL & RHS.VAL);
00773     return AndSlowCase(RHS);
00774   }
00775   APInt LLVM_ATTRIBUTE_UNUSED_RESULT And(const APInt &RHS) const {
00776     return this->operator&(RHS);
00777   }
00778 
00779   /// \brief Bitwise OR operator.
00780   ///
00781   /// Performs a bitwise OR operation on *this and RHS.
00782   ///
00783   /// \returns An APInt value representing the bitwise OR of *this and RHS.
00784   APInt operator|(const APInt &RHS) const {
00785     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
00786     if (isSingleWord())
00787       return APInt(getBitWidth(), VAL | RHS.VAL);
00788     return OrSlowCase(RHS);
00789   }
00790 
00791   /// \brief Bitwise OR function.
00792   ///
00793   /// Performs a bitwise or on *this and RHS. This is implemented bny simply
00794   /// calling operator|.
00795   ///
00796   /// \returns An APInt value representing the bitwise OR of *this and RHS.
00797   APInt LLVM_ATTRIBUTE_UNUSED_RESULT Or(const APInt &RHS) const {
00798     return this->operator|(RHS);
00799   }
00800 
00801   /// \brief Bitwise XOR operator.
00802   ///
00803   /// Performs a bitwise XOR operation on *this and RHS.
00804   ///
00805   /// \returns An APInt value representing the bitwise XOR of *this and RHS.
00806   APInt operator^(const APInt &RHS) const {
00807     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
00808     if (isSingleWord())
00809       return APInt(BitWidth, VAL ^ RHS.VAL);
00810     return XorSlowCase(RHS);
00811   }
00812 
00813   /// \brief Bitwise XOR function.
00814   ///
00815   /// Performs a bitwise XOR operation on *this and RHS. This is implemented
00816   /// through the usage of operator^.
00817   ///
00818   /// \returns An APInt value representing the bitwise XOR of *this and RHS.
00819   APInt LLVM_ATTRIBUTE_UNUSED_RESULT Xor(const APInt &RHS) const {
00820     return this->operator^(RHS);
00821   }
00822 
00823   /// \brief Multiplication operator.
00824   ///
00825   /// Multiplies this APInt by RHS and returns the result.
00826   APInt operator*(const APInt &RHS) const;
00827 
00828   /// \brief Addition operator.
00829   ///
00830   /// Adds RHS to this APInt and returns the result.
00831   APInt operator+(const APInt &RHS) const;
00832   APInt operator+(uint64_t RHS) const { return (*this) + APInt(BitWidth, RHS); }
00833 
00834   /// \brief Subtraction operator.
00835   ///
00836   /// Subtracts RHS from this APInt and returns the result.
00837   APInt operator-(const APInt &RHS) const;
00838   APInt operator-(uint64_t RHS) const { return (*this) - APInt(BitWidth, RHS); }
00839 
00840   /// \brief Left logical shift operator.
00841   ///
00842   /// Shifts this APInt left by \p Bits and returns the result.
00843   APInt operator<<(unsigned Bits) const { return shl(Bits); }
00844 
00845   /// \brief Left logical shift operator.
00846   ///
00847   /// Shifts this APInt left by \p Bits and returns the result.
00848   APInt operator<<(const APInt &Bits) const { return shl(Bits); }
00849 
00850   /// \brief Arithmetic right-shift function.
00851   ///
00852   /// Arithmetic right-shift this APInt by shiftAmt.
00853   APInt LLVM_ATTRIBUTE_UNUSED_RESULT ashr(unsigned shiftAmt) const;
00854 
00855   /// \brief Logical right-shift function.
00856   ///
00857   /// Logical right-shift this APInt by shiftAmt.
00858   APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const;
00859 
00860   /// \brief Left-shift function.
00861   ///
00862   /// Left-shift this APInt by shiftAmt.
00863   APInt LLVM_ATTRIBUTE_UNUSED_RESULT shl(unsigned shiftAmt) const {
00864     assert(shiftAmt <= BitWidth && "Invalid shift amount");
00865     if (isSingleWord()) {
00866       if (shiftAmt >= BitWidth)
00867         return APInt(BitWidth, 0); // avoid undefined shift results
00868       return APInt(BitWidth, VAL << shiftAmt);
00869     }
00870     return shlSlowCase(shiftAmt);
00871   }
00872 
00873   /// \brief Rotate left by rotateAmt.
00874   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotl(unsigned rotateAmt) const;
00875 
00876   /// \brief Rotate right by rotateAmt.
00877   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotr(unsigned rotateAmt) const;
00878 
00879   /// \brief Arithmetic right-shift function.
00880   ///
00881   /// Arithmetic right-shift this APInt by shiftAmt.
00882   APInt LLVM_ATTRIBUTE_UNUSED_RESULT ashr(const APInt &shiftAmt) const;
00883 
00884   /// \brief Logical right-shift function.
00885   ///
00886   /// Logical right-shift this APInt by shiftAmt.
00887   APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(const APInt &shiftAmt) const;
00888 
00889   /// \brief Left-shift function.
00890   ///
00891   /// Left-shift this APInt by shiftAmt.
00892   APInt LLVM_ATTRIBUTE_UNUSED_RESULT shl(const APInt &shiftAmt) const;
00893 
00894   /// \brief Rotate left by rotateAmt.
00895   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotl(const APInt &rotateAmt) const;
00896 
00897   /// \brief Rotate right by rotateAmt.
00898   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotr(const APInt &rotateAmt) const;
00899 
00900   /// \brief Unsigned division operation.
00901   ///
00902   /// Perform an unsigned divide operation on this APInt by RHS. Both this and
00903   /// RHS are treated as unsigned quantities for purposes of this division.
00904   ///
00905   /// \returns a new APInt value containing the division result
00906   APInt LLVM_ATTRIBUTE_UNUSED_RESULT udiv(const APInt &RHS) const;
00907 
00908   /// \brief Signed division function for APInt.
00909   ///
00910   /// Signed divide this APInt by APInt RHS.
00911   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sdiv(const APInt &RHS) const;
00912 
00913   /// \brief Unsigned remainder operation.
00914   ///
00915   /// Perform an unsigned remainder operation on this APInt with RHS being the
00916   /// divisor. Both this and RHS are treated as unsigned quantities for purposes
00917   /// of this operation. Note that this is a true remainder operation and not a
00918   /// modulo operation because the sign follows the sign of the dividend which
00919   /// is *this.
00920   ///
00921   /// \returns a new APInt value containing the remainder result
00922   APInt LLVM_ATTRIBUTE_UNUSED_RESULT urem(const APInt &RHS) const;
00923 
00924   /// \brief Function for signed remainder operation.
00925   ///
00926   /// Signed remainder operation on APInt.
00927   APInt LLVM_ATTRIBUTE_UNUSED_RESULT srem(const APInt &RHS) const;
00928 
00929   /// \brief Dual division/remainder interface.
00930   ///
00931   /// Sometimes it is convenient to divide two APInt values and obtain both the
00932   /// quotient and remainder. This function does both operations in the same
00933   /// computation making it a little more efficient. The pair of input arguments
00934   /// may overlap with the pair of output arguments. It is safe to call
00935   /// udivrem(X, Y, X, Y), for example.
00936   static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
00937                       APInt &Remainder);
00938 
00939   static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
00940                       APInt &Remainder);
00941 
00942   // Operations that return overflow indicators.
00943   APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
00944   APInt uadd_ov(const APInt &RHS, bool &Overflow) const;
00945   APInt ssub_ov(const APInt &RHS, bool &Overflow) const;
00946   APInt usub_ov(const APInt &RHS, bool &Overflow) const;
00947   APInt sdiv_ov(const APInt &RHS, bool &Overflow) const;
00948   APInt smul_ov(const APInt &RHS, bool &Overflow) const;
00949   APInt umul_ov(const APInt &RHS, bool &Overflow) const;
00950   APInt sshl_ov(const APInt &Amt, bool &Overflow) const;
00951   APInt ushl_ov(const APInt &Amt, bool &Overflow) const;
00952 
00953   /// \brief Array-indexing support.
00954   ///
00955   /// \returns the bit value at bitPosition
00956   bool operator[](unsigned bitPosition) const {
00957     assert(bitPosition < getBitWidth() && "Bit position out of bounds!");
00958     return (maskBit(bitPosition) &
00959             (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) !=
00960            0;
00961   }
00962 
00963   /// @}
00964   /// \name Comparison Operators
00965   /// @{
00966 
00967   /// \brief Equality operator.
00968   ///
00969   /// Compares this APInt with RHS for the validity of the equality
00970   /// relationship.
00971   bool operator==(const APInt &RHS) const {
00972     assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
00973     if (isSingleWord())
00974       return VAL == RHS.VAL;
00975     return EqualSlowCase(RHS);
00976   }
00977 
00978   /// \brief Equality operator.
00979   ///
00980   /// Compares this APInt with a uint64_t for the validity of the equality
00981   /// relationship.
00982   ///
00983   /// \returns true if *this == Val
00984   bool operator==(uint64_t Val) const {
00985     if (isSingleWord())
00986       return VAL == Val;
00987     return EqualSlowCase(Val);
00988   }
00989 
00990   /// \brief Equality comparison.
00991   ///
00992   /// Compares this APInt with RHS for the validity of the equality
00993   /// relationship.
00994   ///
00995   /// \returns true if *this == Val
00996   bool eq(const APInt &RHS) const { return (*this) == RHS; }
00997 
00998   /// \brief Inequality operator.
00999   ///
01000   /// Compares this APInt with RHS for the validity of the inequality
01001   /// relationship.
01002   ///
01003   /// \returns true if *this != Val
01004   bool operator!=(const APInt &RHS) const { return !((*this) == RHS); }
01005 
01006   /// \brief Inequality operator.
01007   ///
01008   /// Compares this APInt with a uint64_t for the validity of the inequality
01009   /// relationship.
01010   ///
01011   /// \returns true if *this != Val
01012   bool operator!=(uint64_t Val) const { return !((*this) == Val); }
01013 
01014   /// \brief Inequality comparison
01015   ///
01016   /// Compares this APInt with RHS for the validity of the inequality
01017   /// relationship.
01018   ///
01019   /// \returns true if *this != Val
01020   bool ne(const APInt &RHS) const { return !((*this) == RHS); }
01021 
01022   /// \brief Unsigned less than comparison
01023   ///
01024   /// Regards both *this and RHS as unsigned quantities and compares them for
01025   /// the validity of the less-than relationship.
01026   ///
01027   /// \returns true if *this < RHS when both are considered unsigned.
01028   bool ult(const APInt &RHS) const;
01029 
01030   /// \brief Unsigned less than comparison
01031   ///
01032   /// Regards both *this as an unsigned quantity and compares it with RHS for
01033   /// the validity of the less-than relationship.
01034   ///
01035   /// \returns true if *this < RHS when considered unsigned.
01036   bool ult(uint64_t RHS) const { return ult(APInt(getBitWidth(), RHS)); }
01037 
01038   /// \brief Signed less than comparison
01039   ///
01040   /// Regards both *this and RHS as signed quantities and compares them for
01041   /// validity of the less-than relationship.
01042   ///
01043   /// \returns true if *this < RHS when both are considered signed.
01044   bool slt(const APInt &RHS) const;
01045 
01046   /// \brief Signed less than comparison
01047   ///
01048   /// Regards both *this as a signed quantity and compares it with RHS for
01049   /// the validity of the less-than relationship.
01050   ///
01051   /// \returns true if *this < RHS when considered signed.
01052   bool slt(uint64_t RHS) const { return slt(APInt(getBitWidth(), RHS)); }
01053 
01054   /// \brief Unsigned less or equal comparison
01055   ///
01056   /// Regards both *this and RHS as unsigned quantities and compares them for
01057   /// validity of the less-or-equal relationship.
01058   ///
01059   /// \returns true if *this <= RHS when both are considered unsigned.
01060   bool ule(const APInt &RHS) const { return ult(RHS) || eq(RHS); }
01061 
01062   /// \brief Unsigned less or equal comparison
01063   ///
01064   /// Regards both *this as an unsigned quantity and compares it with RHS for
01065   /// the validity of the less-or-equal relationship.
01066   ///
01067   /// \returns true if *this <= RHS when considered unsigned.
01068   bool ule(uint64_t RHS) const { return ule(APInt(getBitWidth(), RHS)); }
01069 
01070   /// \brief Signed less or equal comparison
01071   ///
01072   /// Regards both *this and RHS as signed quantities and compares them for
01073   /// validity of the less-or-equal relationship.
01074   ///
01075   /// \returns true if *this <= RHS when both are considered signed.
01076   bool sle(const APInt &RHS) const { return slt(RHS) || eq(RHS); }
01077 
01078   /// \brief Signed less or equal comparison
01079   ///
01080   /// Regards both *this as a signed quantity and compares it with RHS for the
01081   /// validity of the less-or-equal relationship.
01082   ///
01083   /// \returns true if *this <= RHS when considered signed.
01084   bool sle(uint64_t RHS) const { return sle(APInt(getBitWidth(), RHS)); }
01085 
01086   /// \brief Unsigned greather than comparison
01087   ///
01088   /// Regards both *this and RHS as unsigned quantities and compares them for
01089   /// the validity of the greater-than relationship.
01090   ///
01091   /// \returns true if *this > RHS when both are considered unsigned.
01092   bool ugt(const APInt &RHS) const { return !ult(RHS) && !eq(RHS); }
01093 
01094   /// \brief Unsigned greater than comparison
01095   ///
01096   /// Regards both *this as an unsigned quantity and compares it with RHS for
01097   /// the validity of the greater-than relationship.
01098   ///
01099   /// \returns true if *this > RHS when considered unsigned.
01100   bool ugt(uint64_t RHS) const { return ugt(APInt(getBitWidth(), RHS)); }
01101 
01102   /// \brief Signed greather than comparison
01103   ///
01104   /// Regards both *this and RHS as signed quantities and compares them for the
01105   /// validity of the greater-than relationship.
01106   ///
01107   /// \returns true if *this > RHS when both are considered signed.
01108   bool sgt(const APInt &RHS) const { return !slt(RHS) && !eq(RHS); }
01109 
01110   /// \brief Signed greater than comparison
01111   ///
01112   /// Regards both *this as a signed quantity and compares it with RHS for
01113   /// the validity of the greater-than relationship.
01114   ///
01115   /// \returns true if *this > RHS when considered signed.
01116   bool sgt(uint64_t RHS) const { return sgt(APInt(getBitWidth(), RHS)); }
01117 
01118   /// \brief Unsigned greater or equal comparison
01119   ///
01120   /// Regards both *this and RHS as unsigned quantities and compares them for
01121   /// validity of the greater-or-equal relationship.
01122   ///
01123   /// \returns true if *this >= RHS when both are considered unsigned.
01124   bool uge(const APInt &RHS) const { return !ult(RHS); }
01125 
01126   /// \brief Unsigned greater or equal comparison
01127   ///
01128   /// Regards both *this as an unsigned quantity and compares it with RHS for
01129   /// the validity of the greater-or-equal relationship.
01130   ///
01131   /// \returns true if *this >= RHS when considered unsigned.
01132   bool uge(uint64_t RHS) const { return uge(APInt(getBitWidth(), RHS)); }
01133 
01134   /// \brief Signed greather or equal comparison
01135   ///
01136   /// Regards both *this and RHS as signed quantities and compares them for
01137   /// validity of the greater-or-equal relationship.
01138   ///
01139   /// \returns true if *this >= RHS when both are considered signed.
01140   bool sge(const APInt &RHS) const { return !slt(RHS); }
01141 
01142   /// \brief Signed greater or equal comparison
01143   ///
01144   /// Regards both *this as a signed quantity and compares it with RHS for
01145   /// the validity of the greater-or-equal relationship.
01146   ///
01147   /// \returns true if *this >= RHS when considered signed.
01148   bool sge(uint64_t RHS) const { return sge(APInt(getBitWidth(), RHS)); }
01149 
01150   /// This operation tests if there are any pairs of corresponding bits
01151   /// between this APInt and RHS that are both set.
01152   bool intersects(const APInt &RHS) const { return (*this & RHS) != 0; }
01153 
01154   /// @}
01155   /// \name Resizing Operators
01156   /// @{
01157 
01158   /// \brief Truncate to new width.
01159   ///
01160   /// Truncate the APInt to a specified width. It is an error to specify a width
01161   /// that is greater than or equal to the current width.
01162   APInt LLVM_ATTRIBUTE_UNUSED_RESULT trunc(unsigned width) const;
01163 
01164   /// \brief Sign extend to a new width.
01165   ///
01166   /// This operation sign extends the APInt to a new width. If the high order
01167   /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
01168   /// It is an error to specify a width that is less than or equal to the
01169   /// current width.
01170   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sext(unsigned width) const;
01171 
01172   /// \brief Zero extend to a new width.
01173   ///
01174   /// This operation zero extends the APInt to a new width. The high order bits
01175   /// are filled with 0 bits.  It is an error to specify a width that is less
01176   /// than or equal to the current width.
01177   APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const;
01178 
01179   /// \brief Sign extend or truncate to width
01180   ///
01181   /// Make this APInt have the bit width given by \p width. The value is sign
01182   /// extended, truncated, or left alone to make it that width.
01183   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sextOrTrunc(unsigned width) const;
01184 
01185   /// \brief Zero extend or truncate to width
01186   ///
01187   /// Make this APInt have the bit width given by \p width. The value is zero
01188   /// extended, truncated, or left alone to make it that width.
01189   APInt LLVM_ATTRIBUTE_UNUSED_RESULT zextOrTrunc(unsigned width) const;
01190 
01191   /// \brief Sign extend or truncate to width
01192   ///
01193   /// Make this APInt have the bit width given by \p width. The value is sign
01194   /// extended, or left alone to make it that width.
01195   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sextOrSelf(unsigned width) const;
01196 
01197   /// \brief Zero extend or truncate to width
01198   ///
01199   /// Make this APInt have the bit width given by \p width. The value is zero
01200   /// extended, or left alone to make it that width.
01201   APInt LLVM_ATTRIBUTE_UNUSED_RESULT zextOrSelf(unsigned width) const;
01202 
01203   /// @}
01204   /// \name Bit Manipulation Operators
01205   /// @{
01206 
01207   /// \brief Set every bit to 1.
01208   void setAllBits() {
01209     if (isSingleWord())
01210       VAL = UINT64_MAX;
01211     else {
01212       // Set all the bits in all the words.
01213       for (unsigned i = 0; i < getNumWords(); ++i)
01214         pVal[i] = UINT64_MAX;
01215     }
01216     // Clear the unused ones
01217     clearUnusedBits();
01218   }
01219 
01220   /// \brief Set a given bit to 1.
01221   ///
01222   /// Set the given bit to 1 whose position is given as "bitPosition".
01223   void setBit(unsigned bitPosition);
01224 
01225   /// \brief Set every bit to 0.
01226   void clearAllBits() {
01227     if (isSingleWord())
01228       VAL = 0;
01229     else
01230       memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
01231   }
01232 
01233   /// \brief Set a given bit to 0.
01234   ///
01235   /// Set the given bit to 0 whose position is given as "bitPosition".
01236   void clearBit(unsigned bitPosition);
01237 
01238   /// \brief Toggle every bit to its opposite value.
01239   void flipAllBits() {
01240     if (isSingleWord())
01241       VAL ^= UINT64_MAX;
01242     else {
01243       for (unsigned i = 0; i < getNumWords(); ++i)
01244         pVal[i] ^= UINT64_MAX;
01245     }
01246     clearUnusedBits();
01247   }
01248 
01249   /// \brief Toggles a given bit to its opposite value.
01250   ///
01251   /// Toggle a given bit to its opposite value whose position is given
01252   /// as "bitPosition".
01253   void flipBit(unsigned bitPosition);
01254 
01255   /// @}
01256   /// \name Value Characterization Functions
01257   /// @{
01258 
01259   /// \brief Return the number of bits in the APInt.
01260   unsigned getBitWidth() const { return BitWidth; }
01261 
01262   /// \brief Get the number of words.
01263   ///
01264   /// Here one word's bitwidth equals to that of uint64_t.
01265   ///
01266   /// \returns the number of words to hold the integer value of this APInt.
01267   unsigned getNumWords() const { return getNumWords(BitWidth); }
01268 
01269   /// \brief Get the number of words.
01270   ///
01271   /// *NOTE* Here one word's bitwidth equals to that of uint64_t.
01272   ///
01273   /// \returns the number of words to hold the integer value with a given bit
01274   /// width.
01275   static unsigned getNumWords(unsigned BitWidth) {
01276     return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
01277   }
01278 
01279   /// \brief Compute the number of active bits in the value
01280   ///
01281   /// This function returns the number of active bits which is defined as the
01282   /// bit width minus the number of leading zeros. This is used in several
01283   /// computations to see how "wide" the value is.
01284   unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); }
01285 
01286   /// \brief Compute the number of active words in the value of this APInt.
01287   ///
01288   /// This is used in conjunction with getActiveData to extract the raw value of
01289   /// the APInt.
01290   unsigned getActiveWords() const {
01291     unsigned numActiveBits = getActiveBits();
01292     return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1;
01293   }
01294 
01295   /// \brief Get the minimum bit size for this signed APInt
01296   ///
01297   /// Computes the minimum bit width for this APInt while considering it to be a
01298   /// signed (and probably negative) value. If the value is not negative, this
01299   /// function returns the same value as getActiveBits()+1. Otherwise, it
01300   /// returns the smallest bit width that will retain the negative value. For
01301   /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
01302   /// for -1, this function will always return 1.
01303   unsigned getMinSignedBits() const {
01304     if (isNegative())
01305       return BitWidth - countLeadingOnes() + 1;
01306     return getActiveBits() + 1;
01307   }
01308 
01309   /// \brief Get zero extended value
01310   ///
01311   /// This method attempts to return the value of this APInt as a zero extended
01312   /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
01313   /// uint64_t. Otherwise an assertion will result.
01314   uint64_t getZExtValue() const {
01315     if (isSingleWord())
01316       return VAL;
01317     assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
01318     return pVal[0];
01319   }
01320 
01321   /// \brief Get sign extended value
01322   ///
01323   /// This method attempts to return the value of this APInt as a sign extended
01324   /// int64_t. The bit width must be <= 64 or the value must fit within an
01325   /// int64_t. Otherwise an assertion will result.
01326   int64_t getSExtValue() const {
01327     if (isSingleWord())
01328       return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >>
01329              (APINT_BITS_PER_WORD - BitWidth);
01330     assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
01331     return int64_t(pVal[0]);
01332   }
01333 
01334   /// \brief Get bits required for string value.
01335   ///
01336   /// This method determines how many bits are required to hold the APInt
01337   /// equivalent of the string given by \p str.
01338   static unsigned getBitsNeeded(StringRef str, uint8_t radix);
01339 
01340   /// \brief The APInt version of the countLeadingZeros functions in
01341   ///   MathExtras.h.
01342   ///
01343   /// It counts the number of zeros from the most significant bit to the first
01344   /// one bit.
01345   ///
01346   /// \returns BitWidth if the value is zero, otherwise returns the number of
01347   ///   zeros from the most significant bit to the first one bits.
01348   unsigned countLeadingZeros() const {
01349     if (isSingleWord()) {
01350       unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
01351       return llvm::countLeadingZeros(VAL) - unusedBits;
01352     }
01353     return countLeadingZerosSlowCase();
01354   }
01355 
01356   /// \brief Count the number of leading one bits.
01357   ///
01358   /// This function is an APInt version of the countLeadingOnes_{32,64}
01359   /// functions in MathExtras.h. It counts the number of ones from the most
01360   /// significant bit to the first zero bit.
01361   ///
01362   /// \returns 0 if the high order bit is not set, otherwise returns the number
01363   /// of 1 bits from the most significant to the least
01364   unsigned countLeadingOnes() const;
01365 
01366   /// Computes the number of leading bits of this APInt that are equal to its
01367   /// sign bit.
01368   unsigned getNumSignBits() const {
01369     return isNegative() ? countLeadingOnes() : countLeadingZeros();
01370   }
01371 
01372   /// \brief Count the number of trailing zero bits.
01373   ///
01374   /// This function is an APInt version of the countTrailingZeros_{32,64}
01375   /// functions in MathExtras.h. It counts the number of zeros from the least
01376   /// significant bit to the first set bit.
01377   ///
01378   /// \returns BitWidth if the value is zero, otherwise returns the number of
01379   /// zeros from the least significant bit to the first one bit.
01380   unsigned countTrailingZeros() const;
01381 
01382   /// \brief Count the number of trailing one bits.
01383   ///
01384   /// This function is an APInt version of the countTrailingOnes_{32,64}
01385   /// functions in MathExtras.h. It counts the number of ones from the least
01386   /// significant bit to the first zero bit.
01387   ///
01388   /// \returns BitWidth if the value is all ones, otherwise returns the number
01389   /// of ones from the least significant bit to the first zero bit.
01390   unsigned countTrailingOnes() const {
01391     if (isSingleWord())
01392       return CountTrailingOnes_64(VAL);
01393     return countTrailingOnesSlowCase();
01394   }
01395 
01396   /// \brief Count the number of bits set.
01397   ///
01398   /// This function is an APInt version of the countPopulation_{32,64} functions
01399   /// in MathExtras.h. It counts the number of 1 bits in the APInt value.
01400   ///
01401   /// \returns 0 if the value is zero, otherwise returns the number of set bits.
01402   unsigned countPopulation() const {
01403     if (isSingleWord())
01404       return CountPopulation_64(VAL);
01405     return countPopulationSlowCase();
01406   }
01407 
01408   /// @}
01409   /// \name Conversion Functions
01410   /// @{
01411   void print(raw_ostream &OS, bool isSigned) const;
01412 
01413   /// Converts an APInt to a string and append it to Str.  Str is commonly a
01414   /// SmallString.
01415   void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
01416                 bool formatAsCLiteral = false) const;
01417 
01418   /// Considers the APInt to be unsigned and converts it into a string in the
01419   /// radix given. The radix can be 2, 8, 10 16, or 36.
01420   void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
01421     toString(Str, Radix, false, false);
01422   }
01423 
01424   /// Considers the APInt to be signed and converts it into a string in the
01425   /// radix given. The radix can be 2, 8, 10, 16, or 36.
01426   void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
01427     toString(Str, Radix, true, false);
01428   }
01429 
01430   /// \brief Return the APInt as a std::string.
01431   ///
01432   /// Note that this is an inefficient method.  It is better to pass in a
01433   /// SmallVector/SmallString to the methods above to avoid thrashing the heap
01434   /// for the string.
01435   std::string toString(unsigned Radix, bool Signed) const;
01436 
01437   /// \returns a byte-swapped representation of this APInt Value.
01438   APInt LLVM_ATTRIBUTE_UNUSED_RESULT byteSwap() const;
01439 
01440   /// \brief Converts this APInt to a double value.
01441   double roundToDouble(bool isSigned) const;
01442 
01443   /// \brief Converts this unsigned APInt to a double value.
01444   double roundToDouble() const { return roundToDouble(false); }
01445 
01446   /// \brief Converts this signed APInt to a double value.
01447   double signedRoundToDouble() const { return roundToDouble(true); }
01448 
01449   /// \brief Converts APInt bits to a double
01450   ///
01451   /// The conversion does not do a translation from integer to double, it just
01452   /// re-interprets the bits as a double. Note that it is valid to do this on
01453   /// any bit width. Exactly 64 bits will be translated.
01454   double bitsToDouble() const {
01455     union {
01456       uint64_t I;
01457       double D;
01458     } T;
01459     T.I = (isSingleWord() ? VAL : pVal[0]);
01460     return T.D;
01461   }
01462 
01463   /// \brief Converts APInt bits to a double
01464   ///
01465   /// The conversion does not do a translation from integer to float, it just
01466   /// re-interprets the bits as a float. Note that it is valid to do this on
01467   /// any bit width. Exactly 32 bits will be translated.
01468   float bitsToFloat() const {
01469     union {
01470       unsigned I;
01471       float F;
01472     } T;
01473     T.I = unsigned((isSingleWord() ? VAL : pVal[0]));
01474     return T.F;
01475   }
01476 
01477   /// \brief Converts a double to APInt bits.
01478   ///
01479   /// The conversion does not do a translation from double to integer, it just
01480   /// re-interprets the bits of the double.
01481   static APInt LLVM_ATTRIBUTE_UNUSED_RESULT doubleToBits(double V) {
01482     union {
01483       uint64_t I;
01484       double D;
01485     } T;
01486     T.D = V;
01487     return APInt(sizeof T * CHAR_BIT, T.I);
01488   }
01489 
01490   /// \brief Converts a float to APInt bits.
01491   ///
01492   /// The conversion does not do a translation from float to integer, it just
01493   /// re-interprets the bits of the float.
01494   static APInt LLVM_ATTRIBUTE_UNUSED_RESULT floatToBits(float V) {
01495     union {
01496       unsigned I;
01497       float F;
01498     } T;
01499     T.F = V;
01500     return APInt(sizeof T * CHAR_BIT, T.I);
01501   }
01502 
01503   /// @}
01504   /// \name Mathematics Operations
01505   /// @{
01506 
01507   /// \returns the floor log base 2 of this APInt.
01508   unsigned logBase2() const { return BitWidth - 1 - countLeadingZeros(); }
01509 
01510   /// \returns the ceil log base 2 of this APInt.
01511   unsigned ceilLogBase2() const {
01512     return BitWidth - (*this - 1).countLeadingZeros();
01513   }
01514 
01515   /// \returns the nearest log base 2 of this APInt. Ties round up.
01516   ///
01517   /// NOTE: When we have a BitWidth of 1, we define:
01518   /// 
01519   ///   log2(0) = UINT32_MAX
01520   ///   log2(1) = 0
01521   ///
01522   /// to get around any mathematical concerns resulting from
01523   /// referencing 2 in a space where 2 does no exist.
01524   unsigned nearestLogBase2() const {
01525     // Special case when we have a bitwidth of 1. If VAL is 1, then we
01526     // get 0. If VAL is 0, we get UINT64_MAX which gets truncated to
01527     // UINT32_MAX.
01528     if (BitWidth == 1)
01529       return VAL - 1;
01530 
01531     // Handle the zero case.
01532     if (!getBoolValue())
01533       return UINT32_MAX;
01534 
01535     // The non-zero case is handled by computing:
01536     //
01537     //   nearestLogBase2(x) = logBase2(x) + x[logBase2(x)-1].
01538     //
01539     // where x[i] is referring to the value of the ith bit of x.
01540     unsigned lg = logBase2();
01541     return lg + unsigned((*this)[lg - 1]);
01542   }
01543 
01544   /// \returns the log base 2 of this APInt if its an exact power of two, -1
01545   /// otherwise
01546   int32_t exactLogBase2() const {
01547     if (!isPowerOf2())
01548       return -1;
01549     return logBase2();
01550   }
01551 
01552   /// \brief Compute the square root
01553   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sqrt() const;
01554 
01555   /// \brief Get the absolute value;
01556   ///
01557   /// If *this is < 0 then return -(*this), otherwise *this;
01558   APInt LLVM_ATTRIBUTE_UNUSED_RESULT abs() const {
01559     if (isNegative())
01560       return -(*this);
01561     return *this;
01562   }
01563 
01564   /// \returns the multiplicative inverse for a given modulo.
01565   APInt multiplicativeInverse(const APInt &modulo) const;
01566 
01567   /// @}
01568   /// \name Support for division by constant
01569   /// @{
01570 
01571   /// Calculate the magic number for signed division by a constant.
01572   struct ms;
01573   ms magic() const;
01574 
01575   /// Calculate the magic number for unsigned division by a constant.
01576   struct mu;
01577   mu magicu(unsigned LeadingZeros = 0) const;
01578 
01579   /// @}
01580   /// \name Building-block Operations for APInt and APFloat
01581   /// @{
01582 
01583   // These building block operations operate on a representation of arbitrary
01584   // precision, two's-complement, bignum integer values. They should be
01585   // sufficient to implement APInt and APFloat bignum requirements. Inputs are
01586   // generally a pointer to the base of an array of integer parts, representing
01587   // an unsigned bignum, and a count of how many parts there are.
01588 
01589   /// Sets the least significant part of a bignum to the input value, and zeroes
01590   /// out higher parts.
01591   static void tcSet(integerPart *, integerPart, unsigned int);
01592 
01593   /// Assign one bignum to another.
01594   static void tcAssign(integerPart *, const integerPart *, unsigned int);
01595 
01596   /// Returns true if a bignum is zero, false otherwise.
01597   static bool tcIsZero(const integerPart *, unsigned int);
01598 
01599   /// Extract the given bit of a bignum; returns 0 or 1.  Zero-based.
01600   static int tcExtractBit(const integerPart *, unsigned int bit);
01601 
01602   /// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to
01603   /// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least
01604   /// significant bit of DST.  All high bits above srcBITS in DST are
01605   /// zero-filled.
01606   static void tcExtract(integerPart *, unsigned int dstCount,
01607                         const integerPart *, unsigned int srcBits,
01608                         unsigned int srcLSB);
01609 
01610   /// Set the given bit of a bignum.  Zero-based.
01611   static void tcSetBit(integerPart *, unsigned int bit);
01612 
01613   /// Clear the given bit of a bignum.  Zero-based.
01614   static void tcClearBit(integerPart *, unsigned int bit);
01615 
01616   /// Returns the bit number of the least or most significant set bit of a
01617   /// number.  If the input number has no bits set -1U is returned.
01618   static unsigned int tcLSB(const integerPart *, unsigned int);
01619   static unsigned int tcMSB(const integerPart *parts, unsigned int n);
01620 
01621   /// Negate a bignum in-place.
01622   static void tcNegate(integerPart *, unsigned int);
01623 
01624   /// DST += RHS + CARRY where CARRY is zero or one.  Returns the carry flag.
01625   static integerPart tcAdd(integerPart *, const integerPart *,
01626                            integerPart carry, unsigned);
01627 
01628   /// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
01629   static integerPart tcSubtract(integerPart *, const integerPart *,
01630                                 integerPart carry, unsigned);
01631 
01632   /// DST += SRC * MULTIPLIER + PART   if add is true
01633   /// DST  = SRC * MULTIPLIER + PART   if add is false
01634   ///
01635   /// Requires 0 <= DSTPARTS <= SRCPARTS + 1.  If DST overlaps SRC they must
01636   /// start at the same point, i.e. DST == SRC.
01637   ///
01638   /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned.
01639   /// Otherwise DST is filled with the least significant DSTPARTS parts of the
01640   /// result, and if all of the omitted higher parts were zero return zero,
01641   /// otherwise overflow occurred and return one.
01642   static int tcMultiplyPart(integerPart *dst, const integerPart *src,
01643                             integerPart multiplier, integerPart carry,
01644                             unsigned int srcParts, unsigned int dstParts,
01645                             bool add);
01646 
01647   /// DST = LHS * RHS, where DST has the same width as the operands and is
01648   /// filled with the least significant parts of the result.  Returns one if
01649   /// overflow occurred, otherwise zero.  DST must be disjoint from both
01650   /// operands.
01651   static int tcMultiply(integerPart *, const integerPart *, const integerPart *,
01652                         unsigned);
01653 
01654   /// DST = LHS * RHS, where DST has width the sum of the widths of the
01655   /// operands.  No overflow occurs.  DST must be disjoint from both
01656   /// operands. Returns the number of parts required to hold the result.
01657   static unsigned int tcFullMultiply(integerPart *, const integerPart *,
01658                                      const integerPart *, unsigned, unsigned);
01659 
01660   /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
01661   /// Otherwise set LHS to LHS / RHS with the fractional part discarded, set
01662   /// REMAINDER to the remainder, return zero.  i.e.
01663   ///
01664   ///  OLD_LHS = RHS * LHS + REMAINDER
01665   ///
01666   /// SCRATCH is a bignum of the same size as the operands and result for use by
01667   /// the routine; its contents need not be initialized and are destroyed.  LHS,
01668   /// REMAINDER and SCRATCH must be distinct.
01669   static int tcDivide(integerPart *lhs, const integerPart *rhs,
01670                       integerPart *remainder, integerPart *scratch,
01671                       unsigned int parts);
01672 
01673   /// Shift a bignum left COUNT bits.  Shifted in bits are zero.  There are no
01674   /// restrictions on COUNT.
01675   static void tcShiftLeft(integerPart *, unsigned int parts,
01676                           unsigned int count);
01677 
01678   /// Shift a bignum right COUNT bits.  Shifted in bits are zero.  There are no
01679   /// restrictions on COUNT.
01680   static void tcShiftRight(integerPart *, unsigned int parts,
01681                            unsigned int count);
01682 
01683   /// The obvious AND, OR and XOR and complement operations.
01684   static void tcAnd(integerPart *, const integerPart *, unsigned int);
01685   static void tcOr(integerPart *, const integerPart *, unsigned int);
01686   static void tcXor(integerPart *, const integerPart *, unsigned int);
01687   static void tcComplement(integerPart *, unsigned int);
01688 
01689   /// Comparison (unsigned) of two bignums.
01690   static int tcCompare(const integerPart *, const integerPart *, unsigned int);
01691 
01692   /// Increment a bignum in-place.  Return the carry flag.
01693   static integerPart tcIncrement(integerPart *, unsigned int);
01694 
01695   /// Decrement a bignum in-place.  Return the borrow flag.
01696   static integerPart tcDecrement(integerPart *, unsigned int);
01697 
01698   /// Set the least significant BITS and clear the rest.
01699   static void tcSetLeastSignificantBits(integerPart *, unsigned int,
01700                                         unsigned int bits);
01701 
01702   /// \brief debug method
01703   void dump() const;
01704 
01705   /// @}
01706 };
01707 
01708 /// Magic data for optimising signed division by a constant.
01709 struct APInt::ms {
01710   APInt m;    ///< magic number
01711   unsigned s; ///< shift amount
01712 };
01713 
01714 /// Magic data for optimising unsigned division by a constant.
01715 struct APInt::mu {
01716   APInt m;    ///< magic number
01717   bool a;     ///< add indicator
01718   unsigned s; ///< shift amount
01719 };
01720 
01721 inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; }
01722 
01723 inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; }
01724 
01725 inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) {
01726   I.print(OS, true);
01727   return OS;
01728 }
01729 
01730 namespace APIntOps {
01731 
01732 /// \brief Determine the smaller of two APInts considered to be signed.
01733 inline APInt smin(const APInt &A, const APInt &B) { return A.slt(B) ? A : B; }
01734 
01735 /// \brief Determine the larger of two APInts considered to be signed.
01736 inline APInt smax(const APInt &A, const APInt &B) { return A.sgt(B) ? A : B; }
01737 
01738 /// \brief Determine the smaller of two APInts considered to be signed.
01739 inline APInt umin(const APInt &A, const APInt &B) { return A.ult(B) ? A : B; }
01740 
01741 /// \brief Determine the larger of two APInts considered to be unsigned.
01742 inline APInt umax(const APInt &A, const APInt &B) { return A.ugt(B) ? A : B; }
01743 
01744 /// \brief Check if the specified APInt has a N-bits unsigned integer value.
01745 inline bool isIntN(unsigned N, const APInt &APIVal) { return APIVal.isIntN(N); }
01746 
01747 /// \brief Check if the specified APInt has a N-bits signed integer value.
01748 inline bool isSignedIntN(unsigned N, const APInt &APIVal) {
01749   return APIVal.isSignedIntN(N);
01750 }
01751 
01752 /// \returns true if the argument APInt value is a sequence of ones starting at
01753 /// the least significant bit with the remainder zero.
01754 inline bool isMask(unsigned numBits, const APInt &APIVal) {
01755   return numBits <= APIVal.getBitWidth() &&
01756          APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
01757 }
01758 
01759 /// \brief Return true if the argument APInt value contains a sequence of ones
01760 /// with the remainder zero.
01761 inline bool isShiftedMask(unsigned numBits, const APInt &APIVal) {
01762   return isMask(numBits, (APIVal - APInt(numBits, 1)) | APIVal);
01763 }
01764 
01765 /// \brief Returns a byte-swapped representation of the specified APInt Value.
01766 inline APInt byteSwap(const APInt &APIVal) { return APIVal.byteSwap(); }
01767 
01768 /// \brief Returns the floor log base 2 of the specified APInt value.
01769 inline unsigned logBase2(const APInt &APIVal) { return APIVal.logBase2(); }
01770 
01771 /// \brief Compute GCD of two APInt values.
01772 ///
01773 /// This function returns the greatest common divisor of the two APInt values
01774 /// using Euclid's algorithm.
01775 ///
01776 /// \returns the greatest common divisor of Val1 and Val2
01777 APInt GreatestCommonDivisor(const APInt &Val1, const APInt &Val2);
01778 
01779 /// \brief Converts the given APInt to a double value.
01780 ///
01781 /// Treats the APInt as an unsigned value for conversion purposes.
01782 inline double RoundAPIntToDouble(const APInt &APIVal) {
01783   return APIVal.roundToDouble();
01784 }
01785 
01786 /// \brief Converts the given APInt to a double value.
01787 ///
01788 /// Treats the APInt as a signed value for conversion purposes.
01789 inline double RoundSignedAPIntToDouble(const APInt &APIVal) {
01790   return APIVal.signedRoundToDouble();
01791 }
01792 
01793 /// \brief Converts the given APInt to a float vlalue.
01794 inline float RoundAPIntToFloat(const APInt &APIVal) {
01795   return float(RoundAPIntToDouble(APIVal));
01796 }
01797 
01798 /// \brief Converts the given APInt to a float value.
01799 ///
01800 /// Treast the APInt as a signed value for conversion purposes.
01801 inline float RoundSignedAPIntToFloat(const APInt &APIVal) {
01802   return float(APIVal.signedRoundToDouble());
01803 }
01804 
01805 /// \brief Converts the given double value into a APInt.
01806 ///
01807 /// This function convert a double value to an APInt value.
01808 APInt RoundDoubleToAPInt(double Double, unsigned width);
01809 
01810 /// \brief Converts a float value into a APInt.
01811 ///
01812 /// Converts a float value into an APInt value.
01813 inline APInt RoundFloatToAPInt(float Float, unsigned width) {
01814   return RoundDoubleToAPInt(double(Float), width);
01815 }
01816 
01817 /// \brief Arithmetic right-shift function.
01818 ///
01819 /// Arithmetic right-shift the APInt by shiftAmt.
01820 inline APInt ashr(const APInt &LHS, unsigned shiftAmt) {
01821   return LHS.ashr(shiftAmt);
01822 }
01823 
01824 /// \brief Logical right-shift function.
01825 ///
01826 /// Logical right-shift the APInt by shiftAmt.
01827 inline APInt lshr(const APInt &LHS, unsigned shiftAmt) {
01828   return LHS.lshr(shiftAmt);
01829 }
01830 
01831 /// \brief Left-shift function.
01832 ///
01833 /// Left-shift the APInt by shiftAmt.
01834 inline APInt shl(const APInt &LHS, unsigned shiftAmt) {
01835   return LHS.shl(shiftAmt);
01836 }
01837 
01838 /// \brief Signed division function for APInt.
01839 ///
01840 /// Signed divide APInt LHS by APInt RHS.
01841 inline APInt sdiv(const APInt &LHS, const APInt &RHS) { return LHS.sdiv(RHS); }
01842 
01843 /// \brief Unsigned division function for APInt.
01844 ///
01845 /// Unsigned divide APInt LHS by APInt RHS.
01846 inline APInt udiv(const APInt &LHS, const APInt &RHS) { return LHS.udiv(RHS); }
01847 
01848 /// \brief Function for signed remainder operation.
01849 ///
01850 /// Signed remainder operation on APInt.
01851 inline APInt srem(const APInt &LHS, const APInt &RHS) { return LHS.srem(RHS); }
01852 
01853 /// \brief Function for unsigned remainder operation.
01854 ///
01855 /// Unsigned remainder operation on APInt.
01856 inline APInt urem(const APInt &LHS, const APInt &RHS) { return LHS.urem(RHS); }
01857 
01858 /// \brief Function for multiplication operation.
01859 ///
01860 /// Performs multiplication on APInt values.
01861 inline APInt mul(const APInt &LHS, const APInt &RHS) { return LHS * RHS; }
01862 
01863 /// \brief Function for addition operation.
01864 ///
01865 /// Performs addition on APInt values.
01866 inline APInt add(const APInt &LHS, const APInt &RHS) { return LHS + RHS; }
01867 
01868 /// \brief Function for subtraction operation.
01869 ///
01870 /// Performs subtraction on APInt values.
01871 inline APInt sub(const APInt &LHS, const APInt &RHS) { return LHS - RHS; }
01872 
01873 /// \brief Bitwise AND function for APInt.
01874 ///
01875 /// Performs bitwise AND operation on APInt LHS and
01876 /// APInt RHS.
01877 inline APInt And(const APInt &LHS, const APInt &RHS) { return LHS & RHS; }
01878 
01879 /// \brief Bitwise OR function for APInt.
01880 ///
01881 /// Performs bitwise OR operation on APInt LHS and APInt RHS.
01882 inline APInt Or(const APInt &LHS, const APInt &RHS) { return LHS | RHS; }
01883 
01884 /// \brief Bitwise XOR function for APInt.
01885 ///
01886 /// Performs bitwise XOR operation on APInt.
01887 inline APInt Xor(const APInt &LHS, const APInt &RHS) { return LHS ^ RHS; }
01888 
01889 /// \brief Bitwise complement function.
01890 ///
01891 /// Performs a bitwise complement operation on APInt.
01892 inline APInt Not(const APInt &APIVal) { return ~APIVal; }
01893 
01894 } // End of APIntOps namespace
01895 
01896 // See friend declaration above. This additional declaration is required in
01897 // order to compile LLVM with IBM xlC compiler.
01898 hash_code hash_value(const APInt &Arg);
01899 } // End of llvm namespace
01900 
01901 #endif