Fix build error.
[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 //===----------------------------------------------------------------------===//
25 //                              APInt Class
26 //===----------------------------------------------------------------------===//
27
28 /// APInt - This class represents arbitrary precision constant integral values.
29 /// It is a functional replacement for common case unsigned integer type like 
30 /// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width 
31 /// integer type and large integer value types such as 3-bits, 15-bits, or more
32 /// than 64-bits of precision. APInt provides a variety of arithmetic operators 
33 /// and methods to manipulate integer values of any bit-width. It supports not 
34 /// only all the operations of uint64_t but also bitwise manipulation.
35 ///
36 /// @brief Class for arbitrary precision integers.
37 ///
38 /// Note: In this class, all bit/byte/word positions are zero-based.
39 ///
40 class APInt {
41   /// Friend Functions of APInt declared here. For detailed comments,
42   /// see bottom of this file.
43   friend bool isIntN(unsigned N, const APInt& APIVal);
44   friend APInt ByteSwap(const APInt& APIVal);
45   friend APInt LogBase2(const APInt& APIVal);
46   friend double APIntToDouble(const APInt& APIVal);
47   friend float APIntToFloat(const APInt& APIVal);
48
49   unsigned BitsNum;      ///< The number of bits.
50   bool isSigned;         ///< The sign flag for this APInt.
51
52   /// This union is used to store the integer value. When the
53   /// integer bit-width <= 64, it uses VAL; 
54   /// otherwise it uses the pVal.
55   union {
56     uint64_t VAL;    ///< Used to store the <= 64 bits integer value.
57     uint64_t *pVal;  ///< Used to store the >64 bits integer value.
58   };
59
60   /// This enum is just used to hold a constant we needed for APInt.
61   enum {
62     APINT_BITS_PER_WORD = sizeof(uint64_t) * 8
63   };
64
65   /// Here one word's bitwidth equals to that of uint64_t.
66   /// @returns the number of words to hold the integer value of this APInt.
67   /// @brief Get the number of words.
68   inline unsigned getNumWords() const {
69     return (BitsNum + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
70   }
71
72   /// @returns true if the number of bits <= 64, false otherwise.
73   /// @brief Determine if this APInt just has one word to store value.
74   inline bool isSingleWord() const
75   { return BitsNum <= APINT_BITS_PER_WORD; }
76
77   /// @returns the word position for the specified bit position.
78   static inline unsigned whichWord(unsigned bitPosition)
79   { return bitPosition / APINT_BITS_PER_WORD; }
80
81   /// @returns the byte position for the specified bit position.
82   static inline unsigned whichByte(unsigned bitPosition)
83   { return (bitPosition % APINT_BITS_PER_WORD) / 8; }
84
85   /// @returns the bit position in a word for the specified bit position 
86   /// in APInt.
87   static inline unsigned whichBit(unsigned bitPosition)
88   { return bitPosition % APINT_BITS_PER_WORD; }
89
90   /// @returns a uint64_t type integer with just bit position at
91   /// "whichBit(bitPosition)" setting, others zero.
92   static inline uint64_t maskBit(unsigned bitPosition)
93   { return (static_cast<uint64_t>(1)) << whichBit(bitPosition); }
94
95   inline void TruncToBits() {
96     if (isSingleWord())
97       VAL &= ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitsNum);
98     else
99       pVal[getNumWords() - 1] &= ~uint64_t(0ULL) >> 
100         (APINT_BITS_PER_WORD - (whichBit(BitsNum - 1) + 1));
101   }
102
103   /// @returns the corresponding word for the specified bit position.
104   inline uint64_t& getWord(unsigned bitPosition)
105   { return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; }
106
107   /// @returns the corresponding word for the specified bit position.
108   /// This is a constant version.
109   inline uint64_t getWord(unsigned bitPosition) const
110   { return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; }
111
112   /// @brief Converts a char array into an integer.
113   void StrToAPInt(const char *StrStart, unsigned slen, uint8_t radix);
114
115 public:
116   /// @brief Create a new APInt of numBits bit-width, and initialized as val.
117   APInt(uint64_t val = 0, unsigned numBits = APINT_BITS_PER_WORD, 
118         bool sign = false);
119
120   /// @brief Create a new APInt of numBits bit-width, and initialized as 
121   /// bigVal[].
122   APInt(unsigned numBits, uint64_t bigVal[], bool sign = false);
123
124   /// @brief Create a new APInt by translating the string represented 
125   /// integer value.
126   APInt(const std::string& Val, uint8_t radix = 10, bool sign = false);
127
128   /// @brief Create a new APInt by translating the char array represented
129   /// integer value.
130   APInt(const char StrStart[], unsigned slen, uint8_t radix, bool sign = false);
131
132   /// @brief Copy Constructor.
133   APInt(const APInt& API);
134
135   /// @brief Destructor.
136   ~APInt();
137
138   /// @brief Copy assignment operator. 
139   APInt& operator=(const APInt& RHS);
140
141   /// Assigns an integer value to the APInt.
142   /// @brief Assignment operator. 
143   APInt& operator=(uint64_t RHS);
144
145   /// Increments the APInt by one.
146   /// @brief Postfix increment operator.
147   inline const APInt operator++(int) {
148     APInt API(*this);
149     return ++API;
150   }
151
152   /// Increments the APInt by one.
153   /// @brief Prefix increment operator.
154   APInt& operator++();
155
156   /// Decrements the APInt by one.
157   /// @brief Postfix decrement operator. 
158   inline const APInt operator--(int) {
159     APInt API(*this);
160     return --API;
161   }
162
163   /// Decrements the APInt by one.
164   /// @brief Prefix decrement operator. 
165   APInt& operator--();
166
167   /// Performs bitwise AND operation on this APInt and the given APInt& RHS, 
168   /// assigns the result to this APInt.
169   /// @brief Bitwise AND assignment operator. 
170   APInt& operator&=(const APInt& RHS);
171
172   /// Performs bitwise OR operation on this APInt and the given APInt& RHS, 
173   /// assigns the result to this APInt.
174   /// @brief Bitwise OR assignment operator. 
175   APInt& operator|=(const APInt& RHS);
176
177   /// Performs bitwise XOR operation on this APInt and the given APInt& RHS, 
178   /// assigns the result to this APInt.
179   /// @brief Bitwise XOR assignment operator. 
180   APInt& operator^=(const APInt& RHS);
181
182   /// Left-shift the APInt by shiftAmt and assigns the result to this APInt.
183   /// @brief Left-shift assignment operator. 
184   APInt& operator<<=(unsigned shiftAmt);
185
186   /// Right-shift the APInt by shiftAmt and assigns the result to this APInt.
187   /// @brief Right-shift assignment operator. 
188   APInt& operator>>=(unsigned shiftAmt);
189
190   /// Performs a bitwise complement operation on this APInt.
191   /// @brief Bitwise complement operator. 
192   APInt operator~() const;
193
194   /// Multiplies this APInt by the  given APInt& RHS and 
195   /// assigns the result to this APInt.
196   /// @brief Multiplication assignment operator. 
197   APInt& operator*=(const APInt& RHS);
198
199   /// Divides this APInt by the given APInt &RHS and 
200   /// assigns the result to this APInt.
201   /// @brief Division assignment operator. 
202   APInt& operator/=(const APInt& RHS);
203
204   /// Adds this APInt by the given APInt& RHS and 
205   /// assigns the result to this APInt.
206   /// @brief Addition assignment operator. 
207   APInt& operator+=(const APInt& RHS);
208
209   /// Subtracts this APInt by the given APInt &RHS and 
210   /// assigns the result to this APInt.
211   /// @brief Subtraction assignment operator. 
212   APInt& operator-=(const APInt& RHS);
213
214   /// Yields the remainder from the division of this APInt by 
215   /// the given APInt& RHS and assigns the remainder to this APInt.
216   /// @brief Remainder assignment operator. 
217   APInt& operator%=(const APInt& RHS);
218
219   /// Performs bitwise AND operation on this APInt and 
220   /// the given APInt& RHS.
221   /// @brief Bitwise AND operator. 
222   APInt operator&(const APInt& RHS) const;
223
224   /// Performs bitwise OR operation on this APInt and the given APInt& RHS.
225   /// @brief Bitwise OR operator. 
226   APInt operator|(const APInt& RHS) const;
227
228   /// Performs bitwise XOR operation on this APInt and the given APInt& RHS.
229   /// @brief Bitwise XOR operator. 
230   APInt operator^(const APInt& RHS) const;
231
232   /// Performs logical AND operation on this APInt and the given APInt& RHS.
233   /// @brief Logical AND operator. 
234   bool operator&&(const APInt& RHS) const;
235
236   /// Performs logical OR operation on this APInt and the given APInt& RHS.
237   /// @brief Logical OR operator. 
238   bool operator||(const APInt& RHS) const;
239
240   /// Performs logical negation operation on this APInt.
241   /// @brief Logical negation operator. 
242   bool operator !() const;
243
244   /// Multiplies this APInt by the given APInt& RHS.
245   /// @brief Multiplication operator. 
246   APInt operator*(const APInt& RHS) const;
247
248   /// Divides this APInt by the given APInt& RHS.
249   /// @brief Division operator. 
250   APInt operator/(const APInt& RHS) const;
251
252   /// Yields the remainder from the division of 
253   /// this APInt and the given APInt& RHS.
254   /// @brief Remainder operator. 
255   APInt operator%(const APInt& RHS) const;
256
257   /// Adds this APInt by the given APInt& RHS.
258   /// @brief Addition operator. 
259   APInt operator+(const APInt& RHS) const;
260
261   /// Subtracts this APInt by the given APInt& RHS
262   /// @brief Subtraction operator. 
263   APInt operator-(const APInt& RHS) const;
264
265   /// Left-shift the APInt by shiftAmt.
266   /// @brief Left-shift operator. 
267   APInt operator<<(unsigned shiftAmt) const;
268
269   /// Right-shift the APInt by shiftAmt.
270   /// @brief Right-shift operator. 
271   APInt operator>>(unsigned shiftAmt) const;
272
273   /// @brief Array-indexing support.
274   bool operator[](unsigned bitPosition) const;
275
276   /// Compare this APInt with the given APInt& RHS 
277   /// for the validity of the equality relationship.
278   /// @brief Equality operator. 
279   bool operator==(const APInt& RHS) const;
280
281   /// Compare this APInt with the given uint64_t value
282   /// for the validity of the equality relationship.
283   /// @brief Equality operator.
284   bool operator==(uint64_t Val) const;
285
286   /// Compare this APInt with the given APInt& RHS 
287   /// for the validity of the inequality relationship.
288   /// @brief Inequality operator. 
289   inline bool operator!=(const APInt& RHS) const {
290     return !((*this) == RHS);
291   }
292
293   /// Compare this APInt with the given uint64_t value 
294   /// for the validity of the inequality relationship.
295   /// @brief Inequality operator. 
296   inline bool operator!=(uint64_t Val) const {
297     return !((*this) == Val);
298   }
299   
300   /// Compare this APInt with the given APInt& RHS for 
301   /// the validity of the less-than relationship.
302   /// @brief Less-than operator. 
303   bool operator <(const APInt& RHS) const;
304
305   /// Compare this APInt with the given APInt& RHS for the validity 
306   /// of the less-than-or-equal relationship.
307   /// @brief Less-than-or-equal operator. 
308   bool operator<=(const APInt& RHS) const;
309
310   /// Compare this APInt with the given APInt& RHS for the validity 
311   /// of the greater-than relationship.
312   /// @brief Greater-than operator. 
313   bool operator> (const APInt& RHS) const;
314
315   /// @brief Greater-than-or-equal operator. 
316   /// Compare this APInt with the given APInt& RHS for the validity 
317   /// of the greater-than-or-equal relationship.
318   bool operator>=(const APInt& RHS) const;
319
320   /// @returns a uint64_t value from this APInt. If this APInt contains a single
321   /// word, just returns VAL, otherwise pVal[0].
322   inline uint64_t getValue() {
323     if (isSingleWord())
324       return isSigned ? ((int64_t(VAL) << (APINT_BITS_PER_WORD - BitsNum)) >> 
325                          (APINT_BITS_PER_WORD - BitsNum)) :
326                         VAL;
327     assert(0 && "This APInt's bitwidth > 64");
328   }
329
330   /// @returns the largest value for an APInt of the specified bit-width and 
331   /// if isSign == true, it should be largest signed value, otherwise largest
332   /// unsigned value.
333   /// @brief Gets max value of the APInt with bitwidth <= 64.
334   static APInt getMaxValue(unsigned numBits, bool isSign);
335
336   /// @returns the smallest value for an APInt of the given bit-width and
337   /// if isSign == true, it should be smallest signed value, otherwise zero.
338   /// @brief Gets min value of the APInt with bitwidth <= 64.
339   static APInt getMinValue(unsigned numBits, bool isSign);
340
341   /// @returns the all-ones value for an APInt of the specified bit-width.
342   /// @brief Get the all-ones value.
343   static APInt getAllOnesValue(unsigned numBits);
344
345   /// @brief Set every bit to 1.
346   APInt& set();
347
348   /// Set the given bit to 1 whose position is given as "bitPosition".
349   /// @brief Set a given bit to 1.
350   APInt& set(unsigned bitPosition);
351
352   /// @returns the '0' value for an APInt of the specified bit-width.
353   /// @brief Get the '0' value.
354   static APInt getNullValue(unsigned numBits);
355
356   /// @brief Set every bit to 0.
357   APInt& clear();
358
359   /// Set the given bit to 0 whose position is given as "bitPosition".
360   /// @brief Set a given bit to 0.
361   APInt& clear(unsigned bitPosition);
362
363   /// @brief Toggle every bit to its opposite value.
364   APInt& flip();
365
366   /// Toggle a given bit to its opposite value whose position is given 
367   /// as "bitPosition".
368   /// @brief Toggles a given bit to its opposite value.
369   APInt& flip(unsigned bitPosition);
370
371   /// @returns a character interpretation of the APInt.
372   std::string to_string(uint8_t radix = 10) const;
373
374   /// Get an APInt with the same BitsNum as this APInt, just zero mask
375   /// the low bits and right shift to the least significant bit.
376   /// @returns the high "numBits" bits of this APInt.
377   APInt HiBits(unsigned numBits) const;
378
379   /// Get an APInt with the same BitsNum as this APInt, just zero mask
380   /// the high bits.
381   /// @returns the low "numBits" bits of this APInt.
382   APInt LoBits(unsigned numBits) const;
383
384   /// @returns true if the argument APInt value is a power of two > 0.
385   inline const bool isPowerOf2() const {
386     return (!!*this) && !(*this & (*this - 1));
387   }
388
389   /// @returns the number of zeros from the most significant bit to the first
390   /// one bits.
391   unsigned CountLeadingZeros() const;
392
393   /// @returns the number of zeros from the least significant bit to the first
394   /// one bit.
395   unsigned CountTrailingZeros() const;
396
397   /// @returns the number of set bits.
398   unsigned CountPopulation() const; 
399
400   /// @returns the total number of bits.
401   inline unsigned getNumBits() const
402   { return BitsNum; }
403
404 };
405
406 /// @brief Check if the specified APInt has a N-bits integer value.
407 inline bool isIntN(unsigned N, const APInt& APIVal) {
408   if (APIVal.isSingleWord()) {
409     APInt Tmp(N, APIVal.VAL);
410     return Tmp == APIVal;
411   } else {
412     APInt Tmp(N, APIVal.pVal);
413     return Tmp == APIVal;
414   }
415 }
416
417 /// @returns true if the argument APInt value is a sequence of ones
418 /// starting at the least significant bit with the remainder zero.
419 inline const bool isMask(unsigned numBits, const APInt& APIVal) {
420   return APIVal && ((APIVal + 1) & APIVal) == 0;
421 }
422
423 /// @returns true if the argument APInt value contains a sequence of ones
424 /// with the remainder zero.
425 inline const bool isShiftedMask(unsigned numBits, const APInt& APIVal) {
426   return isMask(numBits, (APIVal - 1) | APIVal);
427 }
428
429 /// @returns a byte-swapped representation of the specified APInt Value.
430 APInt ByteSwap(const APInt& APIVal);
431
432 /// @returns the floor log base 2 of the specified APInt value.
433 inline APInt LogBase2(const APInt& APIVal) {
434   return APIVal.getNumWords() * APInt::APINT_BITS_PER_WORD - 
435          APIVal.CountLeadingZeros();
436 }
437
438 /// @returns the greatest common divisor of the two values 
439 /// using Euclid's algorithm.
440 APInt GreatestCommonDivisor(const APInt& API1, const APInt& API2);
441
442 } // End of llvm namespace
443
444 #endif