Rework the routines that convert AP[S]Int into a string. Now, instead of
[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 <iosfwd>
21 #include <string>
22
23 namespace llvm {
24   class Serializer;
25   class Deserializer;
26   class FoldingSetNodeID;
27   
28   template<typename T>
29   class SmallVectorImpl;
30   
31   /* An unsigned host type used as a single part of a multi-part
32      bignum.  */
33   typedef uint64_t integerPart;
34
35   const unsigned int host_char_bit = 8;
36   const unsigned int integerPartWidth = host_char_bit *
37     static_cast<unsigned int>(sizeof(integerPart));
38
39 //===----------------------------------------------------------------------===//
40 //                              APInt Class
41 //===----------------------------------------------------------------------===//
42
43 /// APInt - This class represents arbitrary precision constant integral values.
44 /// It is a functional replacement for common case unsigned integer type like 
45 /// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width 
46 /// integer sizes and large integer value types such as 3-bits, 15-bits, or more
47 /// than 64-bits of precision. APInt provides a variety of arithmetic operators 
48 /// and methods to manipulate integer values of any bit-width. It supports both
49 /// the typical integer arithmetic and comparison operations as well as bitwise
50 /// manipulation.
51 ///
52 /// The class has several invariants worth noting:
53 ///   * All bit, byte, and word positions are zero-based.
54 ///   * Once the bit width is set, it doesn't change except by the Truncate, 
55 ///     SignExtend, or ZeroExtend operations.
56 ///   * All binary operators must be on APInt instances of the same bit width.
57 ///     Attempting to use these operators on instances with different bit 
58 ///     widths will yield an assertion.
59 ///   * The value is stored canonically as an unsigned value. For operations
60 ///     where it makes a difference, there are both signed and unsigned variants
61 ///     of the operation. For example, sdiv and udiv. However, because the bit
62 ///     widths must be the same, operations such as Mul and Add produce the same
63 ///     results regardless of whether the values are interpreted as signed or
64 ///     not.
65 ///   * In general, the class tries to follow the style of computation that LLVM
66 ///     uses in its IR. This simplifies its use for LLVM.
67 ///
68 /// @brief Class for arbitrary precision integers.
69 class APInt {
70
71   uint32_t BitWidth;      ///< The number of bits in this APInt.
72
73   /// This union is used to store the integer value. When the
74   /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
75   union {
76     uint64_t VAL;    ///< Used to store the <= 64 bits integer value.
77     uint64_t *pVal;  ///< Used to store the >64 bits integer value.
78   };
79
80   /// This enum is used to hold the constants we needed for APInt.
81   enum {
82     /// Bits in a word
83     APINT_BITS_PER_WORD = static_cast<unsigned int>(sizeof(uint64_t)) * 8,
84     /// Byte size of a word
85     APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
86   };
87
88   /// This constructor is used only internally for speed of construction of
89   /// temporaries. It is unsafe for general use so it is not public.
90   /// @brief Fast internal constructor
91   APInt(uint64_t* val, uint32_t bits) : BitWidth(bits), pVal(val) { }
92
93   /// @returns true if the number of bits <= 64, false otherwise.
94   /// @brief Determine if this APInt just has one word to store value.
95   bool isSingleWord() const { 
96     return BitWidth <= APINT_BITS_PER_WORD; 
97   }
98
99   /// @returns the word position for the specified bit position.
100   /// @brief Determine which word a bit is in.
101   static uint32_t whichWord(uint32_t bitPosition) { 
102     return bitPosition / APINT_BITS_PER_WORD; 
103   }
104
105   /// @returns the bit position in a word for the specified bit position 
106   /// in the APInt.
107   /// @brief Determine which bit in a word a bit is in.
108   static uint32_t whichBit(uint32_t bitPosition) { 
109     return bitPosition % APINT_BITS_PER_WORD; 
110   }
111
112   /// This method generates and returns a uint64_t (word) mask for a single 
113   /// bit at a specific bit position. This is used to mask the bit in the 
114   /// corresponding word.
115   /// @returns a uint64_t with only bit at "whichBit(bitPosition)" set
116   /// @brief Get a single bit mask.
117   static uint64_t maskBit(uint32_t bitPosition) { 
118     return 1ULL << whichBit(bitPosition); 
119   }
120
121   /// This method is used internally to clear the to "N" bits in the high order
122   /// word that are not used by the APInt. This is needed after the most 
123   /// significant word is assigned a value to ensure that those bits are 
124   /// zero'd out.
125   /// @brief Clear unused high order bits
126   APInt& clearUnusedBits() {
127     // Compute how many bits are used in the final word
128     uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
129     if (wordBits == 0)
130       // If all bits are used, we want to leave the value alone. This also
131       // avoids the undefined behavior of >> when the shift is the same size as
132       // the word size (64).
133       return *this;
134
135     // Mask out the high bits.
136     uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
137     if (isSingleWord())
138       VAL &= mask;
139     else
140       pVal[getNumWords() - 1] &= mask;
141     return *this;
142   }
143
144   /// @returns the corresponding word for the specified bit position.
145   /// @brief Get the word corresponding to a bit position
146   uint64_t getWord(uint32_t bitPosition) const { 
147     return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; 
148   }
149
150   /// This is used by the constructors that take string arguments.
151   /// @brief Convert a char array into an APInt
152   void fromString(uint32_t numBits, const char *strStart, uint32_t slen, 
153                   uint8_t radix);
154
155   /// This is used by the toString method to divide by the radix. It simply
156   /// provides a more convenient form of divide for internal use since KnuthDiv
157   /// has specific constraints on its inputs. If those constraints are not met
158   /// then it provides a simpler form of divide.
159   /// @brief An internal division function for dividing APInts.
160   static void divide(const APInt LHS, uint32_t lhsWords, 
161                      const APInt &RHS, uint32_t rhsWords,
162                      APInt *Quotient, APInt *Remainder);
163
164 public:
165   /// @name Constructors
166   /// @{
167   /// If isSigned is true then val is treated as if it were a signed value
168   /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
169   /// will be done. Otherwise, no sign extension occurs (high order bits beyond
170   /// the range of val are zero filled).
171   /// @param numBits the bit width of the constructed APInt
172   /// @param val the initial value of the APInt
173   /// @param isSigned how to treat signedness of val
174   /// @brief Create a new APInt of numBits width, initialized as val.
175   APInt(uint32_t numBits, uint64_t val, bool isSigned = false);
176
177   /// Note that numWords can be smaller or larger than the corresponding bit
178   /// width but any extraneous bits will be dropped.
179   /// @param numBits the bit width of the constructed APInt
180   /// @param numWords the number of words in bigVal
181   /// @param bigVal a sequence of words to form the initial value of the APInt
182   /// @brief Construct an APInt of numBits width, initialized as bigVal[].
183   APInt(uint32_t numBits, uint32_t numWords, const uint64_t bigVal[]);
184
185   /// This constructor interprets the slen characters starting at StrStart as
186   /// a string in the given radix. The interpretation stops when the first 
187   /// character that is not suitable for the radix is encountered. Acceptable
188   /// radix values are 2, 8, 10 and 16. It is an error for the value implied by
189   /// the string to require more bits than numBits.
190   /// @param numBits the bit width of the constructed APInt
191   /// @param strStart the start of the string to be interpreted
192   /// @param slen the maximum number of characters to interpret
193   /// @param radix the radix to use for the conversion
194   /// @brief Construct an APInt from a string representation.
195   APInt(uint32_t numBits, const char strStart[], uint32_t slen, uint8_t radix);
196
197   /// Simply makes *this a copy of that.
198   /// @brief Copy Constructor.
199   APInt(const APInt& that);
200
201   /// @brief Destructor.
202   ~APInt();
203   
204   /// Default constructor that creates an uninitialized APInt.  This is useful
205   ///  for object deserialization (pair this with the static method Read).
206   explicit APInt() : BitWidth(1) {}
207   
208   /// Profile - Used to insert APInt objects, or objects that contain APInt 
209   ///  objects, into FoldingSets.
210   void Profile(FoldingSetNodeID& id) const;
211   
212   /// @brief Used by the Bitcode serializer to emit APInts to Bitcode.
213   void Emit(Serializer& S) const;
214   
215   /// @brief Used by the Bitcode deserializer to deserialize APInts.
216   void Read(Deserializer& D);
217
218   /// @}
219   /// @name Value Tests
220   /// @{
221   /// This tests the high bit of this APInt to determine if it is set.
222   /// @returns true if this APInt is negative, false otherwise
223   /// @brief Determine sign of this APInt.
224   bool isNegative() const {
225     return (*this)[BitWidth - 1];
226   }
227
228   /// This tests the high bit of the APInt to determine if it is unset.
229   /// @brief Determine if this APInt Value is non-negative (>= 0)
230   bool isNonNegative() const {
231     return !isNegative();
232   }
233
234   /// This tests if the value of this APInt is positive (> 0). Note
235   /// that 0 is not a positive value.
236   /// @returns true if this APInt is positive.
237   /// @brief Determine if this APInt Value is positive.
238   bool isStrictlyPositive() const {
239     return isNonNegative() && (*this) != 0;
240   }
241
242   /// This checks to see if the value has all bits of the APInt are set or not.
243   /// @brief Determine if all bits are set
244   bool isAllOnesValue() const {
245     return countPopulation() == BitWidth;
246   }
247
248   /// This checks to see if the value of this APInt is the maximum unsigned
249   /// value for the APInt's bit width.
250   /// @brief Determine if this is the largest unsigned value.
251   bool isMaxValue() const {
252     return countPopulation() == BitWidth;
253   }
254
255   /// This checks to see if the value of this APInt is the maximum signed
256   /// value for the APInt's bit width.
257   /// @brief Determine if this is the largest signed value.
258   bool isMaxSignedValue() const {
259     return BitWidth == 1 ? VAL == 0 :
260                           !isNegative() && countPopulation() == BitWidth - 1;
261   }
262
263   /// This checks to see if the value of this APInt is the minimum unsigned
264   /// value for the APInt's bit width.
265   /// @brief Determine if this is the smallest unsigned value.
266   bool isMinValue() const {
267     return countPopulation() == 0;
268   }
269
270   /// This checks to see if the value of this APInt is the minimum signed
271   /// value for the APInt's bit width.
272   /// @brief Determine if this is the smallest signed value.
273   bool isMinSignedValue() const {
274     return BitWidth == 1 ? VAL == 1 :
275                            isNegative() && countPopulation() == 1;
276   }
277
278   /// @brief Check if this APInt has an N-bits unsigned integer value.
279   bool isIntN(uint32_t N) const {
280     assert(N && "N == 0 ???");
281     if (isSingleWord()) {
282       return VAL == (VAL & (~0ULL >> (64 - N)));
283     } else {
284       APInt Tmp(N, getNumWords(), pVal);
285       return Tmp == (*this);
286     }
287   }
288
289   /// @brief Check if this APInt has an N-bits signed integer value.
290   bool isSignedIntN(uint32_t N) const {
291     assert(N && "N == 0 ???");
292     return getMinSignedBits() <= N;
293   }
294
295   /// @returns true if the argument APInt value is a power of two > 0.
296   bool isPowerOf2() const; 
297
298   /// isSignBit - Return true if this is the value returned by getSignBit.
299   bool isSignBit() const { return isMinSignedValue(); }
300   
301   /// This converts the APInt to a boolean value as a test against zero.
302   /// @brief Boolean conversion function. 
303   bool getBoolValue() const {
304     return *this != 0;
305   }
306
307   /// getLimitedValue - If this value is smaller than the specified limit,
308   /// return it, otherwise return the limit value.  This causes the value
309   /// to saturate to the limit.
310   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
311     return (getActiveBits() > 64 || getZExtValue() > Limit) ?
312       Limit :  getZExtValue();
313   }
314
315   /// @}
316   /// @name Value Generators
317   /// @{
318   /// @brief Gets maximum unsigned value of APInt for specific bit width.
319   static APInt getMaxValue(uint32_t numBits) {
320     return APInt(numBits, 0).set();
321   }
322
323   /// @brief Gets maximum signed value of APInt for a specific bit width.
324   static APInt getSignedMaxValue(uint32_t numBits) {
325     return APInt(numBits, 0).set().clear(numBits - 1);
326   }
327
328   /// @brief Gets minimum unsigned value of APInt for a specific bit width.
329   static APInt getMinValue(uint32_t numBits) {
330     return APInt(numBits, 0);
331   }
332
333   /// @brief Gets minimum signed value of APInt for a specific bit width.
334   static APInt getSignedMinValue(uint32_t numBits) {
335     return APInt(numBits, 0).set(numBits - 1);
336   }
337
338   /// getSignBit - This is just a wrapper function of getSignedMinValue(), and
339   /// it helps code readability when we want to get a SignBit.
340   /// @brief Get the SignBit for a specific bit width.
341   static APInt getSignBit(uint32_t BitWidth) {
342     return getSignedMinValue(BitWidth);
343   }
344
345   /// @returns the all-ones value for an APInt of the specified bit-width.
346   /// @brief Get the all-ones value.
347   static APInt getAllOnesValue(uint32_t numBits) {
348     return APInt(numBits, 0).set();
349   }
350
351   /// @returns the '0' value for an APInt of the specified bit-width.
352   /// @brief Get the '0' value.
353   static APInt getNullValue(uint32_t numBits) {
354     return APInt(numBits, 0);
355   }
356
357   /// Get an APInt with the same BitWidth as this APInt, just zero mask
358   /// the low bits and right shift to the least significant bit.
359   /// @returns the high "numBits" bits of this APInt.
360   APInt getHiBits(uint32_t numBits) const;
361
362   /// Get an APInt with the same BitWidth as this APInt, just zero mask
363   /// the high bits.
364   /// @returns the low "numBits" bits of this APInt.
365   APInt getLoBits(uint32_t numBits) const;
366
367   /// Constructs an APInt value that has a contiguous range of bits set. The
368   /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
369   /// bits will be zero. For example, with parameters(32, 0, 16) you would get
370   /// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For
371   /// example, with parameters (32, 28, 4), you would get 0xF000000F. 
372   /// @param numBits the intended bit width of the result
373   /// @param loBit the index of the lowest bit set.
374   /// @param hiBit the index of the highest bit set.
375   /// @returns An APInt value with the requested bits set.
376   /// @brief Get a value with a block of bits set.
377   static APInt getBitsSet(uint32_t numBits, uint32_t loBit, uint32_t hiBit) {
378     assert(hiBit <= numBits && "hiBit out of range");
379     assert(loBit < numBits && "loBit out of range");
380     if (hiBit < loBit)
381       return getLowBitsSet(numBits, hiBit) |
382              getHighBitsSet(numBits, numBits-loBit);
383     return getLowBitsSet(numBits, hiBit-loBit).shl(loBit);
384   }
385
386   /// Constructs an APInt value that has the top hiBitsSet bits set.
387   /// @param numBits the bitwidth of the result
388   /// @param hiBitsSet the number of high-order bits set in the result.
389   /// @brief Get a value with high bits set
390   static APInt getHighBitsSet(uint32_t numBits, uint32_t hiBitsSet) {
391     assert(hiBitsSet <= numBits && "Too many bits to set!");
392     // Handle a degenerate case, to avoid shifting by word size
393     if (hiBitsSet == 0)
394       return APInt(numBits, 0);
395     uint32_t shiftAmt = numBits - hiBitsSet;
396     // For small values, return quickly
397     if (numBits <= APINT_BITS_PER_WORD)
398       return APInt(numBits, ~0ULL << shiftAmt);
399     return (~APInt(numBits, 0)).shl(shiftAmt);
400   }
401
402   /// Constructs an APInt value that has the bottom loBitsSet bits set.
403   /// @param numBits the bitwidth of the result
404   /// @param loBitsSet the number of low-order bits set in the result.
405   /// @brief Get a value with low bits set
406   static APInt getLowBitsSet(uint32_t numBits, uint32_t loBitsSet) {
407     assert(loBitsSet <= numBits && "Too many bits to set!");
408     // Handle a degenerate case, to avoid shifting by word size
409     if (loBitsSet == 0)
410       return APInt(numBits, 0);
411     if (loBitsSet == APINT_BITS_PER_WORD)
412       return APInt(numBits, -1ULL);
413     // For small values, return quickly
414     if (numBits < APINT_BITS_PER_WORD)
415       return APInt(numBits, (1ULL << loBitsSet) - 1);
416     return (~APInt(numBits, 0)).lshr(numBits - loBitsSet);
417   }
418
419   /// The hash value is computed as the sum of the words and the bit width.
420   /// @returns A hash value computed from the sum of the APInt words.
421   /// @brief Get a hash value based on this APInt
422   uint64_t getHashValue() const;
423
424   /// This function returns a pointer to the internal storage of the APInt. 
425   /// This is useful for writing out the APInt in binary form without any
426   /// conversions.
427   const uint64_t* getRawData() const {
428     if (isSingleWord())
429       return &VAL;
430     return &pVal[0];
431   }
432
433   /// @}
434   /// @name Unary Operators
435   /// @{
436   /// @returns a new APInt value representing *this incremented by one
437   /// @brief Postfix increment operator.
438   const APInt operator++(int) {
439     APInt API(*this);
440     ++(*this);
441     return API;
442   }
443
444   /// @returns *this incremented by one
445   /// @brief Prefix increment operator.
446   APInt& operator++();
447
448   /// @returns a new APInt representing *this decremented by one.
449   /// @brief Postfix decrement operator. 
450   const APInt operator--(int) {
451     APInt API(*this);
452     --(*this);
453     return API;
454   }
455
456   /// @returns *this decremented by one.
457   /// @brief Prefix decrement operator. 
458   APInt& operator--();
459
460   /// Performs a bitwise complement operation on this APInt. 
461   /// @returns an APInt that is the bitwise complement of *this
462   /// @brief Unary bitwise complement operator. 
463   APInt operator~() const;
464
465   /// Negates *this using two's complement logic.
466   /// @returns An APInt value representing the negation of *this.
467   /// @brief Unary negation operator
468   APInt operator-() const {
469     return APInt(BitWidth, 0) - (*this);
470   }
471
472   /// Performs logical negation operation on this APInt.
473   /// @returns true if *this is zero, false otherwise.
474   /// @brief Logical negation operator. 
475   bool operator!() const;
476
477   /// @}
478   /// @name Assignment Operators
479   /// @{
480   /// @returns *this after assignment of RHS.
481   /// @brief Copy assignment operator. 
482   APInt& operator=(const APInt& RHS);
483
484   /// The RHS value is assigned to *this. If the significant bits in RHS exceed
485   /// the bit width, the excess bits are truncated. If the bit width is larger
486   /// than 64, the value is zero filled in the unspecified high order bits.
487   /// @returns *this after assignment of RHS value.
488   /// @brief Assignment operator. 
489   APInt& operator=(uint64_t RHS);
490
491   /// Performs a bitwise AND operation on this APInt and RHS. The result is
492   /// assigned to *this. 
493   /// @returns *this after ANDing with RHS.
494   /// @brief Bitwise AND assignment operator. 
495   APInt& operator&=(const APInt& RHS);
496
497   /// Performs a bitwise OR operation on this APInt and RHS. The result is 
498   /// assigned *this;
499   /// @returns *this after ORing with RHS.
500   /// @brief Bitwise OR assignment operator. 
501   APInt& operator|=(const APInt& RHS);
502
503   /// Performs a bitwise XOR operation on this APInt and RHS. The result is
504   /// assigned to *this.
505   /// @returns *this after XORing with RHS.
506   /// @brief Bitwise XOR assignment operator. 
507   APInt& operator^=(const APInt& RHS);
508
509   /// Multiplies this APInt by RHS and assigns the result to *this.
510   /// @returns *this
511   /// @brief Multiplication assignment operator. 
512   APInt& operator*=(const APInt& RHS);
513
514   /// Adds RHS to *this and assigns the result to *this.
515   /// @returns *this
516   /// @brief Addition assignment operator. 
517   APInt& operator+=(const APInt& RHS);
518
519   /// Subtracts RHS from *this and assigns the result to *this.
520   /// @returns *this
521   /// @brief Subtraction assignment operator. 
522   APInt& operator-=(const APInt& RHS);
523
524   /// Shifts *this left by shiftAmt and assigns the result to *this.
525   /// @returns *this after shifting left by shiftAmt
526   /// @brief Left-shift assignment function.
527   APInt& operator<<=(uint32_t shiftAmt) {
528     *this = shl(shiftAmt);
529     return *this;
530   }
531
532   /// @}
533   /// @name Binary Operators
534   /// @{
535   /// Performs a bitwise AND operation on *this and RHS.
536   /// @returns An APInt value representing the bitwise AND of *this and RHS.
537   /// @brief Bitwise AND operator. 
538   APInt operator&(const APInt& RHS) const;
539   APInt And(const APInt& RHS) const {
540     return this->operator&(RHS);
541   }
542
543   /// Performs a bitwise OR operation on *this and RHS.
544   /// @returns An APInt value representing the bitwise OR of *this and RHS.
545   /// @brief Bitwise OR operator. 
546   APInt operator|(const APInt& RHS) const;
547   APInt Or(const APInt& RHS) const {
548     return this->operator|(RHS);
549   }
550
551   /// Performs a bitwise XOR operation on *this and RHS.
552   /// @returns An APInt value representing the bitwise XOR of *this and RHS.
553   /// @brief Bitwise XOR operator. 
554   APInt operator^(const APInt& RHS) const;
555   APInt Xor(const APInt& RHS) const {
556     return this->operator^(RHS);
557   }
558
559   /// Multiplies this APInt by RHS and returns the result.
560   /// @brief Multiplication operator. 
561   APInt operator*(const APInt& RHS) const;
562
563   /// Adds RHS to this APInt and returns the result.
564   /// @brief Addition operator. 
565   APInt operator+(const APInt& RHS) const;
566   APInt operator+(uint64_t RHS) const {
567     return (*this) + APInt(BitWidth, RHS);
568   }
569
570   /// Subtracts RHS from this APInt and returns the result.
571   /// @brief Subtraction operator. 
572   APInt operator-(const APInt& RHS) const;
573   APInt operator-(uint64_t RHS) const {
574     return (*this) - APInt(BitWidth, RHS);
575   }
576   
577   APInt operator<<(unsigned Bits) const {
578     return shl(Bits);
579   }
580
581   APInt operator<<(const APInt &Bits) const {
582     return shl(Bits);
583   }
584
585   /// Arithmetic right-shift this APInt by shiftAmt.
586   /// @brief Arithmetic right-shift function.
587   APInt ashr(uint32_t shiftAmt) const;
588
589   /// Logical right-shift this APInt by shiftAmt.
590   /// @brief Logical right-shift function.
591   APInt lshr(uint32_t shiftAmt) const;
592
593   /// Left-shift this APInt by shiftAmt.
594   /// @brief Left-shift function.
595   APInt shl(uint32_t shiftAmt) const;
596
597   /// @brief Rotate left by rotateAmt.
598   APInt rotl(uint32_t rotateAmt) const;
599
600   /// @brief Rotate right by rotateAmt.
601   APInt rotr(uint32_t rotateAmt) const;
602
603   /// Arithmetic right-shift this APInt by shiftAmt.
604   /// @brief Arithmetic right-shift function.
605   APInt ashr(const APInt &shiftAmt) const;
606
607   /// Logical right-shift this APInt by shiftAmt.
608   /// @brief Logical right-shift function.
609   APInt lshr(const APInt &shiftAmt) const;
610
611   /// Left-shift this APInt by shiftAmt.
612   /// @brief Left-shift function.
613   APInt shl(const APInt &shiftAmt) const;
614
615   /// @brief Rotate left by rotateAmt.
616   APInt rotl(const APInt &rotateAmt) const;
617
618   /// @brief Rotate right by rotateAmt.
619   APInt rotr(const APInt &rotateAmt) const;
620
621   /// Perform an unsigned divide operation on this APInt by RHS. Both this and
622   /// RHS are treated as unsigned quantities for purposes of this division.
623   /// @returns a new APInt value containing the division result
624   /// @brief Unsigned division operation.
625   APInt udiv(const APInt& RHS) const;
626
627   /// Signed divide this APInt by APInt RHS.
628   /// @brief Signed division function for APInt.
629   APInt sdiv(const APInt& RHS) const {
630     if (isNegative())
631       if (RHS.isNegative())
632         return (-(*this)).udiv(-RHS);
633       else
634         return -((-(*this)).udiv(RHS));
635     else if (RHS.isNegative())
636       return -(this->udiv(-RHS));
637     return this->udiv(RHS);
638   }
639
640   /// Perform an unsigned remainder operation on this APInt with RHS being the
641   /// divisor. Both this and RHS are treated as unsigned quantities for purposes
642   /// of this operation. Note that this is a true remainder operation and not
643   /// a modulo operation because the sign follows the sign of the dividend
644   /// which is *this.
645   /// @returns a new APInt value containing the remainder result
646   /// @brief Unsigned remainder operation.
647   APInt urem(const APInt& RHS) const;
648
649   /// Signed remainder operation on APInt.
650   /// @brief Function for signed remainder operation.
651   APInt srem(const APInt& RHS) const {
652     if (isNegative())
653       if (RHS.isNegative())
654         return -((-(*this)).urem(-RHS));
655       else
656         return -((-(*this)).urem(RHS));
657     else if (RHS.isNegative())
658       return this->urem(-RHS);
659     return this->urem(RHS);
660   }
661
662   /// Sometimes it is convenient to divide two APInt values and obtain both the
663   /// quotient and remainder. This function does both operations in the same
664   /// computation making it a little more efficient. The pair of input arguments
665   /// may overlap with the pair of output arguments. It is safe to call
666   /// udivrem(X, Y, X, Y), for example.
667   /// @brief Dual division/remainder interface.
668   static void udivrem(const APInt &LHS, const APInt &RHS, 
669                       APInt &Quotient, APInt &Remainder);
670
671   static void sdivrem(const APInt &LHS, const APInt &RHS,
672                       APInt &Quotient, APInt &Remainder)
673   {
674     if (LHS.isNegative()) {
675       if (RHS.isNegative())
676         APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
677       else
678         APInt::udivrem(-LHS, RHS, Quotient, Remainder);
679       Quotient = -Quotient;
680       Remainder = -Remainder;
681     } else if (RHS.isNegative()) {
682       APInt::udivrem(LHS, -RHS, Quotient, Remainder);
683       Quotient = -Quotient;
684     } else {
685       APInt::udivrem(LHS, RHS, Quotient, Remainder);
686     }
687   }
688
689   /// @returns the bit value at bitPosition
690   /// @brief Array-indexing support.
691   bool operator[](uint32_t bitPosition) const;
692
693   /// @}
694   /// @name Comparison Operators
695   /// @{
696   /// Compares this APInt with RHS for the validity of the equality
697   /// relationship.
698   /// @brief Equality operator. 
699   bool operator==(const APInt& RHS) const;
700
701   /// Compares this APInt with a uint64_t for the validity of the equality 
702   /// relationship.
703   /// @returns true if *this == Val
704   /// @brief Equality operator.
705   bool operator==(uint64_t Val) const;
706
707   /// Compares this APInt with RHS for the validity of the equality
708   /// relationship.
709   /// @returns true if *this == Val
710   /// @brief Equality comparison.
711   bool eq(const APInt &RHS) const {
712     return (*this) == RHS; 
713   }
714
715   /// Compares this APInt with RHS for the validity of the inequality
716   /// relationship.
717   /// @returns true if *this != Val
718   /// @brief Inequality operator. 
719   bool operator!=(const APInt& RHS) const {
720     return !((*this) == RHS);
721   }
722
723   /// Compares this APInt with a uint64_t for the validity of the inequality 
724   /// relationship.
725   /// @returns true if *this != Val
726   /// @brief Inequality operator. 
727   bool operator!=(uint64_t Val) const {
728     return !((*this) == Val);
729   }
730   
731   /// Compares this APInt with RHS for the validity of the inequality
732   /// relationship.
733   /// @returns true if *this != Val
734   /// @brief Inequality comparison
735   bool ne(const APInt &RHS) const {
736     return !((*this) == RHS);
737   }
738
739   /// Regards both *this and RHS as unsigned quantities and compares them for
740   /// the validity of the less-than relationship.
741   /// @returns true if *this < RHS when both are considered unsigned.
742   /// @brief Unsigned less than comparison
743   bool ult(const APInt& RHS) const;
744
745   /// Regards both *this and RHS as signed quantities and compares them for
746   /// validity of the less-than relationship.
747   /// @returns true if *this < RHS when both are considered signed.
748   /// @brief Signed less than comparison
749   bool slt(const APInt& RHS) const;
750
751   /// Regards both *this and RHS as unsigned quantities and compares them for
752   /// validity of the less-or-equal relationship.
753   /// @returns true if *this <= RHS when both are considered unsigned.
754   /// @brief Unsigned less or equal comparison
755   bool ule(const APInt& RHS) const {
756     return ult(RHS) || eq(RHS);
757   }
758
759   /// Regards both *this and RHS as signed quantities and compares them for
760   /// validity of the less-or-equal relationship.
761   /// @returns true if *this <= RHS when both are considered signed.
762   /// @brief Signed less or equal comparison
763   bool sle(const APInt& RHS) const {
764     return slt(RHS) || eq(RHS);
765   }
766
767   /// Regards both *this and RHS as unsigned quantities and compares them for
768   /// the validity of the greater-than relationship.
769   /// @returns true if *this > RHS when both are considered unsigned.
770   /// @brief Unsigned greather than comparison
771   bool ugt(const APInt& RHS) const {
772     return !ult(RHS) && !eq(RHS);
773   }
774
775   /// Regards both *this and RHS as signed quantities and compares them for
776   /// the validity of the greater-than relationship.
777   /// @returns true if *this > RHS when both are considered signed.
778   /// @brief Signed greather than comparison
779   bool sgt(const APInt& RHS) const {
780     return !slt(RHS) && !eq(RHS);
781   }
782
783   /// Regards both *this and RHS as unsigned quantities and compares them for
784   /// validity of the greater-or-equal relationship.
785   /// @returns true if *this >= RHS when both are considered unsigned.
786   /// @brief Unsigned greater or equal comparison
787   bool uge(const APInt& RHS) const {
788     return !ult(RHS);
789   }
790
791   /// Regards both *this and RHS as signed quantities and compares them for
792   /// validity of the greater-or-equal relationship.
793   /// @returns true if *this >= RHS when both are considered signed.
794   /// @brief Signed greather or equal comparison
795   bool sge(const APInt& RHS) const {
796     return !slt(RHS);
797   }
798
799   /// This operation tests if there are any pairs of corresponding bits
800   /// between this APInt and RHS that are both set.
801   bool intersects(const APInt &RHS) const {
802     return (*this & RHS) != 0;
803   }
804
805   /// @}
806   /// @name Resizing Operators
807   /// @{
808   /// Truncate the APInt to a specified width. It is an error to specify a width
809   /// that is greater than or equal to the current width. 
810   /// @brief Truncate to new width.
811   APInt &trunc(uint32_t width);
812
813   /// This operation sign extends the APInt to a new width. If the high order
814   /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
815   /// It is an error to specify a width that is less than or equal to the 
816   /// current width.
817   /// @brief Sign extend to a new width.
818   APInt &sext(uint32_t width);
819
820   /// This operation zero extends the APInt to a new width. The high order bits
821   /// are filled with 0 bits.  It is an error to specify a width that is less 
822   /// than or equal to the current width.
823   /// @brief Zero extend to a new width.
824   APInt &zext(uint32_t width);
825
826   /// Make this APInt have the bit width given by \p width. The value is sign
827   /// extended, truncated, or left alone to make it that width.
828   /// @brief Sign extend or truncate to width
829   APInt &sextOrTrunc(uint32_t width);
830
831   /// Make this APInt have the bit width given by \p width. The value is zero
832   /// extended, truncated, or left alone to make it that width.
833   /// @brief Zero extend or truncate to width
834   APInt &zextOrTrunc(uint32_t width);
835
836   /// @}
837   /// @name Bit Manipulation Operators
838   /// @{
839   /// @brief Set every bit to 1.
840   APInt& set();
841
842   /// Set the given bit to 1 whose position is given as "bitPosition".
843   /// @brief Set a given bit to 1.
844   APInt& set(uint32_t bitPosition);
845
846   /// @brief Set every bit to 0.
847   APInt& clear();
848
849   /// Set the given bit to 0 whose position is given as "bitPosition".
850   /// @brief Set a given bit to 0.
851   APInt& clear(uint32_t bitPosition);
852
853   /// @brief Toggle every bit to its opposite value.
854   APInt& flip();
855
856   /// Toggle a given bit to its opposite value whose position is given 
857   /// as "bitPosition".
858   /// @brief Toggles a given bit to its opposite value.
859   APInt& flip(uint32_t bitPosition);
860
861   /// @}
862   /// @name Value Characterization Functions
863   /// @{
864
865   /// @returns the total number of bits.
866   uint32_t getBitWidth() const { 
867     return BitWidth; 
868   }
869
870   /// Here one word's bitwidth equals to that of uint64_t.
871   /// @returns the number of words to hold the integer value of this APInt.
872   /// @brief Get the number of words.
873   uint32_t getNumWords() const {
874     return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
875   }
876
877   /// This function returns the number of active bits which is defined as the
878   /// bit width minus the number of leading zeros. This is used in several
879   /// computations to see how "wide" the value is.
880   /// @brief Compute the number of active bits in the value
881   uint32_t getActiveBits() const {
882     return BitWidth - countLeadingZeros();
883   }
884
885   /// This function returns the number of active words in the value of this
886   /// APInt. This is used in conjunction with getActiveData to extract the raw
887   /// value of the APInt.
888   uint32_t getActiveWords() const {
889     return whichWord(getActiveBits()-1) + 1;
890   }
891
892   /// Computes the minimum bit width for this APInt while considering it to be
893   /// a signed (and probably negative) value. If the value is not negative, 
894   /// this function returns the same value as getActiveBits()+1. Otherwise, it
895   /// returns the smallest bit width that will retain the negative value. For
896   /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
897   /// for -1, this function will always return 1.
898   /// @brief Get the minimum bit size for this signed APInt 
899   uint32_t getMinSignedBits() const {
900     if (isNegative())
901       return BitWidth - countLeadingOnes() + 1;
902     return getActiveBits()+1;
903   }
904
905   /// This method attempts to return the value of this APInt as a zero extended
906   /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
907   /// uint64_t. Otherwise an assertion will result.
908   /// @brief Get zero extended value
909   uint64_t getZExtValue() const {
910     if (isSingleWord())
911       return VAL;
912     assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
913     return pVal[0];
914   }
915
916   /// This method attempts to return the value of this APInt as a sign extended
917   /// int64_t. The bit width must be <= 64 or the value must fit within an
918   /// int64_t. Otherwise an assertion will result.
919   /// @brief Get sign extended value
920   int64_t getSExtValue() const {
921     if (isSingleWord())
922       return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >> 
923                      (APINT_BITS_PER_WORD - BitWidth);
924     assert(getActiveBits() <= 64 && "Too many bits for int64_t");
925     return int64_t(pVal[0]);
926   }
927
928   /// This method determines how many bits are required to hold the APInt
929   /// equivalent of the string given by \p str of length \p slen.
930   /// @brief Get bits required for string value.
931   static uint32_t getBitsNeeded(const char* str, uint32_t slen, uint8_t radix);
932
933   /// countLeadingZeros - This function is an APInt version of the
934   /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
935   /// of zeros from the most significant bit to the first one bit.
936   /// @returns BitWidth if the value is zero.
937   /// @returns the number of zeros from the most significant bit to the first
938   /// one bits.
939   uint32_t countLeadingZeros() const;
940
941   /// countLeadingOnes - This function is an APInt version of the
942   /// countLeadingOnes_{32,64} functions in MathExtras.h. It counts the number
943   /// of ones from the most significant bit to the first zero bit.
944   /// @returns 0 if the high order bit is not set
945   /// @returns the number of 1 bits from the most significant to the least
946   /// @brief Count the number of leading one bits.
947   uint32_t countLeadingOnes() const;
948
949   /// countTrailingZeros - This function is an APInt version of the 
950   /// countTrailingZeros_{32,64} functions in MathExtras.h. It counts 
951   /// the number of zeros from the least significant bit to the first set bit.
952   /// @returns BitWidth if the value is zero.
953   /// @returns the number of zeros from the least significant bit to the first
954   /// one bit.
955   /// @brief Count the number of trailing zero bits.
956   uint32_t countTrailingZeros() const;
957
958   /// countTrailingOnes - This function is an APInt version of the 
959   /// countTrailingOnes_{32,64} functions in MathExtras.h. It counts 
960   /// the number of ones from the least significant bit to the first zero bit.
961   /// @returns BitWidth if the value is all ones.
962   /// @returns the number of ones from the least significant bit to the first
963   /// zero bit.
964   /// @brief Count the number of trailing one bits.
965   uint32_t countTrailingOnes() const;
966
967   /// countPopulation - This function is an APInt version of the
968   /// countPopulation_{32,64} functions in MathExtras.h. It counts the number
969   /// of 1 bits in the APInt value. 
970   /// @returns 0 if the value is zero.
971   /// @returns the number of set bits.
972   /// @brief Count the number of bits set.
973   uint32_t countPopulation() const; 
974
975   /// @}
976   /// @name Conversion Functions
977   /// @{
978
979   void print(std::ostream &OS, bool isSigned) const;
980   
981   /// toString - Converts an APInt to a string and append it to Str.  Str is
982   /// commonly a SmallString.
983   void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed) const;
984
985   /// Considers the APInt to be unsigned and converts it into a string in the
986   /// radix given. The radix can be 2, 8, 10 or 16.
987   void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
988     return toString(Str, Radix, false);
989   }
990
991   /// Considers the APInt to be signed and converts it into a string in the
992   /// radix given. The radix can be 2, 8, 10 or 16.
993   void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
994     return toString(Str, Radix, true);
995   }
996   
997   /// toString - This returns the APInt as a std::string.  Note that this is an
998   /// inefficient method.  It is better to pass in a SmallVector/SmallString
999   /// to the methods above to avoid thrashing the heap for the string.
1000   std::string toString(unsigned Radix, bool Signed) const;
1001   
1002
1003   /// @returns a byte-swapped representation of this APInt Value.
1004   APInt byteSwap() const;
1005
1006   /// @brief Converts this APInt to a double value.
1007   double roundToDouble(bool isSigned) const;
1008
1009   /// @brief Converts this unsigned APInt to a double value.
1010   double roundToDouble() const {
1011     return roundToDouble(false);
1012   }
1013
1014   /// @brief Converts this signed APInt to a double value.
1015   double signedRoundToDouble() const {
1016     return roundToDouble(true);
1017   }
1018
1019   /// The conversion does not do a translation from integer to double, it just
1020   /// re-interprets the bits as a double. Note that it is valid to do this on
1021   /// any bit width. Exactly 64 bits will be translated.
1022   /// @brief Converts APInt bits to a double
1023   double bitsToDouble() const {
1024     union {
1025       uint64_t I;
1026       double D;
1027     } T;
1028     T.I = (isSingleWord() ? VAL : pVal[0]);
1029     return T.D;
1030   }
1031
1032   /// The conversion does not do a translation from integer to float, it just
1033   /// re-interprets the bits as a float. Note that it is valid to do this on
1034   /// any bit width. Exactly 32 bits will be translated.
1035   /// @brief Converts APInt bits to a double
1036   float bitsToFloat() const {
1037     union {
1038       uint32_t I;
1039       float F;
1040     } T;
1041     T.I = uint32_t((isSingleWord() ? VAL : pVal[0]));
1042     return T.F;
1043   }
1044
1045   /// The conversion does not do a translation from double to integer, it just
1046   /// re-interprets the bits of the double. Note that it is valid to do this on
1047   /// any bit width but bits from V may get truncated.
1048   /// @brief Converts a double to APInt bits.
1049   APInt& doubleToBits(double V) {
1050     union {
1051       uint64_t I;
1052       double D;
1053     } T;
1054     T.D = V;
1055     if (isSingleWord())
1056       VAL = T.I;
1057     else
1058       pVal[0] = T.I;
1059     return clearUnusedBits();
1060   }
1061
1062   /// The conversion does not do a translation from float to integer, it just
1063   /// re-interprets the bits of the float. Note that it is valid to do this on
1064   /// any bit width but bits from V may get truncated.
1065   /// @brief Converts a float to APInt bits.
1066   APInt& floatToBits(float V) {
1067     union {
1068       uint32_t I;
1069       float F;
1070     } T;
1071     T.F = V;
1072     if (isSingleWord())
1073       VAL = T.I;
1074     else
1075       pVal[0] = T.I;
1076     return clearUnusedBits();
1077   }
1078
1079   /// @}
1080   /// @name Mathematics Operations
1081   /// @{
1082
1083   /// @returns the floor log base 2 of this APInt.
1084   uint32_t logBase2() const {
1085     return BitWidth - 1 - countLeadingZeros();
1086   }
1087
1088   /// @returns the log base 2 of this APInt if its an exact power of two, -1
1089   /// otherwise
1090   int32_t exactLogBase2() const {
1091     if (!isPowerOf2())
1092       return -1;
1093     return logBase2();
1094   }
1095
1096   /// @brief Compute the square root
1097   APInt sqrt() const;
1098
1099   /// If *this is < 0 then return -(*this), otherwise *this;
1100   /// @brief Get the absolute value;
1101   APInt abs() const {
1102     if (isNegative())
1103       return -(*this);
1104     return *this;
1105   }
1106
1107   /// @returns the multiplicative inverse for a given modulo.
1108   APInt multiplicativeInverse(const APInt& modulo) const;
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 inline std::ostream &operator<<(std::ostream &OS, const APInt &I) {
1249   I.print(OS, true);
1250   return OS;
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