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)
87 void APInt::initSlowCase(
const APInt& that) {
93 assert(BitWidth &&
"Bitwidth too small");
94 assert(bigVal.
data() &&
"Null pointer detected!");
110 : BitWidth(numBits) {
111 initFromArray(bigVal);
114 APInt::APInt(
unsigned numBits,
unsigned numWords,
const uint64_t bigVal[])
115 : BitWidth(numBits) {
120 : BitWidth(numbits) {
121 assert(BitWidth &&
"Bitwidth too small");
122 fromString(numbits, Str, radix);
125 void APInt::reallocate(
unsigned NewBitWidth) {
128 BitWidth = NewBitWidth;
137 BitWidth = NewBitWidth;
144 void APInt::AssignSlowCase(
const APInt& RHS) {
163 if (isSingleWord()) {
169 for (
unsigned i = 0; i < NumWords; ++i)
179 return clearUnusedBits();
188 return clearUnusedBits();
195 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
200 return clearUnusedBits();
208 return clearUnusedBits();
215 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
220 return clearUnusedBits();
228 return clearUnusedBits();
232 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
234 return APInt(BitWidth, U.VAL * RHS.U.
VAL);
240 Result.clearUnusedBits();
244 void APInt::AndAssignSlowCase(
const APInt& RHS) {
248 void APInt::OrAssignSlowCase(
const APInt& RHS) {
252 void APInt::XorAssignSlowCase(
const APInt& RHS) {
257 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
263 if (isSingleWord()) {
267 tcMultiplyPart(U.pVal, U.pVal, RHS, 0, NumWords, NumWords,
false);
269 return clearUnusedBits();
272 bool APInt::EqualSlowCase(
const APInt& RHS)
const {
276 int APInt::compare(
const APInt& RHS)
const {
277 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be same for comparison");
279 return U.VAL < RHS.U.
VAL ? -1 : U.VAL > RHS.U.
VAL;
284 int APInt::compareSigned(
const APInt& RHS)
const {
285 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be same for comparison");
286 if (isSingleWord()) {
289 return lhsSext < rhsSext ? -1 : lhsSext > rhsSext;
296 if (lhsNeg != rhsNeg)
297 return lhsNeg ? -1 : 1;
304 void APInt::setBitsSlowCase(
unsigned loBit,
unsigned hiBit) {
305 unsigned loWord = whichWord(loBit);
306 unsigned hiWord = whichWord(hiBit);
309 uint64_t loMask =
WORD_MAX << whichBit(loBit);
312 unsigned hiShiftAmt = whichBit(hiBit);
313 if (hiShiftAmt != 0) {
318 if (hiWord == loWord)
321 U.pVal[hiWord] |= hiMask;
324 U.pVal[loWord] |= loMask;
327 for (
unsigned word = loWord + 1; word < hiWord; ++word)
332 void APInt::flipAllBitsSlowCase() {
341 assert(bitPosition < BitWidth &&
"Out of the bit-width range!");
342 if ((*
this)[bitPosition])
clearBit(bitPosition);
348 assert(0 < subBitWidth && (subBitWidth + bitPosition) <= BitWidth &&
349 "Illegal bit insertion");
352 if (subBitWidth == BitWidth) {
358 if (isSingleWord()) {
360 U.VAL &= ~(mask << bitPosition);
361 U.VAL |= (subBits.U.
VAL << bitPosition);
365 unsigned loBit = whichBit(bitPosition);
366 unsigned loWord = whichWord(bitPosition);
367 unsigned hi1Word = whichWord(bitPosition + subBitWidth - 1);
370 if (loWord == hi1Word) {
372 U.pVal[loWord] &= ~(mask << loBit);
373 U.pVal[loWord] |= (subBits.U.
VAL << loBit);
386 if (remainingBits != 0) {
388 U.pVal[hi1Word] &= ~mask;
389 U.pVal[hi1Word] |= subBits.getWord(subBitWidth - 1);
397 for (
unsigned i = 0; i != subBitWidth; ++i) {
406 assert(numBits > 0 &&
"Can't extract zero bits");
407 assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth &&
408 "Illegal bit extraction");
411 return APInt(numBits, U.VAL >> bitPosition);
413 unsigned loBit = whichBit(bitPosition);
414 unsigned loWord = whichWord(bitPosition);
415 unsigned hiWord = whichWord(bitPosition + numBits - 1);
418 if (loWord == hiWord)
419 return APInt(numBits, U.pVal[loWord] >> loBit);
427 APInt Result(numBits, 0);
431 for (
unsigned word = 0; word < NumDstWords; ++word) {
432 uint64_t w0 = U.pVal[loWord + word];
434 (loWord + word + 1) < NumSrcWords ? U.pVal[loWord + word + 1] : 0;
438 return Result.clearUnusedBits();
443 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
445 "Radix should be 2, 8, 10, 16, or 36!");
447 size_t slen = str.
size();
452 if (*p ==
'-' || *p ==
'+') {
455 assert(slen &&
"String is only a sign, needs a value.");
478 = radix == 10? (slen == 1 ? 4 : slen * 64/18)
479 : (slen == 1 ? 7 : slen * 16/3);
487 if (log == (
unsigned)-1) {
488 return isNegative + 1;
490 return isNegative + log + 1;
495 if (Arg.isSingleWord())
503 "SplatSizeInBits must divide width!");
506 return *
this ==
rotl(SplatSizeInBits);
511 return this->
lshr(BitWidth - numBits);
532 unsigned APInt::countLeadingZerosSlowCase()
const {
535 uint64_t V = U.pVal[i];
549 unsigned APInt::countLeadingOnesSlowCase()
const {
560 if (Count == highWordBits) {
561 for (i--; i >= 0; --i) {
573 unsigned APInt::countTrailingZerosSlowCase()
const {
583 unsigned APInt::countTrailingOnesSlowCase()
const {
590 assert(Count <= BitWidth);
594 unsigned APInt::countPopulationSlowCase()
const {
601 bool APInt::intersectsSlowCase(
const APInt &RHS)
const {
602 for (
unsigned i = 0, e =
getNumWords(); i != e; ++i)
603 if ((U.pVal[i] & RHS.U.
pVal[i]) != 0)
609 bool APInt::isSubsetOfSlowCase(
const APInt &RHS)
const {
610 for (
unsigned i = 0, e =
getNumWords(); i != e; ++i)
611 if ((U.pVal[i] & ~RHS.U.
pVal[i]) != 0)
618 assert(BitWidth >= 16 && BitWidth % 16 == 0 &&
"Cannot byteswap!");
623 if (BitWidth == 48) {
624 unsigned Tmp1 =
unsigned(U.VAL >> 16);
626 uint16_t Tmp2 = uint16_t(U.VAL);
628 return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1);
636 if (Result.BitWidth != BitWidth) {
638 Result.BitWidth = BitWidth;
646 return APInt(BitWidth, llvm::reverseBits<uint64_t>(U.VAL));
648 return APInt(BitWidth, llvm::reverseBits<uint32_t>(U.VAL));
650 return APInt(BitWidth, llvm::reverseBits<uint16_t>(U.VAL));
652 return APInt(BitWidth, llvm::reverseBits<uint8_t>(U.VAL));
658 APInt Reversed(BitWidth, 0);
659 unsigned S = BitWidth;
673 if (A == B)
return A;
684 if (Pow2_A > Pow2_B) {
687 }
else if (Pow2_B > Pow2_A) {
722 bool isNeg =
T.I >> 63;
725 int64_t exp = ((
T.I >> 52) & 0x7ff) - 1023;
729 return APInt(width, 0u);
732 uint64_t mantissa = (
T.I & (~0ULL >> 12)) | 1ULL << 52;
736 return isNeg ? -
APInt(width, mantissa >> (52 - exp)) :
737 APInt(width, mantissa >> (52 - exp));
741 if (width <= exp - 52)
742 return APInt(width, 0);
745 APInt Tmp(width, mantissa);
747 return isNeg ? -Tmp : Tmp;
766 return double(getWord(0));
770 bool isNeg = isSigned ? (*this)[BitWidth-1] :
false;
773 APInt Tmp(isNeg ? -(*
this) : (*
this));
785 if (!isSigned || !isNeg)
786 return std::numeric_limits<double>::infinity();
788 return -std::numeric_limits<double>::infinity();
795 unsigned hiWord = whichWord(n-1);
797 mantissa = Tmp.U.
pVal[0];
801 assert(hiWord > 0 &&
"huh?");
804 mantissa = hibits | lobits;
813 T.I = sign | (exp << 52) | mantissa;
819 assert(width < BitWidth &&
"Invalid APInt Truncate request");
820 assert(width &&
"Can't truncate to 0 bits");
830 Result.U.
pVal[i] = U.pVal[i];
842 assert(Width > BitWidth &&
"Invalid APInt SignExtend request");
860 Result.clearUnusedBits();
866 assert(width > BitWidth &&
"Invalid APInt ZeroExtend request");
869 return APInt(width, U.VAL);
884 if (BitWidth < width)
886 if (BitWidth > width)
892 if (BitWidth < width)
894 if (BitWidth > width)
900 if (BitWidth < width)
906 if (BitWidth < width)
919 void APInt::ashrSlowCase(
unsigned ShiftAmt) {
932 if (WordsToMove != 0) {
939 std::memmove(U.pVal, U.pVal + WordShift, WordsToMove *
APINT_WORD_SIZE);
942 for (
unsigned i = 0; i != WordsToMove - 1; ++i)
943 U.pVal[i] = (U.pVal[i + WordShift] >> BitShift) |
947 U.pVal[WordsToMove - 1] = U.pVal[WordShift + WordsToMove - 1] >> BitShift;
949 U.pVal[WordsToMove - 1] =
955 std::memset(U.pVal + WordsToMove, Negative ? -1 : 0,
968 void APInt::lshrSlowCase(
unsigned ShiftAmt) {
980 void APInt::shlSlowCase(
unsigned ShiftAmt) {
988 APInt rot = rotateAmt;
989 if (rotBitWidth < BitWidth) {
992 rot = rotateAmt.
zext(BitWidth);
1003 rotateAmt %= BitWidth;
1006 return shl(rotateAmt) |
lshr(BitWidth - rotateAmt);
1014 rotateAmt %= BitWidth;
1017 return lshr(rotateAmt) |
shl(BitWidth - rotateAmt);
1034 if (magnitude <= 5) {
1035 static const uint8_t results[32] = {
1040 4, 4, 4, 4, 4, 4, 4, 4,
1041 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1044 return APInt(BitWidth, results[ (isSingleWord() ? U.VAL : U.pVal[0]) ]);
1051 if (magnitude < 52) {
1052 return APInt(BitWidth,
1053 uint64_t(::
round(::
sqrt(
double(isSingleWord() ? U.VAL
1062 unsigned nbits = BitWidth, i = 4;
1063 APInt testy(BitWidth, 16);
1064 APInt x_old(BitWidth, 1);
1065 APInt x_new(BitWidth, 0);
1066 APInt two(BitWidth, 2);
1069 for (;; i += 2, testy = testy.
shl(2))
1070 if (i >= nbits || this->
ule(testy)) {
1071 x_old = x_old.
shl(i / 2);
1077 x_new = (this->
udiv(x_old) + x_old).
udiv(two);
1078 if (x_old.
ule(x_new))
1089 APInt square(x_old * x_old);
1090 APInt nextSquare((x_old + 1) * (x_old +1));
1091 if (this->
ult(square))
1093 assert(this->
ule(nextSquare) &&
"Error in APInt::sqrt computation");
1094 APInt midpoint((nextSquare - square).
udiv(two));
1095 APInt offset(*
this - square);
1096 if (offset.
ult(midpoint))
1107 assert(
ult(modulo) &&
"This APInt must be smaller than the modulo");
1117 APInt r[2] = { modulo, *
this };
1119 APInt q(BitWidth, 0);
1122 for (i = 0; r[i^1] != 0; i ^= 1) {
1127 udivrem(r[i], r[i^1], q, r[i]);
1136 return APInt(BitWidth, 0);
1145 return std::move(t[i]);
1153 const APInt& d = *
this;
1161 anc = t - 1 - t.
urem(ad);
1163 q1 = signedMin.
udiv(anc);
1164 r1 = signedMin - q1*anc;
1165 q2 = signedMin.
udiv(ad);
1166 r2 = signedMin - q2*ad;
1182 }
while (q1.
ult(delta) || (q1 == delta && r1 == 0));
1197 const APInt& d = *
this;
1206 nc = allOnes - (allOnes - d).
urem(d);
1208 q1 = signedMin.
udiv(nc);
1209 r1 = signedMin - q1*nc;
1210 q2 = signedMax.
udiv(d);
1211 r2 = signedMax - q2*d;
1214 if (r1.
uge(nc - r1)) {
1222 if ((r2 + 1).uge(d - r2)) {
1223 if (q2.
uge(signedMax)) magu.
a = 1;
1228 if (q2.
uge(signedMin)) magu.
a = 1;
1233 }
while (p < d.getBitWidth()*2 &&
1234 (q1.
ult(delta) || (q1 == delta && r1 == 0)));
1236 magu.
s = p - d.getBitWidth();
1245 unsigned m,
unsigned n) {
1246 assert(u &&
"Must provide dividend");
1247 assert(v &&
"Must provide divisor");
1248 assert(q &&
"Must provide quotient");
1249 assert(u != v && u != q && v != q &&
"Must use different memory");
1250 assert(n>1 &&
"n must be > 1");
1253 const uint64_t b = uint64_t(1) << 32;
1255 DEBUG(
dbgs() <<
"KnuthDiv: m=" << m <<
" n=" << n <<
'\n');
1257 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1259 DEBUG(
for (
int i = n; i >0; i--)
dbgs() <<
" " << v[i-1]);
1273 for (
unsigned i = 0; i < m+n; ++i) {
1274 uint32_t u_tmp = u[i] >> (32 - shift);
1275 u[i] = (u[i] << shift) | u_carry;
1278 for (
unsigned i = 0; i < n; ++i) {
1279 uint32_t v_tmp = v[i] >> (32 - shift);
1280 v[i] = (v[i] << shift) | v_carry;
1287 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1289 DEBUG(
for (
int i = n; i >0; i--)
dbgs() <<
" " << v[i-1]);
1295 DEBUG(
dbgs() <<
"KnuthDiv: quotient digit #" << j <<
'\n');
1304 uint64_t dividend =
Make_64(u[j+n], u[j+n-1]);
1305 DEBUG(
dbgs() <<
"KnuthDiv: dividend == " << dividend <<
'\n');
1306 uint64_t qp = dividend / v[n-1];
1307 uint64_t rp = dividend % v[n-1];
1308 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1311 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
1314 DEBUG(
dbgs() <<
"KnuthDiv: qp == " << qp <<
", rp == " << rp <<
'\n');
1325 for (
unsigned i = 0; i < n; ++i) {
1326 uint64_t p = uint64_t(qp) * uint64_t(v[i]);
1327 int64_t subres = int64_t(u[j+i]) - borrow -
Lo_32(p);
1328 u[j+i] =
Lo_32(subres);
1330 DEBUG(
dbgs() <<
"KnuthDiv: u[j+i] = " << u[j+i]
1331 <<
", borrow = " << borrow <<
'\n');
1333 bool isNeg = u[j+n] < borrow;
1334 u[j+n] -=
Lo_32(borrow);
1336 DEBUG(
dbgs() <<
"KnuthDiv: after subtraction:");
1337 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1352 for (
unsigned i = 0; i < n; i++) {
1354 u[j+i] += v[i] + carry;
1355 carry = u[j+i] < limit || (carry && u[j+i] == limit);
1359 DEBUG(
dbgs() <<
"KnuthDiv: after correction:");
1360 DEBUG(
for (
int i = m+n; i >=0; i--)
dbgs() <<
" " << u[i]);
1361 DEBUG(
dbgs() <<
"\nKnuthDiv: digit result = " << q[j] <<
'\n');
1367 DEBUG(
for (
int i = m; i >=0; i--)
dbgs() <<
" " << q[i]);
1379 DEBUG(
dbgs() <<
"KnuthDiv: remainder:");
1380 for (
int i = n-1; i >= 0; i--) {
1381 r[i] = (u[i] >> shift) | carry;
1382 carry = u[i] << (32 - shift);
1386 for (
int i = n-1; i >= 0; i--) {
1396 void APInt::divide(
const WordType *LHS,
unsigned lhsWords,
const WordType *RHS,
1397 unsigned rhsWords, WordType *Quotient, WordType *Remainder) {
1398 assert(lhsWords >= rhsWords &&
"Fractional result");
1407 unsigned n = rhsWords * 2;
1408 unsigned m = (lhsWords * 2) - n;
1417 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1420 Q = &SPACE[(m+n+1) + n];
1422 R = &SPACE[(m+n+1) + n + (m+n)];
1432 memset(U, 0, (m+n+1)*
sizeof(
uint32_t));
1433 for (
unsigned i = 0; i < lhsWords; ++i) {
1434 uint64_t tmp = LHS[i];
1435 U[i * 2] =
Lo_32(tmp);
1436 U[i * 2 + 1] =
Hi_32(tmp);
1441 memset(V, 0, (n)*
sizeof(
uint32_t));
1442 for (
unsigned i = 0; i < rhsWords; ++i) {
1443 uint64_t tmp = RHS[i];
1444 V[i * 2] =
Lo_32(tmp);
1445 V[i * 2 + 1] =
Hi_32(tmp);
1449 memset(Q, 0, (m+n) *
sizeof(
uint32_t));
1451 memset(R, 0, n *
sizeof(
uint32_t));
1457 for (
unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1461 for (
unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1470 assert(n != 0 &&
"Divide by zero?");
1474 for (
int i = m; i >= 0; i--) {
1475 uint64_t partial_dividend =
Make_64(remainder, U[i]);
1476 if (partial_dividend == 0) {
1479 }
else if (partial_dividend < divisor) {
1481 remainder =
Lo_32(partial_dividend);
1482 }
else if (partial_dividend == divisor) {
1486 Q[i] =
Lo_32(partial_dividend / divisor);
1487 remainder =
Lo_32(partial_dividend - (Q[i] * divisor));
1500 for (
unsigned i = 0; i < lhsWords; ++i)
1501 Quotient[i] =
Make_64(Q[i*2+1], Q[i*2]);
1506 for (
unsigned i = 0; i < rhsWords; ++i)
1507 Remainder[i] =
Make_64(R[i*2+1], R[i*2]);
1511 if (U != &SPACE[0]) {
1520 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1523 if (isSingleWord()) {
1524 assert(RHS.U.
VAL != 0 &&
"Divide by zero?");
1525 return APInt(BitWidth, U.VAL / RHS.U.
VAL);
1532 assert(rhsWords &&
"Divided by zero???");
1537 return APInt(BitWidth, 0);
1541 if (lhsWords < rhsWords || this->
ult(RHS))
1543 return APInt(BitWidth, 0);
1546 return APInt(BitWidth, 1);
1549 return APInt(BitWidth, this->U.pVal[0] / RHS.U.
pVal[0]);
1552 APInt Quotient(BitWidth, 0);
1553 divide(U.pVal, lhsWords, RHS.U.
pVal, rhsWords, Quotient.U.
pVal,
nullptr);
1558 assert(RHS != 0 &&
"Divide by zero?");
1562 return APInt(BitWidth, U.VAL / RHS);
1570 return APInt(BitWidth, 0);
1576 return APInt(BitWidth, 0);
1579 return APInt(BitWidth, 1);
1582 return APInt(BitWidth, this->U.pVal[0] / RHS);
1585 APInt Quotient(BitWidth, 0);
1586 divide(U.pVal, lhsWords, &RHS, 1, Quotient.U.
pVal,
nullptr);
1593 return (-(*
this)).
udiv(-RHS);
1594 return -((-(*this)).
udiv(RHS));
1597 return -(this->
udiv(-RHS));
1598 return this->
udiv(RHS);
1604 return (-(*
this)).
udiv(-RHS);
1605 return -((-(*this)).
udiv(RHS));
1608 return -(this->
udiv(-RHS));
1609 return this->
udiv(RHS);
1613 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1614 if (isSingleWord()) {
1615 assert(RHS.U.
VAL != 0 &&
"Remainder by zero?");
1616 return APInt(BitWidth, U.VAL % RHS.U.
VAL);
1625 assert(rhsWords &&
"Performing remainder operation by zero ???");
1630 return APInt(BitWidth, 0);
1633 return APInt(BitWidth, 0);
1634 if (lhsWords < rhsWords || this->
ult(RHS))
1639 return APInt(BitWidth, 0);
1642 return APInt(BitWidth, U.pVal[0] % RHS.U.
pVal[0]);
1645 APInt Remainder(BitWidth, 0);
1646 divide(U.pVal, lhsWords, RHS.U.
pVal, rhsWords,
nullptr, Remainder.U.
pVal);
1651 assert(RHS != 0 &&
"Remainder by zero?");
1674 return U.pVal[0] % RHS;
1678 divide(U.pVal, lhsWords, &RHS, 1,
nullptr, &Remainder);
1685 return -((-(*
this)).urem(-RHS));
1686 return -((-(*this)).
urem(RHS));
1689 return this->
urem(-RHS);
1690 return this->
urem(RHS);
1696 return -((-(*this)).
urem(-RHS));
1697 return -((-(*this)).
urem(RHS));
1700 return this->
urem(-RHS);
1701 return this->
urem(RHS);
1706 assert(LHS.BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1707 unsigned BitWidth = LHS.BitWidth;
1710 if (LHS.isSingleWord()) {
1711 assert(RHS.U.
VAL != 0 &&
"Divide by zero?");
1712 uint64_t QuotVal = LHS.U.
VAL / RHS.U.
VAL;
1713 uint64_t RemVal = LHS.U.
VAL % RHS.U.
VAL;
1714 Quotient =
APInt(BitWidth, QuotVal);
1715 Remainder =
APInt(BitWidth, RemVal);
1723 assert(rhsWords &&
"Performing divrem operation by zero ???");
1726 if (lhsWords == 0) {
1737 if (lhsWords < rhsWords || LHS.
ult(RHS)) {
1753 Quotient.reallocate(BitWidth);
1754 Remainder.reallocate(BitWidth);
1756 if (lhsWords == 1) {
1758 uint64_t lhsValue = LHS.U.
pVal[0];
1759 uint64_t rhsValue = RHS.U.
pVal[0];
1760 Quotient = lhsValue / rhsValue;
1761 Remainder = lhsValue % rhsValue;
1766 divide(LHS.U.
pVal, lhsWords, RHS.U.
pVal, rhsWords, Quotient.U.
pVal,
1769 std::memset(Quotient.U.
pVal + lhsWords, 0,
1771 std::memset(Remainder.U.
pVal + rhsWords, 0,
1772 (
getNumWords(BitWidth) - rhsWords) * APINT_WORD_SIZE);
1776 uint64_t &Remainder) {
1777 assert(RHS != 0 &&
"Divide by zero?");
1778 unsigned BitWidth = LHS.BitWidth;
1781 if (LHS.isSingleWord()) {
1782 uint64_t QuotVal = LHS.U.
VAL / RHS;
1783 Remainder = LHS.U.
VAL % RHS;
1784 Quotient =
APInt(BitWidth, QuotVal);
1792 if (lhsWords == 0) {
1818 Quotient.reallocate(BitWidth);
1820 if (lhsWords == 1) {
1822 uint64_t lhsValue = LHS.U.
pVal[0];
1823 Quotient = lhsValue / RHS;
1824 Remainder = lhsValue % RHS;
1829 divide(LHS.U.
pVal, lhsWords, &RHS, 1, Quotient.U.
pVal, &Remainder);
1831 std::memset(Quotient.U.
pVal + lhsWords, 0,
1854 APInt &Quotient, int64_t &Remainder) {
1855 uint64_t R = Remainder;
1864 }
else if (RHS < 0) {
1874 APInt Res = *
this+RHS;
1881 APInt Res = *
this+RHS;
1882 Overflow = Res.
ult(RHS);
1887 APInt Res = *
this - RHS;
1894 APInt Res = *
this-RHS;
1895 Overflow = Res.
ugt(*
this);
1906 APInt Res = *
this * RHS;
1908 if (*
this != 0 && RHS != 0)
1909 Overflow = Res.
sdiv(RHS) != *
this || Res.
sdiv(*
this) != RHS;
1916 APInt Res = *
this * RHS;
1918 if (*
this != 0 && RHS != 0)
1919 Overflow = Res.
udiv(RHS) != *
this || Res.
udiv(*
this) != RHS;
1928 return APInt(BitWidth, 0);
1935 return *
this << ShAmt;
1941 return APInt(BitWidth, 0);
1945 return *
this << ShAmt;
1951 void APInt::fromString(
unsigned numbits,
StringRef str, uint8_t radix) {
1954 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
1956 "Radix should be 2, 8, 10, 16, or 36!");
1959 size_t slen = str.
size();
1960 bool isNeg = *p ==
'-';
1961 if (*p ==
'-' || *p ==
'+') {
1964 assert(slen &&
"String is only a sign, needs a value.");
1966 assert((slen <= numbits || radix != 2) &&
"Insufficient bit width");
1967 assert(((slen-1)*3 <= numbits || radix != 8) &&
"Insufficient bit width");
1968 assert(((slen-1)*4 <= numbits || radix != 16) &&
"Insufficient bit width");
1969 assert((((slen-1)*64)/22 <= numbits || radix != 10) &&
1970 "Insufficient bit width");
1979 unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
1983 unsigned digit =
getDigit(*p, radix);
1984 assert(digit < radix &&
"Invalid character in digit string");
2003 bool Signed,
bool formatAsCLiteral)
const {
2004 assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
2006 "Radix should be 2, 8, 10, 16, or 36!");
2009 if (formatAsCLiteral) {
2039 static const char Digits[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2041 if (isSingleWord()) {
2064 *--BufPtr = Digits[N % Radix];
2087 unsigned StartDig = Str.
size();
2092 if (Radix == 2 || Radix == 8 || Radix == 16) {
2094 unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
2095 unsigned MaskAmt = Radix - 1;
2105 udivrem(Tmp, Radix, Tmp, Digit);
2106 assert(Digit < Radix &&
"divide failed");
2123 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2128 dbgs() <<
"APInt(" << BitWidth <<
"b, "
2129 << U <<
"u " << S <<
"s)\n";
2135 this->
toString(S, 10, isSigned,
false);
2145 "Part width must be divisible by 2!");
2185 for (
unsigned i = 1; i < parts; i++)
2191 for (
unsigned i = 0; i < parts; i++)
2197 for (
unsigned i = 0; i < parts; i++)
2206 return (parts[whichWord(bit)] & maskBit(bit)) != 0;
2211 parts[whichWord(bit)] |= maskBit(bit);
2216 parts[whichWord(bit)] &= ~maskBit(bit);
2222 for (
unsigned i = 0; i < n; i++) {
2223 if (parts[i] != 0) {
2224 unsigned lsb =
partLSB(parts[i]);
2239 if (parts[n] != 0) {
2240 unsigned msb =
partMSB(parts[n]);
2255 unsigned srcBits,
unsigned srcLSB) {
2257 assert(dstParts <= dstCount);
2260 tcAssign (dst, src + firstSrcPart, dstParts);
2271 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
2273 }
else if (n > srcBits) {
2275 dst[dstParts - 1] &=
lowBitMask (srcBits % APINT_BITS_PER_WORD);
2279 while (dstParts < dstCount)
2280 dst[dstParts++] = 0;
2288 for (
unsigned i = 0; i < parts; i++) {
2291 dst[i] += rhs[i] + 1;
2308 for (
unsigned i = 0; i < parts; ++i) {
2323 for (
unsigned i = 0; i < parts; i++) {
2326 dst[i] -= rhs[i] + 1;
2346 for (
unsigned i = 0; i < parts; ++i) {
2376 unsigned srcParts,
unsigned dstParts,
2379 assert(dst <= src || dst >= src + srcParts);
2380 assert(dstParts <= srcParts + 1);
2383 unsigned n =
std::min(dstParts, srcParts);
2385 for (
unsigned i = 0; i < n; i++) {
2398 if (multiplier == 0 || srcPart == 0) {
2408 if (low + mid < low)
2415 if (low + mid < low)
2420 if (low + carry < low)
2427 if (low + dst[i] < low)
2436 if (srcParts < dstParts) {
2438 assert(srcParts + 1 == dstParts);
2439 dst[srcParts] = carry;
2451 for (
unsigned i = dstParts; i < srcParts; i++)
2464 const WordType *rhs,
unsigned parts) {
2465 assert(dst != lhs && dst != rhs);
2468 tcSet(dst, 0, parts);
2470 for (
unsigned i = 0; i < parts; i++)
2480 const WordType *rhs,
unsigned lhsParts,
2481 unsigned rhsParts) {
2483 if (lhsParts > rhsParts)
2486 assert(dst != lhs && dst != rhs);
2488 tcSet(dst, 0, rhsParts);
2490 for (
unsigned i = 0; i < lhsParts; i++)
2491 tcMultiplyPart(&dst[i], rhs, lhs[i], 0, rhsParts, rhsParts + 1,
true);
2507 assert(lhs != remainder && lhs != srhs && remainder != srhs);
2509 unsigned shiftCount =
tcMSB(rhs, parts) + 1;
2510 if (shiftCount == 0)
2520 tcSet(lhs, 0, parts);
2531 if (shiftCount == 0)
2535 if ((mask >>= 1) == 0) {
2536 mask = (
WordType) 1 << (APINT_BITS_PER_WORD - 1);
2556 if (BitShift == 0) {
2557 std::memmove(Dst + WordShift, Dst, (Words - WordShift) *
APINT_WORD_SIZE);
2559 while (Words-- > WordShift) {
2560 Dst[Words] = Dst[Words - WordShift] << BitShift;
2561 if (Words > WordShift)
2582 unsigned WordsToMove = Words - WordShift;
2584 if (BitShift == 0) {
2587 for (
unsigned i = 0; i != WordsToMove; ++i) {
2588 Dst[i] = Dst[i + WordShift] >> BitShift;
2589 if (i + 1 != WordsToMove)
2600 for (
unsigned i = 0; i < parts; i++)
2606 for (
unsigned i = 0; i < parts; i++)
2612 for (
unsigned i = 0; i < parts; i++)
2618 for (
unsigned i = 0; i < parts; i++)
2627 if (lhs[parts] != rhs[parts])
2628 return (lhs[parts] > rhs[parts]) ? 1 : -1;
APInt multiplicativeInverse(const APInt &modulo) const
Computes the multiplicative inverse of this APInt for a given modulo.
static void tcOr(WordType *, const WordType *, unsigned)
void push_back(const T &Elt)
static APInt::WordType lowHalf(APInt::WordType part)
const_iterator end(StringRef path)
Get end iterator over path.
static bool tcIsZero(const WordType *, unsigned)
Returns true if a bignum is zero, false otherwise.
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 unsigned tcLSB(const WordType *, unsigned n)
Returns the bit number of the least or most significant set bit of a number.
T findLastSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the last set bit starting from the least significant bit.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
uint64_t getZExtValue() const
Get zero extended value.
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...
bool ult(const APInt &RHS) const
Unsigned less than comparison.
void dump() const
debug method
static void tcExtract(WordType *, unsigned dstCount, const WordType *, unsigned srcBits, unsigned srcLSB)
Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to DST, of dstCOUNT parts...
APInt reverseBits() const
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 tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
static uint64_t * getMemory(unsigned numWords)
A utility function for allocating memory and checking for allocation failure.
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
void print(raw_ostream &OS, bool isSigned) const
static unsigned getBitsNeeded(StringRef str, uint8_t radix)
Get bits required for string value.
static int tcExtractBit(const WordType *, unsigned bit)
Extract the given bit of a bignum; returns 0 or 1. Zero-based.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
static void tcSetLeastSignificantBits(WordType *, unsigned, unsigned bits)
Set the least significant BITS and clear the rest.
APInt smul_ov(const APInt &RHS, bool &Overflow) const
static int tcMultiply(WordType *, const WordType *, const WordType *, unsigned)
DST = LHS * RHS, where DST has the same width as the operands and is filled with the least significan...
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
static uint64_t round(uint64_t Acc, uint64_t Input)
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t *r, unsigned m, unsigned n)
Implementation of Knuth's Algorithm D (Division of nonnegative integers) from "Art of Computer Progra...
static int tcCompare(const WordType *, const WordType *, unsigned)
Comparison (unsigned) of two bignums.
ms magic() const
Calculate the magic numbers required to implement a signed integer division by a constant as a sequen...
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
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.
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.
void setBit(unsigned BitPosition)
Set a given bit to 1.
APInt & operator*=(const APInt &RHS)
Multiplication assignment operator.
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 getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
uint64_t VAL
Used to store the <= 64 bits integer value.
static WordType tcAddPart(WordType *, WordType, unsigned)
DST += RHS. Returns the carry flag.
static APInt::WordType lowBitMask(unsigned bits)
void AddInteger(signed I)
uint32_t ByteSwap_32(uint32_t Value)
Return a byte-swapped representation of the 32-bit argument.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt 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.
bool getBoolValue() const
Convert APInt to a boolean value.
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.
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
static const WordType WORD_MAX
APInt zextOrSelf(unsigned width) const
Zero extend or truncate to width.
APInt usub_ov(const APInt &RHS, bool &Overflow) const
uint16_t ByteSwap_16(uint16_t Value)
Return a byte-swapped representation of the 16-bit argument.
void clearBit(unsigned BitPosition)
Set a given bit to 0.
static void tcXor(WordType *, const WordType *, unsigned)
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 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.
APInt & operator--()
Prefix decrement operator.
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
static WordType tcIncrement(WordType *dst, unsigned parts)
Increment a bignum in-place. Return the carry flag.
void toString(SmallVectorImpl< char > &Str, unsigned Radix, bool Signed, bool formatAsCLiteral=false) const
Converts an APInt to a string and append it to Str.
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 extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
APInt operator*(const APInt &RHS) const
Multiplication operator.
void flipBit(unsigned bitPosition)
Toggles a given bit to its opposite value.
static void tcAssign(WordType *, const WordType *, unsigned)
Assign one bignum to another.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
int64_t getSExtValue() const
Get sign extended value.
unsigned countLeadingOnes() const
Count the number of leading one bits.
static WordType tcSubtract(WordType *, const WordType *, WordType carry, unsigned)
DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
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
unsigned getBitWidth() const
Return the number of bits in the APInt.
The returned value is numeric_limits<T>::max()
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
static int tcDivide(WordType *lhs, const WordType *rhs, WordType *remainder, WordType *scratch, unsigned parts)
If RHS is zero LHS and REMAINDER are left unchanged, return one.
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
static WordType tcSubtractPart(WordType *, WordType, unsigned)
DST -= RHS. Returns the carry flag.
static void tcComplement(WordType *, unsigned)
static void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
static WordType tcAdd(WordType *, const WordType *, WordType carry, unsigned)
DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
T findFirstSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the first set bit starting from the least significant bit.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
static void tcSet(WordType *, WordType, unsigned)
Sets the least significant part of a bignum to the input value, and zeroes out higher parts...
APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
static unsigned rotateModulo(unsigned BitWidth, const APInt &rotateAmt)
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.
static unsigned partLSB(APInt::WordType value)
APInt & operator++()
Prefix increment operator.
unsigned logBase2() const
static void tcShiftLeft(WordType *, unsigned Words, unsigned Count)
Shift a bignum left Count bits.
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
static void tcAnd(WordType *, const WordType *, unsigned)
The obvious AND, OR and XOR and complement operations.
void negate()
Negate this APInt in place.
static APInt::WordType highHalf(APInt::WordType part)
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.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
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.
llvm_unreachable("Unknown RegBank!")
static void tcNegate(WordType *, unsigned)
Negate a bignum in-place.
bool isAllOnesValue() const
Determine if all bits are set.
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.
uint64_t ByteSwap_64(uint64_t Value)
Return a byte-swapped representation of the 64-bit argument.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
static void tcClearBit(WordType *, unsigned bit)
Clear the given bit of a bignum. Zero-based.
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.
static void tcFullMultiply(WordType *, const WordType *, const WordType *, unsigned, unsigned)
DST = LHS * RHS, where DST has width the sum of the widths of the operands.
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
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.
static unsigned tcMSB(const WordType *parts, unsigned n)
void Profile(FoldingSetNodeID &id) const
Used to insert APInt objects, or objects that contain APInt objects, into FoldingSets.
static WordType tcDecrement(WordType *dst, unsigned parts)
Decrement a bignum in-place. Return the borrow flag.
constexpr uint64_t Make_64(uint32_t High, uint32_t Low)
Make a 64-bit integer from a high / low pair of 32-bit integers.
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.
static void tcSetBit(WordType *, unsigned bit)
Set the given bit of a bignum. Zero-based.
APInt & operator<<=(unsigned ShiftAmt)
Left-shift assignment function.
StringRef - Represent a constant reference to a string, i.e.
static int tcMultiplyPart(WordType *dst, const WordType *src, WordType multiplier, WordType carry, unsigned srcParts, unsigned dstParts, bool add)
DST += SRC * MULTIPLIER + PART if add is true DST = SRC * MULTIPLIER + PART if add is false...
APInt zext(unsigned width) const
Zero extend to a new width.
static void tcShiftRight(WordType *, unsigned Words, unsigned Count)
Shift a bignum right Count bits.
APInt abs() const
Get the absolute value;.
APInt RoundDoubleToAPInt(double Double, unsigned width)
Converts the given double value into a APInt.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
static unsigned partMSB(APInt::WordType value)
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.