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 /// @returns true if the number of bits <= 64, false otherwise.
83 /// @brief Determine if this APInt just has one word to store value.
84 inline bool isSingleWord() const {
85 return BitWidth <= APINT_BITS_PER_WORD;
88 /// @returns the word position for the specified bit position.
89 static inline uint32_t whichWord(uint32_t bitPosition) {
90 return bitPosition / APINT_BITS_PER_WORD;
93 /// @returns the bit position in a word for the specified bit position
95 static inline uint32_t whichBit(uint32_t bitPosition) {
96 return bitPosition % APINT_BITS_PER_WORD;
99 /// @returns a uint64_t type integer with just bit position at
100 /// "whichBit(bitPosition)" setting, others zero.
101 static inline uint64_t maskBit(uint32_t bitPosition) {
102 return 1ULL << whichBit(bitPosition);
105 /// This method is used internally to clear the to "N" bits that are not used
106 /// by the APInt. This is needed after the most significant word is assigned
107 /// a value to ensure that those bits are zero'd out.
108 /// @brief Clear high order bits
109 inline APInt& clearUnusedBits() {
110 // Compute how many bits are used in the final word
111 uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
113 // If all bits are used, we want to leave the value alone. This also
114 // avoids the undefined behavior of >> when the shfit is the same size as
115 // the word size (64).
118 // Mask out the hight bits.
119 uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
123 pVal[getNumWords() - 1] &= mask;
127 /// @returns the corresponding word for the specified bit position.
128 /// @brief Get the word corresponding to a bit position
129 inline uint64_t getWord(uint32_t bitPosition) const {
130 return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
133 /// This is used by the constructors that take string arguments.
134 /// @brief Converts a char array into an APInt
135 void fromString(uint32_t numBits, const char *StrStart, uint32_t slen,
138 /// This is used by the toString method to divide by the radix. It simply
139 /// provides a more convenient form of divide for internal use since KnuthDiv
140 /// has specific constraints on its inputs. If those constraints are not met
141 /// then it provides a simpler form of divide.
142 /// @brief An internal division function for dividing APInts.
143 static void divide(const APInt LHS, uint32_t lhsWords,
144 const APInt &RHS, uint32_t rhsWords,
145 APInt *Quotient, APInt *Remainder);
148 /// @brief debug method
153 /// @brief Create a new APInt of numBits width, initialized as val.
154 APInt(uint32_t numBits, uint64_t val);
156 /// Note that numWords can be smaller or larger than the corresponding bit
157 /// width but any extraneous bits will be dropped.
158 /// @brief Create a new APInt of numBits width, initialized as bigVal[].
159 APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[]);
161 /// @brief Create a new APInt by translating the string represented
163 APInt(uint32_t numBits, const std::string& Val, uint8_t radix);
165 /// @brief Create a new APInt by translating the char array represented
167 APInt(uint32_t numBits, const char StrStart[], uint32_t slen, uint8_t radix);
169 /// @brief Copy Constructor.
170 APInt(const APInt& API);
172 /// @brief Destructor.
175 /// @brief Copy assignment operator.
176 APInt& operator=(const APInt& RHS);
178 /// Assigns an integer value to the APInt.
179 /// @brief Assignment operator.
180 APInt& operator=(uint64_t RHS);
182 /// Increments the APInt by one.
183 /// @brief Postfix increment operator.
184 inline const APInt operator++(int) {
190 /// Increments the APInt by one.
191 /// @brief Prefix increment operator.
194 /// Decrements the APInt by one.
195 /// @brief Postfix decrement operator.
196 inline const APInt operator--(int) {
202 /// Decrements the APInt by one.
203 /// @brief Prefix decrement operator.
206 /// Performs bitwise AND operation on this APInt and the given APInt& RHS,
207 /// assigns the result to this APInt.
208 /// @brief Bitwise AND assignment operator.
209 APInt& operator&=(const APInt& RHS);
211 /// Performs bitwise OR operation on this APInt and the given APInt& RHS,
212 /// assigns the result to this APInt.
213 /// @brief Bitwise OR assignment operator.
214 APInt& operator|=(const APInt& RHS);
216 /// Performs bitwise XOR operation on this APInt and the given APInt& RHS,
217 /// assigns the result to this APInt.
218 /// @brief Bitwise XOR assignment operator.
219 APInt& operator^=(const APInt& RHS);
221 /// Performs a bitwise complement operation on this APInt.
222 /// @brief Bitwise complement operator.
223 APInt operator~() const;
225 /// Multiplies this APInt by the given APInt& RHS and
226 /// assigns the result to this APInt.
227 /// @brief Multiplication assignment operator.
228 APInt& operator*=(const APInt& RHS);
230 /// Adds this APInt by the given APInt& RHS and
231 /// assigns the result to this APInt.
232 /// @brief Addition assignment operator.
233 APInt& operator+=(const APInt& RHS);
235 /// Subtracts this APInt by the given APInt &RHS and
236 /// assigns the result to this APInt.
237 /// @brief Subtraction assignment operator.
238 APInt& operator-=(const APInt& RHS);
240 /// Performs bitwise AND operation on this APInt and
241 /// the given APInt& RHS.
242 /// @brief Bitwise AND operator.
243 APInt operator&(const APInt& RHS) const;
244 APInt And(const APInt& RHS) const {
245 return this->operator&(RHS);
248 /// Performs bitwise OR operation on this APInt and the given APInt& RHS.
249 /// @brief Bitwise OR operator.
250 APInt operator|(const APInt& RHS) const;
251 APInt Or(const APInt& RHS) const {
252 return this->operator|(RHS);
255 /// Performs bitwise XOR operation on this APInt and the given APInt& RHS.
256 /// @brief Bitwise XOR operator.
257 APInt operator^(const APInt& RHS) const;
258 APInt Xor(const APInt& RHS) const {
259 return this->operator^(RHS);
262 /// Performs logical negation operation on this APInt.
263 /// @brief Logical negation operator.
264 bool operator !() const;
266 /// Multiplies this APInt by the given APInt& RHS.
267 /// @brief Multiplication operator.
268 APInt operator*(const APInt& RHS) const;
270 /// Adds this APInt by the given APInt& RHS.
271 /// @brief Addition operator.
272 APInt operator+(const APInt& RHS) const;
273 APInt operator+(uint64_t RHS) const {
274 return (*this) + APInt(BitWidth, RHS);
278 /// Subtracts this APInt by the given APInt& RHS
279 /// @brief Subtraction operator.
280 APInt operator-(const APInt& RHS) const;
281 APInt operator-(uint64_t RHS) const {
282 return (*this) - APInt(BitWidth, RHS);
285 /// @brief Unary negation operator
286 inline APInt operator-() const {
287 return APInt(BitWidth, 0) - (*this);
290 /// @brief Array-indexing support.
291 bool operator[](uint32_t bitPosition) const;
293 /// Compare this APInt with the given APInt& RHS
294 /// for the validity of the equality relationship.
295 /// @brief Equality operator.
296 bool operator==(const APInt& RHS) const;
298 /// Compare this APInt with the given uint64_t value
299 /// for the validity of the equality relationship.
300 /// @brief Equality operator.
301 bool operator==(uint64_t Val) const;
303 /// Compare this APInt with the given APInt& RHS
304 /// for the validity of the inequality relationship.
305 /// @brief Inequality operator.
306 inline bool operator!=(const APInt& RHS) const {
307 return !((*this) == RHS);
310 /// Compare this APInt with the given uint64_t value
311 /// for the validity of the inequality relationship.
312 /// @brief Inequality operator.
313 inline bool operator!=(uint64_t Val) const {
314 return !((*this) == Val);
317 /// @brief Equality comparison
318 bool eq(const APInt &RHS) const {
319 return (*this) == RHS;
322 /// @brief Inequality comparison
323 bool ne(const APInt &RHS) const {
324 return !((*this) == RHS);
327 /// @brief Unsigned less than comparison
328 bool ult(const APInt& RHS) const;
330 /// @brief Signed less than comparison
331 bool slt(const APInt& RHS) const;
333 /// @brief Unsigned less or equal comparison
334 bool ule(const APInt& RHS) const {
335 return ult(RHS) || eq(RHS);
338 /// @brief Signed less or equal comparison
339 bool sle(const APInt& RHS) const {
340 return slt(RHS) || eq(RHS);
343 /// @brief Unsigned greather than comparison
344 bool ugt(const APInt& RHS) const {
345 return !ult(RHS) && !eq(RHS);
348 /// @brief Signed greather than comparison
349 bool sgt(const APInt& RHS) const {
350 return !slt(RHS) && !eq(RHS);
353 /// @brief Unsigned greater or equal comparison
354 bool uge(const APInt& RHS) const {
358 /// @brief Signed greather or equal comparison
359 bool sge(const APInt& RHS) const {
363 /// This just tests the high bit of this APInt to determine if it is negative.
364 /// @returns true if this APInt is negative, false otherwise
365 /// @brief Determine sign of this APInt.
366 bool isNegative() const {
367 return (*this)[BitWidth - 1];
370 /// This just tests the high bit of the APInt to determine if the value is
372 /// @brief Determine if this APInt Value is positive.
373 bool isPositive() const {
374 return !isNegative();
377 /// Arithmetic right-shift this APInt by shiftAmt.
378 /// @brief Arithmetic right-shift function.
379 APInt ashr(uint32_t shiftAmt) const;
381 /// Logical right-shift this APInt by shiftAmt.
382 /// @brief Logical right-shift function.
383 APInt lshr(uint32_t shiftAmt) const;
385 /// Left-shift this APInt by shiftAmt.
386 /// @brief Left-shift function.
387 APInt shl(uint32_t shiftAmt) const;
389 /// Signed divide this APInt by APInt RHS.
390 /// @brief Signed division function for APInt.
391 inline APInt sdiv(const APInt& RHS) const {
392 bool isNegativeLHS = isNegative();
393 bool isNegativeRHS = RHS.isNegative();
394 APInt Result = APIntOps::udiv(
395 isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
396 return isNegativeLHS != isNegativeRHS ? -Result : Result;
399 /// Unsigned divide this APInt by APInt RHS.
400 /// @brief Unsigned division function for APInt.
401 APInt udiv(const APInt& RHS) const;
403 /// Signed remainder operation on APInt.
404 /// @brief Function for signed remainder operation.
405 inline APInt srem(const APInt& RHS) const {
406 bool isNegativeLHS = isNegative();
407 bool isNegativeRHS = RHS.isNegative();
408 APInt Result = APIntOps::urem(
409 isNegativeLHS ? -(*this) : (*this), isNegativeRHS ? -RHS : RHS);
410 return isNegativeLHS ? -Result : Result;
413 /// Unsigned remainder operation on APInt.
414 /// @brief Function for unsigned remainder operation.
415 APInt urem(const APInt& RHS) const;
417 /// Truncate the APInt to a specified width. It is an error to specify a width
418 /// that is greater than or equal to the current width.
419 /// @brief Truncate to new width.
420 APInt &trunc(uint32_t width);
422 /// This operation sign extends the APInt to a new width. If the high order
423 /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
424 /// It is an error to specify a width that is less than or equal to the
426 /// @brief Sign extend to a new width.
427 APInt &sext(uint32_t width);
429 /// This operation zero extends the APInt to a new width. Thie high order bits
430 /// are filled with 0 bits. It is an error to specify a width that is less
431 /// than or equal to the current width.
432 /// @brief Zero extend to a new width.
433 APInt &zext(uint32_t width);
435 /// Make this APInt have the bit width given by \p width. The value is sign
436 /// extended, truncated, or left alone to make it that width.
437 /// @brief Sign extend or truncate to width
438 APInt &sextOrTrunc(uint32_t width);
440 /// Make this APInt have the bit width given by \p width. The value is zero
441 /// extended, truncated, or left alone to make it that width.
442 /// @brief Zero extend or truncate to width
443 APInt &zextOrTrunc(uint32_t width);
445 /// @brief Set every bit to 1.
448 /// Set the given bit to 1 whose position is given as "bitPosition".
449 /// @brief Set a given bit to 1.
450 APInt& set(uint32_t bitPosition);
452 /// @brief Set every bit to 0.
455 /// Set the given bit to 0 whose position is given as "bitPosition".
456 /// @brief Set a given bit to 0.
457 APInt& clear(uint32_t bitPosition);
459 /// @brief Toggle every bit to its opposite value.
462 /// Toggle a given bit to its opposite value whose position is given
463 /// as "bitPosition".
464 /// @brief Toggles a given bit to its opposite value.
465 APInt& flip(uint32_t bitPosition);
467 /// This function returns the number of active bits which is defined as the
468 /// bit width minus the number of leading zeros. This is used in several
469 /// computations to see how "wide" the value is.
470 /// @brief Compute the number of active bits in the value
471 inline uint32_t getActiveBits() const {
472 return BitWidth - countLeadingZeros();
475 /// This function returns the number of active words in the value of this
476 /// APInt. This is used in conjunction with getActiveData to extract the raw
477 /// value of the APInt.
478 inline uint32_t getActiveWords() const {
479 return whichWord(getActiveBits()-1) + 1;
482 /// Here one word's bitwidth equals to that of uint64_t.
483 /// @returns the number of words to hold the integer value of this APInt.
484 /// @brief Get the number of words.
485 inline uint32_t getNumWords() const {
486 return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
489 /// This function returns a pointer to the internal storage of the APInt.
490 /// This is useful for writing out the APInt in binary form without any
492 inline const uint64_t* getRawData() const {
498 /// Computes the minimum bit width for this APInt while considering it to be
499 /// a signed (and probably negative) value. If the value is not negative,
500 /// this function returns the same value as getActiveBits(). Otherwise, it
501 /// returns the smallest bit width that will retain the negative value. For
502 /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
503 /// for -1, this function will always return 1.
504 /// @brief Get the minimum bit size for this signed APInt
505 inline uint32_t getMinSignedBits() const {
507 return BitWidth - countLeadingOnes() + 1;
508 return getActiveBits();
511 /// This method attempts to return the value of this APInt as a zero extended
512 /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
513 /// uint64_t. Otherwise an assertion will result.
514 /// @brief Get zero extended value
515 inline uint64_t getZExtValue() const {
518 assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
522 /// This method attempts to return the value of this APInt as a sign extended
523 /// int64_t. The bit width must be <= 64 or the value must fit within an
524 /// int64_t. Otherwise an assertion will result.
525 /// @brief Get sign extended value
526 inline int64_t getSExtValue() const {
528 return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >>
529 (APINT_BITS_PER_WORD - BitWidth);
530 assert(getActiveBits() <= 64 && "Too many bits for int64_t");
531 return int64_t(pVal[0]);
534 /// @brief Gets maximum unsigned value of APInt for specific bit width.
535 static APInt getMaxValue(uint32_t numBits) {
536 return APInt(numBits, 0).set();
539 /// @brief Gets maximum signed value of APInt for a specific bit width.
540 static APInt getSignedMaxValue(uint32_t numBits) {
541 return APInt(numBits, 0).set().clear(numBits - 1);
544 /// @brief Gets minimum unsigned value of APInt for a specific bit width.
545 static APInt getMinValue(uint32_t numBits) {
546 return APInt(numBits, 0);
549 /// @brief Gets minimum signed value of APInt for a specific bit width.
550 static APInt getSignedMinValue(uint32_t numBits) {
551 return APInt(numBits, 0).set(numBits - 1);
554 /// @returns the all-ones value for an APInt of the specified bit-width.
555 /// @brief Get the all-ones value.
556 static APInt getAllOnesValue(uint32_t numBits) {
557 return APInt(numBits, 0).set();
560 /// @returns the '0' value for an APInt of the specified bit-width.
561 /// @brief Get the '0' value.
562 static APInt getNullValue(uint32_t numBits) {
563 return APInt(numBits, 0);
566 /// The hash value is computed as the sum of the words and the bit width.
567 /// @returns A hash value computed from the sum of the APInt words.
568 /// @brief Get a hash value based on this APInt
569 uint64_t getHashValue() const;
571 /// This converts the APInt to a boolean valy as a test against zero.
572 /// @brief Boolean conversion function.
573 inline bool getBoolValue() const {
574 return countLeadingZeros() != BitWidth;
577 /// This checks to see if the value has all bits of the APInt are set or not.
578 /// @brief Determine if all bits are set
579 inline bool isAllOnesValue() const {
580 return countPopulation() == BitWidth;
583 /// This checks to see if the value of this APInt is the maximum unsigned
584 /// value for the APInt's bit width.
585 /// @brief Determine if this is the largest unsigned value.
586 bool isMaxValue() const {
587 return countPopulation() == BitWidth;
590 /// This checks to see if the value of this APInt is the maximum signed
591 /// value for the APInt's bit width.
592 /// @brief Determine if this is the largest signed value.
593 bool isMaxSignedValue() const {
594 return BitWidth == 1 ? VAL == 0 :
595 !isNegative() && countPopulation() == BitWidth - 1;
598 /// This checks to see if the value of this APInt is the minimum signed
599 /// value for the APInt's bit width.
600 /// @brief Determine if this is the smallest unsigned value.
601 bool isMinValue() const {
602 return countPopulation() == 0;
605 /// This checks to see if the value of this APInt is the minimum signed
606 /// value for the APInt's bit width.
607 /// @brief Determine if this is the smallest signed value.
608 bool isMinSignedValue() const {
609 return BitWidth == 1 ? VAL == 1 :
610 isNegative() && countPopulation() == 1;
613 /// This is used internally to convert an APInt to a string.
614 /// @brief Converts an APInt to a std::string
615 std::string toString(uint8_t radix, bool wantSigned) const;
617 /// Considers the APInt to be unsigned and converts it into a string in the
618 /// radix given. The radix can be 2, 8, 10 or 16.
619 /// @returns a character interpretation of the APInt
620 /// @brief Convert unsigned APInt to string representation.
621 inline std::string toString(uint8_t radix = 10) const {
622 return toString(radix, false);
625 /// Considers the APInt to be unsigned and converts it into a string in the
626 /// radix given. The radix can be 2, 8, 10 or 16.
627 /// @returns a character interpretation of the APInt
628 /// @brief Convert unsigned APInt to string representation.
629 inline std::string toStringSigned(uint8_t radix = 10) const {
630 return toString(radix, true);
633 /// Get an APInt with the same BitWidth as this APInt, just zero mask
634 /// the low bits and right shift to the least significant bit.
635 /// @returns the high "numBits" bits of this APInt.
636 APInt getHiBits(uint32_t numBits) const;
638 /// Get an APInt with the same BitWidth as this APInt, just zero mask
640 /// @returns the low "numBits" bits of this APInt.
641 APInt getLoBits(uint32_t numBits) const;
643 /// @returns true if the argument APInt value is a power of two > 0.
644 bool isPowerOf2() const;
646 /// countLeadingZeros - This function is an APInt version of the
647 /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
648 /// of zeros from the most significant bit to the first one bit.
649 /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
650 /// @returns the number of zeros from the most significant bit to the first
652 /// @brief Count the number of leading one bits.
653 uint32_t countLeadingZeros() const;
655 /// countLeadingOnes - This function counts the number of contiguous 1 bits
656 /// in the high order bits. The count stops when the first 0 bit is reached.
657 /// @returns 0 if the high order bit is not set
658 /// @returns the number of 1 bits from the most significant to the least
659 /// @brief Count the number of leading one bits.
660 uint32_t countLeadingOnes() const;
662 /// countTrailingZeros - This function is an APInt version of the
663 /// countTrailingZoers_{32,64} functions in MathExtras.h. It counts
664 /// the number of zeros from the least significant bit to the first one bit.
665 /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
666 /// @returns the number of zeros from the least significant bit to the first
668 /// @brief Count the number of trailing zero bits.
669 uint32_t countTrailingZeros() const;
671 /// countPopulation - This function is an APInt version of the
672 /// countPopulation_{32,64} functions in MathExtras.h. It counts the number
673 /// of 1 bits in the APInt value.
674 /// @returns 0 if the value is zero.
675 /// @returns the number of set bits.
676 /// @brief Count the number of bits set.
677 uint32_t countPopulation() const;
679 /// @returns the total number of bits.
680 inline uint32_t getBitWidth() const {
684 /// @brief Check if this APInt has a N-bits integer value.
685 inline bool isIntN(uint32_t N) const {
686 assert(N && "N == 0 ???");
687 if (isSingleWord()) {
688 return VAL == (VAL & (~0ULL >> (64 - N)));
690 APInt Tmp(N, getNumWords(), pVal);
691 return Tmp == (*this);
695 /// @returns a byte-swapped representation of this APInt Value.
696 APInt byteSwap() const;
698 /// @returns the floor log base 2 of this APInt.
699 inline uint32_t logBase2() const {
700 return BitWidth - 1 - countLeadingZeros();
703 /// @brief Converts this APInt to a double value.
704 double roundToDouble(bool isSigned) const;
706 /// @brief Converts this unsigned APInt to a double value.
707 double roundToDouble() const {
708 return roundToDouble(false);
711 /// @brief Converts this signed APInt to a double value.
712 double signedRoundToDouble() const {
713 return roundToDouble(true);
716 /// The conversion does not do a translation from integer to double, it just
717 /// re-interprets the bits as a double. Note that it is valid to do this on
718 /// any bit width. Exactly 64 bits will be translated.
719 /// @brief Converts APInt bits to a double
720 double bitsToDouble() const {
725 T.I = (isSingleWord() ? VAL : pVal[0]);
729 /// The conversion does not do a translation from integer to float, it just
730 /// re-interprets the bits as a float. Note that it is valid to do this on
731 /// any bit width. Exactly 32 bits will be translated.
732 /// @brief Converts APInt bits to a double
733 float bitsToFloat() const {
738 T.I = uint32_t((isSingleWord() ? VAL : pVal[0]));
742 /// The conversion does not do a translation from double to integer, it just
743 /// re-interprets the bits of the double. Note that it is valid to do this on
744 /// any bit width but bits from V may get truncated.
745 /// @brief Converts a double to APInt bits.
746 APInt& doubleToBits(double V) {
756 return clearUnusedBits();
759 /// The conversion does not do a translation from float to integer, it just
760 /// re-interprets the bits of the float. Note that it is valid to do this on
761 /// any bit width but bits from V may get truncated.
762 /// @brief Converts a float to APInt bits.
763 APInt& floatToBits(float V) {
773 return clearUnusedBits();
776 /// @brief Compute the square root
779 /// If *this is < 0 then return -(*this), otherwise *this;
780 /// @brief Get the absolute value;
788 inline bool operator==(uint64_t V1, const APInt& V2) {
792 inline bool operator!=(uint64_t V1, const APInt& V2) {
798 /// @brief Determine the smaller of two APInts considered to be signed.
799 inline APInt smin(const APInt &A, const APInt &B) {
800 return A.slt(B) ? A : B;
803 /// @brief Determine the larger of two APInts considered to be signed.
804 inline APInt smax(const APInt &A, const APInt &B) {
805 return A.sgt(B) ? A : B;
808 /// @brief Determine the smaller of two APInts considered to be signed.
809 inline APInt umin(const APInt &A, const APInt &B) {
810 return A.ult(B) ? A : B;
813 /// @brief Determine the larger of two APInts considered to be unsigned.
814 inline APInt umax(const APInt &A, const APInt &B) {
815 return A.ugt(B) ? A : B;
818 /// @brief Check if the specified APInt has a N-bits integer value.
819 inline bool isIntN(uint32_t N, const APInt& APIVal) {
820 return APIVal.isIntN(N);
823 /// @returns true if the argument APInt value is a sequence of ones
824 /// starting at the least significant bit with the remainder zero.
825 inline const bool isMask(uint32_t numBits, const APInt& APIVal) {
826 return APIVal.getBoolValue() && ((APIVal + APInt(numBits,1)) & APIVal) == 0;
829 /// @returns true if the argument APInt value contains a sequence of ones
830 /// with the remainder zero.
831 inline const bool isShiftedMask(uint32_t numBits, const APInt& APIVal) {
832 return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
835 /// @returns a byte-swapped representation of the specified APInt Value.
836 inline APInt byteSwap(const APInt& APIVal) {
837 return APIVal.byteSwap();
840 /// @returns the floor log base 2 of the specified APInt value.
841 inline uint32_t logBase2(const APInt& APIVal) {
842 return APIVal.logBase2();
845 /// GreatestCommonDivisor - This function returns the greatest common
846 /// divisor of the two APInt values using Enclid's algorithm.
847 /// @returns the greatest common divisor of Val1 and Val2
848 /// @brief Compute GCD of two APInt values.
849 APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
851 /// Treats the APInt as an unsigned value for conversion purposes.
852 /// @brief Converts the given APInt to a double value.
853 inline double RoundAPIntToDouble(const APInt& APIVal) {
854 return APIVal.roundToDouble();
857 /// Treats the APInt as a signed value for conversion purposes.
858 /// @brief Converts the given APInt to a double value.
859 inline double RoundSignedAPIntToDouble(const APInt& APIVal) {
860 return APIVal.signedRoundToDouble();
863 /// @brief Converts the given APInt to a float vlalue.
864 inline float RoundAPIntToFloat(const APInt& APIVal) {
865 return float(RoundAPIntToDouble(APIVal));
868 /// Treast the APInt as a signed value for conversion purposes.
869 /// @brief Converts the given APInt to a float value.
870 inline float RoundSignedAPIntToFloat(const APInt& APIVal) {
871 return float(APIVal.signedRoundToDouble());
874 /// RoundDoubleToAPInt - This function convert a double value to an APInt value.
875 /// @brief Converts the given double value into a APInt.
876 APInt RoundDoubleToAPInt(double Double, uint32_t width);
878 /// RoundFloatToAPInt - Converts a float value into an APInt value.
879 /// @brief Converts a float value into a APInt.
880 inline APInt RoundFloatToAPInt(float Float, uint32_t width) {
881 return RoundDoubleToAPInt(double(Float), width);
884 /// Arithmetic right-shift the APInt by shiftAmt.
885 /// @brief Arithmetic right-shift function.
886 inline APInt ashr(const APInt& LHS, uint32_t shiftAmt) {
887 return LHS.ashr(shiftAmt);
890 /// Logical right-shift the APInt by shiftAmt.
891 /// @brief Logical right-shift function.
892 inline APInt lshr(const APInt& LHS, uint32_t shiftAmt) {
893 return LHS.lshr(shiftAmt);
896 /// Left-shift the APInt by shiftAmt.
897 /// @brief Left-shift function.
898 inline APInt shl(const APInt& LHS, uint32_t shiftAmt) {
899 return LHS.shl(shiftAmt);
902 /// Signed divide APInt LHS by APInt RHS.
903 /// @brief Signed division function for APInt.
904 inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
905 return LHS.sdiv(RHS);
908 /// Unsigned divide APInt LHS by APInt RHS.
909 /// @brief Unsigned division function for APInt.
910 inline APInt udiv(const APInt& LHS, const APInt& RHS) {
911 return LHS.udiv(RHS);
914 /// Signed remainder operation on APInt.
915 /// @brief Function for signed remainder operation.
916 inline APInt srem(const APInt& LHS, const APInt& RHS) {
917 return LHS.srem(RHS);
920 /// Unsigned remainder operation on APInt.
921 /// @brief Function for unsigned remainder operation.
922 inline APInt urem(const APInt& LHS, const APInt& RHS) {
923 return LHS.urem(RHS);
926 /// Performs multiplication on APInt values.
927 /// @brief Function for multiplication operation.
928 inline APInt mul(const APInt& LHS, const APInt& RHS) {
932 /// Performs addition on APInt values.
933 /// @brief Function for addition operation.
934 inline APInt add(const APInt& LHS, const APInt& RHS) {
938 /// Performs subtraction on APInt values.
939 /// @brief Function for subtraction operation.
940 inline APInt sub(const APInt& LHS, const APInt& RHS) {
944 /// Performs bitwise AND operation on APInt LHS and
946 /// @brief Bitwise AND function for APInt.
947 inline APInt And(const APInt& LHS, const APInt& RHS) {
951 /// Performs bitwise OR operation on APInt LHS and APInt RHS.
952 /// @brief Bitwise OR function for APInt.
953 inline APInt Or(const APInt& LHS, const APInt& RHS) {
957 /// Performs bitwise XOR operation on APInt.
958 /// @brief Bitwise XOR function for APInt.
959 inline APInt Xor(const APInt& LHS, const APInt& RHS) {
963 /// Performs a bitwise complement operation on APInt.
964 /// @brief Bitwise complement function.
965 inline APInt Not(const APInt& APIVal) {
969 } // End of APIntOps namespace
971 } // End of llvm namespace