Make some minor improvements to APInt:
[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 type 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 not 
41 /// only all the operations of uint64_t but also bitwise manipulation.
42 ///
43 /// @brief Class for arbitrary precision integers.
44 ///
45 /// Note: In this class, all bit/byte/word positions are zero-based.
46 ///
47 class APInt {
48   unsigned BitsNum;      ///< The number of bits.
49
50   /// This union is used to store the integer value. When the
51   /// integer bit-width <= 64, it uses VAL; 
52   /// otherwise it uses the pVal.
53   union {
54     uint64_t VAL;    ///< Used to store the <= 64 bits integer value.
55     uint64_t *pVal;  ///< Used to store the >64 bits integer value.
56   };
57
58   /// This enum is just used to hold a constant we needed for APInt.
59   enum {
60     APINT_BITS_PER_WORD = sizeof(uint64_t) * 8
61   };
62
63   /// Here one word's bitwidth equals to that of uint64_t.
64   /// @returns the number of words to hold the integer value of this APInt.
65   /// @brief Get the number of words.
66   inline unsigned getNumWords() const {
67     return (BitsNum + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
68   }
69
70   /// @returns true if the number of bits <= 64, false otherwise.
71   /// @brief Determine if this APInt just has one word to store value.
72   inline bool isSingleWord() const
73   { return BitsNum <= APINT_BITS_PER_WORD; }
74
75   /// @returns the word position for the specified bit position.
76   static inline unsigned whichWord(unsigned bitPosition)
77   { return bitPosition / APINT_BITS_PER_WORD; }
78
79   /// @returns the byte position for the specified bit position.
80   static inline unsigned whichByte(unsigned bitPosition)
81   { return (bitPosition % APINT_BITS_PER_WORD) / 8; }
82
83   /// @returns the bit position in a word for the specified bit position 
84   /// in APInt.
85   static inline unsigned whichBit(unsigned bitPosition)
86   { return bitPosition % APINT_BITS_PER_WORD; }
87
88   /// @returns a uint64_t type integer with just bit position at
89   /// "whichBit(bitPosition)" setting, others zero.
90   static inline uint64_t maskBit(unsigned bitPosition)
91   { return (static_cast<uint64_t>(1)) << whichBit(bitPosition); }
92
93   inline void TruncToBits() {
94     if (isSingleWord())
95       VAL &= ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitsNum);
96     else
97       pVal[getNumWords() - 1] &= ~uint64_t(0ULL) >> 
98         (APINT_BITS_PER_WORD - (whichBit(BitsNum - 1) + 1));
99   }
100
101   /// @returns the corresponding word for the specified bit position.
102   inline uint64_t& getWord(unsigned bitPosition)
103   { return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; }
104
105   /// @returns the corresponding word for the specified bit position.
106   /// This is a constant version.
107   inline uint64_t getWord(unsigned bitPosition) const
108   { return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; }
109
110   /// @brief Converts a char array into an integer.
111   void StrToAPInt(const char *StrStart, unsigned slen, uint8_t radix);
112
113 public:
114   /// @brief Create a new APInt of numBits bit-width, and initialized as val.
115   APInt(uint64_t val = 0, unsigned numBits = APINT_BITS_PER_WORD);
116
117   /// @brief Create a new APInt of numBits bit-width, and initialized as 
118   /// bigVal[].
119   APInt(unsigned numBits, uint64_t bigVal[]);
120
121   /// @brief Create a new APInt by translating the string represented 
122   /// integer value.
123   APInt(const std::string& Val, uint8_t radix = 10);
124
125   /// @brief Create a new APInt by translating the char array represented
126   /// integer value.
127   APInt(const char StrStart[], unsigned slen, uint8_t radix);
128
129   /// @brief Copy Constructor.
130   APInt(const APInt& API);
131
132   /// @brief Destructor.
133   ~APInt();
134
135   /// @brief Copy assignment operator. 
136   APInt& operator=(const APInt& RHS);
137
138   /// Assigns an integer value to the APInt.
139   /// @brief Assignment operator. 
140   APInt& operator=(uint64_t RHS);
141
142   /// Increments the APInt by one.
143   /// @brief Postfix increment operator.
144   inline const APInt operator++(int) {
145     APInt API(*this);
146     return ++API;
147   }
148
149   /// Increments the APInt by one.
150   /// @brief Prefix increment operator.
151   APInt& operator++();
152
153   /// Decrements the APInt by one.
154   /// @brief Postfix decrement operator. 
155   inline const APInt operator--(int) {
156     APInt API(*this);
157     return --API;
158   }
159
160   /// Decrements the APInt by one.
161   /// @brief Prefix decrement operator. 
162   APInt& operator--();
163
164   /// Performs bitwise AND operation on this APInt and the given APInt& RHS, 
165   /// assigns the result to this APInt.
166   /// @brief Bitwise AND assignment operator. 
167   APInt& operator&=(const APInt& RHS);
168
169   /// Performs bitwise OR operation on this APInt and the given APInt& RHS, 
170   /// assigns the result to this APInt.
171   /// @brief Bitwise OR assignment operator. 
172   APInt& operator|=(const APInt& RHS);
173
174   /// Performs bitwise XOR operation on this APInt and the given APInt& RHS, 
175   /// assigns the result to this APInt.
176   /// @brief Bitwise XOR assignment operator. 
177   APInt& operator^=(const APInt& RHS);
178
179   /// Performs a bitwise complement operation on this APInt.
180   /// @brief Bitwise complement operator. 
181   APInt operator~() const;
182
183   /// Multiplies this APInt by the  given APInt& RHS and 
184   /// assigns the result to this APInt.
185   /// @brief Multiplication assignment operator. 
186   APInt& operator*=(const APInt& RHS);
187
188   /// Adds this APInt by the given APInt& RHS and 
189   /// assigns the result to this APInt.
190   /// @brief Addition assignment operator. 
191   APInt& operator+=(const APInt& RHS);
192
193   /// Subtracts this APInt by the given APInt &RHS and 
194   /// assigns the result to this APInt.
195   /// @brief Subtraction assignment operator. 
196   APInt& operator-=(const APInt& RHS);
197
198   /// Performs bitwise AND operation on this APInt and 
199   /// the given APInt& RHS.
200   /// @brief Bitwise AND operator. 
201   APInt operator&(const APInt& RHS) const;
202
203   /// Performs bitwise OR operation on this APInt and the given APInt& RHS.
204   /// @brief Bitwise OR operator. 
205   APInt operator|(const APInt& RHS) const;
206
207   /// Performs bitwise XOR operation on this APInt and the given APInt& RHS.
208   /// @brief Bitwise XOR operator. 
209   APInt operator^(const APInt& RHS) const;
210
211   /// Performs logical AND operation on this APInt and the given APInt& RHS.
212   /// @brief Logical AND operator. 
213   bool operator&&(const APInt& RHS) const;
214
215   /// Performs logical OR operation on this APInt and the given APInt& RHS.
216   /// @brief Logical OR operator. 
217   bool operator||(const APInt& RHS) const;
218
219   /// Performs logical negation operation on this APInt.
220   /// @brief Logical negation operator. 
221   bool operator !() const;
222
223   /// Multiplies this APInt by the given APInt& RHS.
224   /// @brief Multiplication operator. 
225   APInt operator*(const APInt& RHS) const;
226
227   /// Adds this APInt by the given APInt& RHS.
228   /// @brief Addition operator. 
229   APInt operator+(const APInt& RHS) const;
230
231   /// Subtracts this APInt by the given APInt& RHS
232   /// @brief Subtraction operator. 
233   APInt operator-(const APInt& RHS) const;
234
235   ///
236   inline APInt operator-() const {
237     return APInt(0, BitsNum) - (*this);
238   }
239
240   /// @brief Array-indexing support.
241   bool operator[](unsigned bitPosition) const;
242
243   /// Compare this APInt with the given APInt& RHS 
244   /// for the validity of the equality relationship.
245   /// @brief Equality operator. 
246   bool operator==(const APInt& RHS) const;
247
248   /// Compare this APInt with the given uint64_t value
249   /// for the validity of the equality relationship.
250   /// @brief Equality operator.
251   bool operator==(uint64_t Val) const;
252
253   /// Compare this APInt with the given APInt& RHS 
254   /// for the validity of the inequality relationship.
255   /// @brief Inequality operator. 
256   inline bool operator!=(const APInt& RHS) const {
257     return !((*this) == RHS);
258   }
259
260   /// Compare this APInt with the given uint64_t value 
261   /// for the validity of the inequality relationship.
262   /// @brief Inequality operator. 
263   inline bool operator!=(uint64_t Val) const {
264     return !((*this) == Val);
265   }
266   
267   /// Compare this APInt with the given APInt& RHS for 
268   /// the validity of the less-than relationship.
269   /// @brief Less-than operator. 
270   bool operator <(const APInt& RHS) const;
271
272   /// Compare this APInt with the given APInt& RHS for the validity 
273   /// of the less-than-or-equal relationship.
274   /// @brief Less-than-or-equal operator. 
275   bool operator<=(const APInt& RHS) const;
276
277   /// Compare this APInt with the given APInt& RHS for the validity 
278   /// of the greater-than relationship.
279   /// @brief Greater-than operator. 
280   bool operator> (const APInt& RHS) const;
281
282   /// @brief Greater-than-or-equal operator. 
283   /// Compare this APInt with the given APInt& RHS for the validity 
284   /// of the greater-than-or-equal relationship.
285   bool operator>=(const APInt& RHS) const;
286
287   /// @returns a uint64_t value from this APInt. If this APInt contains a single
288   /// word, just returns VAL, otherwise pVal[0].
289   inline uint64_t getValue() const {
290     if (isSingleWord())
291       return VAL;
292     unsigned n = getNumWords() * 64 - CountLeadingZeros();
293     if (n <= 64)
294       return pVal[0];
295     assert(0 && "This APInt's bitwidth > 64");
296   }
297
298   /// @returns the largest value for an APInt of the specified bit-width and 
299   /// if isSign == true, it should be largest signed value, otherwise largest
300   /// unsigned value.
301   /// @brief Gets max value of the APInt with bitwidth <= 64.
302   static APInt getMaxValue(unsigned numBits, bool isSign);
303
304   /// @returns the smallest value for an APInt of the given bit-width and
305   /// if isSign == true, it should be smallest signed value, otherwise zero.
306   /// @brief Gets min value of the APInt with bitwidth <= 64.
307   static APInt getMinValue(unsigned numBits, bool isSign);
308
309   /// @returns the all-ones value for an APInt of the specified bit-width.
310   /// @brief Get the all-ones value.
311   static APInt getAllOnesValue(unsigned numBits);
312
313   /// @returns the '0' value for an APInt of the specified bit-width.
314   /// @brief Get the '0' value.
315   static APInt getNullValue(unsigned numBits);
316
317   /// @brief Set every bit to 1.
318   APInt& set();
319
320   /// Set the given bit to 1 whose position is given as "bitPosition".
321   /// @brief Set a given bit to 1.
322   APInt& set(unsigned bitPosition);
323
324   /// @brief Set every bit to 0.
325   APInt& clear();
326
327   /// Set the given bit to 0 whose position is given as "bitPosition".
328   /// @brief Set a given bit to 0.
329   APInt& clear(unsigned bitPosition);
330
331   /// @brief Toggle every bit to its opposite value.
332   APInt& flip();
333
334   /// Toggle a given bit to its opposite value whose position is given 
335   /// as "bitPosition".
336   /// @brief Toggles a given bit to its opposite value.
337   APInt& flip(unsigned bitPosition);
338
339   /// @returns a character interpretation of the APInt.
340   std::string to_string(uint8_t radix = 10) const;
341
342   /// Get an APInt with the same BitsNum as this APInt, just zero mask
343   /// the low bits and right shift to the least significant bit.
344   /// @returns the high "numBits" bits of this APInt.
345   APInt HiBits(unsigned numBits) const;
346
347   /// Get an APInt with the same BitsNum as this APInt, just zero mask
348   /// the high bits.
349   /// @returns the low "numBits" bits of this APInt.
350   APInt LoBits(unsigned numBits) const;
351
352   /// @returns true if the argument APInt value is a power of two > 0.
353   inline const bool isPowerOf2() const {
354     return (!!*this) && !(*this & (*this - 1));
355   }
356
357   /// @returns the number of zeros from the most significant bit to the first
358   /// one bits.
359   unsigned CountLeadingZeros() const;
360
361   /// @returns the number of zeros from the least significant bit to the first
362   /// one bit.
363   unsigned CountTrailingZeros() const;
364
365   /// @returns the number of set bits.
366   unsigned CountPopulation() const; 
367
368   /// @returns the total number of bits.
369   inline unsigned getNumBits() const
370   { return BitsNum; }
371
372   /// @brief Check if this APInt has a N-bits integer value.
373   inline bool IsIntN(unsigned N) const {
374     if (isSingleWord()) {
375       return VAL == VAL & (~uint64_t(0ULL) >> (64 - N));
376     } else {
377       APInt Tmp(N, pVal);
378       return Tmp == (*this);
379     }
380   }
381
382   /// @returns a byte-swapped representation of this APInt Value.
383   APInt ByteSwap() const;
384
385   /// @returns the floor log base 2 of this APInt.
386   inline unsigned LogBase2() const {
387     return getNumWords() * APINT_BITS_PER_WORD - 
388            CountLeadingZeros();
389   }
390
391   /// @brief Converts this APInt to a double value.
392   double RoundToDouble(bool isSigned = false) const;
393
394   /// Arithmetic right-shift this APInt by shiftAmt.
395   /// @brief Arithmetic right-shift function.
396   APInt AShr(unsigned shiftAmt) const;
397
398   /// Logical right-shift this APInt by shiftAmt.
399   /// @brief Logical right-shift function.
400   APInt LShr(unsigned shiftAmt) const;
401
402   /// Left-shift this APInt by shiftAmt.
403   /// @brief Left-shift function.
404   APInt Shl(unsigned shiftAmt) const;
405
406   /// Signed divide this APInt by APInt RHS.
407   /// @brief Signed division function for APInt.
408   inline APInt SDiv(const APInt& RHS) const {
409     bool isSignedLHS = (*this)[BitsNum - 1], isSignedRHS = RHS[RHS.BitsNum - 1];
410     APInt API = APIntOps::UDiv(isSignedLHS ? -(*this) : (*this), isSignedRHS ? -RHS : RHS);
411     return isSignedLHS != isSignedRHS ? -API : API;;
412   }
413
414   /// Unsigned divide this APInt by APInt RHS.
415   /// @brief Unsigned division function for APInt.
416   APInt UDiv(const APInt& RHS) const;
417
418   /// Signed remainder operation on APInt.
419   /// @brief Function for signed remainder operation.
420   inline APInt SRem(const APInt& RHS) const {
421     bool isSignedLHS = (*this)[BitsNum - 1], isSignedRHS = RHS[RHS.BitsNum - 1];
422     APInt API = APIntOps::URem(isSignedLHS ? -(*this) : (*this), isSignedRHS ? -RHS : RHS);
423     return isSignedLHS ? -API : API;
424   }
425
426   /// Unsigned remainder operation on APInt.
427   /// @brief Function for unsigned remainder operation.
428   APInt URem(const APInt& RHS) const;
429
430 };
431
432 namespace APIntOps {
433
434 /// @brief Check if the specified APInt has a N-bits integer value.
435 inline bool isIntN(unsigned N, const APInt& APIVal) {
436   return APIVal.IsIntN(N);
437 }
438
439 /// @returns true if the argument APInt value is a sequence of ones
440 /// starting at the least significant bit with the remainder zero.
441 inline const bool isMask(unsigned numBits, const APInt& APIVal) {
442   return APIVal && ((APIVal + 1) & APIVal) == 0;
443 }
444
445 /// @returns true if the argument APInt value contains a sequence of ones
446 /// with the remainder zero.
447 inline const bool isShiftedMask(unsigned numBits, const APInt& APIVal) {
448   return isMask(numBits, (APIVal - 1) | APIVal);
449 }
450
451 /// @returns a byte-swapped representation of the specified APInt Value.
452 inline APInt ByteSwap(const APInt& APIVal) {
453   return APIVal.ByteSwap();
454 }
455
456 /// @returns the floor log base 2 of the specified APInt value.
457 inline unsigned LogBase2(const APInt& APIVal) {
458   return APIVal.LogBase2(); 
459 }
460
461 /// @returns the greatest common divisor of the two values 
462 /// using Euclid's algorithm.
463 APInt GreatestCommonDivisor(const APInt& API1, const APInt& API2);
464
465 /// @brief Converts the given APInt to a double value.
466 inline double APIntRoundToDouble(const APInt& APIVal, bool isSigned = false) {
467   return APIVal.RoundToDouble(isSigned);
468 }
469
470 /// @brief Converts the given APInt to a float vlalue.
471 inline float APIntRoundToFloat(const APInt& APIVal) {
472   return float(APIntRoundToDouble(APIVal));
473 }
474
475 /// @brief Converts the given double value into a APInt.
476 APInt DoubleRoundToAPInt(double Double);
477
478 /// @brief Converts the given float value into a APInt.
479 inline APInt FloatRoundToAPInt(float Float) {
480   return DoubleRoundToAPInt(double(Float));
481 }
482
483 /// Arithmetic right-shift the APInt by shiftAmt.
484 /// @brief Arithmetic right-shift function.
485 inline APInt AShr(const APInt& LHS, unsigned shiftAmt) {
486   return LHS.AShr(shiftAmt);
487 }
488
489 /// Logical right-shift the APInt by shiftAmt.
490 /// @brief Logical right-shift function.
491 inline APInt LShr(const APInt& LHS, unsigned shiftAmt) {
492   return LHS.LShr(shiftAmt);
493 }
494
495 /// Left-shift the APInt by shiftAmt.
496 /// @brief Left-shift function.
497 inline APInt Shl(const APInt& LHS, unsigned shiftAmt) {
498   return LHS.Shl(shiftAmt);
499 }
500
501 /// Signed divide APInt LHS by APInt RHS.
502 /// @brief Signed division function for APInt.
503 inline APInt SDiv(const APInt& LHS, const APInt& RHS) {
504   return LHS.SDiv(RHS);
505 }
506
507 /// Unsigned divide APInt LHS by APInt RHS.
508 /// @brief Unsigned division function for APInt.
509 inline APInt UDiv(const APInt& LHS, const APInt& RHS) {
510   return LHS.UDiv(RHS);
511 }
512
513 /// Signed remainder operation on APInt.
514 /// @brief Function for signed remainder operation.
515 inline APInt SRem(const APInt& LHS, const APInt& RHS) {
516   return LHS.SRem(RHS);
517 }
518
519 /// Unsigned remainder operation on APInt.
520 /// @brief Function for unsigned remainder operation.
521 inline APInt URem(const APInt& LHS, const APInt& RHS) {
522   return LHS.URem(RHS);
523 }
524
525 /// Performs multiplication on APInt values.
526 /// @brief Function for multiplication operation.
527 inline APInt Mul(const APInt& LHS, const APInt& RHS) {
528   return LHS * RHS;
529 }
530
531 /// Performs addition on APInt values.
532 /// @brief Function for addition operation.
533 inline APInt Add(const APInt& LHS, const APInt& RHS) {
534   return LHS + RHS;
535 }
536
537 /// Performs subtraction on APInt values.
538 /// @brief Function for subtraction operation.
539 inline APInt Sub(const APInt& LHS, const APInt& RHS) {
540   return LHS - RHS;
541 }
542
543 /// Performs bitwise AND operation on APInt LHS and 
544 /// APInt RHS.
545 /// @brief Bitwise AND function for APInt.
546 inline APInt And(const APInt& LHS, const APInt& RHS) {
547   return LHS & RHS;
548 }
549
550 /// Performs bitwise OR operation on APInt LHS and APInt RHS.
551 /// @brief Bitwise OR function for APInt. 
552 inline APInt Or(const APInt& LHS, const APInt& RHS) {
553   return LHS | RHS;
554 }
555
556 /// Performs bitwise XOR operation on APInt.
557 /// @brief Bitwise XOR function for APInt.
558 inline APInt Xor(const APInt& LHS, const APInt& RHS) {
559   return LHS ^ RHS;
560
561
562 /// Performs a bitwise complement operation on APInt.
563 /// @brief Bitwise complement function. 
564 inline APInt Not(const APInt& APIVal) {
565   return ~APIVal;
566 }
567
568 } // End of APIntOps namespace
569
570 } // End of llvm namespace
571
572 #endif