33#define DEBUG_TYPE "apint"
51 if (radix == 16 || radix == 36) {
87void APInt::initSlowCase(
const APInt& that) {
93 assert(bigVal.
data() &&
"Null pointer detected!");
109 initFromArray(bigVal);
113 : BitWidth(numBits) {
114 initFromArray(
ArrayRef(bigVal, numWords));
118 : BitWidth(numbits) {
119 fromString(numbits, Str, radix);
122void APInt::reallocate(
unsigned NewBitWidth) {
141void APInt::assignSlowCase(
const APInt &
RHS) {
147 reallocate(
RHS.getBitWidth());
158 ID.AddInteger(BitWidth);
161 ID.AddInteger(U.VAL);
166 for (
unsigned i = 0; i < NumWords; ++i)
167 ID.AddInteger(U.pVal[i]);
174 const unsigned MinimumTrailingZeroes =
Log2(
A);
175 return TrailingZeroes >= MinimumTrailingZeroes;
184 return clearUnusedBits();
193 return clearUnusedBits();
200 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
205 return clearUnusedBits();
213 return clearUnusedBits();
220 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
225 return clearUnusedBits();
233 return clearUnusedBits();
237 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
239 return APInt(BitWidth, U.VAL * RHS.U.VAL,
false,
244 Result.clearUnusedBits();
248void APInt::andAssignSlowCase(
const APInt &RHS) {
249 WordType *dst = U.pVal, *rhs = RHS.U.pVal;
254void APInt::orAssignSlowCase(
const APInt &
RHS) {
260void APInt::xorAssignSlowCase(
const APInt &
RHS) {
276 tcMultiplyPart(U.pVal, U.pVal, RHS, 0, NumWords, NumWords,
false);
278 return clearUnusedBits();
281bool APInt::equalSlowCase(
const APInt &RHS)
const {
282 return std::equal(U.pVal, U.pVal +
getNumWords(), RHS.U.pVal);
285int APInt::compare(
const APInt&
RHS)
const {
288 return U.VAL <
RHS.U.VAL ? -1 : U.VAL >
RHS.U.VAL;
293int APInt::compareSigned(
const APInt&
RHS)
const {
294 assert(BitWidth ==
RHS.BitWidth &&
"Bit widths must be same for comparison");
298 return lhsSext < rhsSext ? -1 : lhsSext > rhsSext;
302 bool rhsNeg =
RHS.isNegative();
305 if (lhsNeg != rhsNeg)
306 return lhsNeg ? -1 : 1;
313void APInt::setBitsSlowCase(
unsigned loBit,
unsigned hiBit) {
314 unsigned loWord = whichWord(loBit);
315 unsigned hiWord = whichWord(hiBit);
321 unsigned hiShiftAmt = whichBit(hiBit);
322 if (hiShiftAmt != 0) {
327 if (hiWord == loWord)
330 U.pVal[hiWord] |= hiMask;
333 U.pVal[loWord] |= loMask;
336 for (
unsigned word = loWord + 1; word < hiWord; ++word)
340void APInt::clearBitsSlowCase(
unsigned LoBit,
unsigned HiBit) {
341 unsigned LoWord = whichWord(LoBit);
342 unsigned HiWord = whichWord(HiBit);
348 unsigned HiShiftAmt = whichBit(HiBit);
349 if (HiShiftAmt != 0) {
354 if (HiWord == LoWord)
357 U.pVal[HiWord] &= HiMask;
360 U.pVal[LoWord] &= LoMask;
363 for (
unsigned Word = LoWord + 1;
Word < HiWord; ++
Word)
369 for (
unsigned i = 0; i < parts; i++)
374void APInt::flipAllBitsSlowCase() {
383APInt APInt::concatSlowCase(
const APInt &NewLSB)
const {
394 assert(bitPosition < BitWidth &&
"Out of the bit-width range!");
395 setBitVal(bitPosition, !(*
this)[bitPosition]);
400 assert((subBitWidth + bitPosition) <= BitWidth &&
"Illegal bit insertion");
403 if (subBitWidth == 0)
407 if (subBitWidth == BitWidth) {
415 U.VAL &= ~(
mask << bitPosition);
416 U.VAL |= (subBits.U.
VAL << bitPosition);
420 unsigned loBit = whichBit(bitPosition);
421 unsigned loWord = whichWord(bitPosition);
422 unsigned hi1Word = whichWord(bitPosition + subBitWidth - 1);
425 if (loWord == hi1Word) {
427 U.pVal[loWord] &= ~(
mask << loBit);
428 U.pVal[loWord] |= (subBits.U.
VAL << loBit);
441 if (remainingBits != 0) {
443 U.pVal[hi1Word] &=
~mask;
444 U.pVal[hi1Word] |= subBits.getWord(subBitWidth - 1);
452 for (
unsigned i = 0; i != subBitWidth; ++i)
460 U.VAL &= ~(maskBits << bitPosition);
461 U.VAL |= subBits << bitPosition;
465 unsigned loBit = whichBit(bitPosition);
466 unsigned loWord = whichWord(bitPosition);
467 unsigned hiWord = whichWord(bitPosition + numBits - 1);
468 if (loWord == hiWord) {
469 U.pVal[loWord] &= ~(maskBits << loBit);
470 U.pVal[loWord] |= subBits << loBit;
474 static_assert(8 *
sizeof(
WordType) <= 64,
"This code assumes only two words affected");
475 unsigned wordBits = 8 *
sizeof(
WordType);
476 U.pVal[loWord] &= ~(maskBits << loBit);
477 U.pVal[loWord] |= subBits << loBit;
479 U.pVal[hiWord] &= ~(maskBits >> (wordBits - loBit));
480 U.pVal[hiWord] |= subBits >> (wordBits - loBit);
484 assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth &&
485 "Illegal bit extraction");
488 return APInt(numBits, U.VAL >> bitPosition,
false,
491 unsigned loBit = whichBit(bitPosition);
492 unsigned loWord = whichWord(bitPosition);
493 unsigned hiWord = whichWord(bitPosition + numBits - 1);
496 if (loWord == hiWord)
497 return APInt(numBits, U.pVal[loWord] >> loBit,
false,
503 return APInt(numBits,
ArrayRef(U.pVal + loWord, 1 + hiWord - loWord));
506 APInt Result(numBits, 0);
508 unsigned NumDstWords = Result.getNumWords();
510 uint64_t *DestPtr = Result.isSingleWord() ? &Result.U.VAL : Result.U.pVal;
511 for (
unsigned word = 0; word < NumDstWords; ++word) {
512 uint64_t w0 = U.pVal[loWord + word];
514 (loWord + word + 1) < NumSrcWords ? U.pVal[loWord + word + 1] : 0;
518 return Result.clearUnusedBits();
522 unsigned bitPosition)
const {
523 assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth &&
524 "Illegal bit extraction");
525 assert(numBits <= 64 &&
"Illegal bit extraction");
529 return (U.VAL >> bitPosition) & maskBits;
532 "This code assumes only two words affected");
533 unsigned loBit = whichBit(bitPosition);
534 unsigned loWord = whichWord(bitPosition);
535 unsigned hiWord = whichWord(bitPosition + numBits - 1);
536 if (loWord == hiWord)
537 return (U.pVal[loWord] >> loBit) & maskBits;
539 uint64_t retBits = U.pVal[loWord] >> loBit;
546 assert(!Str.empty() &&
"Invalid string length");
547 size_t StrLen = Str.size();
550 unsigned IsNegative =
false;
551 if (Str[0] ==
'-' || Str[0] ==
'+') {
552 IsNegative = Str[0] ==
'-';
554 assert(StrLen &&
"String is only a sign, needs a value.");
560 return StrLen + IsNegative;
562 return StrLen * 3 + IsNegative;
564 return StrLen * 4 + IsNegative;
571 return (StrLen == 1 ? 4 : StrLen * 64 / 18) + IsNegative;
574 return (StrLen == 1 ? 7 : StrLen * 16 / 3) + IsNegative;
584 if (radix == 2 || radix == 8 || radix == 16)
590 size_t slen = str.
size();
595 if (*p ==
'-' || *p ==
'+') {
598 assert(slen &&
"String is only a sign, needs a value.");
609 if (log == (
unsigned)-1) {
633 "SplatSizeInBits must divide width!");
636 return *
this ==
rotl(SplatSizeInBits);
641 return this->
lshr(BitWidth - numBits);
653 assert(NewLen >= V.getBitWidth() &&
"Can't splat to smaller bit width!");
655 APInt Val = V.zext(NewLen);
656 for (
unsigned I = V.getBitWidth();
I < NewLen;
I <<= 1)
662unsigned APInt::countLeadingZerosSlowCase()
const {
679unsigned APInt::countLeadingOnesSlowCase()
const {
690 if (
Count == highWordBits) {
691 for (i--; i >= 0; --i) {
703unsigned APInt::countTrailingZerosSlowCase()
const {
710 return std::min(
Count, BitWidth);
713unsigned APInt::countTrailingOnesSlowCase()
const {
724unsigned APInt::countPopulationSlowCase()
const {
731bool APInt::isPowerOf2SlowCase()
const {
741bool APInt::intersectsSlowCase(
const APInt &
RHS)
const {
743 if ((U.pVal[i] &
RHS.U.pVal[i]) != 0)
749bool APInt::isSubsetOfSlowCase(
const APInt &
RHS)
const {
751 if ((U.pVal[i] & ~
RHS.U.pVal[i]) != 0)
757bool APInt::isInverseOfSlowCase(
const APInt &
RHS)
const {
759 for (
unsigned I = 0;
I !=
Last; ++
I)
765 return (U.pVal[
Last] ^
RHS.U.pVal[
Last]) == TailMask;
769 assert(BitWidth >= 16 && BitWidth % 8 == 0 &&
"Cannot byteswap!");
774 if (BitWidth <= 64) {
776 Tmp1 >>= (64 - BitWidth);
777 return APInt(BitWidth, Tmp1);
783 if (Result.BitWidth != BitWidth) {
784 Result.lshrInPlace(Result.BitWidth - BitWidth);
785 Result.BitWidth = BitWidth;
805 return APInt(BitWidth,
810 APInt Result(BitWidth, 0);
813 if (ExcessBits == 0) {
815 for (
unsigned I = 0;
I < NumWords; ++
I)
821 for (
unsigned I = 0;
I < NumWords - 1; ++
I) {
823 Result.U.pVal[
I] = (PrevRev >> ExcessBits) | (CurrRev << (64 - ExcessBits));
826 Result.U.pVal[NumWords - 1] = PrevRev >> ExcessBits;
832 if (
A ==
B)
return A;
841 unsigned Pow2_A =
A.countr_zero();
842 unsigned Pow2_B =
B.countr_zero();
843 if (Pow2_A > Pow2_B) {
844 A.lshrInPlace(Pow2_A - Pow2_B);
846 }
else if (Pow2_B > Pow2_A) {
847 B.lshrInPlace(Pow2_B - Pow2_A);
863 A.lshrInPlace(
A.countr_zero() - Pow2);
866 B.lshrInPlace(
B.countr_zero() - Pow2);
880 int64_t
exp = ((
I >> 52) & 0x7ff) - 1023;
884 return APInt(width, 0u);
887 uint64_t mantissa = (
I & (~0ULL >> 12)) | 1ULL << 52;
892 APInt(width, mantissa >> (52 -
exp));
896 if (width <=
exp - 52)
897 return APInt(width, 0);
900 APInt Tmp(width, mantissa);
902 return isNeg ? -Tmp : Tmp;
920 return double(getWord(0));
940 return std::numeric_limits<double>::infinity();
942 return -std::numeric_limits<double>::infinity();
949 unsigned hiWord = whichWord(n-1);
951 mantissa = Tmp.U.
pVal[0];
955 assert(hiWord > 0 &&
"huh?");
958 mantissa = hibits | lobits;
969 assert(width <= BitWidth &&
"Invalid APInt Truncate request");
975 if (width == BitWidth)
983 Result.U.pVal[i] = U.pVal[i];
988 Result.U.pVal[i] = U.pVal[i] << bits >> bits;
995 assert(width <= BitWidth &&
"Invalid APInt Truncate request");
1006 assert(width <= BitWidth &&
"Invalid APInt Truncate request");
1010 return trunc(width);
1018 assert(width <= BitWidth &&
"Invalid APInt Truncate request");
1022 return trunc(width);
1029 assert(Width >= BitWidth &&
"Invalid APInt SignExtend request");
1034 if (Width == BitWidth)
1050 Result.clearUnusedBits();
1056 assert(width >= BitWidth &&
"Invalid APInt ZeroExtend request");
1059 return APInt(width, U.VAL);
1061 if (width == BitWidth)
1077 if (BitWidth < width)
1079 if (BitWidth > width)
1080 return trunc(width);
1085 if (BitWidth < width)
1087 if (BitWidth > width)
1088 return trunc(width);
1100void APInt::ashrSlowCase(
unsigned ShiftAmt) {
1113 if (WordsToMove != 0) {
1119 if (BitShift == 0) {
1120 std::memmove(U.pVal, U.pVal + WordShift, WordsToMove *
APINT_WORD_SIZE);
1123 for (
unsigned i = 0; i != WordsToMove - 1; ++i)
1124 U.pVal[i] = (U.pVal[i + WordShift] >> BitShift) |
1129 U.pVal[WordsToMove - 1] =
1130 (int64_t)U.pVal[WordShift + WordsToMove - 1] >> BitShift;
1135 std::memset(U.pVal + WordsToMove, Negative ? -1 : 0,
1148void APInt::lshrSlowCase(
unsigned ShiftAmt) {
1160void APInt::shlSlowCase(
unsigned ShiftAmt) {
1170 APInt rot = rotateAmt;
1187 rotateAmt %= BitWidth;
1190 return shl(rotateAmt) |
lshr(BitWidth - rotateAmt);
1200 rotateAmt %= BitWidth;
1203 return lshr(rotateAmt) |
shl(BitWidth - rotateAmt);
1232 return lg +
unsigned((*
this)[lg - 1]);
1249 if (magnitude <= 5) {
1250 static const uint8_t results[32] = {
1254 3, 3, 3, 3, 3, 3, 3,
1255 4, 4, 4, 4, 4, 4, 4, 4, 4,
1256 5, 5, 5, 5, 5, 5, 5,
1265 if (magnitude < 52) {
1276 unsigned nbits = BitWidth, i = 4;
1277 APInt testy(BitWidth, 16);
1278 APInt x_old(BitWidth, 1);
1279 APInt x_new(BitWidth, 0);
1280 APInt two(BitWidth, 2);
1283 for (;; i += 2, testy = testy.
shl(2))
1284 if (i >= nbits || this->
ule(testy)) {
1285 x_old = x_old.
shl(i / 2);
1291 x_new = (this->
udiv(x_old) + x_old).
udiv(two);
1292 if (x_old.
ule(x_new))
1302 "multiplicative inverse is only defined for odd numbers!");
1305 APInt Factor = *
this;
1307 while (!(
T = *
this * Factor).
isOne())
1308 Factor *= 2 - std::move(
T);
1317 unsigned m,
unsigned n) {
1318 assert(u &&
"Must provide dividend");
1319 assert(v &&
"Must provide divisor");
1320 assert(q &&
"Must provide quotient");
1321 assert(u != v && u != q && v != q &&
"Must use different memory");
1322 assert(n>1 &&
"n must be > 1");
1330#define DEBUG_KNUTH(X) LLVM_DEBUG(X)
1332#define DEBUG_KNUTH(X) do {} while(false)
1353 for (
unsigned i = 0; i < m+n; ++i) {
1354 uint32_t u_tmp = u[i] >> (32 - shift);
1355 u[i] = (u[i] << shift) | u_carry;
1358 for (
unsigned i = 0; i < n; ++i) {
1359 uint32_t v_tmp = v[i] >> (32 - shift);
1360 v[i] = (v[i] << shift) | v_carry;
1388 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1391 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
1394 DEBUG_KNUTH(
dbgs() <<
"KnuthDiv: qp == " << qp <<
", rp == " << rp <<
'\n');
1405 for (
unsigned i = 0; i < n; ++i) {
1407 int64_t subres = int64_t(u[j+i]) - borrow -
Lo_32(p);
1408 u[j+i] =
Lo_32(subres);
1411 <<
", borrow = " << borrow <<
'\n');
1413 bool isNeg = u[j+n] < borrow;
1414 u[j+n] -=
Lo_32(borrow);
1432 for (
unsigned i = 0; i < n; i++) {
1433 uint32_t limit = std::min(u[j+i],v[i]);
1434 u[j+i] += v[i] + carry;
1435 carry = u[j+i] < limit || (carry && u[j+i] == limit);
1460 for (
int i = n-1; i >= 0; i--) {
1461 r[i] = (u[i] >> shift) | carry;
1462 carry = u[i] << (32 - shift);
1466 for (
int i = n-1; i >= 0; i--) {
1476void APInt::divide(
const WordType *
LHS,
unsigned lhsWords,
const WordType *
RHS,
1477 unsigned rhsWords, WordType *Quotient, WordType *Remainder) {
1478 assert(lhsWords >= rhsWords &&
"Fractional result");
1487 unsigned n = rhsWords * 2;
1488 unsigned m = (lhsWords * 2) - n;
1492 uint32_t SPACE[128];
1493 uint32_t *U =
nullptr;
1494 uint32_t *
V =
nullptr;
1495 uint32_t *Q =
nullptr;
1496 uint32_t *
R =
nullptr;
1497 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1500 Q = &SPACE[(m+n+1) + n];
1502 R = &SPACE[(m+n+1) + n + (m+n)];
1504 U =
new uint32_t[m + n + 1];
1505 V =
new uint32_t[n];
1506 Q =
new uint32_t[m+n];
1508 R =
new uint32_t[n];
1512 memset(U, 0, (m+n+1)*
sizeof(uint32_t));
1513 for (
unsigned i = 0; i < lhsWords; ++i) {
1514 uint64_t tmp =
LHS[i];
1515 U[i * 2] =
Lo_32(tmp);
1516 U[i * 2 + 1] =
Hi_32(tmp);
1521 memset(V, 0, (n)*
sizeof(uint32_t));
1522 for (
unsigned i = 0; i < rhsWords; ++i) {
1523 uint64_t tmp =
RHS[i];
1525 V[i * 2 + 1] =
Hi_32(tmp);
1529 memset(Q, 0, (m+n) *
sizeof(uint32_t));
1531 memset(R, 0, n *
sizeof(uint32_t));
1537 for (
unsigned i = n; i > 0 &&
V[i-1] == 0; i--) {
1541 for (
unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1550 assert(n != 0 &&
"Divide by zero?");
1552 uint32_t divisor =
V[0];
1553 uint32_t remainder = 0;
1554 for (
int i = m; i >= 0; i--) {
1555 uint64_t partial_dividend =
Make_64(remainder, U[i]);
1556 if (partial_dividend == 0) {
1559 }
else if (partial_dividend < divisor) {
1561 remainder =
Lo_32(partial_dividend);
1562 }
else if (partial_dividend == divisor) {
1566 Q[i] =
Lo_32(partial_dividend / divisor);
1567 remainder =
Lo_32(partial_dividend - (Q[i] * divisor));
1580 for (
unsigned i = 0; i < lhsWords; ++i)
1581 Quotient[i] =
Make_64(Q[i*2+1], Q[i*2]);
1586 for (
unsigned i = 0; i < rhsWords; ++i)
1587 Remainder[i] =
Make_64(R[i*2+1], R[i*2]);
1591 if (U != &SPACE[0]) {
1600 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1604 assert(RHS.U.VAL != 0 &&
"Divide by zero?");
1605 return APInt(BitWidth, U.VAL / RHS.U.VAL);
1610 unsigned rhsBits = RHS.getActiveBits();
1612 assert(rhsWords &&
"Divided by zero???");
1617 return APInt(BitWidth, 0);
1621 if (lhsWords < rhsWords || this->
ult(RHS))
1623 return APInt(BitWidth, 0);
1626 return APInt(BitWidth, 1);
1629 return APInt(BitWidth, this->U.pVal[0] / RHS.U.pVal[0]);
1632 APInt Quotient(BitWidth, 0);
1633 divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords, Quotient.U.
pVal,
nullptr);
1638 assert(RHS != 0 &&
"Divide by zero?");
1642 return APInt(BitWidth, U.VAL / RHS);
1650 return APInt(BitWidth, 0);
1656 return APInt(BitWidth, 0);
1659 return APInt(BitWidth, 1);
1662 return APInt(BitWidth, this->U.pVal[0] / RHS);
1665 APInt Quotient(BitWidth, 0);
1666 divide(U.pVal, lhsWords, &RHS, 1, Quotient.U.
pVal,
nullptr);
1672 if (RHS.isNegative())
1673 return (-(*
this)).udiv(-RHS);
1674 return -((-(*this)).udiv(RHS));
1676 if (RHS.isNegative())
1677 return -(this->
udiv(-RHS));
1678 return this->
udiv(RHS);
1684 return (-(*
this)).udiv(-RHS);
1685 return -((-(*this)).udiv(RHS));
1688 return -(this->
udiv(-RHS));
1689 return this->
udiv(RHS);
1693 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1695 assert(RHS.U.VAL != 0 &&
"Remainder by zero?");
1696 return APInt(BitWidth, U.VAL % RHS.U.VAL);
1703 unsigned rhsBits = RHS.getActiveBits();
1705 assert(rhsWords &&
"Performing remainder operation by zero ???");
1710 return APInt(BitWidth, 0);
1713 return APInt(BitWidth, 0);
1714 if (lhsWords < rhsWords || this->
ult(RHS))
1719 return APInt(BitWidth, 0);
1722 return APInt(BitWidth, U.pVal[0] % RHS.U.pVal[0]);
1723 if (RHS.isPowerOf2()) {
1725 APInt Result(*
this);
1726 Result.clearBits(RHS.logBase2(), BitWidth);
1732 divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords,
nullptr, Remainder.U.pVal);
1737 assert(RHS != 0 &&
"Remainder by zero?");
1760 return U.pVal[0] % RHS;
1763 return U.pVal[0] & (RHS - 1);
1767 divide(U.pVal, lhsWords, &RHS, 1,
nullptr, &Remainder);
1773 if (RHS.isNegative())
1774 return -((-(*this)).urem(-RHS));
1775 return -((-(*this)).urem(RHS));
1777 if (RHS.isNegative())
1778 return this->
urem(-RHS);
1779 return this->
urem(RHS);
1785 return -((-(*this)).urem(-RHS));
1786 return -((-(*this)).urem(RHS));
1789 return this->
urem(-RHS);
1790 return this->
urem(RHS);
1795 assert(LHS.BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1796 unsigned BitWidth = LHS.BitWidth;
1799 if (LHS.isSingleWord()) {
1800 assert(RHS.U.VAL != 0 &&
"Divide by zero?");
1801 uint64_t QuotVal = LHS.U.VAL / RHS.U.VAL;
1802 uint64_t RemVal = LHS.U.VAL % RHS.U.VAL;
1803 Quotient =
APInt(BitWidth, QuotVal);
1804 Remainder =
APInt(BitWidth, RemVal);
1809 unsigned lhsWords =
getNumWords(LHS.getActiveBits());
1810 unsigned rhsBits = RHS.getActiveBits();
1812 assert(rhsWords &&
"Performing divrem operation by zero ???");
1815 if (lhsWords == 0) {
1816 Quotient =
APInt(BitWidth, 0);
1817 Remainder =
APInt(BitWidth, 0);
1823 Remainder =
APInt(BitWidth, 0);
1826 if (lhsWords < rhsWords || LHS.ult(RHS)) {
1828 Quotient =
APInt(BitWidth, 0);
1833 Quotient =
APInt(BitWidth, 1);
1834 Remainder =
APInt(BitWidth, 0);
1842 Quotient.reallocate(BitWidth);
1843 Remainder.reallocate(BitWidth);
1845 if (lhsWords == 1) {
1849 Quotient = lhsValue / rhsValue;
1850 Remainder = lhsValue % rhsValue;
1855 divide(LHS.U.pVal, lhsWords, RHS.U.pVal, rhsWords, Quotient.U.
pVal,
1858 std::memset(Quotient.U.
pVal + lhsWords, 0,
1860 std::memset(Remainder.U.
pVal + rhsWords, 0,
1866 assert(RHS != 0 &&
"Divide by zero?");
1867 unsigned BitWidth = LHS.BitWidth;
1870 if (LHS.isSingleWord()) {
1871 uint64_t QuotVal = LHS.U.VAL / RHS;
1872 Remainder = LHS.U.VAL % RHS;
1873 Quotient =
APInt(BitWidth, QuotVal);
1878 unsigned lhsWords =
getNumWords(LHS.getActiveBits());
1881 if (lhsWords == 0) {
1882 Quotient =
APInt(BitWidth, 0);
1894 Remainder = LHS.getZExtValue();
1895 Quotient =
APInt(BitWidth, 0);
1900 Quotient =
APInt(BitWidth, 1);
1908 Quotient.reallocate(BitWidth);
1910 if (lhsWords == 1) {
1913 Quotient = lhsValue / RHS;
1914 Remainder = lhsValue % RHS;
1919 divide(LHS.U.pVal, lhsWords, &RHS, 1, Quotient.U.
pVal, &Remainder);
1921 std::memset(Quotient.U.
pVal + lhsWords, 0,
1927 if (LHS.isNegative()) {
1928 if (RHS.isNegative())
1935 }
else if (RHS.isNegative()) {
1944 APInt &Quotient, int64_t &Remainder) {
1946 if (LHS.isNegative()) {
1954 }
else if (RHS < 0) {
1964 APInt Res = *
this+RHS;
1971 APInt Res = *
this+RHS;
1972 Overflow = Res.
ult(RHS);
1977 APInt Res = *
this - RHS;
1984 APInt Res = *
this-RHS;
1985 Overflow = Res.
ugt(*
this);
1996 APInt Res = *
this * RHS;
1999 Overflow = Res.
sdiv(RHS) != *
this ||
2007 if (
countl_zero() + RHS.countl_zero() + 2 <= BitWidth) {
2030 return APInt(BitWidth, 0);
2037 return *
this << ShAmt;
2047 return APInt(BitWidth, 0);
2051 return *
this << ShAmt;
2056 if ((quotient * RHS != *
this) && (
isNegative() != RHS.isNegative()))
2057 return quotient - 1;
2096 return APInt(BitWidth, 0);
2106 bool ResIsNegative =
isNegative() ^ RHS.isNegative();
2151 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
2153 "Radix should be 2, 8, 10, 16, or 36!");
2156 size_t slen = str.
size();
2157 bool isNeg = *p ==
'-';
2158 if (*p ==
'-' || *p ==
'+') {
2161 assert(slen &&
"String is only a sign, needs a value.");
2163 assert((slen <= numbits || radix != 2) &&
"Insufficient bit width");
2164 assert(((slen-1)*3 <= numbits || radix != 8) &&
"Insufficient bit width");
2165 assert(((slen-1)*4 <= numbits || radix != 16) &&
"Insufficient bit width");
2166 assert((((slen-1)*64)/22 <= numbits || radix != 10) &&
2167 "Insufficient bit width");
2176 unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
2180 unsigned digit =
getDigit(*p, radix);
2181 assert(digit < radix &&
"Invalid character in digit string");
2200 bool formatAsCLiteral,
bool UpperCase,
2201 bool InsertSeparators)
const {
2202 assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
2204 "Radix should be 2, 8, 10, 16, or 36!");
2206 const char *Prefix =
"";
2207 if (formatAsCLiteral) {
2228 unsigned Grouping = (Radix == 8 || Radix == 10) ? 3 : 4;
2233 Str.push_back(*Prefix);
2240 static const char BothDigits[] =
"0123456789abcdefghijklmnopqrstuvwxyz"
2241 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2242 const char *Digits = BothDigits + (UpperCase ? 36 : 0);
2246 char *BufPtr = std::end(Buffer);
2262 Str.push_back(*Prefix);
2268 if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
2270 *--BufPtr = Digits[
N % Radix];
2274 Str.append(BufPtr, std::end(Buffer));
2289 Str.push_back(*Prefix);
2294 unsigned StartDig = Str.size();
2299 if (Radix == 2 || Radix == 8 || Radix == 16) {
2301 unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
2302 unsigned MaskAmt = Radix - 1;
2307 if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
2308 Str.push_back(
'\'');
2310 Str.push_back(Digits[Digit]);
2318 udivrem(Tmp, Radix, Tmp, Digit);
2319 assert(Digit < Radix &&
"divide failed");
2320 if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
2321 Str.push_back(
'\'');
2323 Str.push_back(Digits[Digit]);
2329 std::reverse(Str.begin()+StartDig, Str.end());
2332#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2337 dbgs() <<
"APInt(" << BitWidth <<
"b, "
2338 << U <<
"u " << S <<
"s)\n";
2354 "Part width must be divisible by 2!");
2378 for (
unsigned i = 1; i < parts; i++)
2384 for (
unsigned i = 0; i < parts; i++)
2390 for (
unsigned i = 0; i < parts; i++)
2399 return (parts[whichWord(bit)] & maskBit(bit)) != 0;
2404 parts[whichWord(bit)] |= maskBit(bit);
2409 parts[whichWord(bit)] &= ~maskBit(bit);
2415 for (
unsigned i = 0; i < n; i++) {
2416 if (parts[i] != 0) {
2431 if (parts[n] != 0) {
2432 static_assert(
sizeof(parts[n]) <=
sizeof(
uint64_t));
2448 unsigned srcBits,
unsigned srcLSB) {
2450 assert(dstParts <= dstCount);
2453 tcAssign(dst, src + firstSrcPart, dstParts);
2464 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] &
mask)
2466 }
else if (n > srcBits) {
2472 while (dstParts < dstCount)
2473 dst[dstParts++] = 0;
2481 for (
unsigned i = 0; i < parts; i++) {
2484 dst[i] += rhs[i] + 1;
2501 for (
unsigned i = 0; i < parts; ++i) {
2516 for (
unsigned i = 0; i < parts; i++) {
2519 dst[i] -= rhs[i] + 1;
2539 for (
unsigned i = 0; i < parts; ++i) {
2567 unsigned srcParts,
unsigned dstParts,
2570 assert(dst <= src || dst >= src + srcParts);
2571 assert(dstParts <= srcParts + 1);
2574 unsigned n = std::min(dstParts, srcParts);
2576 for (
unsigned i = 0; i < n; i++) {
2583 if (multiplier == 0 || srcPart == 0) {
2593 if (low + mid < low)
2600 if (low + mid < low)
2605 if (low + carry < low)
2612 if (low + dst[i] < low)
2622 if (srcParts < dstParts) {
2624 assert(srcParts + 1 == dstParts);
2625 dst[srcParts] = carry;
2637 for (
unsigned i = dstParts; i < srcParts; i++)
2650 const WordType *rhs,
unsigned parts) {
2651 assert(dst != lhs && dst != rhs);
2655 for (
unsigned i = 0; i < parts; i++) {
2659 tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts, parts - i, i != 0);
2668 const WordType *rhs,
unsigned lhsParts,
2669 unsigned rhsParts) {
2671 if (lhsParts > rhsParts)
2674 assert(dst != lhs && dst != rhs);
2676 for (
unsigned i = 0; i < lhsParts; i++) {
2679 tcMultiplyPart(&dst[i], rhs, lhs[i], 0, rhsParts, rhsParts + 1, i != 0);
2695 assert(lhs != remainder && lhs != srhs && remainder != srhs);
2697 unsigned shiftCount =
tcMSB(rhs, parts) + 1;
2698 if (shiftCount == 0)
2708 tcSet(lhs, 0, parts);
2713 int compare =
tcCompare(remainder, srhs, parts);
2719 if (shiftCount == 0)
2723 if ((
mask >>= 1) == 0) {
2744 if (BitShift == 0) {
2745 std::memmove(Dst + WordShift, Dst, (Words - WordShift) *
APINT_WORD_SIZE);
2747 while (Words-- > WordShift) {
2748 Dst[Words] = Dst[Words - WordShift] << BitShift;
2749 if (Words > WordShift)
2770 unsigned WordsToMove = Words - WordShift;
2772 if (BitShift == 0) {
2775 for (
unsigned i = 0; i != WordsToMove; ++i) {
2776 Dst[i] = Dst[i + WordShift] >> BitShift;
2777 if (i + 1 != WordsToMove)
2791 if (lhs[parts] != rhs[parts])
2792 return (lhs[parts] > rhs[parts]) ? 1 : -1;
2848 unsigned RangeWidth) {
2849 unsigned CoeffWidth =
A.getBitWidth();
2850 assert(CoeffWidth ==
B.getBitWidth() && CoeffWidth ==
C.getBitWidth());
2851 assert(RangeWidth <= CoeffWidth &&
2852 "Value range width should be less than coefficient width");
2853 assert(RangeWidth > 1 &&
"Value range bit width should be > 1");
2856 <<
"x + " <<
C <<
", rw:" << RangeWidth <<
'\n');
2859 if (
C.sextOrTrunc(RangeWidth).isZero()) {
2861 return APInt(CoeffWidth, 0);
2879 A =
A.sext(CoeffWidth);
2880 B =
B.sext(CoeffWidth);
2881 C =
C.sext(CoeffWidth);
2885 if (
A.isNegative()) {
2919 assert(
A.isStrictlyPositive());
2923 return V.isNegative() ? V+
T : V+(
A-
T);
2928 if (
B.isNonNegative()) {
2934 if (
C.isStrictlyPositive())
2945 LowkR = RoundUp(LowkR, R);
2955 C -= -RoundUp(-
C, R);
2972 LLVM_DEBUG(
dbgs() << __func__ <<
": updated coefficients " <<
A <<
"x^2 + "
2973 <<
B <<
"x + " <<
C <<
", rw:" << RangeWidth <<
'\n');
2976 assert(
D.isNonNegative() &&
"Negative discriminant");
2977 APInt SQ =
D.sqrtFloor();
2980 bool InexactSQ = Q !=
D;
2999 assert(
X.isNonNegative() &&
"Solution should be non-negative");
3001 if (!InexactSQ && Rem.
isZero()) {
3006 assert((SQ*SQ).sle(
D) &&
"SQ = |_sqrt(D)_|, so SQ*SQ <= D");
3024 return std::nullopt;
3032std::optional<unsigned>
3034 assert(
A.getBitWidth() ==
B.getBitWidth() &&
"Must have the same bitwidth");
3036 return std::nullopt;
3037 return A.getBitWidth() - ((
A ^
B).countl_zero() + 1);
3041 bool MatchAllBits) {
3042 unsigned OldBitWidth =
A.getBitWidth();
3043 assert((((OldBitWidth % NewBitWidth) == 0) ||
3044 ((NewBitWidth % OldBitWidth) == 0)) &&
3045 "One size should be a multiple of the other one. "
3046 "Can't do fractional scaling.");
3049 if (OldBitWidth == NewBitWidth)
3058 if (NewBitWidth > OldBitWidth) {
3060 unsigned Scale = NewBitWidth / OldBitWidth;
3061 for (
unsigned i = 0; i != OldBitWidth; ++i)
3063 NewA.
setBits(i * Scale, (i + 1) * Scale);
3065 unsigned Scale = OldBitWidth / NewBitWidth;
3066 for (
unsigned i = 0; i != NewBitWidth; ++i) {
3068 if (
A.extractBits(Scale, i * Scale).isAllOnes())
3071 if (!
A.extractBits(Scale, i * Scale).isZero())
3083 unsigned StoreBytes) {
3084 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes &&
"Integer too small!");
3090 memcpy(Dst, Src, StoreBytes);
3095 while (StoreBytes >
sizeof(
uint64_t)) {
3098 memcpy(Dst + StoreBytes, Src,
sizeof(
uint64_t));
3102 memcpy(Dst, Src +
sizeof(
uint64_t) - StoreBytes, StoreBytes);
3109 unsigned LoadBytes) {
3110 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes &&
"Integer too small!");
3112 const_cast<uint64_t *
>(IntVal.getRawData()));
3117 memcpy(Dst, Src, LoadBytes);
3123 while (LoadBytes >
sizeof(
uint64_t)) {
3126 memcpy(Dst, Src + LoadBytes,
sizeof(
uint64_t));
3130 memcpy(Dst +
sizeof(
uint64_t) - LoadBytes, Src, LoadBytes);
3136 return (C1 & C2) + (C1 ^ C2).ashr(1);
3141 return (C1 & C2) + (C1 ^ C2).lshr(1);
3146 return (C1 | C2) - (C1 ^ C2).ashr(1);
3151 return (C1 | C2) - (C1 ^ C2).lshr(1);
3175 return C1Ext * C2Ext;
3183 return C1Ext * C2Ext;
3187 assert(
N >= 0 &&
"negative exponents not supported.");
3192 int64_t RemainingExponent =
N;
3193 while (RemainingExponent > 0) {
3194 while (RemainingExponent % 2 == 0) {
3196 RemainingExponent /= 2;
3198 --RemainingExponent;
3205 const APInt &Shift) {
3206 assert(
Hi.getBitWidth() ==
Lo.getBitWidth());
3210 return Hi.shl(ShiftAmt) |
Lo.lshr(
Hi.getBitWidth() - ShiftAmt);
3214 const APInt &Shift) {
3215 assert(
Hi.getBitWidth() ==
Lo.getBitWidth());
3219 return Hi.shl(
Hi.getBitWidth() - ShiftAmt) |
Lo.lshr(ShiftAmt);
3224 assert(BW == RHS.getBitWidth() &&
"Operand mismatch");
3225 APInt Result(BW, 0);
3226 for (
unsigned I :
seq(std::min(RHS.getActiveBits(), BW - LHS.countr_zero())))
3233 assert(LHS.getBitWidth() == RHS.getBitWidth());
3234 return clmul(LHS.reverseBits(), RHS.reverseBits()).reverseBits();
3238 assert(LHS.getBitWidth() == RHS.getBitWidth());
3239 return clmulr(LHS, RHS).lshr(1);
3244 assert(BW == Mask.getBitWidth() &&
"Operand mismatch");
3246 for (
unsigned I = 0,
P = 0;
I != BW; ++
I)
3248 Result.setBitVal(
P++, Val[
I]);
3254 assert(BW == Mask.getBitWidth() &&
"Operand mismatch");
3256 for (
unsigned I = 0,
P = 0;
I != BW; ++
I)
3258 Result.setBitVal(
I, Val[
P++]);
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 constexpr unsigned long long mask(BlockVerifier::State S)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#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 bool isSigned(unsigned Opcode)
This file defines a hash set that can be used to remove duplication of nodes in a graph.
static uint64_t clearUnusedBits(uint64_t Val, unsigned Size)
Provides some synthesis utilities to produce sequences of values.
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
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
APInt(unsigned numBits, uint64_t val, bool isSigned=false, bool implicitTrunc=false)
Create a new APInt of numBits width, initialized as val.
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 truncSSatU(unsigned width) const
Truncate to new width with signed saturation to unsigned result.
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
LLVM_ABI APInt sqrtFloor() const
Compute the floor of the square root of the unsigned value.
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.
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 to signed result.
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.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
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...
Represent a constant reference to a string, i.e.
constexpr bool empty() const
Check if the string is empty.
constexpr size_t size() const
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 clmulr(const APInt &LHS, const APInt &RHS)
Perform a reversed carry-less multiply.
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 compressBits(const APInt &Val, const APInt &Mask)
Perform a "compress" operation, also known as pext or bext.
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 clmul(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, also known as XOR multiplication, and return low-bits.
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 clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
LLVM_ABI APInt expandBits(const APInt &Val, const APInt &Mask)
Perform an "expand" operation, also known as pdep or bdep.
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ 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)
LLVM_ABI void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes)
Fills the StoreBytes bytes of memory starting from Dst with the integer held in IntVal.
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
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.
FunctionAddr VTableAddr Count
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.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_READONLY LLVM_ABI APFloat exp(const APFloat &X, RoundingMode RM=APFloat::rmNearestTiesToEven)
Implement IEEE 754-2019 exp functions.
@ Mod
The access may modify the value stored in memory.
To bit_cast(const From &from) noexcept
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned BitWidth
constexpr T reverseBits(T Val)
Reverse the bits in Val.
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.
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
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)
Loads the integer stored in the LoadBytes bytes starting from Src into IntVal, which is assumed to be...
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...