1 //===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a class to represent arbitrary precision integral
11 // constant values and operations on them.
13 //===----------------------------------------------------------------------===//
18 #include "llvm/Support/DataTypes.h"
22 #define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
27 class FoldingSetNodeID;
29 /* An unsigned host type used as a single part of a multi-part
31 typedef uint64_t integerPart;
33 const unsigned int host_char_bit = 8;
34 const unsigned int integerPartWidth = host_char_bit *
35 static_cast<unsigned int>(sizeof(integerPart));
37 //===----------------------------------------------------------------------===//
39 //===----------------------------------------------------------------------===//
41 /// APInt - This class represents arbitrary precision constant integral values.
42 /// It is a functional replacement for common case unsigned integer type like
43 /// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
44 /// integer sizes and large integer value types such as 3-bits, 15-bits, or more
45 /// than 64-bits of precision. APInt provides a variety of arithmetic operators
46 /// and methods to manipulate integer values of any bit-width. It supports both
47 /// the typical integer arithmetic and comparison operations as well as bitwise
50 /// The class has several invariants worth noting:
51 /// * All bit, byte, and word positions are zero-based.
52 /// * Once the bit width is set, it doesn't change except by the Truncate,
53 /// SignExtend, or ZeroExtend operations.
54 /// * All binary operators must be on APInt instances of the same bit width.
55 /// Attempting to use these operators on instances with different bit
56 /// widths will yield an assertion.
57 /// * The value is stored canonically as an unsigned value. For operations
58 /// where it makes a difference, there are both signed and unsigned variants
59 /// of the operation. For example, sdiv and udiv. However, because the bit
60 /// widths must be the same, operations such as Mul and Add produce the same
61 /// results regardless of whether the values are interpreted as signed or
63 /// * In general, the class tries to follow the style of computation that LLVM
64 /// uses in its IR. This simplifies its use for LLVM.
66 /// @brief Class for arbitrary precision integers.
69 uint32_t BitWidth; ///< The number of bits in this APInt.
71 /// This union is used to store the integer value. When the
72 /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
74 uint64_t VAL; ///< Used to store the <= 64 bits integer value.
75 uint64_t *pVal; ///< Used to store the >64 bits integer value.
78 /// This enum is used to hold the constants we needed for APInt.
81 APINT_BITS_PER_WORD = static_cast<unsigned int>(sizeof(uint64_t)) * 8,
82 /// Byte size of a word
83 APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
86 /// This constructor is used only internally for speed of construction of
87 /// temporaries. It is unsafe for general use so it is not public.
88 /// @brief Fast internal constructor
89 APInt(uint64_t* val, uint32_t bits) : BitWidth(bits), pVal(val) { }
91 /// @returns true if the number of bits <= 64, false otherwise.
92 /// @brief Determine if this APInt just has one word to store value.
93 bool isSingleWord() const {
94 return BitWidth <= APINT_BITS_PER_WORD;
97 /// @returns the word position for the specified bit position.
98 /// @brief Determine which word a bit is in.
99 static uint32_t whichWord(uint32_t bitPosition) {
100 return bitPosition / APINT_BITS_PER_WORD;
103 /// @returns the bit position in a word for the specified bit position
105 /// @brief Determine which bit in a word a bit is in.
106 static uint32_t whichBit(uint32_t bitPosition) {
107 return bitPosition % APINT_BITS_PER_WORD;
110 /// This method generates and returns a uint64_t (word) mask for a single
111 /// bit at a specific bit position. This is used to mask the bit in the
112 /// corresponding word.
113 /// @returns a uint64_t with only bit at "whichBit(bitPosition)" set
114 /// @brief Get a single bit mask.
115 static uint64_t maskBit(uint32_t bitPosition) {
116 return 1ULL << whichBit(bitPosition);
119 /// This method is used internally to clear the to "N" bits in the high order
120 /// word that are not used by the APInt. This is needed after the most
121 /// significant word is assigned a value to ensure that those bits are
123 /// @brief Clear unused high order bits
124 APInt& clearUnusedBits() {
125 // Compute how many bits are used in the final word
126 uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
128 // If all bits are used, we want to leave the value alone. This also
129 // avoids the undefined behavior of >> when the shift is the same size as
130 // the word size (64).
133 // Mask out the high bits.
134 uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
138 pVal[getNumWords() - 1] &= mask;
142 /// @returns the corresponding word for the specified bit position.
143 /// @brief Get the word corresponding to a bit position
144 uint64_t getWord(uint32_t bitPosition) const {
145 return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
148 /// This is used by the constructors that take string arguments.
149 /// @brief Convert a char array into an APInt
150 void fromString(uint32_t numBits, const char *strStart, uint32_t slen,
153 /// This is used by the toString method to divide by the radix. It simply
154 /// provides a more convenient form of divide for internal use since KnuthDiv
155 /// has specific constraints on its inputs. If those constraints are not met
156 /// then it provides a simpler form of divide.
157 /// @brief An internal division function for dividing APInts.
158 static void divide(const APInt LHS, uint32_t lhsWords,
159 const APInt &RHS, uint32_t rhsWords,
160 APInt *Quotient, APInt *Remainder);
163 /// @name Constructors
165 /// If isSigned is true then val is treated as if it were a signed value
166 /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
167 /// will be done. Otherwise, no sign extension occurs (high order bits beyond
168 /// the range of val are zero filled).
169 /// @param numBits the bit width of the constructed APInt
170 /// @param val the initial value of the APInt
171 /// @param isSigned how to treat signedness of val
172 /// @brief Create a new APInt of numBits width, initialized as val.
173 APInt(uint32_t numBits, uint64_t val, bool isSigned = false);
175 /// Note that numWords can be smaller or larger than the corresponding bit
176 /// width but any extraneous bits will be dropped.
177 /// @param numBits the bit width of the constructed APInt
178 /// @param numWords the number of words in bigVal
179 /// @param bigVal a sequence of words to form the initial value of the APInt
180 /// @brief Construct an APInt of numBits width, initialized as bigVal[].
181 APInt(uint32_t numBits, uint32_t numWords, const uint64_t bigVal[]);
183 /// This constructor interprets Val as a string in the given radix. The
184 /// interpretation stops when the first charater that is not suitable for the
185 /// radix is encountered. Acceptable radix values are 2, 8, 10 and 16. It is
186 /// an error for the value implied by the string to require more bits than
188 /// @param numBits the bit width of the constructed APInt
189 /// @param val the string to be interpreted
190 /// @param radix the radix of Val to use for the intepretation
191 /// @brief Construct an APInt from a string representation.
192 APInt(uint32_t numBits, const std::string& val, uint8_t radix);
194 /// This constructor interprets the slen characters starting at StrStart as
195 /// a string in the given radix. The interpretation stops when the first
196 /// character that is not suitable for the radix is encountered. Acceptable
197 /// radix values are 2, 8, 10 and 16. It is an error for the value implied by
198 /// the string to require more bits than numBits.
199 /// @param numBits the bit width of the constructed APInt
200 /// @param strStart the start of the string to be interpreted
201 /// @param slen the maximum number of characters to interpret
202 /// @param radix the radix to use for the conversion
203 /// @brief Construct an APInt from a string representation.
204 APInt(uint32_t numBits, const char strStart[], uint32_t slen, uint8_t radix);
206 /// Simply makes *this a copy of that.
207 /// @brief Copy Constructor.
208 APInt(const APInt& that);
210 /// @brief Destructor.
213 /// Default constructor that creates an uninitialized APInt. This is useful
214 /// for object deserialization (pair this with the static method Read).
215 explicit APInt() : BitWidth(1) {}
217 /// Profile - Used to insert APInt objects, or objects that contain APInt
218 /// objects, into FoldingSets.
219 void Profile(FoldingSetNodeID& id) const;
221 /// @brief Used by the Bitcode serializer to emit APInts to Bitcode.
222 void Emit(Serializer& S) const;
224 /// @brief Used by the Bitcode deserializer to deserialize APInts.
225 void Read(Deserializer& D);
228 /// @name Value Tests
230 /// This tests the high bit of this APInt to determine if it is set.
231 /// @returns true if this APInt is negative, false otherwise
232 /// @brief Determine sign of this APInt.
233 bool isNegative() const {
234 return (*this)[BitWidth - 1];
237 /// This tests the high bit of the APInt to determine if it is unset.
238 /// @brief Determine if this APInt Value is non-negative (>= 0)
239 bool isNonNegative() const {
240 return !isNegative();
243 /// This tests if the value of this APInt is positive (> 0). Note
244 /// that 0 is not a positive value.
245 /// @returns true if this APInt is positive.
246 /// @brief Determine if this APInt Value is positive.
247 bool isStrictlyPositive() const {
248 return isNonNegative() && (*this) != 0;
251 /// This checks to see if the value has all bits of the APInt are set or not.
252 /// @brief Determine if all bits are set
253 bool isAllOnesValue() const {
254 return countPopulation() == BitWidth;
257 /// This checks to see if the value of this APInt is the maximum unsigned
258 /// value for the APInt's bit width.
259 /// @brief Determine if this is the largest unsigned value.
260 bool isMaxValue() const {
261 return countPopulation() == BitWidth;
264 /// This checks to see if the value of this APInt is the maximum signed
265 /// value for the APInt's bit width.
266 /// @brief Determine if this is the largest signed value.
267 bool isMaxSignedValue() const {
268 return BitWidth == 1 ? VAL == 0 :
269 !isNegative() && countPopulation() == BitWidth - 1;
272 /// This checks to see if the value of this APInt is the minimum unsigned
273 /// value for the APInt's bit width.
274 /// @brief Determine if this is the smallest unsigned value.
275 bool isMinValue() const {
276 return countPopulation() == 0;
279 /// This checks to see if the value of this APInt is the minimum signed
280 /// value for the APInt's bit width.
281 /// @brief Determine if this is the smallest signed value.
282 bool isMinSignedValue() const {
283 return BitWidth == 1 ? VAL == 1 :
284 isNegative() && countPopulation() == 1;
287 /// @brief Check if this APInt has an N-bits unsigned integer value.
288 bool isIntN(uint32_t N) const {
289 assert(N && "N == 0 ???");
290 if (isSingleWord()) {
291 return VAL == (VAL & (~0ULL >> (64 - N)));
293 APInt Tmp(N, getNumWords(), pVal);
294 return Tmp == (*this);
298 /// @brief Check if this APInt has an N-bits signed integer value.
299 bool isSignedIntN(uint32_t N) const {
300 assert(N && "N == 0 ???");
301 return getMinSignedBits() <= N;
304 /// @returns true if the argument APInt value is a power of two > 0.
305 bool isPowerOf2() const;
307 /// isSignBit - Return true if this is the value returned by getSignBit.
308 bool isSignBit() const { return isMinSignedValue(); }
310 /// This converts the APInt to a boolean value as a test against zero.
311 /// @brief Boolean conversion function.
312 bool getBoolValue() const {
316 /// getLimitedValue - If this value is smaller than the specified limit,
317 /// return it, otherwise return the limit value. This causes the value
318 /// to saturate to the limit.
319 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
320 return (getActiveBits() > 64 || getZExtValue() > Limit) ?
321 Limit : getZExtValue();
325 /// @name Value Generators
327 /// @brief Gets maximum unsigned value of APInt for specific bit width.
328 static APInt getMaxValue(uint32_t numBits) {
329 return APInt(numBits, 0).set();
332 /// @brief Gets maximum signed value of APInt for a specific bit width.
333 static APInt getSignedMaxValue(uint32_t numBits) {
334 return APInt(numBits, 0).set().clear(numBits - 1);
337 /// @brief Gets minimum unsigned value of APInt for a specific bit width.
338 static APInt getMinValue(uint32_t numBits) {
339 return APInt(numBits, 0);
342 /// @brief Gets minimum signed value of APInt for a specific bit width.
343 static APInt getSignedMinValue(uint32_t numBits) {
344 return APInt(numBits, 0).set(numBits - 1);
347 /// getSignBit - This is just a wrapper function of getSignedMinValue(), and
348 /// it helps code readability when we want to get a SignBit.
349 /// @brief Get the SignBit for a specific bit width.
350 static APInt getSignBit(uint32_t BitWidth) {
351 return getSignedMinValue(BitWidth);
354 /// @returns the all-ones value for an APInt of the specified bit-width.
355 /// @brief Get the all-ones value.
356 static APInt getAllOnesValue(uint32_t numBits) {
357 return APInt(numBits, 0).set();
360 /// @returns the '0' value for an APInt of the specified bit-width.
361 /// @brief Get the '0' value.
362 static APInt getNullValue(uint32_t numBits) {
363 return APInt(numBits, 0);
366 /// Get an APInt with the same BitWidth as this APInt, just zero mask
367 /// the low bits and right shift to the least significant bit.
368 /// @returns the high "numBits" bits of this APInt.
369 APInt getHiBits(uint32_t numBits) const;
371 /// Get an APInt with the same BitWidth as this APInt, just zero mask
373 /// @returns the low "numBits" bits of this APInt.
374 APInt getLoBits(uint32_t numBits) const;
376 /// Constructs an APInt value that has a contiguous range of bits set. The
377 /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
378 /// bits will be zero. For example, with parameters(32, 0, 16) you would get
379 /// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For
380 /// example, with parameters (32, 28, 4), you would get 0xF000000F.
381 /// @param numBits the intended bit width of the result
382 /// @param loBit the index of the lowest bit set.
383 /// @param hiBit the index of the highest bit set.
384 /// @returns An APInt value with the requested bits set.
385 /// @brief Get a value with a block of bits set.
386 static APInt getBitsSet(uint32_t numBits, uint32_t loBit, uint32_t hiBit) {
387 assert(hiBit <= numBits && "hiBit out of range");
388 assert(loBit < numBits && "loBit out of range");
390 return getLowBitsSet(numBits, hiBit) |
391 getHighBitsSet(numBits, numBits-loBit);
392 return getLowBitsSet(numBits, hiBit-loBit).shl(loBit);
395 /// Constructs an APInt value that has the top hiBitsSet bits set.
396 /// @param numBits the bitwidth of the result
397 /// @param hiBitsSet the number of high-order bits set in the result.
398 /// @brief Get a value with high bits set
399 static APInt getHighBitsSet(uint32_t numBits, uint32_t hiBitsSet) {
400 assert(hiBitsSet <= numBits && "Too many bits to set!");
401 // Handle a degenerate case, to avoid shifting by word size
403 return APInt(numBits, 0);
404 uint32_t shiftAmt = numBits - hiBitsSet;
405 // For small values, return quickly
406 if (numBits <= APINT_BITS_PER_WORD)
407 return APInt(numBits, ~0ULL << shiftAmt);
408 return (~APInt(numBits, 0)).shl(shiftAmt);
411 /// Constructs an APInt value that has the bottom loBitsSet bits set.
412 /// @param numBits the bitwidth of the result
413 /// @param loBitsSet the number of low-order bits set in the result.
414 /// @brief Get a value with low bits set
415 static APInt getLowBitsSet(uint32_t numBits, uint32_t loBitsSet) {
416 assert(loBitsSet <= numBits && "Too many bits to set!");
417 // Handle a degenerate case, to avoid shifting by word size
419 return APInt(numBits, 0);
420 if (loBitsSet == APINT_BITS_PER_WORD)
421 return APInt(numBits, -1ULL);
422 // For small values, return quickly
423 if (numBits < APINT_BITS_PER_WORD)
424 return APInt(numBits, (1ULL << loBitsSet) - 1);
425 return (~APInt(numBits, 0)).lshr(numBits - loBitsSet);
428 /// The hash value is computed as the sum of the words and the bit width.
429 /// @returns A hash value computed from the sum of the APInt words.
430 /// @brief Get a hash value based on this APInt
431 uint64_t getHashValue() const;
433 /// This function returns a pointer to the internal storage of the APInt.
434 /// This is useful for writing out the APInt in binary form without any
436 const uint64_t* getRawData() const {
443 /// @name Unary Operators
445 /// @returns a new APInt value representing *this incremented by one
446 /// @brief Postfix increment operator.
447 const APInt operator++(int) {
453 /// @returns *this incremented by one
454 /// @brief Prefix increment operator.
457 /// @returns a new APInt representing *this decremented by one.
458 /// @brief Postfix decrement operator.
459 const APInt operator--(int) {
465 /// @returns *this decremented by one.
466 /// @brief Prefix decrement operator.
469 /// Performs a bitwise complement operation on this APInt.
470 /// @returns an APInt that is the bitwise complement of *this
471 /// @brief Unary bitwise complement operator.
472 APInt operator~() const;
474 /// Negates *this using two's complement logic.
475 /// @returns An APInt value representing the negation of *this.
476 /// @brief Unary negation operator
477 APInt operator-() const {
478 return APInt(BitWidth, 0) - (*this);
481 /// Performs logical negation operation on this APInt.
482 /// @returns true if *this is zero, false otherwise.
483 /// @brief Logical negation operator.
484 bool operator !() const;
487 /// @name Assignment Operators
489 /// @returns *this after assignment of RHS.
490 /// @brief Copy assignment operator.
491 APInt& operator=(const APInt& RHS);
493 /// The RHS value is assigned to *this. If the significant bits in RHS exceed
494 /// the bit width, the excess bits are truncated. If the bit width is larger
495 /// than 64, the value is zero filled in the unspecified high order bits.
496 /// @returns *this after assignment of RHS value.
497 /// @brief Assignment operator.
498 APInt& operator=(uint64_t RHS);
500 /// Performs a bitwise AND operation on this APInt and RHS. The result is
501 /// assigned to *this.
502 /// @returns *this after ANDing with RHS.
503 /// @brief Bitwise AND assignment operator.
504 APInt& operator&=(const APInt& RHS);
506 /// Performs a bitwise OR operation on this APInt and RHS. The result is
508 /// @returns *this after ORing with RHS.
509 /// @brief Bitwise OR assignment operator.
510 APInt& operator|=(const APInt& RHS);
512 /// Performs a bitwise XOR operation on this APInt and RHS. The result is
513 /// assigned to *this.
514 /// @returns *this after XORing with RHS.
515 /// @brief Bitwise XOR assignment operator.
516 APInt& operator^=(const APInt& RHS);
518 /// Multiplies this APInt by RHS and assigns the result to *this.
520 /// @brief Multiplication assignment operator.
521 APInt& operator*=(const APInt& RHS);
523 /// Adds RHS to *this and assigns the result to *this.
525 /// @brief Addition assignment operator.
526 APInt& operator+=(const APInt& RHS);
528 /// Subtracts RHS from *this and assigns the result to *this.
530 /// @brief Subtraction assignment operator.
531 APInt& operator-=(const APInt& RHS);
533 /// Shifts *this left by shiftAmt and assigns the result to *this.
534 /// @returns *this after shifting left by shiftAmt
535 /// @brief Left-shift assignment function.
536 APInt& operator<<=(uint32_t shiftAmt) {
537 *this = shl(shiftAmt);
542 /// @name Binary Operators
544 /// Performs a bitwise AND operation on *this and RHS.
545 /// @returns An APInt value representing the bitwise AND of *this and RHS.
546 /// @brief Bitwise AND operator.
547 APInt operator&(const APInt& RHS) const;
548 APInt And(const APInt& RHS) const {
549 return this->operator&(RHS);
552 /// Performs a bitwise OR operation on *this and RHS.
553 /// @returns An APInt value representing the bitwise OR of *this and RHS.
554 /// @brief Bitwise OR operator.
555 APInt operator|(const APInt& RHS) const;
556 APInt Or(const APInt& RHS) const {
557 return this->operator|(RHS);
560 /// Performs a bitwise XOR operation on *this and RHS.
561 /// @returns An APInt value representing the bitwise XOR of *this and RHS.
562 /// @brief Bitwise XOR operator.
563 APInt operator^(const APInt& RHS) const;
564 APInt Xor(const APInt& RHS) const {
565 return this->operator^(RHS);
568 /// Multiplies this APInt by RHS and returns the result.
569 /// @brief Multiplication operator.
570 APInt operator*(const APInt& RHS) const;
572 /// Adds RHS to this APInt and returns the result.
573 /// @brief Addition operator.
574 APInt operator+(const APInt& RHS) const;
575 APInt operator+(uint64_t RHS) const {
576 return (*this) + APInt(BitWidth, RHS);
579 /// Subtracts RHS from this APInt and returns the result.
580 /// @brief Subtraction operator.
581 APInt operator-(const APInt& RHS) const;
582 APInt operator-(uint64_t RHS) const {
583 return (*this) - APInt(BitWidth, RHS);
586 APInt operator<<(unsigned Bits) const {
590 APInt operator<<(const APInt &Bits) const {
594 /// Arithmetic right-shift this APInt by shiftAmt.
595 /// @brief Arithmetic right-shift function.
596 APInt ashr(uint32_t shiftAmt) const;
598 /// Logical right-shift this APInt by shiftAmt.
599 /// @brief Logical right-shift function.
600 APInt lshr(uint32_t shiftAmt) const;
602 /// Left-shift this APInt by shiftAmt.
603 /// @brief Left-shift function.
604 APInt shl(uint32_t shiftAmt) const;
606 /// @brief Rotate left by rotateAmt.
607 APInt rotl(uint32_t rotateAmt) const;
609 /// @brief Rotate right by rotateAmt.
610 APInt rotr(uint32_t rotateAmt) const;
612 /// Arithmetic right-shift this APInt by shiftAmt.
613 /// @brief Arithmetic right-shift function.
614 APInt ashr(const APInt &shiftAmt) const;
616 /// Logical right-shift this APInt by shiftAmt.
617 /// @brief Logical right-shift function.
618 APInt lshr(const APInt &shiftAmt) const;
620 /// Left-shift this APInt by shiftAmt.
621 /// @brief Left-shift function.
622 APInt shl(const APInt &shiftAmt) const;
624 /// @brief Rotate left by rotateAmt.
625 APInt rotl(const APInt &rotateAmt) const;
627 /// @brief Rotate right by rotateAmt.
628 APInt rotr(const APInt &rotateAmt) const;
630 /// Perform an unsigned divide operation on this APInt by RHS. Both this and
631 /// RHS are treated as unsigned quantities for purposes of this division.
632 /// @returns a new APInt value containing the division result
633 /// @brief Unsigned division operation.
634 APInt udiv(const APInt& RHS) const;
636 /// Signed divide this APInt by APInt RHS.
637 /// @brief Signed division function for APInt.
638 APInt sdiv(const APInt& RHS) const {
640 if (RHS.isNegative())
641 return (-(*this)).udiv(-RHS);
643 return -((-(*this)).udiv(RHS));
644 else if (RHS.isNegative())
645 return -(this->udiv(-RHS));
646 return this->udiv(RHS);
649 /// Perform an unsigned remainder operation on this APInt with RHS being the
650 /// divisor. Both this and RHS are treated as unsigned quantities for purposes
651 /// of this operation. Note that this is a true remainder operation and not
652 /// a modulo operation because the sign follows the sign of the dividend
654 /// @returns a new APInt value containing the remainder result
655 /// @brief Unsigned remainder operation.
656 APInt urem(const APInt& RHS) const;
658 /// Signed remainder operation on APInt.
659 /// @brief Function for signed remainder operation.
660 APInt srem(const APInt& RHS) const {
662 if (RHS.isNegative())
663 return -((-(*this)).urem(-RHS));
665 return -((-(*this)).urem(RHS));
666 else if (RHS.isNegative())
667 return this->urem(-RHS);
668 return this->urem(RHS);
671 /// Sometimes it is convenient to divide two APInt values and obtain both the
672 /// quotient and remainder. This function does both operations in the same
673 /// computation making it a little more efficient. The pair of input arguments
674 /// may overlap with the pair of output arguments. It is safe to call
675 /// udivrem(X, Y, X, Y), for example.
676 /// @brief Dual division/remainder interface.
677 static void udivrem(const APInt &LHS, const APInt &RHS,
678 APInt &Quotient, APInt &Remainder);
680 static void sdivrem(const APInt &LHS, const APInt &RHS,
681 APInt &Quotient, APInt &Remainder)
683 if (LHS.isNegative()) {
684 if (RHS.isNegative())
685 APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
687 APInt::udivrem(-LHS, RHS, Quotient, Remainder);
688 Quotient = -Quotient;
689 Remainder = -Remainder;
690 } else if (RHS.isNegative()) {
691 APInt::udivrem(LHS, -RHS, Quotient, Remainder);
692 Quotient = -Quotient;
694 APInt::udivrem(LHS, RHS, Quotient, Remainder);
698 /// @returns the bit value at bitPosition
699 /// @brief Array-indexing support.
700 bool operator[](uint32_t bitPosition) const;
703 /// @name Comparison Operators
705 /// Compares this APInt with RHS for the validity of the equality
707 /// @brief Equality operator.
708 bool operator==(const APInt& RHS) const;
710 /// Compares this APInt with a uint64_t for the validity of the equality
712 /// @returns true if *this == Val
713 /// @brief Equality operator.
714 bool operator==(uint64_t Val) const;
716 /// Compares this APInt with RHS for the validity of the equality
718 /// @returns true if *this == Val
719 /// @brief Equality comparison.
720 bool eq(const APInt &RHS) const {
721 return (*this) == RHS;
724 /// Compares this APInt with RHS for the validity of the inequality
726 /// @returns true if *this != Val
727 /// @brief Inequality operator.
728 bool operator!=(const APInt& RHS) const {
729 return !((*this) == RHS);
732 /// Compares this APInt with a uint64_t for the validity of the inequality
734 /// @returns true if *this != Val
735 /// @brief Inequality operator.
736 bool operator!=(uint64_t Val) const {
737 return !((*this) == Val);
740 /// Compares this APInt with RHS for the validity of the inequality
742 /// @returns true if *this != Val
743 /// @brief Inequality comparison
744 bool ne(const APInt &RHS) const {
745 return !((*this) == RHS);
748 /// Regards both *this and RHS as unsigned quantities and compares them for
749 /// the validity of the less-than relationship.
750 /// @returns true if *this < RHS when both are considered unsigned.
751 /// @brief Unsigned less than comparison
752 bool ult(const APInt& RHS) const;
754 /// Regards both *this and RHS as signed quantities and compares them for
755 /// validity of the less-than relationship.
756 /// @returns true if *this < RHS when both are considered signed.
757 /// @brief Signed less than comparison
758 bool slt(const APInt& RHS) const;
760 /// Regards both *this and RHS as unsigned quantities and compares them for
761 /// validity of the less-or-equal relationship.
762 /// @returns true if *this <= RHS when both are considered unsigned.
763 /// @brief Unsigned less or equal comparison
764 bool ule(const APInt& RHS) const {
765 return ult(RHS) || eq(RHS);
768 /// Regards both *this and RHS as signed quantities and compares them for
769 /// validity of the less-or-equal relationship.
770 /// @returns true if *this <= RHS when both are considered signed.
771 /// @brief Signed less or equal comparison
772 bool sle(const APInt& RHS) const {
773 return slt(RHS) || eq(RHS);
776 /// Regards both *this and RHS as unsigned quantities and compares them for
777 /// the validity of the greater-than relationship.
778 /// @returns true if *this > RHS when both are considered unsigned.
779 /// @brief Unsigned greather than comparison
780 bool ugt(const APInt& RHS) const {
781 return !ult(RHS) && !eq(RHS);
784 /// Regards both *this and RHS as signed quantities and compares them for
785 /// the validity of the greater-than relationship.
786 /// @returns true if *this > RHS when both are considered signed.
787 /// @brief Signed greather than comparison
788 bool sgt(const APInt& RHS) const {
789 return !slt(RHS) && !eq(RHS);
792 /// Regards both *this and RHS as unsigned quantities and compares them for
793 /// validity of the greater-or-equal relationship.
794 /// @returns true if *this >= RHS when both are considered unsigned.
795 /// @brief Unsigned greater or equal comparison
796 bool uge(const APInt& RHS) const {
800 /// Regards both *this and RHS as signed quantities and compares them for
801 /// validity of the greater-or-equal relationship.
802 /// @returns true if *this >= RHS when both are considered signed.
803 /// @brief Signed greather or equal comparison
804 bool sge(const APInt& RHS) const {
808 /// This operation tests if there are any pairs of corresponding bits
809 /// between this APInt and RHS that are both set.
810 bool intersects(const APInt &RHS) const {
811 return (*this & RHS) != 0;
815 /// @name Resizing Operators
817 /// Truncate the APInt to a specified width. It is an error to specify a width
818 /// that is greater than or equal to the current width.
819 /// @brief Truncate to new width.
820 APInt &trunc(uint32_t width);
822 /// This operation sign extends the APInt to a new width. If the high order
823 /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
824 /// It is an error to specify a width that is less than or equal to the
826 /// @brief Sign extend to a new width.
827 APInt &sext(uint32_t width);
829 /// This operation zero extends the APInt to a new width. The high order bits
830 /// are filled with 0 bits. It is an error to specify a width that is less
831 /// than or equal to the current width.
832 /// @brief Zero extend to a new width.
833 APInt &zext(uint32_t width);
835 /// Make this APInt have the bit width given by \p width. The value is sign
836 /// extended, truncated, or left alone to make it that width.
837 /// @brief Sign extend or truncate to width
838 APInt &sextOrTrunc(uint32_t width);
840 /// Make this APInt have the bit width given by \p width. The value is zero
841 /// extended, truncated, or left alone to make it that width.
842 /// @brief Zero extend or truncate to width
843 APInt &zextOrTrunc(uint32_t width);
846 /// @name Bit Manipulation Operators
848 /// @brief Set every bit to 1.
851 /// Set the given bit to 1 whose position is given as "bitPosition".
852 /// @brief Set a given bit to 1.
853 APInt& set(uint32_t bitPosition);
855 /// @brief Set every bit to 0.
858 /// Set the given bit to 0 whose position is given as "bitPosition".
859 /// @brief Set a given bit to 0.
860 APInt& clear(uint32_t bitPosition);
862 /// @brief Toggle every bit to its opposite value.
865 /// Toggle a given bit to its opposite value whose position is given
866 /// as "bitPosition".
867 /// @brief Toggles a given bit to its opposite value.
868 APInt& flip(uint32_t bitPosition);
871 /// @name Value Characterization Functions
874 /// @returns the total number of bits.
875 uint32_t getBitWidth() const {
879 /// Here one word's bitwidth equals to that of uint64_t.
880 /// @returns the number of words to hold the integer value of this APInt.
881 /// @brief Get the number of words.
882 uint32_t getNumWords() const {
883 return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
886 /// This function returns the number of active bits which is defined as the
887 /// bit width minus the number of leading zeros. This is used in several
888 /// computations to see how "wide" the value is.
889 /// @brief Compute the number of active bits in the value
890 uint32_t getActiveBits() const {
891 return BitWidth - countLeadingZeros();
894 /// This function returns the number of active words in the value of this
895 /// APInt. This is used in conjunction with getActiveData to extract the raw
896 /// value of the APInt.
897 uint32_t getActiveWords() const {
898 return whichWord(getActiveBits()-1) + 1;
901 /// Computes the minimum bit width for this APInt while considering it to be
902 /// a signed (and probably negative) value. If the value is not negative,
903 /// this function returns the same value as getActiveBits()+1. Otherwise, it
904 /// returns the smallest bit width that will retain the negative value. For
905 /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
906 /// for -1, this function will always return 1.
907 /// @brief Get the minimum bit size for this signed APInt
908 uint32_t getMinSignedBits() const {
910 return BitWidth - countLeadingOnes() + 1;
911 return getActiveBits()+1;
914 /// This method attempts to return the value of this APInt as a zero extended
915 /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
916 /// uint64_t. Otherwise an assertion will result.
917 /// @brief Get zero extended value
918 uint64_t getZExtValue() const {
921 assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
925 /// This method attempts to return the value of this APInt as a sign extended
926 /// int64_t. The bit width must be <= 64 or the value must fit within an
927 /// int64_t. Otherwise an assertion will result.
928 /// @brief Get sign extended value
929 int64_t getSExtValue() const {
931 return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >>
932 (APINT_BITS_PER_WORD - BitWidth);
933 assert(getActiveBits() <= 64 && "Too many bits for int64_t");
934 return int64_t(pVal[0]);
937 /// This method determines how many bits are required to hold the APInt
938 /// equivalent of the string given by \p str of length \p slen.
939 /// @brief Get bits required for string value.
940 static uint32_t getBitsNeeded(const char* str, uint32_t slen, uint8_t radix);
942 /// countLeadingZeros - This function is an APInt version of the
943 /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
944 /// of zeros from the most significant bit to the first one bit.
945 /// @returns BitWidth if the value is zero.
946 /// @returns the number of zeros from the most significant bit to the first
948 uint32_t countLeadingZeros() const;
950 /// countLeadingOnes - This function is an APInt version of the
951 /// countLeadingOnes_{32,64} functions in MathExtras.h. It counts the number
952 /// of ones from the most significant bit to the first zero bit.
953 /// @returns 0 if the high order bit is not set
954 /// @returns the number of 1 bits from the most significant to the least
955 /// @brief Count the number of leading one bits.
956 uint32_t countLeadingOnes() const;
958 /// countTrailingZeros - This function is an APInt version of the
959 /// countTrailingZeros_{32,64} functions in MathExtras.h. It counts
960 /// the number of zeros from the least significant bit to the first set bit.
961 /// @returns BitWidth if the value is zero.
962 /// @returns the number of zeros from the least significant bit to the first
964 /// @brief Count the number of trailing zero bits.
965 uint32_t countTrailingZeros() const;
967 /// countTrailingOnes - This function is an APInt version of the
968 /// countTrailingOnes_{32,64} functions in MathExtras.h. It counts
969 /// the number of ones from the least significant bit to the first zero bit.
970 /// @returns BitWidth if the value is all ones.
971 /// @returns the number of ones from the least significant bit to the first
973 /// @brief Count the number of trailing one bits.
974 uint32_t countTrailingOnes() const;
976 /// countPopulation - This function is an APInt version of the
977 /// countPopulation_{32,64} functions in MathExtras.h. It counts the number
978 /// of 1 bits in the APInt value.
979 /// @returns 0 if the value is zero.
980 /// @returns the number of set bits.
981 /// @brief Count the number of bits set.
982 uint32_t countPopulation() const;
985 /// @name Conversion Functions
988 /// This is used internally to convert an APInt to a string.
989 /// @brief Converts an APInt to a std::string
990 std::string toString(uint8_t radix, bool wantSigned) const;
992 /// Considers the APInt to be unsigned and converts it into a string in the
993 /// radix given. The radix can be 2, 8, 10 or 16.
994 /// @returns a character interpretation of the APInt
995 /// @brief Convert unsigned APInt to string representation.
996 std::string toStringUnsigned(uint8_t radix = 10) const {
997 return toString(radix, false);
1000 /// Considers the APInt to be signed and converts it into a string in the
1001 /// radix given. The radix can be 2, 8, 10 or 16.
1002 /// @returns a character interpretation of the APInt
1003 /// @brief Convert signed APInt to string representation.
1004 std::string toStringSigned(uint8_t radix = 10) const {
1005 return toString(radix, true);
1008 /// @returns a byte-swapped representation of this APInt Value.
1009 APInt byteSwap() const;
1011 /// @brief Converts this APInt to a double value.
1012 double roundToDouble(bool isSigned) const;
1014 /// @brief Converts this unsigned APInt to a double value.
1015 double roundToDouble() const {
1016 return roundToDouble(false);
1019 /// @brief Converts this signed APInt to a double value.
1020 double signedRoundToDouble() const {
1021 return roundToDouble(true);
1024 /// The conversion does not do a translation from integer to double, it just
1025 /// re-interprets the bits as a double. Note that it is valid to do this on
1026 /// any bit width. Exactly 64 bits will be translated.
1027 /// @brief Converts APInt bits to a double
1028 double bitsToDouble() const {
1033 T.I = (isSingleWord() ? VAL : pVal[0]);
1037 /// The conversion does not do a translation from integer to float, it just
1038 /// re-interprets the bits as a float. Note that it is valid to do this on
1039 /// any bit width. Exactly 32 bits will be translated.
1040 /// @brief Converts APInt bits to a double
1041 float bitsToFloat() const {
1046 T.I = uint32_t((isSingleWord() ? VAL : pVal[0]));
1050 /// The conversion does not do a translation from double to integer, it just
1051 /// re-interprets the bits of the double. Note that it is valid to do this on
1052 /// any bit width but bits from V may get truncated.
1053 /// @brief Converts a double to APInt bits.
1054 APInt& doubleToBits(double V) {
1064 return clearUnusedBits();
1067 /// The conversion does not do a translation from float to integer, it just
1068 /// re-interprets the bits of the float. Note that it is valid to do this on
1069 /// any bit width but bits from V may get truncated.
1070 /// @brief Converts a float to APInt bits.
1071 APInt& floatToBits(float V) {
1081 return clearUnusedBits();
1085 /// @name Mathematics Operations
1088 /// @returns the floor log base 2 of this APInt.
1089 uint32_t logBase2() const {
1090 return BitWidth - 1 - countLeadingZeros();
1093 /// @returns the log base 2 of this APInt if its an exact power of two, -1
1095 int32_t exactLogBase2() const {
1101 /// @brief Compute the square root
1104 /// If *this is < 0 then return -(*this), otherwise *this;
1105 /// @brief Get the absolute value;
1112 /// @returns the multiplicative inverse for a given modulo.
1113 APInt multiplicativeInverse(const APInt& modulo) const;
1116 /// @name Building-block Operations for APInt and APFloat
1119 // These building block operations operate on a representation of
1120 // arbitrary precision, two's-complement, bignum integer values.
1121 // They should be sufficient to implement APInt and APFloat bignum
1122 // requirements. Inputs are generally a pointer to the base of an
1123 // array of integer parts, representing an unsigned bignum, and a
1124 // count of how many parts there are.
1126 /// Sets the least significant part of a bignum to the input value,
1127 /// and zeroes out higher parts. */
1128 static void tcSet(integerPart *, integerPart, unsigned int);
1130 /// Assign one bignum to another.
1131 static void tcAssign(integerPart *, const integerPart *, unsigned int);
1133 /// Returns true if a bignum is zero, false otherwise.
1134 static bool tcIsZero(const integerPart *, unsigned int);
1136 /// Extract the given bit of a bignum; returns 0 or 1. Zero-based.
1137 static int tcExtractBit(const integerPart *, unsigned int bit);
1139 /// Copy the bit vector of width srcBITS from SRC, starting at bit
1140 /// srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB
1141 /// becomes the least significant bit of DST. All high bits above
1142 /// srcBITS in DST are zero-filled.
1143 static void tcExtract(integerPart *, unsigned int dstCount, const integerPart *,
1144 unsigned int srcBits, unsigned int srcLSB);
1146 /// Set the given bit of a bignum. Zero-based.
1147 static void tcSetBit(integerPart *, unsigned int bit);
1149 /// Returns the bit number of the least or most significant set bit
1150 /// of a number. If the input number has no bits set -1U is
1152 static unsigned int tcLSB(const integerPart *, unsigned int);
1153 static unsigned int tcMSB(const integerPart *, unsigned int);
1155 /// Negate a bignum in-place.
1156 static void tcNegate(integerPart *, unsigned int);
1158 /// DST += RHS + CARRY where CARRY is zero or one. Returns the
1160 static integerPart tcAdd(integerPart *, const integerPart *,
1161 integerPart carry, unsigned);
1163 /// DST -= RHS + CARRY where CARRY is zero or one. Returns the
1165 static integerPart tcSubtract(integerPart *, const integerPart *,
1166 integerPart carry, unsigned);
1168 /// DST += SRC * MULTIPLIER + PART if add is true
1169 /// DST = SRC * MULTIPLIER + PART if add is false
1171 /// Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC
1172 /// they must start at the same point, i.e. DST == SRC.
1174 /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is
1175 /// returned. Otherwise DST is filled with the least significant
1176 /// DSTPARTS parts of the result, and if all of the omitted higher
1177 /// parts were zero return zero, otherwise overflow occurred and
1179 static int tcMultiplyPart(integerPart *dst, const integerPart *src,
1180 integerPart multiplier, integerPart carry,
1181 unsigned int srcParts, unsigned int dstParts,
1184 /// DST = LHS * RHS, where DST has the same width as the operands
1185 /// and is filled with the least significant parts of the result.
1186 /// Returns one if overflow occurred, otherwise zero. DST must be
1187 /// disjoint from both operands.
1188 static int tcMultiply(integerPart *, const integerPart *,
1189 const integerPart *, unsigned);
1191 /// DST = LHS * RHS, where DST has width the sum of the widths of
1192 /// the operands. No overflow occurs. DST must be disjoint from
1193 /// both operands. Returns the number of parts required to hold the
1195 static unsigned int tcFullMultiply(integerPart *, const integerPart *,
1196 const integerPart *, unsigned, unsigned);
1198 /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1199 /// Otherwise set LHS to LHS / RHS with the fractional part
1200 /// discarded, set REMAINDER to the remainder, return zero. i.e.
1202 /// OLD_LHS = RHS * LHS + REMAINDER
1204 /// SCRATCH is a bignum of the same size as the operands and result
1205 /// for use by the routine; its contents need not be initialized
1206 /// and are destroyed. LHS, REMAINDER and SCRATCH must be
1208 static int tcDivide(integerPart *lhs, const integerPart *rhs,
1209 integerPart *remainder, integerPart *scratch,
1210 unsigned int parts);
1212 /// Shift a bignum left COUNT bits. Shifted in bits are zero.
1213 /// There are no restrictions on COUNT.
1214 static void tcShiftLeft(integerPart *, unsigned int parts,
1215 unsigned int count);
1217 /// Shift a bignum right COUNT bits. Shifted in bits are zero.
1218 /// There are no restrictions on COUNT.
1219 static void tcShiftRight(integerPart *, unsigned int parts,
1220 unsigned int count);
1222 /// The obvious AND, OR and XOR and complement operations.
1223 static void tcAnd(integerPart *, const integerPart *, unsigned int);
1224 static void tcOr(integerPart *, const integerPart *, unsigned int);
1225 static void tcXor(integerPart *, const integerPart *, unsigned int);
1226 static void tcComplement(integerPart *, unsigned int);
1228 /// Comparison (unsigned) of two bignums.
1229 static int tcCompare(const integerPart *, const integerPart *,
1232 /// Increment a bignum in-place. Return the carry flag.
1233 static integerPart tcIncrement(integerPart *, unsigned int);
1235 /// Set the least significant BITS and clear the rest.
1236 static void tcSetLeastSignificantBits(integerPart *, unsigned int,
1239 /// @brief debug method
1245 inline bool operator==(uint64_t V1, const APInt& V2) {
1249 inline bool operator!=(uint64_t V1, const APInt& V2) {
1253 namespace APIntOps {
1255 /// @brief Determine the smaller of two APInts considered to be signed.
1256 inline APInt smin(const APInt &A, const APInt &B) {
1257 return A.slt(B) ? A : B;
1260 /// @brief Determine the larger of two APInts considered to be signed.
1261 inline APInt smax(const APInt &A, const APInt &B) {
1262 return A.sgt(B) ? A : B;
1265 /// @brief Determine the smaller of two APInts considered to be signed.
1266 inline APInt umin(const APInt &A, const APInt &B) {
1267 return A.ult(B) ? A : B;
1270 /// @brief Determine the larger of two APInts considered to be unsigned.
1271 inline APInt umax(const APInt &A, const APInt &B) {
1272 return A.ugt(B) ? A : B;
1275 /// @brief Check if the specified APInt has a N-bits unsigned integer value.
1276 inline bool isIntN(uint32_t N, const APInt& APIVal) {
1277 return APIVal.isIntN(N);
1280 /// @brief Check if the specified APInt has a N-bits signed integer value.
1281 inline bool isSignedIntN(uint32_t N, const APInt& APIVal) {
1282 return APIVal.isSignedIntN(N);
1285 /// @returns true if the argument APInt value is a sequence of ones
1286 /// starting at the least significant bit with the remainder zero.
1287 inline bool isMask(uint32_t numBits, const APInt& APIVal) {
1288 return numBits <= APIVal.getBitWidth() &&
1289 APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
1292 /// @returns true if the argument APInt value contains a sequence of ones
1293 /// with the remainder zero.
1294 inline bool isShiftedMask(uint32_t numBits, const APInt& APIVal) {
1295 return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
1298 /// @returns a byte-swapped representation of the specified APInt Value.
1299 inline APInt byteSwap(const APInt& APIVal) {
1300 return APIVal.byteSwap();
1303 /// @returns the floor log base 2 of the specified APInt value.
1304 inline uint32_t logBase2(const APInt& APIVal) {
1305 return APIVal.logBase2();
1308 /// GreatestCommonDivisor - This function returns the greatest common
1309 /// divisor of the two APInt values using Euclid's algorithm.
1310 /// @returns the greatest common divisor of Val1 and Val2
1311 /// @brief Compute GCD of two APInt values.
1312 APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
1314 /// Treats the APInt as an unsigned value for conversion purposes.
1315 /// @brief Converts the given APInt to a double value.
1316 inline double RoundAPIntToDouble(const APInt& APIVal) {
1317 return APIVal.roundToDouble();
1320 /// Treats the APInt as a signed value for conversion purposes.
1321 /// @brief Converts the given APInt to a double value.
1322 inline double RoundSignedAPIntToDouble(const APInt& APIVal) {
1323 return APIVal.signedRoundToDouble();
1326 /// @brief Converts the given APInt to a float vlalue.
1327 inline float RoundAPIntToFloat(const APInt& APIVal) {
1328 return float(RoundAPIntToDouble(APIVal));
1331 /// Treast the APInt as a signed value for conversion purposes.
1332 /// @brief Converts the given APInt to a float value.
1333 inline float RoundSignedAPIntToFloat(const APInt& APIVal) {
1334 return float(APIVal.signedRoundToDouble());
1337 /// RoundDoubleToAPInt - This function convert a double value to an APInt value.
1338 /// @brief Converts the given double value into a APInt.
1339 APInt RoundDoubleToAPInt(double Double, uint32_t width);
1341 /// RoundFloatToAPInt - Converts a float value into an APInt value.
1342 /// @brief Converts a float value into a APInt.
1343 inline APInt RoundFloatToAPInt(float Float, uint32_t width) {
1344 return RoundDoubleToAPInt(double(Float), width);
1347 /// Arithmetic right-shift the APInt by shiftAmt.
1348 /// @brief Arithmetic right-shift function.
1349 inline APInt ashr(const APInt& LHS, uint32_t shiftAmt) {
1350 return LHS.ashr(shiftAmt);
1353 /// Logical right-shift the APInt by shiftAmt.
1354 /// @brief Logical right-shift function.
1355 inline APInt lshr(const APInt& LHS, uint32_t shiftAmt) {
1356 return LHS.lshr(shiftAmt);
1359 /// Left-shift the APInt by shiftAmt.
1360 /// @brief Left-shift function.
1361 inline APInt shl(const APInt& LHS, uint32_t shiftAmt) {
1362 return LHS.shl(shiftAmt);
1365 /// Signed divide APInt LHS by APInt RHS.
1366 /// @brief Signed division function for APInt.
1367 inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
1368 return LHS.sdiv(RHS);
1371 /// Unsigned divide APInt LHS by APInt RHS.
1372 /// @brief Unsigned division function for APInt.
1373 inline APInt udiv(const APInt& LHS, const APInt& RHS) {
1374 return LHS.udiv(RHS);
1377 /// Signed remainder operation on APInt.
1378 /// @brief Function for signed remainder operation.
1379 inline APInt srem(const APInt& LHS, const APInt& RHS) {
1380 return LHS.srem(RHS);
1383 /// Unsigned remainder operation on APInt.
1384 /// @brief Function for unsigned remainder operation.
1385 inline APInt urem(const APInt& LHS, const APInt& RHS) {
1386 return LHS.urem(RHS);
1389 /// Performs multiplication on APInt values.
1390 /// @brief Function for multiplication operation.
1391 inline APInt mul(const APInt& LHS, const APInt& RHS) {
1395 /// Performs addition on APInt values.
1396 /// @brief Function for addition operation.
1397 inline APInt add(const APInt& LHS, const APInt& RHS) {
1401 /// Performs subtraction on APInt values.
1402 /// @brief Function for subtraction operation.
1403 inline APInt sub(const APInt& LHS, const APInt& RHS) {
1407 /// Performs bitwise AND operation on APInt LHS and
1409 /// @brief Bitwise AND function for APInt.
1410 inline APInt And(const APInt& LHS, const APInt& RHS) {
1414 /// Performs bitwise OR operation on APInt LHS and APInt RHS.
1415 /// @brief Bitwise OR function for APInt.
1416 inline APInt Or(const APInt& LHS, const APInt& RHS) {
1420 /// Performs bitwise XOR operation on APInt.
1421 /// @brief Bitwise XOR function for APInt.
1422 inline APInt Xor(const APInt& LHS, const APInt& RHS) {
1426 /// Performs a bitwise complement operation on APInt.
1427 /// @brief Bitwise complement function.
1428 inline APInt Not(const APInt& APIVal) {
1432 } // End of APIntOps namespace
1434 } // End of llvm namespace