1 //===-- llvm/Support/APInt.h - For Arbitrary Precision Integer -*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements a class to represent arbitrary precision integral
13 //===----------------------------------------------------------------------===//
18 #include "llvm/Support/DataTypes.h"
24 /// Forward declaration.
27 APInt udiv(const APInt& LHS, const APInt& RHS);
28 APInt urem(const APInt& LHS, const APInt& RHS);
31 //===----------------------------------------------------------------------===//
33 //===----------------------------------------------------------------------===//
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.
43 /// @brief Class for arbitrary precision integers.
45 /// Note: In this class, all bit/byte/word positions are zero-based.
48 unsigned BitsNum; ///< The number of bits.
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.
54 uint64_t VAL; ///< Used to store the <= 64 bits integer value.
55 uint64_t *pVal; ///< Used to store the >64 bits integer value.
58 /// This enum is just used to hold a constant we needed for APInt.
60 APINT_BITS_PER_WORD = sizeof(uint64_t) * 8
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;
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; }
75 /// @returns the word position for the specified bit position.
76 static inline unsigned whichWord(unsigned bitPosition)
77 { return bitPosition / APINT_BITS_PER_WORD; }
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; }
83 /// @returns the bit position in a word for the specified bit position
85 static inline unsigned whichBit(unsigned bitPosition)
86 { return bitPosition % APINT_BITS_PER_WORD; }
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); }
93 inline void TruncToBits() {
95 VAL &= ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitsNum);
97 pVal[getNumWords() - 1] &= ~uint64_t(0ULL) >>
98 (APINT_BITS_PER_WORD - (whichBit(BitsNum - 1) + 1));
101 /// @returns the corresponding word for the specified bit position.
102 inline uint64_t& getWord(unsigned bitPosition)
103 { return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; }
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)]; }
110 /// @brief Converts a char array into an integer.
111 void StrToAPInt(const char *StrStart, unsigned slen, uint8_t radix);
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);
117 /// @brief Create a new APInt of numBits bit-width, and initialized as
119 APInt(unsigned numBits, uint64_t bigVal[]);
121 /// @brief Create a new APInt by translating the string represented
123 APInt(const std::string& Val, uint8_t radix = 10);
125 /// @brief Create a new APInt by translating the char array represented
127 APInt(const char StrStart[], unsigned slen, uint8_t radix);
129 /// @brief Copy Constructor.
130 APInt(const APInt& API);
132 /// @brief Destructor.
135 /// @brief Copy assignment operator.
136 APInt& operator=(const APInt& RHS);
138 /// Assigns an integer value to the APInt.
139 /// @brief Assignment operator.
140 APInt& operator=(uint64_t RHS);
142 /// Increments the APInt by one.
143 /// @brief Postfix increment operator.
144 inline const APInt operator++(int) {
149 /// Increments the APInt by one.
150 /// @brief Prefix increment operator.
153 /// Decrements the APInt by one.
154 /// @brief Postfix decrement operator.
155 inline const APInt operator--(int) {
160 /// Decrements the APInt by one.
161 /// @brief Prefix decrement operator.
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);
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);
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);
179 /// Performs a bitwise complement operation on this APInt.
180 /// @brief Bitwise complement operator.
181 APInt operator~() const;
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);
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);
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);
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;
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;
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;
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;
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;
219 /// Performs logical negation operation on this APInt.
220 /// @brief Logical negation operator.
221 bool operator !() const;
223 /// Multiplies this APInt by the given APInt& RHS.
224 /// @brief Multiplication operator.
225 APInt operator*(const APInt& RHS) const;
227 /// Adds this APInt by the given APInt& RHS.
228 /// @brief Addition operator.
229 APInt operator+(const APInt& RHS) const;
231 /// Subtracts this APInt by the given APInt& RHS
232 /// @brief Subtraction operator.
233 APInt operator-(const APInt& RHS) const;
236 inline APInt operator-() const {
237 return APInt(0, BitsNum) - (*this);
240 /// @brief Array-indexing support.
241 bool operator[](unsigned bitPosition) const;
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;
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;
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);
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);
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;
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;
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;
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;
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() {
292 assert(0 && "This APInt's bitwidth > 64");
295 /// @returns the largest value for an APInt of the specified bit-width and
296 /// if isSign == true, it should be largest signed value, otherwise largest
298 /// @brief Gets max value of the APInt with bitwidth <= 64.
299 static APInt getMaxValue(unsigned numBits, bool isSign);
301 /// @returns the smallest value for an APInt of the given bit-width and
302 /// if isSign == true, it should be smallest signed value, otherwise zero.
303 /// @brief Gets min value of the APInt with bitwidth <= 64.
304 static APInt getMinValue(unsigned numBits, bool isSign);
306 /// @returns the all-ones value for an APInt of the specified bit-width.
307 /// @brief Get the all-ones value.
308 static APInt getAllOnesValue(unsigned numBits);
310 /// @brief Set every bit to 1.
313 /// Set the given bit to 1 whose position is given as "bitPosition".
314 /// @brief Set a given bit to 1.
315 APInt& set(unsigned bitPosition);
317 /// @returns the '0' value for an APInt of the specified bit-width.
318 /// @brief Get the '0' value.
319 static APInt getNullValue(unsigned numBits);
321 /// @brief Set every bit to 0.
324 /// Set the given bit to 0 whose position is given as "bitPosition".
325 /// @brief Set a given bit to 0.
326 APInt& clear(unsigned bitPosition);
328 /// @brief Toggle every bit to its opposite value.
331 /// Toggle a given bit to its opposite value whose position is given
332 /// as "bitPosition".
333 /// @brief Toggles a given bit to its opposite value.
334 APInt& flip(unsigned bitPosition);
336 /// @returns a character interpretation of the APInt.
337 std::string to_string(uint8_t radix = 10) const;
339 /// Get an APInt with the same BitsNum 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 HiBits(unsigned numBits) const;
344 /// Get an APInt with the same BitsNum as this APInt, just zero mask
346 /// @returns the low "numBits" bits of this APInt.
347 APInt LoBits(unsigned numBits) const;
349 /// @returns true if the argument APInt value is a power of two > 0.
350 inline const bool isPowerOf2() const {
351 return (!!*this) && !(*this & (*this - 1));
354 /// @returns the number of zeros from the most significant bit to the first
356 unsigned CountLeadingZeros() const;
358 /// @returns the number of zeros from the least significant bit to the first
360 unsigned CountTrailingZeros() const;
362 /// @returns the number of set bits.
363 unsigned CountPopulation() const;
365 /// @returns the total number of bits.
366 inline unsigned getNumBits() const
369 /// @brief Check if this APInt has a N-bits integer value.
370 inline bool isIntN(unsigned N) const {
371 if (isSingleWord()) {
372 return VAL == VAL & (~uint64_t(0ULL) >> (64 - N));
375 return Tmp == (*this);
379 /// @returns a byte-swapped representation of this APInt Value.
380 APInt ByteSwap() const;
382 /// @returns the floor log base 2 of this APInt.
383 inline unsigned LogBase2() const {
384 return getNumWords() * APINT_BITS_PER_WORD -
388 /// Arithmetic right-shift this APInt by shiftAmt.
389 /// @brief Arithmetic right-shift function.
390 APInt ashr(unsigned shiftAmt) const;
392 /// Logical right-shift this APInt by shiftAmt.
393 /// @brief Logical right-shift function.
394 APInt lshr(unsigned shiftAmt) const;
396 /// Left-shift this APInt by shiftAmt.
397 /// @brief Left-shift function.
398 APInt shl(unsigned shiftAmt) const;
400 /// Signed divide this APInt by APInt RHS.
401 /// @brief Signed division function for APInt.
402 inline APInt sdiv(const APInt& RHS) const {
403 bool isSignedLHS = (*this)[BitsNum - 1], isSignedRHS = RHS[RHS.BitsNum - 1];
404 APInt API = APIntOps::udiv(isSignedLHS ? -(*this) : (*this), isSignedRHS ? -RHS : RHS);
405 return isSignedLHS != isSignedRHS ? -API : API;;
408 /// Unsigned divide this APInt by APInt RHS.
409 /// @brief Unsigned division function for APInt.
410 APInt udiv(const APInt& RHS) const;
412 /// Signed remainder operation on APInt.
413 /// @brief Function for signed remainder operation.
414 inline APInt srem(const APInt& RHS) const {
415 bool isSignedLHS = (*this)[BitsNum - 1], isSignedRHS = RHS[RHS.BitsNum - 1];
416 APInt API = APIntOps::urem(isSignedLHS ? -(*this) : (*this), isSignedRHS ? -RHS : RHS);
417 return isSignedLHS ? -API : API;
420 /// Unsigned remainder operation on APInt.
421 /// @brief Function for unsigned remainder operation.
422 APInt urem(const APInt& RHS) const;
428 /// @brief Check if the specified APInt has a N-bits integer value.
429 inline bool isIntN(unsigned N, const APInt& APIVal) {
430 return APIVal.isIntN(N);
433 /// @returns true if the argument APInt value is a sequence of ones
434 /// starting at the least significant bit with the remainder zero.
435 inline const bool isMask(unsigned numBits, const APInt& APIVal) {
436 return APIVal && ((APIVal + 1) & APIVal) == 0;
439 /// @returns true if the argument APInt value contains a sequence of ones
440 /// with the remainder zero.
441 inline const bool isShiftedMask(unsigned numBits, const APInt& APIVal) {
442 return isMask(numBits, (APIVal - 1) | APIVal);
445 /// @returns a byte-swapped representation of the specified APInt Value.
446 inline APInt ByteSwap(const APInt& APIVal) {
447 return APIVal.ByteSwap();
450 /// @returns the floor log base 2 of the specified APInt value.
451 inline unsigned LogBase2(const APInt& APIVal) {
452 return APIVal.LogBase2();
455 /// @returns the greatest common divisor of the two values
456 /// using Euclid's algorithm.
457 APInt GreatestCommonDivisor(const APInt& API1, const APInt& API2);
459 /// Arithmetic right-shift the APInt by shiftAmt.
460 /// @brief Arithmetic right-shift function.
461 inline APInt ashr(const APInt& LHS, unsigned shiftAmt) {
462 return LHS.ashr(shiftAmt);
465 /// Logical right-shift the APInt by shiftAmt.
466 /// @brief Logical right-shift function.
467 inline APInt lshr(const APInt& LHS, unsigned shiftAmt) {
468 return LHS.lshr(shiftAmt);
471 /// Left-shift the APInt by shiftAmt.
472 /// @brief Left-shift function.
473 inline APInt shl(const APInt& LHS, unsigned shiftAmt) {
474 return LHS.shl(shiftAmt);
477 /// Signed divide APInt LHS by APInt RHS.
478 /// @brief Signed division function for APInt.
479 inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
480 return LHS.sdiv(RHS);
483 /// Unsigned divide APInt LHS by APInt RHS.
484 /// @brief Unsigned division function for APInt.
485 inline APInt udiv(const APInt& LHS, const APInt& RHS) {
486 return LHS.udiv(RHS);
489 /// Signed remainder operation on APInt.
490 /// @brief Function for signed remainder operation.
491 inline APInt srem(const APInt& LHS, const APInt& RHS) {
492 return LHS.srem(RHS);
495 /// Unsigned remainder operation on APInt.
496 /// @brief Function for unsigned remainder operation.
497 inline APInt urem(const APInt& LHS, const APInt& RHS) {
498 return LHS.urem(RHS);
501 /// Performs multiplication on APInt values.
502 /// @brief Function for multiplication operation.
503 inline APInt mul(const APInt& LHS, const APInt& RHS) {
507 /// Performs addition on APInt values.
508 /// @brief Function for addition operation.
509 inline APInt add(const APInt& LHS, const APInt& RHS) {
513 /// Performs subtraction on APInt values.
514 /// @brief Function for subtraction operation.
515 inline APInt sub(const APInt& LHS, const APInt& RHS) {
519 /// Performs bitwise AND operation on APInt LHS and
521 /// @brief Bitwise AND function for APInt.
522 inline APInt And(const APInt& LHS, const APInt& RHS) {
526 /// Performs bitwise OR operation on APInt LHS and APInt RHS.
527 /// @brief Bitwise OR function for APInt.
528 inline APInt Or(const APInt& LHS, const APInt& RHS) {
532 /// Performs bitwise XOR operation on APInt.
533 /// @brief Bitwise XOR function for APInt.
534 inline APInt Xor(const APInt& LHS, const APInt& RHS) {
538 /// Performs a bitwise complement operation on APInt.
539 /// @brief Bitwise complement function.
540 inline APInt Not(const APInt& APIVal) {
544 } // End of APIntOps namespace
546 } // End of llvm namespace