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 //===----------------------------------------------------------------------===//
26 //===----------------------------------------------------------------------===//
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.
36 /// @brief Class for arbitrary precision integers.
38 /// Note: In this class, all bit/byte/word positions are zero-based.
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);
49 unsigned BitsNum; ///< The number of bits.
50 bool isSigned; ///< The sign flag for this APInt.
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.
56 uint64_t VAL; ///< Used to store the <= 64 bits integer value.
57 uint64_t *pVal; ///< Used to store the >64 bits integer value.
60 /// This enum is just used to hold a constant we needed for APInt.
62 APINT_BITS_PER_WORD = sizeof(uint64_t) * 8
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;
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; }
77 /// @returns the word position for the specified bit position.
78 static inline unsigned whichWord(unsigned bitPosition)
79 { return bitPosition / APINT_BITS_PER_WORD; }
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; }
85 /// @returns the bit position in a word for the specified bit position
87 static inline unsigned whichBit(unsigned bitPosition)
88 { return bitPosition % APINT_BITS_PER_WORD; }
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); }
95 inline void TruncToBits() {
97 VAL &= ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitsNum);
99 pVal[getNumWords() - 1] &= ~uint64_t(0ULL) >>
100 (APINT_BITS_PER_WORD - (whichBit(BitsNum - 1) + 1));
103 /// @returns the corresponding word for the specified bit position.
104 inline uint64_t& getWord(unsigned bitPosition)
105 { return isSingleWord() ? VAL : pVal[whichWord(bitPosition)]; }
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)]; }
112 /// @brief Converts a char array into an integer.
113 void StrToAPInt(const char *StrStart, unsigned slen, uint8_t radix);
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,
120 /// @brief Create a new APInt of numBits bit-width, and initialized as
122 APInt(unsigned numBits, uint64_t bigVal[], bool sign = false);
124 /// @brief Create a new APInt by translating the string represented
126 APInt(const std::string& Val, uint8_t radix = 10, bool sign = false);
128 /// @brief Create a new APInt by translating the char array represented
130 APInt(const char StrStart[], unsigned slen, uint8_t radix, bool sign = false);
132 /// @brief Copy Constructor.
133 APInt(const APInt& API);
135 /// @brief Destructor.
138 /// @brief Copy assignment operator.
139 APInt& operator=(const APInt& RHS);
141 /// Assigns an integer value to the APInt.
142 /// @brief Assignment operator.
143 APInt& operator=(uint64_t RHS);
145 /// Increments the APInt by one.
146 /// @brief Postfix increment operator.
147 inline const APInt operator++(int) {
152 /// Increments the APInt by one.
153 /// @brief Prefix increment operator.
156 /// Decrements the APInt by one.
157 /// @brief Postfix decrement operator.
158 inline const APInt operator--(int) {
163 /// Decrements the APInt by one.
164 /// @brief Prefix decrement operator.
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);
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);
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);
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);
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);
190 /// Performs a bitwise complement operation on this APInt.
191 /// @brief Bitwise complement operator.
192 APInt operator~() const;
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);
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);
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);
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);
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);
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;
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;
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;
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;
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;
240 /// Performs logical negation operation on this APInt.
241 /// @brief Logical negation operator.
242 bool operator !() const;
244 /// Multiplies this APInt by the given APInt& RHS.
245 /// @brief Multiplication operator.
246 APInt operator*(const APInt& RHS) const;
248 /// Divides this APInt by the given APInt& RHS.
249 /// @brief Division operator.
250 APInt operator/(const APInt& RHS) const;
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;
257 /// Adds this APInt by the given APInt& RHS.
258 /// @brief Addition operator.
259 APInt operator+(const APInt& RHS) const;
261 /// Subtracts this APInt by the given APInt& RHS
262 /// @brief Subtraction operator.
263 APInt operator-(const APInt& RHS) const;
265 /// Left-shift the APInt by shiftAmt.
266 /// @brief Left-shift operator.
267 APInt operator<<(unsigned shiftAmt) const;
269 /// Right-shift the APInt by shiftAmt.
270 /// @brief Right-shift operator.
271 APInt operator>>(unsigned shiftAmt) const;
273 /// @brief Array-indexing support.
274 bool operator[](unsigned bitPosition) const;
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;
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;
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);
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);
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;
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;
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;
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;
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() {
324 return isSigned ? ((int64_t(VAL) << (APINT_BITS_PER_WORD - BitsNum)) >>
325 (APINT_BITS_PER_WORD - BitsNum)) :
327 assert(0 && "This APInt's bitwidth > 64");
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
333 /// @brief Gets max value of the APInt with bitwidth <= 64.
334 static APInt getMaxValue(unsigned numBits, bool isSign);
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);
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);
345 /// @brief Set every bit to 1.
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);
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);
356 /// @brief Set every bit to 0.
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);
363 /// @brief Toggle every bit to its opposite value.
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);
371 /// @returns a character interpretation of the APInt.
372 std::string to_string(uint8_t radix = 10) const;
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;
379 /// Get an APInt with the same BitsNum as this APInt, just zero mask
381 /// @returns the low "numBits" bits of this APInt.
382 APInt LoBits(unsigned numBits) const;
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));
389 /// @returns the number of zeros from the most significant bit to the first
391 unsigned CountLeadingZeros() const;
393 /// @returns the number of zeros from the least significant bit to the first
395 unsigned CountTrailingZeros() const;
397 /// @returns the number of set bits.
398 unsigned CountPopulation() const;
400 /// @returns the total number of bits.
401 inline unsigned getNumBits() const
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;
412 APInt Tmp(N, APIVal.pVal);
413 return Tmp == APIVal;
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;
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);
429 /// @returns a byte-swapped representation of the specified APInt Value.
430 APInt ByteSwap(const APInt& APIVal);
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();
438 /// @returns the greatest common divisor of the two values
439 /// using Euclid's algorithm.
440 APInt GreatestCommonDivisor(const APInt& API1, const APInt& API2);
442 } // End of llvm namespace