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 sizes 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 both
41 /// the typical integer arithmetic and comparison operations as well as bitwise
44 /// The class has several invariants worth noting:
45 /// * All bit, byte, and word positions are zero-based.
46 /// * Once the bit width is set, it doesn't change except by the Truncate,
47 /// SignExtend, or ZeroExtend operations.
48 /// * All binary operators must be on APInt instances of the same bit width.
49 /// Attempting to use these operators on instances with different bit
50 /// widths will yield an assertion.
51 /// * The value is stored canonically as an unsigned value. For operations
52 /// where it makes a difference, there are both signed and unsigned variants
53 /// of the operation. For example, sdiv and udiv. However, because the bit
54 /// widths must be the same, operations such as Mul and Add produce the same
55 /// results regardless of whether the values are interpreted as signed or
57 /// * In general, the class tries to follow the style of computation that LLVM
58 /// uses in its IR. This simplifies its use for LLVM.
60 /// @brief Class for arbitrary precision integers.
63 uint32_t BitWidth; ///< The number of bits in this APInt.
65 /// This union is used to store the integer value. When the
66 /// integer bit-width <= 64, it uses VAL;
67 /// otherwise it uses the pVal.
69 uint64_t VAL; ///< Used to store the <= 64 bits integer value.
70 uint64_t *pVal; ///< Used to store the >64 bits integer value.
73 /// This enum is just used to hold a constant we needed for APInt.
75 APINT_BITS_PER_WORD = sizeof(uint64_t) * 8,
76 APINT_WORD_SIZE = sizeof(uint64_t)
79 // Fast internal constructor
80 APInt(uint64_t* val, uint32_t bits) : BitWidth(bits), pVal(val) { }
82 /// Here one word's bitwidth equals to that of uint64_t.
83 /// @returns the number of words to hold the integer value of this APInt.
84 /// @brief Get the number of words.
85 inline uint32_t getNumWords() const {
86 return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
89 /// @returns true if the number of bits <= 64, false otherwise.
90 /// @brief Determine if this APInt just has one word to store value.
91 inline bool isSingleWord() const {
92 return BitWidth <= APINT_BITS_PER_WORD;
95 /// @returns the word position for the specified bit position.
96 static inline uint32_t whichWord(uint32_t bitPosition) {
97 return bitPosition / APINT_BITS_PER_WORD;
100 /// @returns the bit position in a word for the specified bit position
102 static inline uint32_t whichBit(uint32_t bitPosition) {
103 return bitPosition % APINT_BITS_PER_WORD;
106 /// @returns a uint64_t type integer with just bit position at
107 /// "whichBit(bitPosition)" setting, others zero.
108 static inline uint64_t maskBit(uint32_t bitPosition) {
109 return 1ULL << whichBit(bitPosition);
112 /// This method is used internally to clear the to "N" bits that are not used
113 /// by the APInt. This is needed after the most significant word is assigned
114 /// a value to ensure that those bits are zero'd out.
115 /// @brief Clear high order bits
116 inline APInt& clearUnusedBits() {
117 // Compute how many bits are used in the final word
118 uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
120 // If all bits are used, we want to leave the value alone. This also
121 // avoids the undefined behavior of >> when the shfit is the same size as
122 // the word size (64).
125 // Mask out the hight bits.
126 uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
130 pVal[getNumWords() - 1] &= mask;
134 /// @returns the corresponding word for the specified bit position.
135 /// @brief Get the word corresponding to a bit position
136 inline uint64_t getWord(uint32_t bitPosition) const {
137 return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
140 /// This is used by the constructors that take string arguments.
141 /// @brief Converts a char array into an APInt
142 void fromString(uint32_t numBits, const char *StrStart, uint32_t slen,
145 /// This is used by the toString method to divide by the radix. It simply
146 /// provides a more convenient form of divide for internal use since KnuthDiv
147 /// has specific constraints on its inputs. If those constraints are not met
148 /// then it provides a simpler form of divide.
149 /// @brief An internal division function for dividing APInts.
150 static void divide(const APInt LHS, uint32_t lhsWords,
151 const APInt &RHS, uint32_t rhsWords,
152 APInt *Quotient, APInt *Remainder);
155 /// @brief debug method
160 /// @brief Create a new APInt of numBits width, initialized as val.
161 APInt(uint32_t numBits, uint64_t val);
163 /// Note that numWords can be smaller or larger than the corresponding bit
164 /// width but any extraneous bits will be dropped.
165 /// @brief Create a new APInt of numBits width, initialized as bigVal[].
166 APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[]);
168 /// @brief Create a new APInt by translating the string represented
170 APInt(uint32_t numBits, const std::string& Val, uint8_t radix);
172 /// @brief Create a new APInt by translating the char array represented
174 APInt(uint32_t numBits, const char StrStart[], uint32_t slen, uint8_t radix);
176 /// @brief Copy Constructor.
177 APInt(const APInt& API);
179 /// @brief Destructor.
182 /// @brief Copy assignment operator.
183 APInt& operator=(const APInt& RHS);
185 /// Assigns an integer value to the APInt.
186 /// @brief Assignment operator.
187 APInt& operator=(uint64_t RHS);
189 /// Increments the APInt by one.
190 /// @brief Postfix increment operator.
191 inline const APInt operator++(int) {
197 /// Increments the APInt by one.
198 /// @brief Prefix increment operator.
201 /// Decrements the APInt by one.
202 /// @brief Postfix decrement operator.
203 inline const APInt operator--(int) {
209 /// Decrements the APInt by one.
210 /// @brief Prefix decrement operator.
213 /// Performs bitwise AND operation on this APInt and the given APInt& RHS,
214 /// assigns the result to this APInt.
215 /// @brief Bitwise AND assignment operator.
216 APInt& operator&=(const APInt& RHS);
218 /// Performs bitwise OR operation on this APInt and the given APInt& RHS,
219 /// assigns the result to this APInt.
220 /// @brief Bitwise OR assignment operator.
221 APInt& operator|=(const APInt& RHS);
223 /// Performs bitwise XOR operation on this APInt and the given APInt& RHS,
224 /// assigns the result to this APInt.
225 /// @brief Bitwise XOR assignment operator.
226 APInt& operator^=(const APInt& RHS);
228 /// Performs a bitwise complement operation on this APInt.
229 /// @brief Bitwise complement operator.
230 APInt operator~() const;
232 /// Multiplies this APInt by the given APInt& RHS and
233 /// assigns the result to this APInt.
234 /// @brief Multiplication assignment operator.
235 APInt& operator*=(const APInt& RHS);
237 /// Adds this APInt by the given APInt& RHS and
238 /// assigns the result to this APInt.
239 /// @brief Addition assignment operator.
240 APInt& operator+=(const APInt& RHS);
242 /// Subtracts this APInt by the given APInt &RHS and
243 /// assigns the result to this APInt.
244 /// @brief Subtraction assignment operator.
245 APInt& operator-=(const APInt& RHS);
247 /// Performs bitwise AND operation on this APInt and
248 /// the given APInt& RHS.
249 /// @brief Bitwise AND operator.
250 APInt operator&(const APInt& RHS) const;
252 /// Performs bitwise OR operation on this APInt and the given APInt& RHS.
253 /// @brief Bitwise OR operator.
254 APInt operator|(const APInt& RHS) const;
256 /// Performs bitwise XOR operation on this APInt and the given APInt& RHS.
257 /// @brief Bitwise XOR operator.
258 APInt operator^(const APInt& RHS) const;
260 /// Performs logical negation operation on this APInt.
261 /// @brief Logical negation operator.
262 bool operator !() const;
264 /// Multiplies this APInt by the given APInt& RHS.
265 /// @brief Multiplication operator.
266 APInt operator*(const APInt& RHS) const;
268 /// Adds this APInt by the given APInt& RHS.
269 /// @brief Addition operator.
270 APInt operator+(const APInt& RHS) const;
271 APInt operator+(uint64_t RHS) const {
272 return (*this) + APInt(BitWidth, RHS);
276 /// Subtracts this APInt by the given APInt& RHS
277 /// @brief Subtraction operator.
278 APInt operator-(const APInt& RHS) const;
279 APInt operator-(uint64_t RHS) const {
280 return (*this) - APInt(BitWidth, RHS);
283 /// @brief Unary negation operator
284 inline APInt operator-() const {
285 return APInt(BitWidth, 0) - (*this);
288 /// @brief Array-indexing support.
289 bool operator[](uint32_t bitPosition) const;
291 /// Compare this APInt with the given APInt& RHS
292 /// for the validity of the equality relationship.
293 /// @brief Equality operator.
294 bool operator==(const APInt& RHS) const;
296 /// Compare this APInt with the given uint64_t value
297 /// for the validity of the equality relationship.
298 /// @brief Equality operator.
299 bool operator==(uint64_t Val) const;
301 /// Compare this APInt with the given APInt& RHS
302 /// for the validity of the inequality relationship.
303 /// @brief Inequality operator.
304 inline bool operator!=(const APInt& RHS) const {
305 return !((*this) == RHS);
308 /// Compare this APInt with the given uint64_t value
309 /// for the validity of the inequality relationship.
310 /// @brief Inequality operator.
311 inline bool operator!=(uint64_t Val) const {
312 return !((*this) == Val);
315 /// @brief Equality comparison
316 bool eq(const APInt &RHS) const {
317 return (*this) == RHS;
320 /// @brief Inequality comparison
321 bool ne(const APInt &RHS) const {
322 return !((*this) == RHS);
325 /// @brief Unsigned less than comparison
326 bool ult(const APInt& RHS) const;
328 /// @brief Signed less than comparison
329 bool slt(const APInt& RHS) const;
331 /// @brief Unsigned less or equal comparison
332 bool ule(const APInt& RHS) const {
333 return ult(RHS) || eq(RHS);
336 /// @brief Signed less or equal comparison
337 bool sle(const APInt& RHS) const {
338 return slt(RHS) || eq(RHS);
341 /// @brief Unsigned greather than comparison
342 bool ugt(const APInt& RHS) const {
343 return !ult(RHS) && !eq(RHS);
346 /// @brief Signed greather than comparison
347 bool sgt(const APInt& RHS) const {
348 return !slt(RHS) && !eq(RHS);
351 /// @brief Unsigned greater or equal comparison
352 bool uge(const APInt& RHS) const {
356 /// @brief Signed greather or equal comparison
357 bool sge(const APInt& RHS) const {
361 /// This just tests the high bit of this APInt to determine if it is negative.
362 /// @returns true if this APInt is negative, false otherwise
363 /// @brief Determine sign of this APInt.
364 bool isNegative() const {
365 return (*this)[BitWidth - 1];
368 /// This just tests the high bit of the APInt to determine if the value is
370 /// @brief Determine if this APInt Value is positive.
371 bool isPositive() const {
372 return !isNegative();
375 /// Arithmetic right-shift this APInt by shiftAmt.
376 /// @brief Arithmetic right-shift function.
377 APInt ashr(uint32_t shiftAmt) const;
379 /// Logical right-shift this APInt by shiftAmt.
380 /// @brief Logical right-shift function.
381 APInt lshr(uint32_t shiftAmt) const;
383 /// Left-shift this APInt by shiftAmt.
384 /// @brief Left-shift function.
385 APInt shl(uint32_t shiftAmt) const;
387 /// Signed divide this APInt by APInt RHS.
388 /// @brief Signed division function for APInt.
389 inline APInt sdiv(const APInt& RHS) const {
390 bool isNegativeLHS = isNegative();
391 bool isNegativeRHS = RHS.isNegative();
392 APInt Result = APIntOps::udiv(
393 isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
394 return isNegativeLHS != isNegativeRHS ? -Result : Result;
397 /// Unsigned divide this APInt by APInt RHS.
398 /// @brief Unsigned division function for APInt.
399 APInt udiv(const APInt& RHS) const;
401 /// Signed remainder operation on APInt.
402 /// @brief Function for signed remainder operation.
403 inline APInt srem(const APInt& RHS) const {
404 bool isNegativeLHS = isNegative();
405 bool isNegativeRHS = RHS.isNegative();
406 APInt Result = APIntOps::urem(
407 isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
408 return isNegativeLHS ? -Result : Result;
411 /// Unsigned remainder operation on APInt.
412 /// @brief Function for unsigned remainder operation.
413 APInt urem(const APInt& RHS) const;
415 /// Truncate the APInt to a specified width. It is an error to specify a width
416 /// that is greater than or equal to the current width.
417 /// @brief Truncate to new width.
418 APInt &trunc(uint32_t width);
420 /// This operation sign extends the APInt to a new width. If the high order
421 /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
422 /// It is an error to specify a width that is less than or equal to the
424 /// @brief Sign extend to a new width.
425 APInt &sext(uint32_t width);
427 /// This operation zero extends the APInt to a new width. Thie high order bits
428 /// are filled with 0 bits. It is an error to specify a width that is less
429 /// than or equal to the current width.
430 /// @brief Zero extend to a new width.
431 APInt &zext(uint32_t width);
433 /// Make this APInt have the bit width given by \p width. The value is sign
434 /// extended, truncated, or left alone to make it that width.
435 /// @brief Sign extend or truncate to width
436 APInt &sextOrTrunc(uint32_t width);
438 /// Make this APInt have the bit width given by \p width. The value is zero
439 /// extended, truncated, or left alone to make it that width.
440 /// @brief Zero extend or truncate to width
441 APInt &zextOrTrunc(uint32_t width);
443 /// @brief Set every bit to 1.
446 /// Set the given bit to 1 whose position is given as "bitPosition".
447 /// @brief Set a given bit to 1.
448 APInt& set(uint32_t bitPosition);
450 /// @brief Set every bit to 0.
453 /// Set the given bit to 0 whose position is given as "bitPosition".
454 /// @brief Set a given bit to 0.
455 APInt& clear(uint32_t bitPosition);
457 /// @brief Toggle every bit to its opposite value.
460 /// Toggle a given bit to its opposite value whose position is given
461 /// as "bitPosition".
462 /// @brief Toggles a given bit to its opposite value.
463 APInt& flip(uint32_t bitPosition);
465 /// This function returns the number of active bits which is defined as the
466 /// bit width minus the number of leading zeros. This is used in several
467 /// computations to see how "wide" the value is.
468 /// @brief Compute the number of active bits in the value
469 inline uint32_t getActiveBits() const {
470 return BitWidth - countLeadingZeros();
473 /// This function returns the number of active words in the value of this
474 /// APInt. This is used in conjunction with getActiveData to extract the raw
475 /// value of the APInt.
476 inline uint32_t getActiveWords() const {
477 return whichWord(getActiveBits()-1) + 1;
480 /// This function returns a pointer to the internal storage of the APInt.
481 /// This is useful for writing out the APInt in binary form without any
483 inline const uint64_t* getRawData() const {
489 /// Computes the minimum bit width for this APInt while considering it to be
490 /// a signed (and probably negative) value. If the value is not negative,
491 /// this function returns the same value as getActiveBits(). Otherwise, it
492 /// returns the smallest bit width that will retain the negative value. For
493 /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
494 /// for -1, this function will always return 1.
495 /// @brief Get the minimum bit size for this signed APInt
496 inline uint32_t getMinSignedBits() const {
498 return BitWidth - countLeadingOnes() + 1;
499 return getActiveBits();
502 /// This method attempts to return the value of this APInt as a zero extended
503 /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
504 /// uint64_t. Otherwise an assertion will result.
505 /// @brief Get zero extended value
506 inline uint64_t getZExtValue() const {
509 assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
513 /// This method attempts to return the value of this APInt as a sign extended
514 /// int64_t. The bit width must be <= 64 or the value must fit within an
515 /// int64_t. Otherwise an assertion will result.
516 /// @brief Get sign extended value
517 inline int64_t getSExtValue() const {
519 return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >>
520 (APINT_BITS_PER_WORD - BitWidth);
521 assert(getActiveBits() <= 64 && "Too many bits for int64_t");
522 return int64_t(pVal[0]);
525 /// @brief Gets maximum unsigned value of APInt for specific bit width.
526 static APInt getMaxValue(uint32_t numBits) {
527 return APInt(numBits, 0).set();
530 /// @brief Gets maximum signed value of APInt for a specific bit width.
531 static APInt getSignedMaxValue(uint32_t numBits) {
532 return APInt(numBits, 0).set().clear(numBits - 1);
535 /// @brief Gets minimum unsigned value of APInt for a specific bit width.
536 static APInt getMinValue(uint32_t numBits) {
537 return APInt(numBits, 0);
540 /// @brief Gets minimum signed value of APInt for a specific bit width.
541 static APInt getSignedMinValue(uint32_t numBits) {
542 return APInt(numBits, 0).set(numBits - 1);
545 /// @returns the all-ones value for an APInt of the specified bit-width.
546 /// @brief Get the all-ones value.
547 static APInt getAllOnesValue(uint32_t numBits) {
548 return APInt(numBits, 0).set();
551 /// @returns the '0' value for an APInt of the specified bit-width.
552 /// @brief Get the '0' value.
553 static APInt getNullValue(uint32_t numBits) {
554 return APInt(numBits, 0);
557 /// The hash value is computed as the sum of the words and the bit width.
558 /// @returns A hash value computed from the sum of the APInt words.
559 /// @brief Get a hash value based on this APInt
560 uint64_t getHashValue() const;
562 /// This converts the APInt to a boolean valy as a test against zero.
563 /// @brief Boolean conversion function.
564 inline bool getBoolValue() const {
565 return countLeadingZeros() != BitWidth;
568 /// This checks to see if the value has all bits of the APInt are set or not.
569 /// @brief Determine if all bits are set
570 inline bool isAllOnesValue() const {
571 return countPopulation() == BitWidth;
574 /// This checks to see if the value of this APInt is the maximum unsigned
575 /// value for the APInt's bit width.
576 /// @brief Determine if this is the largest unsigned value.
577 bool isMaxValue() const {
578 return countPopulation() == BitWidth;
581 /// This checks to see if the value of this APInt is the maximum signed
582 /// value for the APInt's bit width.
583 /// @brief Determine if this is the largest signed value.
584 bool isMaxSignedValue() const {
585 return BitWidth == 1 ? VAL == 0 :
586 !isNegative() && countPopulation() == BitWidth - 1;
589 /// This checks to see if the value of this APInt is the minimum signed
590 /// value for the APInt's bit width.
591 /// @brief Determine if this is the smallest unsigned value.
592 bool isMinValue() const {
593 return countPopulation() == 0;
596 /// This checks to see if the value of this APInt is the minimum signed
597 /// value for the APInt's bit width.
598 /// @brief Determine if this is the smallest signed value.
599 bool isMinSignedValue() const {
600 return BitWidth == 1 ? VAL == 1 :
601 isNegative() && countPopulation() == 1;
604 /// This is used internally to convert an APInt to a string.
605 /// @brief Converts an APInt to a std::string
606 std::string toString(uint8_t radix, bool wantSigned) const;
608 /// Considers the APInt to be unsigned and converts it into a string in the
609 /// radix given. The radix can be 2, 8, 10 or 16.
610 /// @returns a character interpretation of the APInt
611 /// @brief Convert unsigned APInt to string representation.
612 inline std::string toString(uint8_t radix = 10) const {
613 return toString(radix, false);
616 /// Considers the APInt to be unsigned and converts it into a string in the
617 /// radix given. The radix can be 2, 8, 10 or 16.
618 /// @returns a character interpretation of the APInt
619 /// @brief Convert unsigned APInt to string representation.
620 inline std::string toStringSigned(uint8_t radix = 10) const {
621 return toString(radix, true);
624 /// Get an APInt with the same BitWidth as this APInt, just zero mask
625 /// the low bits and right shift to the least significant bit.
626 /// @returns the high "numBits" bits of this APInt.
627 APInt getHiBits(uint32_t numBits) const;
629 /// Get an APInt with the same BitWidth as this APInt, just zero mask
631 /// @returns the low "numBits" bits of this APInt.
632 APInt getLoBits(uint32_t numBits) const;
634 /// @returns true if the argument APInt value is a power of two > 0.
635 bool isPowerOf2() const;
637 /// countLeadingZeros - This function is an APInt version of the
638 /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
639 /// of zeros from the most significant bit to the first one bit.
640 /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
641 /// @returns the number of zeros from the most significant bit to the first
643 /// @brief Count the number of leading one bits.
644 uint32_t countLeadingZeros() const;
646 /// countLeadingOnes - This function counts the number of contiguous 1 bits
647 /// in the high order bits. The count stops when the first 0 bit is reached.
648 /// @returns 0 if the high order bit is not set
649 /// @returns the number of 1 bits from the most significant to the least
650 /// @brief Count the number of leading one bits.
651 uint32_t countLeadingOnes() const;
653 /// countTrailingZeros - This function is an APInt version of the
654 /// countTrailingZoers_{32,64} functions in MathExtras.h. It counts
655 /// the number of zeros from the least significant bit to the first one bit.
656 /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
657 /// @returns the number of zeros from the least significant bit to the first
659 /// @brief Count the number of trailing zero bits.
660 uint32_t countTrailingZeros() const;
662 /// countPopulation - This function is an APInt version of the
663 /// countPopulation_{32,64} functions in MathExtras.h. It counts the number
664 /// of 1 bits in the APInt value.
665 /// @returns 0 if the value is zero.
666 /// @returns the number of set bits.
667 /// @brief Count the number of bits set.
668 uint32_t countPopulation() const;
670 /// @returns the total number of bits.
671 inline uint32_t getBitWidth() const {
675 /// @brief Check if this APInt has a N-bits integer value.
676 inline bool isIntN(uint32_t N) const {
677 assert(N && "N == 0 ???");
678 if (isSingleWord()) {
679 return VAL == (VAL & (~0ULL >> (64 - N)));
681 APInt Tmp(N, getNumWords(), pVal);
682 return Tmp == (*this);
686 /// @returns a byte-swapped representation of this APInt Value.
687 APInt byteSwap() const;
689 /// @returns the floor log base 2 of this APInt.
690 inline uint32_t logBase2() const {
691 return getNumWords() * APINT_BITS_PER_WORD - 1 - countLeadingZeros();
694 /// @brief Converts this APInt to a double value.
695 double roundToDouble(bool isSigned) const;
697 /// @brief Converts this unsigned APInt to a double value.
698 double roundToDouble() const {
699 return roundToDouble(false);
702 /// @brief Converts this signed APInt to a double value.
703 double signedRoundToDouble() const {
704 return roundToDouble(true);
707 /// The conversion does not do a translation from integer to double, it just
708 /// re-interprets the bits as a double. Note that it is valid to do this on
709 /// any bit width. Exactly 64 bits will be translated.
710 /// @brief Converts APInt bits to a double
711 double bitsToDouble() const {
716 T.I = (isSingleWord() ? VAL : pVal[0]);
720 /// The conversion does not do a translation from integer to float, it just
721 /// re-interprets the bits as a float. Note that it is valid to do this on
722 /// any bit width. Exactly 32 bits will be translated.
723 /// @brief Converts APInt bits to a double
724 float bitsToFloat() const {
729 T.I = uint32_t((isSingleWord() ? VAL : pVal[0]));
733 /// The conversion does not do a translation from double to integer, it just
734 /// re-interprets the bits of the double. Note that it is valid to do this on
735 /// any bit width but bits from V may get truncated.
736 /// @brief Converts a double to APInt bits.
737 APInt& doubleToBits(double V) {
747 return clearUnusedBits();
750 /// The conversion does not do a translation from float to integer, it just
751 /// re-interprets the bits of the float. Note that it is valid to do this on
752 /// any bit width but bits from V may get truncated.
753 /// @brief Converts a float to APInt bits.
754 APInt& floatToBits(float V) {
764 return clearUnusedBits();
767 /// @brief Compute the square root
770 /// If *this is < 0 then return -(*this), otherwise *this;
771 /// @brief Get the absolute value;
779 inline bool operator==(uint64_t V1, const APInt& V2) {
783 inline bool operator!=(uint64_t V1, const APInt& V2) {
789 /// @brief Determine the smaller of two APInts considered to be signed.
790 inline APInt smin(const APInt &A, const APInt &B) {
791 return A.slt(B) ? A : B;
794 /// @brief Determine the larger of two APInts considered to be signed.
795 inline APInt smax(const APInt &A, const APInt &B) {
796 return A.sgt(B) ? A : B;
799 /// @brief Determine the smaller of two APInts considered to be signed.
800 inline APInt umin(const APInt &A, const APInt &B) {
801 return A.ult(B) ? A : B;
804 /// @brief Determine the larger of two APInts considered to be unsigned.
805 inline APInt umax(const APInt &A, const APInt &B) {
806 return A.ugt(B) ? A : B;
809 /// @brief Check if the specified APInt has a N-bits integer value.
810 inline bool isIntN(uint32_t N, const APInt& APIVal) {
811 return APIVal.isIntN(N);
814 /// @returns true if the argument APInt value is a sequence of ones
815 /// starting at the least significant bit with the remainder zero.
816 inline const bool isMask(uint32_t numBits, const APInt& APIVal) {
817 return APIVal.getBoolValue() && ((APIVal + APInt(numBits,1)) & APIVal) == 0;
820 /// @returns true if the argument APInt value contains a sequence of ones
821 /// with the remainder zero.
822 inline const bool isShiftedMask(uint32_t numBits, const APInt& APIVal) {
823 return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
826 /// @returns a byte-swapped representation of the specified APInt Value.
827 inline APInt byteSwap(const APInt& APIVal) {
828 return APIVal.byteSwap();
831 /// @returns the floor log base 2 of the specified APInt value.
832 inline uint32_t logBase2(const APInt& APIVal) {
833 return APIVal.logBase2();
836 /// GreatestCommonDivisor - This function returns the greatest common
837 /// divisor of the two APInt values using Enclid's algorithm.
838 /// @returns the greatest common divisor of Val1 and Val2
839 /// @brief Compute GCD of two APInt values.
840 APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
842 /// Treats the APInt as an unsigned value for conversion purposes.
843 /// @brief Converts the given APInt to a double value.
844 inline double RoundAPIntToDouble(const APInt& APIVal) {
845 return APIVal.roundToDouble();
848 /// Treats the APInt as a signed value for conversion purposes.
849 /// @brief Converts the given APInt to a double value.
850 inline double RoundSignedAPIntToDouble(const APInt& APIVal) {
851 return APIVal.signedRoundToDouble();
854 /// @brief Converts the given APInt to a float vlalue.
855 inline float RoundAPIntToFloat(const APInt& APIVal) {
856 return float(RoundAPIntToDouble(APIVal));
859 /// RoundDoubleToAPInt - This function convert a double value to an APInt value.
860 /// @brief Converts the given double value into a APInt.
861 APInt RoundDoubleToAPInt(double Double, uint32_t width = 64);
863 /// RoundFloatToAPInt - Converts a float value into an APInt value.
864 /// @brief Converts a float value into a APInt.
865 inline APInt RoundFloatToAPInt(float Float) {
866 return RoundDoubleToAPInt(double(Float));
869 /// Arithmetic right-shift the APInt by shiftAmt.
870 /// @brief Arithmetic right-shift function.
871 inline APInt ashr(const APInt& LHS, uint32_t shiftAmt) {
872 return LHS.ashr(shiftAmt);
875 /// Logical right-shift the APInt by shiftAmt.
876 /// @brief Logical right-shift function.
877 inline APInt lshr(const APInt& LHS, uint32_t shiftAmt) {
878 return LHS.lshr(shiftAmt);
881 /// Left-shift the APInt by shiftAmt.
882 /// @brief Left-shift function.
883 inline APInt shl(const APInt& LHS, uint32_t shiftAmt) {
884 return LHS.shl(shiftAmt);
887 /// Signed divide APInt LHS by APInt RHS.
888 /// @brief Signed division function for APInt.
889 inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
890 return LHS.sdiv(RHS);
893 /// Unsigned divide APInt LHS by APInt RHS.
894 /// @brief Unsigned division function for APInt.
895 inline APInt udiv(const APInt& LHS, const APInt& RHS) {
896 return LHS.udiv(RHS);
899 /// Signed remainder operation on APInt.
900 /// @brief Function for signed remainder operation.
901 inline APInt srem(const APInt& LHS, const APInt& RHS) {
902 return LHS.srem(RHS);
905 /// Unsigned remainder operation on APInt.
906 /// @brief Function for unsigned remainder operation.
907 inline APInt urem(const APInt& LHS, const APInt& RHS) {
908 return LHS.urem(RHS);
911 /// Performs multiplication on APInt values.
912 /// @brief Function for multiplication operation.
913 inline APInt mul(const APInt& LHS, const APInt& RHS) {
917 /// Performs addition on APInt values.
918 /// @brief Function for addition operation.
919 inline APInt add(const APInt& LHS, const APInt& RHS) {
923 /// Performs subtraction on APInt values.
924 /// @brief Function for subtraction operation.
925 inline APInt sub(const APInt& LHS, const APInt& RHS) {
929 /// Performs bitwise AND operation on APInt LHS and
931 /// @brief Bitwise AND function for APInt.
932 inline APInt And(const APInt& LHS, const APInt& RHS) {
936 /// Performs bitwise OR operation on APInt LHS and APInt RHS.
937 /// @brief Bitwise OR function for APInt.
938 inline APInt Or(const APInt& LHS, const APInt& RHS) {
942 /// Performs bitwise XOR operation on APInt.
943 /// @brief Bitwise XOR function for APInt.
944 inline APInt Xor(const APInt& LHS, const APInt& RHS) {
948 /// Performs a bitwise complement operation on APInt.
949 /// @brief Bitwise complement function.
950 inline APInt Not(const APInt& APIVal) {
954 } // End of APIntOps namespace
956 } // End of llvm namespace