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