30 #define DEBUG_TYPE "apint"
35 uint64_t * result =
new uint64_t[numWords];
36 assert(result &&
"APInt memory allocation fails!");
37 memset(result, 0, numWords *
sizeof(uint64_t));
43 inline static uint64_t*
getMemory(
unsigned numWords) {
44 uint64_t * result =
new uint64_t[numWords];
45 assert(result &&
"APInt memory allocation fails!");
50 inline static unsigned getDigit(
char cdigit, uint8_t radix) {
53 if (radix == 16 || radix == 36) {
77 void APInt::initSlowCase(
unsigned numBits, uint64_t val,
bool isSigned) {
80 if (isSigned && int64_t(val) < 0)
85 void APInt::initSlowCase(
const APInt& that) {
91 assert(BitWidth &&
"Bitwidth too small");
92 assert(bigVal.
data() &&
"Null pointer detected!");
101 memcpy(
pVal, bigVal.
data(), words * APINT_WORD_SIZE);
108 : BitWidth(numBits), VAL(0) {
109 initFromArray(bigVal);
112 APInt::APInt(
unsigned numBits,
unsigned numWords,
const uint64_t bigVal[])
113 : BitWidth(numBits), VAL(0) {
118 : BitWidth(numbits), VAL(0) {
119 assert(BitWidth &&
"Bitwidth too small");
120 fromString(numbits, Str, radix);
123 APInt& APInt::AssignSlowCase(
const APInt& RHS) {
130 assert(!isSingleWord());
135 if (isSingleWord()) {
137 assert(!RHS.isSingleWord());
143 else if (RHS.isSingleWord()) {
151 BitWidth = RHS.BitWidth;
152 return clearUnusedBits();
162 return clearUnusedBits();
169 if (isSingleWord()) {
175 for (
unsigned i = 0; i < NumWords; ++i)
183 static bool add_1(uint64_t dest[], uint64_t x[],
unsigned len, uint64_t y) {
184 for (
unsigned i = 0; i < len; ++i) {
202 return clearUnusedBits();
211 static bool sub_1(uint64_t x[],
unsigned len, uint64_t y) {
212 for (
unsigned i = 0; i < len; ++i) {
231 return clearUnusedBits();
238 static bool add(uint64_t *dest,
const uint64_t *x,
const uint64_t *y,
241 for (
unsigned i = 0; i< len; ++i) {
242 uint64_t limit =
std::min(x[i],y[i]);
243 dest[i] = x[i] + y[i] + carry;
244 carry = dest[i] < limit || (carry && dest[i] == limit);
253 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
259 return clearUnusedBits();
265 static bool sub(uint64_t *dest,
const uint64_t *x,
const uint64_t *y,
268 for (
unsigned i = 0; i < len; ++i) {
269 uint64_t x_tmp = borrow ? x[i] - 1 : x[i];
270 borrow = y[i] > x_tmp || (borrow && x[i] == 0);
271 dest[i] = x_tmp - y[i];
280 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
285 return clearUnusedBits();
292 static uint64_t
mul_1(uint64_t dest[], uint64_t x[],
unsigned len, uint64_t y) {
294 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
298 for (
unsigned i = 0; i < len; ++i) {
300 uint64_t lx = x[i] & 0xffffffffULL;
301 uint64_t hx = x[i] >> 32;
306 uint8_t hasCarry = 0;
307 dest[i] = carry + lx * ly;
309 hasCarry = (dest[i] < carry) ? 1 : 0;
310 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
313 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
315 carry += (lx * hy) & 0xffffffffULL;
316 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
317 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
318 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
326 static void mul(uint64_t dest[], uint64_t x[],
unsigned xlen, uint64_t y[],
328 dest[xlen] =
mul_1(dest, x, xlen, y[0]);
329 for (
unsigned i = 1; i < ylen; ++i) {
330 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
331 uint64_t carry = 0, lx = 0, hx = 0;
332 for (
unsigned j = 0; j < xlen; ++j) {
333 lx = x[j] & 0xffffffffULL;
339 uint8_t hasCarry = 0;
340 uint64_t resul = carry + lx * ly;
341 hasCarry = (resul < carry) ? 1 : 0;
342 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
343 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
345 carry += (lx * hy) & 0xffffffffULL;
346 resul = (carry << 32) | (resul & 0xffffffffULL);
348 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
349 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
350 ((lx * hy) >> 32) + hx * hy;
352 dest[i+xlen] = carry;
357 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
358 if (isSingleWord()) {
366 unsigned lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
373 unsigned rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1;
381 unsigned destWords = rhsWords + lhsWords;
390 memcpy(
pVal, dest, wordsToCopy * APINT_WORD_SIZE);
399 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
400 if (isSingleWord()) {
405 for (
unsigned i = 0; i < numWords; ++i)
411 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
412 if (isSingleWord()) {
417 for (
unsigned i = 0; i < numWords; ++i)
423 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
424 if (isSingleWord()) {
426 this->clearUnusedBits();
430 for (
unsigned i = 0; i < numWords; ++i)
432 return clearUnusedBits();
435 APInt APInt::AndSlowCase(
const APInt& RHS)
const {
438 for (
unsigned i = 0; i < numWords; ++i)
443 APInt APInt::OrSlowCase(
const APInt& RHS)
const {
446 for (
unsigned i = 0; i < numWords; ++i)
451 APInt APInt::XorSlowCase(
const APInt& RHS)
const {
454 for (
unsigned i = 0; i < numWords; ++i)
459 Result.clearUnusedBits();
464 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
466 return APInt(BitWidth, VAL * RHS.
VAL);
473 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
475 return APInt(BitWidth, VAL + RHS.
VAL);
476 APInt Result(BitWidth, 0);
478 Result.clearUnusedBits();
483 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
485 return APInt(BitWidth, VAL - RHS.
VAL);
486 APInt Result(BitWidth, 0);
488 Result.clearUnusedBits();
492 bool APInt::EqualSlowCase(
const APInt& RHS)
const {
502 if (n1 <= APINT_BITS_PER_WORD)
506 for (
int i = whichWord(n1 - 1); i >= 0; --i)
512 bool APInt::EqualSlowCase(uint64_t Val)
const {
514 if (n <= APINT_BITS_PER_WORD)
515 return pVal[0] == Val;
521 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be same for comparison");
523 return VAL < RHS.
VAL;
538 if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
542 unsigned topWord = whichWord(std::max(n1,n2)-1);
543 for (
int i = topWord; i >= 0; --i) {
553 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be same for comparison");
554 if (isSingleWord()) {
555 int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
556 int64_t rhsSext = (int64_t(RHS.
VAL) << (64-BitWidth)) >> (64-BitWidth);
557 return lhsSext < rhsSext;
590 VAL |= maskBit(bitPosition);
592 pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
599 VAL &= ~maskBit(bitPosition);
601 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
610 assert(bitPosition < BitWidth &&
"Out of the bit-width range!");
611 if ((*
this)[bitPosition])
clearBit(bitPosition);
616 assert(!str.
empty() &&
"Invalid string length");
617 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
619 "Radix should be 2, 8, 10, 16, or 36!");
621 size_t slen = str.
size();
626 if (*p ==
'-' || *p ==
'+') {
629 assert(slen &&
"String is only a sign, needs a value.");
652 = radix == 10? (slen == 1 ? 4 : slen * 64/18)
653 : (slen == 1 ? 7 : slen * 16/3);
661 if (log == (
unsigned)-1) {
662 return isNegative + 1;
664 return isNegative + log + 1;
669 if (Arg.isSingleWord())
677 "SplatSizeInBits must divide width!");
680 return *
this ==
rotl(SplatSizeInBits);
694 unsigned APInt::countLeadingZerosSlowCase()
const {
697 unsigned BitsInMSW = BitWidth % APINT_BITS_PER_WORD;
699 if (BitsInMSW) MSWMask = (
integerPart(1) << BitsInMSW) - 1;
702 BitsInMSW = APINT_BITS_PER_WORD;
710 unsigned Count = BitsInMSW;
711 for (--i; i > 0u; --i) {
713 Count += APINT_BITS_PER_WORD;
726 unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD;
729 highWordBits = APINT_BITS_PER_WORD;
732 shift = APINT_BITS_PER_WORD - highWordBits;
736 if (Count == highWordBits) {
737 for (i--; i >= 0; --i) {
738 if (
pVal[i] == -1ULL)
739 Count += APINT_BITS_PER_WORD;
755 Count += APINT_BITS_PER_WORD;
761 unsigned APInt::countTrailingOnesSlowCase()
const {
765 Count += APINT_BITS_PER_WORD;
771 unsigned APInt::countPopulationSlowCase()
const {
780 static void lshrNear(uint64_t *Dst, uint64_t *Src,
unsigned Words,
783 for (
int I = Words - 1;
I >= 0; --
I) {
784 uint64_t Tmp = Src[
I];
785 Dst[
I] = (Tmp >> Shift) | Carry;
786 Carry = Tmp << (64 - Shift);
791 assert(BitWidth >= 16 && BitWidth % 16 == 0 &&
"Cannot byteswap!");
796 if (BitWidth == 48) {
797 unsigned Tmp1 =
unsigned(VAL >> 16);
799 uint16_t Tmp2 = uint16_t(VAL);
801 return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1);
809 if (Result.BitWidth != BitWidth) {
811 Result.BitWidth - BitWidth);
812 Result.BitWidth = BitWidth;
836 bool isNeg =
T.I >> 63;
839 int64_t exp = ((
T.I >> 52) & 0x7ff) - 1023;
843 return APInt(width, 0u);
846 uint64_t mantissa = (
T.I & (~0ULL >> 12)) | 1ULL << 52;
850 return isNeg ? -
APInt(width, mantissa >> (52 - exp)) :
851 APInt(width, mantissa >> (52 - exp));
855 if (width <= exp - 52)
856 return APInt(width, 0);
859 APInt Tmp(width, mantissa);
860 Tmp = Tmp.
shl((
unsigned)exp - 52);
861 return isNeg ? -Tmp : Tmp;
875 if (isSingleWord() ||
getActiveBits() <= APINT_BITS_PER_WORD) {
877 int64_t
sext = (int64_t(getWord(0)) << (64-BitWidth)) >> (64-BitWidth);
880 return double(getWord(0));
884 bool isNeg = isSigned ? (*this)[BitWidth-1] :
false;
887 APInt Tmp(isNeg ? -(*
this) : (*
this));
899 if (!isSigned || !isNeg)
900 return std::numeric_limits<double>::infinity();
902 return -std::numeric_limits<double>::infinity();
909 unsigned hiWord = whichWord(n-1);
911 mantissa = Tmp.
pVal[0];
915 assert(hiWord > 0 &&
"huh?");
916 uint64_t hibits = Tmp.
pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
917 uint64_t lobits = Tmp.
pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
918 mantissa = hibits | lobits;
922 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
927 T.I = sign | (exp << 52) | mantissa;
933 assert(width < BitWidth &&
"Invalid APInt Truncate request");
934 assert(width &&
"Can't truncate to 0 bits");
936 if (width <= APINT_BITS_PER_WORD)
943 for (i = 0; i != width / APINT_BITS_PER_WORD; i++)
944 Result.
pVal[i] = pVal[i];
947 unsigned bits = (0 - width) % APINT_BITS_PER_WORD;
949 Result.
pVal[i] = pVal[i] << bits >> bits;
956 assert(width > BitWidth &&
"Invalid APInt SignExtend request");
958 if (width <= APINT_BITS_PER_WORD) {
959 uint64_t val = VAL << (APINT_BITS_PER_WORD - BitWidth);
960 val = (int64_t)val >> (width - BitWidth);
961 return APInt(width, val >> (APINT_BITS_PER_WORD - width));
969 for (i = 0; i != BitWidth / APINT_BITS_PER_WORD; i++) {
971 Result.
pVal[i] = word;
975 unsigned bits = (0 - BitWidth) % APINT_BITS_PER_WORD;
977 word = (int64_t)
getRawData()[i] << bits >> bits;
979 word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
982 for (; i != width / APINT_BITS_PER_WORD; i++) {
983 Result.
pVal[i] = word;
984 word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
988 bits = (0 - width) % APINT_BITS_PER_WORD;
990 Result.
pVal[i] = word << bits >> bits;
997 assert(width > BitWidth &&
"Invalid APInt ZeroExtend request");
999 if (width <= APINT_BITS_PER_WORD)
1000 return APInt(width, VAL);
1010 memset(&Result.
pVal[i], 0, (Result.
getNumWords() - i) * APINT_WORD_SIZE);
1016 if (BitWidth < width)
1018 if (BitWidth > width)
1019 return trunc(width);
1024 if (BitWidth < width)
1026 if (BitWidth > width)
1027 return trunc(width);
1032 if (BitWidth < width)
1038 if (BitWidth < width)
1052 assert(shiftAmt <= BitWidth &&
"Invalid shift amount");
1058 if (isSingleWord()) {
1059 if (shiftAmt == BitWidth)
1060 return APInt(BitWidth, 0);
1062 unsigned SignBit = APINT_BITS_PER_WORD - BitWidth;
1063 return APInt(BitWidth,
1064 (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt));
1071 if (shiftAmt == BitWidth) {
1073 return APInt(BitWidth, -1ULL,
true);
1075 return APInt(BitWidth, 0);
1082 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1083 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1085 unsigned bitsInWord = whichBit(BitWidth);
1086 if (bitsInWord == 0)
1087 bitsInWord = APINT_BITS_PER_WORD;
1090 if (wordShift == 0) {
1092 for (
unsigned i = 0; i <= breakWord; ++i)
1093 val[i] = pVal[i+offset];
1097 if (bitsInWord < APINT_BITS_PER_WORD)
1098 val[breakWord] |= ~0ULL << bitsInWord;
1101 for (
unsigned i = 0; i < breakWord; ++i) {
1104 val[i] = (pVal[i+offset] >> wordShift) |
1105 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1110 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1115 if (wordShift > bitsInWord) {
1118 ~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord));
1119 val[breakWord] |= ~0ULL;
1121 val[breakWord] |= (~0ULL << (bitsInWord - wordShift));
1126 uint64_t fillValue = (
isNegative() ? -1ULL : 0);
1127 for (
unsigned i = breakWord+1; i <
getNumWords(); ++i)
1129 APInt Result(val, BitWidth);
1130 Result.clearUnusedBits();
1143 if (isSingleWord()) {
1144 if (shiftAmt >= BitWidth)
1145 return APInt(BitWidth, 0);
1147 return APInt(BitWidth, this->VAL >> shiftAmt);
1153 if (shiftAmt >= BitWidth)
1154 return APInt(BitWidth, 0);
1166 if (shiftAmt < APINT_BITS_PER_WORD) {
1168 APInt Result(val, BitWidth);
1169 Result.clearUnusedBits();
1174 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1175 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1178 if (wordShift == 0) {
1179 for (
unsigned i = 0; i <
getNumWords() - offset; ++i)
1180 val[i] = pVal[i+offset];
1183 APInt Result(val, BitWidth);
1184 Result.clearUnusedBits();
1190 for (
unsigned i = 0; i < breakWord; ++i)
1191 val[i] = (pVal[i+offset] >> wordShift) |
1192 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1194 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1197 for (
unsigned i = breakWord+1; i <
getNumWords(); ++i)
1199 APInt Result(val, BitWidth);
1200 Result.clearUnusedBits();
1211 APInt APInt::shlSlowCase(
unsigned shiftAmt)
const {
1215 if (shiftAmt == BitWidth)
1216 return APInt(BitWidth, 0);
1228 if (shiftAmt < APINT_BITS_PER_WORD) {
1231 val[i] = pVal[i] << shiftAmt | carry;
1232 carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt);
1234 APInt Result(val, BitWidth);
1235 Result.clearUnusedBits();
1240 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1241 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1244 if (wordShift == 0) {
1245 for (
unsigned i = 0; i < offset; i++)
1248 val[i] = pVal[i-offset];
1249 APInt Result(val, BitWidth);
1250 Result.clearUnusedBits();
1256 for (; i > offset; --i)
1257 val[i] = pVal[i-offset] << wordShift |
1258 pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift);
1259 val[offset] = pVal[0] << wordShift;
1260 for (i = 0; i < offset; ++i)
1262 APInt Result(val, BitWidth);
1263 Result.clearUnusedBits();
1272 rotateAmt %= BitWidth;
1275 return shl(rotateAmt) |
lshr(BitWidth - rotateAmt);
1283 rotateAmt %= BitWidth;
1286 return lshr(rotateAmt) |
shl(BitWidth - rotateAmt);
1303 if (magnitude <= 5) {
1304 static const uint8_t results[32] = {
1309 4, 4, 4, 4, 4, 4, 4, 4,
1310 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1313 return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]);
1320 if (magnitude < 52) {
1321 return APInt(BitWidth,
1322 uint64_t(::round(::
sqrt(
double(isSingleWord()?VAL:pVal[0])))));
1330 unsigned nbits = BitWidth, i = 4;
1331 APInt testy(BitWidth, 16);
1332 APInt x_old(BitWidth, 1);
1333 APInt x_new(BitWidth, 0);
1334 APInt two(BitWidth, 2);
1337 for (;; i += 2, testy = testy.
shl(2))
1338 if (i >= nbits || this->
ule(testy)) {
1339 x_old = x_old.
shl(i / 2);
1345 x_new = (this->
udiv(x_old) + x_old).
udiv(two);
1346 if (x_old.
ule(x_new))
1357 APInt square(x_old * x_old);
1358 APInt nextSquare((x_old + 1) * (x_old +1));
1359 if (this->
ult(square))
1361 assert(this->
ule(nextSquare) &&
"Error in APInt::sqrt computation");
1362 APInt midpoint((nextSquare - square).
udiv(two));
1363 APInt offset(*
this - square);
1364 if (offset.
ult(midpoint))
1375 assert(
ult(modulo) &&
"This APInt must be smaller than the modulo");
1385 APInt r[2] = { modulo, *
this };
1387 APInt q(BitWidth, 0);
1390 for (i = 0; r[i^1] != 0; i ^= 1) {
1395 udivrem(r[i], r[i^1], q, r[i]);
1404 return APInt(BitWidth, 0);
1410 return t[i].
isNegative() ? t[i] + modulo : t[i];
1418 const APInt& d = *
this;
1420 APInt ad, anc, delta, q1, r1, q2, r2, t;
1426 anc = t - 1 - t.
urem(ad);
1428 q1 = signedMin.
udiv(anc);
1429 r1 = signedMin - q1*anc;
1430 q2 = signedMin.
udiv(ad);
1431 r2 = signedMin - q2*ad;
1447 }
while (q1.
ult(delta) || (q1 == delta && r1 == 0));
1462 const APInt& d = *
this;
1464 APInt nc, delta, q1, r1, q2, r2;
1471 nc = allOnes - (allOnes - d).
urem(d);
1473 q1 = signedMin.
udiv(nc);
1474 r1 = signedMin - q1*nc;
1475 q2 = signedMax.
udiv(d);
1476 r2 = signedMax - q2*d;
1479 if (r1.
uge(nc - r1)) {
1487 if ((r2 + 1).uge(d - r2)) {
1488 if (q2.
uge(signedMax)) magu.
a = 1;
1493 if (q2.
uge(signedMin)) magu.
a = 1;
1498 }
while (p < d.getBitWidth()*2 &&
1499 (q1.
ult(delta) || (q1 == delta && r1 == 0)));
1501 magu.
s = p - d.getBitWidth();
1509 static void KnuthDiv(
unsigned *u,
unsigned *v,
unsigned *q,
unsigned* r,
1510 unsigned m,
unsigned n) {
1511 assert(u &&
"Must provide dividend");
1512 assert(v &&
"Must provide divisor");
1513 assert(q &&
"Must provide quotient");
1514 assert(u != v && u != q && v != q &&
"Must use different memory");
1515 assert(n>1 &&
"n must be > 1");
1520 DEBUG(
dbgs() <<
"KnuthDiv: m=" << m <<
" n=" << n <<
'\n');
1522 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1524 DEBUG(
for (
int i = n; i >0; i--)
dbgs() <<
" " << v[i-1]);
1535 unsigned v_carry = 0;
1536 unsigned u_carry = 0;
1538 for (
unsigned i = 0; i < m+n; ++i) {
1539 unsigned u_tmp = u[i] >> (32 - shift);
1540 u[i] = (u[i] << shift) | u_carry;
1543 for (
unsigned i = 0; i < n; ++i) {
1544 unsigned v_tmp = v[i] >> (32 - shift);
1545 v[i] = (v[i] << shift) | v_carry;
1552 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1554 DEBUG(
for (
int i = n; i >0; i--)
dbgs() <<
" " << v[i-1]);
1560 DEBUG(
dbgs() <<
"KnuthDiv: quotient digit #" << j <<
'\n');
1569 uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]);
1570 DEBUG(
dbgs() <<
"KnuthDiv: dividend == " << dividend <<
'\n');
1571 uint64_t qp = dividend / v[n-1];
1572 uint64_t rp = dividend % v[n-1];
1573 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1576 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
1579 DEBUG(
dbgs() <<
"KnuthDiv: qp == " << qp <<
", rp == " << rp <<
'\n');
1590 for (
unsigned i = 0; i < n; ++i) {
1591 uint64_t p = uint64_t(qp) * uint64_t(v[i]);
1592 int64_t subres = int64_t(u[j+i]) - borrow - (
unsigned)p;
1594 borrow = (p >> 32) - (subres >> 32);
1595 DEBUG(
dbgs() <<
"KnuthDiv: u[j+i] = " << u[j+i]
1596 <<
", borrow = " << borrow <<
'\n');
1598 bool isNeg = u[j+n] < borrow;
1601 DEBUG(
dbgs() <<
"KnuthDiv: after subtraction:");
1602 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1617 for (
unsigned i = 0; i < n; i++) {
1618 unsigned limit =
std::min(u[j+i],v[i]);
1619 u[j+i] += v[i] + carry;
1620 carry = u[j+i] < limit || (carry && u[j+i] == limit);
1624 DEBUG(
dbgs() <<
"KnuthDiv: after correction:");
1625 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1626 DEBUG(
dbgs() <<
"\nKnuthDiv: digit result = " << q[j] <<
'\n');
1632 DEBUG(
for (
int i = m; i >=0; i--)
dbgs() <<
" " << q[i]);
1644 DEBUG(
dbgs() <<
"KnuthDiv: remainder:");
1645 for (
int i = n-1; i >= 0; i--) {
1646 r[i] = (u[i] >> shift) | carry;
1647 carry = u[i] << (32 - shift);
1651 for (
int i = n-1; i >= 0; i--) {
1661 void APInt::divide(
const APInt LHS,
unsigned lhsWords,
1662 const APInt &RHS,
unsigned rhsWords,
1665 assert(lhsWords >= rhsWords &&
"Fractional result");
1674 uint64_t mask = ~0ull >> (
sizeof(
unsigned)*CHAR_BIT);
1675 unsigned n = rhsWords * 2;
1676 unsigned m = (lhsWords * 2) - n;
1680 unsigned SPACE[128];
1681 unsigned *U =
nullptr;
1682 unsigned *V =
nullptr;
1683 unsigned *Q =
nullptr;
1684 unsigned *R =
nullptr;
1685 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1688 Q = &SPACE[(m+n+1) + n];
1690 R = &SPACE[(m+n+1) + n + (m+n)];
1692 U =
new unsigned[m + n + 1];
1693 V =
new unsigned[n];
1694 Q =
new unsigned[m+n];
1696 R =
new unsigned[n];
1700 memset(U, 0, (m+n+1)*
sizeof(
unsigned));
1701 for (
unsigned i = 0; i < lhsWords; ++i) {
1702 uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]);
1704 U[i * 2 + 1] = (
unsigned)(tmp >> (
sizeof(
unsigned)*CHAR_BIT));
1709 memset(V, 0, (n)*
sizeof(
unsigned));
1710 for (
unsigned i = 0; i < rhsWords; ++i) {
1713 V[i * 2 + 1] = (
unsigned)(tmp >> (
sizeof(
unsigned)*CHAR_BIT));
1717 memset(Q, 0, (m+n) *
sizeof(
unsigned));
1719 memset(R, 0, n *
sizeof(
unsigned));
1725 for (
unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1729 for (
unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1738 assert(n != 0 &&
"Divide by zero?");
1740 unsigned divisor = V[0];
1741 unsigned remainder = 0;
1742 for (
int i = m+n-1; i >= 0; i--) {
1743 uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i];
1744 if (partial_dividend == 0) {
1747 }
else if (partial_dividend < divisor) {
1749 remainder = (
unsigned)partial_dividend;
1750 }
else if (partial_dividend == divisor) {
1754 Q[i] = (
unsigned)(partial_dividend / divisor);
1755 remainder = (
unsigned)(partial_dividend - (Q[i] * divisor));
1769 if (Quotient->BitWidth != LHS.BitWidth) {
1770 if (Quotient->isSingleWord())
1773 delete [] Quotient->pVal;
1774 Quotient->BitWidth = LHS.BitWidth;
1775 if (!Quotient->isSingleWord())
1778 Quotient->clearAllBits();
1784 if (lhsWords == 1) {
1786 uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2));
1787 if (Quotient->isSingleWord())
1788 Quotient->VAL = tmp;
1790 Quotient->pVal[0] = tmp;
1792 assert(!Quotient->isSingleWord() &&
"Quotient APInt not large enough");
1793 for (
unsigned i = 0; i < lhsWords; ++i)
1795 uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1802 if (Remainder->BitWidth != RHS.BitWidth) {
1803 if (Remainder->isSingleWord())
1806 delete [] Remainder->pVal;
1807 Remainder->BitWidth = RHS.BitWidth;
1808 if (!Remainder->isSingleWord())
1811 Remainder->clearAllBits();
1815 if (rhsWords == 1) {
1817 uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2));
1818 if (Remainder->isSingleWord())
1819 Remainder->VAL = tmp;
1821 Remainder->pVal[0] = tmp;
1823 assert(!Remainder->isSingleWord() &&
"Remainder APInt not large enough");
1824 for (
unsigned i = 0; i < rhsWords; ++i)
1825 Remainder->pVal[i] =
1826 uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1831 if (U != &SPACE[0]) {
1840 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1843 if (isSingleWord()) {
1844 assert(RHS.
VAL != 0 &&
"Divide by zero?");
1845 return APInt(BitWidth, VAL / RHS.
VAL);
1850 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1851 assert(rhsWords &&
"Divided by zero???");
1853 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1858 return APInt(BitWidth, 0);
1859 else if (lhsWords < rhsWords || this->
ult(RHS)) {
1861 return APInt(BitWidth, 0);
1862 }
else if (*
this == RHS) {
1864 return APInt(BitWidth, 1);
1865 }
else if (lhsWords == 1 && rhsWords == 1) {
1867 return APInt(BitWidth, this->pVal[0] / RHS.
pVal[0]);
1871 APInt Quotient(1,0);
1872 divide(*
this, lhsWords, RHS, rhsWords, &Quotient,
nullptr);
1879 return (-(*
this)).
udiv(-RHS);
1880 return -((-(*this)).
udiv(RHS));
1883 return -(this->
udiv(-RHS));
1884 return this->
udiv(RHS);
1888 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1889 if (isSingleWord()) {
1890 assert(RHS.
VAL != 0 &&
"Remainder by zero?");
1891 return APInt(BitWidth, VAL % RHS.
VAL);
1896 unsigned lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1);
1900 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1901 assert(rhsWords &&
"Performing remainder operation by zero ???");
1904 if (lhsWords == 0) {
1906 return APInt(BitWidth, 0);
1907 }
else if (lhsWords < rhsWords || this->
ult(RHS)) {
1910 }
else if (*
this == RHS) {
1912 return APInt(BitWidth, 0);
1913 }
else if (lhsWords == 1) {
1915 return APInt(BitWidth, pVal[0] % RHS.
pVal[0]);
1919 APInt Remainder(1,0);
1920 divide(*
this, lhsWords, RHS, rhsWords,
nullptr, &Remainder);
1927 return -((-(*
this)).urem(-RHS));
1928 return -((-(*this)).
urem(RHS));
1931 return this->
urem(-RHS);
1932 return this->
urem(RHS);
1937 assert(LHS.BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1940 if (LHS.isSingleWord()) {
1941 assert(RHS.
VAL != 0 &&
"Divide by zero?");
1942 uint64_t QuotVal = LHS.
VAL / RHS.
VAL;
1943 uint64_t RemVal = LHS.
VAL % RHS.
VAL;
1944 Quotient =
APInt(LHS.BitWidth, QuotVal);
1945 Remainder =
APInt(LHS.BitWidth, RemVal);
1951 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1953 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1956 if (lhsWords == 0) {
1962 if (lhsWords < rhsWords || LHS.
ult(RHS)) {
1974 if (lhsWords == 1 && rhsWords == 1) {
1976 uint64_t lhsValue = LHS.isSingleWord() ? LHS.
VAL : LHS.
pVal[0];
1977 uint64_t rhsValue = RHS.isSingleWord() ? RHS.
VAL : RHS.
pVal[0];
1984 divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder);
1994 Quotient = -Quotient;
1996 Remainder = -Remainder;
1999 Quotient = -Quotient;
2006 APInt Res = *
this+RHS;
2013 APInt Res = *
this+RHS;
2014 Overflow = Res.
ult(RHS);
2019 APInt Res = *
this - RHS;
2026 APInt Res = *
this-RHS;
2027 Overflow = Res.
ugt(*
this);
2038 APInt Res = *
this * RHS;
2040 if (*
this != 0 && RHS != 0)
2041 Overflow = Res.
sdiv(RHS) != *
this || Res.
sdiv(*
this) != RHS;
2048 APInt Res = *
this * RHS;
2050 if (*
this != 0 && RHS != 0)
2051 Overflow = Res.
udiv(RHS) != *
this || Res.
udiv(*
this) != RHS;
2060 return APInt(BitWidth, 0);
2067 return *
this << ShAmt;
2073 return APInt(BitWidth, 0);
2077 return *
this << ShAmt;
2083 void APInt::fromString(
unsigned numbits,
StringRef str, uint8_t radix) {
2085 assert(!str.
empty() &&
"Invalid string length");
2086 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
2088 "Radix should be 2, 8, 10, 16, or 36!");
2091 size_t slen = str.
size();
2092 bool isNeg = *p ==
'-';
2093 if (*p ==
'-' || *p ==
'+') {
2096 assert(slen &&
"String is only a sign, needs a value.");
2098 assert((slen <= numbits || radix != 2) &&
"Insufficient bit width");
2099 assert(((slen-1)*3 <= numbits || radix != 8) &&
"Insufficient bit width");
2100 assert(((slen-1)*4 <= numbits || radix != 16) &&
"Insufficient bit width");
2101 assert((((slen-1)*64)/22 <= numbits || radix != 10) &&
2102 "Insufficient bit width");
2105 if (!isSingleWord())
2109 unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
2118 unsigned digit =
getDigit(*p, radix);
2119 assert(digit < radix &&
"Invalid character in digit string");
2130 if (apdigit.isSingleWord())
2131 apdigit.VAL = digit;
2133 apdigit.pVal[0] = digit;
2144 bool Signed,
bool formatAsCLiteral)
const {
2145 assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
2147 "Radix should be 2, 8, 10, 16, or 36!");
2150 if (formatAsCLiteral) {
2180 static const char Digits[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2182 if (isSingleWord()) {
2184 char *BufPtr = Buffer+65;
2205 *--BufPtr = Digits[N % Radix];
2208 Str.
append(BufPtr, Buffer+65);
2229 unsigned StartDig = Str.
size();
2234 if (Radix == 2 || Radix == 8 || Radix == 16) {
2236 unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
2237 unsigned MaskAmt = Radix - 1;
2242 Tmp = Tmp.
lshr(ShiftAmt);
2245 APInt divisor(Radix == 10? 4 : 8, Radix);
2247 APInt APdigit(1, 0);
2252 assert(Digit < Radix &&
"divide failed");
2259 std::reverse(Str.
begin()+StartDig, Str.
end());
2275 dbgs() <<
"APInt(" << BitWidth <<
"b, "
2276 << U <<
"u " << S <<
"s)";
2281 this->
toString(S, 10, isSigned,
false);
2290 static_assert(
integerPartWidth % 2 == 0,
"Part width must be divisible by 2!");
2298 lowBitMask(
unsigned int bits)
2346 for (i = 1; i < parts; i++)
2356 for (i = 0; i < parts; i++)
2366 for (i = 0; i < parts; i++)
2401 unsigned int i, lsb;
2403 for (i = 0; i < n; i++) {
2404 if (parts[i] != 0) {
2405 lsb = partLSB(parts[i]);
2424 if (parts[n] != 0) {
2425 msb = partMSB(parts[n]);
2440 unsigned int srcBits,
unsigned int srcLSB)
2442 unsigned int firstSrcPart, dstParts, shift, n;
2445 assert(dstParts <= dstCount);
2448 tcAssign (dst, src + firstSrcPart, dstParts);
2459 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
2461 }
else if (n > srcBits) {
2463 dst[dstParts - 1] &= lowBitMask (srcBits % integerPartWidth);
2467 while (dstParts < dstCount)
2468 dst[dstParts++] = 0;
2480 for (i = 0; i < parts; i++) {
2485 dst[i] += rhs[i] + 1;
2505 for (i = 0; i < parts; i++) {
2510 dst[i] -= rhs[i] + 1;
2543 unsigned int srcParts,
unsigned int dstParts,
2549 assert(dst <= src || dst >= src + srcParts);
2550 assert(dstParts <= srcParts + 1);
2553 n = dstParts < srcParts ? dstParts: srcParts;
2555 for (i = 0; i < n; i++) {
2568 if (multiplier == 0 || srcPart == 0) {
2572 low = lowHalf(srcPart) * lowHalf(multiplier);
2573 high = highHalf(srcPart) * highHalf(multiplier);
2575 mid = lowHalf(srcPart) * highHalf(multiplier);
2576 high += highHalf(mid);
2578 if (low + mid < low)
2582 mid = highHalf(srcPart) * lowHalf(multiplier);
2583 high += highHalf(mid);
2585 if (low + mid < low)
2590 if (low + carry < low)
2597 if (low + dst[i] < low)
2608 assert(i + 1 == dstParts);
2620 for (; i < srcParts; i++)
2640 assert(dst != lhs && dst != rhs);
2643 tcSet(dst, 0, parts);
2645 for (i = 0; i < parts; i++)
2659 unsigned int rhsParts)
2662 if (lhsParts > rhsParts) {
2667 assert(dst != lhs && dst != rhs);
2669 tcSet(dst, 0, rhsParts);
2671 for (n = 0; n < lhsParts; n++)
2672 tcMultiplyPart(&dst[n], rhs, lhs[n], 0, rhsParts, rhsParts + 1,
true);
2674 n = lhsParts + rhsParts;
2676 return n - (dst[n - 1] == 0);
2695 unsigned int n, shiftCount;
2698 assert(lhs != remainder && lhs != srhs && remainder != srhs);
2700 shiftCount =
tcMSB(rhs, parts) + 1;
2701 if (shiftCount == 0)
2711 tcSet(lhs, 0, parts);
2718 compare =
tcCompare(remainder, srhs, parts);
2724 if (shiftCount == 0)
2728 if ((mask >>= 1) == 0)
2729 mask = (
integerPart) 1 << (integerPartWidth - 1), n--;
2741 unsigned int jump, shift;
2747 while (parts > jump) {
2754 part = dst[parts - jump];
2757 if (parts >= jump + 1)
2775 unsigned int i, jump, shift;
2783 for (i = 0; i < parts; i++) {
2786 if (i + jump >= parts) {
2789 part = dst[i + jump];
2792 if (i + jump + 1 < parts)
2808 for (i = 0; i < parts; i++)
2818 for (i = 0; i < parts; i++)
2828 for (i = 0; i < parts; i++)
2838 for (i = 0; i < parts; i++)
2849 if (lhs[parts] == rhs[parts])
2852 if (lhs[parts] > rhs[parts])
2867 for (i = 0; i < parts; i++)
2877 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
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...
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;.
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.
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)
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...
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 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 LLVM_ATTRIBUTE_UNUSED_RESULT zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
APInt operator+(const APInt &RHS) const
Addition operator.
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 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.
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.
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.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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 LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const
Logical right-shift function.
static bool tcIsZero(const integerPart *, unsigned int)
Returns true if a bignum is zero, false otherwise.
void AddInteger(signed I)
Number of individual test Apply this number of consecutive mutations to each input exit after the first new interesting input is found the minimized corpus is saved into the first input directory Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
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.
APInt operator-() const
Unary negation operator.
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.
static void tcShiftLeft(integerPart *, unsigned int parts, unsigned int count)
Shift a bignum left COUNT bits.
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)
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...
APInt LLVM_ATTRIBUTE_UNUSED_RESULT shl(unsigned shiftAmt) const
Left-shift function.
APInt umul_ov(const APInt &RHS, bool &Overflow) const
APInt ushl_ov(const APInt &Amt, 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.
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 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.
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)
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 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.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
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.
T findFirstSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the first set bit starting from the least significant bit.
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.
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 LLVM_ATTRIBUTE_UNUSED_RESULT 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.
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
This function returns a pointer to the internal storage of the APInt.
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)
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.
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
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.
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
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 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)
Shift a bignum right COUNT bits.
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...
bool empty() const
empty - Check if the string is empty.
APInt sshl_ov(const APInt &Amt, bool &Overflow) const
unsigned getNumWords() const
Get the number of words.