15 #define DEBUG_TYPE "apint"
34 uint64_t * result =
new uint64_t[numWords];
35 assert(result &&
"APInt memory allocation fails!");
36 memset(result, 0, numWords *
sizeof(uint64_t));
42 inline static uint64_t*
getMemory(
unsigned numWords) {
43 uint64_t * result =
new uint64_t[numWords];
44 assert(result &&
"APInt memory allocation fails!");
49 inline static unsigned getDigit(
char cdigit, uint8_t radix) {
52 if (radix == 16 || radix == 36) {
76 void APInt::initSlowCase(
unsigned numBits, uint64_t val,
bool isSigned) {
79 if (isSigned && int64_t(val) < 0)
84 void APInt::initSlowCase(
const APInt& that) {
90 assert(BitWidth &&
"Bitwidth too small");
91 assert(bigVal.
data() &&
"Null pointer detected!");
107 : BitWidth(numBits), VAL(0) {
108 initFromArray(bigVal);
111 APInt::APInt(
unsigned numBits,
unsigned numWords,
const uint64_t bigVal[])
112 : BitWidth(numBits), VAL(0) {
117 : BitWidth(numbits), VAL(0) {
118 assert(BitWidth &&
"Bitwidth too small");
119 fromString(numbits, Str, radix);
122 APInt& APInt::AssignSlowCase(
const APInt& RHS) {
129 assert(!isSingleWord());
134 if (isSingleWord()) {
136 assert(!RHS.isSingleWord());
142 else if (RHS.isSingleWord()) {
150 BitWidth = RHS.BitWidth;
151 return clearUnusedBits();
161 return clearUnusedBits();
168 if (isSingleWord()) {
174 for (
unsigned i = 0; i < NumWords; ++i)
182 static bool add_1(uint64_t dest[], uint64_t x[],
unsigned len, uint64_t y) {
183 for (
unsigned i = 0; i < len; ++i) {
201 return clearUnusedBits();
210 static bool sub_1(uint64_t x[],
unsigned len, uint64_t y) {
211 for (
unsigned i = 0; i < len; ++i) {
230 return clearUnusedBits();
237 static bool add(uint64_t *dest,
const uint64_t *x,
const uint64_t *y,
240 for (
unsigned i = 0; i< len; ++i) {
241 uint64_t limit = std::min(x[i],y[i]);
242 dest[i] = x[i] + y[i] + carry;
243 carry = dest[i] < limit || (carry && dest[i] == limit);
252 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
258 return clearUnusedBits();
264 static bool sub(uint64_t *dest,
const uint64_t *x,
const uint64_t *y,
267 for (
unsigned i = 0; i < len; ++i) {
268 uint64_t x_tmp = borrow ? x[i] - 1 : x[i];
269 borrow = y[i] > x_tmp || (borrow && x[i] == 0);
270 dest[i] = x_tmp - y[i];
279 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
284 return clearUnusedBits();
291 static uint64_t
mul_1(uint64_t dest[], uint64_t x[],
unsigned len, uint64_t y) {
293 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
297 for (
unsigned i = 0; i < len; ++i) {
299 uint64_t lx = x[i] & 0xffffffffULL;
300 uint64_t hx = x[i] >> 32;
305 uint8_t hasCarry = 0;
306 dest[i] = carry + lx * ly;
308 hasCarry = (dest[i] < carry) ? 1 : 0;
309 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
312 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
314 carry += (lx * hy) & 0xffffffffULL;
315 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
316 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
317 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
325 static void mul(uint64_t dest[], uint64_t x[],
unsigned xlen, uint64_t y[],
327 dest[xlen] =
mul_1(dest, x, xlen, y[0]);
328 for (
unsigned i = 1; i < ylen; ++i) {
329 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
330 uint64_t carry = 0, lx = 0, hx = 0;
331 for (
unsigned j = 0; j < xlen; ++j) {
332 lx = x[j] & 0xffffffffULL;
338 uint8_t hasCarry = 0;
339 uint64_t resul = carry + lx * ly;
340 hasCarry = (resul < carry) ? 1 : 0;
341 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
342 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
344 carry += (lx * hy) & 0xffffffffULL;
345 resul = (carry << 32) | (resul & 0xffffffffULL);
347 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
348 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
349 ((lx * hy) >> 32) + hx * hy;
351 dest[i+xlen] = carry;
356 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
357 if (isSingleWord()) {
365 unsigned lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
372 unsigned rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1;
380 unsigned destWords = rhsWords + lhsWords;
389 memcpy(
pVal, dest, wordsToCopy * APINT_WORD_SIZE);
398 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
399 if (isSingleWord()) {
404 for (
unsigned i = 0; i < numWords; ++i)
410 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
411 if (isSingleWord()) {
416 for (
unsigned i = 0; i < numWords; ++i)
422 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
423 if (isSingleWord()) {
425 this->clearUnusedBits();
429 for (
unsigned i = 0; i < numWords; ++i)
431 return clearUnusedBits();
434 APInt APInt::AndSlowCase(
const APInt& RHS)
const {
437 for (
unsigned i = 0; i < numWords; ++i)
442 APInt APInt::OrSlowCase(
const APInt& RHS)
const {
445 for (
unsigned i = 0; i < numWords; ++i)
450 APInt APInt::XorSlowCase(
const APInt& RHS)
const {
453 for (
unsigned i = 0; i < numWords; ++i)
461 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
463 return APInt(BitWidth, VAL * RHS.
VAL);
470 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
472 return APInt(BitWidth, VAL + RHS.
VAL);
473 APInt Result(BitWidth, 0);
475 return Result.clearUnusedBits();
479 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
481 return APInt(BitWidth, VAL - RHS.
VAL);
482 APInt Result(BitWidth, 0);
484 return Result.clearUnusedBits();
487 bool APInt::EqualSlowCase(
const APInt& RHS)
const {
497 if (n1 <= APINT_BITS_PER_WORD)
501 for (
int i = whichWord(n1 - 1); i >= 0; --i)
507 bool APInt::EqualSlowCase(uint64_t Val)
const {
509 if (n <= APINT_BITS_PER_WORD)
510 return pVal[0] == Val;
516 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be same for comparison");
518 return VAL < RHS.
VAL;
533 if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
537 unsigned topWord = whichWord(std::max(n1,n2)-1);
538 for (
int i = topWord; i >= 0; --i) {
548 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be same for comparison");
549 if (isSingleWord()) {
550 int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
551 int64_t rhsSext = (int64_t(RHS.
VAL) << (64-BitWidth)) >> (64-BitWidth);
552 return lhsSext < rhsSext;
585 VAL |= maskBit(bitPosition);
587 pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
594 VAL &= ~maskBit(bitPosition);
596 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
605 assert(bitPosition < BitWidth &&
"Out of the bit-width range!");
606 if ((*
this)[bitPosition])
clearBit(bitPosition);
611 assert(!str.
empty() &&
"Invalid string length");
612 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
614 "Radix should be 2, 8, 10, 16, or 36!");
616 size_t slen = str.
size();
621 if (*p ==
'-' || *p ==
'+') {
624 assert(slen &&
"String is only a sign, needs a value.");
647 = radix == 10? (slen == 1 ? 4 : slen * 64/18)
648 : (slen == 1 ? 7 : slen * 16/3);
656 if (log == (
unsigned)-1) {
657 return isNegative + 1;
659 return isNegative + log + 1;
664 if (Arg.isSingleWord())
681 unsigned APInt::countLeadingZerosSlowCase()
const {
684 unsigned BitsInMSW = BitWidth % APINT_BITS_PER_WORD;
686 if (BitsInMSW) MSWMask = (
integerPart(1) << BitsInMSW) - 1;
689 BitsInMSW = APINT_BITS_PER_WORD;
697 unsigned Count = BitsInMSW;
698 for (--i; i > 0u; --i) {
700 Count += APINT_BITS_PER_WORD;
713 unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD;
716 highWordBits = APINT_BITS_PER_WORD;
719 shift = APINT_BITS_PER_WORD - highWordBits;
723 if (Count == highWordBits) {
724 for (i--; i >= 0; --i) {
725 if (
pVal[i] == -1ULL)
726 Count += APINT_BITS_PER_WORD;
742 Count += APINT_BITS_PER_WORD;
745 return std::min(Count, BitWidth);
748 unsigned APInt::countTrailingOnesSlowCase()
const {
752 Count += APINT_BITS_PER_WORD;
755 return std::min(Count, BitWidth);
758 unsigned APInt::countPopulationSlowCase()
const {
767 static void lshrNear(uint64_t *Dst, uint64_t *Src,
unsigned Words,
770 for (
int I = Words - 1;
I >= 0; --
I) {
771 uint64_t Tmp = Src[
I];
772 Dst[
I] = (Tmp >> Shift) | Carry;
773 Carry = Tmp << (64 - Shift);
778 assert(BitWidth >= 16 && BitWidth % 16 == 0 &&
"Cannot byteswap!");
783 if (BitWidth == 48) {
784 unsigned Tmp1 =
unsigned(VAL >> 16);
786 uint16_t Tmp2 = uint16_t(VAL);
788 return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1);
796 if (Result.BitWidth != BitWidth) {
798 Result.BitWidth - BitWidth);
799 Result.BitWidth = BitWidth;
823 bool isNeg =
T.I >> 63;
826 int64_t
exp = ((
T.I >> 52) & 0x7ff) - 1023;
830 return APInt(width, 0u);
833 uint64_t mantissa = (
T.I & (~0ULL >> 12)) | 1ULL << 52;
837 return isNeg ? -
APInt(width, mantissa >> (52 - exp)) :
838 APInt(width, mantissa >> (52 - exp));
842 if (width <= exp - 52)
843 return APInt(width, 0);
846 APInt Tmp(width, mantissa);
847 Tmp = Tmp.
shl((
unsigned)exp - 52);
848 return isNeg ? -Tmp : Tmp;
862 if (isSingleWord() ||
getActiveBits() <= APINT_BITS_PER_WORD) {
864 int64_t
sext = (int64_t(getWord(0)) << (64-BitWidth)) >> (64-BitWidth);
867 return double(getWord(0));
871 bool isNeg = isSigned ? (*this)[BitWidth-1] :
false;
874 APInt Tmp(isNeg ? -(*
this) : (*
this));
886 if (!isSigned || !isNeg)
887 return std::numeric_limits<double>::infinity();
889 return -std::numeric_limits<double>::infinity();
896 unsigned hiWord = whichWord(n-1);
898 mantissa = Tmp.
pVal[0];
902 assert(hiWord > 0 &&
"huh?");
903 uint64_t hibits = Tmp.
pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
904 uint64_t lobits = Tmp.
pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
905 mantissa = hibits | lobits;
909 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
914 T.I = sign | (exp << 52) | mantissa;
920 assert(width < BitWidth &&
"Invalid APInt Truncate request");
921 assert(width &&
"Can't truncate to 0 bits");
923 if (width <= APINT_BITS_PER_WORD)
930 for (i = 0; i != width / APINT_BITS_PER_WORD; i++)
931 Result.
pVal[i] = pVal[i];
934 unsigned bits = (0 - width) % APINT_BITS_PER_WORD;
936 Result.
pVal[i] = pVal[i] << bits >> bits;
943 assert(width > BitWidth &&
"Invalid APInt SignExtend request");
945 if (width <= APINT_BITS_PER_WORD) {
946 uint64_t val = VAL << (APINT_BITS_PER_WORD - BitWidth);
947 val = (int64_t)val >> (width - BitWidth);
948 return APInt(width, val >> (APINT_BITS_PER_WORD - width));
956 for (i = 0; i != BitWidth / APINT_BITS_PER_WORD; i++) {
958 Result.
pVal[i] = word;
962 unsigned bits = (0 - BitWidth) % APINT_BITS_PER_WORD;
964 word = (int64_t)
getRawData()[i] << bits >> bits;
966 word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
969 for (; i != width / APINT_BITS_PER_WORD; i++) {
970 Result.
pVal[i] = word;
971 word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
975 bits = (0 - width) % APINT_BITS_PER_WORD;
977 Result.
pVal[i] = word << bits >> bits;
984 assert(width > BitWidth &&
"Invalid APInt ZeroExtend request");
986 if (width <= APINT_BITS_PER_WORD)
987 return APInt(width, VAL);
1003 if (BitWidth < width)
1005 if (BitWidth > width)
1006 return trunc(width);
1011 if (BitWidth < width)
1013 if (BitWidth > width)
1014 return trunc(width);
1019 if (BitWidth < width)
1025 if (BitWidth < width)
1039 assert(shiftAmt <= BitWidth &&
"Invalid shift amount");
1045 if (isSingleWord()) {
1046 if (shiftAmt == BitWidth)
1047 return APInt(BitWidth, 0);
1049 unsigned SignBit = APINT_BITS_PER_WORD - BitWidth;
1050 return APInt(BitWidth,
1051 (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt));
1058 if (shiftAmt == BitWidth) {
1060 return APInt(BitWidth, -1ULL,
true);
1062 return APInt(BitWidth, 0);
1069 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1070 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1072 unsigned bitsInWord = whichBit(BitWidth);
1073 if (bitsInWord == 0)
1074 bitsInWord = APINT_BITS_PER_WORD;
1077 if (wordShift == 0) {
1079 for (
unsigned i = 0; i <= breakWord; ++i)
1080 val[i] = pVal[i+offset];
1084 if (bitsInWord < APINT_BITS_PER_WORD)
1085 val[breakWord] |= ~0ULL << bitsInWord;
1088 for (
unsigned i = 0; i < breakWord; ++i) {
1091 val[i] = (pVal[i+offset] >> wordShift) |
1092 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1097 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1102 if (wordShift > bitsInWord) {
1105 ~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord));
1106 val[breakWord] |= ~0ULL;
1108 val[breakWord] |= (~0ULL << (bitsInWord - wordShift));
1113 uint64_t fillValue = (
isNegative() ? -1ULL : 0);
1114 for (
unsigned i = breakWord+1; i <
getNumWords(); ++i)
1116 return APInt(val, BitWidth).clearUnusedBits();
1128 if (isSingleWord()) {
1129 if (shiftAmt >= BitWidth)
1130 return APInt(BitWidth, 0);
1132 return APInt(BitWidth, this->VAL >> shiftAmt);
1138 if (shiftAmt >= BitWidth)
1139 return APInt(BitWidth, 0);
1151 if (shiftAmt < APINT_BITS_PER_WORD) {
1153 return APInt(val, BitWidth).clearUnusedBits();
1157 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1158 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1161 if (wordShift == 0) {
1162 for (
unsigned i = 0; i <
getNumWords() - offset; ++i)
1163 val[i] = pVal[i+offset];
1166 return APInt(val,BitWidth).clearUnusedBits();
1171 for (
unsigned i = 0; i < breakWord; ++i)
1172 val[i] = (pVal[i+offset] >> wordShift) |
1173 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1175 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1178 for (
unsigned i = breakWord+1; i <
getNumWords(); ++i)
1180 return APInt(val, BitWidth).clearUnusedBits();
1190 APInt APInt::shlSlowCase(
unsigned shiftAmt)
const {
1194 if (shiftAmt == BitWidth)
1195 return APInt(BitWidth, 0);
1207 if (shiftAmt < APINT_BITS_PER_WORD) {
1210 val[i] = pVal[i] << shiftAmt | carry;
1211 carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt);
1213 return APInt(val, BitWidth).clearUnusedBits();
1217 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1218 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1221 if (wordShift == 0) {
1222 for (
unsigned i = 0; i < offset; i++)
1225 val[i] = pVal[i-offset];
1226 return APInt(val,BitWidth).clearUnusedBits();
1231 for (; i > offset; --i)
1232 val[i] = pVal[i-offset] << wordShift |
1233 pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift);
1234 val[offset] = pVal[0] << wordShift;
1235 for (i = 0; i < offset; ++i)
1237 return APInt(val, BitWidth).clearUnusedBits();
1245 rotateAmt %= BitWidth;
1248 return shl(rotateAmt) |
lshr(BitWidth - rotateAmt);
1256 rotateAmt %= BitWidth;
1259 return lshr(rotateAmt) |
shl(BitWidth - rotateAmt);
1276 if (magnitude <= 5) {
1277 static const uint8_t results[32] = {
1282 4, 4, 4, 4, 4, 4, 4, 4,
1283 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1286 return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]);
1293 if (magnitude < 52) {
1295 return APInt(BitWidth,
1296 uint64_t(::
round(::
sqrt(
double(isSingleWord()?VAL:pVal[0])))));
1298 return APInt(BitWidth,
1299 uint64_t(::
sqrt(
double(isSingleWord()?VAL:pVal[0])) + 0.5));
1308 unsigned nbits = BitWidth, i = 4;
1309 APInt testy(BitWidth, 16);
1310 APInt x_old(BitWidth, 1);
1311 APInt x_new(BitWidth, 0);
1312 APInt two(BitWidth, 2);
1315 for (;; i += 2, testy = testy.
shl(2))
1316 if (i >= nbits || this->
ule(testy)) {
1317 x_old = x_old.
shl(i / 2);
1323 x_new = (this->
udiv(x_old) + x_old).
udiv(two);
1324 if (x_old.
ule(x_new))
1335 APInt square(x_old * x_old);
1336 APInt nextSquare((x_old + 1) * (x_old +1));
1337 if (this->
ult(square))
1339 assert(this->
ule(nextSquare) &&
"Error in APInt::sqrt computation");
1340 APInt midpoint((nextSquare - square).
udiv(two));
1341 APInt offset(*
this - square);
1342 if (offset.
ult(midpoint))
1353 assert(
ult(modulo) &&
"This APInt must be smaller than the modulo");
1363 APInt r[2] = { modulo, *
this };
1365 APInt q(BitWidth, 0);
1368 for (i = 0; r[i^1] != 0; i ^= 1) {
1373 udivrem(r[i], r[i^1], q, r[i]);
1382 return APInt(BitWidth, 0);
1388 return t[i].
isNegative() ? t[i] + modulo : t[i];
1396 const APInt& d = *
this;
1398 APInt ad, anc, delta, q1, r1, q2, r2, t;
1404 anc = t - 1 - t.
urem(ad);
1406 q1 = signedMin.
udiv(anc);
1407 r1 = signedMin - q1*anc;
1408 q2 = signedMin.
udiv(ad);
1409 r2 = signedMin - q2*ad;
1425 }
while (q1.
ult(delta) || (q1 == delta && r1 == 0));
1440 const APInt& d = *
this;
1442 APInt nc, delta, q1, r1, q2, r2;
1449 nc = allOnes - (allOnes - d).
urem(d);
1451 q1 = signedMin.
udiv(nc);
1452 r1 = signedMin - q1*nc;
1453 q2 = signedMax.
udiv(d);
1454 r2 = signedMax - q2*d;
1457 if (r1.
uge(nc - r1)) {
1465 if ((r2 + 1).uge(d - r2)) {
1466 if (q2.
uge(signedMax)) magu.
a = 1;
1471 if (q2.
uge(signedMin)) magu.
a = 1;
1476 }
while (p < d.getBitWidth()*2 &&
1477 (q1.
ult(delta) || (q1 == delta && r1 == 0)));
1479 magu.
s = p - d.getBitWidth();
1487 static void KnuthDiv(
unsigned *u,
unsigned *v,
unsigned *q,
unsigned* r,
1488 unsigned m,
unsigned n) {
1489 assert(u &&
"Must provide dividend");
1490 assert(v &&
"Must provide divisor");
1491 assert(q &&
"Must provide quotient");
1492 assert(u != v && u != q && v != q &&
"Must us different memory");
1493 assert(n>1 &&
"n must be > 1");
1497 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]);
1516 unsigned v_carry = 0;
1517 unsigned u_carry = 0;
1519 for (
unsigned i = 0; i < m+n; ++i) {
1520 unsigned u_tmp = u[i] >> (32 - shift);
1521 u[i] = (u[i] << shift) | u_carry;
1524 for (
unsigned i = 0; i < n; ++i) {
1525 unsigned v_tmp = v[i] >> (32 - shift);
1526 v[i] = (v[i] << shift) | v_carry;
1533 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1535 DEBUG(
for (
int i = n; i >0; i--)
dbgs() <<
" " << v[i-1]);
1542 DEBUG(
dbgs() <<
"KnuthDiv: quotient digit #" << j <<
'\n');
1551 uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]);
1552 DEBUG(
dbgs() <<
"KnuthDiv: dividend == " << dividend <<
'\n');
1553 uint64_t qp = dividend / v[n-1];
1554 uint64_t rp = dividend % v[n-1];
1555 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1558 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
1561 DEBUG(
dbgs() <<
"KnuthDiv: qp == " << qp <<
", rp == " << rp <<
'\n');
1568 for (
unsigned i = 0; i < n; ++i) {
1569 uint64_t u_tmp = uint64_t(u[j+i]) | (uint64_t(u[j+i+1]) << 32);
1570 uint64_t subtrahend = uint64_t(qp) * uint64_t(v[i]);
1571 bool borrow = subtrahend > u_tmp;
1572 DEBUG(
dbgs() <<
"KnuthDiv: u_tmp == " << u_tmp
1573 <<
", subtrahend == " << subtrahend
1574 <<
", borrow = " << borrow <<
'\n');
1576 uint64_t result = u_tmp - subtrahend;
1578 u[k++] = (
unsigned)(result & (b-1));
1580 while (borrow && k <= m+n) {
1586 DEBUG(
dbgs() <<
"KnuthDiv: u[j+i] == " << u[j+i] <<
", u[j+i+1] == " <<
1589 DEBUG(
dbgs() <<
"KnuthDiv: after subtraction:");
1590 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1599 for (
unsigned i = 0; i <= m+n; ++i) {
1600 u[i] = ~u[i] + carry;
1601 carry = carry && u[i] == 0;
1604 DEBUG(
dbgs() <<
"KnuthDiv: after complement:");
1605 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1620 for (
unsigned i = 0; i < n; i++) {
1621 unsigned limit = std::min(u[j+i],v[i]);
1622 u[j+i] += v[i] + carry;
1623 carry = u[j+i] < limit || (carry && u[j+i] == limit);
1627 DEBUG(
dbgs() <<
"KnuthDiv: after correction:");
1628 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1629 DEBUG(
dbgs() <<
"\nKnuthDiv: digit result = " << q[j] <<
'\n');
1635 DEBUG(
for (
int i = m; i >=0; i--)
dbgs() <<
" " << q[i]);
1647 DEBUG(
dbgs() <<
"KnuthDiv: remainder:");
1648 for (
int i = n-1; i >= 0; i--) {
1649 r[i] = (u[i] >> shift) | carry;
1650 carry = u[i] << (32 - shift);
1654 for (
int i = n-1; i >= 0; i--) {
1666 void APInt::divide(
const APInt LHS,
unsigned lhsWords,
1667 const APInt &RHS,
unsigned rhsWords,
1670 assert(lhsWords >= rhsWords &&
"Fractional result");
1679 uint64_t mask = ~0ull >> (
sizeof(
unsigned)*CHAR_BIT);
1680 unsigned n = rhsWords * 2;
1681 unsigned m = (lhsWords * 2) - n;
1685 unsigned SPACE[128];
1690 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1693 Q = &SPACE[(m+n+1) + n];
1695 R = &SPACE[(m+n+1) + n + (m+n)];
1697 U =
new unsigned[m + n + 1];
1698 V =
new unsigned[n];
1699 Q =
new unsigned[m+n];
1701 R =
new unsigned[n];
1705 memset(U, 0, (m+n+1)*
sizeof(
unsigned));
1706 for (
unsigned i = 0; i < lhsWords; ++i) {
1707 uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]);
1709 U[i * 2 + 1] = (
unsigned)(tmp >> (
sizeof(
unsigned)*CHAR_BIT));
1714 memset(V, 0, (n)*
sizeof(
unsigned));
1715 for (
unsigned i = 0; i < rhsWords; ++i) {
1718 V[i * 2 + 1] = (
unsigned)(tmp >> (
sizeof(
unsigned)*CHAR_BIT));
1722 memset(Q, 0, (m+n) *
sizeof(
unsigned));
1724 memset(R, 0, n *
sizeof(
unsigned));
1730 for (
unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1734 for (
unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1743 assert(n != 0 &&
"Divide by zero?");
1745 unsigned divisor = V[0];
1746 unsigned remainder = 0;
1747 for (
int i = m+n-1; i >= 0; i--) {
1748 uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i];
1749 if (partial_dividend == 0) {
1752 }
else if (partial_dividend < divisor) {
1754 remainder = (
unsigned)partial_dividend;
1755 }
else if (partial_dividend == divisor) {
1759 Q[i] = (
unsigned)(partial_dividend / divisor);
1760 remainder = (
unsigned)(partial_dividend - (Q[i] * divisor));
1774 if (Quotient->BitWidth != LHS.BitWidth) {
1775 if (Quotient->isSingleWord())
1778 delete [] Quotient->pVal;
1779 Quotient->BitWidth = LHS.BitWidth;
1780 if (!Quotient->isSingleWord())
1783 Quotient->clearAllBits();
1787 if (lhsWords == 1) {
1789 uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2));
1790 if (Quotient->isSingleWord())
1791 Quotient->VAL = tmp;
1793 Quotient->pVal[0] = tmp;
1795 assert(!Quotient->isSingleWord() &&
"Quotient APInt not large enough");
1796 for (
unsigned i = 0; i < lhsWords; ++i)
1798 uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1805 if (Remainder->BitWidth != RHS.BitWidth) {
1806 if (Remainder->isSingleWord())
1809 delete [] Remainder->pVal;
1810 Remainder->BitWidth = RHS.BitWidth;
1811 if (!Remainder->isSingleWord())
1814 Remainder->clearAllBits();
1818 if (rhsWords == 1) {
1820 uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2));
1821 if (Remainder->isSingleWord())
1822 Remainder->VAL = tmp;
1824 Remainder->pVal[0] = tmp;
1826 assert(!Remainder->isSingleWord() &&
"Remainder APInt not large enough");
1827 for (
unsigned i = 0; i < rhsWords; ++i)
1828 Remainder->pVal[i] =
1829 uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1834 if (U != &SPACE[0]) {
1843 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1846 if (isSingleWord()) {
1847 assert(RHS.
VAL != 0 &&
"Divide by zero?");
1848 return APInt(BitWidth, VAL / RHS.
VAL);
1853 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1854 assert(rhsWords &&
"Divided by zero???");
1856 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1861 return APInt(BitWidth, 0);
1862 else if (lhsWords < rhsWords || this->
ult(RHS)) {
1864 return APInt(BitWidth, 0);
1865 }
else if (*
this == RHS) {
1867 return APInt(BitWidth, 1);
1868 }
else if (lhsWords == 1 && rhsWords == 1) {
1870 return APInt(BitWidth, this->pVal[0] / RHS.
pVal[0]);
1874 APInt Quotient(1,0);
1875 divide(*
this, lhsWords, RHS, rhsWords, &Quotient, 0);
1882 return (-(*
this)).
udiv(-RHS);
1883 return -((-(*this)).
udiv(RHS));
1886 return -(this->
udiv(-RHS));
1887 return this->
udiv(RHS);
1891 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1892 if (isSingleWord()) {
1893 assert(RHS.
VAL != 0 &&
"Remainder by zero?");
1894 return APInt(BitWidth, VAL % RHS.
VAL);
1899 unsigned lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1);
1903 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1904 assert(rhsWords &&
"Performing remainder operation by zero ???");
1907 if (lhsWords == 0) {
1909 return APInt(BitWidth, 0);
1910 }
else if (lhsWords < rhsWords || this->
ult(RHS)) {
1913 }
else if (*
this == RHS) {
1915 return APInt(BitWidth, 0);
1916 }
else if (lhsWords == 1) {
1918 return APInt(BitWidth, pVal[0] % RHS.
pVal[0]);
1922 APInt Remainder(1,0);
1923 divide(*
this, lhsWords, RHS, rhsWords, 0, &Remainder);
1930 return -((-(*
this)).urem(-RHS));
1931 return -((-(*this)).
urem(RHS));
1934 return this->
urem(-RHS);
1935 return this->
urem(RHS);
1942 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1944 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1947 if (lhsWords == 0) {
1953 if (lhsWords < rhsWords || LHS.
ult(RHS)) {
1965 if (lhsWords == 1 && rhsWords == 1) {
1967 uint64_t lhsValue = LHS.isSingleWord() ? LHS.
VAL : LHS.
pVal[0];
1968 uint64_t rhsValue = RHS.isSingleWord() ? RHS.
VAL : RHS.
pVal[0];
1975 divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder);
1985 Quotient = -Quotient;
1987 Remainder = -Remainder;
1990 Quotient = -Quotient;
1997 APInt Res = *
this+RHS;
2004 APInt Res = *
this+RHS;
2005 Overflow = Res.
ult(RHS);
2010 APInt Res = *
this - RHS;
2017 APInt Res = *
this-RHS;
2018 Overflow = Res.
ugt(*
this);
2029 APInt Res = *
this * RHS;
2031 if (*
this != 0 && RHS != 0)
2032 Overflow = Res.
sdiv(RHS) != *
this || Res.
sdiv(*
this) != RHS;
2039 APInt Res = *
this * RHS;
2041 if (*
this != 0 && RHS != 0)
2042 Overflow = Res.
udiv(RHS) != *
this || Res.
udiv(*
this) != RHS;
2058 return *
this << ShAmt;
2064 void APInt::fromString(
unsigned numbits,
StringRef str, uint8_t radix) {
2066 assert(!str.
empty() &&
"Invalid string length");
2067 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
2069 "Radix should be 2, 8, 10, 16, or 36!");
2072 size_t slen = str.
size();
2073 bool isNeg = *p ==
'-';
2074 if (*p ==
'-' || *p ==
'+') {
2077 assert(slen &&
"String is only a sign, needs a value.");
2079 assert((slen <= numbits || radix != 2) &&
"Insufficient bit width");
2080 assert(((slen-1)*3 <= numbits || radix != 8) &&
"Insufficient bit width");
2081 assert(((slen-1)*4 <= numbits || radix != 16) &&
"Insufficient bit width");
2082 assert((((slen-1)*64)/22 <= numbits || radix != 10) &&
2083 "Insufficient bit width");
2086 if (!isSingleWord())
2090 unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
2099 unsigned digit =
getDigit(*p, radix);
2100 assert(digit < radix &&
"Invalid character in digit string");
2111 if (apdigit.isSingleWord())
2112 apdigit.VAL = digit;
2114 apdigit.pVal[0] = digit;
2125 bool Signed,
bool formatAsCLiteral)
const {
2126 assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
2128 "Radix should be 2, 8, 10, 16, or 36!");
2131 if (formatAsCLiteral) {
2161 static const char Digits[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2163 if (isSingleWord()) {
2165 char *BufPtr = Buffer+65;
2186 *--BufPtr = Digits[N % Radix];
2189 Str.
append(BufPtr, Buffer+65);
2210 unsigned StartDig = Str.
size();
2215 if (Radix == 2 || Radix == 8 || Radix == 16) {
2217 unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
2218 unsigned MaskAmt = Radix - 1;
2223 Tmp = Tmp.
lshr(ShiftAmt);
2226 APInt divisor(Radix == 10? 4 : 8, Radix);
2228 APInt APdigit(1, 0);
2233 assert(Digit < Radix &&
"divide failed");
2240 std::reverse(Str.
begin()+StartDig, Str.
end());
2257 dbgs() <<
"APInt(" << BitWidth <<
"b, "
2258 << U.
str() <<
"u " << S.
str() <<
"s)";
2263 this->
toString(S, 10, isSigned,
false);
2272 #define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
2281 lowBitMask(
unsigned int bits)
2329 for (i = 1; i < parts; i++)
2339 for (i = 0; i < parts; i++)
2349 for (i = 0; i < parts; i++)
2384 unsigned int i, lsb;
2386 for (i = 0; i < n; i++) {
2387 if (parts[i] != 0) {
2388 lsb = partLSB(parts[i]);
2407 if (parts[n] != 0) {
2408 msb = partMSB(parts[n]);
2423 unsigned int srcBits,
unsigned int srcLSB)
2425 unsigned int firstSrcPart, dstParts, shift, n;
2428 assert(dstParts <= dstCount);
2431 tcAssign (dst, src + firstSrcPart, dstParts);
2442 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
2444 }
else if (n > srcBits) {
2446 dst[dstParts - 1] &= lowBitMask (srcBits % integerPartWidth);
2450 while (dstParts < dstCount)
2451 dst[dstParts++] = 0;
2463 for (i = 0; i < parts; i++) {
2468 dst[i] += rhs[i] + 1;
2488 for (i = 0; i < parts; i++) {
2493 dst[i] -= rhs[i] + 1;
2526 unsigned int srcParts,
unsigned int dstParts,
2532 assert(dst <= src || dst >= src + srcParts);
2533 assert(dstParts <= srcParts + 1);
2536 n = dstParts < srcParts ? dstParts: srcParts;
2538 for (i = 0; i < n; i++) {
2551 if (multiplier == 0 || srcPart == 0) {
2555 low = lowHalf(srcPart) * lowHalf(multiplier);
2556 high = highHalf(srcPart) * highHalf(multiplier);
2558 mid = lowHalf(srcPart) * highHalf(multiplier);
2559 high += highHalf(mid);
2561 if (low + mid < low)
2565 mid = highHalf(srcPart) * lowHalf(multiplier);
2566 high += highHalf(mid);
2568 if (low + mid < low)
2573 if (low + carry < low)
2580 if (low + dst[i] < low)
2591 assert(i + 1 == dstParts);
2603 for (; i < srcParts; i++)
2623 assert(dst != lhs && dst != rhs);
2626 tcSet(dst, 0, parts);
2628 for (i = 0; i < parts; i++)
2642 unsigned int rhsParts)
2645 if (lhsParts > rhsParts) {
2650 assert(dst != lhs && dst != rhs);
2652 tcSet(dst, 0, rhsParts);
2654 for (n = 0; n < lhsParts; n++)
2655 tcMultiplyPart(&dst[n], rhs, lhs[n], 0, rhsParts, rhsParts + 1,
true);
2657 n = lhsParts + rhsParts;
2659 return n - (dst[n - 1] == 0);
2678 unsigned int n, shiftCount;
2681 assert(lhs != remainder && lhs != srhs && remainder != srhs);
2683 shiftCount =
tcMSB(rhs, parts) + 1;
2684 if (shiftCount == 0)
2694 tcSet(lhs, 0, parts);
2701 compare =
tcCompare(remainder, srhs, parts);
2707 if (shiftCount == 0)
2711 if ((mask >>= 1) == 0)
2712 mask = (
integerPart) 1 << (integerPartWidth - 1), n--;
2724 unsigned int jump, shift;
2730 while (parts > jump) {
2737 part = dst[parts - jump];
2740 if (parts >= jump + 1)
2758 unsigned int i, jump, shift;
2766 for (i = 0; i < parts; i++) {
2769 if (i + jump >= parts) {
2772 part = dst[i + jump];
2775 if (i + jump + 1 < parts)
2791 for (i = 0; i < parts; i++)
2801 for (i = 0; i < parts; i++)
2811 for (i = 0; i < parts; i++)
2821 for (i = 0; i < parts; i++)
2832 if (lhs[parts] == rhs[parts])
2835 if (lhs[parts] > rhs[parts])
2850 for (i = 0; i < parts; i++)
2860 for (
unsigned int i = 0; i < parts; i++) {
void clearAllBits()
Set every bit to 0.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT ashr(unsigned shiftAmt) const
Arithmetic right-shift function.
APInt multiplicativeInverse(const APInt &modulo) const
void push_back(const T &Elt)
static void tcExtract(integerPart *, unsigned int dstCount, const integerPart *, unsigned int srcBits, unsigned int srcLSB)
mu magicu(unsigned LeadingZeros=0) const
APInt LLVM_ATTRIBUTE_UNUSED_RESULT byteSwap() const
static void tcXor(integerPart *, const integerPart *, unsigned int)
void flipAllBits()
Toggle every bit to its opposite value.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT abs() const
Get the absolute value;.
static int tcDivide(integerPart *lhs, const integerPart *rhs, integerPart *remainder, integerPart *scratch, unsigned int parts)
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.
size_t size() const
size - Get the string size.
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)
static void KnuthDiv(unsigned *u, unsigned *v, unsigned *q, unsigned *r, unsigned m, unsigned n)
static uint64_t * getMemory(unsigned numWords)
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
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, T >::type findFirstSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the first set bit starting from the least significant bit.
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
static integerPart tcIncrement(integerPart *, unsigned int)
Increment a bignum in-place. Return the carry flag.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
APInt operator+(const APInt &RHS) const
Addition operator.
Magic data for optimising unsigned division by a constant.
static unsigned int tcLSB(const integerPart *, unsigned int)
void toStringUnsigned(SmallVectorImpl< char > &Str, unsigned Radix=10) const
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 LLVM_ATTRIBUTE_UNUSED_RESULT rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT urem(const APInt &RHS) const
Unsigned remainder operation.
unsigned CountTrailingOnes_64(uint64_t Value)
#define COMPILE_TIME_ASSERT(cond)
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 uninitialized APInt.
#define llvm_unreachable(msg)
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)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const
Logical right-shift function.
APInt sshl_ov(unsigned Amt, bool &Overflow) const
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)
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, std::size_t >::type 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...
ID
LLVM Calling Convention Representation.
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...
APInt lshr(const APInt &LHS, unsigned shiftAmt)
Logical right-shift function.
static void tcAssign(integerPart *, const integerPart *, unsigned int)
Assign one bignum to another.
APInt operator-() const
Unary negation operator.
static uint64_t mul_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y)
Multiply a multi-digit APInt by a single digit (64-bit) integer.
static void tcShiftLeft(integerPart *, unsigned int parts, unsigned int count)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT 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)
Generalized subtraction of 64-bit integer arrays.
static void mul(uint64_t dest[], uint64_t x[], unsigned xlen, uint64_t y[], unsigned ylen)
Generalized multiplicate of integer arrays.
APInt usub_ov(const APInt &RHS, bool &Overflow) const
uint16_t ByteSwap_16(uint16_t Value)
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, std::size_t >::type 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...
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)
General addition of 64-bit integer arrays.
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)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT shl(unsigned shiftAmt) const
Left-shift function.
APInt umul_ov(const APInt &RHS, bool &Overflow) const
size_t size() const
size - Get the array size.
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.
static void tcSet(integerPart *, integerPart, unsigned int)
void toString(SmallVectorImpl< char > &Str, unsigned Radix, bool Signed, bool formatAsCLiteral=false) const
* if(!EatIfPresent(lltok::kw_thread_local)) return false
APInt LLVM_ATTRIBUTE_UNUSED_RESULT trunc(unsigned width) const
Truncate to new width.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT 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.
hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, const T6 &arg6)
int64_t getSExtValue() const
Get sign extended value.
const unsigned int integerPartWidth
APInt & operator=(const APInt &RHS)
Copy assignment operator.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT 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)
unsigned getBitWidth() const
Return the number of bits in the APInt.
static bool sub_1(uint64_t x[], unsigned len, uint64_t y)
The returned value is numeric_limits<T>::max()
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT 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.
unsigned CountPopulation_64(uint64_t Value)
void append(in_iter in_start, in_iter in_end)
void dump() const
debug method
static int tcCompare(const integerPart *, const integerPart *, unsigned int)
Comparison (unsigned) of two bignums.
APInt & operator^=(const APInt &RHS)
Bitwise XOR assignment operator.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT 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.
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 LLVM_ATTRIBUTE_UNUSED_RESULT sqrt() const
Compute the square root.
unsigned CountLeadingOnes_64(uint64_t Value)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Class for arbitrary precision integers.
StringRef str() const
Explicit conversion to StringRef.
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.
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.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
const uint64_t * getRawData() const
APInt LLVM_ATTRIBUTE_UNUSED_RESULT udiv(const APInt &RHS) const
Unsigned division operation.
APInt & operator|=(const APInt &RHS)
Bitwise OR assignment operator.
uint64_t ByteSwap_64(uint64_t Value)
void clearBit(unsigned bitPosition)
Set a given bit to 0.
static int tcMultiply(integerPart *, const integerPart *, const integerPart *, unsigned)
static void lshrNear(uint64_t *Dst, uint64_t *Src, unsigned Words, unsigned Shift)
APInt & operator&=(const APInt &RHS)
Bitwise AND assignment operator.
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
static integerPart tcSubtract(integerPart *, const integerPart *, integerPart carry, unsigned)
DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
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 LLVM_ATTRIBUTE_UNUSED_RESULT 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
Profile - This method 'profiles' an APInt for use with FoldingSet.
static void tcComplement(integerPart *, unsigned int)
void toStringSigned(SmallVectorImpl< char > &Str, unsigned Radix=10) const
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const
Zero extend to a new width.
APInt RoundDoubleToAPInt(double Double, unsigned width)
Converts the given double value into a APInt.
static void tcShiftRight(integerPart *, unsigned int parts, unsigned int count)
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
uint64_t * pVal
Used to store the >64 bits integer value.
static uint64_t * getClearedMemory(unsigned numWords)
bool empty() const
empty - Check if the string is empty.
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, T >::type findLastSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the last set bit starting from the least significant bit.
unsigned getNumWords() const
Get the number of words.