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