xref: /aosp_15_r20/external/llvm/include/llvm/ADT/APInt.h (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker ///
10*9880d681SAndroid Build Coastguard Worker /// \file
11*9880d681SAndroid Build Coastguard Worker /// \brief This file implements a class to represent arbitrary precision
12*9880d681SAndroid Build Coastguard Worker /// integral constant values and operations on them.
13*9880d681SAndroid Build Coastguard Worker ///
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker 
16*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_ADT_APINT_H
17*9880d681SAndroid Build Coastguard Worker #define LLVM_ADT_APINT_H
18*9880d681SAndroid Build Coastguard Worker 
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Compiler.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MathExtras.h"
21*9880d681SAndroid Build Coastguard Worker #include <cassert>
22*9880d681SAndroid Build Coastguard Worker #include <climits>
23*9880d681SAndroid Build Coastguard Worker #include <cstring>
24*9880d681SAndroid Build Coastguard Worker #include <string>
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker namespace llvm {
27*9880d681SAndroid Build Coastguard Worker class FoldingSetNodeID;
28*9880d681SAndroid Build Coastguard Worker class StringRef;
29*9880d681SAndroid Build Coastguard Worker class hash_code;
30*9880d681SAndroid Build Coastguard Worker class raw_ostream;
31*9880d681SAndroid Build Coastguard Worker 
32*9880d681SAndroid Build Coastguard Worker template <typename T> class SmallVectorImpl;
33*9880d681SAndroid Build Coastguard Worker template <typename T> class ArrayRef;
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker // An unsigned host type used as a single part of a multi-part
36*9880d681SAndroid Build Coastguard Worker // bignum.
37*9880d681SAndroid Build Coastguard Worker typedef uint64_t integerPart;
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker const unsigned int host_char_bit = 8;
40*9880d681SAndroid Build Coastguard Worker const unsigned int integerPartWidth =
41*9880d681SAndroid Build Coastguard Worker     host_char_bit * static_cast<unsigned int>(sizeof(integerPart));
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
44*9880d681SAndroid Build Coastguard Worker //                              APInt Class
45*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker /// \brief Class for arbitrary precision integers.
48*9880d681SAndroid Build Coastguard Worker ///
49*9880d681SAndroid Build Coastguard Worker /// APInt is a functional replacement for common case unsigned integer type like
50*9880d681SAndroid Build Coastguard Worker /// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
51*9880d681SAndroid Build Coastguard Worker /// integer sizes and large integer value types such as 3-bits, 15-bits, or more
52*9880d681SAndroid Build Coastguard Worker /// than 64-bits of precision. APInt provides a variety of arithmetic operators
53*9880d681SAndroid Build Coastguard Worker /// and methods to manipulate integer values of any bit-width. It supports both
54*9880d681SAndroid Build Coastguard Worker /// the typical integer arithmetic and comparison operations as well as bitwise
55*9880d681SAndroid Build Coastguard Worker /// manipulation.
56*9880d681SAndroid Build Coastguard Worker ///
57*9880d681SAndroid Build Coastguard Worker /// The class has several invariants worth noting:
58*9880d681SAndroid Build Coastguard Worker ///   * All bit, byte, and word positions are zero-based.
59*9880d681SAndroid Build Coastguard Worker ///   * Once the bit width is set, it doesn't change except by the Truncate,
60*9880d681SAndroid Build Coastguard Worker ///     SignExtend, or ZeroExtend operations.
61*9880d681SAndroid Build Coastguard Worker ///   * All binary operators must be on APInt instances of the same bit width.
62*9880d681SAndroid Build Coastguard Worker ///     Attempting to use these operators on instances with different bit
63*9880d681SAndroid Build Coastguard Worker ///     widths will yield an assertion.
64*9880d681SAndroid Build Coastguard Worker ///   * The value is stored canonically as an unsigned value. For operations
65*9880d681SAndroid Build Coastguard Worker ///     where it makes a difference, there are both signed and unsigned variants
66*9880d681SAndroid Build Coastguard Worker ///     of the operation. For example, sdiv and udiv. However, because the bit
67*9880d681SAndroid Build Coastguard Worker ///     widths must be the same, operations such as Mul and Add produce the same
68*9880d681SAndroid Build Coastguard Worker ///     results regardless of whether the values are interpreted as signed or
69*9880d681SAndroid Build Coastguard Worker ///     not.
70*9880d681SAndroid Build Coastguard Worker ///   * In general, the class tries to follow the style of computation that LLVM
71*9880d681SAndroid Build Coastguard Worker ///     uses in its IR. This simplifies its use for LLVM.
72*9880d681SAndroid Build Coastguard Worker ///
73*9880d681SAndroid Build Coastguard Worker class APInt {
74*9880d681SAndroid Build Coastguard Worker   unsigned BitWidth; ///< The number of bits in this APInt.
75*9880d681SAndroid Build Coastguard Worker 
76*9880d681SAndroid Build Coastguard Worker   /// This union is used to store the integer value. When the
77*9880d681SAndroid Build Coastguard Worker   /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
78*9880d681SAndroid Build Coastguard Worker   union {
79*9880d681SAndroid Build Coastguard Worker     uint64_t VAL;   ///< Used to store the <= 64 bits integer value.
80*9880d681SAndroid Build Coastguard Worker     uint64_t *pVal; ///< Used to store the >64 bits integer value.
81*9880d681SAndroid Build Coastguard Worker   };
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker   /// This enum is used to hold the constants we needed for APInt.
84*9880d681SAndroid Build Coastguard Worker   enum {
85*9880d681SAndroid Build Coastguard Worker     /// Bits in a word
86*9880d681SAndroid Build Coastguard Worker     APINT_BITS_PER_WORD =
87*9880d681SAndroid Build Coastguard Worker         static_cast<unsigned int>(sizeof(uint64_t)) * CHAR_BIT,
88*9880d681SAndroid Build Coastguard Worker     /// Byte size of a word
89*9880d681SAndroid Build Coastguard Worker     APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
90*9880d681SAndroid Build Coastguard Worker   };
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker   friend struct DenseMapAPIntKeyInfo;
93*9880d681SAndroid Build Coastguard Worker 
94*9880d681SAndroid Build Coastguard Worker   /// \brief Fast internal constructor
95*9880d681SAndroid Build Coastguard Worker   ///
96*9880d681SAndroid Build Coastguard Worker   /// This constructor is used only internally for speed of construction of
97*9880d681SAndroid Build Coastguard Worker   /// temporaries. It is unsafe for general use so it is not public.
APInt(uint64_t * val,unsigned bits)98*9880d681SAndroid Build Coastguard Worker   APInt(uint64_t *val, unsigned bits) : BitWidth(bits), pVal(val) {}
99*9880d681SAndroid Build Coastguard Worker 
100*9880d681SAndroid Build Coastguard Worker   /// \brief Determine if this APInt just has one word to store value.
101*9880d681SAndroid Build Coastguard Worker   ///
102*9880d681SAndroid Build Coastguard Worker   /// \returns true if the number of bits <= 64, false otherwise.
isSingleWord()103*9880d681SAndroid Build Coastguard Worker   bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; }
104*9880d681SAndroid Build Coastguard Worker 
105*9880d681SAndroid Build Coastguard Worker   /// \brief Determine which word a bit is in.
106*9880d681SAndroid Build Coastguard Worker   ///
107*9880d681SAndroid Build Coastguard Worker   /// \returns the word position for the specified bit position.
whichWord(unsigned bitPosition)108*9880d681SAndroid Build Coastguard Worker   static unsigned whichWord(unsigned bitPosition) {
109*9880d681SAndroid Build Coastguard Worker     return bitPosition / APINT_BITS_PER_WORD;
110*9880d681SAndroid Build Coastguard Worker   }
111*9880d681SAndroid Build Coastguard Worker 
112*9880d681SAndroid Build Coastguard Worker   /// \brief Determine which bit in a word a bit is in.
113*9880d681SAndroid Build Coastguard Worker   ///
114*9880d681SAndroid Build Coastguard Worker   /// \returns the bit position in a word for the specified bit position
115*9880d681SAndroid Build Coastguard Worker   /// in the APInt.
whichBit(unsigned bitPosition)116*9880d681SAndroid Build Coastguard Worker   static unsigned whichBit(unsigned bitPosition) {
117*9880d681SAndroid Build Coastguard Worker     return bitPosition % APINT_BITS_PER_WORD;
118*9880d681SAndroid Build Coastguard Worker   }
119*9880d681SAndroid Build Coastguard Worker 
120*9880d681SAndroid Build Coastguard Worker   /// \brief Get a single bit mask.
121*9880d681SAndroid Build Coastguard Worker   ///
122*9880d681SAndroid Build Coastguard Worker   /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set
123*9880d681SAndroid Build Coastguard Worker   /// This method generates and returns a uint64_t (word) mask for a single
124*9880d681SAndroid Build Coastguard Worker   /// bit at a specific bit position. This is used to mask the bit in the
125*9880d681SAndroid Build Coastguard Worker   /// corresponding word.
maskBit(unsigned bitPosition)126*9880d681SAndroid Build Coastguard Worker   static uint64_t maskBit(unsigned bitPosition) {
127*9880d681SAndroid Build Coastguard Worker     return 1ULL << whichBit(bitPosition);
128*9880d681SAndroid Build Coastguard Worker   }
129*9880d681SAndroid Build Coastguard Worker 
130*9880d681SAndroid Build Coastguard Worker   /// \brief Clear unused high order bits
131*9880d681SAndroid Build Coastguard Worker   ///
132*9880d681SAndroid Build Coastguard Worker   /// This method is used internally to clear the top "N" bits in the high order
133*9880d681SAndroid Build Coastguard Worker   /// word that are not used by the APInt. This is needed after the most
134*9880d681SAndroid Build Coastguard Worker   /// significant word is assigned a value to ensure that those bits are
135*9880d681SAndroid Build Coastguard Worker   /// zero'd out.
clearUnusedBits()136*9880d681SAndroid Build Coastguard Worker   APInt &clearUnusedBits() {
137*9880d681SAndroid Build Coastguard Worker     // Compute how many bits are used in the final word
138*9880d681SAndroid Build Coastguard Worker     unsigned wordBits = BitWidth % APINT_BITS_PER_WORD;
139*9880d681SAndroid Build Coastguard Worker     if (wordBits == 0)
140*9880d681SAndroid Build Coastguard Worker       // If all bits are used, we want to leave the value alone. This also
141*9880d681SAndroid Build Coastguard Worker       // avoids the undefined behavior of >> when the shift is the same size as
142*9880d681SAndroid Build Coastguard Worker       // the word size (64).
143*9880d681SAndroid Build Coastguard Worker       return *this;
144*9880d681SAndroid Build Coastguard Worker 
145*9880d681SAndroid Build Coastguard Worker     // Mask out the high bits.
146*9880d681SAndroid Build Coastguard Worker     uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
147*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
148*9880d681SAndroid Build Coastguard Worker       VAL &= mask;
149*9880d681SAndroid Build Coastguard Worker     else
150*9880d681SAndroid Build Coastguard Worker       pVal[getNumWords() - 1] &= mask;
151*9880d681SAndroid Build Coastguard Worker     return *this;
152*9880d681SAndroid Build Coastguard Worker   }
153*9880d681SAndroid Build Coastguard Worker 
154*9880d681SAndroid Build Coastguard Worker   /// \brief Get the word corresponding to a bit position
155*9880d681SAndroid Build Coastguard Worker   /// \returns the corresponding word for the specified bit position.
getWord(unsigned bitPosition)156*9880d681SAndroid Build Coastguard Worker   uint64_t getWord(unsigned bitPosition) const {
157*9880d681SAndroid Build Coastguard Worker     return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
158*9880d681SAndroid Build Coastguard Worker   }
159*9880d681SAndroid Build Coastguard Worker 
160*9880d681SAndroid Build Coastguard Worker   /// \brief Convert a char array into an APInt
161*9880d681SAndroid Build Coastguard Worker   ///
162*9880d681SAndroid Build Coastguard Worker   /// \param radix 2, 8, 10, 16, or 36
163*9880d681SAndroid Build Coastguard Worker   /// Converts a string into a number.  The string must be non-empty
164*9880d681SAndroid Build Coastguard Worker   /// and well-formed as a number of the given base. The bit-width
165*9880d681SAndroid Build Coastguard Worker   /// must be sufficient to hold the result.
166*9880d681SAndroid Build Coastguard Worker   ///
167*9880d681SAndroid Build Coastguard Worker   /// This is used by the constructors that take string arguments.
168*9880d681SAndroid Build Coastguard Worker   ///
169*9880d681SAndroid Build Coastguard Worker   /// StringRef::getAsInteger is superficially similar but (1) does
170*9880d681SAndroid Build Coastguard Worker   /// not assume that the string is well-formed and (2) grows the
171*9880d681SAndroid Build Coastguard Worker   /// result to hold the input.
172*9880d681SAndroid Build Coastguard Worker   void fromString(unsigned numBits, StringRef str, uint8_t radix);
173*9880d681SAndroid Build Coastguard Worker 
174*9880d681SAndroid Build Coastguard Worker   /// \brief An internal division function for dividing APInts.
175*9880d681SAndroid Build Coastguard Worker   ///
176*9880d681SAndroid Build Coastguard Worker   /// This is used by the toString method to divide by the radix. It simply
177*9880d681SAndroid Build Coastguard Worker   /// provides a more convenient form of divide for internal use since KnuthDiv
178*9880d681SAndroid Build Coastguard Worker   /// has specific constraints on its inputs. If those constraints are not met
179*9880d681SAndroid Build Coastguard Worker   /// then it provides a simpler form of divide.
180*9880d681SAndroid Build Coastguard Worker   static void divide(const APInt &LHS, unsigned lhsWords, const APInt &RHS,
181*9880d681SAndroid Build Coastguard Worker                      unsigned rhsWords, APInt *Quotient, APInt *Remainder);
182*9880d681SAndroid Build Coastguard Worker 
183*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for inline constructor
184*9880d681SAndroid Build Coastguard Worker   void initSlowCase(uint64_t val, bool isSigned);
185*9880d681SAndroid Build Coastguard Worker 
186*9880d681SAndroid Build Coastguard Worker   /// shared code between two array constructors
187*9880d681SAndroid Build Coastguard Worker   void initFromArray(ArrayRef<uint64_t> array);
188*9880d681SAndroid Build Coastguard Worker 
189*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for inline copy constructor
190*9880d681SAndroid Build Coastguard Worker   void initSlowCase(const APInt &that);
191*9880d681SAndroid Build Coastguard Worker 
192*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for shl
193*9880d681SAndroid Build Coastguard Worker   APInt shlSlowCase(unsigned shiftAmt) const;
194*9880d681SAndroid Build Coastguard Worker 
195*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for operator&
196*9880d681SAndroid Build Coastguard Worker   APInt AndSlowCase(const APInt &RHS) const;
197*9880d681SAndroid Build Coastguard Worker 
198*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for operator|
199*9880d681SAndroid Build Coastguard Worker   APInt OrSlowCase(const APInt &RHS) const;
200*9880d681SAndroid Build Coastguard Worker 
201*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for operator^
202*9880d681SAndroid Build Coastguard Worker   APInt XorSlowCase(const APInt &RHS) const;
203*9880d681SAndroid Build Coastguard Worker 
204*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for operator=
205*9880d681SAndroid Build Coastguard Worker   APInt &AssignSlowCase(const APInt &RHS);
206*9880d681SAndroid Build Coastguard Worker 
207*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for operator==
208*9880d681SAndroid Build Coastguard Worker   bool EqualSlowCase(const APInt &RHS) const;
209*9880d681SAndroid Build Coastguard Worker 
210*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for operator==
211*9880d681SAndroid Build Coastguard Worker   bool EqualSlowCase(uint64_t Val) const;
212*9880d681SAndroid Build Coastguard Worker 
213*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for countLeadingZeros
214*9880d681SAndroid Build Coastguard Worker   unsigned countLeadingZerosSlowCase() const;
215*9880d681SAndroid Build Coastguard Worker 
216*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for countTrailingOnes
217*9880d681SAndroid Build Coastguard Worker   unsigned countTrailingOnesSlowCase() const;
218*9880d681SAndroid Build Coastguard Worker 
219*9880d681SAndroid Build Coastguard Worker   /// out-of-line slow case for countPopulation
220*9880d681SAndroid Build Coastguard Worker   unsigned countPopulationSlowCase() const;
221*9880d681SAndroid Build Coastguard Worker 
222*9880d681SAndroid Build Coastguard Worker public:
223*9880d681SAndroid Build Coastguard Worker   /// \name Constructors
224*9880d681SAndroid Build Coastguard Worker   /// @{
225*9880d681SAndroid Build Coastguard Worker 
226*9880d681SAndroid Build Coastguard Worker   /// \brief Create a new APInt of numBits width, initialized as val.
227*9880d681SAndroid Build Coastguard Worker   ///
228*9880d681SAndroid Build Coastguard Worker   /// If isSigned is true then val is treated as if it were a signed value
229*9880d681SAndroid Build Coastguard Worker   /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
230*9880d681SAndroid Build Coastguard Worker   /// will be done. Otherwise, no sign extension occurs (high order bits beyond
231*9880d681SAndroid Build Coastguard Worker   /// the range of val are zero filled).
232*9880d681SAndroid Build Coastguard Worker   ///
233*9880d681SAndroid Build Coastguard Worker   /// \param numBits the bit width of the constructed APInt
234*9880d681SAndroid Build Coastguard Worker   /// \param val the initial value of the APInt
235*9880d681SAndroid Build Coastguard Worker   /// \param isSigned how to treat signedness of val
236*9880d681SAndroid Build Coastguard Worker   APInt(unsigned numBits, uint64_t val, bool isSigned = false)
BitWidth(numBits)237*9880d681SAndroid Build Coastguard Worker       : BitWidth(numBits), VAL(0) {
238*9880d681SAndroid Build Coastguard Worker     assert(BitWidth && "bitwidth too small");
239*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
240*9880d681SAndroid Build Coastguard Worker       VAL = val;
241*9880d681SAndroid Build Coastguard Worker     else
242*9880d681SAndroid Build Coastguard Worker       initSlowCase(val, isSigned);
243*9880d681SAndroid Build Coastguard Worker     clearUnusedBits();
244*9880d681SAndroid Build Coastguard Worker   }
245*9880d681SAndroid Build Coastguard Worker 
246*9880d681SAndroid Build Coastguard Worker   /// \brief Construct an APInt of numBits width, initialized as bigVal[].
247*9880d681SAndroid Build Coastguard Worker   ///
248*9880d681SAndroid Build Coastguard Worker   /// Note that bigVal.size() can be smaller or larger than the corresponding
249*9880d681SAndroid Build Coastguard Worker   /// bit width but any extraneous bits will be dropped.
250*9880d681SAndroid Build Coastguard Worker   ///
251*9880d681SAndroid Build Coastguard Worker   /// \param numBits the bit width of the constructed APInt
252*9880d681SAndroid Build Coastguard Worker   /// \param bigVal a sequence of words to form the initial value of the APInt
253*9880d681SAndroid Build Coastguard Worker   APInt(unsigned numBits, ArrayRef<uint64_t> bigVal);
254*9880d681SAndroid Build Coastguard Worker 
255*9880d681SAndroid Build Coastguard Worker   /// Equivalent to APInt(numBits, ArrayRef<uint64_t>(bigVal, numWords)), but
256*9880d681SAndroid Build Coastguard Worker   /// deprecated because this constructor is prone to ambiguity with the
257*9880d681SAndroid Build Coastguard Worker   /// APInt(unsigned, uint64_t, bool) constructor.
258*9880d681SAndroid Build Coastguard Worker   ///
259*9880d681SAndroid Build Coastguard Worker   /// If this overload is ever deleted, care should be taken to prevent calls
260*9880d681SAndroid Build Coastguard Worker   /// from being incorrectly captured by the APInt(unsigned, uint64_t, bool)
261*9880d681SAndroid Build Coastguard Worker   /// constructor.
262*9880d681SAndroid Build Coastguard Worker   APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
263*9880d681SAndroid Build Coastguard Worker 
264*9880d681SAndroid Build Coastguard Worker   /// \brief Construct an APInt from a string representation.
265*9880d681SAndroid Build Coastguard Worker   ///
266*9880d681SAndroid Build Coastguard Worker   /// This constructor interprets the string \p str in the given radix. The
267*9880d681SAndroid Build Coastguard Worker   /// interpretation stops when the first character that is not suitable for the
268*9880d681SAndroid Build Coastguard Worker   /// radix is encountered, or the end of the string. Acceptable radix values
269*9880d681SAndroid Build Coastguard Worker   /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the
270*9880d681SAndroid Build Coastguard Worker   /// string to require more bits than numBits.
271*9880d681SAndroid Build Coastguard Worker   ///
272*9880d681SAndroid Build Coastguard Worker   /// \param numBits the bit width of the constructed APInt
273*9880d681SAndroid Build Coastguard Worker   /// \param str the string to be interpreted
274*9880d681SAndroid Build Coastguard Worker   /// \param radix the radix to use for the conversion
275*9880d681SAndroid Build Coastguard Worker   APInt(unsigned numBits, StringRef str, uint8_t radix);
276*9880d681SAndroid Build Coastguard Worker 
277*9880d681SAndroid Build Coastguard Worker   /// Simply makes *this a copy of that.
278*9880d681SAndroid Build Coastguard Worker   /// @brief Copy Constructor.
APInt(const APInt & that)279*9880d681SAndroid Build Coastguard Worker   APInt(const APInt &that) : BitWidth(that.BitWidth), VAL(0) {
280*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
281*9880d681SAndroid Build Coastguard Worker       VAL = that.VAL;
282*9880d681SAndroid Build Coastguard Worker     else
283*9880d681SAndroid Build Coastguard Worker       initSlowCase(that);
284*9880d681SAndroid Build Coastguard Worker   }
285*9880d681SAndroid Build Coastguard Worker 
286*9880d681SAndroid Build Coastguard Worker   /// \brief Move Constructor.
APInt(APInt && that)287*9880d681SAndroid Build Coastguard Worker   APInt(APInt &&that) : BitWidth(that.BitWidth), VAL(that.VAL) {
288*9880d681SAndroid Build Coastguard Worker     that.BitWidth = 0;
289*9880d681SAndroid Build Coastguard Worker   }
290*9880d681SAndroid Build Coastguard Worker 
291*9880d681SAndroid Build Coastguard Worker   /// \brief Destructor.
~APInt()292*9880d681SAndroid Build Coastguard Worker   ~APInt() {
293*9880d681SAndroid Build Coastguard Worker     if (needsCleanup())
294*9880d681SAndroid Build Coastguard Worker       delete[] pVal;
295*9880d681SAndroid Build Coastguard Worker   }
296*9880d681SAndroid Build Coastguard Worker 
297*9880d681SAndroid Build Coastguard Worker   /// \brief Default constructor that creates an uninteresting APInt
298*9880d681SAndroid Build Coastguard Worker   /// representing a 1-bit zero value.
299*9880d681SAndroid Build Coastguard Worker   ///
300*9880d681SAndroid Build Coastguard Worker   /// This is useful for object deserialization (pair this with the static
301*9880d681SAndroid Build Coastguard Worker   ///  method Read).
APInt()302*9880d681SAndroid Build Coastguard Worker   explicit APInt() : BitWidth(1), VAL(0) {}
303*9880d681SAndroid Build Coastguard Worker 
304*9880d681SAndroid Build Coastguard Worker   /// \brief Returns whether this instance allocated memory.
needsCleanup()305*9880d681SAndroid Build Coastguard Worker   bool needsCleanup() const { return !isSingleWord(); }
306*9880d681SAndroid Build Coastguard Worker 
307*9880d681SAndroid Build Coastguard Worker   /// Used to insert APInt objects, or objects that contain APInt objects, into
308*9880d681SAndroid Build Coastguard Worker   ///  FoldingSets.
309*9880d681SAndroid Build Coastguard Worker   void Profile(FoldingSetNodeID &id) const;
310*9880d681SAndroid Build Coastguard Worker 
311*9880d681SAndroid Build Coastguard Worker   /// @}
312*9880d681SAndroid Build Coastguard Worker   /// \name Value Tests
313*9880d681SAndroid Build Coastguard Worker   /// @{
314*9880d681SAndroid Build Coastguard Worker 
315*9880d681SAndroid Build Coastguard Worker   /// \brief Determine sign of this APInt.
316*9880d681SAndroid Build Coastguard Worker   ///
317*9880d681SAndroid Build Coastguard Worker   /// This tests the high bit of this APInt to determine if it is set.
318*9880d681SAndroid Build Coastguard Worker   ///
319*9880d681SAndroid Build Coastguard Worker   /// \returns true if this APInt is negative, false otherwise
isNegative()320*9880d681SAndroid Build Coastguard Worker   bool isNegative() const { return (*this)[BitWidth - 1]; }
321*9880d681SAndroid Build Coastguard Worker 
322*9880d681SAndroid Build Coastguard Worker   /// \brief Determine if this APInt Value is non-negative (>= 0)
323*9880d681SAndroid Build Coastguard Worker   ///
324*9880d681SAndroid Build Coastguard Worker   /// This tests the high bit of the APInt to determine if it is unset.
isNonNegative()325*9880d681SAndroid Build Coastguard Worker   bool isNonNegative() const { return !isNegative(); }
326*9880d681SAndroid Build Coastguard Worker 
327*9880d681SAndroid Build Coastguard Worker   /// \brief Determine if this APInt Value is positive.
328*9880d681SAndroid Build Coastguard Worker   ///
329*9880d681SAndroid Build Coastguard Worker   /// This tests if the value of this APInt is positive (> 0). Note
330*9880d681SAndroid Build Coastguard Worker   /// that 0 is not a positive value.
331*9880d681SAndroid Build Coastguard Worker   ///
332*9880d681SAndroid Build Coastguard Worker   /// \returns true if this APInt is positive.
isStrictlyPositive()333*9880d681SAndroid Build Coastguard Worker   bool isStrictlyPositive() const { return isNonNegative() && !!*this; }
334*9880d681SAndroid Build Coastguard Worker 
335*9880d681SAndroid Build Coastguard Worker   /// \brief Determine if all bits are set
336*9880d681SAndroid Build Coastguard Worker   ///
337*9880d681SAndroid Build Coastguard Worker   /// This checks to see if the value has all bits of the APInt are set or not.
isAllOnesValue()338*9880d681SAndroid Build Coastguard Worker   bool isAllOnesValue() const {
339*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
340*9880d681SAndroid Build Coastguard Worker       return VAL == ~integerPart(0) >> (APINT_BITS_PER_WORD - BitWidth);
341*9880d681SAndroid Build Coastguard Worker     return countPopulationSlowCase() == BitWidth;
342*9880d681SAndroid Build Coastguard Worker   }
343*9880d681SAndroid Build Coastguard Worker 
344*9880d681SAndroid Build Coastguard Worker   /// \brief Determine if this is the largest unsigned value.
345*9880d681SAndroid Build Coastguard Worker   ///
346*9880d681SAndroid Build Coastguard Worker   /// This checks to see if the value of this APInt is the maximum unsigned
347*9880d681SAndroid Build Coastguard Worker   /// value for the APInt's bit width.
isMaxValue()348*9880d681SAndroid Build Coastguard Worker   bool isMaxValue() const { return isAllOnesValue(); }
349*9880d681SAndroid Build Coastguard Worker 
350*9880d681SAndroid Build Coastguard Worker   /// \brief Determine if this is the largest signed value.
351*9880d681SAndroid Build Coastguard Worker   ///
352*9880d681SAndroid Build Coastguard Worker   /// This checks to see if the value of this APInt is the maximum signed
353*9880d681SAndroid Build Coastguard Worker   /// value for the APInt's bit width.
isMaxSignedValue()354*9880d681SAndroid Build Coastguard Worker   bool isMaxSignedValue() const {
355*9880d681SAndroid Build Coastguard Worker     return !isNegative() && countPopulation() == BitWidth - 1;
356*9880d681SAndroid Build Coastguard Worker   }
357*9880d681SAndroid Build Coastguard Worker 
358*9880d681SAndroid Build Coastguard Worker   /// \brief Determine if this is the smallest unsigned value.
359*9880d681SAndroid Build Coastguard Worker   ///
360*9880d681SAndroid Build Coastguard Worker   /// This checks to see if the value of this APInt is the minimum unsigned
361*9880d681SAndroid Build Coastguard Worker   /// value for the APInt's bit width.
isMinValue()362*9880d681SAndroid Build Coastguard Worker   bool isMinValue() const { return !*this; }
363*9880d681SAndroid Build Coastguard Worker 
364*9880d681SAndroid Build Coastguard Worker   /// \brief Determine if this is the smallest signed value.
365*9880d681SAndroid Build Coastguard Worker   ///
366*9880d681SAndroid Build Coastguard Worker   /// This checks to see if the value of this APInt is the minimum signed
367*9880d681SAndroid Build Coastguard Worker   /// value for the APInt's bit width.
isMinSignedValue()368*9880d681SAndroid Build Coastguard Worker   bool isMinSignedValue() const {
369*9880d681SAndroid Build Coastguard Worker     return isNegative() && isPowerOf2();
370*9880d681SAndroid Build Coastguard Worker   }
371*9880d681SAndroid Build Coastguard Worker 
372*9880d681SAndroid Build Coastguard Worker   /// \brief Check if this APInt has an N-bits unsigned integer value.
isIntN(unsigned N)373*9880d681SAndroid Build Coastguard Worker   bool isIntN(unsigned N) const {
374*9880d681SAndroid Build Coastguard Worker     assert(N && "N == 0 ???");
375*9880d681SAndroid Build Coastguard Worker     return getActiveBits() <= N;
376*9880d681SAndroid Build Coastguard Worker   }
377*9880d681SAndroid Build Coastguard Worker 
378*9880d681SAndroid Build Coastguard Worker   /// \brief Check if this APInt has an N-bits signed integer value.
isSignedIntN(unsigned N)379*9880d681SAndroid Build Coastguard Worker   bool isSignedIntN(unsigned N) const {
380*9880d681SAndroid Build Coastguard Worker     assert(N && "N == 0 ???");
381*9880d681SAndroid Build Coastguard Worker     return getMinSignedBits() <= N;
382*9880d681SAndroid Build Coastguard Worker   }
383*9880d681SAndroid Build Coastguard Worker 
384*9880d681SAndroid Build Coastguard Worker   /// \brief Check if this APInt's value is a power of two greater than zero.
385*9880d681SAndroid Build Coastguard Worker   ///
386*9880d681SAndroid Build Coastguard Worker   /// \returns true if the argument APInt value is a power of two > 0.
isPowerOf2()387*9880d681SAndroid Build Coastguard Worker   bool isPowerOf2() const {
388*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
389*9880d681SAndroid Build Coastguard Worker       return isPowerOf2_64(VAL);
390*9880d681SAndroid Build Coastguard Worker     return countPopulationSlowCase() == 1;
391*9880d681SAndroid Build Coastguard Worker   }
392*9880d681SAndroid Build Coastguard Worker 
393*9880d681SAndroid Build Coastguard Worker   /// \brief Check if the APInt's value is returned by getSignBit.
394*9880d681SAndroid Build Coastguard Worker   ///
395*9880d681SAndroid Build Coastguard Worker   /// \returns true if this is the value returned by getSignBit.
isSignBit()396*9880d681SAndroid Build Coastguard Worker   bool isSignBit() const { return isMinSignedValue(); }
397*9880d681SAndroid Build Coastguard Worker 
398*9880d681SAndroid Build Coastguard Worker   /// \brief Convert APInt to a boolean value.
399*9880d681SAndroid Build Coastguard Worker   ///
400*9880d681SAndroid Build Coastguard Worker   /// This converts the APInt to a boolean value as a test against zero.
getBoolValue()401*9880d681SAndroid Build Coastguard Worker   bool getBoolValue() const { return !!*this; }
402*9880d681SAndroid Build Coastguard Worker 
403*9880d681SAndroid Build Coastguard Worker   /// If this value is smaller than the specified limit, return it, otherwise
404*9880d681SAndroid Build Coastguard Worker   /// return the limit value.  This causes the value to saturate to the limit.
405*9880d681SAndroid Build Coastguard Worker   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
406*9880d681SAndroid Build Coastguard Worker     return (getActiveBits() > 64 || getZExtValue() > Limit) ? Limit
407*9880d681SAndroid Build Coastguard Worker                                                             : getZExtValue();
408*9880d681SAndroid Build Coastguard Worker   }
409*9880d681SAndroid Build Coastguard Worker 
410*9880d681SAndroid Build Coastguard Worker   /// \brief Check if the APInt consists of a repeated bit pattern.
411*9880d681SAndroid Build Coastguard Worker   ///
412*9880d681SAndroid Build Coastguard Worker   /// e.g. 0x01010101 satisfies isSplat(8).
413*9880d681SAndroid Build Coastguard Worker   /// \param SplatSizeInBits The size of the pattern in bits. Must divide bit
414*9880d681SAndroid Build Coastguard Worker   /// width without remainder.
415*9880d681SAndroid Build Coastguard Worker   bool isSplat(unsigned SplatSizeInBits) const;
416*9880d681SAndroid Build Coastguard Worker 
417*9880d681SAndroid Build Coastguard Worker   /// @}
418*9880d681SAndroid Build Coastguard Worker   /// \name Value Generators
419*9880d681SAndroid Build Coastguard Worker   /// @{
420*9880d681SAndroid Build Coastguard Worker 
421*9880d681SAndroid Build Coastguard Worker   /// \brief Gets maximum unsigned value of APInt for specific bit width.
getMaxValue(unsigned numBits)422*9880d681SAndroid Build Coastguard Worker   static APInt getMaxValue(unsigned numBits) {
423*9880d681SAndroid Build Coastguard Worker     return getAllOnesValue(numBits);
424*9880d681SAndroid Build Coastguard Worker   }
425*9880d681SAndroid Build Coastguard Worker 
426*9880d681SAndroid Build Coastguard Worker   /// \brief Gets maximum signed value of APInt for a specific bit width.
getSignedMaxValue(unsigned numBits)427*9880d681SAndroid Build Coastguard Worker   static APInt getSignedMaxValue(unsigned numBits) {
428*9880d681SAndroid Build Coastguard Worker     APInt API = getAllOnesValue(numBits);
429*9880d681SAndroid Build Coastguard Worker     API.clearBit(numBits - 1);
430*9880d681SAndroid Build Coastguard Worker     return API;
431*9880d681SAndroid Build Coastguard Worker   }
432*9880d681SAndroid Build Coastguard Worker 
433*9880d681SAndroid Build Coastguard Worker   /// \brief Gets minimum unsigned value of APInt for a specific bit width.
getMinValue(unsigned numBits)434*9880d681SAndroid Build Coastguard Worker   static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); }
435*9880d681SAndroid Build Coastguard Worker 
436*9880d681SAndroid Build Coastguard Worker   /// \brief Gets minimum signed value of APInt for a specific bit width.
getSignedMinValue(unsigned numBits)437*9880d681SAndroid Build Coastguard Worker   static APInt getSignedMinValue(unsigned numBits) {
438*9880d681SAndroid Build Coastguard Worker     APInt API(numBits, 0);
439*9880d681SAndroid Build Coastguard Worker     API.setBit(numBits - 1);
440*9880d681SAndroid Build Coastguard Worker     return API;
441*9880d681SAndroid Build Coastguard Worker   }
442*9880d681SAndroid Build Coastguard Worker 
443*9880d681SAndroid Build Coastguard Worker   /// \brief Get the SignBit for a specific bit width.
444*9880d681SAndroid Build Coastguard Worker   ///
445*9880d681SAndroid Build Coastguard Worker   /// This is just a wrapper function of getSignedMinValue(), and it helps code
446*9880d681SAndroid Build Coastguard Worker   /// readability when we want to get a SignBit.
getSignBit(unsigned BitWidth)447*9880d681SAndroid Build Coastguard Worker   static APInt getSignBit(unsigned BitWidth) {
448*9880d681SAndroid Build Coastguard Worker     return getSignedMinValue(BitWidth);
449*9880d681SAndroid Build Coastguard Worker   }
450*9880d681SAndroid Build Coastguard Worker 
451*9880d681SAndroid Build Coastguard Worker   /// \brief Get the all-ones value.
452*9880d681SAndroid Build Coastguard Worker   ///
453*9880d681SAndroid Build Coastguard Worker   /// \returns the all-ones value for an APInt of the specified bit-width.
getAllOnesValue(unsigned numBits)454*9880d681SAndroid Build Coastguard Worker   static APInt getAllOnesValue(unsigned numBits) {
455*9880d681SAndroid Build Coastguard Worker     return APInt(numBits, UINT64_MAX, true);
456*9880d681SAndroid Build Coastguard Worker   }
457*9880d681SAndroid Build Coastguard Worker 
458*9880d681SAndroid Build Coastguard Worker   /// \brief Get the '0' value.
459*9880d681SAndroid Build Coastguard Worker   ///
460*9880d681SAndroid Build Coastguard Worker   /// \returns the '0' value for an APInt of the specified bit-width.
getNullValue(unsigned numBits)461*9880d681SAndroid Build Coastguard Worker   static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); }
462*9880d681SAndroid Build Coastguard Worker 
463*9880d681SAndroid Build Coastguard Worker   /// \brief Compute an APInt containing numBits highbits from this APInt.
464*9880d681SAndroid Build Coastguard Worker   ///
465*9880d681SAndroid Build Coastguard Worker   /// Get an APInt with the same BitWidth as this APInt, just zero mask
466*9880d681SAndroid Build Coastguard Worker   /// the low bits and right shift to the least significant bit.
467*9880d681SAndroid Build Coastguard Worker   ///
468*9880d681SAndroid Build Coastguard Worker   /// \returns the high "numBits" bits of this APInt.
469*9880d681SAndroid Build Coastguard Worker   APInt getHiBits(unsigned numBits) const;
470*9880d681SAndroid Build Coastguard Worker 
471*9880d681SAndroid Build Coastguard Worker   /// \brief Compute an APInt containing numBits lowbits from this APInt.
472*9880d681SAndroid Build Coastguard Worker   ///
473*9880d681SAndroid Build Coastguard Worker   /// Get an APInt with the same BitWidth as this APInt, just zero mask
474*9880d681SAndroid Build Coastguard Worker   /// the high bits.
475*9880d681SAndroid Build Coastguard Worker   ///
476*9880d681SAndroid Build Coastguard Worker   /// \returns the low "numBits" bits of this APInt.
477*9880d681SAndroid Build Coastguard Worker   APInt getLoBits(unsigned numBits) const;
478*9880d681SAndroid Build Coastguard Worker 
479*9880d681SAndroid Build Coastguard Worker   /// \brief Return an APInt with exactly one bit set in the result.
getOneBitSet(unsigned numBits,unsigned BitNo)480*9880d681SAndroid Build Coastguard Worker   static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
481*9880d681SAndroid Build Coastguard Worker     APInt Res(numBits, 0);
482*9880d681SAndroid Build Coastguard Worker     Res.setBit(BitNo);
483*9880d681SAndroid Build Coastguard Worker     return Res;
484*9880d681SAndroid Build Coastguard Worker   }
485*9880d681SAndroid Build Coastguard Worker 
486*9880d681SAndroid Build Coastguard Worker   /// \brief Get a value with a block of bits set.
487*9880d681SAndroid Build Coastguard Worker   ///
488*9880d681SAndroid Build Coastguard Worker   /// Constructs an APInt value that has a contiguous range of bits set. The
489*9880d681SAndroid Build Coastguard Worker   /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
490*9880d681SAndroid Build Coastguard Worker   /// bits will be zero. For example, with parameters(32, 0, 16) you would get
491*9880d681SAndroid Build Coastguard Worker   /// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For
492*9880d681SAndroid Build Coastguard Worker   /// example, with parameters (32, 28, 4), you would get 0xF000000F.
493*9880d681SAndroid Build Coastguard Worker   ///
494*9880d681SAndroid Build Coastguard Worker   /// \param numBits the intended bit width of the result
495*9880d681SAndroid Build Coastguard Worker   /// \param loBit the index of the lowest bit set.
496*9880d681SAndroid Build Coastguard Worker   /// \param hiBit the index of the highest bit set.
497*9880d681SAndroid Build Coastguard Worker   ///
498*9880d681SAndroid Build Coastguard Worker   /// \returns An APInt value with the requested bits set.
getBitsSet(unsigned numBits,unsigned loBit,unsigned hiBit)499*9880d681SAndroid Build Coastguard Worker   static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
500*9880d681SAndroid Build Coastguard Worker     assert(hiBit <= numBits && "hiBit out of range");
501*9880d681SAndroid Build Coastguard Worker     assert(loBit < numBits && "loBit out of range");
502*9880d681SAndroid Build Coastguard Worker     if (hiBit < loBit)
503*9880d681SAndroid Build Coastguard Worker       return getLowBitsSet(numBits, hiBit) |
504*9880d681SAndroid Build Coastguard Worker              getHighBitsSet(numBits, numBits - loBit);
505*9880d681SAndroid Build Coastguard Worker     return getLowBitsSet(numBits, hiBit - loBit).shl(loBit);
506*9880d681SAndroid Build Coastguard Worker   }
507*9880d681SAndroid Build Coastguard Worker 
508*9880d681SAndroid Build Coastguard Worker   /// \brief Get a value with high bits set
509*9880d681SAndroid Build Coastguard Worker   ///
510*9880d681SAndroid Build Coastguard Worker   /// Constructs an APInt value that has the top hiBitsSet bits set.
511*9880d681SAndroid Build Coastguard Worker   ///
512*9880d681SAndroid Build Coastguard Worker   /// \param numBits the bitwidth of the result
513*9880d681SAndroid Build Coastguard Worker   /// \param hiBitsSet the number of high-order bits set in the result.
getHighBitsSet(unsigned numBits,unsigned hiBitsSet)514*9880d681SAndroid Build Coastguard Worker   static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
515*9880d681SAndroid Build Coastguard Worker     assert(hiBitsSet <= numBits && "Too many bits to set!");
516*9880d681SAndroid Build Coastguard Worker     // Handle a degenerate case, to avoid shifting by word size
517*9880d681SAndroid Build Coastguard Worker     if (hiBitsSet == 0)
518*9880d681SAndroid Build Coastguard Worker       return APInt(numBits, 0);
519*9880d681SAndroid Build Coastguard Worker     unsigned shiftAmt = numBits - hiBitsSet;
520*9880d681SAndroid Build Coastguard Worker     // For small values, return quickly
521*9880d681SAndroid Build Coastguard Worker     if (numBits <= APINT_BITS_PER_WORD)
522*9880d681SAndroid Build Coastguard Worker       return APInt(numBits, ~0ULL << shiftAmt);
523*9880d681SAndroid Build Coastguard Worker     return getAllOnesValue(numBits).shl(shiftAmt);
524*9880d681SAndroid Build Coastguard Worker   }
525*9880d681SAndroid Build Coastguard Worker 
526*9880d681SAndroid Build Coastguard Worker   /// \brief Get a value with low bits set
527*9880d681SAndroid Build Coastguard Worker   ///
528*9880d681SAndroid Build Coastguard Worker   /// Constructs an APInt value that has the bottom loBitsSet bits set.
529*9880d681SAndroid Build Coastguard Worker   ///
530*9880d681SAndroid Build Coastguard Worker   /// \param numBits the bitwidth of the result
531*9880d681SAndroid Build Coastguard Worker   /// \param loBitsSet the number of low-order bits set in the result.
getLowBitsSet(unsigned numBits,unsigned loBitsSet)532*9880d681SAndroid Build Coastguard Worker   static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) {
533*9880d681SAndroid Build Coastguard Worker     assert(loBitsSet <= numBits && "Too many bits to set!");
534*9880d681SAndroid Build Coastguard Worker     // Handle a degenerate case, to avoid shifting by word size
535*9880d681SAndroid Build Coastguard Worker     if (loBitsSet == 0)
536*9880d681SAndroid Build Coastguard Worker       return APInt(numBits, 0);
537*9880d681SAndroid Build Coastguard Worker     if (loBitsSet == APINT_BITS_PER_WORD)
538*9880d681SAndroid Build Coastguard Worker       return APInt(numBits, UINT64_MAX);
539*9880d681SAndroid Build Coastguard Worker     // For small values, return quickly.
540*9880d681SAndroid Build Coastguard Worker     if (loBitsSet <= APINT_BITS_PER_WORD)
541*9880d681SAndroid Build Coastguard Worker       return APInt(numBits, UINT64_MAX >> (APINT_BITS_PER_WORD - loBitsSet));
542*9880d681SAndroid Build Coastguard Worker     return getAllOnesValue(numBits).lshr(numBits - loBitsSet);
543*9880d681SAndroid Build Coastguard Worker   }
544*9880d681SAndroid Build Coastguard Worker 
545*9880d681SAndroid Build Coastguard Worker   /// \brief Return a value containing V broadcasted over NewLen bits.
getSplat(unsigned NewLen,const APInt & V)546*9880d681SAndroid Build Coastguard Worker   static APInt getSplat(unsigned NewLen, const APInt &V) {
547*9880d681SAndroid Build Coastguard Worker     assert(NewLen >= V.getBitWidth() && "Can't splat to smaller bit width!");
548*9880d681SAndroid Build Coastguard Worker 
549*9880d681SAndroid Build Coastguard Worker     APInt Val = V.zextOrSelf(NewLen);
550*9880d681SAndroid Build Coastguard Worker     for (unsigned I = V.getBitWidth(); I < NewLen; I <<= 1)
551*9880d681SAndroid Build Coastguard Worker       Val |= Val << I;
552*9880d681SAndroid Build Coastguard Worker 
553*9880d681SAndroid Build Coastguard Worker     return Val;
554*9880d681SAndroid Build Coastguard Worker   }
555*9880d681SAndroid Build Coastguard Worker 
556*9880d681SAndroid Build Coastguard Worker   /// \brief Determine if two APInts have the same value, after zero-extending
557*9880d681SAndroid Build Coastguard Worker   /// one of them (if needed!) to ensure that the bit-widths match.
isSameValue(const APInt & I1,const APInt & I2)558*9880d681SAndroid Build Coastguard Worker   static bool isSameValue(const APInt &I1, const APInt &I2) {
559*9880d681SAndroid Build Coastguard Worker     if (I1.getBitWidth() == I2.getBitWidth())
560*9880d681SAndroid Build Coastguard Worker       return I1 == I2;
561*9880d681SAndroid Build Coastguard Worker 
562*9880d681SAndroid Build Coastguard Worker     if (I1.getBitWidth() > I2.getBitWidth())
563*9880d681SAndroid Build Coastguard Worker       return I1 == I2.zext(I1.getBitWidth());
564*9880d681SAndroid Build Coastguard Worker 
565*9880d681SAndroid Build Coastguard Worker     return I1.zext(I2.getBitWidth()) == I2;
566*9880d681SAndroid Build Coastguard Worker   }
567*9880d681SAndroid Build Coastguard Worker 
568*9880d681SAndroid Build Coastguard Worker   /// \brief Overload to compute a hash_code for an APInt value.
569*9880d681SAndroid Build Coastguard Worker   friend hash_code hash_value(const APInt &Arg);
570*9880d681SAndroid Build Coastguard Worker 
571*9880d681SAndroid Build Coastguard Worker   /// This function returns a pointer to the internal storage of the APInt.
572*9880d681SAndroid Build Coastguard Worker   /// This is useful for writing out the APInt in binary form without any
573*9880d681SAndroid Build Coastguard Worker   /// conversions.
getRawData()574*9880d681SAndroid Build Coastguard Worker   const uint64_t *getRawData() const {
575*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
576*9880d681SAndroid Build Coastguard Worker       return &VAL;
577*9880d681SAndroid Build Coastguard Worker     return &pVal[0];
578*9880d681SAndroid Build Coastguard Worker   }
579*9880d681SAndroid Build Coastguard Worker 
580*9880d681SAndroid Build Coastguard Worker   /// @}
581*9880d681SAndroid Build Coastguard Worker   /// \name Unary Operators
582*9880d681SAndroid Build Coastguard Worker   /// @{
583*9880d681SAndroid Build Coastguard Worker 
584*9880d681SAndroid Build Coastguard Worker   /// \brief Postfix increment operator.
585*9880d681SAndroid Build Coastguard Worker   ///
586*9880d681SAndroid Build Coastguard Worker   /// \returns a new APInt value representing *this incremented by one
587*9880d681SAndroid Build Coastguard Worker   const APInt operator++(int) {
588*9880d681SAndroid Build Coastguard Worker     APInt API(*this);
589*9880d681SAndroid Build Coastguard Worker     ++(*this);
590*9880d681SAndroid Build Coastguard Worker     return API;
591*9880d681SAndroid Build Coastguard Worker   }
592*9880d681SAndroid Build Coastguard Worker 
593*9880d681SAndroid Build Coastguard Worker   /// \brief Prefix increment operator.
594*9880d681SAndroid Build Coastguard Worker   ///
595*9880d681SAndroid Build Coastguard Worker   /// \returns *this incremented by one
596*9880d681SAndroid Build Coastguard Worker   APInt &operator++();
597*9880d681SAndroid Build Coastguard Worker 
598*9880d681SAndroid Build Coastguard Worker   /// \brief Postfix decrement operator.
599*9880d681SAndroid Build Coastguard Worker   ///
600*9880d681SAndroid Build Coastguard Worker   /// \returns a new APInt representing *this decremented by one.
601*9880d681SAndroid Build Coastguard Worker   const APInt operator--(int) {
602*9880d681SAndroid Build Coastguard Worker     APInt API(*this);
603*9880d681SAndroid Build Coastguard Worker     --(*this);
604*9880d681SAndroid Build Coastguard Worker     return API;
605*9880d681SAndroid Build Coastguard Worker   }
606*9880d681SAndroid Build Coastguard Worker 
607*9880d681SAndroid Build Coastguard Worker   /// \brief Prefix decrement operator.
608*9880d681SAndroid Build Coastguard Worker   ///
609*9880d681SAndroid Build Coastguard Worker   /// \returns *this decremented by one.
610*9880d681SAndroid Build Coastguard Worker   APInt &operator--();
611*9880d681SAndroid Build Coastguard Worker 
612*9880d681SAndroid Build Coastguard Worker   /// \brief Unary bitwise complement operator.
613*9880d681SAndroid Build Coastguard Worker   ///
614*9880d681SAndroid Build Coastguard Worker   /// Performs a bitwise complement operation on this APInt.
615*9880d681SAndroid Build Coastguard Worker   ///
616*9880d681SAndroid Build Coastguard Worker   /// \returns an APInt that is the bitwise complement of *this
617*9880d681SAndroid Build Coastguard Worker   APInt operator~() const {
618*9880d681SAndroid Build Coastguard Worker     APInt Result(*this);
619*9880d681SAndroid Build Coastguard Worker     Result.flipAllBits();
620*9880d681SAndroid Build Coastguard Worker     return Result;
621*9880d681SAndroid Build Coastguard Worker   }
622*9880d681SAndroid Build Coastguard Worker 
623*9880d681SAndroid Build Coastguard Worker   /// \brief Unary negation operator
624*9880d681SAndroid Build Coastguard Worker   ///
625*9880d681SAndroid Build Coastguard Worker   /// Negates *this using two's complement logic.
626*9880d681SAndroid Build Coastguard Worker   ///
627*9880d681SAndroid Build Coastguard Worker   /// \returns An APInt value representing the negation of *this.
628*9880d681SAndroid Build Coastguard Worker   APInt operator-() const {
629*9880d681SAndroid Build Coastguard Worker     APInt Result(*this);
630*9880d681SAndroid Build Coastguard Worker     Result.flipAllBits();
631*9880d681SAndroid Build Coastguard Worker     ++Result;
632*9880d681SAndroid Build Coastguard Worker     return Result;
633*9880d681SAndroid Build Coastguard Worker   }
634*9880d681SAndroid Build Coastguard Worker 
635*9880d681SAndroid Build Coastguard Worker   /// \brief Logical negation operator.
636*9880d681SAndroid Build Coastguard Worker   ///
637*9880d681SAndroid Build Coastguard Worker   /// Performs logical negation operation on this APInt.
638*9880d681SAndroid Build Coastguard Worker   ///
639*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this is zero, false otherwise.
640*9880d681SAndroid Build Coastguard Worker   bool operator!() const {
641*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
642*9880d681SAndroid Build Coastguard Worker       return !VAL;
643*9880d681SAndroid Build Coastguard Worker 
644*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0; i != getNumWords(); ++i)
645*9880d681SAndroid Build Coastguard Worker       if (pVal[i])
646*9880d681SAndroid Build Coastguard Worker         return false;
647*9880d681SAndroid Build Coastguard Worker     return true;
648*9880d681SAndroid Build Coastguard Worker   }
649*9880d681SAndroid Build Coastguard Worker 
650*9880d681SAndroid Build Coastguard Worker   /// @}
651*9880d681SAndroid Build Coastguard Worker   /// \name Assignment Operators
652*9880d681SAndroid Build Coastguard Worker   /// @{
653*9880d681SAndroid Build Coastguard Worker 
654*9880d681SAndroid Build Coastguard Worker   /// \brief Copy assignment operator.
655*9880d681SAndroid Build Coastguard Worker   ///
656*9880d681SAndroid Build Coastguard Worker   /// \returns *this after assignment of RHS.
657*9880d681SAndroid Build Coastguard Worker   APInt &operator=(const APInt &RHS) {
658*9880d681SAndroid Build Coastguard Worker     // If the bitwidths are the same, we can avoid mucking with memory
659*9880d681SAndroid Build Coastguard Worker     if (isSingleWord() && RHS.isSingleWord()) {
660*9880d681SAndroid Build Coastguard Worker       VAL = RHS.VAL;
661*9880d681SAndroid Build Coastguard Worker       BitWidth = RHS.BitWidth;
662*9880d681SAndroid Build Coastguard Worker       return clearUnusedBits();
663*9880d681SAndroid Build Coastguard Worker     }
664*9880d681SAndroid Build Coastguard Worker 
665*9880d681SAndroid Build Coastguard Worker     return AssignSlowCase(RHS);
666*9880d681SAndroid Build Coastguard Worker   }
667*9880d681SAndroid Build Coastguard Worker 
668*9880d681SAndroid Build Coastguard Worker   /// @brief Move assignment operator.
669*9880d681SAndroid Build Coastguard Worker   APInt &operator=(APInt &&that) {
670*9880d681SAndroid Build Coastguard Worker     if (!isSingleWord()) {
671*9880d681SAndroid Build Coastguard Worker       // The MSVC STL shipped in 2013 requires that self move assignment be a
672*9880d681SAndroid Build Coastguard Worker       // no-op.  Otherwise algorithms like stable_sort will produce answers
673*9880d681SAndroid Build Coastguard Worker       // where half of the output is left in a moved-from state.
674*9880d681SAndroid Build Coastguard Worker       if (this == &that)
675*9880d681SAndroid Build Coastguard Worker         return *this;
676*9880d681SAndroid Build Coastguard Worker       delete[] pVal;
677*9880d681SAndroid Build Coastguard Worker     }
678*9880d681SAndroid Build Coastguard Worker 
679*9880d681SAndroid Build Coastguard Worker     // Use memcpy so that type based alias analysis sees both VAL and pVal
680*9880d681SAndroid Build Coastguard Worker     // as modified.
681*9880d681SAndroid Build Coastguard Worker     memcpy(&VAL, &that.VAL, sizeof(uint64_t));
682*9880d681SAndroid Build Coastguard Worker 
683*9880d681SAndroid Build Coastguard Worker     // If 'this == &that', avoid zeroing our own bitwidth by storing to 'that'
684*9880d681SAndroid Build Coastguard Worker     // first.
685*9880d681SAndroid Build Coastguard Worker     unsigned ThatBitWidth = that.BitWidth;
686*9880d681SAndroid Build Coastguard Worker     that.BitWidth = 0;
687*9880d681SAndroid Build Coastguard Worker     BitWidth = ThatBitWidth;
688*9880d681SAndroid Build Coastguard Worker 
689*9880d681SAndroid Build Coastguard Worker     return *this;
690*9880d681SAndroid Build Coastguard Worker   }
691*9880d681SAndroid Build Coastguard Worker 
692*9880d681SAndroid Build Coastguard Worker   /// \brief Assignment operator.
693*9880d681SAndroid Build Coastguard Worker   ///
694*9880d681SAndroid Build Coastguard Worker   /// The RHS value is assigned to *this. If the significant bits in RHS exceed
695*9880d681SAndroid Build Coastguard Worker   /// the bit width, the excess bits are truncated. If the bit width is larger
696*9880d681SAndroid Build Coastguard Worker   /// than 64, the value is zero filled in the unspecified high order bits.
697*9880d681SAndroid Build Coastguard Worker   ///
698*9880d681SAndroid Build Coastguard Worker   /// \returns *this after assignment of RHS value.
699*9880d681SAndroid Build Coastguard Worker   APInt &operator=(uint64_t RHS);
700*9880d681SAndroid Build Coastguard Worker 
701*9880d681SAndroid Build Coastguard Worker   /// \brief Bitwise AND assignment operator.
702*9880d681SAndroid Build Coastguard Worker   ///
703*9880d681SAndroid Build Coastguard Worker   /// Performs a bitwise AND operation on this APInt and RHS. The result is
704*9880d681SAndroid Build Coastguard Worker   /// assigned to *this.
705*9880d681SAndroid Build Coastguard Worker   ///
706*9880d681SAndroid Build Coastguard Worker   /// \returns *this after ANDing with RHS.
707*9880d681SAndroid Build Coastguard Worker   APInt &operator&=(const APInt &RHS);
708*9880d681SAndroid Build Coastguard Worker 
709*9880d681SAndroid Build Coastguard Worker   /// \brief Bitwise OR assignment operator.
710*9880d681SAndroid Build Coastguard Worker   ///
711*9880d681SAndroid Build Coastguard Worker   /// Performs a bitwise OR operation on this APInt and RHS. The result is
712*9880d681SAndroid Build Coastguard Worker   /// assigned *this;
713*9880d681SAndroid Build Coastguard Worker   ///
714*9880d681SAndroid Build Coastguard Worker   /// \returns *this after ORing with RHS.
715*9880d681SAndroid Build Coastguard Worker   APInt &operator|=(const APInt &RHS);
716*9880d681SAndroid Build Coastguard Worker 
717*9880d681SAndroid Build Coastguard Worker   /// \brief Bitwise OR assignment operator.
718*9880d681SAndroid Build Coastguard Worker   ///
719*9880d681SAndroid Build Coastguard Worker   /// Performs a bitwise OR operation on this APInt and RHS. RHS is
720*9880d681SAndroid Build Coastguard Worker   /// logically zero-extended or truncated to match the bit-width of
721*9880d681SAndroid Build Coastguard Worker   /// the LHS.
722*9880d681SAndroid Build Coastguard Worker   APInt &operator|=(uint64_t RHS) {
723*9880d681SAndroid Build Coastguard Worker     if (isSingleWord()) {
724*9880d681SAndroid Build Coastguard Worker       VAL |= RHS;
725*9880d681SAndroid Build Coastguard Worker       clearUnusedBits();
726*9880d681SAndroid Build Coastguard Worker     } else {
727*9880d681SAndroid Build Coastguard Worker       pVal[0] |= RHS;
728*9880d681SAndroid Build Coastguard Worker     }
729*9880d681SAndroid Build Coastguard Worker     return *this;
730*9880d681SAndroid Build Coastguard Worker   }
731*9880d681SAndroid Build Coastguard Worker 
732*9880d681SAndroid Build Coastguard Worker   /// \brief Bitwise XOR assignment operator.
733*9880d681SAndroid Build Coastguard Worker   ///
734*9880d681SAndroid Build Coastguard Worker   /// Performs a bitwise XOR operation on this APInt and RHS. The result is
735*9880d681SAndroid Build Coastguard Worker   /// assigned to *this.
736*9880d681SAndroid Build Coastguard Worker   ///
737*9880d681SAndroid Build Coastguard Worker   /// \returns *this after XORing with RHS.
738*9880d681SAndroid Build Coastguard Worker   APInt &operator^=(const APInt &RHS);
739*9880d681SAndroid Build Coastguard Worker 
740*9880d681SAndroid Build Coastguard Worker   /// \brief Multiplication assignment operator.
741*9880d681SAndroid Build Coastguard Worker   ///
742*9880d681SAndroid Build Coastguard Worker   /// Multiplies this APInt by RHS and assigns the result to *this.
743*9880d681SAndroid Build Coastguard Worker   ///
744*9880d681SAndroid Build Coastguard Worker   /// \returns *this
745*9880d681SAndroid Build Coastguard Worker   APInt &operator*=(const APInt &RHS);
746*9880d681SAndroid Build Coastguard Worker 
747*9880d681SAndroid Build Coastguard Worker   /// \brief Addition assignment operator.
748*9880d681SAndroid Build Coastguard Worker   ///
749*9880d681SAndroid Build Coastguard Worker   /// Adds RHS to *this and assigns the result to *this.
750*9880d681SAndroid Build Coastguard Worker   ///
751*9880d681SAndroid Build Coastguard Worker   /// \returns *this
752*9880d681SAndroid Build Coastguard Worker   APInt &operator+=(const APInt &RHS);
753*9880d681SAndroid Build Coastguard Worker 
754*9880d681SAndroid Build Coastguard Worker   /// \brief Subtraction assignment operator.
755*9880d681SAndroid Build Coastguard Worker   ///
756*9880d681SAndroid Build Coastguard Worker   /// Subtracts RHS from *this and assigns the result to *this.
757*9880d681SAndroid Build Coastguard Worker   ///
758*9880d681SAndroid Build Coastguard Worker   /// \returns *this
759*9880d681SAndroid Build Coastguard Worker   APInt &operator-=(const APInt &RHS);
760*9880d681SAndroid Build Coastguard Worker 
761*9880d681SAndroid Build Coastguard Worker   /// \brief Left-shift assignment function.
762*9880d681SAndroid Build Coastguard Worker   ///
763*9880d681SAndroid Build Coastguard Worker   /// Shifts *this left by shiftAmt and assigns the result to *this.
764*9880d681SAndroid Build Coastguard Worker   ///
765*9880d681SAndroid Build Coastguard Worker   /// \returns *this after shifting left by shiftAmt
766*9880d681SAndroid Build Coastguard Worker   APInt &operator<<=(unsigned shiftAmt) {
767*9880d681SAndroid Build Coastguard Worker     *this = shl(shiftAmt);
768*9880d681SAndroid Build Coastguard Worker     return *this;
769*9880d681SAndroid Build Coastguard Worker   }
770*9880d681SAndroid Build Coastguard Worker 
771*9880d681SAndroid Build Coastguard Worker   /// @}
772*9880d681SAndroid Build Coastguard Worker   /// \name Binary Operators
773*9880d681SAndroid Build Coastguard Worker   /// @{
774*9880d681SAndroid Build Coastguard Worker 
775*9880d681SAndroid Build Coastguard Worker   /// \brief Bitwise AND operator.
776*9880d681SAndroid Build Coastguard Worker   ///
777*9880d681SAndroid Build Coastguard Worker   /// Performs a bitwise AND operation on *this and RHS.
778*9880d681SAndroid Build Coastguard Worker   ///
779*9880d681SAndroid Build Coastguard Worker   /// \returns An APInt value representing the bitwise AND of *this and RHS.
780*9880d681SAndroid Build Coastguard Worker   APInt operator&(const APInt &RHS) const {
781*9880d681SAndroid Build Coastguard Worker     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
782*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
783*9880d681SAndroid Build Coastguard Worker       return APInt(getBitWidth(), VAL & RHS.VAL);
784*9880d681SAndroid Build Coastguard Worker     return AndSlowCase(RHS);
785*9880d681SAndroid Build Coastguard Worker   }
And(const APInt & RHS)786*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT And(const APInt &RHS) const {
787*9880d681SAndroid Build Coastguard Worker     return this->operator&(RHS);
788*9880d681SAndroid Build Coastguard Worker   }
789*9880d681SAndroid Build Coastguard Worker 
790*9880d681SAndroid Build Coastguard Worker   /// \brief Bitwise OR operator.
791*9880d681SAndroid Build Coastguard Worker   ///
792*9880d681SAndroid Build Coastguard Worker   /// Performs a bitwise OR operation on *this and RHS.
793*9880d681SAndroid Build Coastguard Worker   ///
794*9880d681SAndroid Build Coastguard Worker   /// \returns An APInt value representing the bitwise OR of *this and RHS.
795*9880d681SAndroid Build Coastguard Worker   APInt operator|(const APInt &RHS) const {
796*9880d681SAndroid Build Coastguard Worker     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
797*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
798*9880d681SAndroid Build Coastguard Worker       return APInt(getBitWidth(), VAL | RHS.VAL);
799*9880d681SAndroid Build Coastguard Worker     return OrSlowCase(RHS);
800*9880d681SAndroid Build Coastguard Worker   }
801*9880d681SAndroid Build Coastguard Worker 
802*9880d681SAndroid Build Coastguard Worker   /// \brief Bitwise OR function.
803*9880d681SAndroid Build Coastguard Worker   ///
804*9880d681SAndroid Build Coastguard Worker   /// Performs a bitwise or on *this and RHS. This is implemented by simply
805*9880d681SAndroid Build Coastguard Worker   /// calling operator|.
806*9880d681SAndroid Build Coastguard Worker   ///
807*9880d681SAndroid Build Coastguard Worker   /// \returns An APInt value representing the bitwise OR of *this and RHS.
Or(const APInt & RHS)808*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT Or(const APInt &RHS) const {
809*9880d681SAndroid Build Coastguard Worker     return this->operator|(RHS);
810*9880d681SAndroid Build Coastguard Worker   }
811*9880d681SAndroid Build Coastguard Worker 
812*9880d681SAndroid Build Coastguard Worker   /// \brief Bitwise XOR operator.
813*9880d681SAndroid Build Coastguard Worker   ///
814*9880d681SAndroid Build Coastguard Worker   /// Performs a bitwise XOR operation on *this and RHS.
815*9880d681SAndroid Build Coastguard Worker   ///
816*9880d681SAndroid Build Coastguard Worker   /// \returns An APInt value representing the bitwise XOR of *this and RHS.
817*9880d681SAndroid Build Coastguard Worker   APInt operator^(const APInt &RHS) const {
818*9880d681SAndroid Build Coastguard Worker     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
819*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
820*9880d681SAndroid Build Coastguard Worker       return APInt(BitWidth, VAL ^ RHS.VAL);
821*9880d681SAndroid Build Coastguard Worker     return XorSlowCase(RHS);
822*9880d681SAndroid Build Coastguard Worker   }
823*9880d681SAndroid Build Coastguard Worker 
824*9880d681SAndroid Build Coastguard Worker   /// \brief Bitwise XOR function.
825*9880d681SAndroid Build Coastguard Worker   ///
826*9880d681SAndroid Build Coastguard Worker   /// Performs a bitwise XOR operation on *this and RHS. This is implemented
827*9880d681SAndroid Build Coastguard Worker   /// through the usage of operator^.
828*9880d681SAndroid Build Coastguard Worker   ///
829*9880d681SAndroid Build Coastguard Worker   /// \returns An APInt value representing the bitwise XOR of *this and RHS.
Xor(const APInt & RHS)830*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT Xor(const APInt &RHS) const {
831*9880d681SAndroid Build Coastguard Worker     return this->operator^(RHS);
832*9880d681SAndroid Build Coastguard Worker   }
833*9880d681SAndroid Build Coastguard Worker 
834*9880d681SAndroid Build Coastguard Worker   /// \brief Multiplication operator.
835*9880d681SAndroid Build Coastguard Worker   ///
836*9880d681SAndroid Build Coastguard Worker   /// Multiplies this APInt by RHS and returns the result.
837*9880d681SAndroid Build Coastguard Worker   APInt operator*(const APInt &RHS) const;
838*9880d681SAndroid Build Coastguard Worker 
839*9880d681SAndroid Build Coastguard Worker   /// \brief Addition operator.
840*9880d681SAndroid Build Coastguard Worker   ///
841*9880d681SAndroid Build Coastguard Worker   /// Adds RHS to this APInt and returns the result.
842*9880d681SAndroid Build Coastguard Worker   APInt operator+(const APInt &RHS) const;
843*9880d681SAndroid Build Coastguard Worker   APInt operator+(uint64_t RHS) const;
844*9880d681SAndroid Build Coastguard Worker 
845*9880d681SAndroid Build Coastguard Worker   /// \brief Subtraction operator.
846*9880d681SAndroid Build Coastguard Worker   ///
847*9880d681SAndroid Build Coastguard Worker   /// Subtracts RHS from this APInt and returns the result.
848*9880d681SAndroid Build Coastguard Worker   APInt operator-(const APInt &RHS) const;
849*9880d681SAndroid Build Coastguard Worker   APInt operator-(uint64_t RHS) const;
850*9880d681SAndroid Build Coastguard Worker 
851*9880d681SAndroid Build Coastguard Worker   /// \brief Left logical shift operator.
852*9880d681SAndroid Build Coastguard Worker   ///
853*9880d681SAndroid Build Coastguard Worker   /// Shifts this APInt left by \p Bits and returns the result.
854*9880d681SAndroid Build Coastguard Worker   APInt operator<<(unsigned Bits) const { return shl(Bits); }
855*9880d681SAndroid Build Coastguard Worker 
856*9880d681SAndroid Build Coastguard Worker   /// \brief Left logical shift operator.
857*9880d681SAndroid Build Coastguard Worker   ///
858*9880d681SAndroid Build Coastguard Worker   /// Shifts this APInt left by \p Bits and returns the result.
859*9880d681SAndroid Build Coastguard Worker   APInt operator<<(const APInt &Bits) const { return shl(Bits); }
860*9880d681SAndroid Build Coastguard Worker 
861*9880d681SAndroid Build Coastguard Worker   /// \brief Arithmetic right-shift function.
862*9880d681SAndroid Build Coastguard Worker   ///
863*9880d681SAndroid Build Coastguard Worker   /// Arithmetic right-shift this APInt by shiftAmt.
864*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT ashr(unsigned shiftAmt) const;
865*9880d681SAndroid Build Coastguard Worker 
866*9880d681SAndroid Build Coastguard Worker   /// \brief Logical right-shift function.
867*9880d681SAndroid Build Coastguard Worker   ///
868*9880d681SAndroid Build Coastguard Worker   /// Logical right-shift this APInt by shiftAmt.
869*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const;
870*9880d681SAndroid Build Coastguard Worker 
871*9880d681SAndroid Build Coastguard Worker   /// \brief Left-shift function.
872*9880d681SAndroid Build Coastguard Worker   ///
873*9880d681SAndroid Build Coastguard Worker   /// Left-shift this APInt by shiftAmt.
shl(unsigned shiftAmt)874*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT shl(unsigned shiftAmt) const {
875*9880d681SAndroid Build Coastguard Worker     assert(shiftAmt <= BitWidth && "Invalid shift amount");
876*9880d681SAndroid Build Coastguard Worker     if (isSingleWord()) {
877*9880d681SAndroid Build Coastguard Worker       if (shiftAmt >= BitWidth)
878*9880d681SAndroid Build Coastguard Worker         return APInt(BitWidth, 0); // avoid undefined shift results
879*9880d681SAndroid Build Coastguard Worker       return APInt(BitWidth, VAL << shiftAmt);
880*9880d681SAndroid Build Coastguard Worker     }
881*9880d681SAndroid Build Coastguard Worker     return shlSlowCase(shiftAmt);
882*9880d681SAndroid Build Coastguard Worker   }
883*9880d681SAndroid Build Coastguard Worker 
884*9880d681SAndroid Build Coastguard Worker   /// \brief Rotate left by rotateAmt.
885*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotl(unsigned rotateAmt) const;
886*9880d681SAndroid Build Coastguard Worker 
887*9880d681SAndroid Build Coastguard Worker   /// \brief Rotate right by rotateAmt.
888*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotr(unsigned rotateAmt) const;
889*9880d681SAndroid Build Coastguard Worker 
890*9880d681SAndroid Build Coastguard Worker   /// \brief Arithmetic right-shift function.
891*9880d681SAndroid Build Coastguard Worker   ///
892*9880d681SAndroid Build Coastguard Worker   /// Arithmetic right-shift this APInt by shiftAmt.
893*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT ashr(const APInt &shiftAmt) const;
894*9880d681SAndroid Build Coastguard Worker 
895*9880d681SAndroid Build Coastguard Worker   /// \brief Logical right-shift function.
896*9880d681SAndroid Build Coastguard Worker   ///
897*9880d681SAndroid Build Coastguard Worker   /// Logical right-shift this APInt by shiftAmt.
898*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(const APInt &shiftAmt) const;
899*9880d681SAndroid Build Coastguard Worker 
900*9880d681SAndroid Build Coastguard Worker   /// \brief Left-shift function.
901*9880d681SAndroid Build Coastguard Worker   ///
902*9880d681SAndroid Build Coastguard Worker   /// Left-shift this APInt by shiftAmt.
903*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT shl(const APInt &shiftAmt) const;
904*9880d681SAndroid Build Coastguard Worker 
905*9880d681SAndroid Build Coastguard Worker   /// \brief Rotate left by rotateAmt.
906*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotl(const APInt &rotateAmt) const;
907*9880d681SAndroid Build Coastguard Worker 
908*9880d681SAndroid Build Coastguard Worker   /// \brief Rotate right by rotateAmt.
909*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotr(const APInt &rotateAmt) const;
910*9880d681SAndroid Build Coastguard Worker 
911*9880d681SAndroid Build Coastguard Worker   /// \brief Unsigned division operation.
912*9880d681SAndroid Build Coastguard Worker   ///
913*9880d681SAndroid Build Coastguard Worker   /// Perform an unsigned divide operation on this APInt by RHS. Both this and
914*9880d681SAndroid Build Coastguard Worker   /// RHS are treated as unsigned quantities for purposes of this division.
915*9880d681SAndroid Build Coastguard Worker   ///
916*9880d681SAndroid Build Coastguard Worker   /// \returns a new APInt value containing the division result
917*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT udiv(const APInt &RHS) const;
918*9880d681SAndroid Build Coastguard Worker 
919*9880d681SAndroid Build Coastguard Worker   /// \brief Signed division function for APInt.
920*9880d681SAndroid Build Coastguard Worker   ///
921*9880d681SAndroid Build Coastguard Worker   /// Signed divide this APInt by APInt RHS.
922*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sdiv(const APInt &RHS) const;
923*9880d681SAndroid Build Coastguard Worker 
924*9880d681SAndroid Build Coastguard Worker   /// \brief Unsigned remainder operation.
925*9880d681SAndroid Build Coastguard Worker   ///
926*9880d681SAndroid Build Coastguard Worker   /// Perform an unsigned remainder operation on this APInt with RHS being the
927*9880d681SAndroid Build Coastguard Worker   /// divisor. Both this and RHS are treated as unsigned quantities for purposes
928*9880d681SAndroid Build Coastguard Worker   /// of this operation. Note that this is a true remainder operation and not a
929*9880d681SAndroid Build Coastguard Worker   /// modulo operation because the sign follows the sign of the dividend which
930*9880d681SAndroid Build Coastguard Worker   /// is *this.
931*9880d681SAndroid Build Coastguard Worker   ///
932*9880d681SAndroid Build Coastguard Worker   /// \returns a new APInt value containing the remainder result
933*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT urem(const APInt &RHS) const;
934*9880d681SAndroid Build Coastguard Worker 
935*9880d681SAndroid Build Coastguard Worker   /// \brief Function for signed remainder operation.
936*9880d681SAndroid Build Coastguard Worker   ///
937*9880d681SAndroid Build Coastguard Worker   /// Signed remainder operation on APInt.
938*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT srem(const APInt &RHS) const;
939*9880d681SAndroid Build Coastguard Worker 
940*9880d681SAndroid Build Coastguard Worker   /// \brief Dual division/remainder interface.
941*9880d681SAndroid Build Coastguard Worker   ///
942*9880d681SAndroid Build Coastguard Worker   /// Sometimes it is convenient to divide two APInt values and obtain both the
943*9880d681SAndroid Build Coastguard Worker   /// quotient and remainder. This function does both operations in the same
944*9880d681SAndroid Build Coastguard Worker   /// computation making it a little more efficient. The pair of input arguments
945*9880d681SAndroid Build Coastguard Worker   /// may overlap with the pair of output arguments. It is safe to call
946*9880d681SAndroid Build Coastguard Worker   /// udivrem(X, Y, X, Y), for example.
947*9880d681SAndroid Build Coastguard Worker   static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
948*9880d681SAndroid Build Coastguard Worker                       APInt &Remainder);
949*9880d681SAndroid Build Coastguard Worker 
950*9880d681SAndroid Build Coastguard Worker   static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
951*9880d681SAndroid Build Coastguard Worker                       APInt &Remainder);
952*9880d681SAndroid Build Coastguard Worker 
953*9880d681SAndroid Build Coastguard Worker   // Operations that return overflow indicators.
954*9880d681SAndroid Build Coastguard Worker   APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
955*9880d681SAndroid Build Coastguard Worker   APInt uadd_ov(const APInt &RHS, bool &Overflow) const;
956*9880d681SAndroid Build Coastguard Worker   APInt ssub_ov(const APInt &RHS, bool &Overflow) const;
957*9880d681SAndroid Build Coastguard Worker   APInt usub_ov(const APInt &RHS, bool &Overflow) const;
958*9880d681SAndroid Build Coastguard Worker   APInt sdiv_ov(const APInt &RHS, bool &Overflow) const;
959*9880d681SAndroid Build Coastguard Worker   APInt smul_ov(const APInt &RHS, bool &Overflow) const;
960*9880d681SAndroid Build Coastguard Worker   APInt umul_ov(const APInt &RHS, bool &Overflow) const;
961*9880d681SAndroid Build Coastguard Worker   APInt sshl_ov(const APInt &Amt, bool &Overflow) const;
962*9880d681SAndroid Build Coastguard Worker   APInt ushl_ov(const APInt &Amt, bool &Overflow) const;
963*9880d681SAndroid Build Coastguard Worker 
964*9880d681SAndroid Build Coastguard Worker   /// \brief Array-indexing support.
965*9880d681SAndroid Build Coastguard Worker   ///
966*9880d681SAndroid Build Coastguard Worker   /// \returns the bit value at bitPosition
967*9880d681SAndroid Build Coastguard Worker   bool operator[](unsigned bitPosition) const {
968*9880d681SAndroid Build Coastguard Worker     assert(bitPosition < getBitWidth() && "Bit position out of bounds!");
969*9880d681SAndroid Build Coastguard Worker     return (maskBit(bitPosition) &
970*9880d681SAndroid Build Coastguard Worker             (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) !=
971*9880d681SAndroid Build Coastguard Worker            0;
972*9880d681SAndroid Build Coastguard Worker   }
973*9880d681SAndroid Build Coastguard Worker 
974*9880d681SAndroid Build Coastguard Worker   /// @}
975*9880d681SAndroid Build Coastguard Worker   /// \name Comparison Operators
976*9880d681SAndroid Build Coastguard Worker   /// @{
977*9880d681SAndroid Build Coastguard Worker 
978*9880d681SAndroid Build Coastguard Worker   /// \brief Equality operator.
979*9880d681SAndroid Build Coastguard Worker   ///
980*9880d681SAndroid Build Coastguard Worker   /// Compares this APInt with RHS for the validity of the equality
981*9880d681SAndroid Build Coastguard Worker   /// relationship.
982*9880d681SAndroid Build Coastguard Worker   bool operator==(const APInt &RHS) const {
983*9880d681SAndroid Build Coastguard Worker     assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
984*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
985*9880d681SAndroid Build Coastguard Worker       return VAL == RHS.VAL;
986*9880d681SAndroid Build Coastguard Worker     return EqualSlowCase(RHS);
987*9880d681SAndroid Build Coastguard Worker   }
988*9880d681SAndroid Build Coastguard Worker 
989*9880d681SAndroid Build Coastguard Worker   /// \brief Equality operator.
990*9880d681SAndroid Build Coastguard Worker   ///
991*9880d681SAndroid Build Coastguard Worker   /// Compares this APInt with a uint64_t for the validity of the equality
992*9880d681SAndroid Build Coastguard Worker   /// relationship.
993*9880d681SAndroid Build Coastguard Worker   ///
994*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this == Val
995*9880d681SAndroid Build Coastguard Worker   bool operator==(uint64_t Val) const {
996*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
997*9880d681SAndroid Build Coastguard Worker       return VAL == Val;
998*9880d681SAndroid Build Coastguard Worker     return EqualSlowCase(Val);
999*9880d681SAndroid Build Coastguard Worker   }
1000*9880d681SAndroid Build Coastguard Worker 
1001*9880d681SAndroid Build Coastguard Worker   /// \brief Equality comparison.
1002*9880d681SAndroid Build Coastguard Worker   ///
1003*9880d681SAndroid Build Coastguard Worker   /// Compares this APInt with RHS for the validity of the equality
1004*9880d681SAndroid Build Coastguard Worker   /// relationship.
1005*9880d681SAndroid Build Coastguard Worker   ///
1006*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this == Val
eq(const APInt & RHS)1007*9880d681SAndroid Build Coastguard Worker   bool eq(const APInt &RHS) const { return (*this) == RHS; }
1008*9880d681SAndroid Build Coastguard Worker 
1009*9880d681SAndroid Build Coastguard Worker   /// \brief Inequality operator.
1010*9880d681SAndroid Build Coastguard Worker   ///
1011*9880d681SAndroid Build Coastguard Worker   /// Compares this APInt with RHS for the validity of the inequality
1012*9880d681SAndroid Build Coastguard Worker   /// relationship.
1013*9880d681SAndroid Build Coastguard Worker   ///
1014*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this != Val
1015*9880d681SAndroid Build Coastguard Worker   bool operator!=(const APInt &RHS) const { return !((*this) == RHS); }
1016*9880d681SAndroid Build Coastguard Worker 
1017*9880d681SAndroid Build Coastguard Worker   /// \brief Inequality operator.
1018*9880d681SAndroid Build Coastguard Worker   ///
1019*9880d681SAndroid Build Coastguard Worker   /// Compares this APInt with a uint64_t for the validity of the inequality
1020*9880d681SAndroid Build Coastguard Worker   /// relationship.
1021*9880d681SAndroid Build Coastguard Worker   ///
1022*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this != Val
1023*9880d681SAndroid Build Coastguard Worker   bool operator!=(uint64_t Val) const { return !((*this) == Val); }
1024*9880d681SAndroid Build Coastguard Worker 
1025*9880d681SAndroid Build Coastguard Worker   /// \brief Inequality comparison
1026*9880d681SAndroid Build Coastguard Worker   ///
1027*9880d681SAndroid Build Coastguard Worker   /// Compares this APInt with RHS for the validity of the inequality
1028*9880d681SAndroid Build Coastguard Worker   /// relationship.
1029*9880d681SAndroid Build Coastguard Worker   ///
1030*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this != Val
ne(const APInt & RHS)1031*9880d681SAndroid Build Coastguard Worker   bool ne(const APInt &RHS) const { return !((*this) == RHS); }
1032*9880d681SAndroid Build Coastguard Worker 
1033*9880d681SAndroid Build Coastguard Worker   /// \brief Unsigned less than comparison
1034*9880d681SAndroid Build Coastguard Worker   ///
1035*9880d681SAndroid Build Coastguard Worker   /// Regards both *this and RHS as unsigned quantities and compares them for
1036*9880d681SAndroid Build Coastguard Worker   /// the validity of the less-than relationship.
1037*9880d681SAndroid Build Coastguard Worker   ///
1038*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this < RHS when both are considered unsigned.
1039*9880d681SAndroid Build Coastguard Worker   bool ult(const APInt &RHS) const;
1040*9880d681SAndroid Build Coastguard Worker 
1041*9880d681SAndroid Build Coastguard Worker   /// \brief Unsigned less than comparison
1042*9880d681SAndroid Build Coastguard Worker   ///
1043*9880d681SAndroid Build Coastguard Worker   /// Regards both *this as an unsigned quantity and compares it with RHS for
1044*9880d681SAndroid Build Coastguard Worker   /// the validity of the less-than relationship.
1045*9880d681SAndroid Build Coastguard Worker   ///
1046*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this < RHS when considered unsigned.
ult(uint64_t RHS)1047*9880d681SAndroid Build Coastguard Worker   bool ult(uint64_t RHS) const {
1048*9880d681SAndroid Build Coastguard Worker     return getActiveBits() > 64 ? false : getZExtValue() < RHS;
1049*9880d681SAndroid Build Coastguard Worker   }
1050*9880d681SAndroid Build Coastguard Worker 
1051*9880d681SAndroid Build Coastguard Worker   /// \brief Signed less than comparison
1052*9880d681SAndroid Build Coastguard Worker   ///
1053*9880d681SAndroid Build Coastguard Worker   /// Regards both *this and RHS as signed quantities and compares them for
1054*9880d681SAndroid Build Coastguard Worker   /// validity of the less-than relationship.
1055*9880d681SAndroid Build Coastguard Worker   ///
1056*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this < RHS when both are considered signed.
1057*9880d681SAndroid Build Coastguard Worker   bool slt(const APInt &RHS) const;
1058*9880d681SAndroid Build Coastguard Worker 
1059*9880d681SAndroid Build Coastguard Worker   /// \brief Signed less than comparison
1060*9880d681SAndroid Build Coastguard Worker   ///
1061*9880d681SAndroid Build Coastguard Worker   /// Regards both *this as a signed quantity and compares it with RHS for
1062*9880d681SAndroid Build Coastguard Worker   /// the validity of the less-than relationship.
1063*9880d681SAndroid Build Coastguard Worker   ///
1064*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this < RHS when considered signed.
slt(int64_t RHS)1065*9880d681SAndroid Build Coastguard Worker   bool slt(int64_t RHS) const {
1066*9880d681SAndroid Build Coastguard Worker     return getMinSignedBits() > 64 ? isNegative() : getSExtValue() < RHS;
1067*9880d681SAndroid Build Coastguard Worker   }
1068*9880d681SAndroid Build Coastguard Worker 
1069*9880d681SAndroid Build Coastguard Worker   /// \brief Unsigned less or equal comparison
1070*9880d681SAndroid Build Coastguard Worker   ///
1071*9880d681SAndroid Build Coastguard Worker   /// Regards both *this and RHS as unsigned quantities and compares them for
1072*9880d681SAndroid Build Coastguard Worker   /// validity of the less-or-equal relationship.
1073*9880d681SAndroid Build Coastguard Worker   ///
1074*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this <= RHS when both are considered unsigned.
ule(const APInt & RHS)1075*9880d681SAndroid Build Coastguard Worker   bool ule(const APInt &RHS) const { return ult(RHS) || eq(RHS); }
1076*9880d681SAndroid Build Coastguard Worker 
1077*9880d681SAndroid Build Coastguard Worker   /// \brief Unsigned less or equal comparison
1078*9880d681SAndroid Build Coastguard Worker   ///
1079*9880d681SAndroid Build Coastguard Worker   /// Regards both *this as an unsigned quantity and compares it with RHS for
1080*9880d681SAndroid Build Coastguard Worker   /// the validity of the less-or-equal relationship.
1081*9880d681SAndroid Build Coastguard Worker   ///
1082*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this <= RHS when considered unsigned.
ule(uint64_t RHS)1083*9880d681SAndroid Build Coastguard Worker   bool ule(uint64_t RHS) const { return !ugt(RHS); }
1084*9880d681SAndroid Build Coastguard Worker 
1085*9880d681SAndroid Build Coastguard Worker   /// \brief Signed less or equal comparison
1086*9880d681SAndroid Build Coastguard Worker   ///
1087*9880d681SAndroid Build Coastguard Worker   /// Regards both *this and RHS as signed quantities and compares them for
1088*9880d681SAndroid Build Coastguard Worker   /// validity of the less-or-equal relationship.
1089*9880d681SAndroid Build Coastguard Worker   ///
1090*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this <= RHS when both are considered signed.
sle(const APInt & RHS)1091*9880d681SAndroid Build Coastguard Worker   bool sle(const APInt &RHS) const { return slt(RHS) || eq(RHS); }
1092*9880d681SAndroid Build Coastguard Worker 
1093*9880d681SAndroid Build Coastguard Worker   /// \brief Signed less or equal comparison
1094*9880d681SAndroid Build Coastguard Worker   ///
1095*9880d681SAndroid Build Coastguard Worker   /// Regards both *this as a signed quantity and compares it with RHS for the
1096*9880d681SAndroid Build Coastguard Worker   /// validity of the less-or-equal relationship.
1097*9880d681SAndroid Build Coastguard Worker   ///
1098*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this <= RHS when considered signed.
sle(uint64_t RHS)1099*9880d681SAndroid Build Coastguard Worker   bool sle(uint64_t RHS) const { return !sgt(RHS); }
1100*9880d681SAndroid Build Coastguard Worker 
1101*9880d681SAndroid Build Coastguard Worker   /// \brief Unsigned greather than comparison
1102*9880d681SAndroid Build Coastguard Worker   ///
1103*9880d681SAndroid Build Coastguard Worker   /// Regards both *this and RHS as unsigned quantities and compares them for
1104*9880d681SAndroid Build Coastguard Worker   /// the validity of the greater-than relationship.
1105*9880d681SAndroid Build Coastguard Worker   ///
1106*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this > RHS when both are considered unsigned.
ugt(const APInt & RHS)1107*9880d681SAndroid Build Coastguard Worker   bool ugt(const APInt &RHS) const { return !ult(RHS) && !eq(RHS); }
1108*9880d681SAndroid Build Coastguard Worker 
1109*9880d681SAndroid Build Coastguard Worker   /// \brief Unsigned greater than comparison
1110*9880d681SAndroid Build Coastguard Worker   ///
1111*9880d681SAndroid Build Coastguard Worker   /// Regards both *this as an unsigned quantity and compares it with RHS for
1112*9880d681SAndroid Build Coastguard Worker   /// the validity of the greater-than relationship.
1113*9880d681SAndroid Build Coastguard Worker   ///
1114*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this > RHS when considered unsigned.
ugt(uint64_t RHS)1115*9880d681SAndroid Build Coastguard Worker   bool ugt(uint64_t RHS) const {
1116*9880d681SAndroid Build Coastguard Worker     return getActiveBits() > 64 ? true : getZExtValue() > RHS;
1117*9880d681SAndroid Build Coastguard Worker   }
1118*9880d681SAndroid Build Coastguard Worker 
1119*9880d681SAndroid Build Coastguard Worker   /// \brief Signed greather than comparison
1120*9880d681SAndroid Build Coastguard Worker   ///
1121*9880d681SAndroid Build Coastguard Worker   /// Regards both *this and RHS as signed quantities and compares them for the
1122*9880d681SAndroid Build Coastguard Worker   /// validity of the greater-than relationship.
1123*9880d681SAndroid Build Coastguard Worker   ///
1124*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this > RHS when both are considered signed.
sgt(const APInt & RHS)1125*9880d681SAndroid Build Coastguard Worker   bool sgt(const APInt &RHS) const { return !slt(RHS) && !eq(RHS); }
1126*9880d681SAndroid Build Coastguard Worker 
1127*9880d681SAndroid Build Coastguard Worker   /// \brief Signed greater than comparison
1128*9880d681SAndroid Build Coastguard Worker   ///
1129*9880d681SAndroid Build Coastguard Worker   /// Regards both *this as a signed quantity and compares it with RHS for
1130*9880d681SAndroid Build Coastguard Worker   /// the validity of the greater-than relationship.
1131*9880d681SAndroid Build Coastguard Worker   ///
1132*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this > RHS when considered signed.
sgt(int64_t RHS)1133*9880d681SAndroid Build Coastguard Worker   bool sgt(int64_t RHS) const {
1134*9880d681SAndroid Build Coastguard Worker     return getMinSignedBits() > 64 ? !isNegative() : getSExtValue() > RHS;
1135*9880d681SAndroid Build Coastguard Worker   }
1136*9880d681SAndroid Build Coastguard Worker 
1137*9880d681SAndroid Build Coastguard Worker   /// \brief Unsigned greater or equal comparison
1138*9880d681SAndroid Build Coastguard Worker   ///
1139*9880d681SAndroid Build Coastguard Worker   /// Regards both *this and RHS as unsigned quantities and compares them for
1140*9880d681SAndroid Build Coastguard Worker   /// validity of the greater-or-equal relationship.
1141*9880d681SAndroid Build Coastguard Worker   ///
1142*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this >= RHS when both are considered unsigned.
uge(const APInt & RHS)1143*9880d681SAndroid Build Coastguard Worker   bool uge(const APInt &RHS) const { return !ult(RHS); }
1144*9880d681SAndroid Build Coastguard Worker 
1145*9880d681SAndroid Build Coastguard Worker   /// \brief Unsigned greater or equal comparison
1146*9880d681SAndroid Build Coastguard Worker   ///
1147*9880d681SAndroid Build Coastguard Worker   /// Regards both *this as an unsigned quantity and compares it with RHS for
1148*9880d681SAndroid Build Coastguard Worker   /// the validity of the greater-or-equal relationship.
1149*9880d681SAndroid Build Coastguard Worker   ///
1150*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this >= RHS when considered unsigned.
uge(uint64_t RHS)1151*9880d681SAndroid Build Coastguard Worker   bool uge(uint64_t RHS) const { return !ult(RHS); }
1152*9880d681SAndroid Build Coastguard Worker 
1153*9880d681SAndroid Build Coastguard Worker   /// \brief Signed greather or equal comparison
1154*9880d681SAndroid Build Coastguard Worker   ///
1155*9880d681SAndroid Build Coastguard Worker   /// Regards both *this and RHS as signed quantities and compares them for
1156*9880d681SAndroid Build Coastguard Worker   /// validity of the greater-or-equal relationship.
1157*9880d681SAndroid Build Coastguard Worker   ///
1158*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this >= RHS when both are considered signed.
sge(const APInt & RHS)1159*9880d681SAndroid Build Coastguard Worker   bool sge(const APInt &RHS) const { return !slt(RHS); }
1160*9880d681SAndroid Build Coastguard Worker 
1161*9880d681SAndroid Build Coastguard Worker   /// \brief Signed greater or equal comparison
1162*9880d681SAndroid Build Coastguard Worker   ///
1163*9880d681SAndroid Build Coastguard Worker   /// Regards both *this as a signed quantity and compares it with RHS for
1164*9880d681SAndroid Build Coastguard Worker   /// the validity of the greater-or-equal relationship.
1165*9880d681SAndroid Build Coastguard Worker   ///
1166*9880d681SAndroid Build Coastguard Worker   /// \returns true if *this >= RHS when considered signed.
sge(int64_t RHS)1167*9880d681SAndroid Build Coastguard Worker   bool sge(int64_t RHS) const { return !slt(RHS); }
1168*9880d681SAndroid Build Coastguard Worker 
1169*9880d681SAndroid Build Coastguard Worker   /// This operation tests if there are any pairs of corresponding bits
1170*9880d681SAndroid Build Coastguard Worker   /// between this APInt and RHS that are both set.
intersects(const APInt & RHS)1171*9880d681SAndroid Build Coastguard Worker   bool intersects(const APInt &RHS) const { return (*this & RHS) != 0; }
1172*9880d681SAndroid Build Coastguard Worker 
1173*9880d681SAndroid Build Coastguard Worker   /// @}
1174*9880d681SAndroid Build Coastguard Worker   /// \name Resizing Operators
1175*9880d681SAndroid Build Coastguard Worker   /// @{
1176*9880d681SAndroid Build Coastguard Worker 
1177*9880d681SAndroid Build Coastguard Worker   /// \brief Truncate to new width.
1178*9880d681SAndroid Build Coastguard Worker   ///
1179*9880d681SAndroid Build Coastguard Worker   /// Truncate the APInt to a specified width. It is an error to specify a width
1180*9880d681SAndroid Build Coastguard Worker   /// that is greater than or equal to the current width.
1181*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT trunc(unsigned width) const;
1182*9880d681SAndroid Build Coastguard Worker 
1183*9880d681SAndroid Build Coastguard Worker   /// \brief Sign extend to a new width.
1184*9880d681SAndroid Build Coastguard Worker   ///
1185*9880d681SAndroid Build Coastguard Worker   /// This operation sign extends the APInt to a new width. If the high order
1186*9880d681SAndroid Build Coastguard Worker   /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
1187*9880d681SAndroid Build Coastguard Worker   /// It is an error to specify a width that is less than or equal to the
1188*9880d681SAndroid Build Coastguard Worker   /// current width.
1189*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sext(unsigned width) const;
1190*9880d681SAndroid Build Coastguard Worker 
1191*9880d681SAndroid Build Coastguard Worker   /// \brief Zero extend to a new width.
1192*9880d681SAndroid Build Coastguard Worker   ///
1193*9880d681SAndroid Build Coastguard Worker   /// This operation zero extends the APInt to a new width. The high order bits
1194*9880d681SAndroid Build Coastguard Worker   /// are filled with 0 bits.  It is an error to specify a width that is less
1195*9880d681SAndroid Build Coastguard Worker   /// than or equal to the current width.
1196*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const;
1197*9880d681SAndroid Build Coastguard Worker 
1198*9880d681SAndroid Build Coastguard Worker   /// \brief Sign extend or truncate to width
1199*9880d681SAndroid Build Coastguard Worker   ///
1200*9880d681SAndroid Build Coastguard Worker   /// Make this APInt have the bit width given by \p width. The value is sign
1201*9880d681SAndroid Build Coastguard Worker   /// extended, truncated, or left alone to make it that width.
1202*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sextOrTrunc(unsigned width) const;
1203*9880d681SAndroid Build Coastguard Worker 
1204*9880d681SAndroid Build Coastguard Worker   /// \brief Zero extend or truncate to width
1205*9880d681SAndroid Build Coastguard Worker   ///
1206*9880d681SAndroid Build Coastguard Worker   /// Make this APInt have the bit width given by \p width. The value is zero
1207*9880d681SAndroid Build Coastguard Worker   /// extended, truncated, or left alone to make it that width.
1208*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT zextOrTrunc(unsigned width) const;
1209*9880d681SAndroid Build Coastguard Worker 
1210*9880d681SAndroid Build Coastguard Worker   /// \brief Sign extend or truncate to width
1211*9880d681SAndroid Build Coastguard Worker   ///
1212*9880d681SAndroid Build Coastguard Worker   /// Make this APInt have the bit width given by \p width. The value is sign
1213*9880d681SAndroid Build Coastguard Worker   /// extended, or left alone to make it that width.
1214*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sextOrSelf(unsigned width) const;
1215*9880d681SAndroid Build Coastguard Worker 
1216*9880d681SAndroid Build Coastguard Worker   /// \brief Zero extend or truncate to width
1217*9880d681SAndroid Build Coastguard Worker   ///
1218*9880d681SAndroid Build Coastguard Worker   /// Make this APInt have the bit width given by \p width. The value is zero
1219*9880d681SAndroid Build Coastguard Worker   /// extended, or left alone to make it that width.
1220*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT zextOrSelf(unsigned width) const;
1221*9880d681SAndroid Build Coastguard Worker 
1222*9880d681SAndroid Build Coastguard Worker   /// @}
1223*9880d681SAndroid Build Coastguard Worker   /// \name Bit Manipulation Operators
1224*9880d681SAndroid Build Coastguard Worker   /// @{
1225*9880d681SAndroid Build Coastguard Worker 
1226*9880d681SAndroid Build Coastguard Worker   /// \brief Set every bit to 1.
setAllBits()1227*9880d681SAndroid Build Coastguard Worker   void setAllBits() {
1228*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
1229*9880d681SAndroid Build Coastguard Worker       VAL = UINT64_MAX;
1230*9880d681SAndroid Build Coastguard Worker     else {
1231*9880d681SAndroid Build Coastguard Worker       // Set all the bits in all the words.
1232*9880d681SAndroid Build Coastguard Worker       for (unsigned i = 0; i < getNumWords(); ++i)
1233*9880d681SAndroid Build Coastguard Worker         pVal[i] = UINT64_MAX;
1234*9880d681SAndroid Build Coastguard Worker     }
1235*9880d681SAndroid Build Coastguard Worker     // Clear the unused ones
1236*9880d681SAndroid Build Coastguard Worker     clearUnusedBits();
1237*9880d681SAndroid Build Coastguard Worker   }
1238*9880d681SAndroid Build Coastguard Worker 
1239*9880d681SAndroid Build Coastguard Worker   /// \brief Set a given bit to 1.
1240*9880d681SAndroid Build Coastguard Worker   ///
1241*9880d681SAndroid Build Coastguard Worker   /// Set the given bit to 1 whose position is given as "bitPosition".
1242*9880d681SAndroid Build Coastguard Worker   void setBit(unsigned bitPosition);
1243*9880d681SAndroid Build Coastguard Worker 
1244*9880d681SAndroid Build Coastguard Worker   /// \brief Set every bit to 0.
clearAllBits()1245*9880d681SAndroid Build Coastguard Worker   void clearAllBits() {
1246*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
1247*9880d681SAndroid Build Coastguard Worker       VAL = 0;
1248*9880d681SAndroid Build Coastguard Worker     else
1249*9880d681SAndroid Build Coastguard Worker       memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
1250*9880d681SAndroid Build Coastguard Worker   }
1251*9880d681SAndroid Build Coastguard Worker 
1252*9880d681SAndroid Build Coastguard Worker   /// \brief Set a given bit to 0.
1253*9880d681SAndroid Build Coastguard Worker   ///
1254*9880d681SAndroid Build Coastguard Worker   /// Set the given bit to 0 whose position is given as "bitPosition".
1255*9880d681SAndroid Build Coastguard Worker   void clearBit(unsigned bitPosition);
1256*9880d681SAndroid Build Coastguard Worker 
1257*9880d681SAndroid Build Coastguard Worker   /// \brief Toggle every bit to its opposite value.
flipAllBits()1258*9880d681SAndroid Build Coastguard Worker   void flipAllBits() {
1259*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
1260*9880d681SAndroid Build Coastguard Worker       VAL ^= UINT64_MAX;
1261*9880d681SAndroid Build Coastguard Worker     else {
1262*9880d681SAndroid Build Coastguard Worker       for (unsigned i = 0; i < getNumWords(); ++i)
1263*9880d681SAndroid Build Coastguard Worker         pVal[i] ^= UINT64_MAX;
1264*9880d681SAndroid Build Coastguard Worker     }
1265*9880d681SAndroid Build Coastguard Worker     clearUnusedBits();
1266*9880d681SAndroid Build Coastguard Worker   }
1267*9880d681SAndroid Build Coastguard Worker 
1268*9880d681SAndroid Build Coastguard Worker   /// \brief Toggles a given bit to its opposite value.
1269*9880d681SAndroid Build Coastguard Worker   ///
1270*9880d681SAndroid Build Coastguard Worker   /// Toggle a given bit to its opposite value whose position is given
1271*9880d681SAndroid Build Coastguard Worker   /// as "bitPosition".
1272*9880d681SAndroid Build Coastguard Worker   void flipBit(unsigned bitPosition);
1273*9880d681SAndroid Build Coastguard Worker 
1274*9880d681SAndroid Build Coastguard Worker   /// @}
1275*9880d681SAndroid Build Coastguard Worker   /// \name Value Characterization Functions
1276*9880d681SAndroid Build Coastguard Worker   /// @{
1277*9880d681SAndroid Build Coastguard Worker 
1278*9880d681SAndroid Build Coastguard Worker   /// \brief Return the number of bits in the APInt.
getBitWidth()1279*9880d681SAndroid Build Coastguard Worker   unsigned getBitWidth() const { return BitWidth; }
1280*9880d681SAndroid Build Coastguard Worker 
1281*9880d681SAndroid Build Coastguard Worker   /// \brief Get the number of words.
1282*9880d681SAndroid Build Coastguard Worker   ///
1283*9880d681SAndroid Build Coastguard Worker   /// Here one word's bitwidth equals to that of uint64_t.
1284*9880d681SAndroid Build Coastguard Worker   ///
1285*9880d681SAndroid Build Coastguard Worker   /// \returns the number of words to hold the integer value of this APInt.
getNumWords()1286*9880d681SAndroid Build Coastguard Worker   unsigned getNumWords() const { return getNumWords(BitWidth); }
1287*9880d681SAndroid Build Coastguard Worker 
1288*9880d681SAndroid Build Coastguard Worker   /// \brief Get the number of words.
1289*9880d681SAndroid Build Coastguard Worker   ///
1290*9880d681SAndroid Build Coastguard Worker   /// *NOTE* Here one word's bitwidth equals to that of uint64_t.
1291*9880d681SAndroid Build Coastguard Worker   ///
1292*9880d681SAndroid Build Coastguard Worker   /// \returns the number of words to hold the integer value with a given bit
1293*9880d681SAndroid Build Coastguard Worker   /// width.
getNumWords(unsigned BitWidth)1294*9880d681SAndroid Build Coastguard Worker   static unsigned getNumWords(unsigned BitWidth) {
1295*9880d681SAndroid Build Coastguard Worker     return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
1296*9880d681SAndroid Build Coastguard Worker   }
1297*9880d681SAndroid Build Coastguard Worker 
1298*9880d681SAndroid Build Coastguard Worker   /// \brief Compute the number of active bits in the value
1299*9880d681SAndroid Build Coastguard Worker   ///
1300*9880d681SAndroid Build Coastguard Worker   /// This function returns the number of active bits which is defined as the
1301*9880d681SAndroid Build Coastguard Worker   /// bit width minus the number of leading zeros. This is used in several
1302*9880d681SAndroid Build Coastguard Worker   /// computations to see how "wide" the value is.
getActiveBits()1303*9880d681SAndroid Build Coastguard Worker   unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); }
1304*9880d681SAndroid Build Coastguard Worker 
1305*9880d681SAndroid Build Coastguard Worker   /// \brief Compute the number of active words in the value of this APInt.
1306*9880d681SAndroid Build Coastguard Worker   ///
1307*9880d681SAndroid Build Coastguard Worker   /// This is used in conjunction with getActiveData to extract the raw value of
1308*9880d681SAndroid Build Coastguard Worker   /// the APInt.
getActiveWords()1309*9880d681SAndroid Build Coastguard Worker   unsigned getActiveWords() const {
1310*9880d681SAndroid Build Coastguard Worker     unsigned numActiveBits = getActiveBits();
1311*9880d681SAndroid Build Coastguard Worker     return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1;
1312*9880d681SAndroid Build Coastguard Worker   }
1313*9880d681SAndroid Build Coastguard Worker 
1314*9880d681SAndroid Build Coastguard Worker   /// \brief Get the minimum bit size for this signed APInt
1315*9880d681SAndroid Build Coastguard Worker   ///
1316*9880d681SAndroid Build Coastguard Worker   /// Computes the minimum bit width for this APInt while considering it to be a
1317*9880d681SAndroid Build Coastguard Worker   /// signed (and probably negative) value. If the value is not negative, this
1318*9880d681SAndroid Build Coastguard Worker   /// function returns the same value as getActiveBits()+1. Otherwise, it
1319*9880d681SAndroid Build Coastguard Worker   /// returns the smallest bit width that will retain the negative value. For
1320*9880d681SAndroid Build Coastguard Worker   /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
1321*9880d681SAndroid Build Coastguard Worker   /// for -1, this function will always return 1.
getMinSignedBits()1322*9880d681SAndroid Build Coastguard Worker   unsigned getMinSignedBits() const {
1323*9880d681SAndroid Build Coastguard Worker     if (isNegative())
1324*9880d681SAndroid Build Coastguard Worker       return BitWidth - countLeadingOnes() + 1;
1325*9880d681SAndroid Build Coastguard Worker     return getActiveBits() + 1;
1326*9880d681SAndroid Build Coastguard Worker   }
1327*9880d681SAndroid Build Coastguard Worker 
1328*9880d681SAndroid Build Coastguard Worker   /// \brief Get zero extended value
1329*9880d681SAndroid Build Coastguard Worker   ///
1330*9880d681SAndroid Build Coastguard Worker   /// This method attempts to return the value of this APInt as a zero extended
1331*9880d681SAndroid Build Coastguard Worker   /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
1332*9880d681SAndroid Build Coastguard Worker   /// uint64_t. Otherwise an assertion will result.
getZExtValue()1333*9880d681SAndroid Build Coastguard Worker   uint64_t getZExtValue() const {
1334*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
1335*9880d681SAndroid Build Coastguard Worker       return VAL;
1336*9880d681SAndroid Build Coastguard Worker     assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
1337*9880d681SAndroid Build Coastguard Worker     return pVal[0];
1338*9880d681SAndroid Build Coastguard Worker   }
1339*9880d681SAndroid Build Coastguard Worker 
1340*9880d681SAndroid Build Coastguard Worker   /// \brief Get sign extended value
1341*9880d681SAndroid Build Coastguard Worker   ///
1342*9880d681SAndroid Build Coastguard Worker   /// This method attempts to return the value of this APInt as a sign extended
1343*9880d681SAndroid Build Coastguard Worker   /// int64_t. The bit width must be <= 64 or the value must fit within an
1344*9880d681SAndroid Build Coastguard Worker   /// int64_t. Otherwise an assertion will result.
getSExtValue()1345*9880d681SAndroid Build Coastguard Worker   int64_t getSExtValue() const {
1346*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
1347*9880d681SAndroid Build Coastguard Worker       return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >>
1348*9880d681SAndroid Build Coastguard Worker              (APINT_BITS_PER_WORD - BitWidth);
1349*9880d681SAndroid Build Coastguard Worker     assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
1350*9880d681SAndroid Build Coastguard Worker     return int64_t(pVal[0]);
1351*9880d681SAndroid Build Coastguard Worker   }
1352*9880d681SAndroid Build Coastguard Worker 
1353*9880d681SAndroid Build Coastguard Worker   /// \brief Get bits required for string value.
1354*9880d681SAndroid Build Coastguard Worker   ///
1355*9880d681SAndroid Build Coastguard Worker   /// This method determines how many bits are required to hold the APInt
1356*9880d681SAndroid Build Coastguard Worker   /// equivalent of the string given by \p str.
1357*9880d681SAndroid Build Coastguard Worker   static unsigned getBitsNeeded(StringRef str, uint8_t radix);
1358*9880d681SAndroid Build Coastguard Worker 
1359*9880d681SAndroid Build Coastguard Worker   /// \brief The APInt version of the countLeadingZeros functions in
1360*9880d681SAndroid Build Coastguard Worker   ///   MathExtras.h.
1361*9880d681SAndroid Build Coastguard Worker   ///
1362*9880d681SAndroid Build Coastguard Worker   /// It counts the number of zeros from the most significant bit to the first
1363*9880d681SAndroid Build Coastguard Worker   /// one bit.
1364*9880d681SAndroid Build Coastguard Worker   ///
1365*9880d681SAndroid Build Coastguard Worker   /// \returns BitWidth if the value is zero, otherwise returns the number of
1366*9880d681SAndroid Build Coastguard Worker   ///   zeros from the most significant bit to the first one bits.
countLeadingZeros()1367*9880d681SAndroid Build Coastguard Worker   unsigned countLeadingZeros() const {
1368*9880d681SAndroid Build Coastguard Worker     if (isSingleWord()) {
1369*9880d681SAndroid Build Coastguard Worker       unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
1370*9880d681SAndroid Build Coastguard Worker       return llvm::countLeadingZeros(VAL) - unusedBits;
1371*9880d681SAndroid Build Coastguard Worker     }
1372*9880d681SAndroid Build Coastguard Worker     return countLeadingZerosSlowCase();
1373*9880d681SAndroid Build Coastguard Worker   }
1374*9880d681SAndroid Build Coastguard Worker 
1375*9880d681SAndroid Build Coastguard Worker   /// \brief Count the number of leading one bits.
1376*9880d681SAndroid Build Coastguard Worker   ///
1377*9880d681SAndroid Build Coastguard Worker   /// This function is an APInt version of the countLeadingOnes
1378*9880d681SAndroid Build Coastguard Worker   /// functions in MathExtras.h. It counts the number of ones from the most
1379*9880d681SAndroid Build Coastguard Worker   /// significant bit to the first zero bit.
1380*9880d681SAndroid Build Coastguard Worker   ///
1381*9880d681SAndroid Build Coastguard Worker   /// \returns 0 if the high order bit is not set, otherwise returns the number
1382*9880d681SAndroid Build Coastguard Worker   /// of 1 bits from the most significant to the least
1383*9880d681SAndroid Build Coastguard Worker   unsigned countLeadingOnes() const;
1384*9880d681SAndroid Build Coastguard Worker 
1385*9880d681SAndroid Build Coastguard Worker   /// Computes the number of leading bits of this APInt that are equal to its
1386*9880d681SAndroid Build Coastguard Worker   /// sign bit.
getNumSignBits()1387*9880d681SAndroid Build Coastguard Worker   unsigned getNumSignBits() const {
1388*9880d681SAndroid Build Coastguard Worker     return isNegative() ? countLeadingOnes() : countLeadingZeros();
1389*9880d681SAndroid Build Coastguard Worker   }
1390*9880d681SAndroid Build Coastguard Worker 
1391*9880d681SAndroid Build Coastguard Worker   /// \brief Count the number of trailing zero bits.
1392*9880d681SAndroid Build Coastguard Worker   ///
1393*9880d681SAndroid Build Coastguard Worker   /// This function is an APInt version of the countTrailingZeros
1394*9880d681SAndroid Build Coastguard Worker   /// functions in MathExtras.h. It counts the number of zeros from the least
1395*9880d681SAndroid Build Coastguard Worker   /// significant bit to the first set bit.
1396*9880d681SAndroid Build Coastguard Worker   ///
1397*9880d681SAndroid Build Coastguard Worker   /// \returns BitWidth if the value is zero, otherwise returns the number of
1398*9880d681SAndroid Build Coastguard Worker   /// zeros from the least significant bit to the first one bit.
1399*9880d681SAndroid Build Coastguard Worker   unsigned countTrailingZeros() const;
1400*9880d681SAndroid Build Coastguard Worker 
1401*9880d681SAndroid Build Coastguard Worker   /// \brief Count the number of trailing one bits.
1402*9880d681SAndroid Build Coastguard Worker   ///
1403*9880d681SAndroid Build Coastguard Worker   /// This function is an APInt version of the countTrailingOnes
1404*9880d681SAndroid Build Coastguard Worker   /// functions in MathExtras.h. It counts the number of ones from the least
1405*9880d681SAndroid Build Coastguard Worker   /// significant bit to the first zero bit.
1406*9880d681SAndroid Build Coastguard Worker   ///
1407*9880d681SAndroid Build Coastguard Worker   /// \returns BitWidth if the value is all ones, otherwise returns the number
1408*9880d681SAndroid Build Coastguard Worker   /// of ones from the least significant bit to the first zero bit.
countTrailingOnes()1409*9880d681SAndroid Build Coastguard Worker   unsigned countTrailingOnes() const {
1410*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
1411*9880d681SAndroid Build Coastguard Worker       return llvm::countTrailingOnes(VAL);
1412*9880d681SAndroid Build Coastguard Worker     return countTrailingOnesSlowCase();
1413*9880d681SAndroid Build Coastguard Worker   }
1414*9880d681SAndroid Build Coastguard Worker 
1415*9880d681SAndroid Build Coastguard Worker   /// \brief Count the number of bits set.
1416*9880d681SAndroid Build Coastguard Worker   ///
1417*9880d681SAndroid Build Coastguard Worker   /// This function is an APInt version of the countPopulation functions
1418*9880d681SAndroid Build Coastguard Worker   /// in MathExtras.h. It counts the number of 1 bits in the APInt value.
1419*9880d681SAndroid Build Coastguard Worker   ///
1420*9880d681SAndroid Build Coastguard Worker   /// \returns 0 if the value is zero, otherwise returns the number of set bits.
countPopulation()1421*9880d681SAndroid Build Coastguard Worker   unsigned countPopulation() const {
1422*9880d681SAndroid Build Coastguard Worker     if (isSingleWord())
1423*9880d681SAndroid Build Coastguard Worker       return llvm::countPopulation(VAL);
1424*9880d681SAndroid Build Coastguard Worker     return countPopulationSlowCase();
1425*9880d681SAndroid Build Coastguard Worker   }
1426*9880d681SAndroid Build Coastguard Worker 
1427*9880d681SAndroid Build Coastguard Worker   /// @}
1428*9880d681SAndroid Build Coastguard Worker   /// \name Conversion Functions
1429*9880d681SAndroid Build Coastguard Worker   /// @{
1430*9880d681SAndroid Build Coastguard Worker   void print(raw_ostream &OS, bool isSigned) const;
1431*9880d681SAndroid Build Coastguard Worker 
1432*9880d681SAndroid Build Coastguard Worker   /// Converts an APInt to a string and append it to Str.  Str is commonly a
1433*9880d681SAndroid Build Coastguard Worker   /// SmallString.
1434*9880d681SAndroid Build Coastguard Worker   void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
1435*9880d681SAndroid Build Coastguard Worker                 bool formatAsCLiteral = false) const;
1436*9880d681SAndroid Build Coastguard Worker 
1437*9880d681SAndroid Build Coastguard Worker   /// Considers the APInt to be unsigned and converts it into a string in the
1438*9880d681SAndroid Build Coastguard Worker   /// radix given. The radix can be 2, 8, 10 16, or 36.
1439*9880d681SAndroid Build Coastguard Worker   void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1440*9880d681SAndroid Build Coastguard Worker     toString(Str, Radix, false, false);
1441*9880d681SAndroid Build Coastguard Worker   }
1442*9880d681SAndroid Build Coastguard Worker 
1443*9880d681SAndroid Build Coastguard Worker   /// Considers the APInt to be signed and converts it into a string in the
1444*9880d681SAndroid Build Coastguard Worker   /// radix given. The radix can be 2, 8, 10, 16, or 36.
1445*9880d681SAndroid Build Coastguard Worker   void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1446*9880d681SAndroid Build Coastguard Worker     toString(Str, Radix, true, false);
1447*9880d681SAndroid Build Coastguard Worker   }
1448*9880d681SAndroid Build Coastguard Worker 
1449*9880d681SAndroid Build Coastguard Worker   /// \brief Return the APInt as a std::string.
1450*9880d681SAndroid Build Coastguard Worker   ///
1451*9880d681SAndroid Build Coastguard Worker   /// Note that this is an inefficient method.  It is better to pass in a
1452*9880d681SAndroid Build Coastguard Worker   /// SmallVector/SmallString to the methods above to avoid thrashing the heap
1453*9880d681SAndroid Build Coastguard Worker   /// for the string.
1454*9880d681SAndroid Build Coastguard Worker   std::string toString(unsigned Radix, bool Signed) const;
1455*9880d681SAndroid Build Coastguard Worker 
1456*9880d681SAndroid Build Coastguard Worker   /// \returns a byte-swapped representation of this APInt Value.
1457*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT byteSwap() const;
1458*9880d681SAndroid Build Coastguard Worker 
1459*9880d681SAndroid Build Coastguard Worker   /// \returns the value with the bit representation reversed of this APInt
1460*9880d681SAndroid Build Coastguard Worker   /// Value.
1461*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT reverseBits() const;
1462*9880d681SAndroid Build Coastguard Worker 
1463*9880d681SAndroid Build Coastguard Worker   /// \brief Converts this APInt to a double value.
1464*9880d681SAndroid Build Coastguard Worker   double roundToDouble(bool isSigned) const;
1465*9880d681SAndroid Build Coastguard Worker 
1466*9880d681SAndroid Build Coastguard Worker   /// \brief Converts this unsigned APInt to a double value.
roundToDouble()1467*9880d681SAndroid Build Coastguard Worker   double roundToDouble() const { return roundToDouble(false); }
1468*9880d681SAndroid Build Coastguard Worker 
1469*9880d681SAndroid Build Coastguard Worker   /// \brief Converts this signed APInt to a double value.
signedRoundToDouble()1470*9880d681SAndroid Build Coastguard Worker   double signedRoundToDouble() const { return roundToDouble(true); }
1471*9880d681SAndroid Build Coastguard Worker 
1472*9880d681SAndroid Build Coastguard Worker   /// \brief Converts APInt bits to a double
1473*9880d681SAndroid Build Coastguard Worker   ///
1474*9880d681SAndroid Build Coastguard Worker   /// The conversion does not do a translation from integer to double, it just
1475*9880d681SAndroid Build Coastguard Worker   /// re-interprets the bits as a double. Note that it is valid to do this on
1476*9880d681SAndroid Build Coastguard Worker   /// any bit width. Exactly 64 bits will be translated.
bitsToDouble()1477*9880d681SAndroid Build Coastguard Worker   double bitsToDouble() const {
1478*9880d681SAndroid Build Coastguard Worker     union {
1479*9880d681SAndroid Build Coastguard Worker       uint64_t I;
1480*9880d681SAndroid Build Coastguard Worker       double D;
1481*9880d681SAndroid Build Coastguard Worker     } T;
1482*9880d681SAndroid Build Coastguard Worker     T.I = (isSingleWord() ? VAL : pVal[0]);
1483*9880d681SAndroid Build Coastguard Worker     return T.D;
1484*9880d681SAndroid Build Coastguard Worker   }
1485*9880d681SAndroid Build Coastguard Worker 
1486*9880d681SAndroid Build Coastguard Worker   /// \brief Converts APInt bits to a double
1487*9880d681SAndroid Build Coastguard Worker   ///
1488*9880d681SAndroid Build Coastguard Worker   /// The conversion does not do a translation from integer to float, it just
1489*9880d681SAndroid Build Coastguard Worker   /// re-interprets the bits as a float. Note that it is valid to do this on
1490*9880d681SAndroid Build Coastguard Worker   /// any bit width. Exactly 32 bits will be translated.
bitsToFloat()1491*9880d681SAndroid Build Coastguard Worker   float bitsToFloat() const {
1492*9880d681SAndroid Build Coastguard Worker     union {
1493*9880d681SAndroid Build Coastguard Worker       unsigned I;
1494*9880d681SAndroid Build Coastguard Worker       float F;
1495*9880d681SAndroid Build Coastguard Worker     } T;
1496*9880d681SAndroid Build Coastguard Worker     T.I = unsigned((isSingleWord() ? VAL : pVal[0]));
1497*9880d681SAndroid Build Coastguard Worker     return T.F;
1498*9880d681SAndroid Build Coastguard Worker   }
1499*9880d681SAndroid Build Coastguard Worker 
1500*9880d681SAndroid Build Coastguard Worker   /// \brief Converts a double to APInt bits.
1501*9880d681SAndroid Build Coastguard Worker   ///
1502*9880d681SAndroid Build Coastguard Worker   /// The conversion does not do a translation from double to integer, it just
1503*9880d681SAndroid Build Coastguard Worker   /// re-interprets the bits of the double.
doubleToBits(double V)1504*9880d681SAndroid Build Coastguard Worker   static APInt LLVM_ATTRIBUTE_UNUSED_RESULT doubleToBits(double V) {
1505*9880d681SAndroid Build Coastguard Worker     union {
1506*9880d681SAndroid Build Coastguard Worker       uint64_t I;
1507*9880d681SAndroid Build Coastguard Worker       double D;
1508*9880d681SAndroid Build Coastguard Worker     } T;
1509*9880d681SAndroid Build Coastguard Worker     T.D = V;
1510*9880d681SAndroid Build Coastguard Worker     return APInt(sizeof T * CHAR_BIT, T.I);
1511*9880d681SAndroid Build Coastguard Worker   }
1512*9880d681SAndroid Build Coastguard Worker 
1513*9880d681SAndroid Build Coastguard Worker   /// \brief Converts a float to APInt bits.
1514*9880d681SAndroid Build Coastguard Worker   ///
1515*9880d681SAndroid Build Coastguard Worker   /// The conversion does not do a translation from float to integer, it just
1516*9880d681SAndroid Build Coastguard Worker   /// re-interprets the bits of the float.
floatToBits(float V)1517*9880d681SAndroid Build Coastguard Worker   static APInt LLVM_ATTRIBUTE_UNUSED_RESULT floatToBits(float V) {
1518*9880d681SAndroid Build Coastguard Worker     union {
1519*9880d681SAndroid Build Coastguard Worker       unsigned I;
1520*9880d681SAndroid Build Coastguard Worker       float F;
1521*9880d681SAndroid Build Coastguard Worker     } T;
1522*9880d681SAndroid Build Coastguard Worker     T.F = V;
1523*9880d681SAndroid Build Coastguard Worker     return APInt(sizeof T * CHAR_BIT, T.I);
1524*9880d681SAndroid Build Coastguard Worker   }
1525*9880d681SAndroid Build Coastguard Worker 
1526*9880d681SAndroid Build Coastguard Worker   /// @}
1527*9880d681SAndroid Build Coastguard Worker   /// \name Mathematics Operations
1528*9880d681SAndroid Build Coastguard Worker   /// @{
1529*9880d681SAndroid Build Coastguard Worker 
1530*9880d681SAndroid Build Coastguard Worker   /// \returns the floor log base 2 of this APInt.
logBase2()1531*9880d681SAndroid Build Coastguard Worker   unsigned logBase2() const { return BitWidth - 1 - countLeadingZeros(); }
1532*9880d681SAndroid Build Coastguard Worker 
1533*9880d681SAndroid Build Coastguard Worker   /// \returns the ceil log base 2 of this APInt.
ceilLogBase2()1534*9880d681SAndroid Build Coastguard Worker   unsigned ceilLogBase2() const {
1535*9880d681SAndroid Build Coastguard Worker     return BitWidth - (*this - 1).countLeadingZeros();
1536*9880d681SAndroid Build Coastguard Worker   }
1537*9880d681SAndroid Build Coastguard Worker 
1538*9880d681SAndroid Build Coastguard Worker   /// \returns the nearest log base 2 of this APInt. Ties round up.
1539*9880d681SAndroid Build Coastguard Worker   ///
1540*9880d681SAndroid Build Coastguard Worker   /// NOTE: When we have a BitWidth of 1, we define:
1541*9880d681SAndroid Build Coastguard Worker   ///
1542*9880d681SAndroid Build Coastguard Worker   ///   log2(0) = UINT32_MAX
1543*9880d681SAndroid Build Coastguard Worker   ///   log2(1) = 0
1544*9880d681SAndroid Build Coastguard Worker   ///
1545*9880d681SAndroid Build Coastguard Worker   /// to get around any mathematical concerns resulting from
1546*9880d681SAndroid Build Coastguard Worker   /// referencing 2 in a space where 2 does no exist.
nearestLogBase2()1547*9880d681SAndroid Build Coastguard Worker   unsigned nearestLogBase2() const {
1548*9880d681SAndroid Build Coastguard Worker     // Special case when we have a bitwidth of 1. If VAL is 1, then we
1549*9880d681SAndroid Build Coastguard Worker     // get 0. If VAL is 0, we get UINT64_MAX which gets truncated to
1550*9880d681SAndroid Build Coastguard Worker     // UINT32_MAX.
1551*9880d681SAndroid Build Coastguard Worker     if (BitWidth == 1)
1552*9880d681SAndroid Build Coastguard Worker       return VAL - 1;
1553*9880d681SAndroid Build Coastguard Worker 
1554*9880d681SAndroid Build Coastguard Worker     // Handle the zero case.
1555*9880d681SAndroid Build Coastguard Worker     if (!getBoolValue())
1556*9880d681SAndroid Build Coastguard Worker       return UINT32_MAX;
1557*9880d681SAndroid Build Coastguard Worker 
1558*9880d681SAndroid Build Coastguard Worker     // The non-zero case is handled by computing:
1559*9880d681SAndroid Build Coastguard Worker     //
1560*9880d681SAndroid Build Coastguard Worker     //   nearestLogBase2(x) = logBase2(x) + x[logBase2(x)-1].
1561*9880d681SAndroid Build Coastguard Worker     //
1562*9880d681SAndroid Build Coastguard Worker     // where x[i] is referring to the value of the ith bit of x.
1563*9880d681SAndroid Build Coastguard Worker     unsigned lg = logBase2();
1564*9880d681SAndroid Build Coastguard Worker     return lg + unsigned((*this)[lg - 1]);
1565*9880d681SAndroid Build Coastguard Worker   }
1566*9880d681SAndroid Build Coastguard Worker 
1567*9880d681SAndroid Build Coastguard Worker   /// \returns the log base 2 of this APInt if its an exact power of two, -1
1568*9880d681SAndroid Build Coastguard Worker   /// otherwise
exactLogBase2()1569*9880d681SAndroid Build Coastguard Worker   int32_t exactLogBase2() const {
1570*9880d681SAndroid Build Coastguard Worker     if (!isPowerOf2())
1571*9880d681SAndroid Build Coastguard Worker       return -1;
1572*9880d681SAndroid Build Coastguard Worker     return logBase2();
1573*9880d681SAndroid Build Coastguard Worker   }
1574*9880d681SAndroid Build Coastguard Worker 
1575*9880d681SAndroid Build Coastguard Worker   /// \brief Compute the square root
1576*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sqrt() const;
1577*9880d681SAndroid Build Coastguard Worker 
1578*9880d681SAndroid Build Coastguard Worker   /// \brief Get the absolute value;
1579*9880d681SAndroid Build Coastguard Worker   ///
1580*9880d681SAndroid Build Coastguard Worker   /// If *this is < 0 then return -(*this), otherwise *this;
abs()1581*9880d681SAndroid Build Coastguard Worker   APInt LLVM_ATTRIBUTE_UNUSED_RESULT abs() const {
1582*9880d681SAndroid Build Coastguard Worker     if (isNegative())
1583*9880d681SAndroid Build Coastguard Worker       return -(*this);
1584*9880d681SAndroid Build Coastguard Worker     return *this;
1585*9880d681SAndroid Build Coastguard Worker   }
1586*9880d681SAndroid Build Coastguard Worker 
1587*9880d681SAndroid Build Coastguard Worker   /// \returns the multiplicative inverse for a given modulo.
1588*9880d681SAndroid Build Coastguard Worker   APInt multiplicativeInverse(const APInt &modulo) const;
1589*9880d681SAndroid Build Coastguard Worker 
1590*9880d681SAndroid Build Coastguard Worker   /// @}
1591*9880d681SAndroid Build Coastguard Worker   /// \name Support for division by constant
1592*9880d681SAndroid Build Coastguard Worker   /// @{
1593*9880d681SAndroid Build Coastguard Worker 
1594*9880d681SAndroid Build Coastguard Worker   /// Calculate the magic number for signed division by a constant.
1595*9880d681SAndroid Build Coastguard Worker   struct ms;
1596*9880d681SAndroid Build Coastguard Worker   ms magic() const;
1597*9880d681SAndroid Build Coastguard Worker 
1598*9880d681SAndroid Build Coastguard Worker   /// Calculate the magic number for unsigned division by a constant.
1599*9880d681SAndroid Build Coastguard Worker   struct mu;
1600*9880d681SAndroid Build Coastguard Worker   mu magicu(unsigned LeadingZeros = 0) const;
1601*9880d681SAndroid Build Coastguard Worker 
1602*9880d681SAndroid Build Coastguard Worker   /// @}
1603*9880d681SAndroid Build Coastguard Worker   /// \name Building-block Operations for APInt and APFloat
1604*9880d681SAndroid Build Coastguard Worker   /// @{
1605*9880d681SAndroid Build Coastguard Worker 
1606*9880d681SAndroid Build Coastguard Worker   // These building block operations operate on a representation of arbitrary
1607*9880d681SAndroid Build Coastguard Worker   // precision, two's-complement, bignum integer values. They should be
1608*9880d681SAndroid Build Coastguard Worker   // sufficient to implement APInt and APFloat bignum requirements. Inputs are
1609*9880d681SAndroid Build Coastguard Worker   // generally a pointer to the base of an array of integer parts, representing
1610*9880d681SAndroid Build Coastguard Worker   // an unsigned bignum, and a count of how many parts there are.
1611*9880d681SAndroid Build Coastguard Worker 
1612*9880d681SAndroid Build Coastguard Worker   /// Sets the least significant part of a bignum to the input value, and zeroes
1613*9880d681SAndroid Build Coastguard Worker   /// out higher parts.
1614*9880d681SAndroid Build Coastguard Worker   static void tcSet(integerPart *, integerPart, unsigned int);
1615*9880d681SAndroid Build Coastguard Worker 
1616*9880d681SAndroid Build Coastguard Worker   /// Assign one bignum to another.
1617*9880d681SAndroid Build Coastguard Worker   static void tcAssign(integerPart *, const integerPart *, unsigned int);
1618*9880d681SAndroid Build Coastguard Worker 
1619*9880d681SAndroid Build Coastguard Worker   /// Returns true if a bignum is zero, false otherwise.
1620*9880d681SAndroid Build Coastguard Worker   static bool tcIsZero(const integerPart *, unsigned int);
1621*9880d681SAndroid Build Coastguard Worker 
1622*9880d681SAndroid Build Coastguard Worker   /// Extract the given bit of a bignum; returns 0 or 1.  Zero-based.
1623*9880d681SAndroid Build Coastguard Worker   static int tcExtractBit(const integerPart *, unsigned int bit);
1624*9880d681SAndroid Build Coastguard Worker 
1625*9880d681SAndroid Build Coastguard Worker   /// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to
1626*9880d681SAndroid Build Coastguard Worker   /// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least
1627*9880d681SAndroid Build Coastguard Worker   /// significant bit of DST.  All high bits above srcBITS in DST are
1628*9880d681SAndroid Build Coastguard Worker   /// zero-filled.
1629*9880d681SAndroid Build Coastguard Worker   static void tcExtract(integerPart *, unsigned int dstCount,
1630*9880d681SAndroid Build Coastguard Worker                         const integerPart *, unsigned int srcBits,
1631*9880d681SAndroid Build Coastguard Worker                         unsigned int srcLSB);
1632*9880d681SAndroid Build Coastguard Worker 
1633*9880d681SAndroid Build Coastguard Worker   /// Set the given bit of a bignum.  Zero-based.
1634*9880d681SAndroid Build Coastguard Worker   static void tcSetBit(integerPart *, unsigned int bit);
1635*9880d681SAndroid Build Coastguard Worker 
1636*9880d681SAndroid Build Coastguard Worker   /// Clear the given bit of a bignum.  Zero-based.
1637*9880d681SAndroid Build Coastguard Worker   static void tcClearBit(integerPart *, unsigned int bit);
1638*9880d681SAndroid Build Coastguard Worker 
1639*9880d681SAndroid Build Coastguard Worker   /// Returns the bit number of the least or most significant set bit of a
1640*9880d681SAndroid Build Coastguard Worker   /// number.  If the input number has no bits set -1U is returned.
1641*9880d681SAndroid Build Coastguard Worker   static unsigned int tcLSB(const integerPart *, unsigned int);
1642*9880d681SAndroid Build Coastguard Worker   static unsigned int tcMSB(const integerPart *parts, unsigned int n);
1643*9880d681SAndroid Build Coastguard Worker 
1644*9880d681SAndroid Build Coastguard Worker   /// Negate a bignum in-place.
1645*9880d681SAndroid Build Coastguard Worker   static void tcNegate(integerPart *, unsigned int);
1646*9880d681SAndroid Build Coastguard Worker 
1647*9880d681SAndroid Build Coastguard Worker   /// DST += RHS + CARRY where CARRY is zero or one.  Returns the carry flag.
1648*9880d681SAndroid Build Coastguard Worker   static integerPart tcAdd(integerPart *, const integerPart *,
1649*9880d681SAndroid Build Coastguard Worker                            integerPart carry, unsigned);
1650*9880d681SAndroid Build Coastguard Worker 
1651*9880d681SAndroid Build Coastguard Worker   /// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1652*9880d681SAndroid Build Coastguard Worker   static integerPart tcSubtract(integerPart *, const integerPart *,
1653*9880d681SAndroid Build Coastguard Worker                                 integerPart carry, unsigned);
1654*9880d681SAndroid Build Coastguard Worker 
1655*9880d681SAndroid Build Coastguard Worker   /// DST += SRC * MULTIPLIER + PART   if add is true
1656*9880d681SAndroid Build Coastguard Worker   /// DST  = SRC * MULTIPLIER + PART   if add is false
1657*9880d681SAndroid Build Coastguard Worker   ///
1658*9880d681SAndroid Build Coastguard Worker   /// Requires 0 <= DSTPARTS <= SRCPARTS + 1.  If DST overlaps SRC they must
1659*9880d681SAndroid Build Coastguard Worker   /// start at the same point, i.e. DST == SRC.
1660*9880d681SAndroid Build Coastguard Worker   ///
1661*9880d681SAndroid Build Coastguard Worker   /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned.
1662*9880d681SAndroid Build Coastguard Worker   /// Otherwise DST is filled with the least significant DSTPARTS parts of the
1663*9880d681SAndroid Build Coastguard Worker   /// result, and if all of the omitted higher parts were zero return zero,
1664*9880d681SAndroid Build Coastguard Worker   /// otherwise overflow occurred and return one.
1665*9880d681SAndroid Build Coastguard Worker   static int tcMultiplyPart(integerPart *dst, const integerPart *src,
1666*9880d681SAndroid Build Coastguard Worker                             integerPart multiplier, integerPart carry,
1667*9880d681SAndroid Build Coastguard Worker                             unsigned int srcParts, unsigned int dstParts,
1668*9880d681SAndroid Build Coastguard Worker                             bool add);
1669*9880d681SAndroid Build Coastguard Worker 
1670*9880d681SAndroid Build Coastguard Worker   /// DST = LHS * RHS, where DST has the same width as the operands and is
1671*9880d681SAndroid Build Coastguard Worker   /// filled with the least significant parts of the result.  Returns one if
1672*9880d681SAndroid Build Coastguard Worker   /// overflow occurred, otherwise zero.  DST must be disjoint from both
1673*9880d681SAndroid Build Coastguard Worker   /// operands.
1674*9880d681SAndroid Build Coastguard Worker   static int tcMultiply(integerPart *, const integerPart *, const integerPart *,
1675*9880d681SAndroid Build Coastguard Worker                         unsigned);
1676*9880d681SAndroid Build Coastguard Worker 
1677*9880d681SAndroid Build Coastguard Worker   /// DST = LHS * RHS, where DST has width the sum of the widths of the
1678*9880d681SAndroid Build Coastguard Worker   /// operands.  No overflow occurs.  DST must be disjoint from both
1679*9880d681SAndroid Build Coastguard Worker   /// operands. Returns the number of parts required to hold the result.
1680*9880d681SAndroid Build Coastguard Worker   static unsigned int tcFullMultiply(integerPart *, const integerPart *,
1681*9880d681SAndroid Build Coastguard Worker                                      const integerPart *, unsigned, unsigned);
1682*9880d681SAndroid Build Coastguard Worker 
1683*9880d681SAndroid Build Coastguard Worker   /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1684*9880d681SAndroid Build Coastguard Worker   /// Otherwise set LHS to LHS / RHS with the fractional part discarded, set
1685*9880d681SAndroid Build Coastguard Worker   /// REMAINDER to the remainder, return zero.  i.e.
1686*9880d681SAndroid Build Coastguard Worker   ///
1687*9880d681SAndroid Build Coastguard Worker   ///  OLD_LHS = RHS * LHS + REMAINDER
1688*9880d681SAndroid Build Coastguard Worker   ///
1689*9880d681SAndroid Build Coastguard Worker   /// SCRATCH is a bignum of the same size as the operands and result for use by
1690*9880d681SAndroid Build Coastguard Worker   /// the routine; its contents need not be initialized and are destroyed.  LHS,
1691*9880d681SAndroid Build Coastguard Worker   /// REMAINDER and SCRATCH must be distinct.
1692*9880d681SAndroid Build Coastguard Worker   static int tcDivide(integerPart *lhs, const integerPart *rhs,
1693*9880d681SAndroid Build Coastguard Worker                       integerPart *remainder, integerPart *scratch,
1694*9880d681SAndroid Build Coastguard Worker                       unsigned int parts);
1695*9880d681SAndroid Build Coastguard Worker 
1696*9880d681SAndroid Build Coastguard Worker   /// Shift a bignum left COUNT bits.  Shifted in bits are zero.  There are no
1697*9880d681SAndroid Build Coastguard Worker   /// restrictions on COUNT.
1698*9880d681SAndroid Build Coastguard Worker   static void tcShiftLeft(integerPart *, unsigned int parts,
1699*9880d681SAndroid Build Coastguard Worker                           unsigned int count);
1700*9880d681SAndroid Build Coastguard Worker 
1701*9880d681SAndroid Build Coastguard Worker   /// Shift a bignum right COUNT bits.  Shifted in bits are zero.  There are no
1702*9880d681SAndroid Build Coastguard Worker   /// restrictions on COUNT.
1703*9880d681SAndroid Build Coastguard Worker   static void tcShiftRight(integerPart *, unsigned int parts,
1704*9880d681SAndroid Build Coastguard Worker                            unsigned int count);
1705*9880d681SAndroid Build Coastguard Worker 
1706*9880d681SAndroid Build Coastguard Worker   /// The obvious AND, OR and XOR and complement operations.
1707*9880d681SAndroid Build Coastguard Worker   static void tcAnd(integerPart *, const integerPart *, unsigned int);
1708*9880d681SAndroid Build Coastguard Worker   static void tcOr(integerPart *, const integerPart *, unsigned int);
1709*9880d681SAndroid Build Coastguard Worker   static void tcXor(integerPart *, const integerPart *, unsigned int);
1710*9880d681SAndroid Build Coastguard Worker   static void tcComplement(integerPart *, unsigned int);
1711*9880d681SAndroid Build Coastguard Worker 
1712*9880d681SAndroid Build Coastguard Worker   /// Comparison (unsigned) of two bignums.
1713*9880d681SAndroid Build Coastguard Worker   static int tcCompare(const integerPart *, const integerPart *, unsigned int);
1714*9880d681SAndroid Build Coastguard Worker 
1715*9880d681SAndroid Build Coastguard Worker   /// Increment a bignum in-place.  Return the carry flag.
1716*9880d681SAndroid Build Coastguard Worker   static integerPart tcIncrement(integerPart *, unsigned int);
1717*9880d681SAndroid Build Coastguard Worker 
1718*9880d681SAndroid Build Coastguard Worker   /// Decrement a bignum in-place.  Return the borrow flag.
1719*9880d681SAndroid Build Coastguard Worker   static integerPart tcDecrement(integerPart *, unsigned int);
1720*9880d681SAndroid Build Coastguard Worker 
1721*9880d681SAndroid Build Coastguard Worker   /// Set the least significant BITS and clear the rest.
1722*9880d681SAndroid Build Coastguard Worker   static void tcSetLeastSignificantBits(integerPart *, unsigned int,
1723*9880d681SAndroid Build Coastguard Worker                                         unsigned int bits);
1724*9880d681SAndroid Build Coastguard Worker 
1725*9880d681SAndroid Build Coastguard Worker   /// \brief debug method
1726*9880d681SAndroid Build Coastguard Worker   void dump() const;
1727*9880d681SAndroid Build Coastguard Worker 
1728*9880d681SAndroid Build Coastguard Worker   /// @}
1729*9880d681SAndroid Build Coastguard Worker };
1730*9880d681SAndroid Build Coastguard Worker 
1731*9880d681SAndroid Build Coastguard Worker /// Magic data for optimising signed division by a constant.
1732*9880d681SAndroid Build Coastguard Worker struct APInt::ms {
1733*9880d681SAndroid Build Coastguard Worker   APInt m;    ///< magic number
1734*9880d681SAndroid Build Coastguard Worker   unsigned s; ///< shift amount
1735*9880d681SAndroid Build Coastguard Worker };
1736*9880d681SAndroid Build Coastguard Worker 
1737*9880d681SAndroid Build Coastguard Worker /// Magic data for optimising unsigned division by a constant.
1738*9880d681SAndroid Build Coastguard Worker struct APInt::mu {
1739*9880d681SAndroid Build Coastguard Worker   APInt m;    ///< magic number
1740*9880d681SAndroid Build Coastguard Worker   bool a;     ///< add indicator
1741*9880d681SAndroid Build Coastguard Worker   unsigned s; ///< shift amount
1742*9880d681SAndroid Build Coastguard Worker };
1743*9880d681SAndroid Build Coastguard Worker 
1744*9880d681SAndroid Build Coastguard Worker inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; }
1745*9880d681SAndroid Build Coastguard Worker 
1746*9880d681SAndroid Build Coastguard Worker inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; }
1747*9880d681SAndroid Build Coastguard Worker 
1748*9880d681SAndroid Build Coastguard Worker inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) {
1749*9880d681SAndroid Build Coastguard Worker   I.print(OS, true);
1750*9880d681SAndroid Build Coastguard Worker   return OS;
1751*9880d681SAndroid Build Coastguard Worker }
1752*9880d681SAndroid Build Coastguard Worker 
1753*9880d681SAndroid Build Coastguard Worker namespace APIntOps {
1754*9880d681SAndroid Build Coastguard Worker 
1755*9880d681SAndroid Build Coastguard Worker /// \brief Determine the smaller of two APInts considered to be signed.
smin(const APInt & A,const APInt & B)1756*9880d681SAndroid Build Coastguard Worker inline const APInt &smin(const APInt &A, const APInt &B) {
1757*9880d681SAndroid Build Coastguard Worker   return A.slt(B) ? A : B;
1758*9880d681SAndroid Build Coastguard Worker }
1759*9880d681SAndroid Build Coastguard Worker 
1760*9880d681SAndroid Build Coastguard Worker /// \brief Determine the larger of two APInts considered to be signed.
smax(const APInt & A,const APInt & B)1761*9880d681SAndroid Build Coastguard Worker inline const APInt &smax(const APInt &A, const APInt &B) {
1762*9880d681SAndroid Build Coastguard Worker   return A.sgt(B) ? A : B;
1763*9880d681SAndroid Build Coastguard Worker }
1764*9880d681SAndroid Build Coastguard Worker 
1765*9880d681SAndroid Build Coastguard Worker /// \brief Determine the smaller of two APInts considered to be signed.
umin(const APInt & A,const APInt & B)1766*9880d681SAndroid Build Coastguard Worker inline const APInt &umin(const APInt &A, const APInt &B) {
1767*9880d681SAndroid Build Coastguard Worker   return A.ult(B) ? A : B;
1768*9880d681SAndroid Build Coastguard Worker }
1769*9880d681SAndroid Build Coastguard Worker 
1770*9880d681SAndroid Build Coastguard Worker /// \brief Determine the larger of two APInts considered to be unsigned.
umax(const APInt & A,const APInt & B)1771*9880d681SAndroid Build Coastguard Worker inline const APInt &umax(const APInt &A, const APInt &B) {
1772*9880d681SAndroid Build Coastguard Worker   return A.ugt(B) ? A : B;
1773*9880d681SAndroid Build Coastguard Worker }
1774*9880d681SAndroid Build Coastguard Worker 
1775*9880d681SAndroid Build Coastguard Worker /// \brief Check if the specified APInt has a N-bits unsigned integer value.
isIntN(unsigned N,const APInt & APIVal)1776*9880d681SAndroid Build Coastguard Worker inline bool isIntN(unsigned N, const APInt &APIVal) { return APIVal.isIntN(N); }
1777*9880d681SAndroid Build Coastguard Worker 
1778*9880d681SAndroid Build Coastguard Worker /// \brief Check if the specified APInt has a N-bits signed integer value.
isSignedIntN(unsigned N,const APInt & APIVal)1779*9880d681SAndroid Build Coastguard Worker inline bool isSignedIntN(unsigned N, const APInt &APIVal) {
1780*9880d681SAndroid Build Coastguard Worker   return APIVal.isSignedIntN(N);
1781*9880d681SAndroid Build Coastguard Worker }
1782*9880d681SAndroid Build Coastguard Worker 
1783*9880d681SAndroid Build Coastguard Worker /// \returns true if the argument APInt value is a sequence of ones starting at
1784*9880d681SAndroid Build Coastguard Worker /// the least significant bit with the remainder zero.
isMask(unsigned numBits,const APInt & APIVal)1785*9880d681SAndroid Build Coastguard Worker inline bool isMask(unsigned numBits, const APInt &APIVal) {
1786*9880d681SAndroid Build Coastguard Worker   return numBits <= APIVal.getBitWidth() &&
1787*9880d681SAndroid Build Coastguard Worker          APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
1788*9880d681SAndroid Build Coastguard Worker }
1789*9880d681SAndroid Build Coastguard Worker 
1790*9880d681SAndroid Build Coastguard Worker /// \returns true if the argument is a non-empty sequence of ones starting at
1791*9880d681SAndroid Build Coastguard Worker /// the least significant bit with the remainder zero (32 bit version).
1792*9880d681SAndroid Build Coastguard Worker /// Ex. isMask(0x0000FFFFU) == true.
isMask(const APInt & Value)1793*9880d681SAndroid Build Coastguard Worker inline bool isMask(const APInt &Value) {
1794*9880d681SAndroid Build Coastguard Worker   return (Value != 0) && ((Value + 1) & Value) == 0;
1795*9880d681SAndroid Build Coastguard Worker }
1796*9880d681SAndroid Build Coastguard Worker 
1797*9880d681SAndroid Build Coastguard Worker /// \brief Return true if the argument APInt value contains a sequence of ones
1798*9880d681SAndroid Build Coastguard Worker /// with the remainder zero.
isShiftedMask(unsigned numBits,const APInt & APIVal)1799*9880d681SAndroid Build Coastguard Worker inline bool isShiftedMask(unsigned numBits, const APInt &APIVal) {
1800*9880d681SAndroid Build Coastguard Worker   return isMask(numBits, (APIVal - APInt(numBits, 1)) | APIVal);
1801*9880d681SAndroid Build Coastguard Worker }
1802*9880d681SAndroid Build Coastguard Worker 
1803*9880d681SAndroid Build Coastguard Worker /// \brief Returns a byte-swapped representation of the specified APInt Value.
byteSwap(const APInt & APIVal)1804*9880d681SAndroid Build Coastguard Worker inline APInt byteSwap(const APInt &APIVal) { return APIVal.byteSwap(); }
1805*9880d681SAndroid Build Coastguard Worker 
1806*9880d681SAndroid Build Coastguard Worker /// \brief Returns the floor log base 2 of the specified APInt value.
logBase2(const APInt & APIVal)1807*9880d681SAndroid Build Coastguard Worker inline unsigned logBase2(const APInt &APIVal) { return APIVal.logBase2(); }
1808*9880d681SAndroid Build Coastguard Worker 
1809*9880d681SAndroid Build Coastguard Worker /// \brief Compute GCD of two APInt values.
1810*9880d681SAndroid Build Coastguard Worker ///
1811*9880d681SAndroid Build Coastguard Worker /// This function returns the greatest common divisor of the two APInt values
1812*9880d681SAndroid Build Coastguard Worker /// using Euclid's algorithm.
1813*9880d681SAndroid Build Coastguard Worker ///
1814*9880d681SAndroid Build Coastguard Worker /// \returns the greatest common divisor of Val1 and Val2
1815*9880d681SAndroid Build Coastguard Worker APInt GreatestCommonDivisor(const APInt &Val1, const APInt &Val2);
1816*9880d681SAndroid Build Coastguard Worker 
1817*9880d681SAndroid Build Coastguard Worker /// \brief Converts the given APInt to a double value.
1818*9880d681SAndroid Build Coastguard Worker ///
1819*9880d681SAndroid Build Coastguard Worker /// Treats the APInt as an unsigned value for conversion purposes.
RoundAPIntToDouble(const APInt & APIVal)1820*9880d681SAndroid Build Coastguard Worker inline double RoundAPIntToDouble(const APInt &APIVal) {
1821*9880d681SAndroid Build Coastguard Worker   return APIVal.roundToDouble();
1822*9880d681SAndroid Build Coastguard Worker }
1823*9880d681SAndroid Build Coastguard Worker 
1824*9880d681SAndroid Build Coastguard Worker /// \brief Converts the given APInt to a double value.
1825*9880d681SAndroid Build Coastguard Worker ///
1826*9880d681SAndroid Build Coastguard Worker /// Treats the APInt as a signed value for conversion purposes.
RoundSignedAPIntToDouble(const APInt & APIVal)1827*9880d681SAndroid Build Coastguard Worker inline double RoundSignedAPIntToDouble(const APInt &APIVal) {
1828*9880d681SAndroid Build Coastguard Worker   return APIVal.signedRoundToDouble();
1829*9880d681SAndroid Build Coastguard Worker }
1830*9880d681SAndroid Build Coastguard Worker 
1831*9880d681SAndroid Build Coastguard Worker /// \brief Converts the given APInt to a float vlalue.
RoundAPIntToFloat(const APInt & APIVal)1832*9880d681SAndroid Build Coastguard Worker inline float RoundAPIntToFloat(const APInt &APIVal) {
1833*9880d681SAndroid Build Coastguard Worker   return float(RoundAPIntToDouble(APIVal));
1834*9880d681SAndroid Build Coastguard Worker }
1835*9880d681SAndroid Build Coastguard Worker 
1836*9880d681SAndroid Build Coastguard Worker /// \brief Converts the given APInt to a float value.
1837*9880d681SAndroid Build Coastguard Worker ///
1838*9880d681SAndroid Build Coastguard Worker /// Treast the APInt as a signed value for conversion purposes.
RoundSignedAPIntToFloat(const APInt & APIVal)1839*9880d681SAndroid Build Coastguard Worker inline float RoundSignedAPIntToFloat(const APInt &APIVal) {
1840*9880d681SAndroid Build Coastguard Worker   return float(APIVal.signedRoundToDouble());
1841*9880d681SAndroid Build Coastguard Worker }
1842*9880d681SAndroid Build Coastguard Worker 
1843*9880d681SAndroid Build Coastguard Worker /// \brief Converts the given double value into a APInt.
1844*9880d681SAndroid Build Coastguard Worker ///
1845*9880d681SAndroid Build Coastguard Worker /// This function convert a double value to an APInt value.
1846*9880d681SAndroid Build Coastguard Worker APInt RoundDoubleToAPInt(double Double, unsigned width);
1847*9880d681SAndroid Build Coastguard Worker 
1848*9880d681SAndroid Build Coastguard Worker /// \brief Converts a float value into a APInt.
1849*9880d681SAndroid Build Coastguard Worker ///
1850*9880d681SAndroid Build Coastguard Worker /// Converts a float value into an APInt value.
RoundFloatToAPInt(float Float,unsigned width)1851*9880d681SAndroid Build Coastguard Worker inline APInt RoundFloatToAPInt(float Float, unsigned width) {
1852*9880d681SAndroid Build Coastguard Worker   return RoundDoubleToAPInt(double(Float), width);
1853*9880d681SAndroid Build Coastguard Worker }
1854*9880d681SAndroid Build Coastguard Worker 
1855*9880d681SAndroid Build Coastguard Worker /// \brief Arithmetic right-shift function.
1856*9880d681SAndroid Build Coastguard Worker ///
1857*9880d681SAndroid Build Coastguard Worker /// Arithmetic right-shift the APInt by shiftAmt.
ashr(const APInt & LHS,unsigned shiftAmt)1858*9880d681SAndroid Build Coastguard Worker inline APInt ashr(const APInt &LHS, unsigned shiftAmt) {
1859*9880d681SAndroid Build Coastguard Worker   return LHS.ashr(shiftAmt);
1860*9880d681SAndroid Build Coastguard Worker }
1861*9880d681SAndroid Build Coastguard Worker 
1862*9880d681SAndroid Build Coastguard Worker /// \brief Logical right-shift function.
1863*9880d681SAndroid Build Coastguard Worker ///
1864*9880d681SAndroid Build Coastguard Worker /// Logical right-shift the APInt by shiftAmt.
lshr(const APInt & LHS,unsigned shiftAmt)1865*9880d681SAndroid Build Coastguard Worker inline APInt lshr(const APInt &LHS, unsigned shiftAmt) {
1866*9880d681SAndroid Build Coastguard Worker   return LHS.lshr(shiftAmt);
1867*9880d681SAndroid Build Coastguard Worker }
1868*9880d681SAndroid Build Coastguard Worker 
1869*9880d681SAndroid Build Coastguard Worker /// \brief Left-shift function.
1870*9880d681SAndroid Build Coastguard Worker ///
1871*9880d681SAndroid Build Coastguard Worker /// Left-shift the APInt by shiftAmt.
shl(const APInt & LHS,unsigned shiftAmt)1872*9880d681SAndroid Build Coastguard Worker inline APInt shl(const APInt &LHS, unsigned shiftAmt) {
1873*9880d681SAndroid Build Coastguard Worker   return LHS.shl(shiftAmt);
1874*9880d681SAndroid Build Coastguard Worker }
1875*9880d681SAndroid Build Coastguard Worker 
1876*9880d681SAndroid Build Coastguard Worker /// \brief Signed division function for APInt.
1877*9880d681SAndroid Build Coastguard Worker ///
1878*9880d681SAndroid Build Coastguard Worker /// Signed divide APInt LHS by APInt RHS.
sdiv(const APInt & LHS,const APInt & RHS)1879*9880d681SAndroid Build Coastguard Worker inline APInt sdiv(const APInt &LHS, const APInt &RHS) { return LHS.sdiv(RHS); }
1880*9880d681SAndroid Build Coastguard Worker 
1881*9880d681SAndroid Build Coastguard Worker /// \brief Unsigned division function for APInt.
1882*9880d681SAndroid Build Coastguard Worker ///
1883*9880d681SAndroid Build Coastguard Worker /// Unsigned divide APInt LHS by APInt RHS.
udiv(const APInt & LHS,const APInt & RHS)1884*9880d681SAndroid Build Coastguard Worker inline APInt udiv(const APInt &LHS, const APInt &RHS) { return LHS.udiv(RHS); }
1885*9880d681SAndroid Build Coastguard Worker 
1886*9880d681SAndroid Build Coastguard Worker /// \brief Function for signed remainder operation.
1887*9880d681SAndroid Build Coastguard Worker ///
1888*9880d681SAndroid Build Coastguard Worker /// Signed remainder operation on APInt.
srem(const APInt & LHS,const APInt & RHS)1889*9880d681SAndroid Build Coastguard Worker inline APInt srem(const APInt &LHS, const APInt &RHS) { return LHS.srem(RHS); }
1890*9880d681SAndroid Build Coastguard Worker 
1891*9880d681SAndroid Build Coastguard Worker /// \brief Function for unsigned remainder operation.
1892*9880d681SAndroid Build Coastguard Worker ///
1893*9880d681SAndroid Build Coastguard Worker /// Unsigned remainder operation on APInt.
urem(const APInt & LHS,const APInt & RHS)1894*9880d681SAndroid Build Coastguard Worker inline APInt urem(const APInt &LHS, const APInt &RHS) { return LHS.urem(RHS); }
1895*9880d681SAndroid Build Coastguard Worker 
1896*9880d681SAndroid Build Coastguard Worker /// \brief Function for multiplication operation.
1897*9880d681SAndroid Build Coastguard Worker ///
1898*9880d681SAndroid Build Coastguard Worker /// Performs multiplication on APInt values.
mul(const APInt & LHS,const APInt & RHS)1899*9880d681SAndroid Build Coastguard Worker inline APInt mul(const APInt &LHS, const APInt &RHS) { return LHS * RHS; }
1900*9880d681SAndroid Build Coastguard Worker 
1901*9880d681SAndroid Build Coastguard Worker /// \brief Function for addition operation.
1902*9880d681SAndroid Build Coastguard Worker ///
1903*9880d681SAndroid Build Coastguard Worker /// Performs addition on APInt values.
add(const APInt & LHS,const APInt & RHS)1904*9880d681SAndroid Build Coastguard Worker inline APInt add(const APInt &LHS, const APInt &RHS) { return LHS + RHS; }
1905*9880d681SAndroid Build Coastguard Worker 
1906*9880d681SAndroid Build Coastguard Worker /// \brief Function for subtraction operation.
1907*9880d681SAndroid Build Coastguard Worker ///
1908*9880d681SAndroid Build Coastguard Worker /// Performs subtraction on APInt values.
sub(const APInt & LHS,const APInt & RHS)1909*9880d681SAndroid Build Coastguard Worker inline APInt sub(const APInt &LHS, const APInt &RHS) { return LHS - RHS; }
1910*9880d681SAndroid Build Coastguard Worker 
1911*9880d681SAndroid Build Coastguard Worker /// \brief Bitwise AND function for APInt.
1912*9880d681SAndroid Build Coastguard Worker ///
1913*9880d681SAndroid Build Coastguard Worker /// Performs bitwise AND operation on APInt LHS and
1914*9880d681SAndroid Build Coastguard Worker /// APInt RHS.
And(const APInt & LHS,const APInt & RHS)1915*9880d681SAndroid Build Coastguard Worker inline APInt And(const APInt &LHS, const APInt &RHS) { return LHS & RHS; }
1916*9880d681SAndroid Build Coastguard Worker 
1917*9880d681SAndroid Build Coastguard Worker /// \brief Bitwise OR function for APInt.
1918*9880d681SAndroid Build Coastguard Worker ///
1919*9880d681SAndroid Build Coastguard Worker /// Performs bitwise OR operation on APInt LHS and APInt RHS.
Or(const APInt & LHS,const APInt & RHS)1920*9880d681SAndroid Build Coastguard Worker inline APInt Or(const APInt &LHS, const APInt &RHS) { return LHS | RHS; }
1921*9880d681SAndroid Build Coastguard Worker 
1922*9880d681SAndroid Build Coastguard Worker /// \brief Bitwise XOR function for APInt.
1923*9880d681SAndroid Build Coastguard Worker ///
1924*9880d681SAndroid Build Coastguard Worker /// Performs bitwise XOR operation on APInt.
Xor(const APInt & LHS,const APInt & RHS)1925*9880d681SAndroid Build Coastguard Worker inline APInt Xor(const APInt &LHS, const APInt &RHS) { return LHS ^ RHS; }
1926*9880d681SAndroid Build Coastguard Worker 
1927*9880d681SAndroid Build Coastguard Worker /// \brief Bitwise complement function.
1928*9880d681SAndroid Build Coastguard Worker ///
1929*9880d681SAndroid Build Coastguard Worker /// Performs a bitwise complement operation on APInt.
Not(const APInt & APIVal)1930*9880d681SAndroid Build Coastguard Worker inline APInt Not(const APInt &APIVal) { return ~APIVal; }
1931*9880d681SAndroid Build Coastguard Worker 
1932*9880d681SAndroid Build Coastguard Worker } // End of APIntOps namespace
1933*9880d681SAndroid Build Coastguard Worker 
1934*9880d681SAndroid Build Coastguard Worker // See friend declaration above. This additional declaration is required in
1935*9880d681SAndroid Build Coastguard Worker // order to compile LLVM with IBM xlC compiler.
1936*9880d681SAndroid Build Coastguard Worker hash_code hash_value(const APInt &Arg);
1937*9880d681SAndroid Build Coastguard Worker } // End of llvm namespace
1938*9880d681SAndroid Build Coastguard Worker 
1939*9880d681SAndroid Build Coastguard Worker #endif
1940