31 #define DEBUG_TYPE "apint"
36 uint64_t * result =
new uint64_t[numWords];
37 assert(result &&
"APInt memory allocation fails!");
38 memset(result, 0, numWords *
sizeof(uint64_t));
44 inline static uint64_t*
getMemory(
unsigned numWords) {
45 uint64_t * result =
new uint64_t[numWords];
46 assert(result &&
"APInt memory allocation fails!");
51 inline static unsigned getDigit(
char cdigit, uint8_t radix) {
54 if (radix == 16 || radix == 36) {
78 void APInt::initSlowCase(uint64_t val,
bool isSigned) {
81 if (isSigned && int64_t(val) < 0)
86 void APInt::initSlowCase(
const APInt& that) {
92 assert(BitWidth &&
"Bitwidth too small");
93 assert(bigVal.
data() &&
"Null pointer detected!");
102 memcpy(
pVal, bigVal.
data(), words * APINT_WORD_SIZE);
109 : BitWidth(numBits), VAL(0) {
110 initFromArray(bigVal);
113 APInt::APInt(
unsigned numBits,
unsigned numWords,
const uint64_t bigVal[])
114 : BitWidth(numBits), VAL(0) {
119 : BitWidth(numbits), VAL(0) {
120 assert(BitWidth &&
"Bitwidth too small");
121 fromString(numbits, Str, radix);
124 APInt& APInt::AssignSlowCase(
const APInt& RHS) {
136 if (isSingleWord()) {
138 assert(!RHS.isSingleWord());
144 else if (RHS.isSingleWord()) {
152 BitWidth = RHS.BitWidth;
153 return clearUnusedBits();
163 return clearUnusedBits();
170 if (isSingleWord()) {
176 for (
unsigned i = 0;
i < NumWords; ++
i)
184 static bool add_1(uint64_t dest[], uint64_t x[],
unsigned len, uint64_t y) {
185 for (
unsigned i = 0;
i < len; ++
i) {
203 return clearUnusedBits();
212 static bool sub_1(uint64_t x[],
unsigned len, uint64_t y) {
213 for (
unsigned i = 0;
i < len; ++
i) {
232 return clearUnusedBits();
239 static bool add(uint64_t *dest,
const uint64_t *x,
const uint64_t *y,
242 for (
unsigned i = 0;
i< len; ++
i) {
244 dest[
i] = x[
i] + y[
i] + carry;
245 carry = dest[
i] < limit || (carry && dest[
i] == limit);
254 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
260 return clearUnusedBits();
268 return clearUnusedBits();
274 static bool sub(uint64_t *dest,
const uint64_t *x,
const uint64_t *y,
277 for (
unsigned i = 0;
i < len; ++
i) {
278 uint64_t x_tmp = borrow ? x[
i] - 1 : x[
i];
279 borrow = y[
i] > x_tmp || (borrow && x[
i] == 0);
280 dest[
i] = x_tmp - y[
i];
289 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
294 return clearUnusedBits();
302 return clearUnusedBits();
309 static uint64_t
mul_1(uint64_t dest[], uint64_t x[],
unsigned len, uint64_t y) {
311 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
315 for (
unsigned i = 0;
i < len; ++
i) {
317 uint64_t lx = x[
i] & 0xffffffffULL;
318 uint64_t hx = x[
i] >> 32;
323 uint8_t hasCarry = 0;
324 dest[
i] = carry + lx * ly;
326 hasCarry = (dest[
i] < carry) ? 1 : 0;
327 carry = hx * ly + (dest[
i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
330 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
332 carry += (lx * hy) & 0xffffffffULL;
333 dest[
i] = (carry << 32) | (dest[
i] & 0xffffffffULL);
334 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
335 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
343 static void mul(uint64_t dest[], uint64_t x[],
unsigned xlen, uint64_t y[],
345 dest[xlen] =
mul_1(dest, x, xlen, y[0]);
346 for (
unsigned i = 1;
i < ylen; ++
i) {
347 uint64_t ly = y[
i] & 0xffffffffULL, hy = y[
i] >> 32;
348 uint64_t carry = 0, lx = 0, hx = 0;
349 for (
unsigned j = 0; j < xlen; ++j) {
350 lx = x[j] & 0xffffffffULL;
356 uint8_t hasCarry = 0;
357 uint64_t resul = carry + lx * ly;
358 hasCarry = (resul < carry) ? 1 : 0;
359 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
360 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
362 carry += (lx * hy) & 0xffffffffULL;
363 resul = (carry << 32) | (resul & 0xffffffffULL);
365 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
366 (carry >> 32) + (dest[
i+j] < resul ? 1 : 0) +
367 ((lx * hy) >> 32) + hx * hy;
369 dest[
i+xlen] = carry;
374 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
375 if (isSingleWord()) {
383 unsigned lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
390 unsigned rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1;
398 unsigned destWords = rhsWords + lhsWords;
407 memcpy(
pVal, dest, wordsToCopy * APINT_WORD_SIZE);
416 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
417 if (isSingleWord()) {
422 for (
unsigned i = 0;
i < numWords; ++
i)
428 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
429 if (isSingleWord()) {
434 for (
unsigned i = 0;
i < numWords; ++
i)
440 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
441 if (isSingleWord()) {
443 this->clearUnusedBits();
447 for (
unsigned i = 0;
i < numWords; ++
i)
449 return clearUnusedBits();
452 APInt APInt::AndSlowCase(
const APInt& RHS)
const {
455 for (
unsigned i = 0;
i < numWords; ++
i)
460 APInt APInt::OrSlowCase(
const APInt& RHS)
const {
463 for (
unsigned i = 0;
i < numWords; ++
i)
468 APInt APInt::XorSlowCase(
const APInt& RHS)
const {
471 for (
unsigned i = 0;
i < numWords; ++
i)
476 Result.clearUnusedBits();
481 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
483 return APInt(BitWidth, VAL * RHS.
VAL);
489 bool APInt::EqualSlowCase(
const APInt& RHS)
const {
493 bool APInt::EqualSlowCase(uint64_t Val)
const {
495 if (n <= APINT_BITS_PER_WORD)
496 return pVal[0] == Val;
502 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be same for comparison");
504 return VAL < RHS.
VAL;
519 if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
523 unsigned topWord = whichWord(std::max(n1,n2)-1);
524 for (
int i = topWord;
i >= 0; --
i) {
534 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be same for comparison");
535 if (isSingleWord()) {
538 return lhsSext < rhsSext;
545 if (lhsNeg != rhsNeg)
555 VAL |= maskBit(bitPosition);
557 pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
564 VAL &= ~maskBit(bitPosition);
566 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
575 assert(bitPosition < BitWidth &&
"Out of the bit-width range!");
576 if ((*
this)[bitPosition])
clearBit(bitPosition);
582 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
584 "Radix should be 2, 8, 10, 16, or 36!");
586 size_t slen = str.
size();
591 if (*p ==
'-' || *p ==
'+') {
594 assert(slen &&
"String is only a sign, needs a value.");
617 = radix == 10? (slen == 1 ? 4 : slen * 64/18)
618 : (slen == 1 ? 7 : slen * 16/3);
626 if (log == (
unsigned)-1) {
627 return isNegative + 1;
629 return isNegative + log + 1;
634 if (Arg.isSingleWord())
642 "SplatSizeInBits must divide width!");
645 return *
this ==
rotl(SplatSizeInBits);
659 unsigned APInt::countLeadingZerosSlowCase()
const {
664 Count += APINT_BITS_PER_WORD;
671 unsigned Mod = BitWidth % APINT_BITS_PER_WORD;
672 Count -= Mod > 0 ? APINT_BITS_PER_WORD - Mod : 0;
680 unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD;
683 highWordBits = APINT_BITS_PER_WORD;
686 shift = APINT_BITS_PER_WORD - highWordBits;
690 if (Count == highWordBits) {
691 for (i--; i >= 0; --
i) {
692 if (
pVal[i] == -1ULL)
693 Count += APINT_BITS_PER_WORD;
709 Count += APINT_BITS_PER_WORD;
715 unsigned APInt::countTrailingOnesSlowCase()
const {
719 Count += APINT_BITS_PER_WORD;
725 unsigned APInt::countPopulationSlowCase()
const {
734 static void lshrNear(uint64_t *Dst, uint64_t *Src,
unsigned Words,
737 for (
int I = Words - 1;
I >= 0; --
I) {
738 uint64_t Tmp = Src[
I];
739 Dst[
I] = (Tmp >> Shift) | Carry;
740 Carry = Tmp << (64 - Shift);
745 assert(BitWidth >= 16 && BitWidth % 16 == 0 &&
"Cannot byteswap!");
750 if (BitWidth == 48) {
751 unsigned Tmp1 =
unsigned(VAL >> 16);
753 uint16_t Tmp2 = uint16_t(VAL);
755 return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1);
763 if (Result.BitWidth != BitWidth) {
765 Result.BitWidth - BitWidth);
766 Result.BitWidth = BitWidth;
774 return APInt(BitWidth, llvm::reverseBits<uint64_t>(VAL));
776 return APInt(BitWidth, llvm::reverseBits<uint32_t>(VAL));
778 return APInt(BitWidth, llvm::reverseBits<uint16_t>(VAL));
780 return APInt(BitWidth, llvm::reverseBits<uint8_t>(VAL));
786 APInt Reversed(*
this);
787 int S = BitWidth - 1;
789 const APInt One(BitWidth, 1);
791 for ((Val = Val.
lshr(1)); Val != 0; (Val = Val.
lshr(1))) {
793 Reversed |= (Val & One);
820 bool isNeg =
T.I >> 63;
823 int64_t exp = ((
T.I >> 52) & 0x7ff) - 1023;
827 return APInt(width, 0u);
830 uint64_t mantissa = (
T.I & (~0ULL >> 12)) | 1ULL << 52;
834 return isNeg ? -
APInt(width, mantissa >> (52 - exp)) :
835 APInt(width, mantissa >> (52 - exp));
839 if (width <= exp - 52)
840 return APInt(width, 0);
843 APInt Tmp(width, mantissa);
844 Tmp = Tmp.
shl((
unsigned)exp - 52);
845 return isNeg ? -Tmp : Tmp;
859 if (isSingleWord() ||
getActiveBits() <= APINT_BITS_PER_WORD) {
864 return double(getWord(0));
868 bool isNeg = isSigned ? (*this)[BitWidth-1] :
false;
871 APInt Tmp(isNeg ? -(*
this) : (*
this));
883 if (!isSigned || !isNeg)
884 return std::numeric_limits<double>::infinity();
886 return -std::numeric_limits<double>::infinity();
893 unsigned hiWord = whichWord(n-1);
895 mantissa = Tmp.
pVal[0];
899 assert(hiWord > 0 &&
"huh?");
900 uint64_t hibits = Tmp.
pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
901 uint64_t lobits = Tmp.
pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
902 mantissa = hibits | lobits;
906 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
911 T.I = sign | (exp << 52) | mantissa;
917 assert(width < BitWidth &&
"Invalid APInt Truncate request");
918 assert(width &&
"Can't truncate to 0 bits");
920 if (width <= APINT_BITS_PER_WORD)
927 for (i = 0; i != width / APINT_BITS_PER_WORD; i++)
928 Result.
pVal[i] = pVal[i];
931 unsigned bits = (0 - width) % APINT_BITS_PER_WORD;
940 assert(width > BitWidth &&
"Invalid APInt SignExtend request");
942 if (width <= APINT_BITS_PER_WORD) {
943 uint64_t val = VAL << (APINT_BITS_PER_WORD - BitWidth);
944 val = (int64_t)val >> (width - BitWidth);
945 return APInt(width, val >> (APINT_BITS_PER_WORD - width));
953 for (i = 0; i != BitWidth / APINT_BITS_PER_WORD; i++) {
955 Result.
pVal[
i] = word;
959 unsigned bits = (0 - BitWidth) % APINT_BITS_PER_WORD;
963 word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
966 for (; i != width / APINT_BITS_PER_WORD; i++) {
967 Result.
pVal[
i] = word;
968 word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
972 bits = (0 - width) % APINT_BITS_PER_WORD;
981 assert(width > BitWidth &&
"Invalid APInt ZeroExtend request");
983 if (width <= APINT_BITS_PER_WORD)
984 return APInt(width, VAL);
1000 if (BitWidth < width)
1002 if (BitWidth > width)
1003 return trunc(width);
1008 if (BitWidth < width)
1010 if (BitWidth > width)
1011 return trunc(width);
1016 if (BitWidth < width)
1022 if (BitWidth < width)
1036 assert(shiftAmt <= BitWidth &&
"Invalid shift amount");
1042 if (isSingleWord()) {
1043 if (shiftAmt == BitWidth)
1044 return APInt(BitWidth, 0);
1051 if (shiftAmt == BitWidth) {
1053 return APInt(BitWidth, -1ULL,
true);
1055 return APInt(BitWidth, 0);
1062 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1063 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1065 unsigned bitsInWord = whichBit(BitWidth);
1066 if (bitsInWord == 0)
1067 bitsInWord = APINT_BITS_PER_WORD;
1070 if (wordShift == 0) {
1072 for (
unsigned i = 0; i <= breakWord; ++
i)
1073 val[i] = pVal[i+offset];
1077 if (bitsInWord < APINT_BITS_PER_WORD)
1078 val[breakWord] |= ~0ULL << bitsInWord;
1081 for (
unsigned i = 0; i < breakWord; ++
i) {
1084 val[
i] = (pVal[i+offset] >> wordShift) |
1085 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1090 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1095 if (wordShift > bitsInWord) {
1098 ~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord));
1099 val[breakWord] |= ~0ULL;
1101 val[breakWord] |= (~0ULL << (bitsInWord - wordShift));
1106 uint64_t fillValue = (
isNegative() ? -1ULL : 0);
1109 APInt Result(val, BitWidth);
1110 Result.clearUnusedBits();
1123 if (isSingleWord()) {
1124 if (shiftAmt >= BitWidth)
1125 return APInt(BitWidth, 0);
1127 return APInt(BitWidth, this->VAL >> shiftAmt);
1133 if (shiftAmt >= BitWidth)
1134 return APInt(BitWidth, 0);
1146 if (shiftAmt < APINT_BITS_PER_WORD) {
1148 APInt Result(val, BitWidth);
1149 Result.clearUnusedBits();
1154 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1155 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1158 if (wordShift == 0) {
1160 val[i] = pVal[i+offset];
1163 APInt Result(val, BitWidth);
1164 Result.clearUnusedBits();
1170 for (
unsigned i = 0; i < breakWord; ++
i)
1171 val[i] = (pVal[i+offset] >> wordShift) |
1172 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1174 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1179 APInt Result(val, BitWidth);
1180 Result.clearUnusedBits();
1191 APInt APInt::shlSlowCase(
unsigned shiftAmt)
const {
1195 if (shiftAmt == BitWidth)
1196 return APInt(BitWidth, 0);
1208 if (shiftAmt < APINT_BITS_PER_WORD) {
1211 val[
i] = pVal[
i] << shiftAmt | carry;
1212 carry = pVal[
i] >> (APINT_BITS_PER_WORD - shiftAmt);
1214 APInt Result(val, BitWidth);
1215 Result.clearUnusedBits();
1220 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1221 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1224 if (wordShift == 0) {
1225 for (
unsigned i = 0; i < offset; i++)
1228 val[i] = pVal[i-offset];
1229 APInt Result(val, BitWidth);
1230 Result.clearUnusedBits();
1236 for (; i > offset; --
i)
1237 val[i] = pVal[i-offset] << wordShift |
1238 pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift);
1239 val[offset] = pVal[0] << wordShift;
1240 for (i = 0; i < offset; ++
i)
1242 APInt Result(val, BitWidth);
1243 Result.clearUnusedBits();
1252 rotateAmt %= BitWidth;
1255 return shl(rotateAmt) |
lshr(BitWidth - rotateAmt);
1263 rotateAmt %= BitWidth;
1266 return lshr(rotateAmt) |
shl(BitWidth - rotateAmt);
1283 if (magnitude <= 5) {
1284 static const uint8_t results[32] = {
1289 4, 4, 4, 4, 4, 4, 4, 4,
1290 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1293 return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]);
1300 if (magnitude < 52) {
1301 return APInt(BitWidth,
1302 uint64_t(::
round(::
sqrt(
double(isSingleWord()?VAL:pVal[0])))));
1310 unsigned nbits = BitWidth, i = 4;
1311 APInt testy(BitWidth, 16);
1312 APInt x_old(BitWidth, 1);
1313 APInt x_new(BitWidth, 0);
1314 APInt two(BitWidth, 2);
1317 for (;; i += 2, testy = testy.
shl(2))
1318 if (i >= nbits || this->
ule(testy)) {
1319 x_old = x_old.
shl(i / 2);
1325 x_new = (this->
udiv(x_old) + x_old).
udiv(two);
1326 if (x_old.
ule(x_new))
1337 APInt square(x_old * x_old);
1338 APInt nextSquare((x_old + 1) * (x_old +1));
1339 if (this->
ult(square))
1341 assert(this->
ule(nextSquare) &&
"Error in APInt::sqrt computation");
1342 APInt midpoint((nextSquare - square).
udiv(two));
1343 APInt offset(*
this - square);
1344 if (offset.
ult(midpoint))
1355 assert(
ult(modulo) &&
"This APInt must be smaller than the modulo");
1365 APInt r[2] = { modulo, *
this };
1367 APInt q(BitWidth, 0);
1370 for (i = 0; r[i^1] != 0; i ^= 1) {
1375 udivrem(r[i], r[i^1], q, r[i]);
1384 return APInt(BitWidth, 0);
1398 const APInt& d = *
this;
1406 anc = t - 1 - t.
urem(ad);
1408 q1 = signedMin.
udiv(anc);
1409 r1 = signedMin - q1*anc;
1410 q2 = signedMin.
udiv(ad);
1411 r2 = signedMin - q2*ad;
1427 }
while (q1.
ult(delta) || (q1 == delta && r1 == 0));
1442 const APInt& d = *
this;
1451 nc = allOnes - (allOnes - d).
urem(d);
1453 q1 = signedMin.
udiv(nc);
1454 r1 = signedMin - q1*nc;
1455 q2 = signedMax.
udiv(d);
1456 r2 = signedMax - q2*d;
1459 if (r1.
uge(nc - r1)) {
1467 if ((r2 + 1).uge(d - r2)) {
1468 if (q2.
uge(signedMax)) magu.
a = 1;
1473 if (q2.
uge(signedMin)) magu.
a = 1;
1478 }
while (p < d.getBitWidth()*2 &&
1479 (q1.
ult(delta) || (q1 == delta && r1 == 0)));
1481 magu.
s = p - d.getBitWidth();
1489 static void KnuthDiv(
unsigned *u,
unsigned *v,
unsigned *q,
unsigned* r,
1490 unsigned m,
unsigned n) {
1491 assert(u &&
"Must provide dividend");
1492 assert(v &&
"Must provide divisor");
1493 assert(q &&
"Must provide quotient");
1494 assert(u != v && u != q && v != q &&
"Must use different memory");
1495 assert(n>1 &&
"n must be > 1");
1498 const uint64_t b = uint64_t(1) << 32;
1500 DEBUG(
dbgs() <<
"KnuthDiv: m=" << m <<
" n=" << n <<
'\n');
1502 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1504 DEBUG(
for (
int i = n; i >0; i--)
dbgs() <<
" " << v[i-1]);
1515 unsigned v_carry = 0;
1516 unsigned u_carry = 0;
1518 for (
unsigned i = 0; i < m+n; ++
i) {
1519 unsigned u_tmp = u[
i] >> (32 - shift);
1520 u[
i] = (u[
i] << shift) | u_carry;
1523 for (
unsigned i = 0; i < n; ++
i) {
1524 unsigned v_tmp = v[
i] >> (32 - shift);
1525 v[
i] = (v[
i] << shift) | v_carry;
1532 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1534 DEBUG(
for (
int i = n; i >0; i--)
dbgs() <<
" " << v[i-1]);
1540 DEBUG(
dbgs() <<
"KnuthDiv: quotient digit #" << j <<
'\n');
1549 uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]);
1550 DEBUG(
dbgs() <<
"KnuthDiv: dividend == " << dividend <<
'\n');
1551 uint64_t qp = dividend / v[n-1];
1552 uint64_t rp = dividend % v[n-1];
1553 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1556 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
1559 DEBUG(
dbgs() <<
"KnuthDiv: qp == " << qp <<
", rp == " << rp <<
'\n');
1570 for (
unsigned i = 0; i < n; ++
i) {
1571 uint64_t p = uint64_t(qp) * uint64_t(v[i]);
1572 int64_t subres = int64_t(u[j+i]) - borrow - (
unsigned)p;
1574 borrow = (p >> 32) - (subres >> 32);
1575 DEBUG(
dbgs() <<
"KnuthDiv: u[j+i] = " << u[j+i]
1576 <<
", borrow = " << borrow <<
'\n');
1578 bool isNeg = u[j+n] < borrow;
1581 DEBUG(
dbgs() <<
"KnuthDiv: after subtraction:");
1582 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1597 for (
unsigned i = 0; i < n; i++) {
1598 unsigned limit =
std::min(u[j+i],v[i]);
1599 u[j+
i] += v[
i] + carry;
1600 carry = u[j+
i] < limit || (carry && u[j+
i] == limit);
1604 DEBUG(
dbgs() <<
"KnuthDiv: after correction:");
1605 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1606 DEBUG(
dbgs() <<
"\nKnuthDiv: digit result = " << q[j] <<
'\n');
1612 DEBUG(
for (
int i = m; i >=0; i--)
dbgs() <<
" " << q[i]);
1624 DEBUG(
dbgs() <<
"KnuthDiv: remainder:");
1625 for (
int i = n-1; i >= 0; i--) {
1626 r[
i] = (u[
i] >> shift) | carry;
1627 carry = u[
i] << (32 - shift);
1631 for (
int i = n-1; i >= 0; i--) {
1641 void APInt::divide(
const APInt &LHS,
unsigned lhsWords,
const APInt &RHS,
1642 unsigned rhsWords,
APInt *Quotient,
APInt *Remainder) {
1643 assert(lhsWords >= rhsWords &&
"Fractional result");
1652 uint64_t mask = ~0ull >> (
sizeof(
unsigned)*CHAR_BIT);
1653 unsigned n = rhsWords * 2;
1654 unsigned m = (lhsWords * 2) - n;
1658 unsigned SPACE[128];
1659 unsigned *U =
nullptr;
1660 unsigned *V =
nullptr;
1661 unsigned *Q =
nullptr;
1662 unsigned *R =
nullptr;
1663 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1666 Q = &SPACE[(m+n+1) + n];
1668 R = &SPACE[(m+n+1) + n + (m+n)];
1670 U =
new unsigned[m + n + 1];
1671 V =
new unsigned[n];
1672 Q =
new unsigned[m+n];
1674 R =
new unsigned[n];
1678 memset(U, 0, (m+n+1)*
sizeof(
unsigned));
1679 for (
unsigned i = 0; i < lhsWords; ++
i) {
1680 uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[
i]);
1682 U[i * 2 + 1] = (
unsigned)(tmp >> (
sizeof(
unsigned)*CHAR_BIT));
1687 memset(V, 0, (n)*
sizeof(
unsigned));
1688 for (
unsigned i = 0; i < rhsWords; ++
i) {
1691 V[i * 2 + 1] = (
unsigned)(tmp >> (
sizeof(
unsigned)*CHAR_BIT));
1695 memset(Q, 0, (m+n) *
sizeof(
unsigned));
1697 memset(R, 0, n *
sizeof(
unsigned));
1703 for (
unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1707 for (
unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1716 assert(n != 0 &&
"Divide by zero?");
1718 unsigned divisor = V[0];
1719 unsigned remainder = 0;
1720 for (
int i = m+n-1; i >= 0; i--) {
1721 uint64_t partial_dividend = uint64_t(remainder) << 32 | U[
i];
1722 if (partial_dividend == 0) {
1725 }
else if (partial_dividend < divisor) {
1727 remainder = (
unsigned)partial_dividend;
1728 }
else if (partial_dividend == divisor) {
1732 Q[
i] = (
unsigned)(partial_dividend / divisor);
1733 remainder = (
unsigned)(partial_dividend - (Q[i] * divisor));
1747 if (Quotient->BitWidth != LHS.BitWidth) {
1748 if (Quotient->isSingleWord())
1751 delete [] Quotient->pVal;
1752 Quotient->BitWidth = LHS.BitWidth;
1753 if (!Quotient->isSingleWord())
1756 Quotient->clearAllBits();
1762 if (lhsWords == 1) {
1764 uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2));
1765 if (Quotient->isSingleWord())
1766 Quotient->VAL = tmp;
1768 Quotient->pVal[0] = tmp;
1770 assert(!Quotient->isSingleWord() &&
"Quotient APInt not large enough");
1771 for (
unsigned i = 0; i < lhsWords; ++
i)
1773 uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1780 if (Remainder->BitWidth != RHS.BitWidth) {
1781 if (Remainder->isSingleWord())
1784 delete [] Remainder->pVal;
1785 Remainder->BitWidth = RHS.BitWidth;
1786 if (!Remainder->isSingleWord())
1789 Remainder->clearAllBits();
1793 if (rhsWords == 1) {
1795 uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2));
1796 if (Remainder->isSingleWord())
1797 Remainder->VAL = tmp;
1799 Remainder->pVal[0] = tmp;
1801 assert(!Remainder->isSingleWord() &&
"Remainder APInt not large enough");
1802 for (
unsigned i = 0; i < rhsWords; ++
i)
1803 Remainder->pVal[i] =
1804 uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1809 if (U != &SPACE[0]) {
1818 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1821 if (isSingleWord()) {
1822 assert(RHS.
VAL != 0 &&
"Divide by zero?");
1823 return APInt(BitWidth, VAL / RHS.
VAL);
1828 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1829 assert(rhsWords &&
"Divided by zero???");
1831 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1836 return APInt(BitWidth, 0);
1837 else if (lhsWords < rhsWords || this->
ult(RHS)) {
1839 return APInt(BitWidth, 0);
1840 }
else if (*
this == RHS) {
1842 return APInt(BitWidth, 1);
1843 }
else if (lhsWords == 1 && rhsWords == 1) {
1845 return APInt(BitWidth, this->pVal[0] / RHS.
pVal[0]);
1849 APInt Quotient(1,0);
1850 divide(*
this, lhsWords, RHS, rhsWords, &Quotient,
nullptr);
1857 return (-(*
this)).
udiv(-RHS);
1858 return -((-(*this)).
udiv(RHS));
1861 return -(this->
udiv(-RHS));
1862 return this->
udiv(RHS);
1866 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1867 if (isSingleWord()) {
1868 assert(RHS.
VAL != 0 &&
"Remainder by zero?");
1869 return APInt(BitWidth, VAL % RHS.
VAL);
1874 unsigned lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1);
1878 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1879 assert(rhsWords &&
"Performing remainder operation by zero ???");
1882 if (lhsWords == 0) {
1884 return APInt(BitWidth, 0);
1885 }
else if (lhsWords < rhsWords || this->
ult(RHS)) {
1888 }
else if (*
this == RHS) {
1890 return APInt(BitWidth, 0);
1891 }
else if (lhsWords == 1) {
1893 return APInt(BitWidth, pVal[0] % RHS.
pVal[0]);
1897 APInt Remainder(1,0);
1898 divide(*
this, lhsWords, RHS, rhsWords,
nullptr, &Remainder);
1905 return -((-(*
this)).urem(-RHS));
1906 return -((-(*this)).
urem(RHS));
1909 return this->
urem(-RHS);
1910 return this->
urem(RHS);
1915 assert(LHS.BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1918 if (LHS.isSingleWord()) {
1919 assert(RHS.
VAL != 0 &&
"Divide by zero?");
1920 uint64_t QuotVal = LHS.
VAL / RHS.
VAL;
1921 uint64_t RemVal = LHS.
VAL % RHS.
VAL;
1922 Quotient =
APInt(LHS.BitWidth, QuotVal);
1923 Remainder =
APInt(LHS.BitWidth, RemVal);
1929 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1931 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1934 if (lhsWords == 0) {
1940 if (lhsWords < rhsWords || LHS.
ult(RHS)) {
1952 if (lhsWords == 1 && rhsWords == 1) {
1954 uint64_t lhsValue = LHS.isSingleWord() ? LHS.
VAL : LHS.
pVal[0];
1955 uint64_t rhsValue = RHS.isSingleWord() ? RHS.
VAL : RHS.
pVal[0];
1962 divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder);
1972 Quotient = -Quotient;
1974 Remainder = -Remainder;
1977 Quotient = -Quotient;
1984 APInt Res = *
this+RHS;
1991 APInt Res = *
this+RHS;
1992 Overflow = Res.
ult(RHS);
1997 APInt Res = *
this - RHS;
2004 APInt Res = *
this-RHS;
2005 Overflow = Res.
ugt(*
this);
2016 APInt Res = *
this * RHS;
2018 if (*
this != 0 && RHS != 0)
2019 Overflow = Res.
sdiv(RHS) != *
this || Res.
sdiv(*
this) != RHS;
2026 APInt Res = *
this * RHS;
2028 if (*
this != 0 && RHS != 0)
2029 Overflow = Res.
udiv(RHS) != *
this || Res.
udiv(*
this) != RHS;
2038 return APInt(BitWidth, 0);
2045 return *
this << ShAmt;
2051 return APInt(BitWidth, 0);
2055 return *
this << ShAmt;
2061 void APInt::fromString(
unsigned numbits,
StringRef str, uint8_t radix) {
2064 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
2066 "Radix should be 2, 8, 10, 16, or 36!");
2069 size_t slen = str.
size();
2070 bool isNeg = *p ==
'-';
2071 if (*p ==
'-' || *p ==
'+') {
2074 assert(slen &&
"String is only a sign, needs a value.");
2076 assert((slen <= numbits || radix != 2) &&
"Insufficient bit width");
2077 assert(((slen-1)*3 <= numbits || radix != 8) &&
"Insufficient bit width");
2078 assert(((slen-1)*4 <= numbits || radix != 16) &&
"Insufficient bit width");
2079 assert((((slen-1)*64)/22 <= numbits || radix != 10) &&
2080 "Insufficient bit width");
2083 if (!isSingleWord())
2087 unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
2096 unsigned digit =
getDigit(*p, radix);
2097 assert(digit < radix &&
"Invalid character in digit string");
2108 if (apdigit.isSingleWord())
2109 apdigit.VAL = digit;
2111 apdigit.pVal[0] = digit;
2122 bool Signed,
bool formatAsCLiteral)
const {
2123 assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
2125 "Radix should be 2, 8, 10, 16, or 36!");
2128 if (formatAsCLiteral) {
2158 static const char Digits[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2160 if (isSingleWord()) {
2162 char *BufPtr = Buffer+65;
2183 *--BufPtr = Digits[N % Radix];
2186 Str.
append(BufPtr, Buffer+65);
2207 unsigned StartDig = Str.
size();
2212 if (Radix == 2 || Radix == 8 || Radix == 16) {
2214 unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
2215 unsigned MaskAmt = Radix - 1;
2220 Tmp = Tmp.
lshr(ShiftAmt);
2223 APInt divisor(Radix == 10? 4 : 8, Radix);
2225 APInt APdigit(1, 0);
2230 assert(Digit < Radix &&
"divide failed");
2253 dbgs() <<
"APInt(" << BitWidth <<
"b, "
2254 << U <<
"u " << S <<
"s)";
2259 this->
toString(S, 10, isSigned,
false);
2268 static_assert(
integerPartWidth % 2 == 0,
"Part width must be divisible by 2!");
2276 lowBitMask(
unsigned int bits)
2324 for (i = 1; i < parts; i++)
2334 for (i = 0; i < parts; i++)
2344 for (i = 0; i < parts; i++)
2379 unsigned int i, lsb;
2381 for (i = 0; i < n; i++) {
2382 if (parts[i] != 0) {
2383 lsb = partLSB(parts[i]);
2402 if (parts[n] != 0) {
2403 msb = partMSB(parts[n]);
2418 unsigned int srcBits,
unsigned int srcLSB)
2420 unsigned int firstSrcPart, dstParts, shift, n;
2423 assert(dstParts <= dstCount);
2426 tcAssign (dst, src + firstSrcPart, dstParts);
2437 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
2439 }
else if (n > srcBits) {
2441 dst[dstParts - 1] &= lowBitMask (srcBits % integerPartWidth);
2445 while (dstParts < dstCount)
2446 dst[dstParts++] = 0;
2458 for (i = 0; i < parts; i++) {
2463 dst[
i] += rhs[
i] + 1;
2483 for (i = 0; i < parts; i++) {
2488 dst[
i] -= rhs[
i] + 1;
2521 unsigned int srcParts,
unsigned int dstParts,
2527 assert(dst <= src || dst >= src + srcParts);
2528 assert(dstParts <= srcParts + 1);
2531 n = dstParts < srcParts ? dstParts: srcParts;
2533 for (i = 0; i < n; i++) {
2546 if (multiplier == 0 || srcPart == 0) {
2550 low = lowHalf(srcPart) * lowHalf(multiplier);
2551 high = highHalf(srcPart) * highHalf(multiplier);
2553 mid = lowHalf(srcPart) * highHalf(multiplier);
2554 high += highHalf(mid);
2556 if (low + mid < low)
2560 mid = highHalf(srcPart) * lowHalf(multiplier);
2561 high += highHalf(mid);
2563 if (low + mid < low)
2568 if (low + carry < low)
2575 if (low + dst[i] < low)
2586 assert(i + 1 == dstParts);
2598 for (; i < srcParts; i++)
2618 assert(dst != lhs && dst != rhs);
2621 tcSet(dst, 0, parts);
2623 for (i = 0; i < parts; i++)
2637 unsigned int rhsParts)
2640 if (lhsParts > rhsParts) {
2645 assert(dst != lhs && dst != rhs);
2647 tcSet(dst, 0, rhsParts);
2649 for (n = 0; n < lhsParts; n++)
2650 tcMultiplyPart(&dst[n], rhs, lhs[n], 0, rhsParts, rhsParts + 1,
true);
2652 n = lhsParts + rhsParts;
2654 return n - (dst[n - 1] == 0);
2673 unsigned int n, shiftCount;
2676 assert(lhs != remainder && lhs != srhs && remainder != srhs);
2678 shiftCount =
tcMSB(rhs, parts) + 1;
2679 if (shiftCount == 0)
2689 tcSet(lhs, 0, parts);
2696 compare =
tcCompare(remainder, srhs, parts);
2702 if (shiftCount == 0)
2706 if ((mask >>= 1) == 0) {
2721 unsigned int jump, shift;
2727 while (parts > jump) {
2734 part = dst[parts - jump];
2737 if (parts >= jump + 1)
2755 unsigned int i, jump, shift;
2763 for (i = 0; i < parts; i++) {
2766 if (i + jump >= parts) {
2769 part = dst[i + jump];
2772 if (i + jump + 1 < parts)
2788 for (i = 0; i < parts; i++)
2798 for (i = 0; i < parts; i++)
2808 for (i = 0; i < parts; i++)
2818 for (i = 0; i < parts; i++)
2829 if (lhs[parts] == rhs[parts])
2832 if (lhs[parts] > rhs[parts])
2847 for (i = 0; i < parts; i++)
2857 for (
unsigned int i = 0; i < parts; i++) {
void clearAllBits()
Set every bit to 0.
APInt ashr(unsigned shiftAmt) const
Arithmetic right-shift function.
APInt multiplicativeInverse(const APInt &modulo) const
Computes the multiplicative inverse of this APInt for a given modulo.
void push_back(const T &Elt)
static void tcExtract(integerPart *, unsigned int dstCount, const integerPart *, unsigned int srcBits, unsigned int srcLSB)
Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to DST, of dstCOUNT parts...
mu magicu(unsigned LeadingZeros=0) const
Calculate the magic numbers required to implement an unsigned integer division by a constant as a seq...
static void r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
static void tcXor(integerPart *, const integerPart *, unsigned int)
void flipAllBits()
Toggle every bit to its opposite value.
T findLastSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the last set bit starting from the least significant bit.
static int tcDivide(integerPart *lhs, const integerPart *rhs, integerPart *remainder, integerPart *scratch, unsigned int parts)
If RHS is zero LHS and REMAINDER are left unchanged, return one.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
uint64_t getZExtValue() const
Get zero extended value.
static void tcSetLeastSignificantBits(integerPart *, unsigned int, unsigned int bits)
Set the least significant BITS and clear the rest.
APInt & operator+=(const APInt &RHS)
Addition assignment operator.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
void dump() const
debug method
APInt GreatestCommonDivisor(const APInt &Val1, const APInt &Val2)
Compute GCD of two APInt values.
static bool add_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y)
This function adds a single "digit" integer, y, to the multiple "digit" integer array, x[].
static void KnuthDiv(unsigned *u, unsigned *v, unsigned *q, unsigned *r, unsigned m, unsigned n)
Implementation of Knuth's Algorithm D (Division of nonnegative integers) from "Art of Computer Progra...
APInt reverseBits() const
static uint64_t * getMemory(unsigned numWords)
A utility function for allocating memory and checking for allocation failure.
void setBit(unsigned bitPosition)
Set a given bit to 1.
void print(raw_ostream &OS, bool isSigned) const
static unsigned getBitsNeeded(StringRef str, uint8_t radix)
Get bits required for string value.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
APInt smul_ov(const APInt &RHS, bool &Overflow) const
static uint64_t round(uint64_t Acc, uint64_t Input)
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
ms magic() const
Calculate the magic numbers required to implement a signed integer division by a constant as a sequen...
static integerPart tcIncrement(integerPart *, unsigned int)
Increment a bignum in-place. Return the carry flag.
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
std::size_t countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the most significant bit to the least stopping at the first 1...
Magic data for optimising unsigned division by a constant.
static unsigned int tcLSB(const integerPart *, unsigned int)
Returns the bit number of the least or most significant set bit of a number.
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.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
static uint64_t allOnes(unsigned int Count)
bool isNegative() const
Determine sign of this APInt.
APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
APInt & operator*=(const APInt &RHS)
Multiplication assignment operator.
APInt urem(const APInt &LHS, const APInt &RHS)
Function for unsigned remainder operation.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
APInt()
Default constructor that creates an uninteresting APInt representing a 1-bit zero value...
std::size_t countTrailingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the least significant bit to the first zero bit.
uint64_t VAL
Used to store the <= 64 bits integer value.
static int tcMultiplyPart(integerPart *dst, const integerPart *src, integerPart multiplier, integerPart carry, unsigned int srcParts, unsigned int dstParts, bool add)
DST += SRC * MULTIPLIER + PART if add is true DST = SRC * MULTIPLIER + PART if add is false...
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
static bool tcIsZero(const integerPart *, unsigned int)
Returns true if a bignum is zero, false otherwise.
void AddInteger(signed I)
uint32_t ByteSwap_32(uint32_t Value)
ByteSwap_32 - This function returns a byte-swapped representation of the 32-bit argument, Value.
static void tcNegate(integerPart *, unsigned int)
Negate a bignum in-place.
This file implements a class to represent arbitrary precision integral constant values and operations...
bool isSplat(unsigned SplatSizeInBits) const
Check if the APInt consists of a repeated bit pattern.
APInt lshr(const APInt &LHS, unsigned shiftAmt)
Logical right-shift function.
static void tcAssign(integerPart *, const integerPart *, unsigned int)
Assign one bignum to another.
static uint64_t mul_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y)
Multiplies an integer array, x, by a uint64_t integer and places the result into dest.
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
APInt shl(unsigned shiftAmt) const
Left-shift function.
static void tcShiftLeft(integerPart *, unsigned int parts, unsigned int count)
Shift a bignum left COUNT bits.
APInt zextOrSelf(unsigned width) const
Zero extend or truncate to width.
static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y, unsigned len)
Subtracts the integer array y from the integer array x.
static void mul(uint64_t dest[], uint64_t x[], unsigned xlen, uint64_t y[], unsigned ylen)
Multiplies integer array x by integer array y and stores the result into the integer array dest...
APInt usub_ov(const APInt &RHS, bool &Overflow) const
uint16_t ByteSwap_16(uint16_t Value)
ByteSwap_16 - This function returns a byte-swapped representation of the 16-bit argument, Value.
static int tcExtractBit(const integerPart *, unsigned int bit)
Extract the given bit of a bignum; returns 0 or 1. Zero-based.
static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y, unsigned len)
This function adds the integer array x to the integer array Y and places the result in dest...
static void tcClearBit(integerPart *, unsigned int bit)
Clear the given bit of a bignum. Zero-based.
unsigned getActiveBits() const
Compute the number of active bits in the value.
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(std::begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
APInt umul_ov(const APInt &RHS, bool &Overflow) const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
APInt ushl_ov(const APInt &Amt, bool &Overflow) const
size_t size() const
size - Get the array size.
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with etc Experimental Use value profile to guide fuzzing Number of simultaneous worker processes to run the jobs If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
APInt & operator--()
Prefix decrement operator.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static integerPart tcDecrement(integerPart *, unsigned int)
Decrement a bignum in-place. Return the borrow flag.
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
static void tcSet(integerPart *, integerPart, unsigned int)
Sets the least significant part of a bignum to the input value, and zeroes out higher parts...
void toString(SmallVectorImpl< char > &Str, unsigned Radix, bool Signed, bool formatAsCLiteral=false) const
Converts an APInt to a string and append it to Str.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
std::size_t countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
APInt trunc(unsigned width) const
Truncate to new width.
APInt sextOrSelf(unsigned width) const
Sign extend or truncate to width.
APInt operator*(const APInt &RHS) const
Multiplication operator.
void flipBit(unsigned bitPosition)
Toggles a given bit to its opposite value.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
int64_t getSExtValue() const
Get sign extended value.
const unsigned int integerPartWidth
APInt & operator=(const APInt &RHS)
Copy assignment operator.
APInt sext(unsigned width) const
Sign extend to a new width.
APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
APInt sdiv_ov(const APInt &RHS, bool &Overflow) const
static unsigned int tcFullMultiply(integerPart *, const integerPart *, const integerPart *, unsigned, unsigned)
DST = LHS * RHS, where DST has width the sum of the widths of the operands.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
unsigned getBitWidth() const
Return the number of bits in the APInt.
static bool sub_1(uint64_t x[], unsigned len, uint64_t y)
This function subtracts a single "digit" (64-bit word), y, from the multi-digit integer array...
The returned value is numeric_limits<T>::max()
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
static integerPart tcAdd(integerPart *, const integerPart *, integerPart carry, unsigned)
DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
static void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
static int tcCompare(const integerPart *, const integerPart *, unsigned int)
Comparison (unsigned) of two bignums.
APInt & operator^=(const APInt &RHS)
Bitwise XOR assignment operator.
T findFirstSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the first set bit starting from the least significant bit.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
static void tcOr(integerPart *, const integerPart *, unsigned int)
static void tcSetBit(integerPart *, unsigned int bit)
Set the given bit of a bignum. Zero-based.
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
double roundToDouble() const
Converts this unsigned APInt to a double value.
unsigned countPopulation(T Value)
Count the number of set bits in a value.
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
bool slt(const APInt &RHS) const
Signed less than comparison.
static void tcAnd(integerPart *, const integerPart *, unsigned int)
The obvious AND, OR and XOR and complement operations.
APInt & operator++()
Prefix increment operator.
unsigned logBase2() const
APInt sqrt() const
Compute the square root.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Class for arbitrary precision integers.
StringRef str() const
Explicit conversion to StringRef.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
An opaque object representing a hash code.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
static unsigned int tcMSB(const integerPart *parts, unsigned int n)
static unsigned getDigit(char cdigit, uint8_t radix)
A utility function that converts a character to a digit.
APInt & operator-=(const APInt &RHS)
Subtraction assignment operator.
bool isAllOnesValue() const
Determine if all bits are set.
unsigned countLeadingOnes() const
Count the number of leading one bits.
Magic data for optimising signed division by a constant.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
bool isMinSignedValue() const
Determine if this is the smallest signed value.
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
APInt udiv(const APInt &RHS) const
Unsigned division operation.
APInt & operator|=(const APInt &RHS)
Bitwise OR assignment operator.
uint64_t ByteSwap_64(uint64_t Value)
ByteSwap_64 - This function returns a byte-swapped representation of the 64-bit argument, Value.
void clearBit(unsigned bitPosition)
Set a given bit to 0.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
static int tcMultiply(integerPart *, const integerPart *, const integerPart *, unsigned)
DST = LHS * RHS, where DST has the same width as the operands and is filled with the least significan...
static void lshrNear(uint64_t *Dst, uint64_t *Src, unsigned Words, unsigned Shift)
Perform a logical right-shift from Src to Dst, which must be equal or non-overlapping, of Words words, by Shift, which must be less than 64.
APInt & operator&=(const APInt &RHS)
Bitwise AND assignment operator.
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
static integerPart tcSubtract(integerPart *, const integerPart *, integerPart carry, unsigned)
DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
APInt shl(const APInt &LHS, unsigned shiftAmt)
Left-shift function.
void Profile(FoldingSetNodeID &id) const
Used to insert APInt objects, or objects that contain APInt objects, into FoldingSets.
static void tcComplement(integerPart *, unsigned int)
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.
This class implements an extremely fast bulk output stream that can only output to a stream...
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
StringRef - Represent a constant reference to a string, i.e.
APInt zext(unsigned width) const
Zero extend to a new width.
APInt abs() const
Get the absolute value;.
APInt RoundDoubleToAPInt(double Double, unsigned width)
Converts the given double value into a APInt.
static void tcShiftRight(integerPart *, unsigned int parts, unsigned int count)
Shift a bignum right COUNT bits.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
std::size_t countLeadingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the most significant bit to the first zero bit.
uint64_t * pVal
Used to store the >64 bits integer value.
static uint64_t * getClearedMemory(unsigned numWords)
A utility function for allocating memory, checking for allocation failures, and ensuring the contents...
APInt sshl_ov(const APInt &Amt, bool &Overflow) const
unsigned getNumWords() const
Get the number of words.