getActiveWords should return the number of words, not the index of the
[oota-llvm.git] / include / llvm / ADT / APInt.h
1 //===-- llvm/Support/APInt.h - For Arbitrary Precision Integer -*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Sheng Zhou and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a class to represent arbitrary precision integral
11 // constant values.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_APINT_H
16 #define LLVM_APINT_H
17
18 #include "llvm/Support/DataTypes.h"
19 #include <cassert>
20 #include <string>
21
22 namespace llvm {
23
24 /// Forward declaration.
25 class APInt;
26 namespace APIntOps {
27   APInt udiv(const APInt& LHS, const APInt& RHS);
28   APInt urem(const APInt& LHS, const APInt& RHS);
29 }
30
31 //===----------------------------------------------------------------------===//
32 //                              APInt Class
33 //===----------------------------------------------------------------------===//
34
35 /// APInt - This class represents arbitrary precision constant integral values.
36 /// It is a functional replacement for common case unsigned integer type like 
37 /// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width 
38 /// integer sizes and large integer value types such as 3-bits, 15-bits, or more
39 /// than 64-bits of precision. APInt provides a variety of arithmetic operators 
40 /// and methods to manipulate integer values of any bit-width. It supports both
41 /// the typical integer arithmetic and comparison operations as well as bitwise
42 /// manipulation.
43 ///
44 /// The class has several invariants worth noting:
45 ///   * All bit, byte, and word positions are zero-based.
46 ///   * Once the bit width is set, it doesn't change except by the Truncate, 
47 ///     SignExtend, or ZeroExtend operations.
48 ///   * All binary operators must be on APInt instances of the same bit width.
49 ///     Attempting to use these operators on instances with different bit 
50 ///     widths will yield an assertion.
51 ///   * The value is stored canonically as an unsigned value. For operations
52 ///     where it makes a difference, there are both signed and unsigned variants
53 ///     of the operation. For example, sdiv and udiv. However, because the bit
54 ///     widths must be the same, operations such as Mul and Add produce the same
55 ///     results regardless of whether the values are interpreted as signed or
56 ///     not.
57 ///   * In general, the class tries to follow the style of computation that LLVM
58 ///     uses in its IR. This simplifies its use for LLVM.
59 ///
60 /// @brief Class for arbitrary precision integers.
61 class APInt {
62
63   uint32_t BitWidth;      ///< The number of bits in this APInt.
64
65   /// This union is used to store the integer value. When the
66   /// integer bit-width <= 64, it uses VAL; 
67   /// otherwise it uses the pVal.
68   union {
69     uint64_t VAL;    ///< Used to store the <= 64 bits integer value.
70     uint64_t *pVal;  ///< Used to store the >64 bits integer value.
71   };
72
73   /// This enum is just used to hold a constant we needed for APInt.
74   enum {
75     APINT_BITS_PER_WORD = sizeof(uint64_t) * 8,
76     APINT_WORD_SIZE = sizeof(uint64_t)
77   };
78
79   // Fast internal constructor
80   APInt(uint64_t* val, uint32_t bits) : BitWidth(bits), pVal(val) { }
81
82   /// Here one word's bitwidth equals to that of uint64_t.
83   /// @returns the number of words to hold the integer value of this APInt.
84   /// @brief Get the number of words.
85   inline uint32_t getNumWords() const {
86     return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
87   }
88
89   /// @returns true if the number of bits <= 64, false otherwise.
90   /// @brief Determine if this APInt just has one word to store value.
91   inline bool isSingleWord() const { 
92     return BitWidth <= APINT_BITS_PER_WORD; 
93   }
94
95   /// @returns the word position for the specified bit position.
96   static inline uint32_t whichWord(uint32_t bitPosition) { 
97     return bitPosition / APINT_BITS_PER_WORD; 
98   }
99
100   /// @returns the bit position in a word for the specified bit position 
101   /// in APInt.
102   static inline uint32_t whichBit(uint32_t bitPosition) { 
103     return bitPosition % APINT_BITS_PER_WORD; 
104   }
105
106   /// @returns a uint64_t type integer with just bit position at
107   /// "whichBit(bitPosition)" setting, others zero.
108   static inline uint64_t maskBit(uint32_t bitPosition) { 
109     return (static_cast<uint64_t>(1)) << whichBit(bitPosition); 
110   }
111
112   /// This method is used internally to clear the to "N" bits that are not used
113   /// by the APInt. This is needed after the most significant word is assigned 
114   /// a value to ensure that those bits are zero'd out.
115   /// @brief Clear high order bits
116   inline APInt& clearUnusedBits() {
117     // Compute how many bits are used in the final word
118     uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
119     if (wordBits == 0)
120       // If all bits are used, we want to leave the value alone. This also
121       // avoids the undefined behavior of >> when the shfit is the same size as
122       // the word size (64).
123       return *this;
124
125     // Mask out the hight bits.
126     uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
127     if (isSingleWord())
128       VAL &= mask;
129     else
130       pVal[getNumWords() - 1] &= mask;
131     return *this;
132   }
133
134   /// @returns the corresponding word for the specified bit position.
135   /// @brief Get the word corresponding to a bit position
136   inline uint64_t getWord(uint32_t bitPosition) const { 
137     return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; 
138   }
139
140   /// This is used by the constructors that take string arguments.
141   /// @brief Converts a char array into an APInt
142   void fromString(uint32_t numBits, const char *StrStart, uint32_t slen, 
143                   uint8_t radix);
144
145   /// This is used by the toString method to divide by the radix. It simply
146   /// provides a more convenient form of divide for internal use since KnuthDiv
147   /// has specific constraints on its inputs. If those constraints are not met
148   /// then it provides a simpler form of divide.
149   /// @brief An internal division function for dividing APInts.
150   static void divide(const APInt LHS, uint32_t lhsWords, 
151                      const APInt &RHS, uint32_t rhsWords,
152                      APInt *Quotient, APInt *Remainder);
153
154 #ifndef NDEBUG
155   /// @brief debug method
156   void dump() const;
157 #endif
158
159 public:
160   /// @brief Create a new APInt of numBits width, initialized as val.
161   APInt(uint32_t numBits, uint64_t val);
162
163   /// Note that numWords can be smaller or larger than the corresponding bit
164   /// width but any extraneous bits will be dropped.
165   /// @brief Create a new APInt of numBits width, initialized as bigVal[].
166   APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[]);
167
168   /// @brief Create a new APInt by translating the string represented 
169   /// integer value.
170   APInt(uint32_t numBits, const std::string& Val, uint8_t radix);
171
172   /// @brief Create a new APInt by translating the char array represented
173   /// integer value.
174   APInt(uint32_t numBits, const char StrStart[], uint32_t slen, uint8_t radix);
175
176   /// @brief Copy Constructor.
177   APInt(const APInt& API);
178
179   /// @brief Destructor.
180   ~APInt();
181
182   /// @brief Copy assignment operator. 
183   APInt& operator=(const APInt& RHS);
184
185   /// Assigns an integer value to the APInt.
186   /// @brief Assignment operator. 
187   APInt& operator=(uint64_t RHS);
188
189   /// Increments the APInt by one.
190   /// @brief Postfix increment operator.
191   inline const APInt operator++(int) {
192     APInt API(*this);
193     ++(*this);
194     return API;
195   }
196
197   /// Increments the APInt by one.
198   /// @brief Prefix increment operator.
199   APInt& operator++();
200
201   /// Decrements the APInt by one.
202   /// @brief Postfix decrement operator. 
203   inline const APInt operator--(int) {
204     APInt API(*this);
205     --(*this);
206     return API;
207   }
208
209   /// Decrements the APInt by one.
210   /// @brief Prefix decrement operator. 
211   APInt& operator--();
212
213   /// Performs bitwise AND operation on this APInt and the given APInt& RHS, 
214   /// assigns the result to this APInt.
215   /// @brief Bitwise AND assignment operator. 
216   APInt& operator&=(const APInt& RHS);
217
218   /// Performs bitwise OR operation on this APInt and the given APInt& RHS, 
219   /// assigns the result to this APInt.
220   /// @brief Bitwise OR assignment operator. 
221   APInt& operator|=(const APInt& RHS);
222
223   /// Performs bitwise XOR operation on this APInt and the given APInt& RHS, 
224   /// assigns the result to this APInt.
225   /// @brief Bitwise XOR assignment operator. 
226   APInt& operator^=(const APInt& RHS);
227
228   /// Performs a bitwise complement operation on this APInt.
229   /// @brief Bitwise complement operator. 
230   APInt operator~() const;
231
232   /// Multiplies this APInt by the  given APInt& RHS and 
233   /// assigns the result to this APInt.
234   /// @brief Multiplication assignment operator. 
235   APInt& operator*=(const APInt& RHS);
236
237   /// Adds this APInt by the given APInt& RHS and 
238   /// assigns the result to this APInt.
239   /// @brief Addition assignment operator. 
240   APInt& operator+=(const APInt& RHS);
241
242   /// Subtracts this APInt by the given APInt &RHS and 
243   /// assigns the result to this APInt.
244   /// @brief Subtraction assignment operator. 
245   APInt& operator-=(const APInt& RHS);
246
247   /// Performs bitwise AND operation on this APInt and 
248   /// the given APInt& RHS.
249   /// @brief Bitwise AND operator. 
250   APInt operator&(const APInt& RHS) const;
251
252   /// Performs bitwise OR operation on this APInt and the given APInt& RHS.
253   /// @brief Bitwise OR operator. 
254   APInt operator|(const APInt& RHS) const;
255
256   /// Performs bitwise XOR operation on this APInt and the given APInt& RHS.
257   /// @brief Bitwise XOR operator. 
258   APInt operator^(const APInt& RHS) const;
259
260   /// Performs logical negation operation on this APInt.
261   /// @brief Logical negation operator. 
262   bool operator !() const;
263
264   /// Multiplies this APInt by the given APInt& RHS.
265   /// @brief Multiplication operator. 
266   APInt operator*(const APInt& RHS) const;
267
268   /// Adds this APInt by the given APInt& RHS.
269   /// @brief Addition operator. 
270   APInt operator+(const APInt& RHS) const;
271
272   /// Subtracts this APInt by the given APInt& RHS
273   /// @brief Subtraction operator. 
274   APInt operator-(const APInt& RHS) const;
275
276   /// @brief Unary negation operator
277   inline APInt operator-() const {
278     return APInt(BitWidth, 0) - (*this);
279   }
280
281   /// @brief Array-indexing support.
282   bool operator[](uint32_t bitPosition) const;
283
284   /// Compare this APInt with the given APInt& RHS 
285   /// for the validity of the equality relationship.
286   /// @brief Equality operator. 
287   bool operator==(const APInt& RHS) const;
288
289   /// Compare this APInt with the given uint64_t value
290   /// for the validity of the equality relationship.
291   /// @brief Equality operator.
292   bool operator==(uint64_t Val) const;
293
294   /// Compare this APInt with the given APInt& RHS 
295   /// for the validity of the inequality relationship.
296   /// @brief Inequality operator. 
297   inline bool operator!=(const APInt& RHS) const {
298     return !((*this) == RHS);
299   }
300
301   /// Compare this APInt with the given uint64_t value 
302   /// for the validity of the inequality relationship.
303   /// @brief Inequality operator. 
304   inline bool operator!=(uint64_t Val) const {
305     return !((*this) == Val);
306   }
307   
308   /// @brief Equality comparison
309   bool eq(const APInt &RHS) const {
310     return (*this) == RHS; 
311   }
312
313   /// @brief Inequality comparison
314   bool ne(const APInt &RHS) const {
315     return !((*this) == RHS);
316   }
317
318   /// @brief Unsigned less than comparison
319   bool ult(const APInt& RHS) const;
320
321   /// @brief Signed less than comparison
322   bool slt(const APInt& RHS) const;
323
324   /// @brief Unsigned less or equal comparison
325   bool ule(const APInt& RHS) const {
326     return ult(RHS) || eq(RHS);
327   }
328
329   /// @brief Signed less or equal comparison
330   bool sle(const APInt& RHS) const {
331     return slt(RHS) || eq(RHS);
332   }
333
334   /// @brief Unsigned greather than comparison
335   bool ugt(const APInt& RHS) const {
336     return !ult(RHS) && !eq(RHS);
337   }
338
339   /// @brief Signed greather than comparison
340   bool sgt(const APInt& RHS) const {
341     return !slt(RHS) && !eq(RHS);
342   }
343
344   /// @brief Unsigned greater or equal comparison
345   bool uge(const APInt& RHS) const {
346     return !ult(RHS);
347   }
348
349   /// @brief Signed greather or equal comparison
350   bool sge(const APInt& RHS) const {
351     return !slt(RHS);
352   }
353
354   /// This just tests the high bit of this APInt to determine if it is negative.
355   /// @returns true if this APInt is negative, false otherwise
356   /// @brief Determine sign of this APInt.
357   bool isNegative() const {
358     return (*this)[BitWidth - 1];
359   }
360
361   /// Arithmetic right-shift this APInt by shiftAmt.
362   /// @brief Arithmetic right-shift function.
363   APInt ashr(uint32_t shiftAmt) const;
364
365   /// Logical right-shift this APInt by shiftAmt.
366   /// @brief Logical right-shift function.
367   APInt lshr(uint32_t shiftAmt) const;
368
369   /// Left-shift this APInt by shiftAmt.
370   /// @brief Left-shift function.
371   APInt shl(uint32_t shiftAmt) const;
372
373   /// Signed divide this APInt by APInt RHS.
374   /// @brief Signed division function for APInt.
375   inline APInt sdiv(const APInt& RHS) const {
376     bool isNegativeLHS = isNegative();
377     bool isNegativeRHS = RHS.isNegative();
378     APInt Result = APIntOps::udiv(
379         isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
380     return isNegativeLHS != isNegativeRHS ? -Result : Result;
381   }
382
383   /// Unsigned divide this APInt by APInt RHS.
384   /// @brief Unsigned division function for APInt.
385   APInt udiv(const APInt& RHS) const;
386
387   /// Signed remainder operation on APInt.
388   /// @brief Function for signed remainder operation.
389   inline APInt srem(const APInt& RHS) const {
390     bool isNegativeLHS = isNegative();
391     bool isNegativeRHS = RHS.isNegative();
392     APInt Result = APIntOps::urem(
393         isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
394     return isNegativeLHS ? -Result : Result;
395   }
396
397   /// Unsigned remainder operation on APInt.
398   /// @brief Function for unsigned remainder operation.
399   APInt urem(const APInt& RHS) const;
400
401   /// Truncate the APInt to a specified width. It is an error to specify a width
402   /// that is greater than or equal to the current width. 
403   /// @brief Truncate to new width.
404   void trunc(uint32_t width);
405
406   /// This operation sign extends the APInt to a new width. If the high order
407   /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
408   /// It is an error to specify a width that is less than or equal to the 
409   /// current width.
410   /// @brief Sign extend to a new width.
411   void sext(uint32_t width);
412
413   /// This operation zero extends the APInt to a new width. Thie high order bits
414   /// are filled with 0 bits.  It is an error to specify a width that is less 
415   /// than or equal to the current width.
416   /// @brief Zero extend to a new width.
417   void zext(uint32_t width);
418
419   /// @brief Set every bit to 1.
420   APInt& set();
421
422   /// Set the given bit to 1 whose position is given as "bitPosition".
423   /// @brief Set a given bit to 1.
424   APInt& set(uint32_t bitPosition);
425
426   /// @brief Set every bit to 0.
427   APInt& clear();
428
429   /// Set the given bit to 0 whose position is given as "bitPosition".
430   /// @brief Set a given bit to 0.
431   APInt& clear(uint32_t bitPosition);
432
433   /// @brief Toggle every bit to its opposite value.
434   APInt& flip();
435
436   /// Toggle a given bit to its opposite value whose position is given 
437   /// as "bitPosition".
438   /// @brief Toggles a given bit to its opposite value.
439   APInt& flip(uint32_t bitPosition);
440
441   /// This function returns the number of active bits which is defined as the
442   /// bit width minus the number of leading zeros. This is used in several
443   /// computations to see how "wide" the value is.
444   /// @brief Compute the number of active bits in the value
445   inline uint32_t getActiveBits() const {
446     return BitWidth - countLeadingZeros();
447   }
448
449   /// This function returns the number of active words in the value of this
450   /// APInt. This is used in conjunction with getActiveData to extract the raw
451   /// value of the APInt.
452   inline uint32_t getActiveWords() const {
453     return whichWord(getActiveBits()-1) + 1;
454   }
455
456   /// This function returns a pointer to the internal storage of the APInt. 
457   /// This is useful for writing out the APInt in binary form without any
458   /// conversions.
459   inline const uint64_t* getRawData() const {
460     if (isSingleWord())
461       return &VAL;
462     return &pVal[0];
463   }
464
465   /// Computes the minimum bit width for this APInt while considering it to be
466   /// a signed (and probably negative) value. If the value is not negative, 
467   /// this function returns the same value as getActiveBits(). Otherwise, it
468   /// returns the smallest bit width that will retain the negative value. For
469   /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
470   /// for -1, this function will always return 1.
471   /// @brief Get the minimum bit size for this signed APInt 
472   inline uint32_t getMinSignedBits() const {
473     if (isNegative())
474       return BitWidth - countLeadingOnes() + 1;
475     return getActiveBits();
476   }
477
478   /// This method attempts to return the value of this APInt as a zero extended
479   /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
480   /// uint64_t. Otherwise an assertion will result.
481   /// @brief Get zero extended value
482   inline uint64_t getZExtValue() const {
483     if (isSingleWord())
484       return VAL;
485     assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
486     return pVal[0];
487   }
488
489   /// This method attempts to return the value of this APInt as a sign extended
490   /// int64_t. The bit width must be <= 64 or the value must fit within an
491   /// int64_t. Otherwise an assertion will result.
492   /// @brief Get sign extended value
493   inline int64_t getSExtValue() const {
494     if (isSingleWord())
495       return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >> 
496                      (APINT_BITS_PER_WORD - BitWidth);
497     assert(getActiveBits() <= 64 && "Too many bits for int64_t");
498     return int64_t(pVal[0]);
499   }
500
501   /// @brief Gets maximum unsigned value of APInt for specific bit width.
502   static APInt getMaxValue(uint32_t numBits) {
503     return APInt(numBits, 0).set();
504   }
505
506   /// @brief Gets maximum signed value of APInt for a specific bit width.
507   static APInt getSignedMaxValue(uint32_t numBits) {
508     return APInt(numBits, 0).set().clear(numBits - 1);
509   }
510
511   /// @brief Gets minimum unsigned value of APInt for a specific bit width.
512   static APInt getMinValue(uint32_t numBits) {
513     return APInt(numBits, 0);
514   }
515
516   /// @brief Gets minimum signed value of APInt for a specific bit width.
517   static APInt getSignedMinValue(uint32_t numBits) {
518     return APInt(numBits, 0).set(numBits - 1);
519   }
520
521   /// @returns the all-ones value for an APInt of the specified bit-width.
522   /// @brief Get the all-ones value.
523   static APInt getAllOnesValue(uint32_t numBits) {
524     return APInt(numBits, 0).set();
525   }
526
527   /// @returns the '0' value for an APInt of the specified bit-width.
528   /// @brief Get the '0' value.
529   static APInt getNullValue(uint32_t numBits) {
530     return APInt(numBits, 0);
531   }
532
533   /// The hash value is computed as the sum of the words and the bit width.
534   /// @returns A hash value computed from the sum of the APInt words.
535   /// @brief Get a hash value based on this APInt
536   uint64_t getHashValue() const;
537
538   /// This converts the APInt to a boolean valy as a test against zero.
539   /// @brief Boolean conversion function. 
540   inline bool getBoolValue() const {
541     return countLeadingZeros() != BitWidth;
542   }
543
544   /// This checks to see if the value has all bits of the APInt are set or not.
545   /// @brief Determine if all bits are set
546   inline bool isAllOnesValue() const {
547     return countPopulation() == BitWidth;
548   }
549
550   /// This checks to see if the value of this APInt is the maximum unsigned
551   /// value for the APInt's bit width.
552   /// @brief Determine if this is the largest unsigned value.
553   bool isMaxValue() const {
554     return countPopulation() == BitWidth;
555   }
556
557   /// This checks to see if the value of this APInt is the maximum signed
558   /// value for the APInt's bit width.
559   /// @brief Determine if this is the largest signed value.
560   bool isMaxSignedValue() const {
561     return BitWidth == 1 ? VAL == 0 :
562                           !isNegative() && countPopulation() == BitWidth - 1;
563   }
564
565   /// This checks to see if the value of this APInt is the minimum signed
566   /// value for the APInt's bit width.
567   /// @brief Determine if this is the smallest unsigned value.
568   bool isMinValue() const {
569     return countPopulation() == 0;
570   }
571
572   /// This checks to see if the value of this APInt is the minimum signed
573   /// value for the APInt's bit width.
574   /// @brief Determine if this is the smallest signed value.
575   bool isMinSignedValue() const {
576     return BitWidth == 1 ? VAL == 1 :
577                            isNegative() && countPopulation() == 1;
578   }
579
580   /// This is used internally to convert an APInt to a string.
581   /// @brief Converts an APInt to a std::string
582   std::string toString(uint8_t radix, bool wantSigned) const;
583
584   /// Considers the APInt to be unsigned and converts it into a string in the
585   /// radix given. The radix can be 2, 8, 10 or 16.
586   /// @returns a character interpretation of the APInt
587   /// @brief Convert unsigned APInt to string representation.
588   inline std::string toString(uint8_t radix = 10) const {
589     return toString(radix, false);
590   }
591
592   /// Considers the APInt to be unsigned and converts it into a string in the
593   /// radix given. The radix can be 2, 8, 10 or 16.
594   /// @returns a character interpretation of the APInt
595   /// @brief Convert unsigned APInt to string representation.
596   inline std::string toStringSigned(uint8_t radix = 10) const {
597     return toString(radix, true);
598   }
599
600   /// Get an APInt with the same BitWidth as this APInt, just zero mask
601   /// the low bits and right shift to the least significant bit.
602   /// @returns the high "numBits" bits of this APInt.
603   APInt getHiBits(uint32_t numBits) const;
604
605   /// Get an APInt with the same BitWidth as this APInt, just zero mask
606   /// the high bits.
607   /// @returns the low "numBits" bits of this APInt.
608   APInt getLoBits(uint32_t numBits) const;
609
610   /// @returns true if the argument APInt value is a power of two > 0.
611   bool isPowerOf2() const; 
612
613   /// countLeadingZeros - This function is an APInt version of the
614   /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
615   /// of zeros from the most significant bit to the first one bit.
616   /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
617   /// @returns the number of zeros from the most significant bit to the first
618   /// one bits.
619   /// @brief Count the number of leading one bits.
620   uint32_t countLeadingZeros() const;
621
622   /// countLeadingOnes - This function counts the number of contiguous 1 bits
623   /// in the high order bits. The count stops when the first 0 bit is reached.
624   /// @returns 0 if the high order bit is not set
625   /// @returns the number of 1 bits from the most significant to the least
626   /// @brief Count the number of leading one bits.
627   uint32_t countLeadingOnes() const;
628
629   /// countTrailingZeros - This function is an APInt version of the 
630   /// countTrailingZoers_{32,64} functions in MathExtras.h. It counts 
631   /// the number of zeros from the least significant bit to the first one bit.
632   /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
633   /// @returns the number of zeros from the least significant bit to the first
634   /// one bit.
635   /// @brief Count the number of trailing zero bits.
636   uint32_t countTrailingZeros() const;
637
638   /// countPopulation - This function is an APInt version of the
639   /// countPopulation_{32,64} functions in MathExtras.h. It counts the number
640   /// of 1 bits in the APInt value. 
641   /// @returns 0 if the value is zero.
642   /// @returns the number of set bits.
643   /// @brief Count the number of bits set.
644   uint32_t countPopulation() const; 
645
646   /// @returns the total number of bits.
647   inline uint32_t getBitWidth() const { 
648     return BitWidth; 
649   }
650
651   /// @brief Check if this APInt has a N-bits integer value.
652   inline bool isIntN(uint32_t N) const {
653     assert(N && "N == 0 ???");
654     if (isSingleWord()) {
655       return VAL == (VAL & (~0ULL >> (64 - N)));
656     } else {
657       APInt Tmp(N, getNumWords(), pVal);
658       return Tmp == (*this);
659     }
660   }
661
662   /// @returns a byte-swapped representation of this APInt Value.
663   APInt byteSwap() const;
664
665   /// @returns the floor log base 2 of this APInt.
666   inline uint32_t logBase2() const {
667     return getNumWords() * APINT_BITS_PER_WORD - 1 - countLeadingZeros();
668   }
669
670   /// @brief Converts this APInt to a double value.
671   double roundToDouble(bool isSigned) const;
672
673   /// @brief Converts this unsigned APInt to a double value.
674   double roundToDouble() const {
675     return roundToDouble(false);
676   }
677
678   /// @brief Converts this signed APInt to a double value.
679   double signedRoundToDouble() const {
680     return roundToDouble(true);
681   }
682 };
683
684 inline bool operator==(uint64_t V1, const APInt& V2) {
685   return V2 == V1;
686 }
687
688 inline bool operator!=(uint64_t V1, const APInt& V2) {
689   return V2 != V1;
690 }
691
692 namespace APIntOps {
693
694 /// @brief Check if the specified APInt has a N-bits integer value.
695 inline bool isIntN(uint32_t N, const APInt& APIVal) {
696   return APIVal.isIntN(N);
697 }
698
699 /// @returns true if the argument APInt value is a sequence of ones
700 /// starting at the least significant bit with the remainder zero.
701 inline const bool isMask(uint32_t numBits, const APInt& APIVal) {
702   return APIVal.getBoolValue() && ((APIVal + APInt(numBits,1)) & APIVal) == 0;
703 }
704
705 /// @returns true if the argument APInt value contains a sequence of ones
706 /// with the remainder zero.
707 inline const bool isShiftedMask(uint32_t numBits, const APInt& APIVal) {
708   return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
709 }
710
711 /// @returns a byte-swapped representation of the specified APInt Value.
712 inline APInt byteSwap(const APInt& APIVal) {
713   return APIVal.byteSwap();
714 }
715
716 /// @returns the floor log base 2 of the specified APInt value.
717 inline uint32_t logBase2(const APInt& APIVal) {
718   return APIVal.logBase2(); 
719 }
720
721 /// GreatestCommonDivisor - This function returns the greatest common
722 /// divisor of the two APInt values using Enclid's algorithm.
723 /// @returns the greatest common divisor of Val1 and Val2
724 /// @brief Compute GCD of two APInt values.
725 APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
726
727 /// Treats the APInt as an unsigned value for conversion purposes.
728 /// @brief Converts the given APInt to a double value.
729 inline double RoundAPIntToDouble(const APInt& APIVal) {
730   return APIVal.roundToDouble();
731 }
732
733 /// Treats the APInt as a signed value for conversion purposes.
734 /// @brief Converts the given APInt to a double value.
735 inline double RoundSignedAPIntToDouble(const APInt& APIVal) {
736   return APIVal.signedRoundToDouble();
737 }
738
739 /// @brief Converts the given APInt to a float vlalue.
740 inline float RoundAPIntToFloat(const APInt& APIVal) {
741   return float(RoundAPIntToDouble(APIVal));
742 }
743
744 /// RoundDoubleToAPInt - This function convert a double value to an APInt value.
745 /// @brief Converts the given double value into a APInt.
746 APInt RoundDoubleToAPInt(double Double, uint32_t width = 64);
747
748 /// RoundFloatToAPInt - Converts a float value into an APInt value.
749 /// @brief Converts a float value into a APInt.
750 inline APInt RoundFloatToAPInt(float Float) {
751   return RoundDoubleToAPInt(double(Float));
752 }
753
754 /// Arithmetic right-shift the APInt by shiftAmt.
755 /// @brief Arithmetic right-shift function.
756 inline APInt ashr(const APInt& LHS, uint32_t shiftAmt) {
757   return LHS.ashr(shiftAmt);
758 }
759
760 /// Logical right-shift the APInt by shiftAmt.
761 /// @brief Logical right-shift function.
762 inline APInt lshr(const APInt& LHS, uint32_t shiftAmt) {
763   return LHS.lshr(shiftAmt);
764 }
765
766 /// Left-shift the APInt by shiftAmt.
767 /// @brief Left-shift function.
768 inline APInt shl(const APInt& LHS, uint32_t shiftAmt) {
769   return LHS.shl(shiftAmt);
770 }
771
772 /// Signed divide APInt LHS by APInt RHS.
773 /// @brief Signed division function for APInt.
774 inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
775   return LHS.sdiv(RHS);
776 }
777
778 /// Unsigned divide APInt LHS by APInt RHS.
779 /// @brief Unsigned division function for APInt.
780 inline APInt udiv(const APInt& LHS, const APInt& RHS) {
781   return LHS.udiv(RHS);
782 }
783
784 /// Signed remainder operation on APInt.
785 /// @brief Function for signed remainder operation.
786 inline APInt srem(const APInt& LHS, const APInt& RHS) {
787   return LHS.srem(RHS);
788 }
789
790 /// Unsigned remainder operation on APInt.
791 /// @brief Function for unsigned remainder operation.
792 inline APInt urem(const APInt& LHS, const APInt& RHS) {
793   return LHS.urem(RHS);
794 }
795
796 /// Performs multiplication on APInt values.
797 /// @brief Function for multiplication operation.
798 inline APInt mul(const APInt& LHS, const APInt& RHS) {
799   return LHS * RHS;
800 }
801
802 /// Performs addition on APInt values.
803 /// @brief Function for addition operation.
804 inline APInt add(const APInt& LHS, const APInt& RHS) {
805   return LHS + RHS;
806 }
807
808 /// Performs subtraction on APInt values.
809 /// @brief Function for subtraction operation.
810 inline APInt sub(const APInt& LHS, const APInt& RHS) {
811   return LHS - RHS;
812 }
813
814 /// Performs bitwise AND operation on APInt LHS and 
815 /// APInt RHS.
816 /// @brief Bitwise AND function for APInt.
817 inline APInt And(const APInt& LHS, const APInt& RHS) {
818   return LHS & RHS;
819 }
820
821 /// Performs bitwise OR operation on APInt LHS and APInt RHS.
822 /// @brief Bitwise OR function for APInt. 
823 inline APInt Or(const APInt& LHS, const APInt& RHS) {
824   return LHS | RHS;
825 }
826
827 /// Performs bitwise XOR operation on APInt.
828 /// @brief Bitwise XOR function for APInt.
829 inline APInt Xor(const APInt& LHS, const APInt& RHS) {
830   return LHS ^ RHS;
831
832
833 /// Performs a bitwise complement operation on APInt.
834 /// @brief Bitwise complement function. 
835 inline APInt Not(const APInt& APIVal) {
836   return ~APIVal;
837 }
838
839 } // End of APIntOps namespace
840
841 } // End of llvm namespace
842
843 #endif