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