71
72
/// * In general, the class tries to follow the style of computation that LLVM
72
73
/// uses in its IR. This simplifies its use for LLVM.
74
/// @brief Class for arbitrary precision integers.
76
unsigned BitWidth; ///< The number of bits in this APInt.
76
unsigned BitWidth; ///< The number of bits in this APInt.
78
78
/// This union is used to store the integer value. When the
79
79
/// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
81
uint64_t VAL; ///< Used to store the <= 64 bits integer value.
82
uint64_t *pVal; ///< Used to store the >64 bits integer value.
81
uint64_t VAL; ///< Used to store the <= 64 bits integer value.
82
uint64_t *pVal; ///< Used to store the >64 bits integer value.
85
85
/// This enum is used to hold the constants we needed for APInt.
88
APINT_BITS_PER_WORD = static_cast<unsigned int>(sizeof(uint64_t)) *
89
static_cast<unsigned int>(sizeof(uint64_t)) * CHAR_BIT,
90
90
/// Byte size of a word
91
91
APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
94
/// \brief Fast internal constructor
94
96
/// This constructor is used only internally for speed of construction of
95
97
/// temporaries. It is unsafe for general use so it is not public.
96
/// @brief Fast internal constructor
97
APInt(uint64_t* val, unsigned bits) : BitWidth(bits), pVal(val) { }
99
/// @returns true if the number of bits <= 64, false otherwise.
100
/// @brief Determine if this APInt just has one word to store value.
101
bool isSingleWord() const {
102
return BitWidth <= APINT_BITS_PER_WORD;
105
/// @returns the word position for the specified bit position.
106
/// @brief Determine which word a bit is in.
98
APInt(uint64_t *val, unsigned bits) : BitWidth(bits), pVal(val) {}
100
/// \brief Determine if this APInt just has one word to store value.
102
/// \returns true if the number of bits <= 64, false otherwise.
103
bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; }
105
/// \brief Determine which word a bit is in.
107
/// \returns the word position for the specified bit position.
107
108
static unsigned whichWord(unsigned bitPosition) {
108
109
return bitPosition / APINT_BITS_PER_WORD;
111
/// @returns the bit position in a word for the specified bit position
112
/// \brief Determine which bit in a word a bit is in.
114
/// \returns the bit position in a word for the specified bit position
112
115
/// in the APInt.
113
/// @brief Determine which bit in a word a bit is in.
114
116
static unsigned whichBit(unsigned bitPosition) {
115
117
return bitPosition % APINT_BITS_PER_WORD;
120
/// \brief Get a single bit mask.
122
/// \returns a uint64_t with only bit at "whichBit(bitPosition)" set
118
123
/// This method generates and returns a uint64_t (word) mask for a single
119
124
/// bit at a specific bit position. This is used to mask the bit in the
120
125
/// corresponding word.
121
/// @returns a uint64_t with only bit at "whichBit(bitPosition)" set
122
/// @brief Get a single bit mask.
123
126
static uint64_t maskBit(unsigned bitPosition) {
124
127
return 1ULL << whichBit(bitPosition);
130
/// \brief Clear unused high order bits
127
132
/// This method is used internally to clear the to "N" bits in the high order
128
133
/// word that are not used by the APInt. This is needed after the most
129
134
/// significant word is assigned a value to ensure that those bits are
131
/// @brief Clear unused high order bits
132
APInt& clearUnusedBits() {
136
APInt &clearUnusedBits() {
133
137
// Compute how many bits are used in the final word
134
138
unsigned wordBits = BitWidth % APINT_BITS_PER_WORD;
135
139
if (wordBits == 0)
277
287
#if LLVM_HAS_RVALUE_REFERENCES
278
/// @brief Move Constructor.
279
APInt(APInt&& that) : BitWidth(that.BitWidth), VAL(that.VAL) {
288
/// \brief Move Constructor.
289
APInt(APInt &&that) : BitWidth(that.BitWidth), VAL(that.VAL) {
280
290
that.BitWidth = 0;
284
/// @brief Destructor.
294
/// \brief Destructor.
286
296
if (!isSingleWord())
290
/// Default constructor that creates an uninitialized APInt. This is useful
291
/// for object deserialization (pair this with the static method Read).
300
/// \brief Default constructor that creates an uninitialized APInt.
302
/// This is useful for object deserialization (pair this with the static
292
304
explicit APInt() : BitWidth(1) {}
294
/// Profile - Used to insert APInt objects, or objects that contain APInt
295
/// objects, into FoldingSets.
296
void Profile(FoldingSetNodeID& id) const;
306
/// Used to insert APInt objects, or objects that contain APInt objects, into
308
void Profile(FoldingSetNodeID &id) const;
299
/// @name Value Tests
311
/// \name Value Tests
314
/// \brief Determine sign of this APInt.
301
316
/// This tests the high bit of this APInt to determine if it is set.
302
/// @returns true if this APInt is negative, false otherwise
303
/// @brief Determine sign of this APInt.
304
bool isNegative() const {
305
return (*this)[BitWidth - 1];
318
/// \returns true if this APInt is negative, false otherwise
319
bool isNegative() const { return (*this)[BitWidth - 1]; }
321
/// \brief Determine if this APInt Value is non-negative (>= 0)
308
323
/// This tests the high bit of the APInt to determine if it is unset.
309
/// @brief Determine if this APInt Value is non-negative (>= 0)
310
bool isNonNegative() const {
311
return !isNegative();
324
bool isNonNegative() const { return !isNegative(); }
326
/// \brief Determine if this APInt Value is positive.
314
328
/// This tests if the value of this APInt is positive (> 0). Note
315
329
/// that 0 is not a positive value.
316
/// @returns true if this APInt is positive.
317
/// @brief Determine if this APInt Value is positive.
318
bool isStrictlyPositive() const {
319
return isNonNegative() && !!*this;
331
/// \returns true if this APInt is positive.
332
bool isStrictlyPositive() const { return isNonNegative() && !!*this; }
334
/// \brief Determine if all bits are set
322
336
/// This checks to see if the value has all bits of the APInt are set or not.
323
/// @brief Determine if all bits are set
324
bool isAllOnesValue() const {
325
return countPopulation() == BitWidth;
337
bool isAllOnesValue() const { return countPopulation() == BitWidth; }
339
/// \brief Determine if this is the largest unsigned value.
328
341
/// This checks to see if the value of this APInt is the maximum unsigned
329
342
/// value for the APInt's bit width.
330
/// @brief Determine if this is the largest unsigned value.
331
bool isMaxValue() const {
332
return countPopulation() == BitWidth;
343
bool isMaxValue() const { return countPopulation() == BitWidth; }
345
/// \brief Determine if this is the largest signed value.
335
347
/// This checks to see if the value of this APInt is the maximum signed
336
348
/// value for the APInt's bit width.
337
/// @brief Determine if this is the largest signed value.
338
349
bool isMaxSignedValue() const {
339
return BitWidth == 1 ? VAL == 0 :
340
!isNegative() && countPopulation() == BitWidth - 1;
350
return BitWidth == 1 ? VAL == 0
351
: !isNegative() && countPopulation() == BitWidth - 1;
354
/// \brief Determine if this is the smallest unsigned value.
343
356
/// This checks to see if the value of this APInt is the minimum unsigned
344
357
/// value for the APInt's bit width.
345
/// @brief Determine if this is the smallest unsigned value.
346
bool isMinValue() const {
358
bool isMinValue() const { return !*this; }
360
/// \brief Determine if this is the smallest signed value.
350
362
/// This checks to see if the value of this APInt is the minimum signed
351
363
/// value for the APInt's bit width.
352
/// @brief Determine if this is the smallest signed value.
353
364
bool isMinSignedValue() const {
354
365
return BitWidth == 1 ? VAL == 1 : isNegative() && isPowerOf2();
357
/// @brief Check if this APInt has an N-bits unsigned integer value.
368
/// \brief Check if this APInt has an N-bits unsigned integer value.
358
369
bool isIntN(unsigned N) const {
359
370
assert(N && "N == 0 ???");
360
371
return getActiveBits() <= N;
363
/// @brief Check if this APInt has an N-bits signed integer value.
374
/// \brief Check if this APInt has an N-bits signed integer value.
364
375
bool isSignedIntN(unsigned N) const {
365
376
assert(N && "N == 0 ???");
366
377
return getMinSignedBits() <= N;
369
/// @returns true if the argument APInt value is a power of two > 0.
380
/// \brief Check if this APInt's value is a power of two greater than zero.
382
/// \returns true if the argument APInt value is a power of two > 0.
370
383
bool isPowerOf2() const {
371
384
if (isSingleWord())
372
385
return isPowerOf2_64(VAL);
373
386
return countPopulationSlowCase() == 1;
376
/// isSignBit - Return true if this is the value returned by getSignBit.
389
/// \brief Check if the APInt's value is returned by getSignBit.
391
/// \returns true if this is the value returned by getSignBit.
377
392
bool isSignBit() const { return isMinSignedValue(); }
394
/// \brief Convert APInt to a boolean value.
379
396
/// This converts the APInt to a boolean value as a test against zero.
380
/// @brief Boolean conversion function.
381
bool getBoolValue() const {
397
bool getBoolValue() const { return !!*this; }
385
/// getLimitedValue - If this value is smaller than the specified limit,
386
/// return it, otherwise return the limit value. This causes the value
387
/// to saturate to the limit.
399
/// If this value is smaller than the specified limit, return it, otherwise
400
/// return the limit value. This causes the value to saturate to the limit.
388
401
uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
389
return (getActiveBits() > 64 || getZExtValue() > Limit) ?
390
Limit : getZExtValue();
402
return (getActiveBits() > 64 || getZExtValue() > Limit) ? Limit
394
/// @name Value Generators
407
/// \name Value Generators
396
/// @brief Gets maximum unsigned value of APInt for specific bit width.
410
/// \brief Gets maximum unsigned value of APInt for specific bit width.
397
411
static APInt getMaxValue(unsigned numBits) {
398
412
return getAllOnesValue(numBits);
401
/// @brief Gets maximum signed value of APInt for a specific bit width.
415
/// \brief Gets maximum signed value of APInt for a specific bit width.
402
416
static APInt getSignedMaxValue(unsigned numBits) {
403
417
APInt API = getAllOnesValue(numBits);
404
418
API.clearBit(numBits - 1);
408
/// @brief Gets minimum unsigned value of APInt for a specific bit width.
409
static APInt getMinValue(unsigned numBits) {
410
return APInt(numBits, 0);
422
/// \brief Gets minimum unsigned value of APInt for a specific bit width.
423
static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); }
413
/// @brief Gets minimum signed value of APInt for a specific bit width.
425
/// \brief Gets minimum signed value of APInt for a specific bit width.
414
426
static APInt getSignedMinValue(unsigned numBits) {
415
427
APInt API(numBits, 0);
416
428
API.setBit(numBits - 1);
420
/// getSignBit - This is just a wrapper function of getSignedMinValue(), and
421
/// it helps code readability when we want to get a SignBit.
422
/// @brief Get the SignBit for a specific bit width.
432
/// \brief Get the SignBit for a specific bit width.
434
/// This is just a wrapper function of getSignedMinValue(), and it helps code
435
/// readability when we want to get a SignBit.
423
436
static APInt getSignBit(unsigned BitWidth) {
424
437
return getSignedMinValue(BitWidth);
427
/// @returns the all-ones value for an APInt of the specified bit-width.
428
/// @brief Get the all-ones value.
440
/// \brief Get the all-ones value.
442
/// \returns the all-ones value for an APInt of the specified bit-width.
429
443
static APInt getAllOnesValue(unsigned numBits) {
430
444
return APInt(numBits, UINT64_MAX, true);
433
/// @returns the '0' value for an APInt of the specified bit-width.
434
/// @brief Get the '0' value.
435
static APInt getNullValue(unsigned numBits) {
436
return APInt(numBits, 0);
447
/// \brief Get the '0' value.
449
/// \returns the '0' value for an APInt of the specified bit-width.
450
static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); }
452
/// \brief Compute an APInt containing numBits highbits from this APInt.
439
454
/// Get an APInt with the same BitWidth as this APInt, just zero mask
440
455
/// the low bits and right shift to the least significant bit.
441
/// @returns the high "numBits" bits of this APInt.
457
/// \returns the high "numBits" bits of this APInt.
442
458
APInt getHiBits(unsigned numBits) const;
460
/// \brief Compute an APInt containing numBits lowbits from this APInt.
444
462
/// Get an APInt with the same BitWidth as this APInt, just zero mask
445
463
/// the high bits.
446
/// @returns the low "numBits" bits of this APInt.
465
/// \returns the low "numBits" bits of this APInt.
447
466
APInt getLoBits(unsigned numBits) const;
449
/// getOneBitSet - Return an APInt with exactly one bit set in the result.
468
/// \brief Return an APInt with exactly one bit set in the result.
450
469
static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
451
470
APInt Res(numBits, 0);
452
471
Res.setBit(BitNo);
475
/// \brief Get a value with a block of bits set.
456
477
/// Constructs an APInt value that has a contiguous range of bits set. The
457
478
/// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
458
479
/// bits will be zero. For example, with parameters(32, 0, 16) you would get
459
480
/// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For
460
481
/// example, with parameters (32, 28, 4), you would get 0xF000000F.
461
/// @param numBits the intended bit width of the result
462
/// @param loBit the index of the lowest bit set.
463
/// @param hiBit the index of the highest bit set.
464
/// @returns An APInt value with the requested bits set.
465
/// @brief Get a value with a block of bits set.
483
/// \param numBits the intended bit width of the result
484
/// \param loBit the index of the lowest bit set.
485
/// \param hiBit the index of the highest bit set.
487
/// \returns An APInt value with the requested bits set.
466
488
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
467
489
assert(hiBit <= numBits && "hiBit out of range");
468
490
assert(loBit < numBits && "loBit out of range");
469
491
if (hiBit < loBit)
470
492
return getLowBitsSet(numBits, hiBit) |
471
getHighBitsSet(numBits, numBits-loBit);
472
return getLowBitsSet(numBits, hiBit-loBit).shl(loBit);
493
getHighBitsSet(numBits, numBits - loBit);
494
return getLowBitsSet(numBits, hiBit - loBit).shl(loBit);
497
/// \brief Get a value with high bits set
475
499
/// Constructs an APInt value that has the top hiBitsSet bits set.
476
/// @param numBits the bitwidth of the result
477
/// @param hiBitsSet the number of high-order bits set in the result.
478
/// @brief Get a value with high bits set
501
/// \param numBits the bitwidth of the result
502
/// \param hiBitsSet the number of high-order bits set in the result.
479
503
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
480
504
assert(hiBitsSet <= numBits && "Too many bits to set!");
481
505
// Handle a degenerate case, to avoid shifting by word size
707
/// \brief Bitwise XOR assignment operator.
664
709
/// Performs a bitwise XOR operation on this APInt and RHS. The result is
665
710
/// assigned to *this.
666
/// @returns *this after XORing with RHS.
667
/// @brief Bitwise XOR assignment operator.
668
APInt& operator^=(const APInt& RHS);
712
/// \returns *this after XORing with RHS.
713
APInt &operator^=(const APInt &RHS);
715
/// \brief Multiplication assignment operator.
670
717
/// Multiplies this APInt by RHS and assigns the result to *this.
672
/// @brief Multiplication assignment operator.
673
APInt& operator*=(const APInt& RHS);
720
APInt &operator*=(const APInt &RHS);
722
/// \brief Addition assignment operator.
675
724
/// Adds RHS to *this and assigns the result to *this.
677
/// @brief Addition assignment operator.
678
APInt& operator+=(const APInt& RHS);
727
APInt &operator+=(const APInt &RHS);
729
/// \brief Subtraction assignment operator.
680
731
/// Subtracts RHS from *this and assigns the result to *this.
682
/// @brief Subtraction assignment operator.
683
APInt& operator-=(const APInt& RHS);
734
APInt &operator-=(const APInt &RHS);
736
/// \brief Left-shift assignment function.
685
738
/// Shifts *this left by shiftAmt and assigns the result to *this.
686
/// @returns *this after shifting left by shiftAmt
687
/// @brief Left-shift assignment function.
688
APInt& operator<<=(unsigned shiftAmt) {
740
/// \returns *this after shifting left by shiftAmt
741
APInt &operator<<=(unsigned shiftAmt) {
689
742
*this = shl(shiftAmt);
694
/// @name Binary Operators
747
/// \name Binary Operators
750
/// \brief Bitwise AND operator.
696
752
/// Performs a bitwise AND operation on *this and RHS.
697
/// @returns An APInt value representing the bitwise AND of *this and RHS.
698
/// @brief Bitwise AND operator.
699
APInt operator&(const APInt& RHS) const {
754
/// \returns An APInt value representing the bitwise AND of *this and RHS.
755
APInt operator&(const APInt &RHS) const {
700
756
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
701
757
if (isSingleWord())
702
758
return APInt(getBitWidth(), VAL & RHS.VAL);
703
759
return AndSlowCase(RHS);
705
APInt And(const APInt& RHS) const {
706
return this->operator&(RHS);
761
APInt And(const APInt &RHS) const { return this->operator&(RHS); }
763
/// \brief Bitwise OR operator.
709
765
/// Performs a bitwise OR operation on *this and RHS.
710
/// @returns An APInt value representing the bitwise OR of *this and RHS.
711
/// @brief Bitwise OR operator.
712
APInt operator|(const APInt& RHS) const {
767
/// \returns An APInt value representing the bitwise OR of *this and RHS.
768
APInt operator|(const APInt &RHS) const {
713
769
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
714
770
if (isSingleWord())
715
771
return APInt(getBitWidth(), VAL | RHS.VAL);
716
772
return OrSlowCase(RHS);
718
APInt Or(const APInt& RHS) const {
719
return this->operator|(RHS);
775
/// \brief Bitwise OR function.
777
/// Performs a bitwise or on *this and RHS. This is implemented bny simply
778
/// calling operator|.
780
/// \returns An APInt value representing the bitwise OR of *this and RHS.
781
APInt Or(const APInt &RHS) const { return this->operator|(RHS); }
783
/// \brief Bitwise XOR operator.
722
785
/// Performs a bitwise XOR operation on *this and RHS.
723
/// @returns An APInt value representing the bitwise XOR of *this and RHS.
724
/// @brief Bitwise XOR operator.
725
APInt operator^(const APInt& RHS) const {
787
/// \returns An APInt value representing the bitwise XOR of *this and RHS.
788
APInt operator^(const APInt &RHS) const {
726
789
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
727
790
if (isSingleWord())
728
791
return APInt(BitWidth, VAL ^ RHS.VAL);
729
792
return XorSlowCase(RHS);
731
APInt Xor(const APInt& RHS) const {
732
return this->operator^(RHS);
795
/// \brief Bitwise XOR function.
797
/// Performs a bitwise XOR operation on *this and RHS. This is implemented
798
/// through the usage of operator^.
800
/// \returns An APInt value representing the bitwise XOR of *this and RHS.
801
APInt Xor(const APInt &RHS) const { return this->operator^(RHS); }
803
/// \brief Multiplication operator.
735
805
/// Multiplies this APInt by RHS and returns the result.
736
/// @brief Multiplication operator.
737
APInt operator*(const APInt& RHS) const;
806
APInt operator*(const APInt &RHS) const;
808
/// \brief Addition operator.
739
810
/// Adds RHS to this APInt and returns the result.
740
/// @brief Addition operator.
741
APInt operator+(const APInt& RHS) const;
742
APInt operator+(uint64_t RHS) const {
743
return (*this) + APInt(BitWidth, RHS);
811
APInt operator+(const APInt &RHS) const;
812
APInt operator+(uint64_t RHS) const { return (*this) + APInt(BitWidth, RHS); }
814
/// \brief Subtraction operator.
746
816
/// Subtracts RHS from this APInt and returns the result.
747
/// @brief Subtraction operator.
748
APInt operator-(const APInt& RHS) const;
749
APInt operator-(uint64_t RHS) const {
750
return (*this) - APInt(BitWidth, RHS);
753
APInt operator<<(unsigned Bits) const {
757
APInt operator<<(const APInt &Bits) const {
817
APInt operator-(const APInt &RHS) const;
818
APInt operator-(uint64_t RHS) const { return (*this) - APInt(BitWidth, RHS); }
820
/// \brief Left logical shift operator.
822
/// Shifts this APInt left by \p Bits and returns the result.
823
APInt operator<<(unsigned Bits) const { return shl(Bits); }
825
/// \brief Left logical shift operator.
827
/// Shifts this APInt left by \p Bits and returns the result.
828
APInt operator<<(const APInt &Bits) const { return shl(Bits); }
830
/// \brief Arithmetic right-shift function.
761
832
/// Arithmetic right-shift this APInt by shiftAmt.
762
/// @brief Arithmetic right-shift function.
763
833
APInt ashr(unsigned shiftAmt) const;
835
/// \brief Logical right-shift function.
765
837
/// Logical right-shift this APInt by shiftAmt.
766
/// @brief Logical right-shift function.
767
838
APInt lshr(unsigned shiftAmt) const;
840
/// \brief Left-shift function.
769
842
/// Left-shift this APInt by shiftAmt.
770
/// @brief Left-shift function.
771
843
APInt shl(unsigned shiftAmt) const {
772
844
assert(shiftAmt <= BitWidth && "Invalid shift amount");
773
845
if (isSingleWord()) {
778
850
return shlSlowCase(shiftAmt);
781
/// @brief Rotate left by rotateAmt.
853
/// \brief Rotate left by rotateAmt.
782
854
APInt rotl(unsigned rotateAmt) const;
784
/// @brief Rotate right by rotateAmt.
856
/// \brief Rotate right by rotateAmt.
785
857
APInt rotr(unsigned rotateAmt) const;
859
/// \brief Arithmetic right-shift function.
787
861
/// Arithmetic right-shift this APInt by shiftAmt.
788
/// @brief Arithmetic right-shift function.
789
862
APInt ashr(const APInt &shiftAmt) const;
864
/// \brief Logical right-shift function.
791
866
/// Logical right-shift this APInt by shiftAmt.
792
/// @brief Logical right-shift function.
793
867
APInt lshr(const APInt &shiftAmt) const;
869
/// \brief Left-shift function.
795
871
/// Left-shift this APInt by shiftAmt.
796
/// @brief Left-shift function.
797
872
APInt shl(const APInt &shiftAmt) const;
799
/// @brief Rotate left by rotateAmt.
874
/// \brief Rotate left by rotateAmt.
800
875
APInt rotl(const APInt &rotateAmt) const;
802
/// @brief Rotate right by rotateAmt.
877
/// \brief Rotate right by rotateAmt.
803
878
APInt rotr(const APInt &rotateAmt) const;
880
/// \brief Unsigned division operation.
805
882
/// Perform an unsigned divide operation on this APInt by RHS. Both this and
806
883
/// RHS are treated as unsigned quantities for purposes of this division.
807
/// @returns a new APInt value containing the division result
808
/// @brief Unsigned division operation.
885
/// \returns a new APInt value containing the division result
809
886
APInt udiv(const APInt &RHS) const;
888
/// \brief Signed division function for APInt.
811
890
/// Signed divide this APInt by APInt RHS.
812
/// @brief Signed division function for APInt.
813
891
APInt sdiv(const APInt &RHS) const;
893
/// \brief Unsigned remainder operation.
815
895
/// Perform an unsigned remainder operation on this APInt with RHS being the
816
896
/// divisor. Both this and RHS are treated as unsigned quantities for purposes
817
/// of this operation. Note that this is a true remainder operation and not
818
/// a modulo operation because the sign follows the sign of the dividend
820
/// @returns a new APInt value containing the remainder result
821
/// @brief Unsigned remainder operation.
897
/// of this operation. Note that this is a true remainder operation and not a
898
/// modulo operation because the sign follows the sign of the dividend which
901
/// \returns a new APInt value containing the remainder result
822
902
APInt urem(const APInt &RHS) const;
904
/// \brief Function for signed remainder operation.
824
906
/// Signed remainder operation on APInt.
825
/// @brief Function for signed remainder operation.
826
907
APInt srem(const APInt &RHS) const;
909
/// \brief Dual division/remainder interface.
828
911
/// Sometimes it is convenient to divide two APInt values and obtain both the
829
912
/// quotient and remainder. This function does both operations in the same
830
913
/// computation making it a little more efficient. The pair of input arguments
831
914
/// may overlap with the pair of output arguments. It is safe to call
832
915
/// udivrem(X, Y, X, Y), for example.
833
/// @brief Dual division/remainder interface.
834
static void udivrem(const APInt &LHS, const APInt &RHS,
835
APInt &Quotient, APInt &Remainder);
837
static void sdivrem(const APInt &LHS, const APInt &RHS,
838
APInt &Quotient, APInt &Remainder);
916
static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
919
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
841
922
// Operations that return overflow indicators.
842
923
APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
848
929
APInt umul_ov(const APInt &RHS, bool &Overflow) const;
849
930
APInt sshl_ov(unsigned Amt, bool &Overflow) const;
851
/// @returns the bit value at bitPosition
852
/// @brief Array-indexing support.
932
/// \brief Array-indexing support.
934
/// \returns the bit value at bitPosition
853
935
bool operator[](unsigned bitPosition) const {
854
936
assert(bitPosition < getBitWidth() && "Bit position out of bounds!");
855
937
return (maskBit(bitPosition) &
856
(isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) != 0;
938
(isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) !=
860
/// @name Comparison Operators
943
/// \name Comparison Operators
946
/// \brief Equality operator.
862
948
/// Compares this APInt with RHS for the validity of the equality
863
949
/// relationship.
864
/// @brief Equality operator.
865
bool operator==(const APInt& RHS) const {
950
bool operator==(const APInt &RHS) const {
866
951
assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
867
952
if (isSingleWord())
868
953
return VAL == RHS.VAL;
869
954
return EqualSlowCase(RHS);
957
/// \brief Equality operator.
872
959
/// Compares this APInt with a uint64_t for the validity of the equality
873
960
/// relationship.
874
/// @returns true if *this == Val
875
/// @brief Equality operator.
962
/// \returns true if *this == Val
876
963
bool operator==(uint64_t Val) const {
877
964
if (isSingleWord())
878
965
return VAL == Val;
879
966
return EqualSlowCase(Val);
969
/// \brief Equality comparison.
882
971
/// Compares this APInt with RHS for the validity of the equality
883
972
/// relationship.
884
/// @returns true if *this == Val
885
/// @brief Equality comparison.
886
bool eq(const APInt &RHS) const {
887
return (*this) == RHS;
974
/// \returns true if *this == Val
975
bool eq(const APInt &RHS) const { return (*this) == RHS; }
977
/// \brief Inequality operator.
890
979
/// Compares this APInt with RHS for the validity of the inequality
891
980
/// relationship.
892
/// @returns true if *this != Val
893
/// @brief Inequality operator.
894
bool operator!=(const APInt& RHS) const {
895
return !((*this) == RHS);
982
/// \returns true if *this != Val
983
bool operator!=(const APInt &RHS) const { return !((*this) == RHS); }
985
/// \brief Inequality operator.
898
987
/// Compares this APInt with a uint64_t for the validity of the inequality
899
988
/// relationship.
900
/// @returns true if *this != Val
901
/// @brief Inequality operator.
902
bool operator!=(uint64_t Val) const {
903
return !((*this) == Val);
990
/// \returns true if *this != Val
991
bool operator!=(uint64_t Val) const { return !((*this) == Val); }
993
/// \brief Inequality comparison
906
995
/// Compares this APInt with RHS for the validity of the inequality
907
996
/// relationship.
908
/// @returns true if *this != Val
909
/// @brief Inequality comparison
910
bool ne(const APInt &RHS) const {
911
return !((*this) == RHS);
998
/// \returns true if *this != Val
999
bool ne(const APInt &RHS) const { return !((*this) == RHS); }
1001
/// \brief Unsigned less than comparison
914
1003
/// Regards both *this and RHS as unsigned quantities and compares them for
915
1004
/// the validity of the less-than relationship.
916
/// @returns true if *this < RHS when both are considered unsigned.
917
/// @brief Unsigned less than comparison
1006
/// \returns true if *this < RHS when both are considered unsigned.
918
1007
bool ult(const APInt &RHS) const;
1009
/// \brief Unsigned less than comparison
920
1011
/// Regards both *this as an unsigned quantity and compares it with RHS for
921
1012
/// the validity of the less-than relationship.
922
/// @returns true if *this < RHS when considered unsigned.
923
/// @brief Unsigned less than comparison
924
bool ult(uint64_t RHS) const {
925
return ult(APInt(getBitWidth(), RHS));
1014
/// \returns true if *this < RHS when considered unsigned.
1015
bool ult(uint64_t RHS) const { return ult(APInt(getBitWidth(), RHS)); }
1017
/// \brief Signed less than comparison
928
1019
/// Regards both *this and RHS as signed quantities and compares them for
929
1020
/// validity of the less-than relationship.
930
/// @returns true if *this < RHS when both are considered signed.
931
/// @brief Signed less than comparison
932
bool slt(const APInt& RHS) const;
1022
/// \returns true if *this < RHS when both are considered signed.
1023
bool slt(const APInt &RHS) const;
1025
/// \brief Signed less than comparison
934
1027
/// Regards both *this as a signed quantity and compares it with RHS for
935
1028
/// the validity of the less-than relationship.
936
/// @returns true if *this < RHS when considered signed.
937
/// @brief Signed less than comparison
938
bool slt(uint64_t RHS) const {
939
return slt(APInt(getBitWidth(), RHS));
942
/// Regards both *this and RHS as unsigned quantities and compares them for
943
/// validity of the less-or-equal relationship.
944
/// @returns true if *this <= RHS when both are considered unsigned.
945
/// @brief Unsigned less or equal comparison
946
bool ule(const APInt& RHS) const {
947
return ult(RHS) || eq(RHS);
950
/// Regards both *this as an unsigned quantity and compares it with RHS for
951
/// the validity of the less-or-equal relationship.
952
/// @returns true if *this <= RHS when considered unsigned.
953
/// @brief Unsigned less or equal comparison
954
bool ule(uint64_t RHS) const {
955
return ule(APInt(getBitWidth(), RHS));
958
/// Regards both *this and RHS as signed quantities and compares them for
959
/// validity of the less-or-equal relationship.
960
/// @returns true if *this <= RHS when both are considered signed.
961
/// @brief Signed less or equal comparison
962
bool sle(const APInt& RHS) const {
963
return slt(RHS) || eq(RHS);
966
/// Regards both *this as a signed quantity and compares it with RHS for
967
/// the validity of the less-or-equal relationship.
968
/// @returns true if *this <= RHS when considered signed.
969
/// @brief Signed less or equal comparison
970
bool sle(uint64_t RHS) const {
971
return sle(APInt(getBitWidth(), RHS));
974
/// Regards both *this and RHS as unsigned quantities and compares them for
975
/// the validity of the greater-than relationship.
976
/// @returns true if *this > RHS when both are considered unsigned.
977
/// @brief Unsigned greather than comparison
978
bool ugt(const APInt& RHS) const {
979
return !ult(RHS) && !eq(RHS);
982
/// Regards both *this as an unsigned quantity and compares it with RHS for
983
/// the validity of the greater-than relationship.
984
/// @returns true if *this > RHS when considered unsigned.
985
/// @brief Unsigned greater than comparison
986
bool ugt(uint64_t RHS) const {
987
return ugt(APInt(getBitWidth(), RHS));
990
/// Regards both *this and RHS as signed quantities and compares them for
991
/// the validity of the greater-than relationship.
992
/// @returns true if *this > RHS when both are considered signed.
993
/// @brief Signed greather than comparison
994
bool sgt(const APInt& RHS) const {
995
return !slt(RHS) && !eq(RHS);
998
/// Regards both *this as a signed quantity and compares it with RHS for
999
/// the validity of the greater-than relationship.
1000
/// @returns true if *this > RHS when considered signed.
1001
/// @brief Signed greater than comparison
1002
bool sgt(uint64_t RHS) const {
1003
return sgt(APInt(getBitWidth(), RHS));
1006
/// Regards both *this and RHS as unsigned quantities and compares them for
1007
/// validity of the greater-or-equal relationship.
1008
/// @returns true if *this >= RHS when both are considered unsigned.
1009
/// @brief Unsigned greater or equal comparison
1010
bool uge(const APInt& RHS) const {
1014
/// Regards both *this as an unsigned quantity and compares it with RHS for
1015
/// the validity of the greater-or-equal relationship.
1016
/// @returns true if *this >= RHS when considered unsigned.
1017
/// @brief Unsigned greater or equal comparison
1018
bool uge(uint64_t RHS) const {
1019
return uge(APInt(getBitWidth(), RHS));
1022
/// Regards both *this and RHS as signed quantities and compares them for
1023
/// validity of the greater-or-equal relationship.
1024
/// @returns true if *this >= RHS when both are considered signed.
1025
/// @brief Signed greather or equal comparison
1026
bool sge(const APInt& RHS) const {
1030
/// Regards both *this as a signed quantity and compares it with RHS for
1031
/// the validity of the greater-or-equal relationship.
1032
/// @returns true if *this >= RHS when considered signed.
1033
/// @brief Signed greater or equal comparison
1034
bool sge(uint64_t RHS) const {
1035
return sge(APInt(getBitWidth(), RHS));
1030
/// \returns true if *this < RHS when considered signed.
1031
bool slt(uint64_t RHS) const { return slt(APInt(getBitWidth(), RHS)); }
1033
/// \brief Unsigned less or equal comparison
1035
/// Regards both *this and RHS as unsigned quantities and compares them for
1036
/// validity of the less-or-equal relationship.
1038
/// \returns true if *this <= RHS when both are considered unsigned.
1039
bool ule(const APInt &RHS) const { return ult(RHS) || eq(RHS); }
1041
/// \brief Unsigned less or equal comparison
1043
/// Regards both *this as an unsigned quantity and compares it with RHS for
1044
/// the validity of the less-or-equal relationship.
1046
/// \returns true if *this <= RHS when considered unsigned.
1047
bool ule(uint64_t RHS) const { return ule(APInt(getBitWidth(), RHS)); }
1049
/// \brief Signed less or equal comparison
1051
/// Regards both *this and RHS as signed quantities and compares them for
1052
/// validity of the less-or-equal relationship.
1054
/// \returns true if *this <= RHS when both are considered signed.
1055
bool sle(const APInt &RHS) const { return slt(RHS) || eq(RHS); }
1057
/// \brief Signed less or equal comparison
1059
/// Regards both *this as a signed quantity and compares it with RHS for the
1060
/// validity of the less-or-equal relationship.
1062
/// \returns true if *this <= RHS when considered signed.
1063
bool sle(uint64_t RHS) const { return sle(APInt(getBitWidth(), RHS)); }
1065
/// \brief Unsigned greather than comparison
1067
/// Regards both *this and RHS as unsigned quantities and compares them for
1068
/// the validity of the greater-than relationship.
1070
/// \returns true if *this > RHS when both are considered unsigned.
1071
bool ugt(const APInt &RHS) const { return !ult(RHS) && !eq(RHS); }
1073
/// \brief Unsigned greater than comparison
1075
/// Regards both *this as an unsigned quantity and compares it with RHS for
1076
/// the validity of the greater-than relationship.
1078
/// \returns true if *this > RHS when considered unsigned.
1079
bool ugt(uint64_t RHS) const { return ugt(APInt(getBitWidth(), RHS)); }
1081
/// \brief Signed greather than comparison
1083
/// Regards both *this and RHS as signed quantities and compares them for the
1084
/// validity of the greater-than relationship.
1086
/// \returns true if *this > RHS when both are considered signed.
1087
bool sgt(const APInt &RHS) const { return !slt(RHS) && !eq(RHS); }
1089
/// \brief Signed greater than comparison
1091
/// Regards both *this as a signed quantity and compares it with RHS for
1092
/// the validity of the greater-than relationship.
1094
/// \returns true if *this > RHS when considered signed.
1095
bool sgt(uint64_t RHS) const { return sgt(APInt(getBitWidth(), RHS)); }
1097
/// \brief Unsigned greater or equal comparison
1099
/// Regards both *this and RHS as unsigned quantities and compares them for
1100
/// validity of the greater-or-equal relationship.
1102
/// \returns true if *this >= RHS when both are considered unsigned.
1103
bool uge(const APInt &RHS) const { return !ult(RHS); }
1105
/// \brief Unsigned greater or equal comparison
1107
/// Regards both *this as an unsigned quantity and compares it with RHS for
1108
/// the validity of the greater-or-equal relationship.
1110
/// \returns true if *this >= RHS when considered unsigned.
1111
bool uge(uint64_t RHS) const { return uge(APInt(getBitWidth(), RHS)); }
1113
/// \brief Signed greather or equal comparison
1115
/// Regards both *this and RHS as signed quantities and compares them for
1116
/// validity of the greater-or-equal relationship.
1118
/// \returns true if *this >= RHS when both are considered signed.
1119
bool sge(const APInt &RHS) const { return !slt(RHS); }
1121
/// \brief Signed greater or equal comparison
1123
/// Regards both *this as a signed quantity and compares it with RHS for
1124
/// the validity of the greater-or-equal relationship.
1126
/// \returns true if *this >= RHS when considered signed.
1127
bool sge(uint64_t RHS) const { return sge(APInt(getBitWidth(), RHS)); }
1041
1129
/// This operation tests if there are any pairs of corresponding bits
1042
1130
/// between this APInt and RHS that are both set.
1043
bool intersects(const APInt &RHS) const {
1044
return (*this & RHS) != 0;
1131
bool intersects(const APInt &RHS) const { return (*this & RHS) != 0; }
1048
/// @name Resizing Operators
1134
/// \name Resizing Operators
1137
/// \brief Truncate to new width.
1050
1139
/// Truncate the APInt to a specified width. It is an error to specify a width
1051
1140
/// that is greater than or equal to the current width.
1052
/// @brief Truncate to new width.
1053
1141
APInt trunc(unsigned width) const;
1143
/// \brief Sign extend to a new width.
1055
1145
/// This operation sign extends the APInt to a new width. If the high order
1056
1146
/// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
1057
1147
/// It is an error to specify a width that is less than or equal to the
1058
1148
/// current width.
1059
/// @brief Sign extend to a new width.
1060
1149
APInt sext(unsigned width) const;
1151
/// \brief Zero extend to a new width.
1062
1153
/// This operation zero extends the APInt to a new width. The high order bits
1063
1154
/// are filled with 0 bits. It is an error to specify a width that is less
1064
1155
/// than or equal to the current width.
1065
/// @brief Zero extend to a new width.
1066
1156
APInt zext(unsigned width) const;
1158
/// \brief Sign extend or truncate to width
1068
1160
/// Make this APInt have the bit width given by \p width. The value is sign
1069
1161
/// extended, truncated, or left alone to make it that width.
1070
/// @brief Sign extend or truncate to width
1071
1162
APInt sextOrTrunc(unsigned width) const;
1164
/// \brief Zero extend or truncate to width
1073
1166
/// Make this APInt have the bit width given by \p width. The value is zero
1074
1167
/// extended, truncated, or left alone to make it that width.
1075
/// @brief Zero extend or truncate to width
1076
1168
APInt zextOrTrunc(unsigned width) const;
1170
/// \brief Sign extend or truncate to width
1078
1172
/// Make this APInt have the bit width given by \p width. The value is sign
1079
1173
/// extended, or left alone to make it that width.
1080
/// @brief Sign extend or truncate to width
1081
1174
APInt sextOrSelf(unsigned width) const;
1176
/// \brief Zero extend or truncate to width
1083
1178
/// Make this APInt have the bit width given by \p width. The value is zero
1084
1179
/// extended, or left alone to make it that width.
1085
/// @brief Zero extend or truncate to width
1086
1180
APInt zextOrSelf(unsigned width) const;
1089
/// @name Bit Manipulation Operators
1183
/// \name Bit Manipulation Operators
1091
/// @brief Set every bit to 1.
1186
/// \brief Set every bit to 1.
1092
1187
void setAllBits() {
1093
1188
if (isSingleWord())
1094
1189
VAL = UINT64_MAX;
1128
1225
clearUnusedBits();
1228
/// \brief Toggles a given bit to its opposite value.
1131
1230
/// Toggle a given bit to its opposite value whose position is given
1132
1231
/// as "bitPosition".
1133
/// @brief Toggles a given bit to its opposite value.
1134
1232
void flipBit(unsigned bitPosition);
1137
/// @name Value Characterization Functions
1235
/// \name Value Characterization Functions
1140
/// @returns the total number of bits.
1141
unsigned getBitWidth() const {
1145
/// Here one word's bitwidth equals to that of uint64_t.
1146
/// @returns the number of words to hold the integer value of this APInt.
1147
/// @brief Get the number of words.
1148
unsigned getNumWords() const {
1149
return getNumWords(BitWidth);
1152
/// Here one word's bitwidth equals to that of uint64_t.
1153
/// @returns the number of words to hold the integer value with a
1154
/// given bit width.
1155
/// @brief Get the number of words.
1238
/// \brief Return the number of bits in the APInt.
1239
unsigned getBitWidth() const { return BitWidth; }
1241
/// \brief Get the number of words.
1243
/// Here one word's bitwidth equals to that of uint64_t.
1245
/// \returns the number of words to hold the integer value of this APInt.
1246
unsigned getNumWords() const { return getNumWords(BitWidth); }
1248
/// \brief Get the number of words.
1250
/// *NOTE* Here one word's bitwidth equals to that of uint64_t.
1252
/// \returns the number of words to hold the integer value with a given bit
1156
1254
static unsigned getNumWords(unsigned BitWidth) {
1157
1255
return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
1258
/// \brief Compute the number of active bits in the value
1160
1260
/// This function returns the number of active bits which is defined as the
1161
1261
/// bit width minus the number of leading zeros. This is used in several
1162
1262
/// computations to see how "wide" the value is.
1163
/// @brief Compute the number of active bits in the value
1164
unsigned getActiveBits() const {
1165
return BitWidth - countLeadingZeros();
1263
unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); }
1168
/// This function returns the number of active words in the value of this
1169
/// APInt. This is used in conjunction with getActiveData to extract the raw
1170
/// value of the APInt.
1265
/// \brief Compute the number of active words in the value of this APInt.
1267
/// This is used in conjunction with getActiveData to extract the raw value of
1171
1269
unsigned getActiveWords() const {
1172
1270
unsigned numActiveBits = getActiveBits();
1173
1271
return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1;
1176
/// Computes the minimum bit width for this APInt while considering it to be
1177
/// a signed (and probably negative) value. If the value is not negative,
1178
/// this function returns the same value as getActiveBits()+1. Otherwise, it
1274
/// \brief Get the minimum bit size for this signed APInt
1276
/// Computes the minimum bit width for this APInt while considering it to be a
1277
/// signed (and probably negative) value. If the value is not negative, this
1278
/// function returns the same value as getActiveBits()+1. Otherwise, it
1179
1279
/// returns the smallest bit width that will retain the negative value. For
1180
1280
/// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
1181
1281
/// for -1, this function will always return 1.
1182
/// @brief Get the minimum bit size for this signed APInt
1183
1282
unsigned getMinSignedBits() const {
1184
1283
if (isNegative())
1185
1284
return BitWidth - countLeadingOnes() + 1;
1186
return getActiveBits()+1;
1285
return getActiveBits() + 1;
1288
/// \brief Get zero extended value
1189
1290
/// This method attempts to return the value of this APInt as a zero extended
1190
1291
/// uint64_t. The bitwidth must be <= 64 or the value must fit within a
1191
1292
/// uint64_t. Otherwise an assertion will result.
1192
/// @brief Get zero extended value
1193
1293
uint64_t getZExtValue() const {
1194
1294
if (isSingleWord())
1456
1563
/// Clear the given bit of a bignum. Zero-based.
1457
1564
static void tcClearBit(integerPart *, unsigned int bit);
1459
/// Returns the bit number of the least or most significant set bit
1460
/// of a number. If the input number has no bits set -1U is
1566
/// Returns the bit number of the least or most significant set bit of a
1567
/// number. If the input number has no bits set -1U is returned.
1462
1568
static unsigned int tcLSB(const integerPart *, unsigned int);
1463
1569
static unsigned int tcMSB(const integerPart *parts, unsigned int n);
1465
1571
/// Negate a bignum in-place.
1466
1572
static void tcNegate(integerPart *, unsigned int);
1468
/// DST += RHS + CARRY where CARRY is zero or one. Returns the
1574
/// DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1470
1575
static integerPart tcAdd(integerPart *, const integerPart *,
1471
1576
integerPart carry, unsigned);
1473
/// DST -= RHS + CARRY where CARRY is zero or one. Returns the
1578
/// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1475
1579
static integerPart tcSubtract(integerPart *, const integerPart *,
1476
1580
integerPart carry, unsigned);
1478
/// DST += SRC * MULTIPLIER + PART if add is true
1479
/// DST = SRC * MULTIPLIER + PART if add is false
1481
/// Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC
1482
/// they must start at the same point, i.e. DST == SRC.
1484
/// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is
1485
/// returned. Otherwise DST is filled with the least significant
1486
/// DSTPARTS parts of the result, and if all of the omitted higher
1487
/// parts were zero return zero, otherwise overflow occurred and
1582
/// DST += SRC * MULTIPLIER + PART if add is true
1583
/// DST = SRC * MULTIPLIER + PART if add is false
1585
/// Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC they must
1586
/// start at the same point, i.e. DST == SRC.
1588
/// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned.
1589
/// Otherwise DST is filled with the least significant DSTPARTS parts of the
1590
/// result, and if all of the omitted higher parts were zero return zero,
1591
/// otherwise overflow occurred and return one.
1489
1592
static int tcMultiplyPart(integerPart *dst, const integerPart *src,
1490
1593
integerPart multiplier, integerPart carry,
1491
1594
unsigned int srcParts, unsigned int dstParts,
1494
/// DST = LHS * RHS, where DST has the same width as the operands
1495
/// and is filled with the least significant parts of the result.
1496
/// Returns one if overflow occurred, otherwise zero. DST must be
1497
/// disjoint from both operands.
1498
static int tcMultiply(integerPart *, const integerPart *,
1499
const integerPart *, unsigned);
1597
/// DST = LHS * RHS, where DST has the same width as the operands and is
1598
/// filled with the least significant parts of the result. Returns one if
1599
/// overflow occurred, otherwise zero. DST must be disjoint from both
1601
static int tcMultiply(integerPart *, const integerPart *, const integerPart *,
1501
/// DST = LHS * RHS, where DST has width the sum of the widths of
1502
/// the operands. No overflow occurs. DST must be disjoint from
1503
/// both operands. Returns the number of parts required to hold the
1604
/// DST = LHS * RHS, where DST has width the sum of the widths of the
1605
/// operands. No overflow occurs. DST must be disjoint from both
1606
/// operands. Returns the number of parts required to hold the result.
1505
1607
static unsigned int tcFullMultiply(integerPart *, const integerPart *,
1506
1608
const integerPart *, unsigned, unsigned);
1508
1610
/// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1509
/// Otherwise set LHS to LHS / RHS with the fractional part
1510
/// discarded, set REMAINDER to the remainder, return zero. i.e.
1611
/// Otherwise set LHS to LHS / RHS with the fractional part discarded, set
1612
/// REMAINDER to the remainder, return zero. i.e.
1512
1614
/// OLD_LHS = RHS * LHS + REMAINDER
1514
/// SCRATCH is a bignum of the same size as the operands and result
1515
/// for use by the routine; its contents need not be initialized
1516
/// and are destroyed. LHS, REMAINDER and SCRATCH must be
1616
/// SCRATCH is a bignum of the same size as the operands and result for use by
1617
/// the routine; its contents need not be initialized and are destroyed. LHS,
1618
/// REMAINDER and SCRATCH must be distinct.
1518
1619
static int tcDivide(integerPart *lhs, const integerPart *rhs,
1519
1620
integerPart *remainder, integerPart *scratch,
1520
1621
unsigned int parts);
1522
/// Shift a bignum left COUNT bits. Shifted in bits are zero.
1523
/// There are no restrictions on COUNT.
1623
/// Shift a bignum left COUNT bits. Shifted in bits are zero. There are no
1624
/// restrictions on COUNT.
1524
1625
static void tcShiftLeft(integerPart *, unsigned int parts,
1525
1626
unsigned int count);
1527
/// Shift a bignum right COUNT bits. Shifted in bits are zero.
1528
/// There are no restrictions on COUNT.
1628
/// Shift a bignum right COUNT bits. Shifted in bits are zero. There are no
1629
/// restrictions on COUNT.
1529
1630
static void tcShiftRight(integerPart *, unsigned int parts,
1530
1631
unsigned int count);
1581
1677
namespace APIntOps {
1583
/// @brief Determine the smaller of two APInts considered to be signed.
1584
inline APInt smin(const APInt &A, const APInt &B) {
1585
return A.slt(B) ? A : B;
1588
/// @brief Determine the larger of two APInts considered to be signed.
1589
inline APInt smax(const APInt &A, const APInt &B) {
1590
return A.sgt(B) ? A : B;
1593
/// @brief Determine the smaller of two APInts considered to be signed.
1594
inline APInt umin(const APInt &A, const APInt &B) {
1595
return A.ult(B) ? A : B;
1598
/// @brief Determine the larger of two APInts considered to be unsigned.
1599
inline APInt umax(const APInt &A, const APInt &B) {
1600
return A.ugt(B) ? A : B;
1603
/// @brief Check if the specified APInt has a N-bits unsigned integer value.
1604
inline bool isIntN(unsigned N, const APInt& APIVal) {
1605
return APIVal.isIntN(N);
1608
/// @brief Check if the specified APInt has a N-bits signed integer value.
1609
inline bool isSignedIntN(unsigned N, const APInt& APIVal) {
1679
/// \brief Determine the smaller of two APInts considered to be signed.
1680
inline APInt smin(const APInt &A, const APInt &B) { return A.slt(B) ? A : B; }
1682
/// \brief Determine the larger of two APInts considered to be signed.
1683
inline APInt smax(const APInt &A, const APInt &B) { return A.sgt(B) ? A : B; }
1685
/// \brief Determine the smaller of two APInts considered to be signed.
1686
inline APInt umin(const APInt &A, const APInt &B) { return A.ult(B) ? A : B; }
1688
/// \brief Determine the larger of two APInts considered to be unsigned.
1689
inline APInt umax(const APInt &A, const APInt &B) { return A.ugt(B) ? A : B; }
1691
/// \brief Check if the specified APInt has a N-bits unsigned integer value.
1692
inline bool isIntN(unsigned N, const APInt &APIVal) { return APIVal.isIntN(N); }
1694
/// \brief Check if the specified APInt has a N-bits signed integer value.
1695
inline bool isSignedIntN(unsigned N, const APInt &APIVal) {
1610
1696
return APIVal.isSignedIntN(N);
1613
/// @returns true if the argument APInt value is a sequence of ones
1614
/// starting at the least significant bit with the remainder zero.
1615
inline bool isMask(unsigned numBits, const APInt& APIVal) {
1699
/// \returns true if the argument APInt value is a sequence of ones starting at
1700
/// the least significant bit with the remainder zero.
1701
inline bool isMask(unsigned numBits, const APInt &APIVal) {
1616
1702
return numBits <= APIVal.getBitWidth() &&
1617
APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
1703
APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
1620
/// @returns true if the argument APInt value contains a sequence of ones
1706
/// \brief Return true if the argument APInt value contains a sequence of ones
1621
1707
/// with the remainder zero.
1622
inline bool isShiftedMask(unsigned numBits, const APInt& APIVal) {
1623
return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
1626
/// @returns a byte-swapped representation of the specified APInt Value.
1627
inline APInt byteSwap(const APInt& APIVal) {
1628
return APIVal.byteSwap();
1631
/// @returns the floor log base 2 of the specified APInt value.
1632
inline unsigned logBase2(const APInt& APIVal) {
1633
return APIVal.logBase2();
1636
/// GreatestCommonDivisor - This function returns the greatest common
1637
/// divisor of the two APInt values using Euclid's algorithm.
1638
/// @returns the greatest common divisor of Val1 and Val2
1639
/// @brief Compute GCD of two APInt values.
1640
APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
1708
inline bool isShiftedMask(unsigned numBits, const APInt &APIVal) {
1709
return isMask(numBits, (APIVal - APInt(numBits, 1)) | APIVal);
1712
/// \brief Returns a byte-swapped representation of the specified APInt Value.
1713
inline APInt byteSwap(const APInt &APIVal) { return APIVal.byteSwap(); }
1715
/// \brief Returns the floor log base 2 of the specified APInt value.
1716
inline unsigned logBase2(const APInt &APIVal) { return APIVal.logBase2(); }
1718
/// \brief Compute GCD of two APInt values.
1720
/// This function returns the greatest common divisor of the two APInt values
1721
/// using Euclid's algorithm.
1723
/// \returns the greatest common divisor of Val1 and Val2
1724
APInt GreatestCommonDivisor(const APInt &Val1, const APInt &Val2);
1726
/// \brief Converts the given APInt to a double value.
1642
1728
/// Treats the APInt as an unsigned value for conversion purposes.
1643
/// @brief Converts the given APInt to a double value.
1644
inline double RoundAPIntToDouble(const APInt& APIVal) {
1729
inline double RoundAPIntToDouble(const APInt &APIVal) {
1645
1730
return APIVal.roundToDouble();
1733
/// \brief Converts the given APInt to a double value.
1648
1735
/// Treats the APInt as a signed value for conversion purposes.
1649
/// @brief Converts the given APInt to a double value.
1650
inline double RoundSignedAPIntToDouble(const APInt& APIVal) {
1736
inline double RoundSignedAPIntToDouble(const APInt &APIVal) {
1651
1737
return APIVal.signedRoundToDouble();
1654
/// @brief Converts the given APInt to a float vlalue.
1655
inline float RoundAPIntToFloat(const APInt& APIVal) {
1740
/// \brief Converts the given APInt to a float vlalue.
1741
inline float RoundAPIntToFloat(const APInt &APIVal) {
1656
1742
return float(RoundAPIntToDouble(APIVal));
1745
/// \brief Converts the given APInt to a float value.
1659
1747
/// Treast the APInt as a signed value for conversion purposes.
1660
/// @brief Converts the given APInt to a float value.
1661
inline float RoundSignedAPIntToFloat(const APInt& APIVal) {
1748
inline float RoundSignedAPIntToFloat(const APInt &APIVal) {
1662
1749
return float(APIVal.signedRoundToDouble());
1665
/// RoundDoubleToAPInt - This function convert a double value to an APInt value.
1666
/// @brief Converts the given double value into a APInt.
1752
/// \brief Converts the given double value into a APInt.
1754
/// This function convert a double value to an APInt value.
1667
1755
APInt RoundDoubleToAPInt(double Double, unsigned width);
1669
/// RoundFloatToAPInt - Converts a float value into an APInt value.
1670
/// @brief Converts a float value into a APInt.
1757
/// \brief Converts a float value into a APInt.
1759
/// Converts a float value into an APInt value.
1671
1760
inline APInt RoundFloatToAPInt(float Float, unsigned width) {
1672
1761
return RoundDoubleToAPInt(double(Float), width);
1764
/// \brief Arithmetic right-shift function.
1675
1766
/// Arithmetic right-shift the APInt by shiftAmt.
1676
/// @brief Arithmetic right-shift function.
1677
inline APInt ashr(const APInt& LHS, unsigned shiftAmt) {
1767
inline APInt ashr(const APInt &LHS, unsigned shiftAmt) {
1678
1768
return LHS.ashr(shiftAmt);
1771
/// \brief Logical right-shift function.
1681
1773
/// Logical right-shift the APInt by shiftAmt.
1682
/// @brief Logical right-shift function.
1683
inline APInt lshr(const APInt& LHS, unsigned shiftAmt) {
1774
inline APInt lshr(const APInt &LHS, unsigned shiftAmt) {
1684
1775
return LHS.lshr(shiftAmt);
1778
/// \brief Left-shift function.
1687
1780
/// Left-shift the APInt by shiftAmt.
1688
/// @brief Left-shift function.
1689
inline APInt shl(const APInt& LHS, unsigned shiftAmt) {
1781
inline APInt shl(const APInt &LHS, unsigned shiftAmt) {
1690
1782
return LHS.shl(shiftAmt);
1785
/// \brief Signed division function for APInt.
1693
1787
/// Signed divide APInt LHS by APInt RHS.
1694
/// @brief Signed division function for APInt.
1695
inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
1696
return LHS.sdiv(RHS);
1788
inline APInt sdiv(const APInt &LHS, const APInt &RHS) { return LHS.sdiv(RHS); }
1790
/// \brief Unsigned division function for APInt.
1699
1792
/// Unsigned divide APInt LHS by APInt RHS.
1700
/// @brief Unsigned division function for APInt.
1701
inline APInt udiv(const APInt& LHS, const APInt& RHS) {
1702
return LHS.udiv(RHS);
1793
inline APInt udiv(const APInt &LHS, const APInt &RHS) { return LHS.udiv(RHS); }
1795
/// \brief Function for signed remainder operation.
1705
1797
/// Signed remainder operation on APInt.
1706
/// @brief Function for signed remainder operation.
1707
inline APInt srem(const APInt& LHS, const APInt& RHS) {
1708
return LHS.srem(RHS);
1798
inline APInt srem(const APInt &LHS, const APInt &RHS) { return LHS.srem(RHS); }
1800
/// \brief Function for unsigned remainder operation.
1711
1802
/// Unsigned remainder operation on APInt.
1712
/// @brief Function for unsigned remainder operation.
1713
inline APInt urem(const APInt& LHS, const APInt& RHS) {
1714
return LHS.urem(RHS);
1803
inline APInt urem(const APInt &LHS, const APInt &RHS) { return LHS.urem(RHS); }
1805
/// \brief Function for multiplication operation.
1717
1807
/// Performs multiplication on APInt values.
1718
/// @brief Function for multiplication operation.
1719
inline APInt mul(const APInt& LHS, const APInt& RHS) {
1808
inline APInt mul(const APInt &LHS, const APInt &RHS) { return LHS * RHS; }
1810
/// \brief Function for addition operation.
1723
1812
/// Performs addition on APInt values.
1724
/// @brief Function for addition operation.
1725
inline APInt add(const APInt& LHS, const APInt& RHS) {
1813
inline APInt add(const APInt &LHS, const APInt &RHS) { return LHS + RHS; }
1815
/// \brief Function for subtraction operation.
1729
1817
/// Performs subtraction on APInt values.
1730
/// @brief Function for subtraction operation.
1731
inline APInt sub(const APInt& LHS, const APInt& RHS) {
1818
inline APInt sub(const APInt &LHS, const APInt &RHS) { return LHS - RHS; }
1820
/// \brief Bitwise AND function for APInt.
1735
1822
/// Performs bitwise AND operation on APInt LHS and
1737
/// @brief Bitwise AND function for APInt.
1738
inline APInt And(const APInt& LHS, const APInt& RHS) {
1824
inline APInt And(const APInt &LHS, const APInt &RHS) { return LHS & RHS; }
1826
/// \brief Bitwise OR function for APInt.
1742
1828
/// Performs bitwise OR operation on APInt LHS and APInt RHS.
1743
/// @brief Bitwise OR function for APInt.
1744
inline APInt Or(const APInt& LHS, const APInt& RHS) {
1829
inline APInt Or(const APInt &LHS, const APInt &RHS) { return LHS | RHS; }
1831
/// \brief Bitwise XOR function for APInt.
1748
1833
/// Performs bitwise XOR operation on APInt.
1749
/// @brief Bitwise XOR function for APInt.
1750
inline APInt Xor(const APInt& LHS, const APInt& RHS) {
1834
inline APInt Xor(const APInt &LHS, const APInt &RHS) { return LHS ^ RHS; }
1836
/// \brief Bitwise complement function.
1754
1838
/// Performs a bitwise complement operation on APInt.
1755
/// @brief Bitwise complement function.
1756
inline APInt Not(const APInt& APIVal) {
1839
inline APInt Not(const APInt &APIVal) { return ~APIVal; }
1760
1841
} // End of APIntOps namespace
1762
// See friend declaration above. This additional declaration is required in
1763
// order to compile LLVM with IBM xlC compiler.
1764
hash_code hash_value(const APInt &Arg);
1843
// See friend declaration above. This additional declaration is required in
1844
// order to compile LLVM with IBM xlC compiler.
1845
hash_code hash_value(const APInt &Arg);
1765
1846
} // End of llvm namespace