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