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() const {
292 unsigned n = getNumWords() * 64 - CountLeadingZeros();
295 assert(0 && "This APInt's bitwidth > 64");
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
301 /// @brief Gets max value of the APInt with bitwidth <= 64.
302 static APInt getMaxValue(unsigned numBits, bool isSign);
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);
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);
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);
317 /// @brief Set every bit to 1.
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);
324 /// @brief Set every bit to 0.
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);
331 /// @brief Toggle every bit to its opposite value.
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);
339 /// @returns a character interpretation of the APInt.
340 std::string to_string(uint8_t radix = 10) const;
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;
347 /// Get an APInt with the same BitsNum as this APInt, just zero mask
349 /// @returns the low "numBits" bits of this APInt.
350 APInt LoBits(unsigned numBits) const;
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));
357 /// @returns the number of zeros from the most significant bit to the first
359 unsigned CountLeadingZeros() const;
361 /// @returns the number of zeros from the least significant bit to the first
363 unsigned CountTrailingZeros() const;
365 /// @returns the number of set bits.
366 unsigned CountPopulation() const;
368 /// @returns the total number of bits.
369 inline unsigned getNumBits() const
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));
378 return Tmp == (*this);
382 /// @returns a byte-swapped representation of this APInt Value.
383 APInt ByteSwap() const;
385 /// @returns the floor log base 2 of this APInt.
386 inline unsigned LogBase2() const {
387 return getNumWords() * APINT_BITS_PER_WORD -
391 /// @brief Converts this APInt to a double value.
392 double RoundToDouble(bool isSigned = false) const;
394 /// Arithmetic right-shift this APInt by shiftAmt.
395 /// @brief Arithmetic right-shift function.
396 APInt AShr(unsigned shiftAmt) const;
398 /// Logical right-shift this APInt by shiftAmt.
399 /// @brief Logical right-shift function.
400 APInt LShr(unsigned shiftAmt) const;
402 /// Left-shift this APInt by shiftAmt.
403 /// @brief Left-shift function.
404 APInt Shl(unsigned shiftAmt) const;
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;;
414 /// Unsigned divide this APInt by APInt RHS.
415 /// @brief Unsigned division function for APInt.
416 APInt UDiv(const APInt& RHS) const;
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;
426 /// Unsigned remainder operation on APInt.
427 /// @brief Function for unsigned remainder operation.
428 APInt URem(const APInt& RHS) const;
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);
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;
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);
451 /// @returns a byte-swapped representation of the specified APInt Value.
452 inline APInt ByteSwap(const APInt& APIVal) {
453 return APIVal.ByteSwap();
456 /// @returns the floor log base 2 of the specified APInt value.
457 inline unsigned LogBase2(const APInt& APIVal) {
458 return APIVal.LogBase2();
461 /// @returns the greatest common divisor of the two values
462 /// using Euclid's algorithm.
463 APInt GreatestCommonDivisor(const APInt& API1, const APInt& API2);
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);
470 /// @brief Converts the given APInt to a float vlalue.
471 inline float APIntRoundToFloat(const APInt& APIVal) {
472 return float(APIntRoundToDouble(APIVal));
475 /// @brief Converts the given double value into a APInt.
476 APInt DoubleRoundToAPInt(double Double);
478 /// @brief Converts the given float value into a APInt.
479 inline APInt FloatRoundToAPInt(float Float) {
480 return DoubleRoundToAPInt(double(Float));
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);
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);
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);
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);
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);
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);
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);
525 /// Performs multiplication on APInt values.
526 /// @brief Function for multiplication operation.
527 inline APInt Mul(const APInt& LHS, const APInt& RHS) {
531 /// Performs addition on APInt values.
532 /// @brief Function for addition operation.
533 inline APInt Add(const APInt& LHS, const APInt& RHS) {
537 /// Performs subtraction on APInt values.
538 /// @brief Function for subtraction operation.
539 inline APInt Sub(const APInt& LHS, const APInt& RHS) {
543 /// Performs bitwise AND operation on APInt LHS and
545 /// @brief Bitwise AND function for APInt.
546 inline APInt And(const APInt& LHS, const APInt& RHS) {
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) {
556 /// Performs bitwise XOR operation on APInt.
557 /// @brief Bitwise XOR function for APInt.
558 inline APInt Xor(const APInt& LHS, const APInt& RHS) {
562 /// Performs a bitwise complement operation on APInt.
563 /// @brief Bitwise complement function.
564 inline APInt Not(const APInt& APIVal) {
568 } // End of APIntOps namespace
570 } // End of llvm namespace