APInt.h revision 9b6af8de58140566a0e6567508bf906027422e7c
102760ed19ae02feca0cc536c035cdf08d222a8f3Ted Kremenek//===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===//
2d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//
3d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//                     The LLVM Compiler Infrastructure
4d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
7d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//
8d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===//
9d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//
10d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng// This file implements a class to represent arbitrary precision integral
117ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer// constant values and operations on them.
12d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//
13d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===//
14d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
15d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#ifndef LLVM_APINT_H
16d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#define LLVM_APINT_H
17d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner#include "llvm/Support/MathExtras.h"
1958a0d64fae7ace05b27dad94fd427991d853619bLauro Ramos Venancio#include <cassert>
20de551f91d8816632a76a065084caab9fab6aacffDan Gohman#include <climits>
21cbd56db62b7fa5a685bdca2eddefe9d04a1295f8Nick Lewycky#include <cstring>
22d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#include <string>
23d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
24d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Shengnamespace llvm {
2573481e04da1573d10c72982cfba0716c39902502Chris Lattner  class Serializer;
2673481e04da1573d10c72982cfba0716c39902502Chris Lattner  class Deserializer;
27e420debd26300cbeb6af2722e3342132b2be1b3cTed Kremenek  class FoldingSetNodeID;
28944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner  class raw_ostream;
29689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  class StringRef;
303a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
31fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  template<typename T>
32fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  class SmallVectorImpl;
333a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
3491021d339004ff5a64c3c9614cff8966899c0c99Chris Lattner  // An unsigned host type used as a single part of a multi-part
3591021d339004ff5a64c3c9614cff8966899c0c99Chris Lattner  // bignum.
36fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  typedef uint64_t integerPart;
37fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
38b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  const unsigned int host_char_bit = 8;
3948e8c80e1791adb7a07b8fd6e27edcdfbb756950Evan Cheng  const unsigned int integerPartWidth = host_char_bit *
4048e8c80e1791adb7a07b8fd6e27edcdfbb756950Evan Cheng    static_cast<unsigned int>(sizeof(integerPart));
41b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
42d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===//
43d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//                              APInt Class
44d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===//
45d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
46d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// APInt - This class represents arbitrary precision constant integral values.
473a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman/// It is a functional replacement for common case unsigned integer type like
483a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman/// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
49e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// integer sizes and large integer value types such as 3-bits, 15-bits, or more
503a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman/// than 64-bits of precision. APInt provides a variety of arithmetic operators
51e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// and methods to manipulate integer values of any bit-width. It supports both
52e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// the typical integer arithmetic and comparison operations as well as bitwise
53e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// manipulation.
54d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng///
55e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// The class has several invariants worth noting:
56e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///   * All bit, byte, and word positions are zero-based.
573a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman///   * Once the bit width is set, it doesn't change except by the Truncate,
58e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     SignExtend, or ZeroExtend operations.
59e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///   * All binary operators must be on APInt instances of the same bit width.
603a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman///     Attempting to use these operators on instances with different bit
61e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     widths will yield an assertion.
62e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///   * The value is stored canonically as an unsigned value. For operations
63e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     where it makes a difference, there are both signed and unsigned variants
64e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     of the operation. For example, sdiv and udiv. However, because the bit
65e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     widths must be the same, operations such as Mul and Add produce the same
66e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     results regardless of whether the values are interpreted as signed or
67e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     not.
68e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///   * In general, the class tries to follow the style of computation that LLVM
69e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     uses in its IR. This simplifies its use for LLVM.
707406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng///
71e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// @brief Class for arbitrary precision integers.
72d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Shengclass APInt {
739981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned BitWidth;      ///< The number of bits in this APInt.
74d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
75d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// This union is used to store the integer value. When the
767ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
77d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  union {
78d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng    uint64_t VAL;    ///< Used to store the <= 64 bits integer value.
79d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng    uint64_t *pVal;  ///< Used to store the >64 bits integer value.
80d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  };
81d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
827ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This enum is used to hold the constants we needed for APInt.
83d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  enum {
8448e8c80e1791adb7a07b8fd6e27edcdfbb756950Evan Cheng    /// Bits in a word
85de551f91d8816632a76a065084caab9fab6aacffDan Gohman    APINT_BITS_PER_WORD = static_cast<unsigned int>(sizeof(uint64_t)) *
86de551f91d8816632a76a065084caab9fab6aacffDan Gohman                          CHAR_BIT,
8748e8c80e1791adb7a07b8fd6e27edcdfbb756950Evan Cheng    /// Byte size of a word
8848e8c80e1791adb7a07b8fd6e27edcdfbb756950Evan Cheng    APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
89d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  };
90d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
917ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This constructor is used only internally for speed of construction of
927ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// temporaries. It is unsafe for general use so it is not public.
937ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Fast internal constructor
949981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt(uint64_t* val, unsigned bits) : BitWidth(bits), pVal(val) { }
9520b1f5db1e93d28debc6449dcd56da6ca7aa6b67Reid Spencer
96d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// @returns true if the number of bits <= 64, false otherwise.
97d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// @brief Determine if this APInt just has one word to store value.
983a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  bool isSingleWord() const {
993a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    return BitWidth <= APINT_BITS_PER_WORD;
100e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
101d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
102d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// @returns the word position for the specified bit position.
1037ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Determine which word a bit is in.
1049981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static unsigned whichWord(unsigned bitPosition) {
1053a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    return bitPosition / APINT_BITS_PER_WORD;
106e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
107d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1083a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @returns the bit position in a word for the specified bit position
1097ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// in the APInt.
1107ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Determine which bit in a word a bit is in.
1119981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static unsigned whichBit(unsigned bitPosition) {
1123a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    return bitPosition % APINT_BITS_PER_WORD;
113e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
114d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1153a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// This method generates and returns a uint64_t (word) mask for a single
1163a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// bit at a specific bit position. This is used to mask the bit in the
1177ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// corresponding word.
1187ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns a uint64_t with only bit at "whichBit(bitPosition)" set
1197ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Get a single bit mask.
1209981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static uint64_t maskBit(unsigned bitPosition) {
1213a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    return 1ULL << whichBit(bitPosition);
122e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
123d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1247ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This method is used internally to clear the to "N" bits in the high order
1253a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// word that are not used by the APInt. This is needed after the most
1263a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// significant word is assigned a value to ensure that those bits are
1277ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// zero'd out.
1287ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Clear unused high order bits
129c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  APInt& clearUnusedBits() {
130c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer    // Compute how many bits are used in the final word
1319981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner    unsigned wordBits = BitWidth % APINT_BITS_PER_WORD;
132c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer    if (wordBits == 0)
133c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer      // If all bits are used, we want to leave the value alone. This also
134cd2ad1df5d2e0600200c81bc214e0b65ff699c5fDan Gohman      // avoids the undefined behavior of >> when the shift is the same size as
135c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer      // the word size (64).
136c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer      return *this;
137c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer
1388eab8a2798fe74c98703bdeac64661beea0b4dbcDuncan Sands    // Mask out the high bits.
139c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer    uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
140d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng    if (isSingleWord())
141c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer      VAL &= mask;
142d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng    else
143c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer      pVal[getNumWords() - 1] &= mask;
144c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer    return *this;
145d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  }
146d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
147d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// @returns the corresponding word for the specified bit position.
148d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer  /// @brief Get the word corresponding to a bit position
1499981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  uint64_t getWord(unsigned bitPosition) const {
1503a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
151e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
152d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1531e7ad3993d8700488895fa372ecad55443f53485John McCall  /// Converts a string into a number.  The string must be non-empty
1541e7ad3993d8700488895fa372ecad55443f53485John McCall  /// and well-formed as a number of the given base. The bit-width
1551e7ad3993d8700488895fa372ecad55443f53485John McCall  /// must be sufficient to hold the result.
1561e7ad3993d8700488895fa372ecad55443f53485John McCall  ///
157d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer  /// This is used by the constructors that take string arguments.
1581e7ad3993d8700488895fa372ecad55443f53485John McCall  ///
1591e7ad3993d8700488895fa372ecad55443f53485John McCall  /// StringRef::getAsInteger is superficially similar but (1) does
1601e7ad3993d8700488895fa372ecad55443f53485John McCall  /// not assume that the string is well-formed and (2) grows the
1611e7ad3993d8700488895fa372ecad55443f53485John McCall  /// result to hold the input.
1621e7ad3993d8700488895fa372ecad55443f53485John McCall  ///
1631e7ad3993d8700488895fa372ecad55443f53485John McCall  /// @param radix 2, 8, 10, or 16
1647ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Convert a char array into an APInt
16538e59891ee4417a9be2f8146ce0ba3269e38ac21Benjamin Kramer  void fromString(unsigned numBits, StringRef str, uint8_t radix);
166f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng
167d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer  /// This is used by the toString method to divide by the radix. It simply
168c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// provides a more convenient form of divide for internal use since KnuthDiv
169c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// has specific constraints on its inputs. If those constraints are not met
170c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// then it provides a simpler form of divide.
171d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer  /// @brief An internal division function for dividing APInts.
1729981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static void divide(const APInt LHS, unsigned lhsWords,
1739981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner                     const APInt &RHS, unsigned rhsWords,
174d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer                     APInt *Quotient, APInt *Remainder);
175d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer
17698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for inline constructor
1779981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  void initSlowCase(unsigned numBits, uint64_t val, bool isSigned);
17898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
17998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for inline copy constructor
18098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  void initSlowCase(const APInt& that);
18198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
18298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for shl
1839981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt shlSlowCase(unsigned shiftAmt) const;
18498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
18598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator&
18698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  APInt AndSlowCase(const APInt& RHS) const;
18798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
18898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator|
18998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  APInt OrSlowCase(const APInt& RHS) const;
19098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
19198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator^
19298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  APInt XorSlowCase(const APInt& RHS) const;
19398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
19498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator=
19598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  APInt& AssignSlowCase(const APInt& RHS);
19698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
19798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator==
19898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  bool EqualSlowCase(const APInt& RHS) const;
19998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
20098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator==
20198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  bool EqualSlowCase(uint64_t Val) const;
20298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
20398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for countLeadingZeros
2049981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countLeadingZerosSlowCase() const;
20598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
20698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for countTrailingOnes
2079981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countTrailingOnesSlowCase() const;
20898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
20998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for countPopulation
2109981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countPopulationSlowCase() const;
21198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
212f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencerpublic:
2137ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Constructors
2147ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
2157ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// If isSigned is true then val is treated as if it were a signed value
2167ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
2177ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// will be done. Otherwise, no sign extension occurs (high order bits beyond
2187ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the range of val are zero filled).
2197ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param numBits the bit width of the constructed APInt
2207ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param val the initial value of the APInt
2217ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param isSigned how to treat signedness of val
222d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer  /// @brief Create a new APInt of numBits width, initialized as val.
2239981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt(unsigned numBits, uint64_t val, bool isSigned = false)
22498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    : BitWidth(numBits), VAL(0) {
22598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth && "bitwidth too small");
22698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
22798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      VAL = val;
22898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    else
22998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      initSlowCase(numBits, val, isSigned);
23098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    clearUnusedBits();
23198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
232d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
233d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer  /// Note that numWords can be smaller or larger than the corresponding bit
234d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer  /// width but any extraneous bits will be dropped.
2357ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param numBits the bit width of the constructed APInt
2367ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param numWords the number of words in bigVal
2377ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param bigVal a sequence of words to form the initial value of the APInt
2387ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Construct an APInt of numBits width, initialized as bigVal[].
2399981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
240d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
241689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  /// This constructor interprets the string \arg str in the given radix. The
242689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  /// interpretation stops when the first character that is not suitable for the
243689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  /// radix is encountered, or the end of the string. Acceptable radix values
244689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  /// are 2, 8, 10 and 16. It is an error for the value implied by the string to
245689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  /// require more bits than numBits.
246689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  ///
2477ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param numBits the bit width of the constructed APInt
248689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  /// @param str the string to be interpreted
249689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  /// @param radix the radix to use for the conversion
2507ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Construct an APInt from a string representation.
25138e59891ee4417a9be2f8146ce0ba3269e38ac21Benjamin Kramer  APInt(unsigned numBits, StringRef str, uint8_t radix);
2527ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
2537ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Simply makes *this a copy of that.
2547406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng  /// @brief Copy Constructor.
25598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  APInt(const APInt& that)
25698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    : BitWidth(that.BitWidth), VAL(0) {
25798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth && "bitwidth too small");
2583a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    if (isSingleWord())
25998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      VAL = that.VAL;
26098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    else
26198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      initSlowCase(that);
26298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
263d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
2647406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng  /// @brief Destructor.
26598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  ~APInt() {
2663a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    if (!isSingleWord())
26798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      delete [] pVal;
26898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
26998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
2701a43e5ffe1ba9bfcb531f7c6a68ac8c49bd28dc1Ted Kremenek  /// Default constructor that creates an uninitialized APInt.  This is useful
2711a43e5ffe1ba9bfcb531f7c6a68ac8c49bd28dc1Ted Kremenek  ///  for object deserialization (pair this with the static method Read).
272586504d5ed35d5765652370c4b4e68eb21b2b34aTed Kremenek  explicit APInt() : BitWidth(1) {}
2733a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
2743a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// Profile - Used to insert APInt objects, or objects that contain APInt
275e420debd26300cbeb6af2722e3342132b2be1b3cTed Kremenek  ///  objects, into FoldingSets.
276187784996c1bdf8aafb5f8719bcbde9260465865Ted Kremenek  void Profile(FoldingSetNodeID& id) const;
2773a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
2787ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
2797ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Value Tests
2807ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
2817ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This tests the high bit of this APInt to determine if it is set.
2827ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if this APInt is negative, false otherwise
2837ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Determine sign of this APInt.
2847ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  bool isNegative() const {
2857ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return (*this)[BitWidth - 1];
2867ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
287d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
2887ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This tests the high bit of the APInt to determine if it is unset.
2897649127ec152e226d761845983ba91e69712d7daDan Gohman  /// @brief Determine if this APInt Value is non-negative (>= 0)
2907649127ec152e226d761845983ba91e69712d7daDan Gohman  bool isNonNegative() const {
2917ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return !isNegative();
2927ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
2937ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
2947649127ec152e226d761845983ba91e69712d7daDan Gohman  /// This tests if the value of this APInt is positive (> 0). Note
2957649127ec152e226d761845983ba91e69712d7daDan Gohman  /// that 0 is not a positive value.
2967649127ec152e226d761845983ba91e69712d7daDan Gohman  /// @returns true if this APInt is positive.
2977649127ec152e226d761845983ba91e69712d7daDan Gohman  /// @brief Determine if this APInt Value is positive.
298c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  bool isStrictlyPositive() const {
299fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer    return isNonNegative() && !!*this;
3007ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3017ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
3027ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This checks to see if the value has all bits of the APInt are set or not.
3037ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Determine if all bits are set
304c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  bool isAllOnesValue() const {
3057ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return countPopulation() == BitWidth;
3067ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3077ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
3087ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This checks to see if the value of this APInt is the maximum unsigned
3097ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// value for the APInt's bit width.
3107ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Determine if this is the largest unsigned value.
3117ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  bool isMaxValue() const {
3127ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return countPopulation() == BitWidth;
3137ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3147ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
3157ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This checks to see if the value of this APInt is the maximum signed
3167ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// value for the APInt's bit width.
3177ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Determine if this is the largest signed value.
3187ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  bool isMaxSignedValue() const {
3197ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return BitWidth == 1 ? VAL == 0 :
3207ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer                          !isNegative() && countPopulation() == BitWidth - 1;
3217ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3227ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
3237ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This checks to see if the value of this APInt is the minimum unsigned
3247ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// value for the APInt's bit width.
3257ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Determine if this is the smallest unsigned value.
3267ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  bool isMinValue() const {
327fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer    return !*this;
3287ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3297ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
3307ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This checks to see if the value of this APInt is the minimum signed
3317ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// value for the APInt's bit width.
3327ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Determine if this is the smallest signed value.
3337ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  bool isMinSignedValue() const {
334fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer    return BitWidth == 1 ? VAL == 1 : isNegative() && isPowerOf2();
3357ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3367ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
337ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman  /// @brief Check if this APInt has an N-bits unsigned integer value.
3389981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  bool isIntN(unsigned N) const {
3397ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    assert(N && "N == 0 ???");
3401d93b2e1b638cee7f012e8f636eb2cc137d855ffChris Lattner    if (N >= getBitWidth())
3411d93b2e1b638cee7f012e8f636eb2cc137d855ffChris Lattner      return true;
3423a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
3431d93b2e1b638cee7f012e8f636eb2cc137d855ffChris Lattner    if (isSingleWord())
3445005e27f9714f0eaa5b8b7a5a1f6751afa163f07Dan Gohman      return isUIntN(N, VAL);
34540f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    return APInt(N, getNumWords(), pVal).zext(getBitWidth()) == (*this);
3467ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3477ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
348ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman  /// @brief Check if this APInt has an N-bits signed integer value.
3499981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  bool isSignedIntN(unsigned N) const {
350ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman    assert(N && "N == 0 ???");
351ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman    return getMinSignedBits() <= N;
352ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman  }
353ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman
3547ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if the argument APInt value is a power of two > 0.
355fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer  bool isPowerOf2() const {
356fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer    if (isSingleWord())
357fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer      return isPowerOf2_64(VAL);
358fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer    return countPopulationSlowCase() == 1;
359fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer  }
3607ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
361febabcc02a1303a8e4d9bed76718a32f0273607aChris Lattner  /// isSignBit - Return true if this is the value returned by getSignBit.
362febabcc02a1303a8e4d9bed76718a32f0273607aChris Lattner  bool isSignBit() const { return isMinSignedValue(); }
3633a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
36448a3e98c272d673f075c51d078140765fc267eb8Duncan Sands  /// This converts the APInt to a boolean value as a test against zero.
3653a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Boolean conversion function.
366c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  bool getBoolValue() const {
367fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer    return !!*this;
3687ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3697ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
3702b4c23438838034ee2a812f3cb094a9742b272f6Chris Lattner  /// getLimitedValue - If this value is smaller than the specified limit,
3712b4c23438838034ee2a812f3cb094a9742b272f6Chris Lattner  /// return it, otherwise return the limit value.  This causes the value
3722b4c23438838034ee2a812f3cb094a9742b272f6Chris Lattner  /// to saturate to the limit.
3734bda52d6b5ffa13314fe32baa1784566f70483f9Chris Lattner  uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
3744bda52d6b5ffa13314fe32baa1784566f70483f9Chris Lattner    return (getActiveBits() > 64 || getZExtValue() > Limit) ?
3754bda52d6b5ffa13314fe32baa1784566f70483f9Chris Lattner      Limit :  getZExtValue();
3764bda52d6b5ffa13314fe32baa1784566f70483f9Chris Lattner  }
3774bda52d6b5ffa13314fe32baa1784566f70483f9Chris Lattner
3787ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
3797ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Value Generators
3807ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
3817ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Gets maximum unsigned value of APInt for specific bit width.
3829981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getMaxValue(unsigned numBits) {
383bd7d2622a2ee828a02017dca19170b6f79ff6684Benjamin Kramer    return getAllOnesValue(numBits);
3847ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3857ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
3867ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Gets maximum signed value of APInt for a specific bit width.
3879981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getSignedMaxValue(unsigned numBits) {
388bd7d2622a2ee828a02017dca19170b6f79ff6684Benjamin Kramer    APInt API = getAllOnesValue(numBits);
3897a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad    API.clearBit(numBits - 1);
390a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    return API;
3917ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3927ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
3937ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Gets minimum unsigned value of APInt for a specific bit width.
3949981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getMinValue(unsigned numBits) {
3957ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return APInt(numBits, 0);
3967ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3977ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
3987ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Gets minimum signed value of APInt for a specific bit width.
3999981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getSignedMinValue(unsigned numBits) {
400a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    APInt API(numBits, 0);
4017a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad    API.setBit(numBits - 1);
402a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    return API;
4037ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
4047ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
4057ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// getSignBit - This is just a wrapper function of getSignedMinValue(), and
4067ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// it helps code readability when we want to get a SignBit.
4077ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Get the SignBit for a specific bit width.
4089981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getSignBit(unsigned BitWidth) {
4097ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return getSignedMinValue(BitWidth);
4107ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
4117ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
4127ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns the all-ones value for an APInt of the specified bit-width.
4137ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Get the all-ones value.
4149981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getAllOnesValue(unsigned numBits) {
415bd7d2622a2ee828a02017dca19170b6f79ff6684Benjamin Kramer    return APInt(numBits, -1ULL, true);
4167ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
4177ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
4187ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns the '0' value for an APInt of the specified bit-width.
4197ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Get the '0' value.
4209981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getNullValue(unsigned numBits) {
4217ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return APInt(numBits, 0);
4227ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
4237ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
4247ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Get an APInt with the same BitWidth as this APInt, just zero mask
4257ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the low bits and right shift to the least significant bit.
4267ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns the high "numBits" bits of this APInt.
4279981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt getHiBits(unsigned numBits) const;
428d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
4297ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Get an APInt with the same BitWidth as this APInt, just zero mask
4307ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the high bits.
4317ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns the low "numBits" bits of this APInt.
4329981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt getLoBits(unsigned numBits) const;
4337ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
43416e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner  /// getOneBitSet - Return an APInt with exactly one bit set in the result.
43516e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner  static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
43616e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner    APInt Res(numBits, 0);
43716e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner    Res.setBit(BitNo);
43816e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner    return Res;
43916e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner  }
44016e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner
4417ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Constructs an APInt value that has a contiguous range of bits set. The
442ffe3e2514a2550152267072ed7afeebb3dd3016cDan Gohman  /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
443ffe3e2514a2550152267072ed7afeebb3dd3016cDan Gohman  /// bits will be zero. For example, with parameters(32, 0, 16) you would get
444ffe3e2514a2550152267072ed7afeebb3dd3016cDan Gohman  /// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For
4453a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// example, with parameters (32, 28, 4), you would get 0xF000000F.
4467ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param numBits the intended bit width of the result
4477ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param loBit the index of the lowest bit set.
4485b7e659e220ed01c8d3a7335e2b52ff2f0fb2ef3Reid Spencer  /// @param hiBit the index of the highest bit set.
4497ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns An APInt value with the requested bits set.
4507ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Get a value with a block of bits set.
4519981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
452ffe3e2514a2550152267072ed7afeebb3dd3016cDan Gohman    assert(hiBit <= numBits && "hiBit out of range");
4538f969ee62c3fed26f835a37c41f7c009f99ea3bfReid Spencer    assert(loBit < numBits && "loBit out of range");
4548f969ee62c3fed26f835a37c41f7c009f99ea3bfReid Spencer    if (hiBit < loBit)
455ffe3e2514a2550152267072ed7afeebb3dd3016cDan Gohman      return getLowBitsSet(numBits, hiBit) |
456d3993d13be7b34fe8753eab9eb3d6499b7c4cd9fDan Gohman             getHighBitsSet(numBits, numBits-loBit);
457ffe3e2514a2550152267072ed7afeebb3dd3016cDan Gohman    return getLowBitsSet(numBits, hiBit-loBit).shl(loBit);
4588f969ee62c3fed26f835a37c41f7c009f99ea3bfReid Spencer  }
4597ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
4607ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Constructs an APInt value that has the top hiBitsSet bits set.
4617ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param numBits the bitwidth of the result
4627ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param hiBitsSet the number of high-order bits set in the result.
4637ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Get a value with high bits set
4649981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
4658da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer    assert(hiBitsSet <= numBits && "Too many bits to set!");
466a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer    // Handle a degenerate case, to avoid shifting by word size
467a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer    if (hiBitsSet == 0)
468a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer      return APInt(numBits, 0);
4699981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner    unsigned shiftAmt = numBits - hiBitsSet;
4708da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer    // For small values, return quickly
4718da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer    if (numBits <= APINT_BITS_PER_WORD)
472ca76fc2cd3b97e4821e7d8247bc03bbbd2789ad9Reid Spencer      return APInt(numBits, ~0ULL << shiftAmt);
473452b93e7dc2802dea3a31ea12c397d11c3c5e23bBenjamin Kramer    return getAllOnesValue(numBits).shl(shiftAmt);
4748da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer  }
4757ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
4767ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Constructs an APInt value that has the bottom loBitsSet bits set.
4777ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param numBits the bitwidth of the result
4787ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @param loBitsSet the number of low-order bits set in the result.
4797ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Get a value with low bits set
4809981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) {
4818da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer    assert(loBitsSet <= numBits && "Too many bits to set!");
482a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer    // Handle a degenerate case, to avoid shifting by word size
483a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer    if (loBitsSet == 0)
484a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer      return APInt(numBits, 0);
485f6bef488eeab75b6c7746f9faa9731d45bb84aefReid Spencer    if (loBitsSet == APINT_BITS_PER_WORD)
486f6bef488eeab75b6c7746f9faa9731d45bb84aefReid Spencer      return APInt(numBits, -1ULL);
48771f95b8531183d4a2f7c84d66d5aec17bc41b316Chris Lattner    // For small values, return quickly.
488f6bef488eeab75b6c7746f9faa9731d45bb84aefReid Spencer    if (numBits < APINT_BITS_PER_WORD)
489f6bef488eeab75b6c7746f9faa9731d45bb84aefReid Spencer      return APInt(numBits, (1ULL << loBitsSet) - 1);
490452b93e7dc2802dea3a31ea12c397d11c3c5e23bBenjamin Kramer    return getAllOnesValue(numBits).lshr(numBits - loBitsSet);
4918da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer  }
4927ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
4937ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// The hash value is computed as the sum of the words and the bit width.
4947ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns A hash value computed from the sum of the APInt words.
4957ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Get a hash value based on this APInt
4967ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  uint64_t getHashValue() const;
4977ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
4983a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// This function returns a pointer to the internal storage of the APInt.
4997ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This is useful for writing out the APInt in binary form without any
5007ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// conversions.
501c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  const uint64_t* getRawData() const {
5027ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    if (isSingleWord())
5037ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer      return &VAL;
5047ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return &pVal[0];
5057ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
5067ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
5077ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
5087ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Unary Operators
5097ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
5107ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns a new APInt value representing *this incremented by one
5117406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng  /// @brief Postfix increment operator.
512c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  const APInt operator++(int) {
513f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng    APInt API(*this);
514b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng    ++(*this);
515b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng    return API;
516f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng  }
517d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
5187ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns *this incremented by one
5197406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng  /// @brief Prefix increment operator.
520d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  APInt& operator++();
521d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
5227ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns a new APInt representing *this decremented by one.
5233a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Postfix decrement operator.
524c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  const APInt operator--(int) {
525f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng    APInt API(*this);
526b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng    --(*this);
527b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng    return API;
528f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng  }
529d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
5307ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns *this decremented by one.
5313a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Prefix decrement operator.
532d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  APInt& operator--();
533d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
5343a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// Performs a bitwise complement operation on this APInt.
5357ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns an APInt that is the bitwise complement of *this
5363a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Unary bitwise complement operator.
53798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  APInt operator~() const {
53898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    APInt Result(*this);
5397a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad    Result.flipAllBits();
54098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return Result;
54198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
5427ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
5437ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Negates *this using two's complement logic.
5447ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns An APInt value representing the negation of *this.
5457ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Unary negation operator
546c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  APInt operator-() const {
5477ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return APInt(BitWidth, 0) - (*this);
5487ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
5497ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
5507ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs logical negation operation on this APInt.
5517ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this is zero, false otherwise.
5523a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Logical negation operator.
553fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  bool operator!() const;
5547ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
5557ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
5567ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Assignment Operators
5577ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
5587ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns *this after assignment of RHS.
5593a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Copy assignment operator.
56098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  APInt& operator=(const APInt& RHS) {
56198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    // If the bitwidths are the same, we can avoid mucking with memory
56298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord() && RHS.isSingleWord()) {
56398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      VAL = RHS.VAL;
56498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      BitWidth = RHS.BitWidth;
56598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return clearUnusedBits();
56698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    }
56798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
56898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return AssignSlowCase(RHS);
56998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
5707ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
5717ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// The RHS value is assigned to *this. If the significant bits in RHS exceed
5727ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the bit width, the excess bits are truncated. If the bit width is larger
5737ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// than 64, the value is zero filled in the unspecified high order bits.
5747ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns *this after assignment of RHS value.
5753a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Assignment operator.
5767ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  APInt& operator=(uint64_t RHS);
5777ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
5787ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs a bitwise AND operation on this APInt and RHS. The result is
5793a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// assigned to *this.
5807ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns *this after ANDing with RHS.
5813a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Bitwise AND assignment operator.
582d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  APInt& operator&=(const APInt& RHS);
583d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
5843a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// Performs a bitwise OR operation on this APInt and RHS. The result is
5857ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// assigned *this;
5867ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns *this after ORing with RHS.
5873a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Bitwise OR assignment operator.
588d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  APInt& operator|=(const APInt& RHS);
589d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
5901e7ad3993d8700488895fa372ecad55443f53485John McCall  /// Performs a bitwise OR operation on this APInt and RHS. RHS is
5911e7ad3993d8700488895fa372ecad55443f53485John McCall  /// logically zero-extended or truncated to match the bit-width of
5921e7ad3993d8700488895fa372ecad55443f53485John McCall  /// the LHS.
5931e7ad3993d8700488895fa372ecad55443f53485John McCall  ///
5941e7ad3993d8700488895fa372ecad55443f53485John McCall  /// @brief Bitwise OR assignment operator.
5951e7ad3993d8700488895fa372ecad55443f53485John McCall  APInt& operator|=(uint64_t RHS) {
5961e7ad3993d8700488895fa372ecad55443f53485John McCall    if (isSingleWord()) {
5971e7ad3993d8700488895fa372ecad55443f53485John McCall      VAL |= RHS;
5981e7ad3993d8700488895fa372ecad55443f53485John McCall      clearUnusedBits();
5991e7ad3993d8700488895fa372ecad55443f53485John McCall    } else {
6001e7ad3993d8700488895fa372ecad55443f53485John McCall      pVal[0] |= RHS;
6011e7ad3993d8700488895fa372ecad55443f53485John McCall    }
6021e7ad3993d8700488895fa372ecad55443f53485John McCall    return *this;
6031e7ad3993d8700488895fa372ecad55443f53485John McCall  }
6041e7ad3993d8700488895fa372ecad55443f53485John McCall
6057ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs a bitwise XOR operation on this APInt and RHS. The result is
6067ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// assigned to *this.
6077ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns *this after XORing with RHS.
6083a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Bitwise XOR assignment operator.
609d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  APInt& operator^=(const APInt& RHS);
610d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
6117ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Multiplies this APInt by RHS and assigns the result to *this.
6127ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns *this
6133a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Multiplication assignment operator.
614d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  APInt& operator*=(const APInt& RHS);
615d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
6167ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Adds RHS to *this and assigns the result to *this.
6177ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns *this
6183a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Addition assignment operator.
619d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  APInt& operator+=(const APInt& RHS);
620d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
6217ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Subtracts RHS from *this and assigns the result to *this.
6227ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns *this
6233a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Subtraction assignment operator.
624d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  APInt& operator-=(const APInt& RHS);
625d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
6267ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Shifts *this left by shiftAmt and assigns the result to *this.
6277ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns *this after shifting left by shiftAmt
6287ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Left-shift assignment function.
6299981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt& operator<<=(unsigned shiftAmt) {
6307ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    *this = shl(shiftAmt);
6317ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return *this;
6327ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
6337ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
6347ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
6357ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Binary Operators
6367ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
6377ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs a bitwise AND operation on *this and RHS.
6387ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns An APInt value representing the bitwise AND of *this and RHS.
6393a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Bitwise AND operator.
64098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  APInt operator&(const APInt& RHS) const {
64198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
64298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
64398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return APInt(getBitWidth(), VAL & RHS.VAL);
64498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return AndSlowCase(RHS);
64598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
646da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer  APInt And(const APInt& RHS) const {
647da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer    return this->operator&(RHS);
648da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer  }
649d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
6507ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs a bitwise OR operation on *this and RHS.
6517ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns An APInt value representing the bitwise OR of *this and RHS.
6523a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Bitwise OR operator.
65398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  APInt operator|(const APInt& RHS) const {
65498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
65598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
65698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return APInt(getBitWidth(), VAL | RHS.VAL);
65798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return OrSlowCase(RHS);
65898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
659da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer  APInt Or(const APInt& RHS) const {
660da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer    return this->operator|(RHS);
661da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer  }
662d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
6637ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs a bitwise XOR operation on *this and RHS.
6647ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns An APInt value representing the bitwise XOR of *this and RHS.
6653a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Bitwise XOR operator.
66698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  APInt operator^(const APInt& RHS) const {
66798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
66898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
66998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return APInt(BitWidth, VAL ^ RHS.VAL);
67098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return XorSlowCase(RHS);
67198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
672da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer  APInt Xor(const APInt& RHS) const {
673da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer    return this->operator^(RHS);
674da347141ecb7ce44c532cdc51b000d5ab65db79fReid Spencer  }
675d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
6767ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Multiplies this APInt by RHS and returns the result.
6773a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Multiplication operator.
678d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  APInt operator*(const APInt& RHS) const;
679d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
6807ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Adds RHS to this APInt and returns the result.
6813a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Addition operator.
682d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  APInt operator+(const APInt& RHS) const;
683f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer  APInt operator+(uint64_t RHS) const {
684f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer    return (*this) + APInt(BitWidth, RHS);
685f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer  }
686f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer
6877ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Subtracts RHS from this APInt and returns the result.
6883a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Subtraction operator.
689d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  APInt operator-(const APInt& RHS) const;
690f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer  APInt operator-(uint64_t RHS) const {
691f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer    return (*this) - APInt(BitWidth, RHS);
692f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer  }
6933a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
694ef65293dc27c2d7b1ba1fc7c7ffd802e86b780ecChris Lattner  APInt operator<<(unsigned Bits) const {
695ef65293dc27c2d7b1ba1fc7c7ffd802e86b780ecChris Lattner    return shl(Bits);
696ef65293dc27c2d7b1ba1fc7c7ffd802e86b780ecChris Lattner  }
697d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
698cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  APInt operator<<(const APInt &Bits) const {
699cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman    return shl(Bits);
700cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  }
701cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
7027ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Arithmetic right-shift this APInt by shiftAmt.
7037ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Arithmetic right-shift function.
7049981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt ashr(unsigned shiftAmt) const;
7057ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
7067ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Logical right-shift this APInt by shiftAmt.
7077ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Logical right-shift function.
7089981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt lshr(unsigned shiftAmt) const;
7097ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
7107ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Left-shift this APInt by shiftAmt.
7117ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Left-shift function.
7129981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt shl(unsigned shiftAmt) const {
71398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(shiftAmt <= BitWidth && "Invalid shift amount");
71498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord()) {
71598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      if (shiftAmt == BitWidth)
71698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner        return APInt(BitWidth, 0); // avoid undefined shift results
71798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return APInt(BitWidth, VAL << shiftAmt);
71898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    }
71998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return shlSlowCase(shiftAmt);
72098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
7217ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
72219dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer  /// @brief Rotate left by rotateAmt.
7239981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt rotl(unsigned rotateAmt) const;
72419dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer
72519dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer  /// @brief Rotate right by rotateAmt.
7269981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt rotr(unsigned rotateAmt) const;
72719dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer
728cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  /// Arithmetic right-shift this APInt by shiftAmt.
729cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  /// @brief Arithmetic right-shift function.
730cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  APInt ashr(const APInt &shiftAmt) const;
731cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
732cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  /// Logical right-shift this APInt by shiftAmt.
733cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  /// @brief Logical right-shift function.
734cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  APInt lshr(const APInt &shiftAmt) const;
735cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
736cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  /// Left-shift this APInt by shiftAmt.
737cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  /// @brief Left-shift function.
738cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  APInt shl(const APInt &shiftAmt) const;
739cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
740cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  /// @brief Rotate left by rotateAmt.
741cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  APInt rotl(const APInt &rotateAmt) const;
742cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
743cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  /// @brief Rotate right by rotateAmt.
744cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  APInt rotr(const APInt &rotateAmt) const;
745cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
7467ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Perform an unsigned divide operation on this APInt by RHS. Both this and
7477ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// RHS are treated as unsigned quantities for purposes of this division.
7487ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns a new APInt value containing the division result
7497ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Unsigned division operation.
750f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner  APInt udiv(const APInt &RHS) const;
7517ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
7527ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Signed divide this APInt by APInt RHS.
7537ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Signed division function for APInt.
754f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner  APInt sdiv(const APInt &RHS) const {
7557ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    if (isNegative())
7567ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer      if (RHS.isNegative())
7577ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer        return (-(*this)).udiv(-RHS);
7587ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer      else
7597ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer        return -((-(*this)).udiv(RHS));
7607ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    else if (RHS.isNegative())
7617ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer      return -(this->udiv(-RHS));
7627ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return this->udiv(RHS);
7637ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
7647ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
765d3af825d29ab02f78665d5010c8439d34d65db65Reid Spencer  /// Perform an unsigned remainder operation on this APInt with RHS being the
7667ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// divisor. Both this and RHS are treated as unsigned quantities for purposes
767d3af825d29ab02f78665d5010c8439d34d65db65Reid Spencer  /// of this operation. Note that this is a true remainder operation and not
768d3af825d29ab02f78665d5010c8439d34d65db65Reid Spencer  /// a modulo operation because the sign follows the sign of the dividend
769d3af825d29ab02f78665d5010c8439d34d65db65Reid Spencer  /// which is *this.
7707ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns a new APInt value containing the remainder result
7717ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Unsigned remainder operation.
772f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner  APInt urem(const APInt &RHS) const;
7737ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
7747ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Signed remainder operation on APInt.
7757ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Function for signed remainder operation.
776f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner  APInt srem(const APInt &RHS) const {
7777ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    if (isNegative())
7787ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer      if (RHS.isNegative())
77953c9520b238514a9c1b63ff02221f7467aad1337Reid Spencer        return -((-(*this)).urem(-RHS));
7807ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer      else
781013263f7b008e2fca293cf2720205f3f6db37dc2Reid Spencer        return -((-(*this)).urem(RHS));
7827ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    else if (RHS.isNegative())
783d3af825d29ab02f78665d5010c8439d34d65db65Reid Spencer      return this->urem(-RHS);
7847ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return this->urem(RHS);
7850b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng  }
786d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
787300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  /// Sometimes it is convenient to divide two APInt values and obtain both the
788300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  /// quotient and remainder. This function does both operations in the same
789300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  /// computation making it a little more efficient. The pair of input arguments
790300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  /// may overlap with the pair of output arguments. It is safe to call
791300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  /// udivrem(X, Y, X, Y), for example.
79219dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer  /// @brief Dual division/remainder interface.
7933a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  static void udivrem(const APInt &LHS, const APInt &RHS,
79419dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer                      APInt &Quotient, APInt &Remainder);
79519dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer
79619dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer  static void sdivrem(const APInt &LHS, const APInt &RHS,
797f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner                      APInt &Quotient, APInt &Remainder) {
79819dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer    if (LHS.isNegative()) {
79919dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer      if (RHS.isNegative())
80019dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer        APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
80119dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer      else
80219dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer        APInt::udivrem(-LHS, RHS, Quotient, Remainder);
80319dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer      Quotient = -Quotient;
80419dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer      Remainder = -Remainder;
80519dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer    } else if (RHS.isNegative()) {
80619dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer      APInt::udivrem(LHS, -RHS, Quotient, Remainder);
80719dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer      Quotient = -Quotient;
80819dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer    } else {
80919dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer      APInt::udivrem(LHS, RHS, Quotient, Remainder);
81019dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer    }
81119dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer  }
812f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner
813f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner
814f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner  // Operations that return overflow indicators.
8150a0a585e6bfc112cb8346b17edecb76969fb5532Chris Lattner  APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
816eafc5cb80d58cb9447623a557be4d4f55f42fbc3Chris Lattner  APInt uadd_ov(const APInt &RHS, bool &Overflow) const;
8170a0a585e6bfc112cb8346b17edecb76969fb5532Chris Lattner  APInt ssub_ov(const APInt &RHS, bool &Overflow) const;
818eafc5cb80d58cb9447623a557be4d4f55f42fbc3Chris Lattner  APInt usub_ov(const APInt &RHS, bool &Overflow) const;
8190a0a585e6bfc112cb8346b17edecb76969fb5532Chris Lattner  APInt sdiv_ov(const APInt &RHS, bool &Overflow) const;
8200a0a585e6bfc112cb8346b17edecb76969fb5532Chris Lattner  APInt smul_ov(const APInt &RHS, bool &Overflow) const;
82108e90f5646e61c3be0eebfa172ec73a8b56abee8Chris Lattner  APInt sshl_ov(unsigned Amt, bool &Overflow) const;
82219dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer
8237ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns the bit value at bitPosition
824d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// @brief Array-indexing support.
8259981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  bool operator[](unsigned bitPosition) const;
826d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
8277ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
8287ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Comparison Operators
8297ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
8307ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Compares this APInt with RHS for the validity of the equality
8317ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
8323a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Equality operator.
83398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  bool operator==(const APInt& RHS) const {
83498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
83598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
83698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return VAL == RHS.VAL;
83798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return EqualSlowCase(RHS);
83898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
839d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
8403a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// Compares this APInt with a uint64_t for the validity of the equality
8417ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
8427ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this == Val
843f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng  /// @brief Equality operator.
84498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  bool operator==(uint64_t Val) const {
84598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
84698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return VAL == Val;
84798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return EqualSlowCase(Val);
84898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
849f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng
8507ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Compares this APInt with RHS for the validity of the equality
8517ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
8527ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this == Val
8537ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Equality comparison.
8547ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  bool eq(const APInt &RHS) const {
8553a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    return (*this) == RHS;
8567ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
8577ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
8587ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Compares this APInt with RHS for the validity of the inequality
8597ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
8607ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this != Val
8613a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Inequality operator.
862c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  bool operator!=(const APInt& RHS) const {
863f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng    return !((*this) == RHS);
864f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng  }
865d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
8663a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// Compares this APInt with a uint64_t for the validity of the inequality
8677ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
8687ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this != Val
8693a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Inequality operator.
870c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  bool operator!=(uint64_t Val) const {
871f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng    return !((*this) == Val);
872f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng  }
8733a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
8747ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Compares this APInt with RHS for the validity of the inequality
8757ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
8767ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this != Val
877e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Inequality comparison
878e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  bool ne(const APInt &RHS) const {
879e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer    return !((*this) == RHS);
880e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
881e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
8827ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as unsigned quantities and compares them for
8837ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the validity of the less-than relationship.
8847ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this < RHS when both are considered unsigned.
885e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Unsigned less than comparison
886eafc5cb80d58cb9447623a557be4d4f55f42fbc3Chris Lattner  bool ult(const APInt &RHS) const;
887e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
888e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as an unsigned quantity and compares it with RHS for
889e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the less-than relationship.
890e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @returns true if *this < RHS when considered unsigned.
891e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @brief Unsigned less than comparison
892e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  bool ult(uint64_t RHS) const {
893e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman    return ult(APInt(getBitWidth(), RHS));
894e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  }
895e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
8967ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as signed quantities and compares them for
8977ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// validity of the less-than relationship.
8987ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this < RHS when both are considered signed.
899e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Signed less than comparison
900e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  bool slt(const APInt& RHS) const;
901e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
902e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as a signed quantity and compares it with RHS for
903e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the less-than relationship.
904e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @returns true if *this < RHS when considered signed.
905e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @brief Signed less than comparison
906e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  bool slt(uint64_t RHS) const {
907e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman    return slt(APInt(getBitWidth(), RHS));
908e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  }
909e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
9107ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as unsigned quantities and compares them for
9117ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// validity of the less-or-equal relationship.
9127ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this <= RHS when both are considered unsigned.
913e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Unsigned less or equal comparison
914e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  bool ule(const APInt& RHS) const {
915e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer    return ult(RHS) || eq(RHS);
916e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
917e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
918e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as an unsigned quantity and compares it with RHS for
919e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the less-or-equal relationship.
920e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @returns true if *this <= RHS when considered unsigned.
921e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @brief Unsigned less or equal comparison
922e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  bool ule(uint64_t RHS) const {
923e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman    return ule(APInt(getBitWidth(), RHS));
924e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  }
925e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
9267ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as signed quantities and compares them for
9277ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// validity of the less-or-equal relationship.
9287ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this <= RHS when both are considered signed.
929e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Signed less or equal comparison
930e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  bool sle(const APInt& RHS) const {
931e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer    return slt(RHS) || eq(RHS);
932e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
933e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
934e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as a signed quantity and compares it with RHS for
935e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the less-or-equal relationship.
936e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @returns true if *this <= RHS when considered signed.
937e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @brief Signed less or equal comparison
938e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  bool sle(uint64_t RHS) const {
939e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman    return sle(APInt(getBitWidth(), RHS));
940e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  }
941e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
9427ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as unsigned quantities and compares them for
9437ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the validity of the greater-than relationship.
9447ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this > RHS when both are considered unsigned.
945e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Unsigned greather than comparison
946e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  bool ugt(const APInt& RHS) const {
947e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer    return !ult(RHS) && !eq(RHS);
948e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
949e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
950e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as an unsigned quantity and compares it with RHS for
951e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the greater-than relationship.
952e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @returns true if *this > RHS when considered unsigned.
953e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @brief Unsigned greater than comparison
954e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  bool ugt(uint64_t RHS) const {
955e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman    return ugt(APInt(getBitWidth(), RHS));
956e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  }
957e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
9587ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as signed quantities and compares them for
9597ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the validity of the greater-than relationship.
9607ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this > RHS when both are considered signed.
961e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Signed greather than comparison
962e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  bool sgt(const APInt& RHS) const {
963e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer    return !slt(RHS) && !eq(RHS);
964e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
965e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
966e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as a signed quantity and compares it with RHS for
967e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the greater-than relationship.
968e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @returns true if *this > RHS when considered signed.
969e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @brief Signed greater than comparison
970e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  bool sgt(uint64_t RHS) const {
971e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman    return sgt(APInt(getBitWidth(), RHS));
972e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  }
973e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
9747ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as unsigned quantities and compares them for
9757ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// validity of the greater-or-equal relationship.
9767ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this >= RHS when both are considered unsigned.
977e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Unsigned greater or equal comparison
978e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  bool uge(const APInt& RHS) const {
979e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer    return !ult(RHS);
980e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
981e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
982e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as an unsigned quantity and compares it with RHS for
983e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the greater-or-equal relationship.
984e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @returns true if *this >= RHS when considered unsigned.
985e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @brief Unsigned greater or equal comparison
986e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  bool uge(uint64_t RHS) const {
987e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman    return uge(APInt(getBitWidth(), RHS));
988e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  }
989e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
9907ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as signed quantities and compares them for
9917ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// validity of the greater-or-equal relationship.
9927ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns true if *this >= RHS when both are considered signed.
993e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Signed greather or equal comparison
994e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  bool sge(const APInt& RHS) const {
995e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer    return !slt(RHS);
996e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
997e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
998e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as a signed quantity and compares it with RHS for
999e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the greater-or-equal relationship.
1000e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @returns true if *this >= RHS when considered signed.
1001e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// @brief Signed greater or equal comparison
1002e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  bool sge(uint64_t RHS) const {
1003e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman    return sge(APInt(getBitWidth(), RHS));
1004e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  }
1005e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
1006f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner
1007f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner
1008f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner
1009bd999178929c10bdeb0a2577fa02981778edaee7Dan Gohman  /// This operation tests if there are any pairs of corresponding bits
1010bd999178929c10bdeb0a2577fa02981778edaee7Dan Gohman  /// between this APInt and RHS that are both set.
1011bd999178929c10bdeb0a2577fa02981778edaee7Dan Gohman  bool intersects(const APInt &RHS) const {
1012bd999178929c10bdeb0a2577fa02981778edaee7Dan Gohman    return (*this & RHS) != 0;
1013bd999178929c10bdeb0a2577fa02981778edaee7Dan Gohman  }
1014bd999178929c10bdeb0a2577fa02981778edaee7Dan Gohman
10157ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
10167ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Resizing Operators
10177ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
1018e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// Truncate the APInt to a specified width. It is an error to specify a width
10193a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// that is greater than or equal to the current width.
1020e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Truncate to new width.
102140f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  APInt trunc(unsigned width) const;
1022e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1023e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// This operation sign extends the APInt to a new width. If the high order
1024e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
10253a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// It is an error to specify a width that is less than or equal to the
1026e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// current width.
1027e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Sign extend to a new width.
102840f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  APInt sext(unsigned width) const;
1029e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
10305b9f2d6186317df88dbaf603bf8d0eafd6e83897Reid Spencer  /// This operation zero extends the APInt to a new width. The high order bits
10313a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// are filled with 0 bits.  It is an error to specify a width that is less
1032e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// than or equal to the current width.
1033e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Zero extend to a new width.
103440f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  APInt zext(unsigned width) const;
1035e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
103668e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer  /// Make this APInt have the bit width given by \p width. The value is sign
103768e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer  /// extended, truncated, or left alone to make it that width.
103868e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer  /// @brief Sign extend or truncate to width
103940f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  APInt sextOrTrunc(unsigned width) const;
104068e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer
104168e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer  /// Make this APInt have the bit width given by \p width. The value is zero
104268e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer  /// extended, truncated, or left alone to make it that width.
104368e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer  /// @brief Zero extend or truncate to width
104440f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  APInt zextOrTrunc(unsigned width) const;
104568e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer
10467ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
10477ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Bit Manipulation Operators
10487ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
1049e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Set every bit to 1.
10507a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void setAllBits() {
1051a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    if (isSingleWord())
105298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      VAL = -1ULL;
1053a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    else {
1054a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad      // Set all the bits in all the words.
1055a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad      for (unsigned i = 0; i < getNumWords(); ++i)
1056a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad	pVal[i] = -1ULL;
105798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    }
105898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    // Clear the unused ones
1059a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    clearUnusedBits();
106098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
1061e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1062e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// Set the given bit to 1 whose position is given as "bitPosition".
1063e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Set a given bit to 1.
10647a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void setBit(unsigned bitPosition);
1065e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1066e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Set every bit to 0.
10677a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void clearAllBits() {
10683a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    if (isSingleWord())
106998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      VAL = 0;
10703a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    else
107198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
107298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
1073e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1074e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// Set the given bit to 0 whose position is given as "bitPosition".
1075e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Set a given bit to 0.
10767a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void clearBit(unsigned bitPosition);
1077e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1078e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Toggle every bit to its opposite value.
10797a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void flipAllBits() {
1080a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    if (isSingleWord())
108198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      VAL ^= -1ULL;
1082a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    else {
1083a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad      for (unsigned i = 0; i < getNumWords(); ++i)
1084a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad        pVal[i] ^= -1ULL;
108598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    }
1086a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    clearUnusedBits();
108798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
1088e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
10893a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// Toggle a given bit to its opposite value whose position is given
1090e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// as "bitPosition".
1091e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Toggles a given bit to its opposite value.
10927a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void flipBit(unsigned bitPosition);
1093e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
10947ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
10957ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Value Characterization Functions
10967ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
10977ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
10987ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns the total number of bits.
10999981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned getBitWidth() const {
11003a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    return BitWidth;
11017ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
11027ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
11037ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Here one word's bitwidth equals to that of uint64_t.
11047ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns the number of words to hold the integer value of this APInt.
11057ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @brief Get the number of words.
11069981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned getNumWords() const {
110717893a5fb6b3b4b9e84674dfb7692abb57410dd1Douglas Gregor    return getNumWords(BitWidth);
110817893a5fb6b3b4b9e84674dfb7692abb57410dd1Douglas Gregor  }
110917893a5fb6b3b4b9e84674dfb7692abb57410dd1Douglas Gregor
111017893a5fb6b3b4b9e84674dfb7692abb57410dd1Douglas Gregor  /// Here one word's bitwidth equals to that of uint64_t.
111117893a5fb6b3b4b9e84674dfb7692abb57410dd1Douglas Gregor  /// @returns the number of words to hold the integer value with a
111217893a5fb6b3b4b9e84674dfb7692abb57410dd1Douglas Gregor  /// given bit width.
111317893a5fb6b3b4b9e84674dfb7692abb57410dd1Douglas Gregor  /// @brief Get the number of words.
111417893a5fb6b3b4b9e84674dfb7692abb57410dd1Douglas Gregor  static unsigned getNumWords(unsigned BitWidth) {
11157ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
11168db6a445e679817d9972c44d2bc366732388c7b0Zhou Sheng  }
11178db6a445e679817d9972c44d2bc366732388c7b0Zhou Sheng
1118e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// This function returns the number of active bits which is defined as the
1119e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// bit width minus the number of leading zeros. This is used in several
1120e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// computations to see how "wide" the value is.
1121e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// @brief Compute the number of active bits in the value
11229981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned getActiveBits() const {
1123f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencer    return BitWidth - countLeadingZeros();
1124e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
1125d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
11269d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  /// This function returns the number of active words in the value of this
11279d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  /// APInt. This is used in conjunction with getActiveData to extract the raw
11289d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  /// value of the APInt.
11299981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned getActiveWords() const {
113084b4eeccc70b39f975a82ad098413d129d38a7d3Reid Spencer    return whichWord(getActiveBits()-1) + 1;
11319d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  }
11329d3c51923392f69b63512e59e44f72affbe6136eReid Spencer
11339d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  /// Computes the minimum bit width for this APInt while considering it to be
11343a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// a signed (and probably negative) value. If the value is not negative,
1135cdff51cabcebcd0eeb227ecf92eabbf8b1aeb77cDan Gohman  /// this function returns the same value as getActiveBits()+1. Otherwise, it
11369d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  /// returns the smallest bit width that will retain the negative value. For
11379d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
11389d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  /// for -1, this function will always return 1.
11393a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// @brief Get the minimum bit size for this signed APInt
11409981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned getMinSignedBits() const {
1141681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer    if (isNegative())
1142681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer      return BitWidth - countLeadingOnes() + 1;
1143cd3c4cac6ea3ce3bff0ce763d45c3f7a2d673178Chris Lattner    return getActiveBits()+1;
1144681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer  }
1145681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer
114631a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// This method attempts to return the value of this APInt as a zero extended
114731a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
114831a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// uint64_t. Otherwise an assertion will result.
114931a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// @brief Get zero extended value
1150c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  uint64_t getZExtValue() const {
1151d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng    if (isSingleWord())
115231a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer      return VAL;
115331a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer    assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
115431a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer    return pVal[0];
115531a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  }
115631a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer
115731a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// This method attempts to return the value of this APInt as a sign extended
115831a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// int64_t. The bit width must be <= 64 or the value must fit within an
115931a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// int64_t. Otherwise an assertion will result.
116031a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// @brief Get sign extended value
1161c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  int64_t getSExtValue() const {
116231a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer    if (isSingleWord())
11633a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman      return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >>
116431a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer                     (APINT_BITS_PER_WORD - BitWidth);
11653864cd4cab956526b99b634e15e7fd5d5441e3a7Eli Friedman    assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
1166946bca5bae2539ca55f8fb4700bff8b5e3f5a7ebReid Spencer    return int64_t(pVal[0]);
1167d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  }
116857ae4f5f01b6f8edf678d77ab935f3662993065dReid Spencer
116957ae4f5f01b6f8edf678d77ab935f3662993065dReid Spencer  /// This method determines how many bits are required to hold the APInt
1170689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  /// equivalent of the string given by \arg str.
117157ae4f5f01b6f8edf678d77ab935f3662993065dReid Spencer  /// @brief Get bits required for string value.
117238e59891ee4417a9be2f8146ce0ba3269e38ac21Benjamin Kramer  static unsigned getBitsNeeded(StringRef str, uint8_t radix);
117357ae4f5f01b6f8edf678d77ab935f3662993065dReid Spencer
1174c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// countLeadingZeros - This function is an APInt version of the
1175c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
1176c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// of zeros from the most significant bit to the first one bit.
11779e513acd3145036bd06b5e0f5bcc83a3e5c08854Chris Lattner  /// @returns BitWidth if the value is zero.
1178d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// @returns the number of zeros from the most significant bit to the first
1179d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// one bits.
11809981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countLeadingZeros() const {
118198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord()) {
11829981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner      unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
118398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return CountLeadingZeros_64(VAL) - unusedBits;
118498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    }
118598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return countLeadingZerosSlowCase();
118698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
1187d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
118842dd77f20702d5ca1e0f3882ad74e7a02fc9589cDan Gohman  /// countLeadingOnes - This function is an APInt version of the
118942dd77f20702d5ca1e0f3882ad74e7a02fc9589cDan Gohman  /// countLeadingOnes_{32,64} functions in MathExtras.h. It counts the number
119042dd77f20702d5ca1e0f3882ad74e7a02fc9589cDan Gohman  /// of ones from the most significant bit to the first zero bit.
1191681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer  /// @returns 0 if the high order bit is not set
1192681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer  /// @returns the number of 1 bits from the most significant to the least
1193681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer  /// @brief Count the number of leading one bits.
11949981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countLeadingOnes() const;
1195681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer
11969b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich  /// Computes the number of leading bits of this APInt that are equal to its
11979b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich  /// sign bit.
11989b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich  unsigned getNumSignBits() const {
11999b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich    return isNegative() ? countLeadingOnes() : countLeadingZeros();
12009b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich  }
12019b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich
12023a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// countTrailingZeros - This function is an APInt version of the
12033a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// countTrailingZeros_{32,64} functions in MathExtras.h. It counts
12049e513acd3145036bd06b5e0f5bcc83a3e5c08854Chris Lattner  /// the number of zeros from the least significant bit to the first set bit.
12059e513acd3145036bd06b5e0f5bcc83a3e5c08854Chris Lattner  /// @returns BitWidth if the value is zero.
1206d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// @returns the number of zeros from the least significant bit to the first
1207d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// one bit.
1208c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// @brief Count the number of trailing zero bits.
12099981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countTrailingZeros() const;
1210d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
12113a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// countTrailingOnes - This function is an APInt version of the
12123a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// countTrailingOnes_{32,64} functions in MathExtras.h. It counts
121342dd77f20702d5ca1e0f3882ad74e7a02fc9589cDan Gohman  /// the number of ones from the least significant bit to the first zero bit.
121442dd77f20702d5ca1e0f3882ad74e7a02fc9589cDan Gohman  /// @returns BitWidth if the value is all ones.
121542dd77f20702d5ca1e0f3882ad74e7a02fc9589cDan Gohman  /// @returns the number of ones from the least significant bit to the first
121642dd77f20702d5ca1e0f3882ad74e7a02fc9589cDan Gohman  /// zero bit.
121742dd77f20702d5ca1e0f3882ad74e7a02fc9589cDan Gohman  /// @brief Count the number of trailing one bits.
12189981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countTrailingOnes() const {
121998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
122098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return CountTrailingOnes_64(VAL);
122198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return countTrailingOnesSlowCase();
122298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
122342dd77f20702d5ca1e0f3882ad74e7a02fc9589cDan Gohman
1224c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// countPopulation - This function is an APInt version of the
1225c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// countPopulation_{32,64} functions in MathExtras.h. It counts the number
12263a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// of 1 bits in the APInt value.
1227c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// @returns 0 if the value is zero.
1228d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// @returns the number of set bits.
1229c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// @brief Count the number of bits set.
12309981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countPopulation() const {
123198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
123298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return CountPopulation_64(VAL);
123398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return countPopulationSlowCase();
123498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
1235d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
12367ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
12377ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Conversion Functions
12387ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
1239944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner  void print(raw_ostream &OS, bool isSigned) const;
12403a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
1241fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  /// toString - Converts an APInt to a string and append it to Str.  Str is
1242fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  /// commonly a SmallString.
1243fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed) const;
12447ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
12457ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Considers the APInt to be unsigned and converts it into a string in the
12467ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// radix given. The radix can be 2, 8, 10 or 16.
1247fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1248af9b6b264f2bf4315c25b286a1c24331038545e0Bill Wendling    toString(Str, Radix, false);
1249e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
1250d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1251df99c29e7b25771d202b969ca5dec359a62897b1Duncan Sands  /// Considers the APInt to be signed and converts it into a string in the
12527ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// radix given. The radix can be 2, 8, 10 or 16.
1253fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1254af9b6b264f2bf4315c25b286a1c24331038545e0Bill Wendling    toString(Str, Radix, true);
1255ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng  }
12563a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
1257fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  /// toString - This returns the APInt as a std::string.  Note that this is an
1258fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  /// inefficient method.  It is better to pass in a SmallVector/SmallString
1259fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  /// to the methods above to avoid thrashing the heap for the string.
1260fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  std::string toString(unsigned Radix, bool Signed) const;
12613a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
1262ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
1263ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng  /// @returns a byte-swapped representation of this APInt Value.
1264e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  APInt byteSwap() const;
1265ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
1266d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng  /// @brief Converts this APInt to a double value.
126766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  double roundToDouble(bool isSigned) const;
126866ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer
126966ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  /// @brief Converts this unsigned APInt to a double value.
127066ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  double roundToDouble() const {
127166ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer    return roundToDouble(false);
127266ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  }
1273ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
127466ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  /// @brief Converts this signed APInt to a double value.
127566ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  double signedRoundToDouble() const {
127666ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer    return roundToDouble(true);
127766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  }
1278af8fb1984674db462bc6923ed54db0275c78b711Reid Spencer
1279ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// The conversion does not do a translation from integer to double, it just
1280ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// re-interprets the bits as a double. Note that it is valid to do this on
1281ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// any bit width. Exactly 64 bits will be translated.
1282ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// @brief Converts APInt bits to a double
1283ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  double bitsToDouble() const {
1284ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    union {
1285ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer      uint64_t I;
1286ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer      double D;
1287ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    } T;
1288ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    T.I = (isSingleWord() ? VAL : pVal[0]);
1289ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    return T.D;
1290ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  }
1291ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer
1292ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// The conversion does not do a translation from integer to float, it just
1293ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// re-interprets the bits as a float. Note that it is valid to do this on
1294ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// any bit width. Exactly 32 bits will be translated.
1295ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// @brief Converts APInt bits to a double
1296ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  float bitsToFloat() const {
1297ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    union {
12989981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner      unsigned I;
1299ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer      float F;
1300ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    } T;
13019981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner    T.I = unsigned((isSingleWord() ? VAL : pVal[0]));
1302ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    return T.F;
1303ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  }
1304ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer
130553ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer  /// The conversion does not do a translation from double to integer, it just
1306e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad  /// re-interprets the bits of the double.
130753ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer  /// @brief Converts a double to APInt bits.
1308e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad  static APInt doubleToBits(double V) {
130953ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    union {
131053ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer      uint64_t I;
131153ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer      double D;
131253ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    } T;
131353ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    T.D = V;
1314e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad    return APInt(sizeof T * CHAR_BIT, T.I);
131553ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer  }
131653ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer
131753ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer  /// The conversion does not do a translation from float to integer, it just
1318e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad  /// re-interprets the bits of the float.
131953ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer  /// @brief Converts a float to APInt bits.
1320e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad  static APInt floatToBits(float V) {
132153ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    union {
13229981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner      unsigned I;
132353ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer      float F;
132453ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    } T;
132553ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    T.F = V;
1326e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad    return APInt(sizeof T * CHAR_BIT, T.I);
132753ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer  }
132853ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer
13297ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
13307ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @name Mathematics Operations
13317ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
13327ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
13337ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @returns the floor log base 2 of this APInt.
13349981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned logBase2() const {
13357ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return BitWidth - 1 - countLeadingZeros();
13367ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
13377ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
1338cbc7cc63b6c7ee1008f92064388c37327c183328Dan Gohman  /// @returns the ceil log base 2 of this APInt.
1339cbc7cc63b6c7ee1008f92064388c37327c183328Dan Gohman  unsigned ceilLogBase2() const {
1340cbc7cc63b6c7ee1008f92064388c37327c183328Dan Gohman    return BitWidth - (*this - 1).countLeadingZeros();
1341cbc7cc63b6c7ee1008f92064388c37327c183328Dan Gohman  }
1342cbc7cc63b6c7ee1008f92064388c37327c183328Dan Gohman
134319dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer  /// @returns the log base 2 of this APInt if its an exact power of two, -1
134419dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer  /// otherwise
1345c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  int32_t exactLogBase2() const {
134619dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer    if (!isPowerOf2())
134719dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer      return -1;
134819dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer    return logBase2();
134919dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer  }
135019dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer
1351af8fb1984674db462bc6923ed54db0275c78b711Reid Spencer  /// @brief Compute the square root
1352af8fb1984674db462bc6923ed54db0275c78b711Reid Spencer  APInt sqrt() const;
1353b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer
1354b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer  /// If *this is < 0 then return -(*this), otherwise *this;
1355b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer  /// @brief Get the absolute value;
1356b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer  APInt abs() const {
1357b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer    if (isNegative())
1358b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer      return -(*this);
1359b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer    return *this;
1360b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer  }
1361fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1362300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  /// @returns the multiplicative inverse for a given modulo.
1363300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  APInt multiplicativeInverse(const APInt& modulo) const;
1364300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz
1365fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// @}
13664e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  /// @name Support for division by constant
13674e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  /// @{
13684e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad
13694e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  /// Calculate the magic number for signed division by a constant.
13704e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  struct ms;
13714e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  ms magic() const;
13724e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad
13734e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  /// Calculate the magic number for unsigned division by a constant.
13744e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  struct mu;
13754e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  mu magicu() const;
13764e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad
13774e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  /// @}
1378fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// @name Building-block Operations for APInt and APFloat
1379fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// @{
1380fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1381fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  // These building block operations operate on a representation of
1382fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  // arbitrary precision, two's-complement, bignum integer values.
1383fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  // They should be sufficient to implement APInt and APFloat bignum
1384fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  // requirements.  Inputs are generally a pointer to the base of an
1385fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  // array of integer parts, representing an unsigned bignum, and a
1386fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  // count of how many parts there are.
1387fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1388fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Sets the least significant part of a bignum to the input value,
1389fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// and zeroes out higher parts.  */
1390fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcSet(integerPart *, integerPart, unsigned int);
1391fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1392fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Assign one bignum to another.
1393fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcAssign(integerPart *, const integerPart *, unsigned int);
1394fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1395fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Returns true if a bignum is zero, false otherwise.
1396fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static bool tcIsZero(const integerPart *, unsigned int);
1397fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1398fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Extract the given bit of a bignum; returns 0 or 1.  Zero-based.
1399fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static int tcExtractBit(const integerPart *, unsigned int bit);
1400fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
140168e53ad6cb0a9d64d256f9dcef70331cd72d795eNeil Booth  /// Copy the bit vector of width srcBITS from SRC, starting at bit
140268e53ad6cb0a9d64d256f9dcef70331cd72d795eNeil Booth  /// srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB
140368e53ad6cb0a9d64d256f9dcef70331cd72d795eNeil Booth  /// becomes the least significant bit of DST.  All high bits above
140468e53ad6cb0a9d64d256f9dcef70331cd72d795eNeil Booth  /// srcBITS in DST are zero-filled.
1405a2769a33c94f021a609a462b28ebea069eba6f74Misha Brukman  static void tcExtract(integerPart *, unsigned int dstCount,
1406a2769a33c94f021a609a462b28ebea069eba6f74Misha Brukman                        const integerPart *,
140768e53ad6cb0a9d64d256f9dcef70331cd72d795eNeil Booth                        unsigned int srcBits, unsigned int srcLSB);
140868e53ad6cb0a9d64d256f9dcef70331cd72d795eNeil Booth
1409fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Set the given bit of a bignum.  Zero-based.
1410fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcSetBit(integerPart *, unsigned int bit);
1411fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1412e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall  /// Clear the given bit of a bignum.  Zero-based.
1413e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall  static void tcClearBit(integerPart *, unsigned int bit);
1414e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall
1415fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Returns the bit number of the least or most significant set bit
1416fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// of a number.  If the input number has no bits set -1U is
1417fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// returned.
1418fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static unsigned int tcLSB(const integerPart *, unsigned int);
141998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  static unsigned int tcMSB(const integerPart *parts, unsigned int n);
1420fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1421fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Negate a bignum in-place.
1422fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcNegate(integerPart *, unsigned int);
1423fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1424fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// DST += RHS + CARRY where CARRY is zero or one.  Returns the
1425fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// carry flag.
1426fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static integerPart tcAdd(integerPart *, const integerPart *,
1427e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                           integerPart carry, unsigned);
1428fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1429fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// DST -= RHS + CARRY where CARRY is zero or one.  Returns the
1430fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// carry flag.
1431fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static integerPart tcSubtract(integerPart *, const integerPart *,
1432e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                                integerPart carry, unsigned);
1433fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1434fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  DST += SRC * MULTIPLIER + PART   if add is true
1435fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  DST  = SRC * MULTIPLIER + PART   if add is false
1436fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///
1437fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  Requires 0 <= DSTPARTS <= SRCPARTS + 1.  If DST overlaps SRC
1438fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  they must start at the same point, i.e. DST == SRC.
1439fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///
1440fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is
1441fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  returned.  Otherwise DST is filled with the least significant
1442fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  DSTPARTS parts of the result, and if all of the omitted higher
1443fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  parts were zero return zero, otherwise overflow occurred and
1444fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  return one.
1445fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static int tcMultiplyPart(integerPart *dst, const integerPart *src,
1446e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                            integerPart multiplier, integerPart carry,
1447e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                            unsigned int srcParts, unsigned int dstParts,
1448e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                            bool add);
1449fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1450fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// DST = LHS * RHS, where DST has the same width as the operands
1451fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// and is filled with the least significant parts of the result.
1452fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Returns one if overflow occurred, otherwise zero.  DST must be
1453fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// disjoint from both operands.
1454fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static int tcMultiply(integerPart *, const integerPart *,
1455e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                        const integerPart *, unsigned);
1456fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1457978661d05301a9bcd1222c048affef679da5ac43Neil Booth  /// DST = LHS * RHS, where DST has width the sum of the widths of
1458978661d05301a9bcd1222c048affef679da5ac43Neil Booth  /// the operands.  No overflow occurs.  DST must be disjoint from
1459978661d05301a9bcd1222c048affef679da5ac43Neil Booth  /// both operands. Returns the number of parts required to hold the
1460978661d05301a9bcd1222c048affef679da5ac43Neil Booth  /// result.
1461978661d05301a9bcd1222c048affef679da5ac43Neil Booth  static unsigned int tcFullMultiply(integerPart *, const integerPart *,
1462e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                                     const integerPart *, unsigned, unsigned);
1463fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1464fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1465fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Otherwise set LHS to LHS / RHS with the fractional part
1466fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// discarded, set REMAINDER to the remainder, return zero.  i.e.
1467fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///
1468fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  OLD_LHS = RHS * LHS + REMAINDER
1469fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///
1470fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  SCRATCH is a bignum of the same size as the operands and result
1471fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  for use by the routine; its contents need not be initialized
1472fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  and are destroyed.  LHS, REMAINDER and SCRATCH must be
1473fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  distinct.
1474fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static int tcDivide(integerPart *lhs, const integerPart *rhs,
1475e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                      integerPart *remainder, integerPart *scratch,
1476e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                      unsigned int parts);
1477fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1478fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Shift a bignum left COUNT bits.  Shifted in bits are zero.
1479fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// There are no restrictions on COUNT.
1480fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcShiftLeft(integerPart *, unsigned int parts,
1481e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                          unsigned int count);
1482fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1483fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Shift a bignum right COUNT bits.  Shifted in bits are zero.
1484fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// There are no restrictions on COUNT.
1485fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcShiftRight(integerPart *, unsigned int parts,
1486e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                           unsigned int count);
1487fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1488fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// The obvious AND, OR and XOR and complement operations.
1489fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcAnd(integerPart *, const integerPart *, unsigned int);
1490fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcOr(integerPart *, const integerPart *, unsigned int);
1491fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcXor(integerPart *, const integerPart *, unsigned int);
1492fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcComplement(integerPart *, unsigned int);
14933a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
1494fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Comparison (unsigned) of two bignums.
1495fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static int tcCompare(const integerPart *, const integerPart *,
1496e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                       unsigned int);
1497fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1498fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Increment a bignum in-place.  Return the carry flag.
1499fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static integerPart tcIncrement(integerPart *, unsigned int);
1500fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1501fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Set the least significant BITS and clear the rest.
1502fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcSetLeastSignificantBits(integerPart *, unsigned int,
1503e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                                        unsigned int bits);
1504fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
15059e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen  /// @brief debug method
15069e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen  void dump() const;
15079e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen
15087ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
1509d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng};
1510d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
15114e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad/// Magic data for optimising signed division by a constant.
15124e5ea553d055512b0b8aa098e363ae17bafda957Jay Foadstruct APInt::ms {
15134e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  APInt m;  ///< magic number
15144e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  unsigned s;  ///< shift amount
15154e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad};
15164e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad
15174e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad/// Magic data for optimising unsigned division by a constant.
15184e5ea553d055512b0b8aa098e363ae17bafda957Jay Foadstruct APInt::mu {
15194e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  APInt m;     ///< magic number
15204e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  bool a;      ///< add indicator
15214e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  unsigned s;  ///< shift amount
15224e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad};
15234e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad
15249d3c51923392f69b63512e59e44f72affbe6136eReid Spencerinline bool operator==(uint64_t V1, const APInt& V2) {
15259d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  return V2 == V1;
15269d3c51923392f69b63512e59e44f72affbe6136eReid Spencer}
15279d3c51923392f69b63512e59e44f72affbe6136eReid Spencer
15289d3c51923392f69b63512e59e44f72affbe6136eReid Spencerinline bool operator!=(uint64_t V1, const APInt& V2) {
15299d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  return V2 != V1;
15309d3c51923392f69b63512e59e44f72affbe6136eReid Spencer}
15319d3c51923392f69b63512e59e44f72affbe6136eReid Spencer
1532944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattnerinline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) {
1533fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  I.print(OS, true);
1534fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  return OS;
1535fad86b003a839cef40ec8ce8408322f4913368caChris Lattner}
15363a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
15370b706b18bd0a7760d971727460a1f26bff8289b0Zhou Shengnamespace APIntOps {
15380b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1539f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer/// @brief Determine the smaller of two APInts considered to be signed.
1540f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencerinline APInt smin(const APInt &A, const APInt &B) {
1541f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer  return A.slt(B) ? A : B;
1542f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer}
1543f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer
1544f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer/// @brief Determine the larger of two APInts considered to be signed.
1545f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencerinline APInt smax(const APInt &A, const APInt &B) {
1546f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer  return A.sgt(B) ? A : B;
1547f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer}
1548f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer
1549f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer/// @brief Determine the smaller of two APInts considered to be signed.
1550f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencerinline APInt umin(const APInt &A, const APInt &B) {
1551f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer  return A.ult(B) ? A : B;
1552f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer}
1553f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer
1554f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer/// @brief Determine the larger of two APInts considered to be unsigned.
1555f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencerinline APInt umax(const APInt &A, const APInt &B) {
1556f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer  return A.ugt(B) ? A : B;
1557f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer}
1558f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer
1559ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman/// @brief Check if the specified APInt has a N-bits unsigned integer value.
15609981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattnerinline bool isIntN(unsigned N, const APInt& APIVal) {
1561e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return APIVal.isIntN(N);
1562d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng}
1563d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1564ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman/// @brief Check if the specified APInt has a N-bits signed integer value.
15659981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattnerinline bool isSignedIntN(unsigned N, const APInt& APIVal) {
1566ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman  return APIVal.isSignedIntN(N);
1567ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman}
1568ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman
1569d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns true if the argument APInt value is a sequence of ones
1570d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// starting at the least significant bit with the remainder zero.
15719981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattnerinline bool isMask(unsigned numBits, const APInt& APIVal) {
15728eab8a2798fe74c98703bdeac64661beea0b4dbcDuncan Sands  return numBits <= APIVal.getBitWidth() &&
15738eab8a2798fe74c98703bdeac64661beea0b4dbcDuncan Sands    APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
1574d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng}
1575d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1576d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns true if the argument APInt value contains a sequence of ones
1577d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// with the remainder zero.
15789981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattnerinline bool isShiftedMask(unsigned numBits, const APInt& APIVal) {
1579e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
1580d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng}
1581d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1582d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns a byte-swapped representation of the specified APInt Value.
1583e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt byteSwap(const APInt& APIVal) {
1584e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return APIVal.byteSwap();
1585ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
1586d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1587d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// @returns the floor log base 2 of the specified APInt value.
15889981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattnerinline unsigned logBase2(const APInt& APIVal) {
15893a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  return APIVal.logBase2();
1590d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng}
1591d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1592c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// GreatestCommonDivisor - This function returns the greatest common
1593300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz/// divisor of the two APInt values using Euclid's algorithm.
1594c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// @returns the greatest common divisor of Val1 and Val2
1595c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// @brief Compute GCD of two APInt values.
1596c68c2243dcb716bc117f41aa543ca8a41046df41Reid SpencerAPInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
1597d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
159866ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer/// Treats the APInt as an unsigned value for conversion purposes.
159966ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer/// @brief Converts the given APInt to a double value.
160066ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencerinline double RoundAPIntToDouble(const APInt& APIVal) {
160166ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  return APIVal.roundToDouble();
160266ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer}
160366ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer
160466ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer/// Treats the APInt as a signed value for conversion purposes.
1605d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given APInt to a double value.
160666ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencerinline double RoundSignedAPIntToDouble(const APInt& APIVal) {
160766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  return APIVal.signedRoundToDouble();
1608d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng}
1609d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng
1610d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given APInt to a float vlalue.
1611e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline float RoundAPIntToFloat(const APInt& APIVal) {
1612e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return float(RoundAPIntToDouble(APIVal));
1613d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng}
1614d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng
161552f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencer/// Treast the APInt as a signed value for conversion purposes.
161652f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencer/// @brief Converts the given APInt to a float value.
161752f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencerinline float RoundSignedAPIntToFloat(const APInt& APIVal) {
161852f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencer  return float(APIVal.signedRoundToDouble());
161952f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencer}
162052f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencer
1621c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// RoundDoubleToAPInt - This function convert a double value to an APInt value.
1622d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng/// @brief Converts the given double value into a APInt.
16239981b1f15681a2a09c9953cf6773a3b839ead4e7Chris LattnerAPInt RoundDoubleToAPInt(double Double, unsigned width);
1624d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng
1625c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// RoundFloatToAPInt - Converts a float value into an APInt value.
1626c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer/// @brief Converts a float value into a APInt.
16279981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattnerinline APInt RoundFloatToAPInt(float Float, unsigned width) {
162852f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencer  return RoundDoubleToAPInt(double(Float), width);
1629d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng}
1630d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng
16310b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Arithmetic right-shift the APInt by shiftAmt.
16320b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Arithmetic right-shift function.
16339981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattnerinline APInt ashr(const APInt& LHS, unsigned shiftAmt) {
1634e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return LHS.ashr(shiftAmt);
1635ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
16360b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
16370b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Logical right-shift the APInt by shiftAmt.
16380b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Logical right-shift function.
16399981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattnerinline APInt lshr(const APInt& LHS, unsigned shiftAmt) {
1640e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return LHS.lshr(shiftAmt);
1641ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
16420b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
16430b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Left-shift the APInt by shiftAmt.
16440b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Left-shift function.
16459981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattnerinline APInt shl(const APInt& LHS, unsigned shiftAmt) {
1646e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return LHS.shl(shiftAmt);
1647ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
16480b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
16490b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Signed divide APInt LHS by APInt RHS.
16500b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Signed division function for APInt.
1651e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt sdiv(const APInt& LHS, const APInt& RHS) {
1652e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return LHS.sdiv(RHS);
16530b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng}
16540b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
16550b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Unsigned divide APInt LHS by APInt RHS.
16560b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Unsigned division function for APInt.
1657e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt udiv(const APInt& LHS, const APInt& RHS) {
1658e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return LHS.udiv(RHS);
1659ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
16600b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
16610b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Signed remainder operation on APInt.
16620b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for signed remainder operation.
1663e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt srem(const APInt& LHS, const APInt& RHS) {
1664e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return LHS.srem(RHS);
16650b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng}
16660b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
16670b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Unsigned remainder operation on APInt.
16680b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for unsigned remainder operation.
1669e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt urem(const APInt& LHS, const APInt& RHS) {
1670e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return LHS.urem(RHS);
1671ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
16720b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
16730b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs multiplication on APInt values.
16740b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for multiplication operation.
1675e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt mul(const APInt& LHS, const APInt& RHS) {
16760b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng  return LHS * RHS;
16770b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng}
16780b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
16790b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs addition on APInt values.
16800b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for addition operation.
1681e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt add(const APInt& LHS, const APInt& RHS) {
16820b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng  return LHS + RHS;
16830b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng}
16840b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
16850b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs subtraction on APInt values.
16860b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// @brief Function for subtraction operation.
1687e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencerinline APInt sub(const APInt& LHS, const APInt& RHS) {
16880b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng  return LHS - RHS;
16890b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng}
16900b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
16913a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman/// Performs bitwise AND operation on APInt LHS and
1692ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// APInt RHS.
1693ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise AND function for APInt.
1694ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt And(const APInt& LHS, const APInt& RHS) {
1695ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng  return LHS & RHS;
1696ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
1697ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
1698ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise OR operation on APInt LHS and APInt RHS.
16993a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman/// @brief Bitwise OR function for APInt.
1700ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Or(const APInt& LHS, const APInt& RHS) {
1701ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng  return LHS | RHS;
1702ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
1703ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
1704ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise XOR operation on APInt.
1705ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// @brief Bitwise XOR function for APInt.
1706ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Xor(const APInt& LHS, const APInt& RHS) {
1707ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng  return LHS ^ RHS;
17083a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman}
1709ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
1710ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs a bitwise complement operation on APInt.
17113a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman/// @brief Bitwise complement function.
1712ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Shenginline APInt Not(const APInt& APIVal) {
1713ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng  return ~APIVal;
1714ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
1715ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
17160b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} // End of APIntOps namespace
17170b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1718d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} // End of llvm namespace
1719d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1720d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#endif
1721