21#include "llvm/Config/llvm-config.h"
32#define DEBUG_TYPE "apint"
50 if (radix == 16 || radix == 36) {
86void APInt::initSlowCase(
const APInt& that) {
92 assert(bigVal.
data() &&
"Null pointer detected!");
108 initFromArray(bigVal);
113 initFromArray(
ArrayRef(bigVal, numWords));
118 fromString(numbits, Str, radix);
121void APInt::reallocate(
unsigned NewBitWidth) {
124 BitWidth = NewBitWidth;
133 BitWidth = NewBitWidth;
140void APInt::assignSlowCase(
const APInt &RHS) {
146 reallocate(
RHS.getBitWidth());
157 ID.AddInteger(BitWidth);
160 ID.AddInteger(U.VAL);
165 for (
unsigned i = 0; i < NumWords; ++i)
166 ID.AddInteger(U.pVal[i]);
173 const unsigned MinimumTrailingZeroes =
Log2(
A);
174 return TrailingZeroes >= MinimumTrailingZeroes;
183 return clearUnusedBits();
192 return clearUnusedBits();
199 assert(BitWidth ==
RHS.BitWidth &&
"Bit widths must be the same");
204 return clearUnusedBits();
212 return clearUnusedBits();
219 assert(BitWidth ==
RHS.BitWidth &&
"Bit widths must be the same");
224 return clearUnusedBits();
232 return clearUnusedBits();
236 assert(BitWidth ==
RHS.BitWidth &&
"Bit widths must be the same");
238 return APInt(BitWidth, U.VAL *
RHS.U.VAL,
false,
243 Result.clearUnusedBits();
247void APInt::andAssignSlowCase(
const APInt &RHS) {
253void APInt::orAssignSlowCase(
const APInt &RHS) {
259void APInt::xorAssignSlowCase(
const APInt &RHS) {
277 return clearUnusedBits();
280bool APInt::equalSlowCase(
const APInt &RHS)
const {
284int APInt::compare(
const APInt& RHS)
const {
285 assert(BitWidth ==
RHS.BitWidth &&
"Bit widths must be same for comparison");
287 return U.VAL <
RHS.U.VAL ? -1 : U.VAL >
RHS.U.VAL;
292int APInt::compareSigned(
const APInt& RHS)
const {
293 assert(BitWidth ==
RHS.BitWidth &&
"Bit widths must be same for comparison");
297 return lhsSext < rhsSext ? -1 : lhsSext > rhsSext;
301 bool rhsNeg =
RHS.isNegative();
304 if (lhsNeg != rhsNeg)
305 return lhsNeg ? -1 : 1;
312void APInt::setBitsSlowCase(
unsigned loBit,
unsigned hiBit) {
313 unsigned loWord = whichWord(loBit);
314 unsigned hiWord = whichWord(hiBit);
320 unsigned hiShiftAmt = whichBit(hiBit);
321 if (hiShiftAmt != 0) {
326 if (hiWord == loWord)
329 U.pVal[hiWord] |= hiMask;
332 U.pVal[loWord] |= loMask;
335 for (
unsigned word = loWord + 1; word < hiWord; ++word)
339void APInt::clearBitsSlowCase(
unsigned LoBit,
unsigned HiBit) {
340 unsigned LoWord = whichWord(LoBit);
341 unsigned HiWord = whichWord(HiBit);
347 unsigned HiShiftAmt = whichBit(HiBit);
348 if (HiShiftAmt != 0) {
353 if (HiWord == LoWord)
356 U.pVal[HiWord] &= HiMask;
359 U.pVal[LoWord] &= LoMask;
362 for (
unsigned Word = LoWord + 1;
Word < HiWord; ++
Word)
368 for (
unsigned i = 0; i < parts; i++)
373void APInt::flipAllBitsSlowCase() {
382APInt APInt::concatSlowCase(
const APInt &NewLSB)
const {
393 assert(bitPosition < BitWidth &&
"Out of the bit-width range!");
394 setBitVal(bitPosition, !(*
this)[bitPosition]);
399 assert((subBitWidth + bitPosition) <= BitWidth &&
"Illegal bit insertion");
402 if (subBitWidth == 0)
406 if (subBitWidth == BitWidth) {
414 U.VAL &= ~(mask << bitPosition);
415 U.VAL |= (subBits.U.
VAL << bitPosition);
419 unsigned loBit = whichBit(bitPosition);
420 unsigned loWord = whichWord(bitPosition);
421 unsigned hi1Word = whichWord(bitPosition + subBitWidth - 1);
424 if (loWord == hi1Word) {
426 U.pVal[loWord] &= ~(mask << loBit);
427 U.pVal[loWord] |= (subBits.U.
VAL << loBit);
440 if (remainingBits != 0) {
442 U.pVal[hi1Word] &= ~mask;
443 U.pVal[hi1Word] |= subBits.getWord(subBitWidth - 1);
451 for (
unsigned i = 0; i != subBitWidth; ++i)
456 uint64_t maskBits = maskTrailingOnes<uint64_t>(numBits);
459 U.VAL &= ~(maskBits << bitPosition);
460 U.VAL |= subBits << bitPosition;
464 unsigned loBit = whichBit(bitPosition);
465 unsigned loWord = whichWord(bitPosition);
466 unsigned hiWord = whichWord(bitPosition + numBits - 1);
467 if (loWord == hiWord) {
468 U.pVal[loWord] &= ~(maskBits << loBit);
469 U.pVal[loWord] |= subBits << loBit;
473 static_assert(8 *
sizeof(
WordType) <= 64,
"This code assumes only two words affected");
474 unsigned wordBits = 8 *
sizeof(
WordType);
475 U.pVal[loWord] &= ~(maskBits << loBit);
476 U.pVal[loWord] |= subBits << loBit;
478 U.pVal[hiWord] &= ~(maskBits >> (wordBits - loBit));
479 U.pVal[hiWord] |= subBits >> (wordBits - loBit);
483 assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth &&
484 "Illegal bit extraction");
487 return APInt(numBits, U.VAL >> bitPosition,
false,
490 unsigned loBit = whichBit(bitPosition);
491 unsigned loWord = whichWord(bitPosition);
492 unsigned hiWord = whichWord(bitPosition + numBits - 1);
495 if (loWord == hiWord)
496 return APInt(numBits, U.pVal[loWord] >> loBit,
false,
502 return APInt(numBits,
ArrayRef(U.pVal + loWord, 1 + hiWord - loWord));
505 APInt Result(numBits, 0);
507 unsigned NumDstWords = Result.getNumWords();
509 uint64_t *DestPtr = Result.isSingleWord() ? &Result.U.VAL : Result.U.pVal;
510 for (
unsigned word = 0; word < NumDstWords; ++word) {
511 uint64_t w0 = U.pVal[loWord + word];
513 (loWord + word + 1) < NumSrcWords ? U.pVal[loWord + word + 1] : 0;
517 return Result.clearUnusedBits();
521 unsigned bitPosition)
const {
522 assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth &&
523 "Illegal bit extraction");
524 assert(numBits <= 64 &&
"Illegal bit extraction");
526 uint64_t maskBits = maskTrailingOnes<uint64_t>(numBits);
528 return (U.VAL >> bitPosition) & maskBits;
531 "This code assumes only two words affected");
532 unsigned loBit = whichBit(bitPosition);
533 unsigned loWord = whichWord(bitPosition);
534 unsigned hiWord = whichWord(bitPosition + numBits - 1);
535 if (loWord == hiWord)
536 return (U.pVal[loWord] >> loBit) & maskBits;
538 uint64_t retBits = U.pVal[loWord] >> loBit;
545 assert(!Str.empty() &&
"Invalid string length");
546 size_t StrLen = Str.size();
549 unsigned IsNegative =
false;
550 if (Str[0] ==
'-' || Str[0] ==
'+') {
551 IsNegative = Str[0] ==
'-';
553 assert(StrLen &&
"String is only a sign, needs a value.");
559 return StrLen + IsNegative;
561 return StrLen * 3 + IsNegative;
563 return StrLen * 4 + IsNegative;
570 return (StrLen == 1 ? 4 : StrLen * 64 / 18) + IsNegative;
573 return (StrLen == 1 ? 7 : StrLen * 16 / 3) + IsNegative;
583 if (radix == 2 || radix == 8 || radix == 16)
589 size_t slen = str.
size();
594 if (*p ==
'-' || *p ==
'+') {
597 assert(slen &&
"String is only a sign, needs a value.");
608 if (log == (
unsigned)-1) {
627 return static_cast<unsigned>(
hash_value(Key));
632 "SplatSizeInBits must divide width!");
635 return *
this ==
rotl(SplatSizeInBits);
640 return this->
lshr(BitWidth - numBits);
652 assert(NewLen >= V.getBitWidth() &&
"Can't splat to smaller bit width!");
654 APInt Val = V.zext(NewLen);
655 for (
unsigned I = V.getBitWidth();
I < NewLen;
I <<= 1)
661unsigned APInt::countLeadingZerosSlowCase()
const {
678unsigned APInt::countLeadingOnesSlowCase()
const {
689 if (Count == highWordBits) {
690 for (i--; i >= 0; --i) {
702unsigned APInt::countTrailingZerosSlowCase()
const {
709 return std::min(Count, BitWidth);
712unsigned APInt::countTrailingOnesSlowCase()
const {
719 assert(Count <= BitWidth);
723unsigned APInt::countPopulationSlowCase()
const {
730bool APInt::intersectsSlowCase(
const APInt &RHS)
const {
732 if ((U.pVal[i] &
RHS.U.pVal[i]) != 0)
738bool APInt::isSubsetOfSlowCase(
const APInt &RHS)
const {
740 if ((U.pVal[i] & ~
RHS.U.pVal[i]) != 0)
747 assert(BitWidth >= 16 && BitWidth % 8 == 0 &&
"Cannot byteswap!");
749 return APInt(BitWidth, llvm::byteswap<uint16_t>(U.VAL));
751 return APInt(BitWidth, llvm::byteswap<uint32_t>(U.VAL));
752 if (BitWidth <= 64) {
753 uint64_t Tmp1 = llvm::byteswap<uint64_t>(U.VAL);
754 Tmp1 >>= (64 - BitWidth);
755 return APInt(BitWidth, Tmp1);
760 Result.U.pVal[
I] = llvm::byteswap<uint64_t>(U.pVal[
N -
I - 1]);
761 if (Result.BitWidth != BitWidth) {
762 Result.lshrInPlace(Result.BitWidth - BitWidth);
763 Result.BitWidth = BitWidth;
771 return APInt(BitWidth, llvm::reverseBits<uint64_t>(U.VAL));
773 return APInt(BitWidth, llvm::reverseBits<uint32_t>(U.VAL));
775 return APInt(BitWidth, llvm::reverseBits<uint16_t>(U.VAL));
777 return APInt(BitWidth, llvm::reverseBits<uint8_t>(U.VAL));
785 APInt Reversed(BitWidth, 0);
786 unsigned S = BitWidth;
800 if (
A ==
B)
return A;
809 unsigned Pow2_A =
A.countr_zero();
810 unsigned Pow2_B =
B.countr_zero();
811 if (Pow2_A > Pow2_B) {
812 A.lshrInPlace(Pow2_A - Pow2_B);
814 }
else if (Pow2_B > Pow2_A) {
815 B.lshrInPlace(Pow2_B - Pow2_A);
831 A.lshrInPlace(
A.countr_zero() - Pow2);
834 B.lshrInPlace(
B.countr_zero() - Pow2);
848 int64_t exp = ((
I >> 52) & 0x7ff) - 1023;
852 return APInt(width, 0u);
855 uint64_t mantissa = (
I & (~0ULL >> 12)) | 1ULL << 52;
859 return isNeg ? -
APInt(width, mantissa >> (52 - exp)) :
860 APInt(width, mantissa >> (52 - exp));
864 if (width <= exp - 52)
865 return APInt(width, 0);
868 APInt Tmp(width, mantissa);
870 return isNeg ? -Tmp : Tmp;
888 return double(getWord(0));
908 return std::numeric_limits<double>::infinity();
910 return -std::numeric_limits<double>::infinity();
917 unsigned hiWord = whichWord(n-1);
919 mantissa = Tmp.U.
pVal[0];
923 assert(hiWord > 0 &&
"huh?");
926 mantissa = hibits | lobits;
931 uint64_t I = sign | (exp << 52) | mantissa;
932 return bit_cast<double>(
I);
937 assert(width <= BitWidth &&
"Invalid APInt Truncate request");
943 if (width == BitWidth)
951 Result.U.pVal[i] = U.pVal[i];
956 Result.U.pVal[i] = U.pVal[i] << bits >> bits;
963 assert(width <= BitWidth &&
"Invalid APInt Truncate request");
974 assert(width <= BitWidth &&
"Invalid APInt Truncate request");
986 assert(Width >= BitWidth &&
"Invalid APInt SignExtend request");
991 if (Width == BitWidth)
1007 Result.clearUnusedBits();
1013 assert(width >= BitWidth &&
"Invalid APInt ZeroExtend request");
1016 return APInt(width, U.VAL);
1018 if (width == BitWidth)
1034 if (BitWidth < width)
1036 if (BitWidth > width)
1037 return trunc(width);
1042 if (BitWidth < width)
1044 if (BitWidth > width)
1045 return trunc(width);
1057void APInt::ashrSlowCase(
unsigned ShiftAmt) {
1070 if (WordsToMove != 0) {
1076 if (BitShift == 0) {
1077 std::memmove(U.pVal, U.pVal + WordShift, WordsToMove *
APINT_WORD_SIZE);
1080 for (
unsigned i = 0; i != WordsToMove - 1; ++i)
1081 U.pVal[i] = (U.pVal[i + WordShift] >> BitShift) |
1086 U.pVal[WordsToMove - 1] =
1087 (int64_t)U.pVal[WordShift + WordsToMove - 1] >> BitShift;
1092 std::memset(U.pVal + WordsToMove, Negative ? -1 : 0,
1105void APInt::lshrSlowCase(
unsigned ShiftAmt) {
1117void APInt::shlSlowCase(
unsigned ShiftAmt) {
1127 APInt rot = rotateAmt;
1144 rotateAmt %= BitWidth;
1147 return shl(rotateAmt) |
lshr(BitWidth - rotateAmt);
1157 rotateAmt %= BitWidth;
1160 return lshr(rotateAmt) |
shl(BitWidth - rotateAmt);
1189 return lg +
unsigned((*
this)[lg - 1]);
1206 if (magnitude <= 5) {
1207 static const uint8_t results[32] = {
1212 4, 4, 4, 4, 4, 4, 4, 4,
1213 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1223 if (magnitude < 52) {
1224 return APInt(BitWidth,
1234 unsigned nbits = BitWidth, i = 4;
1235 APInt testy(BitWidth, 16);
1236 APInt x_old(BitWidth, 1);
1237 APInt x_new(BitWidth, 0);
1238 APInt two(BitWidth, 2);
1241 for (;; i += 2, testy = testy.
shl(2))
1242 if (i >= nbits || this->
ule(testy)) {
1243 x_old = x_old.
shl(i / 2);
1249 x_new = (this->
udiv(x_old) + x_old).
udiv(two);
1250 if (x_old.
ule(x_new))
1261 APInt square(x_old * x_old);
1262 APInt nextSquare((x_old + 1) * (x_old +1));
1263 if (this->
ult(square))
1265 assert(this->
ule(nextSquare) &&
"Error in APInt::sqrt computation");
1266 APInt midpoint((nextSquare - square).
udiv(two));
1267 APInt offset(*
this - square);
1268 if (offset.
ult(midpoint))
1276 "multiplicative inverse is only defined for odd numbers!");
1279 APInt Factor = *
this;
1281 while (!(
T = *
this * Factor).
isOne())
1282 Factor *= 2 - std::move(
T);
1291 unsigned m,
unsigned n) {
1292 assert(u &&
"Must provide dividend");
1293 assert(v &&
"Must provide divisor");
1294 assert(q &&
"Must provide quotient");
1295 assert(u != v && u != q && v != q &&
"Must use different memory");
1296 assert(n>1 &&
"n must be > 1");
1304#define DEBUG_KNUTH(X) LLVM_DEBUG(X)
1306#define DEBUG_KNUTH(X) do {} while(false)
1327 for (
unsigned i = 0; i < m+n; ++i) {
1328 uint32_t u_tmp = u[i] >> (32 - shift);
1329 u[i] = (u[i] << shift) | u_carry;
1332 for (
unsigned i = 0; i < n; ++i) {
1333 uint32_t v_tmp = v[i] >> (32 - shift);
1334 v[i] = (v[i] << shift) | v_carry;
1362 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1365 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
1368 DEBUG_KNUTH(
dbgs() <<
"KnuthDiv: qp == " << qp <<
", rp == " << rp <<
'\n');
1379 for (
unsigned i = 0; i < n; ++i) {
1381 int64_t subres = int64_t(u[j+i]) - borrow -
Lo_32(p);
1382 u[j+i] =
Lo_32(subres);
1385 <<
", borrow = " << borrow <<
'\n');
1387 bool isNeg = u[j+n] < borrow;
1388 u[j+n] -=
Lo_32(borrow);
1406 for (
unsigned i = 0; i < n; i++) {
1407 uint32_t limit = std::min(u[j+i],v[i]);
1408 u[j+i] += v[i] + carry;
1409 carry = u[j+i] < limit || (carry && u[j+i] == limit);
1434 for (
int i = n-1; i >= 0; i--) {
1435 r[i] = (u[i] >> shift) | carry;
1436 carry = u[i] << (32 - shift);
1440 for (
int i = n-1; i >= 0; i--) {
1450void APInt::divide(
const WordType *LHS,
unsigned lhsWords,
const WordType *RHS,
1451 unsigned rhsWords, WordType *Quotient, WordType *Remainder) {
1452 assert(lhsWords >= rhsWords &&
"Fractional result");
1461 unsigned n = rhsWords * 2;
1462 unsigned m = (lhsWords * 2) - n;
1471 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1474 Q = &SPACE[(m+n+1) + n];
1476 R = &SPACE[(m+n+1) + n + (m+n)];
1486 memset(U, 0, (m+n+1)*
sizeof(
uint32_t));
1487 for (
unsigned i = 0; i < lhsWords; ++i) {
1490 U[i * 2 + 1] =
Hi_32(tmp);
1495 memset(V, 0, (n)*
sizeof(
uint32_t));
1496 for (
unsigned i = 0; i < rhsWords; ++i) {
1499 V[i * 2 + 1] =
Hi_32(tmp);
1503 memset(Q, 0, (m+n) *
sizeof(
uint32_t));
1505 memset(R, 0, n *
sizeof(
uint32_t));
1511 for (
unsigned i = n; i > 0 &&
V[i-1] == 0; i--) {
1515 for (
unsigned i = m+n; i > 0 &&
U[i-1] == 0; i--)
1524 assert(n != 0 &&
"Divide by zero?");
1528 for (
int i = m; i >= 0; i--) {
1530 if (partial_dividend == 0) {
1533 }
else if (partial_dividend < divisor) {
1535 remainder =
Lo_32(partial_dividend);
1536 }
else if (partial_dividend == divisor) {
1540 Q[i] =
Lo_32(partial_dividend / divisor);
1541 remainder =
Lo_32(partial_dividend - (Q[i] * divisor));
1554 for (
unsigned i = 0; i < lhsWords; ++i)
1555 Quotient[i] =
Make_64(Q[i*2+1], Q[i*2]);
1560 for (
unsigned i = 0; i < rhsWords; ++i)
1561 Remainder[i] =
Make_64(R[i*2+1], R[i*2]);
1565 if (U != &SPACE[0]) {
1574 assert(BitWidth ==
RHS.BitWidth &&
"Bit widths must be the same");
1578 assert(
RHS.U.VAL != 0 &&
"Divide by zero?");
1579 return APInt(BitWidth, U.VAL /
RHS.U.VAL);
1584 unsigned rhsBits =
RHS.getActiveBits();
1586 assert(rhsWords &&
"Divided by zero???");
1591 return APInt(BitWidth, 0);
1595 if (lhsWords < rhsWords || this->
ult(
RHS))
1597 return APInt(BitWidth, 0);
1600 return APInt(BitWidth, 1);
1603 return APInt(BitWidth, this->U.pVal[0] /
RHS.U.pVal[0]);
1606 APInt Quotient(BitWidth, 0);
1607 divide(U.pVal, lhsWords,
RHS.U.pVal, rhsWords, Quotient.U.
pVal,
nullptr);
1616 return APInt(BitWidth, U.VAL /
RHS);
1624 return APInt(BitWidth, 0);
1630 return APInt(BitWidth, 0);
1633 return APInt(BitWidth, 1);
1636 return APInt(BitWidth, this->U.pVal[0] /
RHS);
1639 APInt Quotient(BitWidth, 0);
1640 divide(U.pVal, lhsWords, &
RHS, 1, Quotient.U.
pVal,
nullptr);
1646 if (
RHS.isNegative())
1648 return -((-(*this)).
udiv(
RHS));
1650 if (
RHS.isNegative())
1651 return -(this->
udiv(-RHS));
1652 return this->
udiv(RHS);
1659 return -((-(*this)).
udiv(
RHS));
1662 return -(this->
udiv(-RHS));
1663 return this->
udiv(RHS);
1667 assert(BitWidth ==
RHS.BitWidth &&
"Bit widths must be the same");
1669 assert(
RHS.U.VAL != 0 &&
"Remainder by zero?");
1670 return APInt(BitWidth, U.VAL %
RHS.U.VAL);
1677 unsigned rhsBits =
RHS.getActiveBits();
1679 assert(rhsWords &&
"Performing remainder operation by zero ???");
1687 return APInt(BitWidth, 0);
1688 if (lhsWords < rhsWords || this->
ult(RHS))
1693 return APInt(BitWidth, 0);
1696 return APInt(BitWidth, U.pVal[0] %
RHS.U.pVal[0]);
1699 APInt Remainder(BitWidth, 0);
1700 divide(U.pVal, lhsWords,
RHS.U.pVal, rhsWords,
nullptr, Remainder.U.pVal);
1705 assert(
RHS != 0 &&
"Remainder by zero?");
1728 return U.pVal[0] %
RHS;
1732 divide(U.pVal, lhsWords, &
RHS, 1,
nullptr, &Remainder);
1738 if (
RHS.isNegative())
1739 return -((-(*this)).
urem(-
RHS));
1740 return -((-(*this)).
urem(
RHS));
1742 if (
RHS.isNegative())
1743 return this->
urem(-RHS);
1744 return this->
urem(RHS);
1750 return -((-(*this)).
urem(-
RHS));
1751 return -((-(*this)).
urem(
RHS));
1754 return this->
urem(-RHS);
1755 return this->
urem(RHS);
1760 assert(
LHS.BitWidth ==
RHS.BitWidth &&
"Bit widths must be the same");
1761 unsigned BitWidth =
LHS.BitWidth;
1764 if (
LHS.isSingleWord()) {
1765 assert(
RHS.U.VAL != 0 &&
"Divide by zero?");
1775 unsigned rhsBits =
RHS.getActiveBits();
1777 assert(rhsWords &&
"Performing divrem operation by zero ???");
1780 if (lhsWords == 0) {
1791 if (lhsWords < rhsWords ||
LHS.ult(
RHS)) {
1810 if (lhsWords == 1) {
1814 Quotient = lhsValue / rhsValue;
1815 Remainder = lhsValue % rhsValue;
1820 divide(
LHS.U.pVal, lhsWords,
RHS.U.pVal, rhsWords, Quotient.U.
pVal,
1823 std::memset(Quotient.U.
pVal + lhsWords, 0,
1825 std::memset(Remainder.U.
pVal + rhsWords, 0,
1832 unsigned BitWidth =
LHS.BitWidth;
1835 if (
LHS.isSingleWord()) {
1837 Remainder =
LHS.U.VAL %
RHS;
1846 if (lhsWords == 0) {
1859 Remainder =
LHS.getZExtValue();
1875 if (lhsWords == 1) {
1878 Quotient = lhsValue /
RHS;
1879 Remainder = lhsValue %
RHS;
1884 divide(
LHS.U.pVal, lhsWords, &
RHS, 1, Quotient.U.
pVal, &Remainder);
1886 std::memset(Quotient.U.
pVal + lhsWords, 0,
1892 if (
LHS.isNegative()) {
1893 if (
RHS.isNegative())
1900 }
else if (
RHS.isNegative()) {
1909 APInt &Quotient, int64_t &Remainder) {
1911 if (
LHS.isNegative()) {
1919 }
else if (
RHS < 0) {
1950 Overflow = Res.
ugt(*
this);
1964 Overflow = Res.
sdiv(
RHS) != *
this ||
1995 return APInt(BitWidth, 0);
2002 return *
this << ShAmt;
2012 return APInt(BitWidth, 0);
2016 return *
this << ShAmt;
2022 return quotient - 1;
2061 return APInt(BitWidth, 0);
2116 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
2118 "Radix should be 2, 8, 10, 16, or 36!");
2121 size_t slen = str.
size();
2122 bool isNeg = *p ==
'-';
2123 if (*p ==
'-' || *p ==
'+') {
2126 assert(slen &&
"String is only a sign, needs a value.");
2128 assert((slen <= numbits || radix != 2) &&
"Insufficient bit width");
2129 assert(((slen-1)*3 <= numbits || radix != 8) &&
"Insufficient bit width");
2130 assert(((slen-1)*4 <= numbits || radix != 16) &&
"Insufficient bit width");
2131 assert((((slen-1)*64)/22 <= numbits || radix != 10) &&
2132 "Insufficient bit width");
2141 unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
2145 unsigned digit =
getDigit(*p, radix);
2146 assert(digit < radix &&
"Invalid character in digit string");
2165 bool formatAsCLiteral,
bool UpperCase,
2166 bool InsertSeparators)
const {
2167 assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
2169 "Radix should be 2, 8, 10, 16, or 36!");
2171 const char *Prefix =
"";
2172 if (formatAsCLiteral) {
2193 unsigned Grouping = (Radix == 8 || Radix == 10) ? 3 : 4;
2198 Str.push_back(*Prefix);
2205 static const char BothDigits[] =
"0123456789abcdefghijklmnopqrstuvwxyz"
2206 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2207 const char *Digits = BothDigits + (UpperCase ? 36 : 0);
2211 char *BufPtr = std::end(Buffer);
2227 Str.push_back(*Prefix);
2233 if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
2235 *--BufPtr = Digits[
N % Radix];
2239 Str.append(BufPtr, std::end(Buffer));
2254 Str.push_back(*Prefix);
2259 unsigned StartDig = Str.size();
2264 if (Radix == 2 || Radix == 8 || Radix == 16) {
2266 unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
2267 unsigned MaskAmt = Radix - 1;
2272 if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
2273 Str.push_back(
'\'');
2275 Str.push_back(Digits[Digit]);
2283 udivrem(Tmp, Radix, Tmp, Digit);
2284 assert(Digit < Radix &&
"divide failed");
2285 if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
2286 Str.push_back(
'\'');
2288 Str.push_back(Digits[Digit]);
2294 std::reverse(Str.begin()+StartDig, Str.end());
2297#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2302 dbgs() <<
"APInt(" << BitWidth <<
"b, "
2303 << U <<
"u " << S <<
"s)\n";
2319 "Part width must be divisible by 2!");
2343 for (
unsigned i = 1; i < parts; i++)
2349 for (
unsigned i = 0; i < parts; i++)
2355 for (
unsigned i = 0; i < parts; i++)
2364 return (parts[whichWord(bit)] & maskBit(bit)) != 0;
2369 parts[whichWord(bit)] |= maskBit(bit);
2374 parts[whichWord(bit)] &= ~maskBit(bit);
2380 for (
unsigned i = 0; i < n; i++) {
2381 if (parts[i] != 0) {
2396 if (parts[n] != 0) {
2397 static_assert(
sizeof(parts[n]) <=
sizeof(
uint64_t));
2413 unsigned srcBits,
unsigned srcLSB) {
2415 assert(dstParts <= dstCount);
2418 tcAssign(dst, src + firstSrcPart, dstParts);
2429 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
2431 }
else if (n > srcBits) {
2437 while (dstParts < dstCount)
2438 dst[dstParts++] = 0;
2446 for (
unsigned i = 0; i < parts; i++) {
2449 dst[i] += rhs[i] + 1;
2466 for (
unsigned i = 0; i < parts; ++i) {
2481 for (
unsigned i = 0; i < parts; i++) {
2484 dst[i] -= rhs[i] + 1;
2504 for (
unsigned i = 0; i < parts; ++i) {
2532 unsigned srcParts,
unsigned dstParts,
2535 assert(dst <= src || dst >= src + srcParts);
2536 assert(dstParts <= srcParts + 1);
2539 unsigned n = std::min(dstParts, srcParts);
2541 for (
unsigned i = 0; i < n; i++) {
2548 if (multiplier == 0 || srcPart == 0) {
2558 if (low + mid < low)
2565 if (low + mid < low)
2570 if (low + carry < low)
2577 if (low + dst[i] < low)
2587 if (srcParts < dstParts) {
2589 assert(srcParts + 1 == dstParts);
2590 dst[srcParts] = carry;
2602 for (
unsigned i = dstParts; i < srcParts; i++)
2615 const WordType *rhs,
unsigned parts) {
2616 assert(dst != lhs && dst != rhs);
2620 for (
unsigned i = 0; i < parts; i++) {
2624 tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts, parts - i, i != 0);
2633 const WordType *rhs,
unsigned lhsParts,
2634 unsigned rhsParts) {
2636 if (lhsParts > rhsParts)
2639 assert(dst != lhs && dst != rhs);
2641 for (
unsigned i = 0; i < lhsParts; i++) {
2644 tcMultiplyPart(&dst[i], rhs, lhs[i], 0, rhsParts, rhsParts + 1, i != 0);
2660 assert(lhs != remainder && lhs != srhs && remainder != srhs);
2662 unsigned shiftCount =
tcMSB(rhs, parts) + 1;
2663 if (shiftCount == 0)
2673 tcSet(lhs, 0, parts);
2678 int compare =
tcCompare(remainder, srhs, parts);
2684 if (shiftCount == 0)
2688 if ((mask >>= 1) == 0) {
2709 if (BitShift == 0) {
2710 std::memmove(Dst + WordShift, Dst, (Words - WordShift) *
APINT_WORD_SIZE);
2712 while (Words-- > WordShift) {
2713 Dst[Words] = Dst[Words - WordShift] << BitShift;
2714 if (Words > WordShift)
2735 unsigned WordsToMove = Words - WordShift;
2737 if (BitShift == 0) {
2740 for (
unsigned i = 0; i != WordsToMove; ++i) {
2741 Dst[i] = Dst[i + WordShift] >> BitShift;
2742 if (i + 1 != WordsToMove)
2756 if (lhs[parts] != rhs[parts])
2757 return (lhs[parts] > rhs[parts]) ? 1 : -1;
2813 unsigned RangeWidth) {
2814 unsigned CoeffWidth =
A.getBitWidth();
2815 assert(CoeffWidth ==
B.getBitWidth() && CoeffWidth ==
C.getBitWidth());
2816 assert(RangeWidth <= CoeffWidth &&
2817 "Value range width should be less than coefficient width");
2818 assert(RangeWidth > 1 &&
"Value range bit width should be > 1");
2821 <<
"x + " <<
C <<
", rw:" << RangeWidth <<
'\n');
2824 if (
C.sextOrTrunc(RangeWidth).isZero()) {
2826 return APInt(CoeffWidth, 0);
2844 A =
A.sext(CoeffWidth);
2845 B =
B.sext(CoeffWidth);
2846 C =
C.sext(CoeffWidth);
2850 if (
A.isNegative()) {
2884 assert(
A.isStrictlyPositive());
2888 return V.isNegative() ? V+
T : V+(
A-
T);
2893 if (
B.isNonNegative()) {
2899 if (
C.isStrictlyPositive())
2910 LowkR = RoundUp(LowkR, R);
2920 C -= -RoundUp(-
C, R);
2937 LLVM_DEBUG(
dbgs() << __func__ <<
": updated coefficients " <<
A <<
"x^2 + "
2938 <<
B <<
"x + " <<
C <<
", rw:" << RangeWidth <<
'\n');
2941 assert(
D.isNonNegative() &&
"Negative discriminant");
2945 bool InexactSQ = Q !=
D;
2968 assert(
X.isNonNegative() &&
"Solution should be non-negative");
2970 if (!InexactSQ && Rem.
isZero()) {
2975 assert((SQ*SQ).sle(
D) &&
"SQ = |_sqrt(D)_|, so SQ*SQ <= D");
2993 return std::nullopt;
3001std::optional<unsigned>
3003 assert(
A.getBitWidth() ==
B.getBitWidth() &&
"Must have the same bitwidth");
3005 return std::nullopt;
3010 bool MatchAllBits) {
3011 unsigned OldBitWidth =
A.getBitWidth();
3012 assert((((OldBitWidth % NewBitWidth) == 0) ||
3013 ((NewBitWidth % OldBitWidth) == 0)) &&
3014 "One size should be a multiple of the other one. "
3015 "Can't do fractional scaling.");
3018 if (OldBitWidth == NewBitWidth)
3027 if (NewBitWidth > OldBitWidth) {
3029 unsigned Scale = NewBitWidth / OldBitWidth;
3030 for (
unsigned i = 0; i != OldBitWidth; ++i)
3032 NewA.
setBits(i * Scale, (i + 1) * Scale);
3034 unsigned Scale = OldBitWidth / NewBitWidth;
3035 for (
unsigned i = 0; i != NewBitWidth; ++i) {
3037 if (
A.extractBits(Scale, i * Scale).isAllOnes())
3040 if (!
A.extractBits(Scale, i * Scale).isZero())
3052 unsigned StoreBytes) {
3053 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes &&
"Integer too small!");
3059 memcpy(Dst, Src, StoreBytes);
3064 while (StoreBytes >
sizeof(
uint64_t)) {
3067 memcpy(Dst + StoreBytes, Src,
sizeof(
uint64_t));
3071 memcpy(Dst, Src +
sizeof(
uint64_t) - StoreBytes, StoreBytes);
3078 unsigned LoadBytes) {
3079 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes &&
"Integer too small!");
3081 const_cast<uint64_t *
>(IntVal.getRawData()));
3086 memcpy(Dst, Src, LoadBytes);
3092 while (LoadBytes >
sizeof(
uint64_t)) {
3095 memcpy(Dst, Src + LoadBytes,
sizeof(
uint64_t));
3099 memcpy(Dst +
sizeof(
uint64_t) - LoadBytes, Src, LoadBytes);
3105 return (C1 & C2) + (C1 ^ C2).ashr(1);
3110 return (C1 & C2) + (C1 ^ C2).lshr(1);
3115 return (C1 | C2) - (C1 ^ C2).ashr(1);
3120 return (C1 | C2) - (C1 ^ C2).lshr(1);
3144 return C1Ext * C2Ext;
3152 return C1Ext * C2Ext;
3156 assert(
N >= 0 &&
"negative exponents not supported.");
3161 int64_t RemainingExponent =
N;
3162 while (RemainingExponent > 0) {
3163 while (RemainingExponent % 2 == 0) {
3165 RemainingExponent /= 2;
3167 --RemainingExponent;
3174 const APInt &Shift) {
3175 assert(
Hi.getBitWidth() ==
Lo.getBitWidth());
3179 return Hi.shl(ShiftAmt) |
Lo.lshr(
Hi.getBitWidth() - ShiftAmt);
3183 const APInt &Shift) {
3184 assert(
Hi.getBitWidth() ==
Lo.getBitWidth());
3188 return Hi.shl(
Hi.getBitWidth() - ShiftAmt) |
Lo.lshr(ShiftAmt);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static APInt::WordType lowHalf(APInt::WordType part)
Returns the value of the lower half of PART.
static unsigned rotateModulo(unsigned BitWidth, const APInt &rotateAmt)
static APInt::WordType highHalf(APInt::WordType part)
Returns the value of the upper half of PART.
static void tcComplement(APInt::WordType *dst, unsigned parts)
static unsigned getDigit(char cdigit, uint8_t radix)
A utility function that converts a character to a digit.
static APInt::WordType lowBitMask(unsigned bits)
static uint64_t * getMemory(unsigned numWords)
A utility function for allocating memory and checking for allocation failure.
static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t *r, unsigned m, unsigned n)
Implementation of Knuth's Algorithm D (Division of nonnegative integers) from "Art of Computer Progra...
static uint64_t * getClearedMemory(unsigned numWords)
A utility function for allocating memory, checking for allocation failures, and ensuring the contents...
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_UNLIKELY(EXPR)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
static bool isNeg(Value *V)
Returns true if the operation is a negation of V, and it works for both integers and floats.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool isSigned(unsigned int Opcode)
This file defines a hash set that can be used to remove duplication of nodes in a graph.
This file defines the SmallString class.
This file implements the C++20 <bit> header.
Class for arbitrary precision integers.
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
LLVM_ABI APInt usub_sat(const APInt &RHS) const
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
static LLVM_ABI void tcSetBit(WordType *, unsigned bit)
Set the given bit of a bignum. Zero-based.
static LLVM_ABI void tcSet(WordType *, WordType, unsigned)
Sets the least significant part of a bignum to the input value, and zeroes out higher parts.
LLVM_ABI unsigned nearestLogBase2() const
static LLVM_ABI void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
LLVM_ABI APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
static LLVM_ABI int tcExtractBit(const WordType *, unsigned bit)
Extract the given bit of a bignum; returns 0 or 1. Zero-based.
LLVM_ABI bool isAligned(Align A) const
Checks if this APInt -interpreted as an address- is aligned to the provided value.
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
uint64_t getZExtValue() const
Get zero extended value.
LLVM_ABI APInt truncUSat(unsigned width) const
Truncate to new width with unsigned saturation.
uint64_t * pVal
Used to store the >64 bits integer value.
static LLVM_ABI void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
static LLVM_ABI WordType tcAdd(WordType *, const WordType *, WordType carry, unsigned)
DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
static LLVM_ABI void tcExtract(WordType *, unsigned dstCount, const WordType *, unsigned srcBits, unsigned srcLSB)
Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to DST, of dstCOUNT parts,...
LLVM_ABI uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
unsigned getActiveBits() const
Compute the number of active bits in the value.
static LLVM_ABI unsigned getSufficientBitsNeeded(StringRef Str, uint8_t Radix)
Get the bits that are sufficient to represent the string value.
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
void toStringUnsigned(SmallVectorImpl< char > &Str, unsigned Radix=10) const
Considers the APInt to be unsigned and converts it into a string in the radix given.
LLVM_ABI APInt sshl_ov(const APInt &Amt, bool &Overflow) const
LLVM_ABI APInt smul_sat(const APInt &RHS) const
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
bool sgt(const APInt &RHS) const
Signed greater than comparison.
static LLVM_ABI int tcCompare(const WordType *, const WordType *, unsigned)
Comparison (unsigned) of two bignums.
LLVM_ABI APInt & operator++()
Prefix increment operator.
LLVM_ABI APInt usub_ov(const APInt &RHS, bool &Overflow) const
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
LLVM_ABI void print(raw_ostream &OS, bool isSigned) const
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
static LLVM_ABI void tcAssign(WordType *, const WordType *, unsigned)
Assign one bignum to another.
static constexpr unsigned APINT_WORD_SIZE
Byte size of a word.
unsigned getBitWidth() const
Return the number of bits in the APInt.
static LLVM_ABI void tcShiftRight(WordType *, unsigned Words, unsigned Count)
Shift a bignum right Count bits.
static LLVM_ABI void tcFullMultiply(WordType *, const WordType *, const WordType *, unsigned, unsigned)
DST = LHS * RHS, where DST has width the sum of the widths of the operands.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
LLVM_ABI APInt sfloordiv_ov(const APInt &RHS, bool &Overflow) const
Signed integer floor division operation.
bool isSingleWord() const
Determine if this APInt just has one word to store value.
unsigned getNumWords() const
Get the number of words.
APInt()
Default constructor that creates an APInt with a 1-bit zero value.
bool isNegative() const
Determine sign of this APInt.
LLVM_ABI APInt sadd_ov(const APInt &RHS, bool &Overflow) const
APInt & operator<<=(unsigned ShiftAmt)
Left-shift assignment function.
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
double roundToDouble() const
Converts this unsigned APInt to a double value.
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
LLVM_ABI APInt reverseBits() const
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
LLVM_ABI APInt uadd_ov(const APInt &RHS, bool &Overflow) const
static LLVM_ABI void tcClearBit(WordType *, unsigned bit)
Clear the given bit of a bignum. Zero-based.
void negate()
Negate this APInt in place.
static WordType tcDecrement(WordType *dst, unsigned parts)
Decrement a bignum in-place. Return the borrow flag.
unsigned countr_zero() const
Count the number of trailing zero bits.
LLVM_ABI bool isSplat(unsigned SplatSizeInBits) const
Check if the APInt consists of a repeated bit pattern.
LLVM_ABI APInt & operator-=(const APInt &RHS)
Subtraction assignment operator.
bool isSignedIntN(unsigned N) const
Check if this APInt has an N-bits signed integer value.
LLVM_ABI APInt sdiv_ov(const APInt &RHS, bool &Overflow) const
LLVM_ABI APInt operator*(const APInt &RHS) const
Multiplication operator.
static LLVM_ABI unsigned tcLSB(const WordType *, unsigned n)
Returns the bit number of the least or most significant set bit of a number.
unsigned countl_zero() const
The APInt version of std::countl_zero.
static LLVM_ABI void tcShiftLeft(WordType *, unsigned Words, unsigned Count)
Shift a bignum left Count bits.
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
static constexpr WordType WORDTYPE_MAX
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
LLVM_ABI APInt ushl_ov(const APInt &Amt, bool &Overflow) const
static LLVM_ABI WordType tcSubtractPart(WordType *, WordType, unsigned)
DST -= RHS. Returns the carry flag.
static LLVM_ABI bool tcIsZero(const WordType *, unsigned)
Returns true if a bignum is zero, false otherwise.
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
static LLVM_ABI unsigned tcMSB(const WordType *parts, unsigned n)
Returns the bit number of the most significant set bit of a number.
static LLVM_ABI int tcDivide(WordType *lhs, const WordType *rhs, WordType *remainder, WordType *scratch, unsigned parts)
If RHS is zero LHS and REMAINDER are left unchanged, return one.
LLVM_DUMP_METHOD void dump() const
debug method
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
unsigned countl_one() const
Count the number of leading one bits.
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
unsigned logBase2() const
static LLVM_ABI int tcMultiplyPart(WordType *dst, const WordType *src, WordType multiplier, WordType carry, unsigned srcParts, unsigned dstParts, bool add)
DST += SRC * MULTIPLIER + PART if add is true DST = SRC * MULTIPLIER + PART if add is false.
static constexpr unsigned APINT_BITS_PER_WORD
Bits in a word.
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
static LLVM_ABI int tcMultiply(WordType *, const WordType *, const WordType *, unsigned)
DST = LHS * RHS, where DST has the same width as the operands and is filled with the least significan...
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
LLVM_ABI APInt & operator*=(const APInt &RHS)
Multiplication assignment operator.
uint64_t VAL
Used to store the <= 64 bits integer value.
static LLVM_ABI unsigned getBitsNeeded(StringRef str, uint8_t radix)
Get bits required for string value.
static LLVM_ABI WordType tcSubtract(WordType *, const WordType *, WordType carry, unsigned)
DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
LLVM_ABI APInt multiplicativeInverse() const
static LLVM_ABI void tcNegate(WordType *, unsigned)
Negate a bignum in-place.
bool getBoolValue() const
Convert APInt to a boolean value.
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
LLVM_ABI APInt smul_ov(const APInt &RHS, bool &Overflow) const
static WordType tcIncrement(WordType *dst, unsigned parts)
Increment a bignum in-place. Return the carry flag.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
APInt shl(unsigned shiftAmt) const
Left-shift function.
LLVM_ABI APInt byteSwap() const
LLVM_ABI APInt umul_sat(const APInt &RHS) const
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
LLVM_ABI APInt & operator+=(const APInt &RHS)
Addition assignment operator.
LLVM_ABI void flipBit(unsigned bitPosition)
Toggles a given bit to its opposite value.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
static LLVM_ABI WordType tcAddPart(WordType *, WordType, unsigned)
DST += RHS. Returns the carry flag.
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
LLVM_ABI void Profile(FoldingSetNodeID &id) const
Used to insert APInt objects, or objects that contain APInt objects, into FoldingSets.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
LLVM_ABI APInt ssub_ov(const APInt &RHS, bool &Overflow) const
LLVM_ABI APInt & operator--()
Prefix decrement operator.
bool isOne() const
Determine if this is a value of 1.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
int64_t getSExtValue() const
Get sign extended value.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
LLVM_ABI APInt sqrt() const
Compute the square root.
void setBitVal(unsigned BitPosition, bool BitValue)
Set a given bit to a given value.
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
void toStringSigned(SmallVectorImpl< char > &Str, unsigned Radix=10) const
Considers the APInt to be signed and converts it into a string in the radix given.
LLVM_ABI APInt truncSSat(unsigned width) const
Truncate to new width with signed saturation.
LLVM_ABI void toString(SmallVectorImpl< char > &Str, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false) const
Converts an APInt to a string and append it to Str.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringRef - Represent a constant reference to a string, i.e.
constexpr bool empty() const
empty - Check if the string is empty.
constexpr size_t size() const
size - Get the string size.
An opaque object representing a hash code.
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI std::optional< unsigned > GetMostSignificantDifferentBit(const APInt &A, const APInt &B)
Compare two values, and if they are different, return the position of the most significant bit that i...
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
LLVM_ABI APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A unsign-divided by B, rounded by the given rounding mode.
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
LLVM_ABI APInt muluExtended(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
LLVM_ABI APInt mulsExtended(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
LLVM_ABI APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A sign-divided by B, rounded by the given rounding mode.
LLVM_ABI APInt pow(const APInt &X, int64_t N)
Compute X^N for N>=0.
LLVM_ABI APInt RoundDoubleToAPInt(double Double, unsigned width)
Converts the given double value into a APInt.
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
LLVM_ABI std::optional< APInt > SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, unsigned RangeWidth)
Let q(n) = An^2 + Bn + C, and BW = bit width of the value range (e.g.
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
@ C
The default llvm calling convention, compatible with C.
support::ulittle32_t Word
constexpr bool IsLittleEndianHost
This is an optimization pass for GlobalISel generic memory operations.
hash_code hash_value(const FixedPointSemantics &Val)
int popcount(T Value) noexcept
Count the number of set bits in a value.
LLVM_ABI void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes)
StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst with the integer held in In...
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
int countl_one(T Value)
Count the number of ones from the most significant bit to the first zero bit.
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
@ Mod
The access may modify the value stored in memory.
constexpr unsigned BitWidth
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
unsigned Log2(Align A)
Returns the log2 of the alignment.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
constexpr uint64_t Make_64(uint32_t High, uint32_t Low)
Make a 64-bit integer from a high / low pair of 32-bit integers.
LLVM_ABI void LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, unsigned LoadBytes)
LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting from Src into IntVal,...
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
This struct is a compact representation of a valid (non-zero power of two) alignment.
An information struct used to provide DenseMap with the various necessary components for a given valu...
static uint64_t round(uint64_t Acc, uint64_t Input)