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