f37051e71cd1edab41df8ad33131a175933cdfad
[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 public:
63
64   uint32_t BitWidth;      ///< The number of bits in this APInt.
65
66   /// This union is used to store the integer value. When the
67   /// integer bit-width <= 64, it uses VAL; 
68   /// otherwise it uses the pVal.
69   union {
70     uint64_t VAL;    ///< Used to store the <= 64 bits integer value.
71     uint64_t *pVal;  ///< Used to store the >64 bits integer value.
72   };
73
74   /// This enum is just used to hold a constant we needed for APInt.
75   enum {
76     APINT_BITS_PER_WORD = sizeof(uint64_t) * 8,
77     APINT_WORD_SIZE = sizeof(uint64_t)
78   };
79
80   // Fast internal constructor
81   APInt(uint64_t* val, uint32_t bits) : BitWidth(bits), pVal(val) { }
82
83   /// Here one word's bitwidth equals to that of uint64_t.
84   /// @returns the number of words to hold the integer value of this APInt.
85   /// @brief Get the number of words.
86   inline uint32_t getNumWords() const {
87     return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
88   }
89
90   /// @returns true if the number of bits <= 64, false otherwise.
91   /// @brief Determine if this APInt just has one word to store value.
92   inline bool isSingleWord() const { 
93     return BitWidth <= APINT_BITS_PER_WORD; 
94   }
95
96   /// @returns the word position for the specified bit position.
97   static inline uint32_t whichWord(uint32_t bitPosition) { 
98     return bitPosition / APINT_BITS_PER_WORD; 
99   }
100
101   /// @returns the bit position in a word for the specified bit position 
102   /// in APInt.
103   static inline uint32_t whichBit(uint32_t bitPosition) { 
104     return bitPosition % APINT_BITS_PER_WORD; 
105   }
106
107   /// @returns a uint64_t type integer with just bit position at
108   /// "whichBit(bitPosition)" setting, others zero.
109   static inline uint64_t maskBit(uint32_t bitPosition) { 
110     return (static_cast<uint64_t>(1)) << whichBit(bitPosition); 
111   }
112
113   /// This method is used internally to clear the to "N" bits that are not used
114   /// by the APInt. This is needed after the most significant word is assigned 
115   /// a value to ensure that those bits are zero'd out.
116   /// @brief Clear high order bits
117   inline APInt& clearUnusedBits() {
118     // Compute how many bits are used in the final word
119     uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
120     if (wordBits == 0)
121       // If all bits are used, we want to leave the value alone. This also
122       // avoids the undefined behavior of >> when the shfit is the same size as
123       // the word size (64).
124       return *this;
125
126     // Mask out the hight bits.
127     uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
128     if (isSingleWord())
129       VAL &= mask;
130     else
131       pVal[getNumWords() - 1] &= mask;
132     return *this;
133   }
134
135   /// @returns the corresponding word for the specified bit position.
136   /// @brief Get the word corresponding to a bit position
137   inline uint64_t getWord(uint32_t bitPosition) const { 
138     return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; 
139   }
140
141   /// This is used by the constructors that take string arguments.
142   /// @brief Converts a char array into an APInt
143   void fromString(uint32_t numBits, const char *StrStart, uint32_t slen, 
144                   uint8_t radix);
145
146   /// This is used by the toString method to divide by the radix. It simply
147   /// provides a more convenient form of divide for internal use since KnuthDiv
148   /// has specific constraints on its inputs. If those constraints are not met
149   /// then it provides a simpler form of divide.
150   /// @brief An internal division function for dividing APInts.
151   static void divide(const APInt LHS, uint32_t lhsWords, 
152                      const APInt &RHS, uint32_t rhsWords,
153                      APInt *Quotient, APInt *Remainder);
154
155 #ifndef NDEBUG
156   /// @brief debug method
157   void dump() const;
158 #endif
159
160 public:
161   /// @brief Create a new APInt of numBits width, initialized as val.
162   APInt(uint32_t numBits, uint64_t val);
163
164   /// Note that numWords can be smaller or larger than the corresponding bit
165   /// width but any extraneous bits will be dropped.
166   /// @brief Create a new APInt of numBits width, initialized as bigVal[].
167   APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[]);
168
169   /// @brief Create a new APInt by translating the string represented 
170   /// integer value.
171   APInt(uint32_t numBits, const std::string& Val, uint8_t radix);
172
173   /// @brief Create a new APInt by translating the char array represented
174   /// integer value.
175   APInt(uint32_t numBits, const char StrStart[], uint32_t slen, uint8_t radix);
176
177   /// @brief Copy Constructor.
178   APInt(const APInt& API);
179
180   /// @brief Destructor.
181   ~APInt();
182
183   /// @brief Copy assignment operator. 
184   APInt& operator=(const APInt& RHS);
185
186   /// Assigns an integer value to the APInt.
187   /// @brief Assignment operator. 
188   APInt& operator=(uint64_t RHS);
189
190   /// Increments the APInt by one.
191   /// @brief Postfix increment operator.
192   inline const APInt operator++(int) {
193     APInt API(*this);
194     ++(*this);
195     return API;
196   }
197
198   /// Increments the APInt by one.
199   /// @brief Prefix increment operator.
200   APInt& operator++();
201
202   /// Decrements the APInt by one.
203   /// @brief Postfix decrement operator. 
204   inline const APInt operator--(int) {
205     APInt API(*this);
206     --(*this);
207     return API;
208   }
209
210   /// Decrements the APInt by one.
211   /// @brief Prefix decrement operator. 
212   APInt& operator--();
213
214   /// Performs bitwise AND operation on this APInt and the given APInt& RHS, 
215   /// assigns the result to this APInt.
216   /// @brief Bitwise AND assignment operator. 
217   APInt& operator&=(const APInt& RHS);
218
219   /// Performs bitwise OR operation on this APInt and the given APInt& RHS, 
220   /// assigns the result to this APInt.
221   /// @brief Bitwise OR assignment operator. 
222   APInt& operator|=(const APInt& RHS);
223
224   /// Performs bitwise XOR operation on this APInt and the given APInt& RHS, 
225   /// assigns the result to this APInt.
226   /// @brief Bitwise XOR assignment operator. 
227   APInt& operator^=(const APInt& RHS);
228
229   /// Performs a bitwise complement operation on this APInt.
230   /// @brief Bitwise complement operator. 
231   APInt operator~() const;
232
233   /// Multiplies this APInt by the  given APInt& RHS and 
234   /// assigns the result to this APInt.
235   /// @brief Multiplication assignment operator. 
236   APInt& operator*=(const APInt& RHS);
237
238   /// Adds this APInt by the given APInt& RHS and 
239   /// assigns the result to this APInt.
240   /// @brief Addition assignment operator. 
241   APInt& operator+=(const APInt& RHS);
242
243   /// Subtracts this APInt by the given APInt &RHS and 
244   /// assigns the result to this APInt.
245   /// @brief Subtraction assignment operator. 
246   APInt& operator-=(const APInt& RHS);
247
248   /// Performs bitwise AND operation on this APInt and 
249   /// the given APInt& RHS.
250   /// @brief Bitwise AND operator. 
251   APInt operator&(const APInt& RHS) const;
252
253   /// Performs bitwise OR operation on this APInt and the given APInt& RHS.
254   /// @brief Bitwise OR operator. 
255   APInt operator|(const APInt& RHS) const;
256
257   /// Performs bitwise XOR operation on this APInt and the given APInt& RHS.
258   /// @brief Bitwise XOR operator. 
259   APInt operator^(const APInt& RHS) const;
260
261   /// Performs logical negation operation on this APInt.
262   /// @brief Logical negation operator. 
263   bool operator !() const;
264
265   /// Multiplies this APInt by the given APInt& RHS.
266   /// @brief Multiplication operator. 
267   APInt operator*(const APInt& RHS) const;
268
269   /// Adds this APInt by the given APInt& RHS.
270   /// @brief Addition operator. 
271   APInt operator+(const APInt& RHS) const;
272
273   /// Subtracts this APInt by the given APInt& RHS
274   /// @brief Subtraction operator. 
275   APInt operator-(const APInt& RHS) const;
276
277   /// @brief Unary negation operator
278   inline APInt operator-() const {
279     return APInt(BitWidth, 0) - (*this);
280   }
281
282   /// @brief Array-indexing support.
283   bool operator[](uint32_t bitPosition) const;
284
285   /// Compare this APInt with the given APInt& RHS 
286   /// for the validity of the equality relationship.
287   /// @brief Equality operator. 
288   bool operator==(const APInt& RHS) const;
289
290   /// Compare this APInt with the given uint64_t value
291   /// for the validity of the equality relationship.
292   /// @brief Equality operator.
293   bool operator==(uint64_t Val) const;
294
295   /// Compare this APInt with the given APInt& RHS 
296   /// for the validity of the inequality relationship.
297   /// @brief Inequality operator. 
298   inline bool operator!=(const APInt& RHS) const {
299     return !((*this) == RHS);
300   }
301
302   /// Compare this APInt with the given uint64_t value 
303   /// for the validity of the inequality relationship.
304   /// @brief Inequality operator. 
305   inline bool operator!=(uint64_t Val) const {
306     return !((*this) == Val);
307   }
308   
309   /// @brief Equality comparison
310   bool eq(const APInt &RHS) const {
311     return (*this) == RHS; 
312   }
313
314   /// @brief Inequality comparison
315   bool ne(const APInt &RHS) const {
316     return !((*this) == RHS);
317   }
318
319   /// @brief Unsigned less than comparison
320   bool ult(const APInt& RHS) const;
321
322   /// @brief Signed less than comparison
323   bool slt(const APInt& RHS) const;
324
325   /// @brief Unsigned less or equal comparison
326   bool ule(const APInt& RHS) const {
327     return ult(RHS) || eq(RHS);
328   }
329
330   /// @brief Signed less or equal comparison
331   bool sle(const APInt& RHS) const {
332     return slt(RHS) || eq(RHS);
333   }
334
335   /// @brief Unsigned greather than comparison
336   bool ugt(const APInt& RHS) const {
337     return !ult(RHS) && !eq(RHS);
338   }
339
340   /// @brief Signed greather than comparison
341   bool sgt(const APInt& RHS) const {
342     return !slt(RHS) && !eq(RHS);
343   }
344
345   /// @brief Unsigned greater or equal comparison
346   bool uge(const APInt& RHS) const {
347     return !ult(RHS);
348   }
349
350   /// @brief Signed greather or equal comparison
351   bool sge(const APInt& RHS) const {
352     return !slt(RHS);
353   }
354
355   /// This just tests the high bit of this APInt to determine if it is negative.
356   /// @returns true if this APInt is negative, false otherwise
357   /// @brief Determine sign of this APInt.
358   bool isNegative() const {
359     return (*this)[BitWidth - 1];
360   }
361
362   /// Arithmetic right-shift this APInt by shiftAmt.
363   /// @brief Arithmetic right-shift function.
364   APInt ashr(uint32_t shiftAmt) const;
365
366   /// Logical right-shift this APInt by shiftAmt.
367   /// @brief Logical right-shift function.
368   APInt lshr(uint32_t shiftAmt) const;
369
370   /// Left-shift this APInt by shiftAmt.
371   /// @brief Left-shift function.
372   APInt shl(uint32_t shiftAmt) const;
373
374   /// Signed divide this APInt by APInt RHS.
375   /// @brief Signed division function for APInt.
376   inline APInt sdiv(const APInt& RHS) const {
377     bool isNegativeLHS = (*this)[BitWidth - 1];
378     bool isNegativeRHS = RHS[RHS.BitWidth - 1];
379     APInt Result = APIntOps::udiv(
380         isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
381     return isNegativeLHS != isNegativeRHS ? -Result : Result;
382   }
383
384   /// Unsigned divide this APInt by APInt RHS.
385   /// @brief Unsigned division function for APInt.
386   APInt udiv(const APInt& RHS) const;
387
388   /// Signed remainder operation on APInt.
389   /// @brief Function for signed remainder operation.
390   inline APInt srem(const APInt& RHS) const {
391     bool isNegativeLHS = (*this)[BitWidth - 1];
392     bool isNegativeRHS = RHS[RHS.BitWidth - 1];
393     APInt Result = APIntOps::urem(
394         isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
395     return isNegativeLHS ? -Result : Result;
396   }
397
398   /// Unsigned remainder operation on APInt.
399   /// @brief Function for unsigned remainder operation.
400   APInt urem(const APInt& RHS) const;
401
402   /// Truncate the APInt to a specified width. It is an error to specify a width
403   /// that is greater than or equal to the current width. 
404   /// @brief Truncate to new width.
405   void trunc(uint32_t width);
406
407   /// This operation sign extends the APInt to a new width. If the high order
408   /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
409   /// It is an error to specify a width that is less than or equal to the 
410   /// current width.
411   /// @brief Sign extend to a new width.
412   void sext(uint32_t width);
413
414   /// This operation zero extends the APInt to a new width. Thie high order bits
415   /// are filled with 0 bits.  It is an error to specify a width that is less 
416   /// than or equal to the current width.
417   /// @brief Zero extend to a new width.
418   void zext(uint32_t width);
419
420   /// @brief Set every bit to 1.
421   APInt& set();
422
423   /// Set the given bit to 1 whose position is given as "bitPosition".
424   /// @brief Set a given bit to 1.
425   APInt& set(uint32_t bitPosition);
426
427   /// @brief Set every bit to 0.
428   APInt& clear();
429
430   /// Set the given bit to 0 whose position is given as "bitPosition".
431   /// @brief Set a given bit to 0.
432   APInt& clear(uint32_t bitPosition);
433
434   /// @brief Toggle every bit to its opposite value.
435   APInt& flip();
436
437   /// Toggle a given bit to its opposite value whose position is given 
438   /// as "bitPosition".
439   /// @brief Toggles a given bit to its opposite value.
440   APInt& flip(uint32_t bitPosition);
441
442   /// This function returns the number of active bits which is defined as the
443   /// bit width minus the number of leading zeros. This is used in several
444   /// computations to see how "wide" the value is.
445   /// @brief Compute the number of active bits in the value
446   inline uint32_t getActiveBits() const {
447     return BitWidth - countLeadingZeros();
448   }
449
450   /// This method attempts to return the value of this APInt as a zero extended
451   /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
452   /// uint64_t. Otherwise an assertion will result.
453   /// @brief Get zero extended value
454   inline uint64_t getZExtValue() const {
455     if (isSingleWord())
456       return VAL;
457     assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
458     return pVal[0];
459   }
460
461   /// This method attempts to return the value of this APInt as a sign extended
462   /// int64_t. The bit width must be <= 64 or the value must fit within an
463   /// int64_t. Otherwise an assertion will result.
464   /// @brief Get sign extended value
465   inline int64_t getSExtValue() const {
466     if (isSingleWord())
467       return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >> 
468                      (APINT_BITS_PER_WORD - BitWidth);
469     assert(getActiveBits() <= 64 && "Too many bits for int64_t");
470       return int64_t(pVal[0]);
471   }
472
473   /// @returns the largest value for an APInt of the specified bit-width and 
474   /// if isSign == true, it should be largest signed value, otherwise largest
475   /// unsigned value.
476   /// @brief Gets max value of the APInt with bitwidth <= 64.
477   static APInt getMaxValue(uint32_t numBits, bool isSigned);
478
479   /// @returns the smallest value for an APInt of the given bit-width and
480   /// if isSign == true, it should be smallest signed value, otherwise zero.
481   /// @brief Gets min value of the APInt with bitwidth <= 64.
482   static APInt getMinValue(uint32_t numBits, bool isSigned);
483
484   /// @returns the all-ones value for an APInt of the specified bit-width.
485   /// @brief Get the all-ones value.
486   static APInt getAllOnesValue(uint32_t numBits);
487
488   /// @returns the '0' value for an APInt of the specified bit-width.
489   /// @brief Get the '0' value.
490   static APInt getNullValue(uint32_t numBits);
491
492   /// The hash value is computed as the sum of the words and the bit width.
493   /// @returns A hash value computed from the sum of the APInt words.
494   /// @brief Get a hash value based on this APInt
495   uint64_t getHashValue() const;
496
497   /// This converts the APInt to a boolean valy as a test against zero.
498   /// @brief Boolean conversion function. 
499   inline bool getBoolValue() const {
500     return countLeadingZeros() != BitWidth;
501   }
502
503   /// This checks to see if the value has all bits of the APInt are set or not.
504   /// @brief Determine if all bits are set
505   inline bool isAllOnesValue() const {
506     return countPopulation() == BitWidth;
507   }
508
509   /// This checks to see if the value of this APInt is the maximum unsigned
510   /// value for the APInt's bit width.
511   /// @brief Determine if this is the largest unsigned value.
512   bool isMaxValue() const {
513     return countPopulation() == BitWidth;
514   }
515
516   /// This checks to see if the value of this APInt is the maximum signed
517   /// value for the APInt's bit width.
518   /// @brief Determine if this is the largest signed value.
519   bool isMaxSignedValue() const {
520     return BitWidth == 1 ? VAL == 0 :
521                           !isNegative() && countPopulation() == BitWidth - 1;
522   }
523
524   /// This checks to see if the value of this APInt is the minimum signed
525   /// value for the APInt's bit width.
526   /// @brief Determine if this is the smallest unsigned value.
527   bool isMinValue() const {
528     return countPopulation() == 0;
529   }
530
531   /// This checks to see if the value of this APInt is the minimum signed
532   /// value for the APInt's bit width.
533   /// @brief Determine if this is the smallest signed value.
534   bool isMinSignedValue() const {
535     return BitWidth == 1 ? VAL == 1 :
536                            isNegative() && countPopulation() == 1;
537   }
538
539   /// @returns a character interpretation of the APInt.
540   std::string toString(uint8_t radix = 10, bool wantSigned = true) const;
541
542   /// Get an APInt with the same BitWidth as this APInt, just zero mask
543   /// the low bits and right shift to the least significant bit.
544   /// @returns the high "numBits" bits of this APInt.
545   APInt getHiBits(uint32_t numBits) const;
546
547   /// Get an APInt with the same BitWidth as this APInt, just zero mask
548   /// the high bits.
549   /// @returns the low "numBits" bits of this APInt.
550   APInt getLoBits(uint32_t numBits) const;
551
552   /// @returns true if the argument APInt value is a power of two > 0.
553   bool isPowerOf2() const; 
554
555   /// countLeadingZeros - This function is an APInt version of the
556   /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
557   /// of zeros from the most significant bit to the first one bit.
558   /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
559   /// @returns the number of zeros from the most significant bit to the first
560   /// one bits.
561   /// @brief Count the number of trailing one bits.
562   uint32_t countLeadingZeros() const;
563
564   /// countTrailingZeros - This function is an APInt version of the 
565   /// countTrailingZoers_{32,64} functions in MathExtras.h. It counts 
566   /// the number of zeros from the least significant bit to the first one bit.
567   /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
568   /// @returns the number of zeros from the least significant bit to the first
569   /// one bit.
570   /// @brief Count the number of trailing zero bits.
571   uint32_t countTrailingZeros() const;
572
573   /// countPopulation - This function is an APInt version of the
574   /// countPopulation_{32,64} functions in MathExtras.h. It counts the number
575   /// of 1 bits in the APInt value. 
576   /// @returns 0 if the value is zero.
577   /// @returns the number of set bits.
578   /// @brief Count the number of bits set.
579   uint32_t countPopulation() const; 
580
581   /// @returns the total number of bits.
582   inline uint32_t getBitWidth() const { 
583     return BitWidth; 
584   }
585
586   /// @brief Check if this APInt has a N-bits integer value.
587   inline bool isIntN(uint32_t N) const {
588     assert(N && "N == 0 ???");
589     if (isSingleWord()) {
590       return VAL == (VAL & (~0ULL >> (64 - N)));
591     } else {
592       APInt Tmp(N, getNumWords(), pVal);
593       return Tmp == (*this);
594     }
595   }
596
597   /// @returns a byte-swapped representation of this APInt Value.
598   APInt byteSwap() const;
599
600   /// @returns the floor log base 2 of this APInt.
601   inline uint32_t logBase2() const {
602     return getNumWords() * APINT_BITS_PER_WORD - 1 - countLeadingZeros();
603   }
604
605   /// @brief Converts this APInt to a double value.
606   double roundToDouble(bool isSigned = false) const;
607
608 };
609
610 namespace APIntOps {
611
612 /// @brief Check if the specified APInt has a N-bits integer value.
613 inline bool isIntN(uint32_t N, const APInt& APIVal) {
614   return APIVal.isIntN(N);
615 }
616
617 /// @returns true if the argument APInt value is a sequence of ones
618 /// starting at the least significant bit with the remainder zero.
619 inline const bool isMask(uint32_t numBits, const APInt& APIVal) {
620   return APIVal.getBoolValue() && ((APIVal + APInt(numBits,1)) & APIVal) == 0;
621 }
622
623 /// @returns true if the argument APInt value contains a sequence of ones
624 /// with the remainder zero.
625 inline const bool isShiftedMask(uint32_t numBits, const APInt& APIVal) {
626   return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
627 }
628
629 /// @returns a byte-swapped representation of the specified APInt Value.
630 inline APInt byteSwap(const APInt& APIVal) {
631   return APIVal.byteSwap();
632 }
633
634 /// @returns the floor log base 2 of the specified APInt value.
635 inline uint32_t logBase2(const APInt& APIVal) {
636   return APIVal.logBase2(); 
637 }
638
639 /// GreatestCommonDivisor - This function returns the greatest common
640 /// divisor of the two APInt values using Enclid's algorithm.
641 /// @returns the greatest common divisor of Val1 and Val2
642 /// @brief Compute GCD of two APInt values.
643 APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
644
645 /// @brief Converts the given APInt to a double value.
646 inline double RoundAPIntToDouble(const APInt& APIVal, bool isSigned = false) {
647   return APIVal.roundToDouble(isSigned);
648 }
649
650 /// @brief Converts the given APInt to a float vlalue.
651 inline float RoundAPIntToFloat(const APInt& APIVal) {
652   return float(RoundAPIntToDouble(APIVal));
653 }
654
655 /// RoundDoubleToAPInt - This function convert a double value to an APInt value.
656 /// @brief Converts the given double value into a APInt.
657 APInt RoundDoubleToAPInt(double Double);
658
659 /// RoundFloatToAPInt - Converts a float value into an APInt value.
660 /// @brief Converts a float value into a APInt.
661 inline APInt RoundFloatToAPInt(float Float) {
662   return RoundDoubleToAPInt(double(Float));
663 }
664
665 /// Arithmetic right-shift the APInt by shiftAmt.
666 /// @brief Arithmetic right-shift function.
667 inline APInt ashr(const APInt& LHS, uint32_t shiftAmt) {
668   return LHS.ashr(shiftAmt);
669 }
670
671 /// Logical right-shift the APInt by shiftAmt.
672 /// @brief Logical right-shift function.
673 inline APInt lshr(const APInt& LHS, uint32_t shiftAmt) {
674   return LHS.lshr(shiftAmt);
675 }
676
677 /// Left-shift the APInt by shiftAmt.
678 /// @brief Left-shift function.
679 inline APInt shl(const APInt& LHS, uint32_t shiftAmt) {
680   return LHS.shl(shiftAmt);
681 }
682
683 /// Signed divide APInt LHS by APInt RHS.
684 /// @brief Signed division function for APInt.
685 inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
686   return LHS.sdiv(RHS);
687 }
688
689 /// Unsigned divide APInt LHS by APInt RHS.
690 /// @brief Unsigned division function for APInt.
691 inline APInt udiv(const APInt& LHS, const APInt& RHS) {
692   return LHS.udiv(RHS);
693 }
694
695 /// Signed remainder operation on APInt.
696 /// @brief Function for signed remainder operation.
697 inline APInt srem(const APInt& LHS, const APInt& RHS) {
698   return LHS.srem(RHS);
699 }
700
701 /// Unsigned remainder operation on APInt.
702 /// @brief Function for unsigned remainder operation.
703 inline APInt urem(const APInt& LHS, const APInt& RHS) {
704   return LHS.urem(RHS);
705 }
706
707 /// Performs multiplication on APInt values.
708 /// @brief Function for multiplication operation.
709 inline APInt mul(const APInt& LHS, const APInt& RHS) {
710   return LHS * RHS;
711 }
712
713 /// Performs addition on APInt values.
714 /// @brief Function for addition operation.
715 inline APInt add(const APInt& LHS, const APInt& RHS) {
716   return LHS + RHS;
717 }
718
719 /// Performs subtraction on APInt values.
720 /// @brief Function for subtraction operation.
721 inline APInt sub(const APInt& LHS, const APInt& RHS) {
722   return LHS - RHS;
723 }
724
725 /// Performs bitwise AND operation on APInt LHS and 
726 /// APInt RHS.
727 /// @brief Bitwise AND function for APInt.
728 inline APInt And(const APInt& LHS, const APInt& RHS) {
729   return LHS & RHS;
730 }
731
732 /// Performs bitwise OR operation on APInt LHS and APInt RHS.
733 /// @brief Bitwise OR function for APInt. 
734 inline APInt Or(const APInt& LHS, const APInt& RHS) {
735   return LHS | RHS;
736 }
737
738 /// Performs bitwise XOR operation on APInt.
739 /// @brief Bitwise XOR function for APInt.
740 inline APInt Xor(const APInt& LHS, const APInt& RHS) {
741   return LHS ^ RHS;
742
743
744 /// Performs a bitwise complement operation on APInt.
745 /// @brief Bitwise complement function. 
746 inline APInt Not(const APInt& APIVal) {
747   return ~APIVal;
748 }
749
750 } // End of APIntOps namespace
751
752 } // End of llvm namespace
753
754 #endif