First step to fix PR2088. Implement routine to compute the
[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 the
672   /// quotient and remainder. This function does both operations in the same
673   /// computation making it a little more efficient. The pair of input arguments
674   /// may overlap with the pair of output arguments. It is safe to call
675   /// udivrem(X, Y, X, Y), for example.
676   /// @brief Dual division/remainder interface.
677   static void udivrem(const APInt &LHS, const APInt &RHS, 
678                       APInt &Quotient, APInt &Remainder);
679
680   static void sdivrem(const APInt &LHS, const APInt &RHS,
681                       APInt &Quotient, APInt &Remainder)
682   {
683     if (LHS.isNegative()) {
684       if (RHS.isNegative())
685         APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
686       else
687         APInt::udivrem(-LHS, RHS, Quotient, Remainder);
688       Quotient = -Quotient;
689       Remainder = -Remainder;
690     } else if (RHS.isNegative()) {
691       APInt::udivrem(LHS, -RHS, Quotient, Remainder);
692       Quotient = -Quotient;
693     } else {
694       APInt::udivrem(LHS, RHS, Quotient, Remainder);
695     }
696   }
697
698   /// @returns the bit value at bitPosition
699   /// @brief Array-indexing support.
700   bool operator[](uint32_t bitPosition) const;
701
702   /// @}
703   /// @name Comparison Operators
704   /// @{
705   /// Compares this APInt with RHS for the validity of the equality
706   /// relationship.
707   /// @brief Equality operator. 
708   bool operator==(const APInt& RHS) const;
709
710   /// Compares this APInt with a uint64_t for the validity of the equality 
711   /// relationship.
712   /// @returns true if *this == Val
713   /// @brief Equality operator.
714   bool operator==(uint64_t Val) const;
715
716   /// Compares this APInt with RHS for the validity of the equality
717   /// relationship.
718   /// @returns true if *this == Val
719   /// @brief Equality comparison.
720   bool eq(const APInt &RHS) const {
721     return (*this) == RHS; 
722   }
723
724   /// Compares this APInt with RHS for the validity of the inequality
725   /// relationship.
726   /// @returns true if *this != Val
727   /// @brief Inequality operator. 
728   bool operator!=(const APInt& RHS) const {
729     return !((*this) == RHS);
730   }
731
732   /// Compares this APInt with a uint64_t for the validity of the inequality 
733   /// relationship.
734   /// @returns true if *this != Val
735   /// @brief Inequality operator. 
736   bool operator!=(uint64_t Val) const {
737     return !((*this) == Val);
738   }
739   
740   /// Compares this APInt with RHS for the validity of the inequality
741   /// relationship.
742   /// @returns true if *this != Val
743   /// @brief Inequality comparison
744   bool ne(const APInt &RHS) const {
745     return !((*this) == RHS);
746   }
747
748   /// Regards both *this and RHS as unsigned quantities and compares them for
749   /// the validity of the less-than relationship.
750   /// @returns true if *this < RHS when both are considered unsigned.
751   /// @brief Unsigned less than comparison
752   bool ult(const APInt& RHS) const;
753
754   /// Regards both *this and RHS as signed quantities and compares them for
755   /// validity of the less-than relationship.
756   /// @returns true if *this < RHS when both are considered signed.
757   /// @brief Signed less than comparison
758   bool slt(const APInt& RHS) const;
759
760   /// Regards both *this and RHS as unsigned quantities and compares them for
761   /// validity of the less-or-equal relationship.
762   /// @returns true if *this <= RHS when both are considered unsigned.
763   /// @brief Unsigned less or equal comparison
764   bool ule(const APInt& RHS) const {
765     return ult(RHS) || eq(RHS);
766   }
767
768   /// Regards both *this and RHS as signed quantities and compares them for
769   /// validity of the less-or-equal relationship.
770   /// @returns true if *this <= RHS when both are considered signed.
771   /// @brief Signed less or equal comparison
772   bool sle(const APInt& RHS) const {
773     return slt(RHS) || eq(RHS);
774   }
775
776   /// Regards both *this and RHS as unsigned quantities and compares them for
777   /// the validity of the greater-than relationship.
778   /// @returns true if *this > RHS when both are considered unsigned.
779   /// @brief Unsigned greather than comparison
780   bool ugt(const APInt& RHS) const {
781     return !ult(RHS) && !eq(RHS);
782   }
783
784   /// Regards both *this and RHS as signed quantities and compares them for
785   /// the validity of the greater-than relationship.
786   /// @returns true if *this > RHS when both are considered signed.
787   /// @brief Signed greather than comparison
788   bool sgt(const APInt& RHS) const {
789     return !slt(RHS) && !eq(RHS);
790   }
791
792   /// Regards both *this and RHS as unsigned quantities and compares them for
793   /// validity of the greater-or-equal relationship.
794   /// @returns true if *this >= RHS when both are considered unsigned.
795   /// @brief Unsigned greater or equal comparison
796   bool uge(const APInt& RHS) const {
797     return !ult(RHS);
798   }
799
800   /// Regards both *this and RHS as signed quantities and compares them for
801   /// validity of the greater-or-equal relationship.
802   /// @returns true if *this >= RHS when both are considered signed.
803   /// @brief Signed greather or equal comparison
804   bool sge(const APInt& RHS) const {
805     return !slt(RHS);
806   }
807
808   /// This operation tests if there are any pairs of corresponding bits
809   /// between this APInt and RHS that are both set.
810   bool intersects(const APInt &RHS) const {
811     return (*this & RHS) != 0;
812   }
813
814   /// @}
815   /// @name Resizing Operators
816   /// @{
817   /// Truncate the APInt to a specified width. It is an error to specify a width
818   /// that is greater than or equal to the current width. 
819   /// @brief Truncate to new width.
820   APInt &trunc(uint32_t width);
821
822   /// This operation sign extends the APInt to a new width. If the high order
823   /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
824   /// It is an error to specify a width that is less than or equal to the 
825   /// current width.
826   /// @brief Sign extend to a new width.
827   APInt &sext(uint32_t width);
828
829   /// This operation zero extends the APInt to a new width. The high order bits
830   /// are filled with 0 bits.  It is an error to specify a width that is less 
831   /// than or equal to the current width.
832   /// @brief Zero extend to a new width.
833   APInt &zext(uint32_t width);
834
835   /// Make this APInt have the bit width given by \p width. The value is sign
836   /// extended, truncated, or left alone to make it that width.
837   /// @brief Sign extend or truncate to width
838   APInt &sextOrTrunc(uint32_t width);
839
840   /// Make this APInt have the bit width given by \p width. The value is zero
841   /// extended, truncated, or left alone to make it that width.
842   /// @brief Zero extend or truncate to width
843   APInt &zextOrTrunc(uint32_t width);
844
845   /// @}
846   /// @name Bit Manipulation Operators
847   /// @{
848   /// @brief Set every bit to 1.
849   APInt& set();
850
851   /// Set the given bit to 1 whose position is given as "bitPosition".
852   /// @brief Set a given bit to 1.
853   APInt& set(uint32_t bitPosition);
854
855   /// @brief Set every bit to 0.
856   APInt& clear();
857
858   /// Set the given bit to 0 whose position is given as "bitPosition".
859   /// @brief Set a given bit to 0.
860   APInt& clear(uint32_t bitPosition);
861
862   /// @brief Toggle every bit to its opposite value.
863   APInt& flip();
864
865   /// Toggle a given bit to its opposite value whose position is given 
866   /// as "bitPosition".
867   /// @brief Toggles a given bit to its opposite value.
868   APInt& flip(uint32_t bitPosition);
869
870   /// @}
871   /// @name Value Characterization Functions
872   /// @{
873
874   /// @returns the total number of bits.
875   uint32_t getBitWidth() const { 
876     return BitWidth; 
877   }
878
879   /// Here one word's bitwidth equals to that of uint64_t.
880   /// @returns the number of words to hold the integer value of this APInt.
881   /// @brief Get the number of words.
882   uint32_t getNumWords() const {
883     return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
884   }
885
886   /// This function returns the number of active bits which is defined as the
887   /// bit width minus the number of leading zeros. This is used in several
888   /// computations to see how "wide" the value is.
889   /// @brief Compute the number of active bits in the value
890   uint32_t getActiveBits() const {
891     return BitWidth - countLeadingZeros();
892   }
893
894   /// This function returns the number of active words in the value of this
895   /// APInt. This is used in conjunction with getActiveData to extract the raw
896   /// value of the APInt.
897   uint32_t getActiveWords() const {
898     return whichWord(getActiveBits()-1) + 1;
899   }
900
901   /// Computes the minimum bit width for this APInt while considering it to be
902   /// a signed (and probably negative) value. If the value is not negative, 
903   /// this function returns the same value as getActiveBits()+1. Otherwise, it
904   /// returns the smallest bit width that will retain the negative value. For
905   /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
906   /// for -1, this function will always return 1.
907   /// @brief Get the minimum bit size for this signed APInt 
908   uint32_t getMinSignedBits() const {
909     if (isNegative())
910       return BitWidth - countLeadingOnes() + 1;
911     return getActiveBits()+1;
912   }
913
914   /// This method attempts to return the value of this APInt as a zero extended
915   /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
916   /// uint64_t. Otherwise an assertion will result.
917   /// @brief Get zero extended value
918   uint64_t getZExtValue() const {
919     if (isSingleWord())
920       return VAL;
921     assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
922     return pVal[0];
923   }
924
925   /// This method attempts to return the value of this APInt as a sign extended
926   /// int64_t. The bit width must be <= 64 or the value must fit within an
927   /// int64_t. Otherwise an assertion will result.
928   /// @brief Get sign extended value
929   int64_t getSExtValue() const {
930     if (isSingleWord())
931       return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >> 
932                      (APINT_BITS_PER_WORD - BitWidth);
933     assert(getActiveBits() <= 64 && "Too many bits for int64_t");
934     return int64_t(pVal[0]);
935   }
936
937   /// This method determines how many bits are required to hold the APInt
938   /// equivalent of the string given by \p str of length \p slen.
939   /// @brief Get bits required for string value.
940   static uint32_t getBitsNeeded(const char* str, uint32_t slen, uint8_t radix);
941
942   /// countLeadingZeros - This function is an APInt version of the
943   /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
944   /// of zeros from the most significant bit to the first one bit.
945   /// @returns BitWidth if the value is zero.
946   /// @returns the number of zeros from the most significant bit to the first
947   /// one bits.
948   uint32_t countLeadingZeros() const;
949
950   /// countLeadingOnes - This function is an APInt version of the
951   /// countLeadingOnes_{32,64} functions in MathExtras.h. It counts the number
952   /// of ones from the most significant bit to the first zero bit.
953   /// @returns 0 if the high order bit is not set
954   /// @returns the number of 1 bits from the most significant to the least
955   /// @brief Count the number of leading one bits.
956   uint32_t countLeadingOnes() const;
957
958   /// countTrailingZeros - This function is an APInt version of the 
959   /// countTrailingZeros_{32,64} functions in MathExtras.h. It counts 
960   /// the number of zeros from the least significant bit to the first set bit.
961   /// @returns BitWidth if the value is zero.
962   /// @returns the number of zeros from the least significant bit to the first
963   /// one bit.
964   /// @brief Count the number of trailing zero bits.
965   uint32_t countTrailingZeros() const;
966
967   /// countTrailingOnes - This function is an APInt version of the 
968   /// countTrailingOnes_{32,64} functions in MathExtras.h. It counts 
969   /// the number of ones from the least significant bit to the first zero bit.
970   /// @returns BitWidth if the value is all ones.
971   /// @returns the number of ones from the least significant bit to the first
972   /// zero bit.
973   /// @brief Count the number of trailing one bits.
974   uint32_t countTrailingOnes() const;
975
976   /// countPopulation - This function is an APInt version of the
977   /// countPopulation_{32,64} functions in MathExtras.h. It counts the number
978   /// of 1 bits in the APInt value. 
979   /// @returns 0 if the value is zero.
980   /// @returns the number of set bits.
981   /// @brief Count the number of bits set.
982   uint32_t countPopulation() const; 
983
984   /// @}
985   /// @name Conversion Functions
986   /// @{
987
988   /// This is used internally to convert an APInt to a string.
989   /// @brief Converts an APInt to a std::string
990   std::string toString(uint8_t radix, bool wantSigned) const;
991
992   /// Considers the APInt to be unsigned and converts it into a string in the
993   /// radix given. The radix can be 2, 8, 10 or 16.
994   /// @returns a character interpretation of the APInt
995   /// @brief Convert unsigned APInt to string representation.
996   std::string toStringUnsigned(uint8_t radix = 10) const {
997     return toString(radix, false);
998   }
999
1000   /// Considers the APInt to be unsigned and converts it into a string in the
1001   /// radix given. The radix can be 2, 8, 10 or 16.
1002   /// @returns a character interpretation of the APInt
1003   /// @brief Convert unsigned APInt to string representation.
1004   std::string toStringSigned(uint8_t radix = 10) const {
1005     return toString(radix, true);
1006   }
1007
1008   /// @returns a byte-swapped representation of this APInt Value.
1009   APInt byteSwap() const;
1010
1011   /// @brief Converts this APInt to a double value.
1012   double roundToDouble(bool isSigned) const;
1013
1014   /// @brief Converts this unsigned APInt to a double value.
1015   double roundToDouble() const {
1016     return roundToDouble(false);
1017   }
1018
1019   /// @brief Converts this signed APInt to a double value.
1020   double signedRoundToDouble() const {
1021     return roundToDouble(true);
1022   }
1023
1024   /// The conversion does not do a translation from integer to double, it just
1025   /// re-interprets the bits as a double. Note that it is valid to do this on
1026   /// any bit width. Exactly 64 bits will be translated.
1027   /// @brief Converts APInt bits to a double
1028   double bitsToDouble() const {
1029     union {
1030       uint64_t I;
1031       double D;
1032     } T;
1033     T.I = (isSingleWord() ? VAL : pVal[0]);
1034     return T.D;
1035   }
1036
1037   /// The conversion does not do a translation from integer to float, it just
1038   /// re-interprets the bits as a float. Note that it is valid to do this on
1039   /// any bit width. Exactly 32 bits will be translated.
1040   /// @brief Converts APInt bits to a double
1041   float bitsToFloat() const {
1042     union {
1043       uint32_t I;
1044       float F;
1045     } T;
1046     T.I = uint32_t((isSingleWord() ? VAL : pVal[0]));
1047     return T.F;
1048   }
1049
1050   /// The conversion does not do a translation from double to integer, it just
1051   /// re-interprets the bits of the double. Note that it is valid to do this on
1052   /// any bit width but bits from V may get truncated.
1053   /// @brief Converts a double to APInt bits.
1054   APInt& doubleToBits(double V) {
1055     union {
1056       uint64_t I;
1057       double D;
1058     } T;
1059     T.D = V;
1060     if (isSingleWord())
1061       VAL = T.I;
1062     else
1063       pVal[0] = T.I;
1064     return clearUnusedBits();
1065   }
1066
1067   /// The conversion does not do a translation from float to integer, it just
1068   /// re-interprets the bits of the float. Note that it is valid to do this on
1069   /// any bit width but bits from V may get truncated.
1070   /// @brief Converts a float to APInt bits.
1071   APInt& floatToBits(float V) {
1072     union {
1073       uint32_t I;
1074       float F;
1075     } T;
1076     T.F = V;
1077     if (isSingleWord())
1078       VAL = T.I;
1079     else
1080       pVal[0] = T.I;
1081     return clearUnusedBits();
1082   }
1083
1084   /// @}
1085   /// @name Mathematics Operations
1086   /// @{
1087
1088   /// @returns the floor log base 2 of this APInt.
1089   uint32_t logBase2() const {
1090     return BitWidth - 1 - countLeadingZeros();
1091   }
1092
1093   /// @returns the log base 2 of this APInt if its an exact power of two, -1
1094   /// otherwise
1095   int32_t exactLogBase2() const {
1096     if (!isPowerOf2())
1097       return -1;
1098     return logBase2();
1099   }
1100
1101   /// @brief Compute the square root
1102   APInt sqrt() const;
1103
1104   /// If *this is < 0 then return -(*this), otherwise *this;
1105   /// @brief Get the absolute value;
1106   APInt abs() const {
1107     if (isNegative())
1108       return -(*this);
1109     return *this;
1110   }
1111
1112   /// @returns the multiplicative inverse for a given modulo.
1113   APInt multiplicativeInverse(const APInt& modulo) const;
1114
1115   /// @}
1116   /// @name Building-block Operations for APInt and APFloat
1117   /// @{
1118
1119   // These building block operations operate on a representation of
1120   // arbitrary precision, two's-complement, bignum integer values.
1121   // They should be sufficient to implement APInt and APFloat bignum
1122   // requirements.  Inputs are generally a pointer to the base of an
1123   // array of integer parts, representing an unsigned bignum, and a
1124   // count of how many parts there are.
1125
1126   /// Sets the least significant part of a bignum to the input value,
1127   /// and zeroes out higher parts.  */
1128   static void tcSet(integerPart *, integerPart, unsigned int);
1129
1130   /// Assign one bignum to another.
1131   static void tcAssign(integerPart *, const integerPart *, unsigned int);
1132
1133   /// Returns true if a bignum is zero, false otherwise.
1134   static bool tcIsZero(const integerPart *, unsigned int);
1135
1136   /// Extract the given bit of a bignum; returns 0 or 1.  Zero-based.
1137   static int tcExtractBit(const integerPart *, unsigned int bit);
1138
1139   /// Copy the bit vector of width srcBITS from SRC, starting at bit
1140   /// srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB
1141   /// becomes the least significant bit of DST.  All high bits above
1142   /// srcBITS in DST are zero-filled.
1143   static void tcExtract(integerPart *, unsigned int dstCount, const integerPart *,
1144                         unsigned int srcBits, unsigned int srcLSB);
1145
1146   /// Set the given bit of a bignum.  Zero-based.
1147   static void tcSetBit(integerPart *, unsigned int bit);
1148
1149   /// Returns the bit number of the least or most significant set bit
1150   /// of a number.  If the input number has no bits set -1U is
1151   /// returned.
1152   static unsigned int tcLSB(const integerPart *, unsigned int);
1153   static unsigned int tcMSB(const integerPart *, unsigned int);
1154
1155   /// Negate a bignum in-place.
1156   static void tcNegate(integerPart *, unsigned int);
1157
1158   /// DST += RHS + CARRY where CARRY is zero or one.  Returns the
1159   /// carry flag.
1160   static integerPart tcAdd(integerPart *, const integerPart *,
1161                            integerPart carry, unsigned);
1162
1163   /// DST -= RHS + CARRY where CARRY is zero or one.  Returns the
1164   /// carry flag.
1165   static integerPart tcSubtract(integerPart *, const integerPart *,
1166                                 integerPart carry, unsigned);
1167
1168   ///  DST += SRC * MULTIPLIER + PART   if add is true
1169   ///  DST  = SRC * MULTIPLIER + PART   if add is false
1170   ///
1171   ///  Requires 0 <= DSTPARTS <= SRCPARTS + 1.  If DST overlaps SRC
1172   ///  they must start at the same point, i.e. DST == SRC.
1173   ///
1174   ///  If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is
1175   ///  returned.  Otherwise DST is filled with the least significant
1176   ///  DSTPARTS parts of the result, and if all of the omitted higher
1177   ///  parts were zero return zero, otherwise overflow occurred and
1178   ///  return one.
1179   static int tcMultiplyPart(integerPart *dst, const integerPart *src,
1180                             integerPart multiplier, integerPart carry,
1181                             unsigned int srcParts, unsigned int dstParts,
1182                             bool add);
1183
1184   /// DST = LHS * RHS, where DST has the same width as the operands
1185   /// and is filled with the least significant parts of the result.
1186   /// Returns one if overflow occurred, otherwise zero.  DST must be
1187   /// disjoint from both operands.
1188   static int tcMultiply(integerPart *, const integerPart *,
1189                         const integerPart *, unsigned);
1190
1191   /// DST = LHS * RHS, where DST has width the sum of the widths of
1192   /// the operands.  No overflow occurs.  DST must be disjoint from
1193   /// both operands. Returns the number of parts required to hold the
1194   /// result.
1195   static unsigned int tcFullMultiply(integerPart *, const integerPart *,
1196                                      const integerPart *, unsigned, unsigned);
1197
1198   /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1199   /// Otherwise set LHS to LHS / RHS with the fractional part
1200   /// discarded, set REMAINDER to the remainder, return zero.  i.e.
1201   ///
1202   ///  OLD_LHS = RHS * LHS + REMAINDER
1203   ///
1204   ///  SCRATCH is a bignum of the same size as the operands and result
1205   ///  for use by the routine; its contents need not be initialized
1206   ///  and are destroyed.  LHS, REMAINDER and SCRATCH must be
1207   ///  distinct.
1208   static int tcDivide(integerPart *lhs, const integerPart *rhs,
1209                       integerPart *remainder, integerPart *scratch,
1210                       unsigned int parts);
1211
1212   /// Shift a bignum left COUNT bits.  Shifted in bits are zero.
1213   /// There are no restrictions on COUNT.
1214   static void tcShiftLeft(integerPart *, unsigned int parts,
1215                           unsigned int count);
1216
1217   /// Shift a bignum right COUNT bits.  Shifted in bits are zero.
1218   /// There are no restrictions on COUNT.
1219   static void tcShiftRight(integerPart *, unsigned int parts,
1220                            unsigned int count);
1221
1222   /// The obvious AND, OR and XOR and complement operations.
1223   static void tcAnd(integerPart *, const integerPart *, unsigned int);
1224   static void tcOr(integerPart *, const integerPart *, unsigned int);
1225   static void tcXor(integerPart *, const integerPart *, unsigned int);
1226   static void tcComplement(integerPart *, unsigned int);
1227   
1228   /// Comparison (unsigned) of two bignums.
1229   static int tcCompare(const integerPart *, const integerPart *,
1230                        unsigned int);
1231
1232   /// Increment a bignum in-place.  Return the carry flag.
1233   static integerPart tcIncrement(integerPart *, unsigned int);
1234
1235   /// Set the least significant BITS and clear the rest.
1236   static void tcSetLeastSignificantBits(integerPart *, unsigned int,
1237                                         unsigned int bits);
1238
1239   /// @brief debug method
1240   void dump() const;
1241
1242   /// @}
1243 };
1244
1245 inline bool operator==(uint64_t V1, const APInt& V2) {
1246   return V2 == V1;
1247 }
1248
1249 inline bool operator!=(uint64_t V1, const APInt& V2) {
1250   return V2 != V1;
1251 }
1252
1253 namespace APIntOps {
1254
1255 /// @brief Determine the smaller of two APInts considered to be signed.
1256 inline APInt smin(const APInt &A, const APInt &B) {
1257   return A.slt(B) ? A : B;
1258 }
1259
1260 /// @brief Determine the larger of two APInts considered to be signed.
1261 inline APInt smax(const APInt &A, const APInt &B) {
1262   return A.sgt(B) ? A : B;
1263 }
1264
1265 /// @brief Determine the smaller of two APInts considered to be signed.
1266 inline APInt umin(const APInt &A, const APInt &B) {
1267   return A.ult(B) ? A : B;
1268 }
1269
1270 /// @brief Determine the larger of two APInts considered to be unsigned.
1271 inline APInt umax(const APInt &A, const APInt &B) {
1272   return A.ugt(B) ? A : B;
1273 }
1274
1275 /// @brief Check if the specified APInt has a N-bits unsigned integer value.
1276 inline bool isIntN(uint32_t N, const APInt& APIVal) {
1277   return APIVal.isIntN(N);
1278 }
1279
1280 /// @brief Check if the specified APInt has a N-bits signed integer value.
1281 inline bool isSignedIntN(uint32_t N, const APInt& APIVal) {
1282   return APIVal.isSignedIntN(N);
1283 }
1284
1285 /// @returns true if the argument APInt value is a sequence of ones
1286 /// starting at the least significant bit with the remainder zero.
1287 inline bool isMask(uint32_t numBits, const APInt& APIVal) {
1288   return numBits <= APIVal.getBitWidth() &&
1289     APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
1290 }
1291
1292 /// @returns true if the argument APInt value contains a sequence of ones
1293 /// with the remainder zero.
1294 inline bool isShiftedMask(uint32_t numBits, const APInt& APIVal) {
1295   return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
1296 }
1297
1298 /// @returns a byte-swapped representation of the specified APInt Value.
1299 inline APInt byteSwap(const APInt& APIVal) {
1300   return APIVal.byteSwap();
1301 }
1302
1303 /// @returns the floor log base 2 of the specified APInt value.
1304 inline uint32_t logBase2(const APInt& APIVal) {
1305   return APIVal.logBase2(); 
1306 }
1307
1308 /// GreatestCommonDivisor - This function returns the greatest common
1309 /// divisor of the two APInt values using Euclid's algorithm.
1310 /// @returns the greatest common divisor of Val1 and Val2
1311 /// @brief Compute GCD of two APInt values.
1312 APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
1313
1314 /// Treats the APInt as an unsigned value for conversion purposes.
1315 /// @brief Converts the given APInt to a double value.
1316 inline double RoundAPIntToDouble(const APInt& APIVal) {
1317   return APIVal.roundToDouble();
1318 }
1319
1320 /// Treats the APInt as a signed value for conversion purposes.
1321 /// @brief Converts the given APInt to a double value.
1322 inline double RoundSignedAPIntToDouble(const APInt& APIVal) {
1323   return APIVal.signedRoundToDouble();
1324 }
1325
1326 /// @brief Converts the given APInt to a float vlalue.
1327 inline float RoundAPIntToFloat(const APInt& APIVal) {
1328   return float(RoundAPIntToDouble(APIVal));
1329 }
1330
1331 /// Treast the APInt as a signed value for conversion purposes.
1332 /// @brief Converts the given APInt to a float value.
1333 inline float RoundSignedAPIntToFloat(const APInt& APIVal) {
1334   return float(APIVal.signedRoundToDouble());
1335 }
1336
1337 /// RoundDoubleToAPInt - This function convert a double value to an APInt value.
1338 /// @brief Converts the given double value into a APInt.
1339 APInt RoundDoubleToAPInt(double Double, uint32_t width);
1340
1341 /// RoundFloatToAPInt - Converts a float value into an APInt value.
1342 /// @brief Converts a float value into a APInt.
1343 inline APInt RoundFloatToAPInt(float Float, uint32_t width) {
1344   return RoundDoubleToAPInt(double(Float), width);
1345 }
1346
1347 /// Arithmetic right-shift the APInt by shiftAmt.
1348 /// @brief Arithmetic right-shift function.
1349 inline APInt ashr(const APInt& LHS, uint32_t shiftAmt) {
1350   return LHS.ashr(shiftAmt);
1351 }
1352
1353 /// Logical right-shift the APInt by shiftAmt.
1354 /// @brief Logical right-shift function.
1355 inline APInt lshr(const APInt& LHS, uint32_t shiftAmt) {
1356   return LHS.lshr(shiftAmt);
1357 }
1358
1359 /// Left-shift the APInt by shiftAmt.
1360 /// @brief Left-shift function.
1361 inline APInt shl(const APInt& LHS, uint32_t shiftAmt) {
1362   return LHS.shl(shiftAmt);
1363 }
1364
1365 /// Signed divide APInt LHS by APInt RHS.
1366 /// @brief Signed division function for APInt.
1367 inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
1368   return LHS.sdiv(RHS);
1369 }
1370
1371 /// Unsigned divide APInt LHS by APInt RHS.
1372 /// @brief Unsigned division function for APInt.
1373 inline APInt udiv(const APInt& LHS, const APInt& RHS) {
1374   return LHS.udiv(RHS);
1375 }
1376
1377 /// Signed remainder operation on APInt.
1378 /// @brief Function for signed remainder operation.
1379 inline APInt srem(const APInt& LHS, const APInt& RHS) {
1380   return LHS.srem(RHS);
1381 }
1382
1383 /// Unsigned remainder operation on APInt.
1384 /// @brief Function for unsigned remainder operation.
1385 inline APInt urem(const APInt& LHS, const APInt& RHS) {
1386   return LHS.urem(RHS);
1387 }
1388
1389 /// Performs multiplication on APInt values.
1390 /// @brief Function for multiplication operation.
1391 inline APInt mul(const APInt& LHS, const APInt& RHS) {
1392   return LHS * RHS;
1393 }
1394
1395 /// Performs addition on APInt values.
1396 /// @brief Function for addition operation.
1397 inline APInt add(const APInt& LHS, const APInt& RHS) {
1398   return LHS + RHS;
1399 }
1400
1401 /// Performs subtraction on APInt values.
1402 /// @brief Function for subtraction operation.
1403 inline APInt sub(const APInt& LHS, const APInt& RHS) {
1404   return LHS - RHS;
1405 }
1406
1407 /// Performs bitwise AND operation on APInt LHS and 
1408 /// APInt RHS.
1409 /// @brief Bitwise AND function for APInt.
1410 inline APInt And(const APInt& LHS, const APInt& RHS) {
1411   return LHS & RHS;
1412 }
1413
1414 /// Performs bitwise OR operation on APInt LHS and APInt RHS.
1415 /// @brief Bitwise OR function for APInt. 
1416 inline APInt Or(const APInt& LHS, const APInt& RHS) {
1417   return LHS | RHS;
1418 }
1419
1420 /// Performs bitwise XOR operation on APInt.
1421 /// @brief Bitwise XOR function for APInt.
1422 inline APInt Xor(const APInt& LHS, const APInt& RHS) {
1423   return LHS ^ RHS;
1424
1425
1426 /// Performs a bitwise complement operation on APInt.
1427 /// @brief Bitwise complement function. 
1428 inline APInt Not(const APInt& APIVal) {
1429   return ~APIVal;
1430 }
1431
1432 } // End of APIntOps namespace
1433
1434 } // End of llvm namespace
1435
1436 #endif