remove a dead APInt ctor.
[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 // This file implements a class to represent arbitrary precision integral
11 // constant values and operations on them.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_APINT_H
16 #define LLVM_APINT_H
17
18 #include "llvm/Support/DataTypes.h"
19 #include <cassert>
20 #include <string>
21
22 namespace llvm {
23   class Serializer;
24   class Deserializer;
25   class FoldingSetNodeID;
26   
27   /* An unsigned host type used as a single part of a multi-part
28      bignum.  */
29   typedef uint64_t integerPart;
30
31   const unsigned int host_char_bit = 8;
32   const unsigned int integerPartWidth = host_char_bit *
33     static_cast<unsigned int>(sizeof(integerPart));
34
35 //===----------------------------------------------------------------------===//
36 //                              APInt Class
37 //===----------------------------------------------------------------------===//
38
39 /// APInt - This class represents arbitrary precision constant integral values.
40 /// It is a functional replacement for common case unsigned integer type like 
41 /// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width 
42 /// integer sizes and large integer value types such as 3-bits, 15-bits, or more
43 /// than 64-bits of precision. APInt provides a variety of arithmetic operators 
44 /// and methods to manipulate integer values of any bit-width. It supports both
45 /// the typical integer arithmetic and comparison operations as well as bitwise
46 /// manipulation.
47 ///
48 /// The class has several invariants worth noting:
49 ///   * All bit, byte, and word positions are zero-based.
50 ///   * Once the bit width is set, it doesn't change except by the Truncate, 
51 ///     SignExtend, or ZeroExtend operations.
52 ///   * All binary operators must be on APInt instances of the same bit width.
53 ///     Attempting to use these operators on instances with different bit 
54 ///     widths will yield an assertion.
55 ///   * The value is stored canonically as an unsigned value. For operations
56 ///     where it makes a difference, there are both signed and unsigned variants
57 ///     of the operation. For example, sdiv and udiv. However, because the bit
58 ///     widths must be the same, operations such as Mul and Add produce the same
59 ///     results regardless of whether the values are interpreted as signed or
60 ///     not.
61 ///   * In general, the class tries to follow the style of computation that LLVM
62 ///     uses in its IR. This simplifies its use for LLVM.
63 ///
64 /// @brief Class for arbitrary precision integers.
65 class APInt {
66
67   uint32_t BitWidth;      ///< The number of bits in this APInt.
68
69   /// This union is used to store the integer value. When the
70   /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
71   union {
72     uint64_t VAL;    ///< Used to store the <= 64 bits integer value.
73     uint64_t *pVal;  ///< Used to store the >64 bits integer value.
74   };
75
76   /// This enum is used to hold the constants we needed for APInt.
77   enum {
78     /// Bits in a word
79     APINT_BITS_PER_WORD = static_cast<unsigned int>(sizeof(uint64_t)) * 8,
80     /// Byte size of a word
81     APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
82   };
83
84   /// This constructor is used only internally for speed of construction of
85   /// temporaries. It is unsafe for general use so it is not public.
86   /// @brief Fast internal constructor
87   APInt(uint64_t* val, uint32_t bits) : BitWidth(bits), pVal(val) { }
88
89   /// @returns true if the number of bits <= 64, false otherwise.
90   /// @brief Determine if this APInt just has one word to store value.
91   bool isSingleWord() const { 
92     return BitWidth <= APINT_BITS_PER_WORD; 
93   }
94
95   /// @returns the word position for the specified bit position.
96   /// @brief Determine which word a bit is in.
97   static uint32_t whichWord(uint32_t bitPosition) { 
98     return bitPosition / APINT_BITS_PER_WORD; 
99   }
100
101   /// @returns the bit position in a word for the specified bit position 
102   /// in the APInt.
103   /// @brief Determine which bit in a word a bit is in.
104   static uint32_t whichBit(uint32_t bitPosition) { 
105     return bitPosition % APINT_BITS_PER_WORD; 
106   }
107
108   /// This method generates and returns a uint64_t (word) mask for a single 
109   /// bit at a specific bit position. This is used to mask the bit in the 
110   /// corresponding word.
111   /// @returns a uint64_t with only bit at "whichBit(bitPosition)" set
112   /// @brief Get a single bit mask.
113   static uint64_t maskBit(uint32_t bitPosition) { 
114     return 1ULL << whichBit(bitPosition); 
115   }
116
117   /// This method is used internally to clear the to "N" bits in the high order
118   /// word that are not used by the APInt. This is needed after the most 
119   /// significant word is assigned a value to ensure that those bits are 
120   /// zero'd out.
121   /// @brief Clear unused high order bits
122   APInt& clearUnusedBits() {
123     // Compute how many bits are used in the final word
124     uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
125     if (wordBits == 0)
126       // If all bits are used, we want to leave the value alone. This also
127       // avoids the undefined behavior of >> when the shift is the same size as
128       // the word size (64).
129       return *this;
130
131     // Mask out the high bits.
132     uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
133     if (isSingleWord())
134       VAL &= mask;
135     else
136       pVal[getNumWords() - 1] &= mask;
137     return *this;
138   }
139
140   /// @returns the corresponding word for the specified bit position.
141   /// @brief Get the word corresponding to a bit position
142   uint64_t getWord(uint32_t bitPosition) const { 
143     return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; 
144   }
145
146   /// This is used by the constructors that take string arguments.
147   /// @brief Convert a char array into an APInt
148   void fromString(uint32_t numBits, const char *strStart, uint32_t slen, 
149                   uint8_t radix);
150
151   /// This is used by the toString method to divide by the radix. It simply
152   /// provides a more convenient form of divide for internal use since KnuthDiv
153   /// has specific constraints on its inputs. If those constraints are not met
154   /// then it provides a simpler form of divide.
155   /// @brief An internal division function for dividing APInts.
156   static void divide(const APInt LHS, uint32_t lhsWords, 
157                      const APInt &RHS, uint32_t rhsWords,
158                      APInt *Quotient, APInt *Remainder);
159
160 public:
161   /// @name Constructors
162   /// @{
163   /// If isSigned is true then val is treated as if it were a signed value
164   /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
165   /// will be done. Otherwise, no sign extension occurs (high order bits beyond
166   /// the range of val are zero filled).
167   /// @param numBits the bit width of the constructed APInt
168   /// @param val the initial value of the APInt
169   /// @param isSigned how to treat signedness of val
170   /// @brief Create a new APInt of numBits width, initialized as val.
171   APInt(uint32_t numBits, uint64_t val, bool isSigned = false);
172
173   /// Note that numWords can be smaller or larger than the corresponding bit
174   /// width but any extraneous bits will be dropped.
175   /// @param numBits the bit width of the constructed APInt
176   /// @param numWords the number of words in bigVal
177   /// @param bigVal a sequence of words to form the initial value of the APInt
178   /// @brief Construct an APInt of numBits width, initialized as bigVal[].
179   APInt(uint32_t numBits, uint32_t numWords, const uint64_t bigVal[]);
180
181   /// This constructor interprets the slen characters starting at StrStart as
182   /// a string in the given radix. The interpretation stops when the first 
183   /// character that is not suitable for the radix is encountered. Acceptable
184   /// radix values are 2, 8, 10 and 16. It is an error for the value implied by
185   /// the string to require more bits than numBits.
186   /// @param numBits the bit width of the constructed APInt
187   /// @param strStart the start of the string to be interpreted
188   /// @param slen the maximum number of characters to interpret
189   /// @param radix the radix to use for the conversion
190   /// @brief Construct an APInt from a string representation.
191   APInt(uint32_t numBits, const char strStart[], uint32_t slen, uint8_t radix);
192
193   /// Simply makes *this a copy of that.
194   /// @brief Copy Constructor.
195   APInt(const APInt& that);
196
197   /// @brief Destructor.
198   ~APInt();
199   
200   /// Default constructor that creates an uninitialized APInt.  This is useful
201   ///  for object deserialization (pair this with the static method Read).
202   explicit APInt() : BitWidth(1) {}
203   
204   /// Profile - Used to insert APInt objects, or objects that contain APInt 
205   ///  objects, into FoldingSets.
206   void Profile(FoldingSetNodeID& id) const;
207   
208   /// @brief Used by the Bitcode serializer to emit APInts to Bitcode.
209   void Emit(Serializer& S) const;
210   
211   /// @brief Used by the Bitcode deserializer to deserialize APInts.
212   void Read(Deserializer& D);
213
214   /// @}
215   /// @name Value Tests
216   /// @{
217   /// This tests the high bit of this APInt to determine if it is set.
218   /// @returns true if this APInt is negative, false otherwise
219   /// @brief Determine sign of this APInt.
220   bool isNegative() const {
221     return (*this)[BitWidth - 1];
222   }
223
224   /// This tests the high bit of the APInt to determine if it is unset.
225   /// @brief Determine if this APInt Value is non-negative (>= 0)
226   bool isNonNegative() const {
227     return !isNegative();
228   }
229
230   /// This tests if the value of this APInt is positive (> 0). Note
231   /// that 0 is not a positive value.
232   /// @returns true if this APInt is positive.
233   /// @brief Determine if this APInt Value is positive.
234   bool isStrictlyPositive() const {
235     return isNonNegative() && (*this) != 0;
236   }
237
238   /// This checks to see if the value has all bits of the APInt are set or not.
239   /// @brief Determine if all bits are set
240   bool isAllOnesValue() const {
241     return countPopulation() == BitWidth;
242   }
243
244   /// This checks to see if the value of this APInt is the maximum unsigned
245   /// value for the APInt's bit width.
246   /// @brief Determine if this is the largest unsigned value.
247   bool isMaxValue() const {
248     return countPopulation() == BitWidth;
249   }
250
251   /// This checks to see if the value of this APInt is the maximum signed
252   /// value for the APInt's bit width.
253   /// @brief Determine if this is the largest signed value.
254   bool isMaxSignedValue() const {
255     return BitWidth == 1 ? VAL == 0 :
256                           !isNegative() && countPopulation() == BitWidth - 1;
257   }
258
259   /// This checks to see if the value of this APInt is the minimum unsigned
260   /// value for the APInt's bit width.
261   /// @brief Determine if this is the smallest unsigned value.
262   bool isMinValue() const {
263     return countPopulation() == 0;
264   }
265
266   /// This checks to see if the value of this APInt is the minimum signed
267   /// value for the APInt's bit width.
268   /// @brief Determine if this is the smallest signed value.
269   bool isMinSignedValue() const {
270     return BitWidth == 1 ? VAL == 1 :
271                            isNegative() && countPopulation() == 1;
272   }
273
274   /// @brief Check if this APInt has an N-bits unsigned integer value.
275   bool isIntN(uint32_t N) const {
276     assert(N && "N == 0 ???");
277     if (isSingleWord()) {
278       return VAL == (VAL & (~0ULL >> (64 - N)));
279     } else {
280       APInt Tmp(N, getNumWords(), pVal);
281       return Tmp == (*this);
282     }
283   }
284
285   /// @brief Check if this APInt has an N-bits signed integer value.
286   bool isSignedIntN(uint32_t N) const {
287     assert(N && "N == 0 ???");
288     return getMinSignedBits() <= N;
289   }
290
291   /// @returns true if the argument APInt value is a power of two > 0.
292   bool isPowerOf2() const; 
293
294   /// isSignBit - Return true if this is the value returned by getSignBit.
295   bool isSignBit() const { return isMinSignedValue(); }
296   
297   /// This converts the APInt to a boolean value as a test against zero.
298   /// @brief Boolean conversion function. 
299   bool getBoolValue() const {
300     return *this != 0;
301   }
302
303   /// getLimitedValue - If this value is smaller than the specified limit,
304   /// return it, otherwise return the limit value.  This causes the value
305   /// to saturate to the limit.
306   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
307     return (getActiveBits() > 64 || getZExtValue() > Limit) ?
308       Limit :  getZExtValue();
309   }
310
311   /// @}
312   /// @name Value Generators
313   /// @{
314   /// @brief Gets maximum unsigned value of APInt for specific bit width.
315   static APInt getMaxValue(uint32_t numBits) {
316     return APInt(numBits, 0).set();
317   }
318
319   /// @brief Gets maximum signed value of APInt for a specific bit width.
320   static APInt getSignedMaxValue(uint32_t numBits) {
321     return APInt(numBits, 0).set().clear(numBits - 1);
322   }
323
324   /// @brief Gets minimum unsigned value of APInt for a specific bit width.
325   static APInt getMinValue(uint32_t numBits) {
326     return APInt(numBits, 0);
327   }
328
329   /// @brief Gets minimum signed value of APInt for a specific bit width.
330   static APInt getSignedMinValue(uint32_t numBits) {
331     return APInt(numBits, 0).set(numBits - 1);
332   }
333
334   /// getSignBit - This is just a wrapper function of getSignedMinValue(), and
335   /// it helps code readability when we want to get a SignBit.
336   /// @brief Get the SignBit for a specific bit width.
337   static APInt getSignBit(uint32_t BitWidth) {
338     return getSignedMinValue(BitWidth);
339   }
340
341   /// @returns the all-ones value for an APInt of the specified bit-width.
342   /// @brief Get the all-ones value.
343   static APInt getAllOnesValue(uint32_t numBits) {
344     return APInt(numBits, 0).set();
345   }
346
347   /// @returns the '0' value for an APInt of the specified bit-width.
348   /// @brief Get the '0' value.
349   static APInt getNullValue(uint32_t numBits) {
350     return APInt(numBits, 0);
351   }
352
353   /// Get an APInt with the same BitWidth as this APInt, just zero mask
354   /// the low bits and right shift to the least significant bit.
355   /// @returns the high "numBits" bits of this APInt.
356   APInt getHiBits(uint32_t numBits) const;
357
358   /// Get an APInt with the same BitWidth as this APInt, just zero mask
359   /// the high bits.
360   /// @returns the low "numBits" bits of this APInt.
361   APInt getLoBits(uint32_t numBits) const;
362
363   /// Constructs an APInt value that has a contiguous range of bits set. The
364   /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
365   /// bits will be zero. For example, with parameters(32, 0, 16) you would get
366   /// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For
367   /// example, with parameters (32, 28, 4), you would get 0xF000000F. 
368   /// @param numBits the intended bit width of the result
369   /// @param loBit the index of the lowest bit set.
370   /// @param hiBit the index of the highest bit set.
371   /// @returns An APInt value with the requested bits set.
372   /// @brief Get a value with a block of bits set.
373   static APInt getBitsSet(uint32_t numBits, uint32_t loBit, uint32_t hiBit) {
374     assert(hiBit <= numBits && "hiBit out of range");
375     assert(loBit < numBits && "loBit out of range");
376     if (hiBit < loBit)
377       return getLowBitsSet(numBits, hiBit) |
378              getHighBitsSet(numBits, numBits-loBit);
379     return getLowBitsSet(numBits, hiBit-loBit).shl(loBit);
380   }
381
382   /// Constructs an APInt value that has the top hiBitsSet bits set.
383   /// @param numBits the bitwidth of the result
384   /// @param hiBitsSet the number of high-order bits set in the result.
385   /// @brief Get a value with high bits set
386   static APInt getHighBitsSet(uint32_t numBits, uint32_t hiBitsSet) {
387     assert(hiBitsSet <= numBits && "Too many bits to set!");
388     // Handle a degenerate case, to avoid shifting by word size
389     if (hiBitsSet == 0)
390       return APInt(numBits, 0);
391     uint32_t shiftAmt = numBits - hiBitsSet;
392     // For small values, return quickly
393     if (numBits <= APINT_BITS_PER_WORD)
394       return APInt(numBits, ~0ULL << shiftAmt);
395     return (~APInt(numBits, 0)).shl(shiftAmt);
396   }
397
398   /// Constructs an APInt value that has the bottom loBitsSet bits set.
399   /// @param numBits the bitwidth of the result
400   /// @param loBitsSet the number of low-order bits set in the result.
401   /// @brief Get a value with low bits set
402   static APInt getLowBitsSet(uint32_t numBits, uint32_t loBitsSet) {
403     assert(loBitsSet <= numBits && "Too many bits to set!");
404     // Handle a degenerate case, to avoid shifting by word size
405     if (loBitsSet == 0)
406       return APInt(numBits, 0);
407     if (loBitsSet == APINT_BITS_PER_WORD)
408       return APInt(numBits, -1ULL);
409     // For small values, return quickly
410     if (numBits < APINT_BITS_PER_WORD)
411       return APInt(numBits, (1ULL << loBitsSet) - 1);
412     return (~APInt(numBits, 0)).lshr(numBits - loBitsSet);
413   }
414
415   /// The hash value is computed as the sum of the words and the bit width.
416   /// @returns A hash value computed from the sum of the APInt words.
417   /// @brief Get a hash value based on this APInt
418   uint64_t getHashValue() const;
419
420   /// This function returns a pointer to the internal storage of the APInt. 
421   /// This is useful for writing out the APInt in binary form without any
422   /// conversions.
423   const uint64_t* getRawData() const {
424     if (isSingleWord())
425       return &VAL;
426     return &pVal[0];
427   }
428
429   /// @}
430   /// @name Unary Operators
431   /// @{
432   /// @returns a new APInt value representing *this incremented by one
433   /// @brief Postfix increment operator.
434   const APInt operator++(int) {
435     APInt API(*this);
436     ++(*this);
437     return API;
438   }
439
440   /// @returns *this incremented by one
441   /// @brief Prefix increment operator.
442   APInt& operator++();
443
444   /// @returns a new APInt representing *this decremented by one.
445   /// @brief Postfix decrement operator. 
446   const APInt operator--(int) {
447     APInt API(*this);
448     --(*this);
449     return API;
450   }
451
452   /// @returns *this decremented by one.
453   /// @brief Prefix decrement operator. 
454   APInt& operator--();
455
456   /// Performs a bitwise complement operation on this APInt. 
457   /// @returns an APInt that is the bitwise complement of *this
458   /// @brief Unary bitwise complement operator. 
459   APInt operator~() const;
460
461   /// Negates *this using two's complement logic.
462   /// @returns An APInt value representing the negation of *this.
463   /// @brief Unary negation operator
464   APInt operator-() const {
465     return APInt(BitWidth, 0) - (*this);
466   }
467
468   /// Performs logical negation operation on this APInt.
469   /// @returns true if *this is zero, false otherwise.
470   /// @brief Logical negation operator. 
471   bool operator !() const;
472
473   /// @}
474   /// @name Assignment Operators
475   /// @{
476   /// @returns *this after assignment of RHS.
477   /// @brief Copy assignment operator. 
478   APInt& operator=(const APInt& RHS);
479
480   /// The RHS value is assigned to *this. If the significant bits in RHS exceed
481   /// the bit width, the excess bits are truncated. If the bit width is larger
482   /// than 64, the value is zero filled in the unspecified high order bits.
483   /// @returns *this after assignment of RHS value.
484   /// @brief Assignment operator. 
485   APInt& operator=(uint64_t RHS);
486
487   /// Performs a bitwise AND operation on this APInt and RHS. The result is
488   /// assigned to *this. 
489   /// @returns *this after ANDing with RHS.
490   /// @brief Bitwise AND assignment operator. 
491   APInt& operator&=(const APInt& RHS);
492
493   /// Performs a bitwise OR operation on this APInt and RHS. The result is 
494   /// assigned *this;
495   /// @returns *this after ORing with RHS.
496   /// @brief Bitwise OR assignment operator. 
497   APInt& operator|=(const APInt& RHS);
498
499   /// Performs a bitwise XOR operation on this APInt and RHS. The result is
500   /// assigned to *this.
501   /// @returns *this after XORing with RHS.
502   /// @brief Bitwise XOR assignment operator. 
503   APInt& operator^=(const APInt& RHS);
504
505   /// Multiplies this APInt by RHS and assigns the result to *this.
506   /// @returns *this
507   /// @brief Multiplication assignment operator. 
508   APInt& operator*=(const APInt& RHS);
509
510   /// Adds RHS to *this and assigns the result to *this.
511   /// @returns *this
512   /// @brief Addition assignment operator. 
513   APInt& operator+=(const APInt& RHS);
514
515   /// Subtracts RHS from *this and assigns the result to *this.
516   /// @returns *this
517   /// @brief Subtraction assignment operator. 
518   APInt& operator-=(const APInt& RHS);
519
520   /// Shifts *this left by shiftAmt and assigns the result to *this.
521   /// @returns *this after shifting left by shiftAmt
522   /// @brief Left-shift assignment function.
523   APInt& operator<<=(uint32_t shiftAmt) {
524     *this = shl(shiftAmt);
525     return *this;
526   }
527
528   /// @}
529   /// @name Binary Operators
530   /// @{
531   /// Performs a bitwise AND operation on *this and RHS.
532   /// @returns An APInt value representing the bitwise AND of *this and RHS.
533   /// @brief Bitwise AND operator. 
534   APInt operator&(const APInt& RHS) const;
535   APInt And(const APInt& RHS) const {
536     return this->operator&(RHS);
537   }
538
539   /// Performs a bitwise OR operation on *this and RHS.
540   /// @returns An APInt value representing the bitwise OR of *this and RHS.
541   /// @brief Bitwise OR operator. 
542   APInt operator|(const APInt& RHS) const;
543   APInt Or(const APInt& RHS) const {
544     return this->operator|(RHS);
545   }
546
547   /// Performs a bitwise XOR operation on *this and RHS.
548   /// @returns An APInt value representing the bitwise XOR of *this and RHS.
549   /// @brief Bitwise XOR operator. 
550   APInt operator^(const APInt& RHS) const;
551   APInt Xor(const APInt& RHS) const {
552     return this->operator^(RHS);
553   }
554
555   /// Multiplies this APInt by RHS and returns the result.
556   /// @brief Multiplication operator. 
557   APInt operator*(const APInt& RHS) const;
558
559   /// Adds RHS to this APInt and returns the result.
560   /// @brief Addition operator. 
561   APInt operator+(const APInt& RHS) const;
562   APInt operator+(uint64_t RHS) const {
563     return (*this) + APInt(BitWidth, RHS);
564   }
565
566   /// Subtracts RHS from this APInt and returns the result.
567   /// @brief Subtraction operator. 
568   APInt operator-(const APInt& RHS) const;
569   APInt operator-(uint64_t RHS) const {
570     return (*this) - APInt(BitWidth, RHS);
571   }
572   
573   APInt operator<<(unsigned Bits) const {
574     return shl(Bits);
575   }
576
577   APInt operator<<(const APInt &Bits) const {
578     return shl(Bits);
579   }
580
581   /// Arithmetic right-shift this APInt by shiftAmt.
582   /// @brief Arithmetic right-shift function.
583   APInt ashr(uint32_t shiftAmt) const;
584
585   /// Logical right-shift this APInt by shiftAmt.
586   /// @brief Logical right-shift function.
587   APInt lshr(uint32_t shiftAmt) const;
588
589   /// Left-shift this APInt by shiftAmt.
590   /// @brief Left-shift function.
591   APInt shl(uint32_t shiftAmt) const;
592
593   /// @brief Rotate left by rotateAmt.
594   APInt rotl(uint32_t rotateAmt) const;
595
596   /// @brief Rotate right by rotateAmt.
597   APInt rotr(uint32_t rotateAmt) const;
598
599   /// Arithmetic right-shift this APInt by shiftAmt.
600   /// @brief Arithmetic right-shift function.
601   APInt ashr(const APInt &shiftAmt) const;
602
603   /// Logical right-shift this APInt by shiftAmt.
604   /// @brief Logical right-shift function.
605   APInt lshr(const APInt &shiftAmt) const;
606
607   /// Left-shift this APInt by shiftAmt.
608   /// @brief Left-shift function.
609   APInt shl(const APInt &shiftAmt) const;
610
611   /// @brief Rotate left by rotateAmt.
612   APInt rotl(const APInt &rotateAmt) const;
613
614   /// @brief Rotate right by rotateAmt.
615   APInt rotr(const APInt &rotateAmt) const;
616
617   /// Perform an unsigned divide operation on this APInt by RHS. Both this and
618   /// RHS are treated as unsigned quantities for purposes of this division.
619   /// @returns a new APInt value containing the division result
620   /// @brief Unsigned division operation.
621   APInt udiv(const APInt& RHS) const;
622
623   /// Signed divide this APInt by APInt RHS.
624   /// @brief Signed division function for APInt.
625   APInt sdiv(const APInt& RHS) const {
626     if (isNegative())
627       if (RHS.isNegative())
628         return (-(*this)).udiv(-RHS);
629       else
630         return -((-(*this)).udiv(RHS));
631     else if (RHS.isNegative())
632       return -(this->udiv(-RHS));
633     return this->udiv(RHS);
634   }
635
636   /// Perform an unsigned remainder operation on this APInt with RHS being the
637   /// divisor. Both this and RHS are treated as unsigned quantities for purposes
638   /// of this operation. Note that this is a true remainder operation and not
639   /// a modulo operation because the sign follows the sign of the dividend
640   /// which is *this.
641   /// @returns a new APInt value containing the remainder result
642   /// @brief Unsigned remainder operation.
643   APInt urem(const APInt& RHS) const;
644
645   /// Signed remainder operation on APInt.
646   /// @brief Function for signed remainder operation.
647   APInt srem(const APInt& RHS) const {
648     if (isNegative())
649       if (RHS.isNegative())
650         return -((-(*this)).urem(-RHS));
651       else
652         return -((-(*this)).urem(RHS));
653     else if (RHS.isNegative())
654       return this->urem(-RHS);
655     return this->urem(RHS);
656   }
657
658   /// Sometimes it is convenient to divide two APInt values and obtain both the
659   /// quotient and remainder. This function does both operations in the same
660   /// computation making it a little more efficient. The pair of input arguments
661   /// may overlap with the pair of output arguments. It is safe to call
662   /// udivrem(X, Y, X, Y), for example.
663   /// @brief Dual division/remainder interface.
664   static void udivrem(const APInt &LHS, const APInt &RHS, 
665                       APInt &Quotient, APInt &Remainder);
666
667   static void sdivrem(const APInt &LHS, const APInt &RHS,
668                       APInt &Quotient, APInt &Remainder)
669   {
670     if (LHS.isNegative()) {
671       if (RHS.isNegative())
672         APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
673       else
674         APInt::udivrem(-LHS, RHS, Quotient, Remainder);
675       Quotient = -Quotient;
676       Remainder = -Remainder;
677     } else if (RHS.isNegative()) {
678       APInt::udivrem(LHS, -RHS, Quotient, Remainder);
679       Quotient = -Quotient;
680     } else {
681       APInt::udivrem(LHS, RHS, Quotient, Remainder);
682     }
683   }
684
685   /// @returns the bit value at bitPosition
686   /// @brief Array-indexing support.
687   bool operator[](uint32_t bitPosition) const;
688
689   /// @}
690   /// @name Comparison Operators
691   /// @{
692   /// Compares this APInt with RHS for the validity of the equality
693   /// relationship.
694   /// @brief Equality operator. 
695   bool operator==(const APInt& RHS) const;
696
697   /// Compares this APInt with a uint64_t for the validity of the equality 
698   /// relationship.
699   /// @returns true if *this == Val
700   /// @brief Equality operator.
701   bool operator==(uint64_t Val) const;
702
703   /// Compares this APInt with RHS for the validity of the equality
704   /// relationship.
705   /// @returns true if *this == Val
706   /// @brief Equality comparison.
707   bool eq(const APInt &RHS) const {
708     return (*this) == RHS; 
709   }
710
711   /// Compares this APInt with RHS for the validity of the inequality
712   /// relationship.
713   /// @returns true if *this != Val
714   /// @brief Inequality operator. 
715   bool operator!=(const APInt& RHS) const {
716     return !((*this) == RHS);
717   }
718
719   /// Compares this APInt with a uint64_t for the validity of the inequality 
720   /// relationship.
721   /// @returns true if *this != Val
722   /// @brief Inequality operator. 
723   bool operator!=(uint64_t Val) const {
724     return !((*this) == Val);
725   }
726   
727   /// Compares this APInt with RHS for the validity of the inequality
728   /// relationship.
729   /// @returns true if *this != Val
730   /// @brief Inequality comparison
731   bool ne(const APInt &RHS) const {
732     return !((*this) == RHS);
733   }
734
735   /// Regards both *this and RHS as unsigned quantities and compares them for
736   /// the validity of the less-than relationship.
737   /// @returns true if *this < RHS when both are considered unsigned.
738   /// @brief Unsigned less than comparison
739   bool ult(const APInt& RHS) const;
740
741   /// Regards both *this and RHS as signed quantities and compares them for
742   /// validity of the less-than relationship.
743   /// @returns true if *this < RHS when both are considered signed.
744   /// @brief Signed less than comparison
745   bool slt(const APInt& RHS) const;
746
747   /// Regards both *this and RHS as unsigned quantities and compares them for
748   /// validity of the less-or-equal relationship.
749   /// @returns true if *this <= RHS when both are considered unsigned.
750   /// @brief Unsigned less or equal comparison
751   bool ule(const APInt& RHS) const {
752     return ult(RHS) || eq(RHS);
753   }
754
755   /// Regards both *this and RHS as signed quantities and compares them for
756   /// validity of the less-or-equal relationship.
757   /// @returns true if *this <= RHS when both are considered signed.
758   /// @brief Signed less or equal comparison
759   bool sle(const APInt& RHS) const {
760     return slt(RHS) || eq(RHS);
761   }
762
763   /// Regards both *this and RHS as unsigned quantities and compares them for
764   /// the validity of the greater-than relationship.
765   /// @returns true if *this > RHS when both are considered unsigned.
766   /// @brief Unsigned greather than comparison
767   bool ugt(const APInt& RHS) const {
768     return !ult(RHS) && !eq(RHS);
769   }
770
771   /// Regards both *this and RHS as signed quantities and compares them for
772   /// the validity of the greater-than relationship.
773   /// @returns true if *this > RHS when both are considered signed.
774   /// @brief Signed greather than comparison
775   bool sgt(const APInt& RHS) const {
776     return !slt(RHS) && !eq(RHS);
777   }
778
779   /// Regards both *this and RHS as unsigned quantities and compares them for
780   /// validity of the greater-or-equal relationship.
781   /// @returns true if *this >= RHS when both are considered unsigned.
782   /// @brief Unsigned greater or equal comparison
783   bool uge(const APInt& RHS) const {
784     return !ult(RHS);
785   }
786
787   /// Regards both *this and RHS as signed quantities and compares them for
788   /// validity of the greater-or-equal relationship.
789   /// @returns true if *this >= RHS when both are considered signed.
790   /// @brief Signed greather or equal comparison
791   bool sge(const APInt& RHS) const {
792     return !slt(RHS);
793   }
794
795   /// This operation tests if there are any pairs of corresponding bits
796   /// between this APInt and RHS that are both set.
797   bool intersects(const APInt &RHS) const {
798     return (*this & RHS) != 0;
799   }
800
801   /// @}
802   /// @name Resizing Operators
803   /// @{
804   /// Truncate the APInt to a specified width. It is an error to specify a width
805   /// that is greater than or equal to the current width. 
806   /// @brief Truncate to new width.
807   APInt &trunc(uint32_t width);
808
809   /// This operation sign extends the APInt to a new width. If the high order
810   /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
811   /// It is an error to specify a width that is less than or equal to the 
812   /// current width.
813   /// @brief Sign extend to a new width.
814   APInt &sext(uint32_t width);
815
816   /// This operation zero extends the APInt to a new width. The high order bits
817   /// are filled with 0 bits.  It is an error to specify a width that is less 
818   /// than or equal to the current width.
819   /// @brief Zero extend to a new width.
820   APInt &zext(uint32_t width);
821
822   /// Make this APInt have the bit width given by \p width. The value is sign
823   /// extended, truncated, or left alone to make it that width.
824   /// @brief Sign extend or truncate to width
825   APInt &sextOrTrunc(uint32_t width);
826
827   /// Make this APInt have the bit width given by \p width. The value is zero
828   /// extended, truncated, or left alone to make it that width.
829   /// @brief Zero extend or truncate to width
830   APInt &zextOrTrunc(uint32_t width);
831
832   /// @}
833   /// @name Bit Manipulation Operators
834   /// @{
835   /// @brief Set every bit to 1.
836   APInt& set();
837
838   /// Set the given bit to 1 whose position is given as "bitPosition".
839   /// @brief Set a given bit to 1.
840   APInt& set(uint32_t bitPosition);
841
842   /// @brief Set every bit to 0.
843   APInt& clear();
844
845   /// Set the given bit to 0 whose position is given as "bitPosition".
846   /// @brief Set a given bit to 0.
847   APInt& clear(uint32_t bitPosition);
848
849   /// @brief Toggle every bit to its opposite value.
850   APInt& flip();
851
852   /// Toggle a given bit to its opposite value whose position is given 
853   /// as "bitPosition".
854   /// @brief Toggles a given bit to its opposite value.
855   APInt& flip(uint32_t bitPosition);
856
857   /// @}
858   /// @name Value Characterization Functions
859   /// @{
860
861   /// @returns the total number of bits.
862   uint32_t getBitWidth() const { 
863     return BitWidth; 
864   }
865
866   /// Here one word's bitwidth equals to that of uint64_t.
867   /// @returns the number of words to hold the integer value of this APInt.
868   /// @brief Get the number of words.
869   uint32_t getNumWords() const {
870     return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
871   }
872
873   /// This function returns the number of active bits which is defined as the
874   /// bit width minus the number of leading zeros. This is used in several
875   /// computations to see how "wide" the value is.
876   /// @brief Compute the number of active bits in the value
877   uint32_t getActiveBits() const {
878     return BitWidth - countLeadingZeros();
879   }
880
881   /// This function returns the number of active words in the value of this
882   /// APInt. This is used in conjunction with getActiveData to extract the raw
883   /// value of the APInt.
884   uint32_t getActiveWords() const {
885     return whichWord(getActiveBits()-1) + 1;
886   }
887
888   /// Computes the minimum bit width for this APInt while considering it to be
889   /// a signed (and probably negative) value. If the value is not negative, 
890   /// this function returns the same value as getActiveBits()+1. Otherwise, it
891   /// returns the smallest bit width that will retain the negative value. For
892   /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
893   /// for -1, this function will always return 1.
894   /// @brief Get the minimum bit size for this signed APInt 
895   uint32_t getMinSignedBits() const {
896     if (isNegative())
897       return BitWidth - countLeadingOnes() + 1;
898     return getActiveBits()+1;
899   }
900
901   /// This method attempts to return the value of this APInt as a zero extended
902   /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
903   /// uint64_t. Otherwise an assertion will result.
904   /// @brief Get zero extended value
905   uint64_t getZExtValue() const {
906     if (isSingleWord())
907       return VAL;
908     assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
909     return pVal[0];
910   }
911
912   /// This method attempts to return the value of this APInt as a sign extended
913   /// int64_t. The bit width must be <= 64 or the value must fit within an
914   /// int64_t. Otherwise an assertion will result.
915   /// @brief Get sign extended value
916   int64_t getSExtValue() const {
917     if (isSingleWord())
918       return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >> 
919                      (APINT_BITS_PER_WORD - BitWidth);
920     assert(getActiveBits() <= 64 && "Too many bits for int64_t");
921     return int64_t(pVal[0]);
922   }
923
924   /// This method determines how many bits are required to hold the APInt
925   /// equivalent of the string given by \p str of length \p slen.
926   /// @brief Get bits required for string value.
927   static uint32_t getBitsNeeded(const char* str, uint32_t slen, uint8_t radix);
928
929   /// countLeadingZeros - This function is an APInt version of the
930   /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
931   /// of zeros from the most significant bit to the first one bit.
932   /// @returns BitWidth if the value is zero.
933   /// @returns the number of zeros from the most significant bit to the first
934   /// one bits.
935   uint32_t countLeadingZeros() const;
936
937   /// countLeadingOnes - This function is an APInt version of the
938   /// countLeadingOnes_{32,64} functions in MathExtras.h. It counts the number
939   /// of ones from the most significant bit to the first zero bit.
940   /// @returns 0 if the high order bit is not set
941   /// @returns the number of 1 bits from the most significant to the least
942   /// @brief Count the number of leading one bits.
943   uint32_t countLeadingOnes() const;
944
945   /// countTrailingZeros - This function is an APInt version of the 
946   /// countTrailingZeros_{32,64} functions in MathExtras.h. It counts 
947   /// the number of zeros from the least significant bit to the first set bit.
948   /// @returns BitWidth if the value is zero.
949   /// @returns the number of zeros from the least significant bit to the first
950   /// one bit.
951   /// @brief Count the number of trailing zero bits.
952   uint32_t countTrailingZeros() const;
953
954   /// countTrailingOnes - This function is an APInt version of the 
955   /// countTrailingOnes_{32,64} functions in MathExtras.h. It counts 
956   /// the number of ones from the least significant bit to the first zero bit.
957   /// @returns BitWidth if the value is all ones.
958   /// @returns the number of ones from the least significant bit to the first
959   /// zero bit.
960   /// @brief Count the number of trailing one bits.
961   uint32_t countTrailingOnes() const;
962
963   /// countPopulation - This function is an APInt version of the
964   /// countPopulation_{32,64} functions in MathExtras.h. It counts the number
965   /// of 1 bits in the APInt value. 
966   /// @returns 0 if the value is zero.
967   /// @returns the number of set bits.
968   /// @brief Count the number of bits set.
969   uint32_t countPopulation() const; 
970
971   /// @}
972   /// @name Conversion Functions
973   /// @{
974
975   /// This is used internally to convert an APInt to a string.
976   /// @brief Converts an APInt to a std::string
977   std::string toString(uint8_t radix, bool wantSigned) const;
978
979   /// Considers the APInt to be unsigned and converts it into a string in the
980   /// radix given. The radix can be 2, 8, 10 or 16.
981   /// @returns a character interpretation of the APInt
982   /// @brief Convert unsigned APInt to string representation.
983   std::string toStringUnsigned(uint8_t radix = 10) const {
984     return toString(radix, false);
985   }
986
987   /// Considers the APInt to be signed and converts it into a string in the
988   /// radix given. The radix can be 2, 8, 10 or 16.
989   /// @returns a character interpretation of the APInt
990   /// @brief Convert signed APInt to string representation.
991   std::string toStringSigned(uint8_t radix = 10) const {
992     return toString(radix, true);
993   }
994
995   /// @returns a byte-swapped representation of this APInt Value.
996   APInt byteSwap() const;
997
998   /// @brief Converts this APInt to a double value.
999   double roundToDouble(bool isSigned) const;
1000
1001   /// @brief Converts this unsigned APInt to a double value.
1002   double roundToDouble() const {
1003     return roundToDouble(false);
1004   }
1005
1006   /// @brief Converts this signed APInt to a double value.
1007   double signedRoundToDouble() const {
1008     return roundToDouble(true);
1009   }
1010
1011   /// The conversion does not do a translation from integer to double, it just
1012   /// re-interprets the bits as a double. Note that it is valid to do this on
1013   /// any bit width. Exactly 64 bits will be translated.
1014   /// @brief Converts APInt bits to a double
1015   double bitsToDouble() const {
1016     union {
1017       uint64_t I;
1018       double D;
1019     } T;
1020     T.I = (isSingleWord() ? VAL : pVal[0]);
1021     return T.D;
1022   }
1023
1024   /// The conversion does not do a translation from integer to float, it just
1025   /// re-interprets the bits as a float. Note that it is valid to do this on
1026   /// any bit width. Exactly 32 bits will be translated.
1027   /// @brief Converts APInt bits to a double
1028   float bitsToFloat() const {
1029     union {
1030       uint32_t I;
1031       float F;
1032     } T;
1033     T.I = uint32_t((isSingleWord() ? VAL : pVal[0]));
1034     return T.F;
1035   }
1036
1037   /// The conversion does not do a translation from double to integer, it just
1038   /// re-interprets the bits of the double. Note that it is valid to do this on
1039   /// any bit width but bits from V may get truncated.
1040   /// @brief Converts a double to APInt bits.
1041   APInt& doubleToBits(double V) {
1042     union {
1043       uint64_t I;
1044       double D;
1045     } T;
1046     T.D = V;
1047     if (isSingleWord())
1048       VAL = T.I;
1049     else
1050       pVal[0] = T.I;
1051     return clearUnusedBits();
1052   }
1053
1054   /// The conversion does not do a translation from float to integer, it just
1055   /// re-interprets the bits of the float. Note that it is valid to do this on
1056   /// any bit width but bits from V may get truncated.
1057   /// @brief Converts a float to APInt bits.
1058   APInt& floatToBits(float V) {
1059     union {
1060       uint32_t I;
1061       float F;
1062     } T;
1063     T.F = V;
1064     if (isSingleWord())
1065       VAL = T.I;
1066     else
1067       pVal[0] = T.I;
1068     return clearUnusedBits();
1069   }
1070
1071   /// @}
1072   /// @name Mathematics Operations
1073   /// @{
1074
1075   /// @returns the floor log base 2 of this APInt.
1076   uint32_t logBase2() const {
1077     return BitWidth - 1 - countLeadingZeros();
1078   }
1079
1080   /// @returns the log base 2 of this APInt if its an exact power of two, -1
1081   /// otherwise
1082   int32_t exactLogBase2() const {
1083     if (!isPowerOf2())
1084       return -1;
1085     return logBase2();
1086   }
1087
1088   /// @brief Compute the square root
1089   APInt sqrt() const;
1090
1091   /// If *this is < 0 then return -(*this), otherwise *this;
1092   /// @brief Get the absolute value;
1093   APInt abs() const {
1094     if (isNegative())
1095       return -(*this);
1096     return *this;
1097   }
1098
1099   /// @returns the multiplicative inverse for a given modulo.
1100   APInt multiplicativeInverse(const APInt& modulo) const;
1101
1102   /// @}
1103   /// @name Building-block Operations for APInt and APFloat
1104   /// @{
1105
1106   // These building block operations operate on a representation of
1107   // arbitrary precision, two's-complement, bignum integer values.
1108   // They should be sufficient to implement APInt and APFloat bignum
1109   // requirements.  Inputs are generally a pointer to the base of an
1110   // array of integer parts, representing an unsigned bignum, and a
1111   // count of how many parts there are.
1112
1113   /// Sets the least significant part of a bignum to the input value,
1114   /// and zeroes out higher parts.  */
1115   static void tcSet(integerPart *, integerPart, unsigned int);
1116
1117   /// Assign one bignum to another.
1118   static void tcAssign(integerPart *, const integerPart *, unsigned int);
1119
1120   /// Returns true if a bignum is zero, false otherwise.
1121   static bool tcIsZero(const integerPart *, unsigned int);
1122
1123   /// Extract the given bit of a bignum; returns 0 or 1.  Zero-based.
1124   static int tcExtractBit(const integerPart *, unsigned int bit);
1125
1126   /// Copy the bit vector of width srcBITS from SRC, starting at bit
1127   /// srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB
1128   /// becomes the least significant bit of DST.  All high bits above
1129   /// srcBITS in DST are zero-filled.
1130   static void tcExtract(integerPart *, unsigned int dstCount, const integerPart *,
1131                         unsigned int srcBits, unsigned int srcLSB);
1132
1133   /// Set the given bit of a bignum.  Zero-based.
1134   static void tcSetBit(integerPart *, unsigned int bit);
1135
1136   /// Returns the bit number of the least or most significant set bit
1137   /// of a number.  If the input number has no bits set -1U is
1138   /// returned.
1139   static unsigned int tcLSB(const integerPart *, unsigned int);
1140   static unsigned int tcMSB(const integerPart *, unsigned int);
1141
1142   /// Negate a bignum in-place.
1143   static void tcNegate(integerPart *, unsigned int);
1144
1145   /// DST += RHS + CARRY where CARRY is zero or one.  Returns the
1146   /// carry flag.
1147   static integerPart tcAdd(integerPart *, const integerPart *,
1148                            integerPart carry, unsigned);
1149
1150   /// DST -= RHS + CARRY where CARRY is zero or one.  Returns the
1151   /// carry flag.
1152   static integerPart tcSubtract(integerPart *, const integerPart *,
1153                                 integerPart carry, unsigned);
1154
1155   ///  DST += SRC * MULTIPLIER + PART   if add is true
1156   ///  DST  = SRC * MULTIPLIER + PART   if add is false
1157   ///
1158   ///  Requires 0 <= DSTPARTS <= SRCPARTS + 1.  If DST overlaps SRC
1159   ///  they must start at the same point, i.e. DST == SRC.
1160   ///
1161   ///  If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is
1162   ///  returned.  Otherwise DST is filled with the least significant
1163   ///  DSTPARTS parts of the result, and if all of the omitted higher
1164   ///  parts were zero return zero, otherwise overflow occurred and
1165   ///  return one.
1166   static int tcMultiplyPart(integerPart *dst, const integerPart *src,
1167                             integerPart multiplier, integerPart carry,
1168                             unsigned int srcParts, unsigned int dstParts,
1169                             bool add);
1170
1171   /// DST = LHS * RHS, where DST has the same width as the operands
1172   /// and is filled with the least significant parts of the result.
1173   /// Returns one if overflow occurred, otherwise zero.  DST must be
1174   /// disjoint from both operands.
1175   static int tcMultiply(integerPart *, const integerPart *,
1176                         const integerPart *, unsigned);
1177
1178   /// DST = LHS * RHS, where DST has width the sum of the widths of
1179   /// the operands.  No overflow occurs.  DST must be disjoint from
1180   /// both operands. Returns the number of parts required to hold the
1181   /// result.
1182   static unsigned int tcFullMultiply(integerPart *, const integerPart *,
1183                                      const integerPart *, unsigned, unsigned);
1184
1185   /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1186   /// Otherwise set LHS to LHS / RHS with the fractional part
1187   /// discarded, set REMAINDER to the remainder, return zero.  i.e.
1188   ///
1189   ///  OLD_LHS = RHS * LHS + REMAINDER
1190   ///
1191   ///  SCRATCH is a bignum of the same size as the operands and result
1192   ///  for use by the routine; its contents need not be initialized
1193   ///  and are destroyed.  LHS, REMAINDER and SCRATCH must be
1194   ///  distinct.
1195   static int tcDivide(integerPart *lhs, const integerPart *rhs,
1196                       integerPart *remainder, integerPart *scratch,
1197                       unsigned int parts);
1198
1199   /// Shift a bignum left COUNT bits.  Shifted in bits are zero.
1200   /// There are no restrictions on COUNT.
1201   static void tcShiftLeft(integerPart *, unsigned int parts,
1202                           unsigned int count);
1203
1204   /// Shift a bignum right COUNT bits.  Shifted in bits are zero.
1205   /// There are no restrictions on COUNT.
1206   static void tcShiftRight(integerPart *, unsigned int parts,
1207                            unsigned int count);
1208
1209   /// The obvious AND, OR and XOR and complement operations.
1210   static void tcAnd(integerPart *, const integerPart *, unsigned int);
1211   static void tcOr(integerPart *, const integerPart *, unsigned int);
1212   static void tcXor(integerPart *, const integerPart *, unsigned int);
1213   static void tcComplement(integerPart *, unsigned int);
1214   
1215   /// Comparison (unsigned) of two bignums.
1216   static int tcCompare(const integerPart *, const integerPart *,
1217                        unsigned int);
1218
1219   /// Increment a bignum in-place.  Return the carry flag.
1220   static integerPart tcIncrement(integerPart *, unsigned int);
1221
1222   /// Set the least significant BITS and clear the rest.
1223   static void tcSetLeastSignificantBits(integerPart *, unsigned int,
1224                                         unsigned int bits);
1225
1226   /// @brief debug method
1227   void dump() const;
1228
1229   /// @}
1230 };
1231
1232 inline bool operator==(uint64_t V1, const APInt& V2) {
1233   return V2 == V1;
1234 }
1235
1236 inline bool operator!=(uint64_t V1, const APInt& V2) {
1237   return V2 != V1;
1238 }
1239
1240 namespace APIntOps {
1241
1242 /// @brief Determine the smaller of two APInts considered to be signed.
1243 inline APInt smin(const APInt &A, const APInt &B) {
1244   return A.slt(B) ? A : B;
1245 }
1246
1247 /// @brief Determine the larger of two APInts considered to be signed.
1248 inline APInt smax(const APInt &A, const APInt &B) {
1249   return A.sgt(B) ? A : B;
1250 }
1251
1252 /// @brief Determine the smaller of two APInts considered to be signed.
1253 inline APInt umin(const APInt &A, const APInt &B) {
1254   return A.ult(B) ? A : B;
1255 }
1256
1257 /// @brief Determine the larger of two APInts considered to be unsigned.
1258 inline APInt umax(const APInt &A, const APInt &B) {
1259   return A.ugt(B) ? A : B;
1260 }
1261
1262 /// @brief Check if the specified APInt has a N-bits unsigned integer value.
1263 inline bool isIntN(uint32_t N, const APInt& APIVal) {
1264   return APIVal.isIntN(N);
1265 }
1266
1267 /// @brief Check if the specified APInt has a N-bits signed integer value.
1268 inline bool isSignedIntN(uint32_t N, const APInt& APIVal) {
1269   return APIVal.isSignedIntN(N);
1270 }
1271
1272 /// @returns true if the argument APInt value is a sequence of ones
1273 /// starting at the least significant bit with the remainder zero.
1274 inline bool isMask(uint32_t numBits, const APInt& APIVal) {
1275   return numBits <= APIVal.getBitWidth() &&
1276     APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
1277 }
1278
1279 /// @returns true if the argument APInt value contains a sequence of ones
1280 /// with the remainder zero.
1281 inline bool isShiftedMask(uint32_t numBits, const APInt& APIVal) {
1282   return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
1283 }
1284
1285 /// @returns a byte-swapped representation of the specified APInt Value.
1286 inline APInt byteSwap(const APInt& APIVal) {
1287   return APIVal.byteSwap();
1288 }
1289
1290 /// @returns the floor log base 2 of the specified APInt value.
1291 inline uint32_t logBase2(const APInt& APIVal) {
1292   return APIVal.logBase2(); 
1293 }
1294
1295 /// GreatestCommonDivisor - This function returns the greatest common
1296 /// divisor of the two APInt values using Euclid's algorithm.
1297 /// @returns the greatest common divisor of Val1 and Val2
1298 /// @brief Compute GCD of two APInt values.
1299 APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
1300
1301 /// Treats the APInt as an unsigned value for conversion purposes.
1302 /// @brief Converts the given APInt to a double value.
1303 inline double RoundAPIntToDouble(const APInt& APIVal) {
1304   return APIVal.roundToDouble();
1305 }
1306
1307 /// Treats the APInt as a signed value for conversion purposes.
1308 /// @brief Converts the given APInt to a double value.
1309 inline double RoundSignedAPIntToDouble(const APInt& APIVal) {
1310   return APIVal.signedRoundToDouble();
1311 }
1312
1313 /// @brief Converts the given APInt to a float vlalue.
1314 inline float RoundAPIntToFloat(const APInt& APIVal) {
1315   return float(RoundAPIntToDouble(APIVal));
1316 }
1317
1318 /// Treast the APInt as a signed value for conversion purposes.
1319 /// @brief Converts the given APInt to a float value.
1320 inline float RoundSignedAPIntToFloat(const APInt& APIVal) {
1321   return float(APIVal.signedRoundToDouble());
1322 }
1323
1324 /// RoundDoubleToAPInt - This function convert a double value to an APInt value.
1325 /// @brief Converts the given double value into a APInt.
1326 APInt RoundDoubleToAPInt(double Double, uint32_t width);
1327
1328 /// RoundFloatToAPInt - Converts a float value into an APInt value.
1329 /// @brief Converts a float value into a APInt.
1330 inline APInt RoundFloatToAPInt(float Float, uint32_t width) {
1331   return RoundDoubleToAPInt(double(Float), width);
1332 }
1333
1334 /// Arithmetic right-shift the APInt by shiftAmt.
1335 /// @brief Arithmetic right-shift function.
1336 inline APInt ashr(const APInt& LHS, uint32_t shiftAmt) {
1337   return LHS.ashr(shiftAmt);
1338 }
1339
1340 /// Logical right-shift the APInt by shiftAmt.
1341 /// @brief Logical right-shift function.
1342 inline APInt lshr(const APInt& LHS, uint32_t shiftAmt) {
1343   return LHS.lshr(shiftAmt);
1344 }
1345
1346 /// Left-shift the APInt by shiftAmt.
1347 /// @brief Left-shift function.
1348 inline APInt shl(const APInt& LHS, uint32_t shiftAmt) {
1349   return LHS.shl(shiftAmt);
1350 }
1351
1352 /// Signed divide APInt LHS by APInt RHS.
1353 /// @brief Signed division function for APInt.
1354 inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
1355   return LHS.sdiv(RHS);
1356 }
1357
1358 /// Unsigned divide APInt LHS by APInt RHS.
1359 /// @brief Unsigned division function for APInt.
1360 inline APInt udiv(const APInt& LHS, const APInt& RHS) {
1361   return LHS.udiv(RHS);
1362 }
1363
1364 /// Signed remainder operation on APInt.
1365 /// @brief Function for signed remainder operation.
1366 inline APInt srem(const APInt& LHS, const APInt& RHS) {
1367   return LHS.srem(RHS);
1368 }
1369
1370 /// Unsigned remainder operation on APInt.
1371 /// @brief Function for unsigned remainder operation.
1372 inline APInt urem(const APInt& LHS, const APInt& RHS) {
1373   return LHS.urem(RHS);
1374 }
1375
1376 /// Performs multiplication on APInt values.
1377 /// @brief Function for multiplication operation.
1378 inline APInt mul(const APInt& LHS, const APInt& RHS) {
1379   return LHS * RHS;
1380 }
1381
1382 /// Performs addition on APInt values.
1383 /// @brief Function for addition operation.
1384 inline APInt add(const APInt& LHS, const APInt& RHS) {
1385   return LHS + RHS;
1386 }
1387
1388 /// Performs subtraction on APInt values.
1389 /// @brief Function for subtraction operation.
1390 inline APInt sub(const APInt& LHS, const APInt& RHS) {
1391   return LHS - RHS;
1392 }
1393
1394 /// Performs bitwise AND operation on APInt LHS and 
1395 /// APInt RHS.
1396 /// @brief Bitwise AND function for APInt.
1397 inline APInt And(const APInt& LHS, const APInt& RHS) {
1398   return LHS & RHS;
1399 }
1400
1401 /// Performs bitwise OR operation on APInt LHS and APInt RHS.
1402 /// @brief Bitwise OR function for APInt. 
1403 inline APInt Or(const APInt& LHS, const APInt& RHS) {
1404   return LHS | RHS;
1405 }
1406
1407 /// Performs bitwise XOR operation on APInt.
1408 /// @brief Bitwise XOR function for APInt.
1409 inline APInt Xor(const APInt& LHS, const APInt& RHS) {
1410   return LHS ^ RHS;
1411
1412
1413 /// Performs a bitwise complement operation on APInt.
1414 /// @brief Bitwise complement function. 
1415 inline APInt Not(const APInt& APIVal) {
1416   return ~APIVal;
1417 }
1418
1419 } // End of APIntOps namespace
1420
1421 } // End of llvm namespace
1422
1423 #endif