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