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//===----------------------------------------------------------------------===//
9a5196664236bfadccfc3b805079072340e0f542dMichael Gottesman///
10cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \file
11cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief This file implements a class to represent arbitrary precision
12cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// integral constant values and operations on them.
13cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
14d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===//
15d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
16674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_ADT_APINT_H
17674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_ADT_APINT_H
18d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
193ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin#include "llvm/ADT/ArrayRef.h"
200fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer#include "llvm/Support/Compiler.h"
2198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner#include "llvm/Support/MathExtras.h"
2258a0d64fae7ace05b27dad94fd427991d853619bLauro Ramos Venancio#include <cassert>
23de551f91d8816632a76a065084caab9fab6aacffDan Gohman#include <climits>
24cbd56db62b7fa5a685bdca2eddefe9d04a1295f8Nick Lewycky#include <cstring>
25d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#include <string>
26d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
27d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Shengnamespace llvm {
28612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmanclass Deserializer;
29612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmanclass FoldingSetNodeID;
30612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmanclass Serializer;
31612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmanclass StringRef;
32612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmanclass hash_code;
33612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmanclass raw_ostream;
343a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
35612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmantemplate <typename T> class SmallVectorImpl;
363a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
37612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman// An unsigned host type used as a single part of a multi-part
38612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman// bignum.
39612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmantypedef uint64_t integerPart;
40fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
41612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmanconst unsigned int host_char_bit = 8;
42612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmanconst unsigned int integerPartWidth =
43612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman    host_char_bit * static_cast<unsigned int>(sizeof(integerPart));
44b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
45d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===//
46d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//                              APInt Class
47d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng//===----------------------------------------------------------------------===//
48d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
49cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Class for arbitrary precision integers.
50cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
51cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// APInt is a functional replacement for common case unsigned integer type like
523a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman/// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
53e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// integer sizes and large integer value types such as 3-bits, 15-bits, or more
543a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman/// than 64-bits of precision. APInt provides a variety of arithmetic operators
55e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// and methods to manipulate integer values of any bit-width. It supports both
56e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// the typical integer arithmetic and comparison operations as well as bitwise
57e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// manipulation.
58d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng///
59e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer/// The class has several invariants worth noting:
60e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///   * All bit, byte, and word positions are zero-based.
613a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman///   * Once the bit width is set, it doesn't change except by the Truncate,
62e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     SignExtend, or ZeroExtend operations.
63e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///   * All binary operators must be on APInt instances of the same bit width.
643a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman///     Attempting to use these operators on instances with different bit
65e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     widths will yield an assertion.
66e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///   * The value is stored canonically as an unsigned value. For operations
67e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     where it makes a difference, there are both signed and unsigned variants
68e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     of the operation. For example, sdiv and udiv. However, because the bit
69e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     widths must be the same, operations such as Mul and Add produce the same
70e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     results regardless of whether the values are interpreted as signed or
71e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     not.
72e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///   * In general, the class tries to follow the style of computation that LLVM
73e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer///     uses in its IR. This simplifies its use for LLVM.
747406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng///
75d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Shengclass APInt {
76612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  unsigned BitWidth; ///< The number of bits in this APInt.
77d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
78d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  /// This union is used to store the integer value. When the
797ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
80d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  union {
81612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman    uint64_t VAL;   ///< Used to store the <= 64 bits integer value.
82612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman    uint64_t *pVal; ///< Used to store the >64 bits integer value.
83d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  };
84d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
857ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This enum is used to hold the constants we needed for APInt.
86d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  enum {
8748e8c80e1791adb7a07b8fd6e27edcdfbb756950Evan Cheng    /// Bits in a word
88612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman    APINT_BITS_PER_WORD =
89612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman        static_cast<unsigned int>(sizeof(uint64_t)) * CHAR_BIT,
9048e8c80e1791adb7a07b8fd6e27edcdfbb756950Evan Cheng    /// Byte size of a word
9148e8c80e1791adb7a07b8fd6e27edcdfbb756950Evan Cheng    APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
92d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  };
93d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
94cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Fast internal constructor
95cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
967ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This constructor is used only internally for speed of construction of
977ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// temporaries. It is unsafe for general use so it is not public.
98612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt(uint64_t *val, unsigned bits) : BitWidth(bits), pVal(val) {}
9920b1f5db1e93d28debc6449dcd56da6ca7aa6b67Reid Spencer
100cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Determine if this APInt just has one word to store value.
101cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
102cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if the number of bits <= 64, false otherwise.
103612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; }
104d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
105cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Determine which word a bit is in.
106cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
107cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the word position for the specified bit position.
1089981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static unsigned whichWord(unsigned bitPosition) {
1093a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    return bitPosition / APINT_BITS_PER_WORD;
110e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
111d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
112cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Determine which bit in a word a bit is in.
113cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
114cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the bit position in a word for the specified bit position
1157ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// in the APInt.
1169981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static unsigned whichBit(unsigned bitPosition) {
1173a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    return bitPosition % APINT_BITS_PER_WORD;
118e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
119d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
120cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get a single bit mask.
121cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
122cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set
1233a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// This method generates and returns a uint64_t (word) mask for a single
1243a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// bit at a specific bit position. This is used to mask the bit in the
1257ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// corresponding word.
1269981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static uint64_t maskBit(unsigned bitPosition) {
1273a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    return 1ULL << whichBit(bitPosition);
128e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
129d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
130cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Clear unused high order bits
131cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1327ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This method is used internally to clear the to "N" bits in the high order
1333a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// word that are not used by the APInt. This is needed after the most
1343a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// significant word is assigned a value to ensure that those bits are
1357ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// zero'd out.
136612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &clearUnusedBits() {
137c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer    // Compute how many bits are used in the final word
1389981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner    unsigned wordBits = BitWidth % APINT_BITS_PER_WORD;
139c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer    if (wordBits == 0)
140c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer      // If all bits are used, we want to leave the value alone. This also
141cd2ad1df5d2e0600200c81bc214e0b65ff699c5fDan Gohman      // avoids the undefined behavior of >> when the shift is the same size as
142c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer      // the word size (64).
143c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer      return *this;
144c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer
1458eab8a2798fe74c98703bdeac64661beea0b4dbcDuncan Sands    // Mask out the high bits.
146c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer    uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
147d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng    if (isSingleWord())
148c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer      VAL &= mask;
149d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng    else
150c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer      pVal[getNumWords() - 1] &= mask;
151c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer    return *this;
152d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  }
153d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
154cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get the word corresponding to a bit position
155cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the corresponding word for the specified bit position.
1569981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  uint64_t getWord(unsigned bitPosition) const {
1573a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
158e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
159d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
160cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Convert a char array into an APInt
161cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
162cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param radix 2, 8, 10, 16, or 36
1631e7ad3993d8700488895fa372ecad55443f53485John McCall  /// Converts a string into a number.  The string must be non-empty
1641e7ad3993d8700488895fa372ecad55443f53485John McCall  /// and well-formed as a number of the given base. The bit-width
1651e7ad3993d8700488895fa372ecad55443f53485John McCall  /// must be sufficient to hold the result.
1661e7ad3993d8700488895fa372ecad55443f53485John McCall  ///
167d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer  /// This is used by the constructors that take string arguments.
1681e7ad3993d8700488895fa372ecad55443f53485John McCall  ///
1691e7ad3993d8700488895fa372ecad55443f53485John McCall  /// StringRef::getAsInteger is superficially similar but (1) does
1701e7ad3993d8700488895fa372ecad55443f53485John McCall  /// not assume that the string is well-formed and (2) grows the
1711e7ad3993d8700488895fa372ecad55443f53485John McCall  /// result to hold the input.
17238e59891ee4417a9be2f8146ce0ba3269e38ac21Benjamin Kramer  void fromString(unsigned numBits, StringRef str, uint8_t radix);
173f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng
174cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief An internal division function for dividing APInts.
175cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
176d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer  /// This is used by the toString method to divide by the radix. It simply
177c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// provides a more convenient form of divide for internal use since KnuthDiv
178c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// has specific constraints on its inputs. If those constraints are not met
179c68c2243dcb716bc117f41aa543ca8a41046df41Reid Spencer  /// then it provides a simpler form of divide.
180612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  static void divide(const APInt LHS, unsigned lhsWords, const APInt &RHS,
181612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman                     unsigned rhsWords, APInt *Quotient, APInt *Remainder);
182d03d012ad97fad9f91b107d206dadad4c34bebefReid Spencer
18398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for inline constructor
1849981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  void initSlowCase(unsigned numBits, uint64_t val, bool isSigned);
18598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
1863ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  /// shared code between two array constructors
1873ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  void initFromArray(ArrayRef<uint64_t> array);
1883ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin
18998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for inline copy constructor
190612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  void initSlowCase(const APInt &that);
19198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
19298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for shl
1939981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt shlSlowCase(unsigned shiftAmt) const;
19498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
19598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator&
196612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt AndSlowCase(const APInt &RHS) const;
19798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
19898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator|
199612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt OrSlowCase(const APInt &RHS) const;
20098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
20198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator^
202612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt XorSlowCase(const APInt &RHS) const;
20398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
20498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator=
205612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &AssignSlowCase(const APInt &RHS);
20698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
20798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator==
208612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool EqualSlowCase(const APInt &RHS) const;
20998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
21098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for operator==
21198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  bool EqualSlowCase(uint64_t Val) const;
21298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
21398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for countLeadingZeros
2149981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countLeadingZerosSlowCase() const;
21598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
21698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for countTrailingOnes
2179981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countTrailingOnesSlowCase() const;
21898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
21998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  /// out-of-line slow case for countPopulation
2209981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countPopulationSlowCase() const;
22198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
222f31c784f2774311d1f7194ac4ca86262197a8099Reid Spencerpublic:
223cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Constructors
2247ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
225cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
226cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Create a new APInt of numBits width, initialized as val.
227cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
2287ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// If isSigned is true then val is treated as if it were a signed value
2297ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
2307ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// will be done. Otherwise, no sign extension occurs (high order bits beyond
2317ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the range of val are zero filled).
232cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
233cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param numBits the bit width of the constructed APInt
234cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param val the initial value of the APInt
235cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param isSigned how to treat signedness of val
2369981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt(unsigned numBits, uint64_t val, bool isSigned = false)
237612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman      : BitWidth(numBits), VAL(0) {
23898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth && "bitwidth too small");
23998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
24098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      VAL = val;
24198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    else
24298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      initSlowCase(numBits, val, isSigned);
24398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    clearUnusedBits();
24498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
245d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
246cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Construct an APInt of numBits width, initialized as bigVal[].
247cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
2483ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  /// Note that bigVal.size() can be smaller or larger than the corresponding
2493ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  /// bit width but any extraneous bits will be dropped.
250cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
251cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param numBits the bit width of the constructed APInt
252cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param bigVal a sequence of words to form the initial value of the APInt
2533ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  APInt(unsigned numBits, ArrayRef<uint64_t> bigVal);
254cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
2553ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  /// Equivalent to APInt(numBits, ArrayRef<uint64_t>(bigVal, numWords)), but
2563ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  /// deprecated because this constructor is prone to ambiguity with the
2573ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  /// APInt(unsigned, uint64_t, bool) constructor.
2583ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  ///
2593ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  /// If this overload is ever deleted, care should be taken to prevent calls
2603ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  /// from being incorrectly captured by the APInt(unsigned, uint64_t, bool)
2613ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin  /// constructor.
2629981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
263d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
264cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Construct an APInt from a string representation.
265cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
2662d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko  /// This constructor interprets the string \p str in the given radix. The
267689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  /// interpretation stops when the first character that is not suitable for the
268689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  /// radix is encountered, or the end of the string. Acceptable radix values
269a5196664236bfadccfc3b805079072340e0f542dMichael Gottesman  /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the
270dcd999624159842886d4be21efcc3ba0e61bab99Douglas Gregor  /// string to require more bits than numBits.
271689ad6ef3fd2e89394f1e8860dfebfe56b73c3daDaniel Dunbar  ///
272cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param numBits the bit width of the constructed APInt
273cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param str the string to be interpreted
274a5196664236bfadccfc3b805079072340e0f542dMichael Gottesman  /// \param radix the radix to use for the conversion
27538e59891ee4417a9be2f8146ce0ba3269e38ac21Benjamin Kramer  APInt(unsigned numBits, StringRef str, uint8_t radix);
2767ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
2777ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Simply makes *this a copy of that.
2787406dcdc2981ae864a22ba34f96d1db5e3a4cf87Zhou Sheng  /// @brief Copy Constructor.
279612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt(const APInt &that) : BitWidth(that.BitWidth), VAL(0) {
28098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth && "bitwidth too small");
2813a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    if (isSingleWord())
28298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      VAL = that.VAL;
28398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    else
28498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      initSlowCase(that);
28598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
286d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
2874334dd96a9e622fdcf2825a8f73a2d941d67be72Chandler Carruth#if LLVM_HAS_RVALUE_REFERENCES
288cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Move Constructor.
289612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt(APInt &&that) : BitWidth(that.BitWidth), VAL(that.VAL) {
2900fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer    that.BitWidth = 0;
2910fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer  }
2920fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer#endif
2930fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer
294cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Destructor.
29598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  ~APInt() {
296abff3aa8217049cffe6da77c91e510c66b8c2313Manuel Klimek    if (needsCleanup())
297612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman      delete[] pVal;
29898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
29998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
300cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Default constructor that creates an uninitialized APInt.
301cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
302cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// This is useful for object deserialization (pair this with the static
303cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///  method Read).
304586504d5ed35d5765652370c4b4e68eb21b2b34aTed Kremenek  explicit APInt() : BitWidth(1) {}
3053a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
306abff3aa8217049cffe6da77c91e510c66b8c2313Manuel Klimek  /// \brief Returns whether this instance allocated memory.
307abff3aa8217049cffe6da77c91e510c66b8c2313Manuel Klimek  bool needsCleanup() const { return !isSingleWord(); }
308abff3aa8217049cffe6da77c91e510c66b8c2313Manuel Klimek
309cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Used to insert APInt objects, or objects that contain APInt objects, into
310cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///  FoldingSets.
311612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  void Profile(FoldingSetNodeID &id) const;
3123a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
3137ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
314cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Value Tests
3157ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
316cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
317cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Determine sign of this APInt.
318cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
3197ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This tests the high bit of this APInt to determine if it is set.
320cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
321cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if this APInt is negative, false otherwise
322612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool isNegative() const { return (*this)[BitWidth - 1]; }
323d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
324cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Determine if this APInt Value is non-negative (>= 0)
325cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
3267ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This tests the high bit of the APInt to determine if it is unset.
327612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool isNonNegative() const { return !isNegative(); }
3287ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
329cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Determine if this APInt Value is positive.
330cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
3317649127ec152e226d761845983ba91e69712d7daDan Gohman  /// This tests if the value of this APInt is positive (> 0). Note
3327649127ec152e226d761845983ba91e69712d7daDan Gohman  /// that 0 is not a positive value.
333cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
334cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if this APInt is positive.
335612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool isStrictlyPositive() const { return isNonNegative() && !!*this; }
3367ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
337cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Determine if all bits are set
338cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
3397ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This checks to see if the value has all bits of the APInt are set or not.
3408dcff889825c7f38dab27a349b7a21f186a5ff9eBenjamin Kramer  bool isAllOnesValue() const {
3418dcff889825c7f38dab27a349b7a21f186a5ff9eBenjamin Kramer    if (isSingleWord())
3428dcff889825c7f38dab27a349b7a21f186a5ff9eBenjamin Kramer      return VAL == ~integerPart(0) >> (APINT_BITS_PER_WORD - BitWidth);
3438dcff889825c7f38dab27a349b7a21f186a5ff9eBenjamin Kramer    return countPopulationSlowCase() == BitWidth;
3448dcff889825c7f38dab27a349b7a21f186a5ff9eBenjamin Kramer  }
3457ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
346cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Determine if this is the largest unsigned value.
347cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
3487ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This checks to see if the value of this APInt is the maximum unsigned
3497ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// value for the APInt's bit width.
3508dcff889825c7f38dab27a349b7a21f186a5ff9eBenjamin Kramer  bool isMaxValue() const { return isAllOnesValue(); }
3517ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
352cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Determine if this is the largest signed value.
353cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
3547ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This checks to see if the value of this APInt is the maximum signed
3557ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// value for the APInt's bit width.
3567ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  bool isMaxSignedValue() const {
357612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman    return BitWidth == 1 ? VAL == 0
358612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman                         : !isNegative() && countPopulation() == BitWidth - 1;
3597ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3607ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
361cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Determine if this is the smallest unsigned value.
362cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
3637ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This checks to see if the value of this APInt is the minimum unsigned
3647ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// value for the APInt's bit width.
365612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool isMinValue() const { return !*this; }
3667ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
367cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Determine if this is the smallest signed value.
368cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
3697ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This checks to see if the value of this APInt is the minimum signed
3707ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// value for the APInt's bit width.
3717ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  bool isMinSignedValue() const {
372fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer    return BitWidth == 1 ? VAL == 1 : isNegative() && isPowerOf2();
3737ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3747ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
375cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Check if this APInt has an N-bits unsigned integer value.
3769981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  bool isIntN(unsigned N) const {
3777ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    assert(N && "N == 0 ???");
378a77b95a316e0eb04929c5d7fe96935124c3ed478Benjamin Kramer    return getActiveBits() <= N;
3797ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
3807ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
381cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Check if this APInt has an N-bits signed integer value.
3829981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  bool isSignedIntN(unsigned N) const {
383ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman    assert(N && "N == 0 ???");
384ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman    return getMinSignedBits() <= N;
385ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman  }
386ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman
387cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Check if this APInt's value is a power of two greater than zero.
388cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
389cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if the argument APInt value is a power of two > 0.
390fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer  bool isPowerOf2() const {
391fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer    if (isSingleWord())
392fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer      return isPowerOf2_64(VAL);
393fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer    return countPopulationSlowCase() == 1;
394fd6d53fbad9d3f48da4910ebba12d9d2a3c24bd8Benjamin Kramer  }
3957ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
396cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Check if the APInt's value is returned by getSignBit.
397cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
398cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if this is the value returned by getSignBit.
399febabcc02a1303a8e4d9bed76718a32f0273607aChris Lattner  bool isSignBit() const { return isMinSignedValue(); }
4003a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
401cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Convert APInt to a boolean value.
402cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
40348a3e98c272d673f075c51d078140765fc267eb8Duncan Sands  /// This converts the APInt to a boolean value as a test against zero.
404612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool getBoolValue() const { return !!*this; }
4057ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
406cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// If this value is smaller than the specified limit, return it, otherwise
407cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// return the limit value.  This causes the value to saturate to the limit.
4084bda52d6b5ffa13314fe32baa1784566f70483f9Chris Lattner  uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
409612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman    return (getActiveBits() > 64 || getZExtValue() > Limit) ? Limit
410612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman                                                            : getZExtValue();
4114bda52d6b5ffa13314fe32baa1784566f70483f9Chris Lattner  }
4124bda52d6b5ffa13314fe32baa1784566f70483f9Chris Lattner
4137ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
414cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Value Generators
4157ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
416cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
417cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Gets maximum unsigned value of APInt for specific bit width.
4189981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getMaxValue(unsigned numBits) {
419bd7d2622a2ee828a02017dca19170b6f79ff6684Benjamin Kramer    return getAllOnesValue(numBits);
4207ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
4217ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
422cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Gets maximum signed value of APInt for a specific bit width.
4239981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getSignedMaxValue(unsigned numBits) {
424bd7d2622a2ee828a02017dca19170b6f79ff6684Benjamin Kramer    APInt API = getAllOnesValue(numBits);
4257a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad    API.clearBit(numBits - 1);
426a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    return API;
4277ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
4287ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
429cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Gets minimum unsigned value of APInt for a specific bit width.
430612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); }
4317ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
432cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Gets minimum signed value of APInt for a specific bit width.
4339981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getSignedMinValue(unsigned numBits) {
434a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    APInt API(numBits, 0);
4357a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad    API.setBit(numBits - 1);
436a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    return API;
4377ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
4387ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
439cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get the SignBit for a specific bit width.
440cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
441cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// This is just a wrapper function of getSignedMinValue(), and it helps code
442cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// readability when we want to get a SignBit.
4439981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getSignBit(unsigned BitWidth) {
4447ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return getSignedMinValue(BitWidth);
4457ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
4467ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
447cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get the all-ones value.
448cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
449cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the all-ones value for an APInt of the specified bit-width.
4509981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getAllOnesValue(unsigned numBits) {
4518660057d189eb567777ce5bca2b479da8108b5adAaron Ballman    return APInt(numBits, UINT64_MAX, true);
4527ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
4537ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
454cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get the '0' value.
455cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
456cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the '0' value for an APInt of the specified bit-width.
457612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); }
4587ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
459cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Compute an APInt containing numBits highbits from this APInt.
460cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
4617ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Get an APInt with the same BitWidth as this APInt, just zero mask
4627ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the low bits and right shift to the least significant bit.
463cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
464cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the high "numBits" bits of this APInt.
4659981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt getHiBits(unsigned numBits) const;
466d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
467cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Compute an APInt containing numBits lowbits from this APInt.
468cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
4697ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Get an APInt with the same BitWidth as this APInt, just zero mask
4707ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the high bits.
471cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
472cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the low "numBits" bits of this APInt.
4739981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt getLoBits(unsigned numBits) const;
4747ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
475cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Return an APInt with exactly one bit set in the result.
47616e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner  static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
47716e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner    APInt Res(numBits, 0);
47816e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner    Res.setBit(BitNo);
47916e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner    return Res;
48016e036fa8f1d1da02deeaf9a77951f4dc711faa1Chris Lattner  }
481a5196664236bfadccfc3b805079072340e0f542dMichael Gottesman
482cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get a value with a block of bits set.
483cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
4847ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Constructs an APInt value that has a contiguous range of bits set. The
485ffe3e2514a2550152267072ed7afeebb3dd3016cDan Gohman  /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
486ffe3e2514a2550152267072ed7afeebb3dd3016cDan Gohman  /// bits will be zero. For example, with parameters(32, 0, 16) you would get
487ffe3e2514a2550152267072ed7afeebb3dd3016cDan Gohman  /// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For
4883a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// example, with parameters (32, 28, 4), you would get 0xF000000F.
489cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
490cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param numBits the intended bit width of the result
491cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param loBit the index of the lowest bit set.
492cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param hiBit the index of the highest bit set.
493cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
494cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns An APInt value with the requested bits set.
4959981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
496ffe3e2514a2550152267072ed7afeebb3dd3016cDan Gohman    assert(hiBit <= numBits && "hiBit out of range");
4978f969ee62c3fed26f835a37c41f7c009f99ea3bfReid Spencer    assert(loBit < numBits && "loBit out of range");
4988f969ee62c3fed26f835a37c41f7c009f99ea3bfReid Spencer    if (hiBit < loBit)
499ffe3e2514a2550152267072ed7afeebb3dd3016cDan Gohman      return getLowBitsSet(numBits, hiBit) |
500612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman             getHighBitsSet(numBits, numBits - loBit);
501612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman    return getLowBitsSet(numBits, hiBit - loBit).shl(loBit);
5028f969ee62c3fed26f835a37c41f7c009f99ea3bfReid Spencer  }
5037ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
504cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get a value with high bits set
505cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
5067ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Constructs an APInt value that has the top hiBitsSet bits set.
507cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
508cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param numBits the bitwidth of the result
509cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param hiBitsSet the number of high-order bits set in the result.
5109981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
5118da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer    assert(hiBitsSet <= numBits && "Too many bits to set!");
512a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer    // Handle a degenerate case, to avoid shifting by word size
513a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer    if (hiBitsSet == 0)
514a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer      return APInt(numBits, 0);
5159981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner    unsigned shiftAmt = numBits - hiBitsSet;
5168da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer    // For small values, return quickly
5178da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer    if (numBits <= APINT_BITS_PER_WORD)
518ca76fc2cd3b97e4821e7d8247bc03bbbd2789ad9Reid Spencer      return APInt(numBits, ~0ULL << shiftAmt);
519452b93e7dc2802dea3a31ea12c397d11c3c5e23bBenjamin Kramer    return getAllOnesValue(numBits).shl(shiftAmt);
5208da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer  }
5217ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
522cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get a value with low bits set
523cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
5247ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Constructs an APInt value that has the bottom loBitsSet bits set.
525cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
526cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param numBits the bitwidth of the result
527cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \param loBitsSet the number of low-order bits set in the result.
5289981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) {
5298da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer    assert(loBitsSet <= numBits && "Too many bits to set!");
530a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer    // Handle a degenerate case, to avoid shifting by word size
531a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer    if (loBitsSet == 0)
532a1689ea60eafb10a617ba588fe29e09683299368Reid Spencer      return APInt(numBits, 0);
533f6bef488eeab75b6c7746f9faa9731d45bb84aefReid Spencer    if (loBitsSet == APINT_BITS_PER_WORD)
5348660057d189eb567777ce5bca2b479da8108b5adAaron Ballman      return APInt(numBits, UINT64_MAX);
53571f95b8531183d4a2f7c84d66d5aec17bc41b316Chris Lattner    // For small values, return quickly.
53639a2eb7dea034820eb4b93b2361ae307b0cee5f2Benjamin Kramer    if (loBitsSet <= APINT_BITS_PER_WORD)
5378660057d189eb567777ce5bca2b479da8108b5adAaron Ballman      return APInt(numBits, UINT64_MAX >> (APINT_BITS_PER_WORD - loBitsSet));
538452b93e7dc2802dea3a31ea12c397d11c3c5e23bBenjamin Kramer    return getAllOnesValue(numBits).lshr(numBits - loBitsSet);
5398da7d65b8350d0f47f47ab496328d9068ac6764dReid Spencer  }
5407ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
541ad4da0fc321230261b4d0387f0ec216eb8aa50caBenjamin Kramer  /// \brief Return a value containing V broadcasted over NewLen bits.
542ad4da0fc321230261b4d0387f0ec216eb8aa50caBenjamin Kramer  static APInt getSplat(unsigned NewLen, const APInt &V) {
543ad4da0fc321230261b4d0387f0ec216eb8aa50caBenjamin Kramer    assert(NewLen >= V.getBitWidth() && "Can't splat to smaller bit width!");
544ad4da0fc321230261b4d0387f0ec216eb8aa50caBenjamin Kramer
545ad4da0fc321230261b4d0387f0ec216eb8aa50caBenjamin Kramer    APInt Val = V.zextOrSelf(NewLen);
546ad4da0fc321230261b4d0387f0ec216eb8aa50caBenjamin Kramer    for (unsigned I = V.getBitWidth(); I < NewLen; I <<= 1)
547ad4da0fc321230261b4d0387f0ec216eb8aa50caBenjamin Kramer      Val |= Val << I;
548ad4da0fc321230261b4d0387f0ec216eb8aa50caBenjamin Kramer
549ad4da0fc321230261b4d0387f0ec216eb8aa50caBenjamin Kramer    return Val;
550ad4da0fc321230261b4d0387f0ec216eb8aa50caBenjamin Kramer  }
551ad4da0fc321230261b4d0387f0ec216eb8aa50caBenjamin Kramer
5525449a1db40b75586c1daf70a14396295e7b3fe24Eric Christopher  /// \brief Determine if two APInts have the same value, after zero-extending
5535449a1db40b75586c1daf70a14396295e7b3fe24Eric Christopher  /// one of them (if needed!) to ensure that the bit-widths match.
5545449a1db40b75586c1daf70a14396295e7b3fe24Eric Christopher  static bool isSameValue(const APInt &I1, const APInt &I2) {
5555449a1db40b75586c1daf70a14396295e7b3fe24Eric Christopher    if (I1.getBitWidth() == I2.getBitWidth())
5565449a1db40b75586c1daf70a14396295e7b3fe24Eric Christopher      return I1 == I2;
5575449a1db40b75586c1daf70a14396295e7b3fe24Eric Christopher
5585449a1db40b75586c1daf70a14396295e7b3fe24Eric Christopher    if (I1.getBitWidth() > I2.getBitWidth())
5595449a1db40b75586c1daf70a14396295e7b3fe24Eric Christopher      return I1 == I2.zext(I1.getBitWidth());
5605449a1db40b75586c1daf70a14396295e7b3fe24Eric Christopher
5615449a1db40b75586c1daf70a14396295e7b3fe24Eric Christopher    return I1.zext(I2.getBitWidth()) == I2;
5625449a1db40b75586c1daf70a14396295e7b3fe24Eric Christopher  }
563a5196664236bfadccfc3b805079072340e0f542dMichael Gottesman
564ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth  /// \brief Overload to compute a hash_code for an APInt value.
565ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth  friend hash_code hash_value(const APInt &Arg);
5667ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
5673a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// This function returns a pointer to the internal storage of the APInt.
5687ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// This is useful for writing out the APInt in binary form without any
5697ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// conversions.
570612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  const uint64_t *getRawData() const {
5717ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    if (isSingleWord())
5727ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer      return &VAL;
5737ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return &pVal[0];
5747ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
5757ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
5767ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
577cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Unary Operators
5787ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
579cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
580cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Postfix increment operator.
581cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
582cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns a new APInt value representing *this incremented by one
583c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  const APInt operator++(int) {
584f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng    APInt API(*this);
585b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng    ++(*this);
586b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng    return API;
587f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng  }
588d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
589cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Prefix increment operator.
590cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
591cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns *this incremented by one
592612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator++();
593d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
594cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Postfix decrement operator.
595cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
596cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns a new APInt representing *this decremented by one.
597c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  const APInt operator--(int) {
598f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng    APInt API(*this);
599b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng    --(*this);
600b04973edfaffb12905f58379d632f0d7e4bb5d9bZhou Sheng    return API;
601f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng  }
602d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
603cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Prefix decrement operator.
604cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
605cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns *this decremented by one.
606612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator--();
607d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
608cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unary bitwise complement operator.
609cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
6103a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// Performs a bitwise complement operation on this APInt.
611cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
612cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns an APInt that is the bitwise complement of *this
61398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  APInt operator~() const {
61498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    APInt Result(*this);
6157a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad    Result.flipAllBits();
61698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return Result;
61798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
6187ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
619cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unary negation operator
620cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
6217ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Negates *this using two's complement logic.
622cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
623cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns An APInt value representing the negation of *this.
624612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt operator-() const { return APInt(BitWidth, 0) - (*this); }
6257ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
626cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Logical negation operator.
627cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
6287ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs logical negation operation on this APInt.
629cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
630cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this is zero, false otherwise.
631a18988518869a84cb4d6510e265b1fb1a52268d1Benjamin Kramer  bool operator!() const {
632a18988518869a84cb4d6510e265b1fb1a52268d1Benjamin Kramer    if (isSingleWord())
633a18988518869a84cb4d6510e265b1fb1a52268d1Benjamin Kramer      return !VAL;
634a18988518869a84cb4d6510e265b1fb1a52268d1Benjamin Kramer
635a18988518869a84cb4d6510e265b1fb1a52268d1Benjamin Kramer    for (unsigned i = 0; i != getNumWords(); ++i)
636a18988518869a84cb4d6510e265b1fb1a52268d1Benjamin Kramer      if (pVal[i])
637a18988518869a84cb4d6510e265b1fb1a52268d1Benjamin Kramer        return false;
638a18988518869a84cb4d6510e265b1fb1a52268d1Benjamin Kramer    return true;
639a18988518869a84cb4d6510e265b1fb1a52268d1Benjamin Kramer  }
6407ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
6417ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
642cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Assignment Operators
6437ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
644cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
645cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Copy assignment operator.
646cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
647cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns *this after assignment of RHS.
648612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator=(const APInt &RHS) {
64998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    // If the bitwidths are the same, we can avoid mucking with memory
65098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord() && RHS.isSingleWord()) {
65198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      VAL = RHS.VAL;
65298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      BitWidth = RHS.BitWidth;
65398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return clearUnusedBits();
65498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    }
65598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner
65698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return AssignSlowCase(RHS);
65798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
6587ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
6594334dd96a9e622fdcf2825a8f73a2d941d67be72Chandler Carruth#if LLVM_HAS_RVALUE_REFERENCES
6600fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer  /// @brief Move assignment operator.
661612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator=(APInt &&that) {
6620fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer    if (!isSingleWord())
663612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman      delete[] pVal;
6640fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer
6650fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer    BitWidth = that.BitWidth;
6660fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer    VAL = that.VAL;
6670fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer
6680fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer    that.BitWidth = 0;
6690fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer
6700fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer    return *this;
6710fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer  }
6720fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer#endif
6730fa2b7b90df9a20baf2d14b7bc4fe7db5144efdeBenjamin Kramer
674cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Assignment operator.
675cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
6767ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// The RHS value is assigned to *this. If the significant bits in RHS exceed
6777ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the bit width, the excess bits are truncated. If the bit width is larger
6787ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// than 64, the value is zero filled in the unspecified high order bits.
679cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
680cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns *this after assignment of RHS value.
681612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator=(uint64_t RHS);
6827ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
683cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Bitwise AND assignment operator.
684cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
6857ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs a bitwise AND operation on this APInt and RHS. The result is
6863a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// assigned to *this.
687cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
688cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns *this after ANDing with RHS.
689612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator&=(const APInt &RHS);
690d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
691cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Bitwise OR assignment operator.
692cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
6933a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// Performs a bitwise OR operation on this APInt and RHS. The result is
6947ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// assigned *this;
695cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
696cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns *this after ORing with RHS.
697612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator|=(const APInt &RHS);
698d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
699cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Bitwise OR assignment operator.
700cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
7011e7ad3993d8700488895fa372ecad55443f53485John McCall  /// Performs a bitwise OR operation on this APInt and RHS. RHS is
7021e7ad3993d8700488895fa372ecad55443f53485John McCall  /// logically zero-extended or truncated to match the bit-width of
7031e7ad3993d8700488895fa372ecad55443f53485John McCall  /// the LHS.
704612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator|=(uint64_t RHS) {
7051e7ad3993d8700488895fa372ecad55443f53485John McCall    if (isSingleWord()) {
7061e7ad3993d8700488895fa372ecad55443f53485John McCall      VAL |= RHS;
7071e7ad3993d8700488895fa372ecad55443f53485John McCall      clearUnusedBits();
7081e7ad3993d8700488895fa372ecad55443f53485John McCall    } else {
7091e7ad3993d8700488895fa372ecad55443f53485John McCall      pVal[0] |= RHS;
7101e7ad3993d8700488895fa372ecad55443f53485John McCall    }
7111e7ad3993d8700488895fa372ecad55443f53485John McCall    return *this;
7121e7ad3993d8700488895fa372ecad55443f53485John McCall  }
7131e7ad3993d8700488895fa372ecad55443f53485John McCall
714cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Bitwise XOR assignment operator.
715cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
7167ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs a bitwise XOR operation on this APInt and RHS. The result is
7177ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// assigned to *this.
718cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
719cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns *this after XORing with RHS.
720612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator^=(const APInt &RHS);
721d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
722cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Multiplication assignment operator.
723cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
7247ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Multiplies this APInt by RHS and assigns the result to *this.
725cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
726cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns *this
727612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator*=(const APInt &RHS);
728d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
729cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Addition assignment operator.
730cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
7317ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Adds RHS to *this and assigns the result to *this.
732cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
733cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns *this
734612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator+=(const APInt &RHS);
735d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
736cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Subtraction assignment operator.
737cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
7387ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Subtracts RHS from *this and assigns the result to *this.
739cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
740cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns *this
741612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator-=(const APInt &RHS);
742d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
743cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Left-shift assignment function.
744cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
7457ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Shifts *this left by shiftAmt and assigns the result to *this.
746cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
747cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns *this after shifting left by shiftAmt
748612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt &operator<<=(unsigned shiftAmt) {
7497ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    *this = shl(shiftAmt);
7507ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return *this;
7517ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  }
7527ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
7537ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
754cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Binary Operators
7557ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
756cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
757cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Bitwise AND operator.
758cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
7597ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs a bitwise AND operation on *this and RHS.
760cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
761cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns An APInt value representing the bitwise AND of *this and RHS.
762612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt operator&(const APInt &RHS) const {
76398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
76498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
76598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return APInt(getBitWidth(), VAL & RHS.VAL);
76698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return AndSlowCase(RHS);
76798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
768612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt And(const APInt &RHS) const { return this->operator&(RHS); }
769d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
770cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Bitwise OR operator.
771cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
7727ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs a bitwise OR operation on *this and RHS.
773cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
774cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns An APInt value representing the bitwise OR of *this and RHS.
775612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt operator|(const APInt &RHS) const {
77698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
77798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
77898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return APInt(getBitWidth(), VAL | RHS.VAL);
77998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return OrSlowCase(RHS);
78098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
781cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
782cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Bitwise OR function.
783cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
784cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Performs a bitwise or on *this and RHS. This is implemented bny simply
785cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// calling operator|.
786cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
787cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns An APInt value representing the bitwise OR of *this and RHS.
788612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt Or(const APInt &RHS) const { return this->operator|(RHS); }
789d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
790cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Bitwise XOR operator.
791cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
7927ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Performs a bitwise XOR operation on *this and RHS.
793cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
794cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns An APInt value representing the bitwise XOR of *this and RHS.
795612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt operator^(const APInt &RHS) const {
79698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
79798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
79898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return APInt(BitWidth, VAL ^ RHS.VAL);
79998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return XorSlowCase(RHS);
80098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
801cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
802cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Bitwise XOR function.
803cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
804cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Performs a bitwise XOR operation on *this and RHS. This is implemented
805cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// through the usage of operator^.
806cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
807cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns An APInt value representing the bitwise XOR of *this and RHS.
808612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt Xor(const APInt &RHS) const { return this->operator^(RHS); }
809d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
810cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Multiplication operator.
811cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
8127ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Multiplies this APInt by RHS and returns the result.
813612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt operator*(const APInt &RHS) const;
814d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
815cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Addition operator.
816cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
8177ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Adds RHS to this APInt and returns the result.
818612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt operator+(const APInt &RHS) const;
819612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt operator+(uint64_t RHS) const { return (*this) + APInt(BitWidth, RHS); }
820f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer
821cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Subtraction operator.
822cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
8237ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Subtracts RHS from this APInt and returns the result.
824612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt operator-(const APInt &RHS) const;
825612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt operator-(uint64_t RHS) const { return (*this) - APInt(BitWidth, RHS); }
8263a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
827cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Left logical shift operator.
828cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
829cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Shifts this APInt left by \p Bits and returns the result.
830612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt operator<<(unsigned Bits) const { return shl(Bits); }
831d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
832cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Left logical shift operator.
833cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
834cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Shifts this APInt left by \p Bits and returns the result.
835612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt operator<<(const APInt &Bits) const { return shl(Bits); }
836cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
837cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Arithmetic right-shift function.
838cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
8397ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Arithmetic right-shift this APInt by shiftAmt.
8409981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt ashr(unsigned shiftAmt) const;
8417ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
842cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Logical right-shift function.
843cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
8447ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Logical right-shift this APInt by shiftAmt.
8459981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt lshr(unsigned shiftAmt) const;
8467ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
847cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Left-shift function.
848cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
8497ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Left-shift this APInt by shiftAmt.
8509981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt shl(unsigned shiftAmt) const {
85198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(shiftAmt <= BitWidth && "Invalid shift amount");
85298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord()) {
853b61054ff8f5568489109a0ccf2799307c3671309Derek Schuff      if (shiftAmt >= BitWidth)
85498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner        return APInt(BitWidth, 0); // avoid undefined shift results
85598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return APInt(BitWidth, VAL << shiftAmt);
85698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    }
85798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return shlSlowCase(shiftAmt);
85898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
8597ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
860cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Rotate left by rotateAmt.
8619981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt rotl(unsigned rotateAmt) const;
86219dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer
863cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Rotate right by rotateAmt.
8649981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  APInt rotr(unsigned rotateAmt) const;
86519dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer
866cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Arithmetic right-shift function.
867cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
868cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  /// Arithmetic right-shift this APInt by shiftAmt.
869cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  APInt ashr(const APInt &shiftAmt) const;
870cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
871cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Logical right-shift function.
872cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
873cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  /// Logical right-shift this APInt by shiftAmt.
874cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  APInt lshr(const APInt &shiftAmt) const;
875cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
876cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Left-shift function.
877cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
878cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  /// Left-shift this APInt by shiftAmt.
879cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  APInt shl(const APInt &shiftAmt) const;
880cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
881cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Rotate left by rotateAmt.
882cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  APInt rotl(const APInt &rotateAmt) const;
883cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
884cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Rotate right by rotateAmt.
885cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman  APInt rotr(const APInt &rotateAmt) const;
886cf609575ef6e47cd5ce775af175c67a3b621120eDan Gohman
887cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unsigned division operation.
888cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
8897ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Perform an unsigned divide operation on this APInt by RHS. Both this and
8907ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// RHS are treated as unsigned quantities for purposes of this division.
891cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
892cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns a new APInt value containing the division result
893f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner  APInt udiv(const APInt &RHS) const;
8947ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
895cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Signed division function for APInt.
896cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
8977ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Signed divide this APInt by APInt RHS.
8989bc2c994827f2ff881d0563f0c14134b794b4928Jakub Staszak  APInt sdiv(const APInt &RHS) const;
8997ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
900cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unsigned remainder operation.
901cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
902d3af825d29ab02f78665d5010c8439d34d65db65Reid Spencer  /// Perform an unsigned remainder operation on this APInt with RHS being the
9037ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// divisor. Both this and RHS are treated as unsigned quantities for purposes
904cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// of this operation. Note that this is a true remainder operation and not a
905cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// modulo operation because the sign follows the sign of the dividend which
906cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// is *this.
907cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
908cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns a new APInt value containing the remainder result
909f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner  APInt urem(const APInt &RHS) const;
9107ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
911cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Function for signed remainder operation.
912cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
9137ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Signed remainder operation on APInt.
9149bc2c994827f2ff881d0563f0c14134b794b4928Jakub Staszak  APInt srem(const APInt &RHS) const;
915d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
916cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Dual division/remainder interface.
917cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
918300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  /// Sometimes it is convenient to divide two APInt values and obtain both the
919300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  /// quotient and remainder. This function does both operations in the same
920300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  /// computation making it a little more efficient. The pair of input arguments
921300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  /// may overlap with the pair of output arguments. It is safe to call
922300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz  /// udivrem(X, Y, X, Y), for example.
923612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
924612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman                      APInt &Remainder);
9259bc2c994827f2ff881d0563f0c14134b794b4928Jakub Staszak
926612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
927612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman                      APInt &Remainder);
9289bc2c994827f2ff881d0563f0c14134b794b4928Jakub Staszak
929f2ddc64c8701e432cc220f26c48d596cc0f30a97Chris Lattner  // Operations that return overflow indicators.
9300a0a585e6bfc112cb8346b17edecb76969fb5532Chris Lattner  APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
931eafc5cb80d58cb9447623a557be4d4f55f42fbc3Chris Lattner  APInt uadd_ov(const APInt &RHS, bool &Overflow) const;
9320a0a585e6bfc112cb8346b17edecb76969fb5532Chris Lattner  APInt ssub_ov(const APInt &RHS, bool &Overflow) const;
933eafc5cb80d58cb9447623a557be4d4f55f42fbc3Chris Lattner  APInt usub_ov(const APInt &RHS, bool &Overflow) const;
9340a0a585e6bfc112cb8346b17edecb76969fb5532Chris Lattner  APInt sdiv_ov(const APInt &RHS, bool &Overflow) const;
9350a0a585e6bfc112cb8346b17edecb76969fb5532Chris Lattner  APInt smul_ov(const APInt &RHS, bool &Overflow) const;
9366208610fd602ebdb18bb793152899573d0b2b7abFrits van Bommel  APInt umul_ov(const APInt &RHS, bool &Overflow) const;
93708e90f5646e61c3be0eebfa172ec73a8b56abee8Chris Lattner  APInt sshl_ov(unsigned Amt, bool &Overflow) const;
93819dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer
939cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Array-indexing support.
940cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
941cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the bit value at bitPosition
94269ccf9fc0b131f2c71c3d60791425cbf52392ee4Benjamin Kramer  bool operator[](unsigned bitPosition) const {
94369ccf9fc0b131f2c71c3d60791425cbf52392ee4Benjamin Kramer    assert(bitPosition < getBitWidth() && "Bit position out of bounds!");
94469ccf9fc0b131f2c71c3d60791425cbf52392ee4Benjamin Kramer    return (maskBit(bitPosition) &
945612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman            (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) !=
946612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman           0;
94769ccf9fc0b131f2c71c3d60791425cbf52392ee4Benjamin Kramer  }
948d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
9497ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
950cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Comparison Operators
9517ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
952cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
953cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Equality operator.
954cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
9557ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Compares this APInt with RHS for the validity of the equality
9567ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
957612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool operator==(const APInt &RHS) const {
95898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
95998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
96098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return VAL == RHS.VAL;
96198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return EqualSlowCase(RHS);
96298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
963d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
964cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Equality operator.
965cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
9663a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// Compares this APInt with a uint64_t for the validity of the equality
9677ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
968cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
969cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this == Val
97098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  bool operator==(uint64_t Val) const {
97198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
97298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return VAL == Val;
97398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return EqualSlowCase(Val);
97498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
975f29a09d6272265e90362d5dfba201c4ed6dcf6d5Zhou Sheng
976cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Equality comparison.
977cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
9787ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Compares this APInt with RHS for the validity of the equality
9797ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
980cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
981cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this == Val
982612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool eq(const APInt &RHS) const { return (*this) == RHS; }
9837ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
984cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Inequality operator.
985cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
9867ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Compares this APInt with RHS for the validity of the inequality
9877ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
988cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
989cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this != Val
990612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool operator!=(const APInt &RHS) const { return !((*this) == RHS); }
991d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
992cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Inequality operator.
993cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
9943a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// Compares this APInt with a uint64_t for the validity of the inequality
9957ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
996cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
997cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this != Val
998612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool operator!=(uint64_t Val) const { return !((*this) == Val); }
9993a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
1000cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Inequality comparison
1001cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
10027ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Compares this APInt with RHS for the validity of the inequality
10037ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// relationship.
1004cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1005cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this != Val
1006612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool ne(const APInt &RHS) const { return !((*this) == RHS); }
1007e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1008cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unsigned less than comparison
1009cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
10107ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as unsigned quantities and compares them for
10117ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the validity of the less-than relationship.
1012cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1013cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this < RHS when both are considered unsigned.
1014eafc5cb80d58cb9447623a557be4d4f55f42fbc3Chris Lattner  bool ult(const APInt &RHS) const;
1015e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1016cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unsigned less than comparison
1017cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1018e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as an unsigned quantity and compares it with RHS for
1019e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the less-than relationship.
1020cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1021cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this < RHS when considered unsigned.
1022612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool ult(uint64_t RHS) const { return ult(APInt(getBitWidth(), RHS)); }
1023e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
1024cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Signed less than comparison
1025cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
10267ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as signed quantities and compares them for
10277ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// validity of the less-than relationship.
1028cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1029cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this < RHS when both are considered signed.
1030612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool slt(const APInt &RHS) const;
1031e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1032cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Signed less than comparison
1033cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1034e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as a signed quantity and compares it with RHS for
1035e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the less-than relationship.
1036cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1037cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this < RHS when considered signed.
1038612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool slt(uint64_t RHS) const { return slt(APInt(getBitWidth(), RHS)); }
1039e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
1040cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unsigned less or equal comparison
1041cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
10427ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as unsigned quantities and compares them for
10437ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// validity of the less-or-equal relationship.
1044cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1045cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this <= RHS when both are considered unsigned.
1046612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool ule(const APInt &RHS) const { return ult(RHS) || eq(RHS); }
1047e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1048cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unsigned less or equal comparison
1049cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1050e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as an unsigned quantity and compares it with RHS for
1051e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the less-or-equal relationship.
1052cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1053cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this <= RHS when considered unsigned.
1054612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool ule(uint64_t RHS) const { return ule(APInt(getBitWidth(), RHS)); }
1055e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
1056cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Signed less or equal comparison
1057cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
10587ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as signed quantities and compares them for
10597ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// validity of the less-or-equal relationship.
1060cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1061cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this <= RHS when both are considered signed.
1062612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool sle(const APInt &RHS) const { return slt(RHS) || eq(RHS); }
1063e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1064cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Signed less or equal comparison
1065cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1066cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Regards both *this as a signed quantity and compares it with RHS for the
1067cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// validity of the less-or-equal relationship.
1068cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1069cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this <= RHS when considered signed.
1070612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool sle(uint64_t RHS) const { return sle(APInt(getBitWidth(), RHS)); }
1071e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
1072cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unsigned greather than comparison
1073cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
10747ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as unsigned quantities and compares them for
10757ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// the validity of the greater-than relationship.
1076cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1077cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this > RHS when both are considered unsigned.
1078612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool ugt(const APInt &RHS) const { return !ult(RHS) && !eq(RHS); }
1079e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1080cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unsigned greater than comparison
1081cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1082e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as an unsigned quantity and compares it with RHS for
1083e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the greater-than relationship.
1084cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1085cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this > RHS when considered unsigned.
1086612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool ugt(uint64_t RHS) const { return ugt(APInt(getBitWidth(), RHS)); }
1087e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
1088cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Signed greather than comparison
1089cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1090cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Regards both *this and RHS as signed quantities and compares them for the
1091cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// validity of the greater-than relationship.
1092cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1093cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this > RHS when both are considered signed.
1094612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool sgt(const APInt &RHS) const { return !slt(RHS) && !eq(RHS); }
1095e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1096cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Signed greater than comparison
1097cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1098e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as a signed quantity and compares it with RHS for
1099e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the greater-than relationship.
1100cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1101cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this > RHS when considered signed.
1102612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool sgt(uint64_t RHS) const { return sgt(APInt(getBitWidth(), RHS)); }
1103e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
1104cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unsigned greater or equal comparison
1105cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
11067ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as unsigned quantities and compares them for
11077ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// validity of the greater-or-equal relationship.
1108cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1109cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this >= RHS when both are considered unsigned.
1110612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool uge(const APInt &RHS) const { return !ult(RHS); }
1111e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1112cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Unsigned greater or equal comparison
1113cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1114e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as an unsigned quantity and compares it with RHS for
1115e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the greater-or-equal relationship.
1116cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1117cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this >= RHS when considered unsigned.
1118612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool uge(uint64_t RHS) const { return uge(APInt(getBitWidth(), RHS)); }
1119e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
1120cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Signed greather or equal comparison
1121cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
11227ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Regards both *this and RHS as signed quantities and compares them for
11237ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// validity of the greater-or-equal relationship.
1124cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1125cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this >= RHS when both are considered signed.
1126612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool sge(const APInt &RHS) const { return !slt(RHS); }
1127e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1128cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Signed greater or equal comparison
1129cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1130e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// Regards both *this as a signed quantity and compares it with RHS for
1131e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman  /// the validity of the greater-or-equal relationship.
1132cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1133cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns true if *this >= RHS when considered signed.
1134612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool sge(uint64_t RHS) const { return sge(APInt(getBitWidth(), RHS)); }
1135e05678132345eb8a632362dbd320ee7d36226e67Dan Gohman
1136bd999178929c10bdeb0a2577fa02981778edaee7Dan Gohman  /// This operation tests if there are any pairs of corresponding bits
1137bd999178929c10bdeb0a2577fa02981778edaee7Dan Gohman  /// between this APInt and RHS that are both set.
1138612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool intersects(const APInt &RHS) const { return (*this & RHS) != 0; }
1139bd999178929c10bdeb0a2577fa02981778edaee7Dan Gohman
11407ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
1141cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Resizing Operators
11427ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
1143cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
1144cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Truncate to new width.
1145cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1146e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// Truncate the APInt to a specified width. It is an error to specify a width
11473a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// that is greater than or equal to the current width.
114840f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  APInt trunc(unsigned width) const;
1149e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1150cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Sign extend to a new width.
1151cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1152e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// This operation sign extends the APInt to a new width. If the high order
1153e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
11543a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// It is an error to specify a width that is less than or equal to the
1155e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// current width.
115640f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  APInt sext(unsigned width) const;
1157e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1158cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Zero extend to a new width.
1159cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
11605b9f2d6186317df88dbaf603bf8d0eafd6e83897Reid Spencer  /// This operation zero extends the APInt to a new width. The high order bits
11613a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// are filled with 0 bits.  It is an error to specify a width that is less
1162e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// than or equal to the current width.
116340f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  APInt zext(unsigned width) const;
1164e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1165cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Sign extend or truncate to width
1166cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
116768e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer  /// Make this APInt have the bit width given by \p width. The value is sign
116868e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer  /// extended, truncated, or left alone to make it that width.
116940f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  APInt sextOrTrunc(unsigned width) const;
117068e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer
1171cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Zero extend or truncate to width
1172cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
117368e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer  /// Make this APInt have the bit width given by \p width. The value is zero
117468e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer  /// extended, truncated, or left alone to make it that width.
117540f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  APInt zextOrTrunc(unsigned width) const;
117668e2300ad965bf08af11ae363bb85e3badf964dcReid Spencer
1177cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Sign extend or truncate to width
1178cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
117904594aeffa3360882eb09a888a0970321b987b16Rafael Espindola  /// Make this APInt have the bit width given by \p width. The value is sign
118004594aeffa3360882eb09a888a0970321b987b16Rafael Espindola  /// extended, or left alone to make it that width.
118104594aeffa3360882eb09a888a0970321b987b16Rafael Espindola  APInt sextOrSelf(unsigned width) const;
118204594aeffa3360882eb09a888a0970321b987b16Rafael Espindola
1183cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Zero extend or truncate to width
1184cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
118504594aeffa3360882eb09a888a0970321b987b16Rafael Espindola  /// Make this APInt have the bit width given by \p width. The value is zero
118604594aeffa3360882eb09a888a0970321b987b16Rafael Espindola  /// extended, or left alone to make it that width.
118704594aeffa3360882eb09a888a0970321b987b16Rafael Espindola  APInt zextOrSelf(unsigned width) const;
118804594aeffa3360882eb09a888a0970321b987b16Rafael Espindola
11897ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
1190cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Bit Manipulation Operators
11917ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
1192cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman
1193cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Set every bit to 1.
11947a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void setAllBits() {
1195a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    if (isSingleWord())
11968660057d189eb567777ce5bca2b479da8108b5adAaron Ballman      VAL = UINT64_MAX;
1197a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    else {
1198a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad      // Set all the bits in all the words.
1199a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad      for (unsigned i = 0; i < getNumWords(); ++i)
12008660057d189eb567777ce5bca2b479da8108b5adAaron Ballman        pVal[i] = UINT64_MAX;
120198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    }
120298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    // Clear the unused ones
1203a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    clearUnusedBits();
120498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
1205e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1206cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Set a given bit to 1.
1207cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1208e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// Set the given bit to 1 whose position is given as "bitPosition".
12097a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void setBit(unsigned bitPosition);
1210e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1211cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Set every bit to 0.
12127a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void clearAllBits() {
12133a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    if (isSingleWord())
121498f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      VAL = 0;
12153a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman    else
121698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
121798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
1218e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1219cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Set a given bit to 0.
1220cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1221e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// Set the given bit to 0 whose position is given as "bitPosition".
12227a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void clearBit(unsigned bitPosition);
1223e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1224cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Toggle every bit to its opposite value.
12257a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void flipAllBits() {
1226a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    if (isSingleWord())
12278660057d189eb567777ce5bca2b479da8108b5adAaron Ballman      VAL ^= UINT64_MAX;
1228a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    else {
1229a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad      for (unsigned i = 0; i < getNumWords(); ++i)
12308660057d189eb567777ce5bca2b479da8108b5adAaron Ballman        pVal[i] ^= UINT64_MAX;
123198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    }
1232a99793c5ea24dd3839f4925b89b1f6acfcb24604Jay Foad    clearUnusedBits();
123398f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
1234e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
1235cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Toggles a given bit to its opposite value.
1236cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
12373a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman  /// Toggle a given bit to its opposite value whose position is given
1238e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// as "bitPosition".
12397a874ddda037349184fbeb22838cc11a1a9bb78fJay Foad  void flipBit(unsigned bitPosition);
1240e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer
12417ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
1242cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Value Characterization Functions
12437ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
12447ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
1245cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Return the number of bits in the APInt.
1246612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  unsigned getBitWidth() const { return BitWidth; }
12477ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
1248cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get the number of words.
1249cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
12507ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Here one word's bitwidth equals to that of uint64_t.
1251cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1252cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the number of words to hold the integer value of this APInt.
1253612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  unsigned getNumWords() const { return getNumWords(BitWidth); }
125417893a5fb6b3b4b9e84674dfb7692abb57410dd1Douglas Gregor
1255cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get the number of words.
1256cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1257cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// *NOTE* Here one word's bitwidth equals to that of uint64_t.
1258cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1259cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the number of words to hold the integer value with a given bit
1260cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// width.
126117893a5fb6b3b4b9e84674dfb7692abb57410dd1Douglas Gregor  static unsigned getNumWords(unsigned BitWidth) {
12627ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer    return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
12638db6a445e679817d9972c44d2bc366732388c7b0Zhou Sheng  }
12648db6a445e679817d9972c44d2bc366732388c7b0Zhou Sheng
1265cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Compute the number of active bits in the value
1266cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1267e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// This function returns the number of active bits which is defined as the
1268e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// bit width minus the number of leading zeros. This is used in several
1269e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  /// computations to see how "wide" the value is.
1270612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); }
1271d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1272cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Compute the number of active words in the value of this APInt.
1273cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1274cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// This is used in conjunction with getActiveData to extract the raw value of
1275cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// the APInt.
12769981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned getActiveWords() const {
127735b1423ee67a6ec7052016dda486e6ee4a118db4Meador Inge    unsigned numActiveBits = getActiveBits();
127835b1423ee67a6ec7052016dda486e6ee4a118db4Meador Inge    return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1;
12799d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  }
12809d3c51923392f69b63512e59e44f72affbe6136eReid Spencer
1281cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get the minimum bit size for this signed APInt
1282cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1283cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Computes the minimum bit width for this APInt while considering it to be a
1284cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// signed (and probably negative) value. If the value is not negative, this
1285cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// function returns the same value as getActiveBits()+1. Otherwise, it
12869d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  /// returns the smallest bit width that will retain the negative value. For
12879d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
12889d3c51923392f69b63512e59e44f72affbe6136eReid Spencer  /// for -1, this function will always return 1.
12899981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned getMinSignedBits() const {
1290681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer    if (isNegative())
1291681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer      return BitWidth - countLeadingOnes() + 1;
1292612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman    return getActiveBits() + 1;
1293681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer  }
1294681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer
1295cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get zero extended value
1296cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
129731a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// This method attempts to return the value of this APInt as a zero extended
129831a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
129931a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// uint64_t. Otherwise an assertion will result.
1300c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  uint64_t getZExtValue() const {
1301d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng    if (isSingleWord())
130231a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer      return VAL;
130331a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer    assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
130431a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer    return pVal[0];
130531a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  }
130631a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer
1307cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get sign extended value
1308cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
130931a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// This method attempts to return the value of this APInt as a sign extended
131031a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// int64_t. The bit width must be <= 64 or the value must fit within an
131131a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer  /// int64_t. Otherwise an assertion will result.
1312c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  int64_t getSExtValue() const {
131331a81f0190179b19dc72302dfea05cd1c2f0d22eReid Spencer    if (isSingleWord())
13143a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman      return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >>
1315612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman             (APINT_BITS_PER_WORD - BitWidth);
13163864cd4cab956526b99b634e15e7fd5d5441e3a7Eli Friedman    assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
1317946bca5bae2539ca55f8fb4700bff8b5e3f5a7ebReid Spencer    return int64_t(pVal[0]);
1318d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng  }
131957ae4f5f01b6f8edf678d77ab935f3662993065dReid Spencer
1320cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get bits required for string value.
1321cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
132257ae4f5f01b6f8edf678d77ab935f3662993065dReid Spencer  /// This method determines how many bits are required to hold the APInt
13232d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko  /// equivalent of the string given by \p str.
132438e59891ee4417a9be2f8146ce0ba3269e38ac21Benjamin Kramer  static unsigned getBitsNeeded(StringRef str, uint8_t radix);
132557ae4f5f01b6f8edf678d77ab935f3662993065dReid Spencer
1326c6af2432c802d241c8fffbe0371c023e6c58844eMichael J. Spencer  /// \brief The APInt version of the countLeadingZeros functions in
1327c6af2432c802d241c8fffbe0371c023e6c58844eMichael J. Spencer  ///   MathExtras.h.
1328cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1329c6af2432c802d241c8fffbe0371c023e6c58844eMichael J. Spencer  /// It counts the number of zeros from the most significant bit to the first
1330c6af2432c802d241c8fffbe0371c023e6c58844eMichael J. Spencer  /// one bit.
1331cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1332cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns BitWidth if the value is zero, otherwise returns the number of
1333c6af2432c802d241c8fffbe0371c023e6c58844eMichael J. Spencer  ///   zeros from the most significant bit to the first one bits.
13349981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countLeadingZeros() const {
133598f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord()) {
13369981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner      unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
1337c6af2432c802d241c8fffbe0371c023e6c58844eMichael J. Spencer      return llvm::countLeadingZeros(VAL) - unusedBits;
133898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    }
133998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return countLeadingZerosSlowCase();
134098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
1341d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1342cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Count the number of leading one bits.
1343cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1344cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// This function is an APInt version of the countLeadingOnes_{32,64}
1345cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// functions in MathExtras.h. It counts the number of ones from the most
1346cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// significant bit to the first zero bit.
1347cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1348cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns 0 if the high order bit is not set, otherwise returns the number
1349cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// of 1 bits from the most significant to the least
13509981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countLeadingOnes() const;
1351681dcd14e9d59c2070e3a298328db9aea6069480Reid Spencer
13529b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich  /// Computes the number of leading bits of this APInt that are equal to its
13539b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich  /// sign bit.
13549b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich  unsigned getNumSignBits() const {
13559b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich    return isNegative() ? countLeadingOnes() : countLeadingZeros();
13569b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich  }
13579b6af8de58140566a0e6567508bf906027422e7cCameron Zwarich
1358cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Count the number of trailing zero bits.
1359cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1360cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// This function is an APInt version of the countTrailingZeros_{32,64}
1361cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// functions in MathExtras.h. It counts the number of zeros from the least
1362cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// significant bit to the first set bit.
1363cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1364cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns BitWidth if the value is zero, otherwise returns the number of
1365cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// zeros from the least significant bit to the first one bit.
13669981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countTrailingZeros() const;
1367d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1368cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Count the number of trailing one bits.
1369cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1370cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// This function is an APInt version of the countTrailingOnes_{32,64}
1371cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// functions in MathExtras.h. It counts the number of ones from the least
1372cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// significant bit to the first zero bit.
1373cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1374cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns BitWidth if the value is all ones, otherwise returns the number
1375cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// of ones from the least significant bit to the first zero bit.
13769981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countTrailingOnes() const {
137798f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
137898f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return CountTrailingOnes_64(VAL);
137998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return countTrailingOnesSlowCase();
138098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
138142dd77f20702d5ca1e0f3882ad74e7a02fc9589cDan Gohman
1382cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Count the number of bits set.
1383cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1384cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// This function is an APInt version of the countPopulation_{32,64} functions
1385cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// in MathExtras.h. It counts the number of 1 bits in the APInt value.
1386cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1387cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns 0 if the value is zero, otherwise returns the number of set bits.
13889981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner  unsigned countPopulation() const {
138998f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    if (isSingleWord())
139098f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner      return CountPopulation_64(VAL);
139198f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner    return countPopulationSlowCase();
139298f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  }
1393d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
13947ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
1395cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Conversion Functions
13967ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
1397944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner  void print(raw_ostream &OS, bool isSigned) const;
13983a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
1399cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Converts an APInt to a string and append it to Str.  Str is commonly a
1400cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// SmallString.
1401cf886188fb04d9521db39fe5213df1295673f51eTed Kremenek  void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
1402cf886188fb04d9521db39fe5213df1295673f51eTed Kremenek                bool formatAsCLiteral = false) const;
14037ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
14047ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// Considers the APInt to be unsigned and converts it into a string in the
1405dcd999624159842886d4be21efcc3ba0e61bab99Douglas Gregor  /// radix given. The radix can be 2, 8, 10 16, or 36.
1406fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1407cf886188fb04d9521db39fe5213df1295673f51eTed Kremenek    toString(Str, Radix, false, false);
1408e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  }
1409d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1410df99c29e7b25771d202b969ca5dec359a62897b1Duncan Sands  /// Considers the APInt to be signed and converts it into a string in the
1411dcd999624159842886d4be21efcc3ba0e61bab99Douglas Gregor  /// radix given. The radix can be 2, 8, 10, 16, or 36.
1412fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1413cf886188fb04d9521db39fe5213df1295673f51eTed Kremenek    toString(Str, Radix, true, false);
1414ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng  }
14153a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
1416cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Return the APInt as a std::string.
1417cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1418cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Note that this is an inefficient method.  It is better to pass in a
1419cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// SmallVector/SmallString to the methods above to avoid thrashing the heap
1420cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// for the string.
1421fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  std::string toString(unsigned Radix, bool Signed) const;
14223a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
1423cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns a byte-swapped representation of this APInt Value.
1424e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  APInt byteSwap() const;
1425ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
1426cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Converts this APInt to a double value.
142766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  double roundToDouble(bool isSigned) const;
142866ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer
1429cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Converts this unsigned APInt to a double value.
1430612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  double roundToDouble() const { return roundToDouble(false); }
1431ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
1432cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Converts this signed APInt to a double value.
1433612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  double signedRoundToDouble() const { return roundToDouble(true); }
1434af8fb1984674db462bc6923ed54db0275c78b711Reid Spencer
1435cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Converts APInt bits to a double
1436cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1437ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// The conversion does not do a translation from integer to double, it just
1438ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// re-interprets the bits as a double. Note that it is valid to do this on
1439ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// any bit width. Exactly 64 bits will be translated.
1440ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  double bitsToDouble() const {
1441ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    union {
1442ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer      uint64_t I;
1443ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer      double D;
1444ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    } T;
1445ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    T.I = (isSingleWord() ? VAL : pVal[0]);
1446ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    return T.D;
1447ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  }
1448ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer
1449cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Converts APInt bits to a double
1450cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1451ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// The conversion does not do a translation from integer to float, it just
1452ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// re-interprets the bits as a float. Note that it is valid to do this on
1453ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  /// any bit width. Exactly 32 bits will be translated.
1454ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  float bitsToFloat() const {
1455ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    union {
14569981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner      unsigned I;
1457ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer      float F;
1458ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    } T;
14599981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner    T.I = unsigned((isSingleWord() ? VAL : pVal[0]));
1460ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer    return T.F;
1461ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer  }
1462ab2ed8ec850b6dd9b152ab936d70b7376903168aReid Spencer
1463cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Converts a double to APInt bits.
1464cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
146553ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer  /// The conversion does not do a translation from double to integer, it just
1466e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad  /// re-interprets the bits of the double.
1467e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad  static APInt doubleToBits(double V) {
146853ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    union {
146953ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer      uint64_t I;
147053ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer      double D;
147153ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    } T;
147253ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    T.D = V;
1473e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad    return APInt(sizeof T * CHAR_BIT, T.I);
147453ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer  }
147553ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer
1476cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Converts a float to APInt bits.
1477cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
147853ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer  /// The conversion does not do a translation from float to integer, it just
1479e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad  /// re-interprets the bits of the float.
1480e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad  static APInt floatToBits(float V) {
148153ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    union {
14829981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattner      unsigned I;
148353ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer      float F;
148453ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    } T;
148553ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer    T.F = V;
1486e4d19c9eb22899c9a555395d446a9ceef3bea7ebJay Foad    return APInt(sizeof T * CHAR_BIT, T.I);
148753ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer  }
148853ee4f9d4083ce5106d95ab23985570a1f7413e5Reid Spencer
14897ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
1490cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Mathematics Operations
14917ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @{
14927ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
1493cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the floor log base 2 of this APInt.
1494612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  unsigned logBase2() const { return BitWidth - 1 - countLeadingZeros(); }
14957ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer
1496cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the ceil log base 2 of this APInt.
1497cbc7cc63b6c7ee1008f92064388c37327c183328Dan Gohman  unsigned ceilLogBase2() const {
1498cbc7cc63b6c7ee1008f92064388c37327c183328Dan Gohman    return BitWidth - (*this - 1).countLeadingZeros();
1499cbc7cc63b6c7ee1008f92064388c37327c183328Dan Gohman  }
1500cbc7cc63b6c7ee1008f92064388c37327c183328Dan Gohman
1501cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the log base 2 of this APInt if its an exact power of two, -1
150219dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer  /// otherwise
1503c9525263f6c519d987209eb39e33a73165c526d6Dan Gohman  int32_t exactLogBase2() const {
150419dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer    if (!isPowerOf2())
150519dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer      return -1;
150619dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer    return logBase2();
150719dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer  }
150819dc32a2d422ac0aafd047514e3e5e727796696eReid Spencer
1509cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Compute the square root
1510af8fb1984674db462bc6923ed54db0275c78b711Reid Spencer  APInt sqrt() const;
1511b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer
1512cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief Get the absolute value;
1513cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  ///
1514b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer  /// If *this is < 0 then return -(*this), otherwise *this;
1515b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer  APInt abs() const {
1516b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer    if (isNegative())
1517b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer      return -(*this);
1518b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer    return *this;
1519b45a221348ee8693f1bff2df7b42689497083d41Reid Spencer  }
1520fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1521cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \returns the multiplicative inverse for a given modulo.
1522612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt multiplicativeInverse(const APInt &modulo) const;
1523300c6c5167d2869d1568d783d0e3e48bf4b03a6cWojciech Matyjewicz
1524fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// @}
1525cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Support for division by constant
15264e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  /// @{
15274e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad
15284e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  /// Calculate the magic number for signed division by a constant.
15294e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  struct ms;
15304e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  ms magic() const;
15314e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad
15324e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  /// Calculate the magic number for unsigned division by a constant.
15334e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  struct mu;
1534d9103df51b858cf051a1650ac7eb33d416e9ac41Benjamin Kramer  mu magicu(unsigned LeadingZeros = 0) const;
15354e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad
15364e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  /// @}
1537cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \name Building-block Operations for APInt and APFloat
1538fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// @{
1539fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1540cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  // These building block operations operate on a representation of arbitrary
1541cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  // precision, two's-complement, bignum integer values. They should be
1542cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  // sufficient to implement APInt and APFloat bignum requirements. Inputs are
1543cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  // generally a pointer to the base of an array of integer parts, representing
1544cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  // an unsigned bignum, and a count of how many parts there are.
1545fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1546cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Sets the least significant part of a bignum to the input value, and zeroes
1547cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// out higher parts.
1548fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcSet(integerPart *, integerPart, unsigned int);
1549fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1550fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Assign one bignum to another.
1551fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcAssign(integerPart *, const integerPart *, unsigned int);
1552fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1553fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Returns true if a bignum is zero, false otherwise.
1554fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static bool tcIsZero(const integerPart *, unsigned int);
1555fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1556fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Extract the given bit of a bignum; returns 0 or 1.  Zero-based.
1557fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static int tcExtractBit(const integerPart *, unsigned int bit);
1558fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1559cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to
1560cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least
1561cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// significant bit of DST.  All high bits above srcBITS in DST are
1562cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// zero-filled.
1563a2769a33c94f021a609a462b28ebea069eba6f74Misha Brukman  static void tcExtract(integerPart *, unsigned int dstCount,
1564612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman                        const integerPart *, unsigned int srcBits,
1565612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman                        unsigned int srcLSB);
156668e53ad6cb0a9d64d256f9dcef70331cd72d795eNeil Booth
1567fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Set the given bit of a bignum.  Zero-based.
1568fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcSetBit(integerPart *, unsigned int bit);
1569fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1570e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall  /// Clear the given bit of a bignum.  Zero-based.
1571e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall  static void tcClearBit(integerPart *, unsigned int bit);
1572e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall
1573cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Returns the bit number of the least or most significant set bit of a
1574cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// number.  If the input number has no bits set -1U is returned.
1575fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static unsigned int tcLSB(const integerPart *, unsigned int);
157698f8ccfad06c8928d899f506731987f951b2ebe4Chris Lattner  static unsigned int tcMSB(const integerPart *parts, unsigned int n);
1577fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1578fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Negate a bignum in-place.
1579fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcNegate(integerPart *, unsigned int);
1580fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1581cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// DST += RHS + CARRY where CARRY is zero or one.  Returns the carry flag.
1582fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static integerPart tcAdd(integerPart *, const integerPart *,
1583e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                           integerPart carry, unsigned);
1584fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1585cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1586fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static integerPart tcSubtract(integerPart *, const integerPart *,
1587e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                                integerPart carry, unsigned);
1588fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1589cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// DST += SRC * MULTIPLIER + PART   if add is true
1590cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// DST  = SRC * MULTIPLIER + PART   if add is false
1591fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///
1592cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Requires 0 <= DSTPARTS <= SRCPARTS + 1.  If DST overlaps SRC they must
1593cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// start at the same point, i.e. DST == SRC.
1594fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///
1595cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned.
1596cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Otherwise DST is filled with the least significant DSTPARTS parts of the
1597cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// result, and if all of the omitted higher parts were zero return zero,
1598cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// otherwise overflow occurred and return one.
1599fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static int tcMultiplyPart(integerPart *dst, const integerPart *src,
1600e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                            integerPart multiplier, integerPart carry,
1601e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                            unsigned int srcParts, unsigned int dstParts,
1602e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                            bool add);
1603fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1604cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// DST = LHS * RHS, where DST has the same width as the operands and is
1605cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// filled with the least significant parts of the result.  Returns one if
1606cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// overflow occurred, otherwise zero.  DST must be disjoint from both
1607cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// operands.
1608612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  static int tcMultiply(integerPart *, const integerPart *, const integerPart *,
1609612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman                        unsigned);
1610fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1611cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// DST = LHS * RHS, where DST has width the sum of the widths of the
1612cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// operands.  No overflow occurs.  DST must be disjoint from both
1613cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// operands. Returns the number of parts required to hold the result.
1614978661d05301a9bcd1222c048affef679da5ac43Neil Booth  static unsigned int tcFullMultiply(integerPart *, const integerPart *,
1615e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                                     const integerPart *, unsigned, unsigned);
1616fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1617fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1618cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Otherwise set LHS to LHS / RHS with the fractional part discarded, set
1619cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// REMAINDER to the remainder, return zero.  i.e.
1620fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///
1621fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///  OLD_LHS = RHS * LHS + REMAINDER
1622fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  ///
1623cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// SCRATCH is a bignum of the same size as the operands and result for use by
1624cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// the routine; its contents need not be initialized and are destroyed.  LHS,
1625cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// REMAINDER and SCRATCH must be distinct.
1626fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static int tcDivide(integerPart *lhs, const integerPart *rhs,
1627e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                      integerPart *remainder, integerPart *scratch,
1628e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                      unsigned int parts);
1629fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1630cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Shift a bignum left COUNT bits.  Shifted in bits are zero.  There are no
1631cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// restrictions on COUNT.
1632fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcShiftLeft(integerPart *, unsigned int parts,
1633e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                          unsigned int count);
1634fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1635cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// Shift a bignum right COUNT bits.  Shifted in bits are zero.  There are no
1636cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// restrictions on COUNT.
1637fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcShiftRight(integerPart *, unsigned int parts,
1638e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                           unsigned int count);
1639fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1640fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// The obvious AND, OR and XOR and complement operations.
1641fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcAnd(integerPart *, const integerPart *, unsigned int);
1642fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcOr(integerPart *, const integerPart *, unsigned int);
1643fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcXor(integerPart *, const integerPart *, unsigned int);
1644fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcComplement(integerPart *, unsigned int);
16453a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
1646fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Comparison (unsigned) of two bignums.
1647612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  static int tcCompare(const integerPart *, const integerPart *, unsigned int);
1648fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1649fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Increment a bignum in-place.  Return the carry flag.
1650fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static integerPart tcIncrement(integerPart *, unsigned int);
1651fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1652a32edcfbc5b99b808b67360311d513af650eab44Michael Gottesman  /// Decrement a bignum in-place.  Return the borrow flag.
1653a32edcfbc5b99b808b67360311d513af650eab44Michael Gottesman  static integerPart tcDecrement(integerPart *, unsigned int);
1654a32edcfbc5b99b808b67360311d513af650eab44Michael Gottesman
1655fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  /// Set the least significant BITS and clear the rest.
1656fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner  static void tcSetLeastSignificantBits(integerPart *, unsigned int,
1657e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                                        unsigned int bits);
1658fe8e14a6c99261cfe9238c35761083cb4c99cc6aChris Lattner
1659cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman  /// \brief debug method
16609e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen  void dump() const;
16619e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen
16627ac2f81e49442f76e3427227fb1e300ce60fceb4Reid Spencer  /// @}
1663d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng};
1664d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
16654e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad/// Magic data for optimising signed division by a constant.
16664e5ea553d055512b0b8aa098e363ae17bafda957Jay Foadstruct APInt::ms {
1667612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt m;    ///< magic number
1668612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  unsigned s; ///< shift amount
16694e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad};
16704e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad
16714e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad/// Magic data for optimising unsigned division by a constant.
16724e5ea553d055512b0b8aa098e363ae17bafda957Jay Foadstruct APInt::mu {
1673612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  APInt m;    ///< magic number
1674612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  bool a;     ///< add indicator
1675612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  unsigned s; ///< shift amount
16764e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad};
16774e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad
1678612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; }
16799d3c51923392f69b63512e59e44f72affbe6136eReid Spencer
1680612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; }
16819d3c51923392f69b63512e59e44f72affbe6136eReid Spencer
1682944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattnerinline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) {
1683fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  I.print(OS, true);
1684fad86b003a839cef40ec8ce8408322f4913368caChris Lattner  return OS;
1685fad86b003a839cef40ec8ce8408322f4913368caChris Lattner}
16863a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
16870b706b18bd0a7760d971727460a1f26bff8289b0Zhou Shengnamespace APIntOps {
16880b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1689cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Determine the smaller of two APInts considered to be signed.
1690612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt smin(const APInt &A, const APInt &B) { return A.slt(B) ? A : B; }
1691f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer
1692cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Determine the larger of two APInts considered to be signed.
1693612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt smax(const APInt &A, const APInt &B) { return A.sgt(B) ? A : B; }
1694f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer
1695cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Determine the smaller of two APInts considered to be signed.
1696612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt umin(const APInt &A, const APInt &B) { return A.ult(B) ? A : B; }
1697f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer
1698cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Determine the larger of two APInts considered to be unsigned.
1699612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt umax(const APInt &A, const APInt &B) { return A.ugt(B) ? A : B; }
1700f2253449e24712e96f7bdb7b54c20ddca8d6bb51Reid Spencer
1701cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Check if the specified APInt has a N-bits unsigned integer value.
1702612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline bool isIntN(unsigned N, const APInt &APIVal) { return APIVal.isIntN(N); }
1703d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1704cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Check if the specified APInt has a N-bits signed integer value.
1705612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline bool isSignedIntN(unsigned N, const APInt &APIVal) {
1706ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman  return APIVal.isSignedIntN(N);
1707ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman}
1708ec646cfd07f1354364e0899561aabffef5b702fdDan Gohman
1709cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \returns true if the argument APInt value is a sequence of ones starting at
1710cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// the least significant bit with the remainder zero.
1711612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline bool isMask(unsigned numBits, const APInt &APIVal) {
17128eab8a2798fe74c98703bdeac64661beea0b4dbcDuncan Sands  return numBits <= APIVal.getBitWidth() &&
1713612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman         APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
1714d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng}
1715d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1716cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Return true if the argument APInt value contains a sequence of ones
1717d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng/// with the remainder zero.
1718612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline bool isShiftedMask(unsigned numBits, const APInt &APIVal) {
1719612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman  return isMask(numBits, (APIVal - APInt(numBits, 1)) | APIVal);
1720d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng}
1721d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1722cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Returns a byte-swapped representation of the specified APInt Value.
1723612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt byteSwap(const APInt &APIVal) { return APIVal.byteSwap(); }
1724d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1725cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Returns the floor log base 2 of the specified APInt value.
1726612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline unsigned logBase2(const APInt &APIVal) { return APIVal.logBase2(); }
1727d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1728cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Compute GCD of two APInt values.
1729cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
1730cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// This function returns the greatest common divisor of the two APInt values
1731cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// using Euclid's algorithm.
1732cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
1733cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \returns the greatest common divisor of Val1 and Val2
1734612ca08e662da624c29140d075c87c7fe2a70efaMichael GottesmanAPInt GreatestCommonDivisor(const APInt &Val1, const APInt &Val2);
1735d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1736cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Converts the given APInt to a double value.
1737cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
173866ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer/// Treats the APInt as an unsigned value for conversion purposes.
1739612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline double RoundAPIntToDouble(const APInt &APIVal) {
174066ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  return APIVal.roundToDouble();
174166ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer}
174266ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer
1743cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Converts the given APInt to a double value.
1744cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
174566ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer/// Treats the APInt as a signed value for conversion purposes.
1746612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline double RoundSignedAPIntToDouble(const APInt &APIVal) {
174766ed1099ff3591c61e008198bb5a30862e778fc0Reid Spencer  return APIVal.signedRoundToDouble();
1748d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng}
1749d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng
1750cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Converts the given APInt to a float vlalue.
1751612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline float RoundAPIntToFloat(const APInt &APIVal) {
1752e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return float(RoundAPIntToDouble(APIVal));
1753d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng}
1754d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng
1755cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Converts the given APInt to a float value.
1756cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
175752f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencer/// Treast the APInt as a signed value for conversion purposes.
1758612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline float RoundSignedAPIntToFloat(const APInt &APIVal) {
175952f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencer  return float(APIVal.signedRoundToDouble());
176052f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencer}
176152f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencer
1762cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Converts the given double value into a APInt.
1763cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
1764cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// This function convert a double value to an APInt value.
17659981b1f15681a2a09c9953cf6773a3b839ead4e7Chris LattnerAPInt RoundDoubleToAPInt(double Double, unsigned width);
1766d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng
1767cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Converts a float value into a APInt.
1768cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
1769cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// Converts a float value into an APInt value.
17709981b1f15681a2a09c9953cf6773a3b839ead4e7Chris Lattnerinline APInt RoundFloatToAPInt(float Float, unsigned width) {
177152f32d55665ef02e6ebe4518b0a1e2e2a4df0beaReid Spencer  return RoundDoubleToAPInt(double(Float), width);
1772d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng}
1773d93f00c35dbd1ea415bb2b39435253aef9428d71Zhou Sheng
1774cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Arithmetic right-shift function.
1775cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
17760b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Arithmetic right-shift the APInt by shiftAmt.
1777612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt ashr(const APInt &LHS, unsigned shiftAmt) {
1778e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return LHS.ashr(shiftAmt);
1779ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
17800b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1781cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Logical right-shift function.
1782cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
17830b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Logical right-shift the APInt by shiftAmt.
1784612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt lshr(const APInt &LHS, unsigned shiftAmt) {
1785e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return LHS.lshr(shiftAmt);
1786ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
17870b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1788cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Left-shift function.
1789cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
17900b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Left-shift the APInt by shiftAmt.
1791612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt shl(const APInt &LHS, unsigned shiftAmt) {
1792e81d2dad2c54014d36c73573307db5852c5caf8eReid Spencer  return LHS.shl(shiftAmt);
1793ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng}
17940b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1795cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Signed division function for APInt.
1796cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
17970b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Signed divide APInt LHS by APInt RHS.
1798612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt sdiv(const APInt &LHS, const APInt &RHS) { return LHS.sdiv(RHS); }
17990b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1800cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Unsigned division function for APInt.
1801cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
18020b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Unsigned divide APInt LHS by APInt RHS.
1803612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt udiv(const APInt &LHS, const APInt &RHS) { return LHS.udiv(RHS); }
18040b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1805cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Function for signed remainder operation.
1806cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
18070b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Signed remainder operation on APInt.
1808612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt srem(const APInt &LHS, const APInt &RHS) { return LHS.srem(RHS); }
18090b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1810cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Function for unsigned remainder operation.
1811cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
18120b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Unsigned remainder operation on APInt.
1813612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt urem(const APInt &LHS, const APInt &RHS) { return LHS.urem(RHS); }
18140b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1815cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Function for multiplication operation.
1816cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
18170b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs multiplication on APInt values.
1818612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt mul(const APInt &LHS, const APInt &RHS) { return LHS * RHS; }
18190b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1820cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Function for addition operation.
1821cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
18220b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs addition on APInt values.
1823612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt add(const APInt &LHS, const APInt &RHS) { return LHS + RHS; }
18240b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1825cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Function for subtraction operation.
1826cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
18270b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng/// Performs subtraction on APInt values.
1828612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt sub(const APInt &LHS, const APInt &RHS) { return LHS - RHS; }
18290b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1830cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Bitwise AND function for APInt.
1831cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
18323a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman/// Performs bitwise AND operation on APInt LHS and
1833ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// APInt RHS.
1834612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt And(const APInt &LHS, const APInt &RHS) { return LHS & RHS; }
1835ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
1836cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Bitwise OR function for APInt.
1837cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
1838ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise OR operation on APInt LHS and APInt RHS.
1839612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt Or(const APInt &LHS, const APInt &RHS) { return LHS | RHS; }
1840ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
1841cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Bitwise XOR function for APInt.
1842cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
1843ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs bitwise XOR operation on APInt.
1844612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt Xor(const APInt &LHS, const APInt &RHS) { return LHS ^ RHS; }
1845ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
1846cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman/// \brief Bitwise complement function.
1847cbc8777c588173b1d7d98b29f5a0b43ad35dc6e1Michael Gottesman///
1848ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng/// Performs a bitwise complement operation on APInt.
1849612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmaninline APInt Not(const APInt &APIVal) { return ~APIVal; }
1850ff4304f8243f55e2e5c63bc95517cd38ff9295e1Zhou Sheng
18510b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng} // End of APIntOps namespace
18520b706b18bd0a7760d971727460a1f26bff8289b0Zhou Sheng
1853612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman// See friend declaration above. This additional declaration is required in
1854612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesman// order to compile LLVM with IBM xlC compiler.
1855612ca08e662da624c29140d075c87c7fe2a70efaMichael Gottesmanhash_code hash_value(const APInt &Arg);
1856d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng} // End of llvm namespace
1857d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng
1858d0f285e212c47ac71af842bb39ea5364f1e556b4Zhou Sheng#endif
1859