1//===-- APInt.cpp - Implement APInt class ---------------------------------===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//===----------------------------------------------------------------------===//
9// This file implements a class to represent arbitrary precision integer
10// constant values and provide a variety of arithmetic operations on them.
12//===----------------------------------------------------------------------===//
21#include "llvm/Config/llvm-config.h"
32 #define DEBUG_TYPE "apint"
34/// A utility function for allocating memory, checking for allocation failures,
35/// and ensuring the contents are zeroed.
40/// A utility function for allocating memory and checking for allocation
41/// failure. The content is not zeroed.
46/// A utility function that converts a character to a digit.
50 if (radix == 16 || radix == 36) {
86void APInt::initSlowCase(
const APInt& that) {
92 assert(bigVal.
data() &&
"Null pointer detected!");
96 // Get memory, cleared to 0
98 // Calculate the number of words to copy
100 // Copy the words from bigVal to pVal
103 // Make sure unused high bits are cleared
108 initFromArray(bigVal);
112 : BitWidth(numBits) {
113 initFromArray(
ArrayRef(bigVal, numWords));
117 : BitWidth(numbits) {
118 fromString(numbits, Str, radix);
121void APInt::reallocate(
unsigned NewBitWidth) {
122 // If the number of words is the same we can just change the width and stop.
128 // If we have an allocation, delete it.
135 // If we are supposed to have an allocation, create it.
140void APInt::assignSlowCase(
const APInt &
RHS) {
141 // Don't do anything for X = X
145 // Adjust the bit width and handle allocations as necessary.
146 reallocate(
RHS.getBitWidth());
155/// This method 'profiles' an APInt for use with FoldingSet.
157 ID.AddInteger(BitWidth);
160 ID.AddInteger(U.VAL);
165 for (
unsigned i = 0; i < NumWords; ++i)
166 ID.AddInteger(U.pVal[i]);
173 const unsigned MinimumTrailingZeroes =
Log2(
A);
174 return TrailingZeroes >= MinimumTrailingZeroes;
177/// Prefix increment operator. Increments the APInt by one.
183 return clearUnusedBits();
186/// Prefix decrement operator. Decrements the APInt by one.
192 return clearUnusedBits();
195/// Adds the RHS APInt to this APInt.
196/// @returns this, after addition of RHS.
197/// Addition assignment operator.
199 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
204 return clearUnusedBits();
212 return clearUnusedBits();
215/// Subtracts the RHS APInt from this APInt
216/// @returns this, after subtraction
217/// Subtraction assignment operator.
219 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
224 return clearUnusedBits();
232 return clearUnusedBits();
236 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
238 return APInt(BitWidth, U.VAL * RHS.U.VAL,
/*isSigned=*/false,
239 /*implicitTrunc=*/true);
243 Result.clearUnusedBits();
247void APInt::andAssignSlowCase(
const APInt &RHS) {
248 WordType *dst = U.pVal, *rhs = RHS.U.pVal;
253void APInt::orAssignSlowCase(
const APInt &
RHS) {
259void APInt::xorAssignSlowCase(
const APInt &
RHS) {
275 tcMultiplyPart(U.pVal, U.pVal, RHS, 0, NumWords, NumWords,
false);
277 return clearUnusedBits();
280bool APInt::equalSlowCase(
const APInt &RHS)
const {
281 return std::equal(U.pVal, U.pVal +
getNumWords(), RHS.U.pVal);
284int APInt::compare(
const APInt&
RHS)
const {
287 return U.VAL <
RHS.U.VAL ? -1 : U.VAL >
RHS.U.VAL;
292int APInt::compareSigned(
const APInt&
RHS)
const {
293 assert(BitWidth ==
RHS.BitWidth &&
"Bit widths must be same for comparison");
297 return lhsSext < rhsSext ? -1 : lhsSext > rhsSext;
301 bool rhsNeg =
RHS.isNegative();
303 // If the sign bits don't match, then (LHS < RHS) if LHS is negative
304 if (lhsNeg != rhsNeg)
305 return lhsNeg ? -1 : 1;
307 // Otherwise we can just use an unsigned comparison, because even negative
308 // numbers compare correctly this way if both have the same signed-ness.
312void APInt::setBitsSlowCase(
unsigned loBit,
unsigned hiBit) {
313 unsigned loWord = whichWord(loBit);
314 unsigned hiWord = whichWord(hiBit);
316 // Create an initial mask for the low word with zeros below loBit.
319 // If hiBit is not aligned, we need a high mask.
320 unsigned hiShiftAmt = whichBit(hiBit);
321 if (hiShiftAmt != 0) {
322 // Create a high mask with zeros above hiBit.
324 // If loWord and hiWord are equal, then we combine the masks. Otherwise,
325 // set the bits in hiWord.
326 if (hiWord == loWord)
329 U.pVal[hiWord] |= hiMask;
331 // Apply the mask to the low word.
332 U.pVal[loWord] |= loMask;
334 // Fill any words between loWord and hiWord with all ones.
335 for (
unsigned word = loWord + 1; word < hiWord; ++word)
339void APInt::clearBitsSlowCase(
unsigned LoBit,
unsigned HiBit) {
340 unsigned LoWord = whichWord(LoBit);
341 unsigned HiWord = whichWord(HiBit);
343 // Create an initial mask for the low word with ones below loBit.
346 // If HiBit is not aligned, we need a high mask.
347 unsigned HiShiftAmt = whichBit(HiBit);
348 if (HiShiftAmt != 0) {
349 // Create a high mask with ones above HiBit.
351 // If LoWord and HiWord are equal, then we combine the masks. Otherwise,
352 // clear the bits in HiWord.
353 if (HiWord == LoWord)
356 U.pVal[HiWord] &= HiMask;
358 // Apply the mask to the low word.
359 U.pVal[LoWord] &= LoMask;
361 // Fill any words between LoWord and HiWord with all zeros.
362 for (
unsigned Word = LoWord + 1;
Word < HiWord; ++
Word)
366// Complement a bignum in-place.
368 for (
unsigned i = 0; i < parts; i++)
372/// Toggle every bit to its opposite value.
373void APInt::flipAllBitsSlowCase() {
378/// Concatenate the bits from "NewLSB" onto the bottom of *this. This is
380/// (this->zext(NewWidth) << NewLSB.getBitWidth()) | NewLSB.zext(NewWidth)
381/// In the slow case, we know the result is large.
382APInt APInt::concatSlowCase(
const APInt &NewLSB)
const {
389/// Toggle a given bit to its opposite value whose position is given
391/// Toggles a given bit to its opposite value.
393 assert(bitPosition < BitWidth &&
"Out of the bit-width range!");
394 setBitVal(bitPosition, !(*
this)[bitPosition]);
399 assert((subBitWidth + bitPosition) <= BitWidth &&
"Illegal bit insertion");
401 // inserting no bits is a noop.
402 if (subBitWidth == 0)
405 // Insertion is a direct copy.
406 if (subBitWidth == BitWidth) {
411 // Single word result can be done as a direct bitmask.
414 U.VAL &= ~(
mask << bitPosition);
415 U.VAL |= (subBits.U.
VAL << bitPosition);
419 unsigned loBit = whichBit(bitPosition);
420 unsigned loWord = whichWord(bitPosition);
421 unsigned hi1Word = whichWord(bitPosition + subBitWidth - 1);
423 // Insertion within a single word can be done as a direct bitmask.
424 if (loWord == hi1Word) {
426 U.pVal[loWord] &= ~(
mask << loBit);
427 U.pVal[loWord] |= (subBits.U.
VAL << loBit);
431 // Insert on word boundaries.
433 // Direct copy whole words.
438 // Mask+insert remaining bits.
440 if (remainingBits != 0) {
442 U.pVal[hi1Word] &=
~mask;
443 U.pVal[hi1Word] |= subBits.getWord(subBitWidth - 1);
448 // General case - set/clear individual bits in dst based on src.
449 // TODO - there is scope for optimization here, but at the moment this code
450 // path is barely used so prefer readability over performance.
451 for (
unsigned i = 0; i != subBitWidth; ++i)
459 U.VAL &= ~(maskBits << bitPosition);
460 U.VAL |= subBits << bitPosition;
464 unsigned loBit = whichBit(bitPosition);
465 unsigned loWord = whichWord(bitPosition);
466 unsigned hiWord = whichWord(bitPosition + numBits - 1);
467 if (loWord == hiWord) {
468 U.pVal[loWord] &= ~(maskBits << loBit);
469 U.pVal[loWord] |= subBits << loBit;
473 static_assert(8 *
sizeof(
WordType) <= 64,
"This code assumes only two words affected");
474 unsigned wordBits = 8 *
sizeof(
WordType);
475 U.pVal[loWord] &= ~(maskBits << loBit);
476 U.pVal[loWord] |= subBits << loBit;
478 U.pVal[hiWord] &= ~(maskBits >> (wordBits - loBit));
479 U.pVal[hiWord] |= subBits >> (wordBits - loBit);
483 assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth &&
484 "Illegal bit extraction");
487 return APInt(numBits, U.VAL >> bitPosition,
/*isSigned=*/false,
488 /*implicitTrunc=*/true);
490 unsigned loBit = whichBit(bitPosition);
491 unsigned loWord = whichWord(bitPosition);
492 unsigned hiWord = whichWord(bitPosition + numBits - 1);
494 // Single word result extracting bits from a single word source.
495 if (loWord == hiWord)
496 return APInt(numBits, U.pVal[loWord] >> loBit,
/*isSigned=*/false,
497 /*implicitTrunc=*/true);
499 // Extracting bits that start on a source word boundary can be done
500 // as a fast memory copy.
502 return APInt(numBits,
ArrayRef(U.pVal + loWord, 1 + hiWord - loWord));
504 // General case - shift + copy source words directly into place.
505 APInt Result(numBits, 0);
507 unsigned NumDstWords = Result.getNumWords();
509 uint64_t *DestPtr = Result.isSingleWord() ? &Result.U.VAL : Result.U.pVal;
510 for (
unsigned word = 0; word < NumDstWords; ++word) {
511 uint64_t w0 = U.pVal[loWord + word];
513 (loWord + word + 1) < NumSrcWords ? U.pVal[loWord + word + 1] : 0;
517 return Result.clearUnusedBits();
521 unsigned bitPosition)
const {
522 assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth &&
523 "Illegal bit extraction");
524 assert(numBits <= 64 &&
"Illegal bit extraction");
528 return (U.VAL >> bitPosition) & maskBits;
531 "This code assumes only two words affected");
532 unsigned loBit = whichBit(bitPosition);
533 unsigned loWord = whichWord(bitPosition);
534 unsigned hiWord = whichWord(bitPosition + numBits - 1);
535 if (loWord == hiWord)
536 return (U.pVal[loWord] >> loBit) & maskBits;
538 uint64_t retBits = U.pVal[loWord] >> loBit;
545 assert(!Str.empty() &&
"Invalid string length");
546 size_t StrLen = Str.size();
548 // Each computation below needs to know if it's negative.
549 unsigned IsNegative =
false;
550 if (Str[0] ==
'-' || Str[0] ==
'+') {
551 IsNegative = Str[0] ==
'-';
553 assert(StrLen &&
"String is only a sign, needs a value.");
556 // For radixes of power-of-two values, the bits required is accurately and
559 return StrLen + IsNegative;
561 return StrLen * 3 + IsNegative;
563 return StrLen * 4 + IsNegative;
565 // Compute a sufficient number of bits that is always large enough but might
566 // be too large. This avoids the assertion in the constructor. This
567 // calculation doesn't work appropriately for the numbers 0-9, so just use 4
568 // bits in that case.
570 return (StrLen == 1 ? 4 : StrLen * 64 / 18) + IsNegative;
573 return (StrLen == 1 ? 7 : StrLen * 16 / 3) + IsNegative;
577 // Compute a sufficient number of bits that is always large enough but might
581 // For bases 2, 8, and 16, the sufficient number of bits is exact and we can
582 // return the value directly. For bases 10 and 36, we need to do extra work.
583 if (radix == 2 || radix == 8 || radix == 16)
586 // This is grossly inefficient but accurate. We could probably do something
587 // with a computation of roughly slen*64/20 and then adjust by the value of
588 // the first few digits. But, I'm not sure how accurate that could be.
589 size_t slen = str.
size();
591 // Each computation below needs to know if it's negative.
594 if (*p ==
'-' || *p ==
'+') {
597 assert(slen &&
"String is only a sign, needs a value.");
601 // Convert to the actual binary value.
604 // Compute how many bits are required. If the log is infinite, assume we need
605 // just bit. If the log is exact and value is negative, then the value is
606 // MinSignedValue with (log + 1) bits.
608 if (log == (
unsigned)-1) {
632 "SplatSizeInBits must divide width!");
633 // We can check that all parts of an integer are equal by making use of a
634 // little trick: rotate and check if it's still the same value.
635 return *
this ==
rotl(SplatSizeInBits);
638/// This function returns the high "numBits" bits of this APInt.
640 return this->
lshr(BitWidth - numBits);
643/// This function returns the low "numBits" bits of this APInt.
650/// Return a value containing V broadcasted over NewLen bits.
652 assert(NewLen >= V.getBitWidth() &&
"Can't splat to smaller bit width!");
654 APInt Val = V.zext(NewLen);
655 for (
unsigned I = V.getBitWidth();
I < NewLen;
I <<= 1)
661unsigned APInt::countLeadingZerosSlowCase()
const {
672 // Adjust for unused bits in the most significant word (they are zero).
678unsigned APInt::countLeadingOnesSlowCase()
const {
689 if (
Count == highWordBits) {
690 for (i--; i >= 0; --i) {
702unsigned APInt::countTrailingZerosSlowCase()
const {
709 return std::min(
Count, BitWidth);
712unsigned APInt::countTrailingOnesSlowCase()
const {
723unsigned APInt::countPopulationSlowCase()
const {
730bool APInt::intersectsSlowCase(
const APInt &
RHS)
const {
732 if ((U.pVal[i] &
RHS.U.pVal[i]) != 0)
738bool APInt::isSubsetOfSlowCase(
const APInt &
RHS)
const {
740 if ((U.pVal[i] & ~
RHS.U.pVal[i]) != 0)
747 assert(BitWidth >= 16 && BitWidth % 8 == 0 &&
"Cannot byteswap!");
752 if (BitWidth <= 64) {
754 Tmp1 >>= (64 - BitWidth);
755 return APInt(BitWidth, Tmp1);
761 if (Result.BitWidth != BitWidth) {
762 Result.lshrInPlace(Result.BitWidth - BitWidth);
763 Result.BitWidth = BitWidth;
785 APInt Reversed(BitWidth, 0);
786 unsigned S = BitWidth;
799 // Fast-path a common case.
800 if (
A ==
B)
return A;
802 // Corner cases: if either operand is zero, the other is the gcd.
806 // Count common powers of 2 and remove all other powers of 2.
809 unsigned Pow2_A =
A.countr_zero();
810 unsigned Pow2_B =
B.countr_zero();
811 if (Pow2_A > Pow2_B) {
812 A.lshrInPlace(Pow2_A - Pow2_B);
814 }
else if (Pow2_B > Pow2_A) {
815 B.lshrInPlace(Pow2_B - Pow2_A);
822 // Both operands are odd multiples of 2^Pow_2:
824 // gcd(a, b) = gcd(|a - b| / 2^i, min(a, b))
826 // This is a modified version of Stein's algorithm, taking advantage of
827 // efficient countTrailingZeros().
831 A.lshrInPlace(
A.countr_zero() - Pow2);
834 B.lshrInPlace(
B.countr_zero() - Pow2);
844 // Get the sign bit from the highest order bit
847 // Get the 11-bit exponent and adjust for the 1023 bit bias
848 int64_t exp = ((
I >> 52) & 0x7ff) - 1023;
850 // If the exponent is negative, the value is < 0 so just return 0.
852 return APInt(width, 0u);
854 // Extract the mantissa by clearing the top 12 bits (sign + exponent).
855 uint64_t mantissa = (
I & (~0ULL >> 12)) | 1ULL << 52;
857 // If the exponent doesn't shift all bits out of the mantissa
859 return isNeg ? -
APInt(width, mantissa >> (52 - exp)) :
860 APInt(width, mantissa >> (52 - exp));
862 // If the client didn't provide enough bits for us to shift the mantissa into
863 // then the result is undefined, just return 0
864 if (width <= exp - 52)
865 return APInt(width, 0);
867 // Otherwise, we have to shift the mantissa bits up to the right location
868 APInt Tmp(width, mantissa);
870 return isNeg ? -Tmp : Tmp;
873/// This function converts this APInt to a double.
874/// The layout for double is as following (IEEE Standard 754):
875/// --------------------------------------
876/// | Sign Exponent Fraction Bias |
877/// |-------------------------------------- |
878/// | 1[63] 11[62-52] 52[51-00] 1023 |
879/// --------------------------------------
881 // Handle the simple case where the value is contained in one uint64_t.
882 // It is wrong to optimize getWord(0) to VAL; there might be more than one word.
888 return double(getWord(0));
891 // Determine if the value is negative.
894 // Construct the absolute value if we're negative.
897 // Figure out how many bits we're using.
900 // The exponent (without bias normalization) is just the number of bits
901 // we are using. Note that the sign bit is gone since we constructed the
905 // Return infinity for exponent overflow
908 return std::numeric_limits<double>::infinity();
910 return -std::numeric_limits<double>::infinity();
912 exp += 1023;
// Increment for 1023 bias
914 // Number of bits in mantissa is 52. To obtain the mantissa value, we must
915 // extract the high 52 bits from the correct words in pVal.
917 unsigned hiWord = whichWord(n-1);
919 mantissa = Tmp.U.
pVal[0];
921 mantissa >>= n - 52;
// shift down, we want the top 52 bits.
923 assert(hiWord > 0 &&
"huh?");
926 mantissa = hibits | lobits;
929 // The leading bit of mantissa is implicit, so get rid of it.
931 uint64_t I = sign | (exp << 52) | mantissa;
935// Truncate to new width.
937 assert(width <= BitWidth &&
"Invalid APInt Truncate request");
941 /*implicitTrunc=*/true);
943 if (width == BitWidth)
951 Result.U.pVal[i] = U.pVal[i];
953 // Truncate and copy any partial word.
956 Result.U.pVal[i] = U.pVal[i] << bits >> bits;
961// Truncate to new width with unsigned saturation.
963 assert(width <= BitWidth &&
"Invalid APInt Truncate request");
965 // Can we just losslessly truncate it?
968 // If not, then just return the new limit.
972// Truncate to new width with signed saturation.
974 assert(width <= BitWidth &&
"Invalid APInt Truncate request");
976 // Can we just losslessly truncate it?
979 // If not, then just return the new limits.
984// Sign extend to a new width.
986 assert(Width >= BitWidth &&
"Invalid APInt SignExtend request");
991 if (Width == BitWidth)
999 // Sign extend the last word since there may be unused bits in the input.
1004 // Fill with sign bits.
1007 Result.clearUnusedBits();
1011// Zero extend to a new width.
1013 assert(width >= BitWidth &&
"Invalid APInt ZeroExtend request");
1016 return APInt(width, U.VAL);
1018 if (width == BitWidth)
1026 // Zero remaining words.
1034 if (BitWidth < width)
1036 if (BitWidth > width)
1037 return trunc(width);
1042 if (BitWidth < width)
1044 if (BitWidth > width)
1045 return trunc(width);
1049/// Arithmetic right-shift this APInt by shiftAmt.
1050/// Arithmetic right-shift function.
1055/// Arithmetic right-shift this APInt by shiftAmt.
1056/// Arithmetic right-shift function.
1057void APInt::ashrSlowCase(
unsigned ShiftAmt) {
1058 // Don't bother performing a no-op shift.
1062 // Save the original sign bit for later.
1065 // WordShift is the inter-part shift; BitShift is intra-part shift.
1070 if (WordsToMove != 0) {
1071 // Sign extend the last word to fill in the unused bits.
1075 // Fastpath for moving by whole words.
1076 if (BitShift == 0) {
1077 std::memmove(U.pVal, U.pVal + WordShift, WordsToMove *
APINT_WORD_SIZE);
1079 // Move the words containing significant bits.
1080 for (
unsigned i = 0; i != WordsToMove - 1; ++i)
1081 U.pVal[i] = (U.pVal[i + WordShift] >> BitShift) |
1084 // Handle the last word which has no high bits to copy. Use an arithmetic
1085 // shift to preserve the sign bit.
1086 U.pVal[WordsToMove - 1] =
1087 (int64_t)U.pVal[WordShift + WordsToMove - 1] >> BitShift;
1091 // Fill in the remainder based on the original sign.
1092 std::memset(U.pVal + WordsToMove, Negative ? -1 : 0,
1097/// Logical right-shift this APInt by shiftAmt.
1098/// Logical right-shift function.
1103/// Logical right-shift this APInt by shiftAmt.
1104/// Logical right-shift function.
1105void APInt::lshrSlowCase(
unsigned ShiftAmt) {
1109/// Left-shift this APInt by shiftAmt.
1110/// Left-shift function.
1112 // It's undefined behavior in C to shift by BitWidth or greater.
1117void APInt::shlSlowCase(
unsigned ShiftAmt) {
1122// Calculate the rotate amount modulo the bit width.
1127 APInt rot = rotateAmt;
1129 // Extend the rotate APInt, so that the urem doesn't divide by 0.
1130 // e.g. APInt(1, 32) would give APInt(1, 0).
1144 rotateAmt %= BitWidth;
1147 return shl(rotateAmt) |
lshr(BitWidth - rotateAmt);
1157 rotateAmt %= BitWidth;
1160 return lshr(rotateAmt) |
shl(BitWidth - rotateAmt);
1163/// \returns the nearest log base 2 of this APInt. Ties round up.
1165/// NOTE: When we have a BitWidth of 1, we define:
1167/// log2(0) = UINT32_MAX
1170/// to get around any mathematical concerns resulting from
1171/// referencing 2 in a space where 2 does no exist.
1173 // Special case when we have a bitwidth of 1. If VAL is 1, then we
1174 // get 0. If VAL is 0, we get WORDTYPE_MAX which gets truncated to
1179 // Handle the zero case.
1183 // The non-zero case is handled by computing:
1185 // nearestLogBase2(x) = logBase2(x) + x[logBase2(x)-1].
1187 // where x[i] is referring to the value of the ith bit of x.
1189 return lg +
unsigned((*
this)[lg - 1]);
1192// Square Root - this method computes and returns the square root of "this".
1193// Three mechanisms are used for computation. For small values (<= 5 bits),
1194// a table lookup is done. This gets some performance for common cases. For
1195// values using less than 52 bits, the value is converted to double and then
1196// the libc sqrt function is called. The result is rounded and then converted
1197// back to a uint64_t which is then used to construct the result. Finally,
1198// the Babylonian method for computing square roots is used.
1201 // Determine the magnitude of the value.
1204 // Use a fast table for some small values. This also gets rid of some
1205 // rounding errors in libc sqrt for small values.
1206 if (magnitude <= 5) {
1207 static const uint8_t results[32] = {
1210 /* 3- 6 */ 2, 2, 2, 2,
1211 /* 7-12 */ 3, 3, 3, 3, 3, 3,
1212 /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4,
1213 /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1219 // If the magnitude of the value fits in less than 52 bits (the precision of
1220 // an IEEE double precision floating point value), then we can use the
1221 // libc sqrt function which will probably use a hardware sqrt computation.
1222 // This should be faster than the algorithm below.
1223 if (magnitude < 52) {
1224 return APInt(BitWidth,
1229 // Okay, all the short cuts are exhausted. We must compute it. The following
1230 // is a classical Babylonian method for computing the square root. This code
1231 // was adapted to APInt from a wikipedia article on such computations.
1232 // See http://www.wikipedia.org/ and go to the page named
1233 // Calculate_an_integer_square_root.
1234 unsigned nbits = BitWidth, i = 4;
1235 APInt testy(BitWidth, 16);
1236 APInt x_old(BitWidth, 1);
1237 APInt x_new(BitWidth, 0);
1238 APInt two(BitWidth, 2);
1240 // Select a good starting value using binary logarithms.
1241 for (;; i += 2, testy = testy.
shl(2))
1242 if (i >= nbits || this->
ule(testy)) {
1243 x_old = x_old.
shl(i / 2);
1247 // Use the Babylonian method to arrive at the integer square root:
1249 x_new = (this->
udiv(x_old) + x_old).
udiv(two);
1250 if (x_old.
ule(x_new))
1255 // Make sure we return the closest approximation
1256 // NOTE: The rounding calculation below is correct. It will produce an
1257 // off-by-one discrepancy with results from pari/gp. That discrepancy has been
1258 // determined to be a rounding issue with pari/gp as it begins to use a
1259 // floating point representation after 192 bits. There are no discrepancies
1260 // between this algorithm and pari/gp for bit widths < 192 bits.
1261 APInt square(x_old * x_old);
1262 APInt nextSquare((x_old + 1) * (x_old +1));
1263 if (this->
ult(square))
1265 assert(this->
ule(nextSquare) &&
"Error in APInt::sqrt computation");
1266 APInt midpoint((nextSquare - square).
udiv(two));
1267 APInt offset(*
this - square);
1268 if (offset.
ult(midpoint))
1273/// \returns the multiplicative inverse of an odd APInt modulo 2^BitWidth.
1276 "multiplicative inverse is only defined for odd numbers!");
1278 // Use Newton's method.
1279 APInt Factor = *
this;
1281 while (!(
T = *
this * Factor).
isOne())
1282 Factor *= 2 - std::move(
T);
1286/// Implementation of Knuth's Algorithm D (Division of nonnegative integers)
1287/// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The
1288/// variables here have the same names as in the algorithm. Comments explain
1289/// the algorithm and any deviation from it.
1291 unsigned m,
unsigned n) {
1292 assert(u &&
"Must provide dividend");
1293 assert(v &&
"Must provide divisor");
1294 assert(q &&
"Must provide quotient");
1295 assert(u != v && u != q && v != q &&
"Must use different memory");
1296 assert(n>1 &&
"n must be > 1");
1298 // b denotes the base of the number system. In our case b is 2^32.
1301// The DEBUG macros here tend to be spam in the debug output if you're not
1302// debugging this code. Disable them unless KNUTH_DEBUG is defined.
1304#define DEBUG_KNUTH(X) LLVM_DEBUG(X)
1306#define DEBUG_KNUTH(X) do {} while(false)
1315 // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
1316 // u and v by d. Note that we have taken Knuth's advice here to use a power
1317 // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
1318 // 2 allows us to shift instead of multiply and it is easy to determine the
1319 // shift amount from the leading zeros. We are basically normalizing the u
1320 // and v so that its high bits are shifted to the top of v's range without
1321 // overflow. Note that this can require an extra word in u so that u must
1322 // be of length m+n+1.
1327 for (
unsigned i = 0; i < m+n; ++i) {
1328 uint32_t u_tmp = u[i] >> (32 - shift);
1329 u[i] = (u[i] << shift) | u_carry;
1332 for (
unsigned i = 0; i < n; ++i) {
1333 uint32_t v_tmp = v[i] >> (32 - shift);
1334 v[i] = (v[i] << shift) | v_carry;
1346 // D2. [Initialize j.] Set j to m. This is the loop counter over the places.
1350 // D3. [Calculate q'.].
1351 // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
1352 // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
1353 // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
1354 // qp by 1, increase rp by v[n-1], and repeat this test if rp < b. The test
1355 // on v[n-2] determines at high speed most of the cases in which the trial
1356 // value qp is one too large, and it eliminates all cases where qp is two
1362 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1365 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
1368 DEBUG_KNUTH(
dbgs() <<
"KnuthDiv: qp == " << qp <<
", rp == " << rp <<
'\n');
1370 // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
1371 // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
1372 // consists of a simple multiplication by a one-place number, combined with
1374 // The digits (u[j+n]...u[j]) should be kept positive; if the result of
1375 // this step is actually negative, (u[j+n]...u[j]) should be left as the
1376 // true value plus b**(n+1), namely as the b's complement of
1377 // the true value, and a "borrow" to the left should be remembered.
1379 for (
unsigned i = 0; i < n; ++i) {
1381 int64_t subres = int64_t(u[j+i]) - borrow -
Lo_32(p);
1382 u[j+i] =
Lo_32(subres);
1385 <<
", borrow = " << borrow <<
'\n');
1387 bool isNeg = u[j+n] < borrow;
1388 u[j+n] -=
Lo_32(borrow);
1394 // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was
1395 // negative, go to step D6; otherwise go on to step D7.
1398 // D6. [Add back]. The probability that this step is necessary is very
1399 // small, on the order of only 2/b. Make sure that test data accounts for
1400 // this possibility. Decrease q[j] by 1
1402 // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
1403 // A carry will occur to the left of u[j+n], and it should be ignored
1404 // since it cancels with the borrow that occurred in D4.
1406 for (
unsigned i = 0; i < n; i++) {
1407 uint32_t limit = std::min(u[j+i],v[i]);
1408 u[j+i] += v[i] + carry;
1409 carry = u[j+i] < limit || (carry && u[j+i] == limit);
1417 // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3.
1424 // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
1425 // remainder may be obtained by dividing u[...] by d. If r is non-null we
1426 // compute the remainder (urem uses this).
1428 // The value d is expressed by the "shift" value above since we avoided
1429 // multiplication by d by using a shift left. So, all we have to do is
1430 // shift right here.
1434 for (
int i = n-1; i >= 0; i--) {
1435 r[i] = (u[i] >> shift) | carry;
1436 carry = u[i] << (32 - shift);
1440 for (
int i = n-1; i >= 0; i--) {
1450void APInt::divide(
const WordType *
LHS,
unsigned lhsWords,
const WordType *
RHS,
1451 unsigned rhsWords, WordType *Quotient, WordType *Remainder) {
1452 assert(lhsWords >= rhsWords &&
"Fractional result");
1454 // First, compose the values into an array of 32-bit words instead of
1455 // 64-bit words. This is a necessity of both the "short division" algorithm
1456 // and the Knuth "classical algorithm" which requires there to be native
1457 // operations for +, -, and * on an m bit value with an m*2 bit result. We
1458 // can't use 64-bit operands here because we don't have native results of
1459 // 128-bits. Furthermore, casting the 64-bit values to 32-bit values won't
1460 // work on large-endian machines.
1461 unsigned n = rhsWords * 2;
1462 unsigned m = (lhsWords * 2) - n;
1464 // Allocate space for the temporary values we need either on the stack, if
1465 // it will fit, or on the heap if it won't.
1466 uint32_t SPACE[128];
1467 uint32_t *U =
nullptr;
1468 uint32_t *
V =
nullptr;
1469 uint32_t *Q =
nullptr;
1470 uint32_t *
R =
nullptr;
1471 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1474 Q = &SPACE[(m+n+1) + n];
1476 R = &SPACE[(m+n+1) + n + (m+n)];
1478 U =
new uint32_t[m + n + 1];
1479 V =
new uint32_t[n];
1480 Q =
new uint32_t[m+n];
1482 R =
new uint32_t[n];
1485 // Initialize the dividend
1486 memset(U, 0, (m+n+1)*
sizeof(uint32_t));
1487 for (
unsigned i = 0; i < lhsWords; ++i) {
1488 uint64_t tmp =
LHS[i];
1489 U[i * 2] =
Lo_32(tmp);
1490 U[i * 2 + 1] =
Hi_32(tmp);
1492 U[m+n] = 0;
// this extra word is for "spill" in the Knuth algorithm.
1494 // Initialize the divisor
1495 memset(V, 0, (n)*
sizeof(uint32_t));
1496 for (
unsigned i = 0; i < rhsWords; ++i) {
1497 uint64_t tmp =
RHS[i];
1499 V[i * 2 + 1] =
Hi_32(tmp);
1502 // initialize the quotient and remainder
1503 memset(Q, 0, (m+n) *
sizeof(uint32_t));
1505 memset(R, 0, n *
sizeof(uint32_t));
1507 // Now, adjust m and n for the Knuth division. n is the number of words in
1508 // the divisor. m is the number of words by which the dividend exceeds the
1509 // divisor (i.e. m+n is the length of the dividend). These sizes must not
1510 // contain any zero words or the Knuth algorithm fails.
1511 for (
unsigned i = n; i > 0 &&
V[i-1] == 0; i--) {
1515 for (
unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1518 // If we're left with only a single word for the divisor, Knuth doesn't work
1519 // so we implement the short division algorithm here. This is much simpler
1520 // and faster because we are certain that we can divide a 64-bit quantity
1521 // by a 32-bit quantity at hardware speed and short division is simply a
1522 // series of such operations. This is just like doing short division but we
1523 // are using base 2^32 instead of base 10.
1524 assert(n != 0 &&
"Divide by zero?");
1526 uint32_t divisor =
V[0];
1527 uint32_t remainder = 0;
1528 for (
int i = m; i >= 0; i--) {
1529 uint64_t partial_dividend =
Make_64(remainder, U[i]);
1530 if (partial_dividend == 0) {
1533 }
else if (partial_dividend < divisor) {
1535 remainder =
Lo_32(partial_dividend);
1536 }
else if (partial_dividend == divisor) {
1540 Q[i] =
Lo_32(partial_dividend / divisor);
1541 remainder =
Lo_32(partial_dividend - (Q[i] * divisor));
1547 // Now we're ready to invoke the Knuth classical divide algorithm. In this
1552 // If the caller wants the quotient
1554 for (
unsigned i = 0; i < lhsWords; ++i)
1555 Quotient[i] =
Make_64(Q[i*2+1], Q[i*2]);
1558 // If the caller wants the remainder
1560 for (
unsigned i = 0; i < rhsWords; ++i)
1561 Remainder[i] =
Make_64(R[i*2+1], R[i*2]);
1564 // Clean up the memory we allocated.
1565 if (U != &SPACE[0]) {
1574 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1576 // First, deal with the easy case
1578 assert(RHS.U.VAL != 0 &&
"Divide by zero?");
1579 return APInt(BitWidth, U.VAL / RHS.U.VAL);
1582 // Get some facts about the LHS and RHS number of bits and words
1584 unsigned rhsBits = RHS.getActiveBits();
1586 assert(rhsWords &&
"Divided by zero???");
1588 // Deal with some degenerate cases
1591 return APInt(BitWidth, 0);
1595 if (lhsWords < rhsWords || this->
ult(RHS))
1596 // X / Y ===> 0, iff X < Y
1597 return APInt(BitWidth, 0);
1600 return APInt(BitWidth, 1);
1601 if (lhsWords == 1)
// rhsWords is 1 if lhsWords is 1.
1602 // All high words are zero, just use native divide
1603 return APInt(BitWidth, this->U.pVal[0] / RHS.U.pVal[0]);
1605 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1606 APInt Quotient(BitWidth, 0);
// to hold result.
1607 divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords, Quotient.U.
pVal,
nullptr);
1612 assert(RHS != 0 &&
"Divide by zero?");
1614 // First, deal with the easy case
1616 return APInt(BitWidth, U.VAL / RHS);
1618 // Get some facts about the LHS words.
1621 // Deal with some degenerate cases
1624 return APInt(BitWidth, 0);
1629 // X / Y ===> 0, iff X < Y
1630 return APInt(BitWidth, 0);
1633 return APInt(BitWidth, 1);
1634 if (lhsWords == 1)
// rhsWords is 1 if lhsWords is 1.
1635 // All high words are zero, just use native divide
1636 return APInt(BitWidth, this->U.pVal[0] / RHS);
1638 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1639 APInt Quotient(BitWidth, 0);
// to hold result.
1640 divide(U.pVal, lhsWords, &RHS, 1, Quotient.U.
pVal,
nullptr);
1646 if (RHS.isNegative())
1647 return (-(*
this)).udiv(-RHS);
1648 return -((-(*this)).udiv(RHS));
1650 if (RHS.isNegative())
1651 return -(this->
udiv(-RHS));
1652 return this->
udiv(RHS);
1658 return (-(*
this)).udiv(-RHS);
1659 return -((-(*this)).udiv(RHS));
1662 return -(this->
udiv(-RHS));
1663 return this->
udiv(RHS);
1667 assert(BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1669 assert(RHS.U.VAL != 0 &&
"Remainder by zero?");
1670 return APInt(BitWidth, U.VAL % RHS.U.VAL);
1673 // Get some facts about the LHS
1676 // Get some facts about the RHS
1677 unsigned rhsBits = RHS.getActiveBits();
1679 assert(rhsWords &&
"Performing remainder operation by zero ???");
1681 // Check the degenerate cases
1684 return APInt(BitWidth, 0);
1687 return APInt(BitWidth, 0);
1688 if (lhsWords < rhsWords || this->
ult(RHS))
1689 // X % Y ===> X, iff X < Y
1693 return APInt(BitWidth, 0);
1695 // All high words are zero, just use native remainder
1696 return APInt(BitWidth, U.pVal[0] % RHS.U.pVal[0]);
1698 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1699 APInt Remainder(BitWidth, 0);
1700 divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords,
nullptr, Remainder.U.pVal);
1705 assert(RHS != 0 &&
"Remainder by zero?");
1710 // Get some facts about the LHS
1713 // Check the degenerate cases
1721 // X % Y ===> X, iff X < Y
1727 // All high words are zero, just use native remainder
1728 return U.pVal[0] % RHS;
1730 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1732 divide(U.pVal, lhsWords, &RHS, 1,
nullptr, &Remainder);
1738 if (RHS.isNegative())
1739 return -((-(*this)).urem(-RHS));
1740 return -((-(*this)).urem(RHS));
1742 if (RHS.isNegative())
1743 return this->
urem(-RHS);
1744 return this->
urem(RHS);
1750 return -((-(*this)).urem(-RHS));
1751 return -((-(*this)).urem(RHS));
1754 return this->
urem(-RHS);
1755 return this->
urem(RHS);
1760 assert(LHS.BitWidth == RHS.BitWidth &&
"Bit widths must be the same");
1761 unsigned BitWidth = LHS.BitWidth;
1763 // First, deal with the easy case
1764 if (LHS.isSingleWord()) {
1765 assert(RHS.U.VAL != 0 &&
"Divide by zero?");
1766 uint64_t QuotVal = LHS.U.VAL / RHS.U.VAL;
1767 uint64_t RemVal = LHS.U.VAL % RHS.U.VAL;
1768 Quotient =
APInt(BitWidth, QuotVal);
1769 Remainder =
APInt(BitWidth, RemVal);
1773 // Get some size facts about the dividend and divisor
1774 unsigned lhsWords =
getNumWords(LHS.getActiveBits());
1775 unsigned rhsBits = RHS.getActiveBits();
1777 assert(rhsWords &&
"Performing divrem operation by zero ???");
1779 // Check the degenerate cases
1780 if (lhsWords == 0) {
1781 Quotient =
APInt(BitWidth, 0);
// 0 / Y ===> 0
1782 Remainder =
APInt(BitWidth, 0);
// 0 % Y ===> 0
1787 Quotient = LHS;
// X / 1 ===> X
1788 Remainder =
APInt(BitWidth, 0);
// X % 1 ===> 0
1791 if (lhsWords < rhsWords || LHS.ult(RHS)) {
1792 Remainder = LHS;
// X % Y ===> X, iff X < Y
1793 Quotient =
APInt(BitWidth, 0);
// X / Y ===> 0, iff X < Y
1798 Quotient =
APInt(BitWidth, 1);
// X / X ===> 1
1799 Remainder =
APInt(BitWidth, 0);
// X % X ===> 0;
1803 // Make sure there is enough space to hold the results.
1804 // NOTE: This assumes that reallocate won't affect any bits if it doesn't
1805 // change the size. This is necessary if Quotient or Remainder is aliased
1807 Quotient.reallocate(BitWidth);
1808 Remainder.reallocate(BitWidth);
1810 if (lhsWords == 1) {
// rhsWords is 1 if lhsWords is 1.
1811 // There is only one word to consider so use the native versions.
1814 Quotient = lhsValue / rhsValue;
1815 Remainder = lhsValue % rhsValue;
1819 // Okay, lets do it the long way
1820 divide(LHS.U.pVal, lhsWords, RHS.U.pVal, rhsWords, Quotient.U.
pVal,
1822 // Clear the rest of the Quotient and Remainder.
1823 std::memset(Quotient.U.
pVal + lhsWords, 0,
1825 std::memset(Remainder.U.
pVal + rhsWords, 0,
1831 assert(RHS != 0 &&
"Divide by zero?");
1832 unsigned BitWidth = LHS.BitWidth;
1834 // First, deal with the easy case
1835 if (LHS.isSingleWord()) {
1836 uint64_t QuotVal = LHS.U.VAL / RHS;
1837 Remainder = LHS.U.VAL % RHS;
1838 Quotient =
APInt(BitWidth, QuotVal);
1842 // Get some size facts about the dividend and divisor
1843 unsigned lhsWords =
getNumWords(LHS.getActiveBits());
1845 // Check the degenerate cases
1846 if (lhsWords == 0) {
1847 Quotient =
APInt(BitWidth, 0);
// 0 / Y ===> 0
1848 Remainder = 0;
// 0 % Y ===> 0
1853 Quotient = LHS;
// X / 1 ===> X
1854 Remainder = 0;
// X % 1 ===> 0
1859 Remainder = LHS.getZExtValue();
// X % Y ===> X, iff X < Y
1860 Quotient =
APInt(BitWidth, 0);
// X / Y ===> 0, iff X < Y
1865 Quotient =
APInt(BitWidth, 1);
// X / X ===> 1
1866 Remainder = 0;
// X % X ===> 0;
1870 // Make sure there is enough space to hold the results.
1871 // NOTE: This assumes that reallocate won't affect any bits if it doesn't
1872 // change the size. This is necessary if Quotient is aliased with LHS.
1873 Quotient.reallocate(BitWidth);
1875 if (lhsWords == 1) {
// rhsWords is 1 if lhsWords is 1.
1876 // There is only one word to consider so use the native versions.
1878 Quotient = lhsValue / RHS;
1879 Remainder = lhsValue % RHS;
1883 // Okay, lets do it the long way
1884 divide(LHS.U.pVal, lhsWords, &RHS, 1, Quotient.U.
pVal, &Remainder);
1885 // Clear the rest of the Quotient.
1886 std::memset(Quotient.U.
pVal + lhsWords, 0,
1892 if (LHS.isNegative()) {
1893 if (RHS.isNegative())
1900 }
else if (RHS.isNegative()) {
1909 APInt &Quotient, int64_t &Remainder) {
1911 if (LHS.isNegative()) {
1919 }
else if (RHS < 0) {
1929 APInt Res = *
this+RHS;
1936 APInt Res = *
this+RHS;
1937 Overflow = Res.
ult(RHS);
1942 APInt Res = *
this - RHS;
1949 APInt Res = *
this-RHS;
1950 Overflow = Res.
ugt(*
this);
1955 // MININT/-1 --> overflow.
1961 APInt Res = *
this * RHS;
1964 Overflow = Res.
sdiv(RHS) != *
this ||
1972 if (
countl_zero() + RHS.countl_zero() + 2 <= BitWidth) {
1995 return APInt(BitWidth, 0);
2002 return *
this << ShAmt;
2012 return APInt(BitWidth, 0);
2016 return *
this << ShAmt;
2021 if ((quotient * RHS != *
this) && (
isNegative() != RHS.isNegative()))
2022 return quotient - 1;
2061 return APInt(BitWidth, 0);
2070 // The result is negative if one and only one of inputs is negative.
2071 bool ResIsNegative =
isNegative() ^ RHS.isNegative();
2114 // Check our assumptions here
2116 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
2118 "Radix should be 2, 8, 10, 16, or 36!");
2121 size_t slen = str.
size();
2122 bool isNeg = *p ==
'-';
2123 if (*p ==
'-' || *p ==
'+') {
2126 assert(slen &&
"String is only a sign, needs a value.");
2128 assert((slen <= numbits || radix != 2) &&
"Insufficient bit width");
2129 assert(((slen-1)*3 <= numbits || radix != 8) &&
"Insufficient bit width");
2130 assert(((slen-1)*4 <= numbits || radix != 16) &&
"Insufficient bit width");
2131 assert((((slen-1)*64)/22 <= numbits || radix != 10) &&
2132 "Insufficient bit width");
2134 // Allocate memory if needed
2140 // Figure out if we can shift instead of multiply
2141 unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
2143 // Enter digit traversal loop
2145 unsigned digit =
getDigit(*p, radix);
2146 assert(digit < radix &&
"Invalid character in digit string");
2148 // Shift or multiply the value by the radix
2156 // Add in the digit we just interpreted
2159 // If its negative, put it in two's complement form
2165 bool formatAsCLiteral,
bool UpperCase,
2166 bool InsertSeparators)
const {
2167 assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
2169 "Radix should be 2, 8, 10, 16, or 36!");
2171 const char *Prefix =
"";
2172 if (formatAsCLiteral) {
2175 // Binary literals are a non-standard extension added in gcc 4.3:
2176 // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Binary-constants.html
2192 // Number of digits in a group between separators.
2193 unsigned Grouping = (Radix == 8 || Radix == 10) ? 3 : 4;
2195 // First, check for a zero value and just short circuit the logic below.
2198 Str.push_back(*Prefix);
2205 static const char BothDigits[] =
"0123456789abcdefghijklmnopqrstuvwxyz"
2206 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2207 const char *Digits = BothDigits + (UpperCase ? 36 : 0);
2211 char *BufPtr = std::end(Buffer);
2227 Str.push_back(*Prefix);
2233 if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
2235 *--BufPtr = Digits[
N % Radix];
2239 Str.append(BufPtr, std::end(Buffer));
2246 // They want to print the signed version and it is a negative value
2247 // Flip the bits and add one to turn it into the equivalent positive
2248 // value and put a '-' in the result.
2254 Str.push_back(*Prefix);
2258 // We insert the digits backward, then reverse them to get the right order.
2259 unsigned StartDig = Str.size();
2261 // For the 2, 8 and 16 bit cases, we can just shift instead of divide
2262 // because the number of bits per digit (1, 3 and 4 respectively) divides
2263 // equally. We just shift until the value is zero.
2264 if (Radix == 2 || Radix == 8 || Radix == 16) {
2265 // Just shift tmp right for each digit width until it becomes zero
2266 unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
2267 unsigned MaskAmt = Radix - 1;
2272 if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
2273 Str.push_back(
'\'');
2275 Str.push_back(Digits[Digit]);
2283 udivrem(Tmp, Radix, Tmp, Digit);
2284 assert(Digit < Radix &&
"divide failed");
2285 if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
2286 Str.push_back(
'\'');
2288 Str.push_back(Digits[Digit]);
2293 // Reverse the digits before returning.
2294 std::reverse(Str.begin()+StartDig, Str.end());
2297#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2302 dbgs() <<
"APInt(" << BitWidth <<
"b, "
2303 << U <<
"u " << S <<
"s)\n";
2313// This implements a variety of operations on a representation of
2314// arbitrary precision, two's-complement, bignum integer values.
2316// Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe
2317// and unrestricting assumption.
2319 "Part width must be divisible by 2!");
2321// Returns the integer part with the least significant BITS set.
2322// BITS cannot be zero.
2328/// Returns the value of the lower half of PART.
2333/// Returns the value of the upper half of PART.
2338/// Sets the least significant part of a bignum to the input value, and zeroes
2339/// out higher parts.
2343 for (
unsigned i = 1; i < parts; i++)
2347/// Assign one bignum to another.
2349 for (
unsigned i = 0; i < parts; i++)
2353/// Returns true if a bignum is zero, false otherwise.
2355 for (
unsigned i = 0; i < parts; i++)
2362/// Extract the given bit of a bignum; returns 0 or 1.
2364 return (parts[whichWord(bit)] & maskBit(bit)) != 0;
2367/// Set the given bit of a bignum.
2369 parts[whichWord(bit)] |= maskBit(bit);
2372/// Clears the given bit of a bignum.
2374 parts[whichWord(bit)] &= ~maskBit(bit);
2377/// Returns the bit number of the least significant set bit of a number. If the
2378/// input number has no bits set UINT_MAX is returned.
2380 for (
unsigned i = 0; i < n; i++) {
2381 if (parts[i] != 0) {
2390/// Returns the bit number of the most significant set bit of a number.
2391/// If the input number has no bits set UINT_MAX is returned.
2396 if (parts[n] != 0) {
2397 static_assert(
sizeof(parts[n]) <=
sizeof(
uint64_t));
2407/// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to
2408/// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least
2409/// significant bit of DST. All high bits above srcBITS in DST are zero-filled.
2413 unsigned srcBits,
unsigned srcLSB) {
2415 assert(dstParts <= dstCount);
2418 tcAssign(dst, src + firstSrcPart, dstParts);
2423 // We now have (dstParts * APINT_BITS_PER_WORD - shift) bits from SRC
2424 // in DST. If this is less that srcBits, append the rest, else
2425 // clear the high bits.
2429 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] &
mask)
2431 }
else if (n > srcBits) {
2436 // Clear high parts.
2437 while (dstParts < dstCount)
2438 dst[dstParts++] = 0;
2441//// DST += RHS + C where C is zero or one. Returns the carry flag.
2446 for (
unsigned i = 0; i < parts; i++) {
2449 dst[i] += rhs[i] + 1;
2460/// This function adds a single "word" integer, src, to the multiple
2461/// "word" integer array, dst[]. dst[] is modified to reflect the addition and
2462/// 1 is returned if there is a carry out, otherwise 0 is returned.
2463/// @returns the carry of the addition.
2466 for (
unsigned i = 0; i < parts; ++i) {
2469 return 0;
// No need to carry so exit early.
2470 src = 1;
// Carry one to next digit.
2476/// DST -= RHS + C where C is zero or one. Returns the carry flag.
2481 for (
unsigned i = 0; i < parts; i++) {
2484 dst[i] -= rhs[i] + 1;
2495/// This function subtracts a single "word" (64-bit word), src, from
2496/// the multi-word integer array, dst[], propagating the borrowed 1 value until
2497/// no further borrowing is needed or it runs out of "words" in dst. The result
2498/// is 1 if "borrowing" exhausted the digits in dst, or 0 if dst was not
2499/// exhausted. In other words, if src > dst then this function returns 1,
2501/// @returns the borrow out of the subtraction
2504 for (
unsigned i = 0; i < parts; ++i) {
2508 return 0;
// No need to borrow so exit early.
2509 src = 1;
// We have to "borrow 1" from next "word"
2515/// Negate a bignum in-place.
2521/// DST += SRC * MULTIPLIER + CARRY if add is true
2522/// DST = SRC * MULTIPLIER + CARRY if add is false
2523/// Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC
2524/// they must start at the same point, i.e. DST == SRC.
2525/// If DSTPARTS == SRCPARTS + 1 no overflow occurs and zero is
2526/// returned. Otherwise DST is filled with the least significant
2527/// DSTPARTS parts of the result, and if all of the omitted higher
2528/// parts were zero return zero, otherwise overflow occurred and
2532 unsigned srcParts,
unsigned dstParts,
2534 // Otherwise our writes of DST kill our later reads of SRC.
2535 assert(dst <= src || dst >= src + srcParts);
2536 assert(dstParts <= srcParts + 1);
2538 // N loops; minimum of dstParts and srcParts.
2539 unsigned n = std::min(dstParts, srcParts);
2541 for (
unsigned i = 0; i < n; i++) {
2542 // [LOW, HIGH] = MULTIPLIER * SRC[i] + DST[i] + CARRY.
2543 // This cannot overflow, because:
2544 // (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1)
2545 // which is less than n^2.
2548 if (multiplier == 0 || srcPart == 0) {
2558 if (low + mid < low)
2565 if (low + mid < low)
2570 if (low + carry < low)
2576 // And now DST[i], and store the new low part there.
2577 if (low + dst[i] < low)
2587 if (srcParts < dstParts) {
2588 // Full multiplication, there is no overflow.
2589 assert(srcParts + 1 == dstParts);
2590 dst[srcParts] = carry;
2594 // We overflowed if there is carry.
2598 // We would overflow if any significant unwritten parts would be
2599 // non-zero. This is true if any remaining src parts are non-zero
2600 // and the multiplier is non-zero.
2602 for (
unsigned i = dstParts; i < srcParts; i++)
2606 // We fitted in the narrow destination.
2610/// DST = LHS * RHS, where DST has the same width as the operands and
2611/// is filled with the least significant parts of the result. Returns
2612/// one if overflow occurred, otherwise zero. DST must be disjoint
2613/// from both operands.
2615 const WordType *rhs,
unsigned parts) {
2616 assert(dst != lhs && dst != rhs);
2620 for (
unsigned i = 0; i < parts; i++) {
2621 // Don't accumulate on the first iteration so we don't need to initalize
2624 tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts, parts - i, i != 0);
2630/// DST = LHS * RHS, where DST has width the sum of the widths of the
2631/// operands. No overflow occurs. DST must be disjoint from both operands.
2633 const WordType *rhs,
unsigned lhsParts,
2634 unsigned rhsParts) {
2635 // Put the narrower number on the LHS for less loops below.
2636 if (lhsParts > rhsParts)
2639 assert(dst != lhs && dst != rhs);
2641 for (
unsigned i = 0; i < lhsParts; i++) {
2642 // Don't accumulate on the first iteration so we don't need to initalize
2644 tcMultiplyPart(&dst[i], rhs, lhs[i], 0, rhsParts, rhsParts + 1, i != 0);
2648// If RHS is zero LHS and REMAINDER are left unchanged, return one.
2649// Otherwise set LHS to LHS / RHS with the fractional part discarded,
2650// set REMAINDER to the remainder, return zero. i.e.
2652// OLD_LHS = RHS * LHS + REMAINDER
2654// SCRATCH is a bignum of the same size as the operands and result for
2655// use by the routine; its contents need not be initialized and are
2656// destroyed. LHS, REMAINDER and SCRATCH must be distinct.
2660 assert(lhs != remainder && lhs != srhs && remainder != srhs);
2662 unsigned shiftCount =
tcMSB(rhs, parts) + 1;
2663 if (shiftCount == 0)
2673 tcSet(lhs, 0, parts);
2675 // Loop, subtracting SRHS if REMAINDER is greater and adding that to the
2678 int compare =
tcCompare(remainder, srhs, parts);
2684 if (shiftCount == 0)
2688 if ((
mask >>= 1) == 0) {
2697/// Shift a bignum left Count bits in-place. Shifted in bits are zero. There are
2698/// no restrictions on Count.
2700 // Don't bother performing a no-op shift.
2704 // WordShift is the inter-part shift; BitShift is the intra-part shift.
2708 // Fastpath for moving by whole words.
2709 if (BitShift == 0) {
2710 std::memmove(Dst + WordShift, Dst, (Words - WordShift) *
APINT_WORD_SIZE);
2712 while (Words-- > WordShift) {
2713 Dst[Words] = Dst[Words - WordShift] << BitShift;
2714 if (Words > WordShift)
2720 // Fill in the remainder with 0s.
2724/// Shift a bignum right Count bits in-place. Shifted in bits are zero. There
2725/// are no restrictions on Count.
2727 // Don't bother performing a no-op shift.
2731 // WordShift is the inter-part shift; BitShift is the intra-part shift.
2735 unsigned WordsToMove = Words - WordShift;
2736 // Fastpath for moving by whole words.
2737 if (BitShift == 0) {
2740 for (
unsigned i = 0; i != WordsToMove; ++i) {
2741 Dst[i] = Dst[i + WordShift] >> BitShift;
2742 if (i + 1 != WordsToMove)
2747 // Fill in the remainder with 0s.
2751// Comparison (unsigned) of two bignums.
2756 if (lhs[parts] != rhs[parts])
2757 return (lhs[parts] > rhs[parts]) ? 1 : -1;
2765 // Currently udivrem always rounds down.
2790 // This algorithm deals with arbitrary rounding mode used by sdivrem.
2791 // We want to check whether the non-integer part of the mathematical value
2792 // is negative or not. If the non-integer part is negative, we need to round
2793 // down from Quo; otherwise, if it's positive or 0, we return Quo, as it's
2794 // already rounded down.
2804 // Currently sdiv rounds towards zero.
2813 unsigned RangeWidth) {
2814 unsigned CoeffWidth =
A.getBitWidth();
2815 assert(CoeffWidth ==
B.getBitWidth() && CoeffWidth ==
C.getBitWidth());
2816 assert(RangeWidth <= CoeffWidth &&
2817 "Value range width should be less than coefficient width");
2818 assert(RangeWidth > 1 &&
"Value range bit width should be > 1");
2821 <<
"x + " <<
C <<
", rw:" << RangeWidth <<
'\n');
2823 // Identify 0 as a (non)solution immediately.
2824 if (
C.sextOrTrunc(RangeWidth).isZero()) {
2826 return APInt(CoeffWidth, 0);
2829 // The result of APInt arithmetic has the same bit width as the operands,
2830 // so it can actually lose high bits. A product of two n-bit integers needs
2831 // 2n-1 bits to represent the full value.
2832 // The operation done below (on quadratic coefficients) that can produce
2833 // the largest value is the evaluation of the equation during bisection,
2834 // which needs 3 times the bitwidth of the coefficient, so the total number
2835 // of required bits is 3n.
2837 // The purpose of this extension is to simulate the set Z of all integers,
2838 // where n+1 > n for all n in Z. In Z it makes sense to talk about positive
2839 // and negative numbers (not so much in a modulo arithmetic). The method
2840 // used to solve the equation is based on the standard formula for real
2841 // numbers, and uses the concepts of "positive" and "negative" with their
2844 A =
A.sext(CoeffWidth);
2845 B =
B.sext(CoeffWidth);
2846 C =
C.sext(CoeffWidth);
2848 // Make A > 0 for simplicity. Negate cannot overflow at this point because
2849 // the bit width has increased.
2850 if (
A.isNegative()) {
2856 // Solving an equation q(x) = 0 with coefficients in modular arithmetic
2857 // is really solving a set of equations q(x) = kR for k = 0, 1, 2, ...,
2858 // and R = 2^BitWidth.
2859 // Since we're trying not only to find exact solutions, but also values
2860 // that "wrap around", such a set will always have a solution, i.e. an x
2861 // that satisfies at least one of the equations, or such that |q(x)|
2862 // exceeds kR, while |q(x-1)| for the same k does not.
2864 // We need to find a value k, such that Ax^2 + Bx + C = kR will have a
2865 // positive solution n (in the above sense), and also such that the n
2866 // will be the least among all solutions corresponding to k = 0, 1, ...
2867 // (more precisely, the least element in the set
2868 // { n(k) | k is such that a solution n(k) exists }).
2870 // Consider the parabola (over real numbers) that corresponds to the
2871 // quadratic equation. Since A > 0, the arms of the parabola will point
2872 // up. Picking different values of k will shift it up and down by R.
2874 // We want to shift the parabola in such a way as to reduce the problem
2875 // of solving q(x) = kR to solving shifted_q(x) = 0.
2876 // (The interesting solutions are the ceilings of the real number
2884 assert(
A.isStrictlyPositive());
2888 return V.isNegative() ? V+
T : V+(
A-
T);
2891 // The vertex of the parabola is at -B/2A, but since A > 0, it's negative
2892 // iff B is positive.
2893 if (
B.isNonNegative()) {
2894 // If B >= 0, the vertex it at a negative location (or at 0), so in
2895 // order to have a non-negative solution we need to pick k that makes
2896 // C-kR negative. To satisfy all the requirements for the solution
2897 // that we are looking for, it needs to be closest to 0 of all k.
2899 if (
C.isStrictlyPositive())
2901 // Pick the greater solution.
2904 // If B < 0, the vertex is at a positive location. For any solution
2905 // to exist, the discriminant must be non-negative. This means that
2906 // C-kR <= B^2/4A is a necessary condition for k, i.e. there is a
2907 // lower bound on values of k: kR >= C - B^2/4A.
2908 APInt LowkR =
C - SqrB.
udiv(2*TwoA);
// udiv because all values > 0.
2909 // Round LowkR up (towards +inf) to the nearest kR.
2910 LowkR = RoundUp(LowkR, R);
2912 // If there exists k meeting the condition above, and such that
2913 // C-kR > 0, there will be two positive real number solutions of
2914 // q(x) = kR. Out of all such values of k, pick the one that makes
2915 // C-kR closest to 0, (i.e. pick maximum k such that C-kR > 0).
2916 // In other words, find maximum k such that LowkR <= kR < C.
2918 // If LowkR < C, then such a k is guaranteed to exist because
2919 // LowkR itself is a multiple of R.
2920 C -= -RoundUp(-
C, R);
// C = C - RoundDown(C, R)
2921 // Pick the smaller solution.
2924 // If C-kR < 0 for all potential k's, it means that one solution
2925 // will be negative, while the other will be positive. The positive
2926 // solution will shift towards 0 if the parabola is moved up.
2927 // Pick the kR closest to the lower bound (i.e. make C-kR closest
2928 // to 0, or in other words, out of all parabolas that have solutions,
2929 // pick the one that is the farthest "up").
2930 // Since LowkR is itself a multiple of R, simply take C-LowkR.
2932 // Pick the greater solution.
2937 LLVM_DEBUG(
dbgs() << __func__ <<
": updated coefficients " <<
A <<
"x^2 + "
2938 <<
B <<
"x + " <<
C <<
", rw:" << RangeWidth <<
'\n');
2941 assert(
D.isNonNegative() &&
"Negative discriminant");
2945 bool InexactSQ = Q !=
D;
2946 // The calculated SQ may actually be greater than the exact (non-integer)
2947 // value. If that's the case, decrement SQ to get a value that is lower.
2954 // SQ is rounded down (i.e SQ * SQ <= D), so the roots may be inexact.
2955 // When using the quadratic formula directly, the calculated low root
2956 // may be greater than the exact one, since we would be subtracting SQ.
2957 // To make sure that the calculated root is not greater than the exact
2958 // one, subtract SQ+1 when calculating the low root (for inexact value
2965 // The updated coefficients should be such that the (exact) solution is
2966 // positive. Since APInt division rounds towards 0, the calculated one
2967 // can be 0, but cannot be negative.
2968 assert(
X.isNonNegative() &&
"Solution should be non-negative");
2970 if (!InexactSQ && Rem.
isZero()) {
2975 assert((SQ*SQ).sle(
D) &&
"SQ = |_sqrt(D)_|, so SQ*SQ <= D");
2976 // The exact value of the square root of D should be between SQ and SQ+1.
2977 // This implies that the solution should be between that corresponding to
2978 // SQ (i.e. X) and that corresponding to SQ+1.
2980 // The calculated X cannot be greater than the exact (real) solution.
2981 // Actually it must be strictly less than the exact solution, while
2982 // X+1 will be greater than or equal to it.
2988 // If the sign did not change between X and X+1, X is not a valid solution.
2989 // This could happen when the actual (exact) roots don't have an integer
2990 // between them, so they would both be contained between X and X+1.
2993 return std::nullopt;
3001std::optional<unsigned>
3003 assert(
A.getBitWidth() ==
B.getBitWidth() &&
"Must have the same bitwidth");
3005 return std::nullopt;
3006 return A.getBitWidth() - ((
A ^
B).countl_zero() + 1);
3010 bool MatchAllBits) {
3011 unsigned OldBitWidth =
A.getBitWidth();
3012 assert((((OldBitWidth % NewBitWidth) == 0) ||
3013 ((NewBitWidth % OldBitWidth) == 0)) &&
3014 "One size should be a multiple of the other one. "
3015 "Can't do fractional scaling.");
3017 // Check for matching bitwidths.
3018 if (OldBitWidth == NewBitWidth)
3023 // Check for null input.
3027 if (NewBitWidth > OldBitWidth) {
3029 unsigned Scale = NewBitWidth / OldBitWidth;
3030 for (
unsigned i = 0; i != OldBitWidth; ++i)
3032 NewA.
setBits(i * Scale, (i + 1) * Scale);
3034 unsigned Scale = OldBitWidth / NewBitWidth;
3035 for (
unsigned i = 0; i != NewBitWidth; ++i) {
3037 if (
A.extractBits(Scale, i * Scale).isAllOnes())
3040 if (!
A.extractBits(Scale, i * Scale).isZero())
3049/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
3050/// with the integer held in IntVal.
3052 unsigned StoreBytes) {
3053 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes &&
"Integer too small!");
3057 // Little-endian host - the source is ordered from LSB to MSB. Order the
3058 // destination from LSB to MSB: Do a straight copy.
3059 memcpy(Dst, Src, StoreBytes);
3061 // Big-endian host - the source is an array of 64 bit words ordered from
3062 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
3063 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
3064 while (StoreBytes >
sizeof(
uint64_t)) {
3066 // May not be aligned so use memcpy.
3067 memcpy(Dst + StoreBytes, Src,
sizeof(
uint64_t));
3071 memcpy(Dst, Src +
sizeof(
uint64_t) - StoreBytes, StoreBytes);
3075/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
3076/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
3078 unsigned LoadBytes) {
3079 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes &&
"Integer too small!");
3081 const_cast<uint64_t *
>(IntVal.getRawData()));
3084 // Little-endian host - the destination must be ordered from LSB to MSB.
3085 // The source is ordered from LSB to MSB: Do a straight copy.
3086 memcpy(Dst, Src, LoadBytes);
3088 // Big-endian - the destination is an array of 64 bit words ordered from
3089 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
3090 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
3092 while (LoadBytes >
sizeof(
uint64_t)) {
3094 // May not be aligned so use memcpy.
3095 memcpy(Dst, Src + LoadBytes,
sizeof(
uint64_t));
3099 memcpy(Dst +
sizeof(
uint64_t) - LoadBytes, Src, LoadBytes);
3104 // Return floor((C1 + C2) / 2)
3105 return (C1 & C2) + (C1 ^ C2).ashr(1);
3109 // Return floor((C1 + C2) / 2)
3110 return (C1 & C2) + (C1 ^ C2).lshr(1);
3114 // Return ceil((C1 + C2) / 2)
3115 return (C1 | C2) - (C1 ^ C2).ashr(1);
3119 // Return ceil((C1 + C2) / 2)
3120 return (C1 | C2) - (C1 ^ C2).lshr(1);
3144 return C1Ext * C2Ext;
3152 return C1Ext * C2Ext;
3156 assert(
N >= 0 &&
"negative exponents not supported.");
3161 int64_t RemainingExponent =
N;
3162 while (RemainingExponent > 0) {
3163 while (RemainingExponent % 2 == 0) {
3165 RemainingExponent /= 2;
3167 --RemainingExponent;
3174 const APInt &Shift) {
3175 assert(
Hi.getBitWidth() ==
Lo.getBitWidth());
3179 return Hi.shl(ShiftAmt) |
Lo.lshr(
Hi.getBitWidth() - ShiftAmt);
3183 const APInt &Shift) {
3184 assert(
Hi.getBitWidth() ==
Lo.getBitWidth());
3188 return Hi.shl(
Hi.getBitWidth() - ShiftAmt) |
Lo.lshr(ShiftAmt);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static APInt::WordType lowHalf(APInt::WordType part)
Returns the value of the lower half of PART.
static unsigned rotateModulo(unsigned BitWidth, const APInt &rotateAmt)
static APInt::WordType highHalf(APInt::WordType part)
Returns the value of the upper half of PART.
static void tcComplement(APInt::WordType *dst, unsigned parts)
static unsigned getDigit(char cdigit, uint8_t radix)
A utility function that converts a character to a digit.
static APInt::WordType lowBitMask(unsigned bits)
static uint64_t * getMemory(unsigned numWords)
A utility function for allocating memory and checking for allocation failure.
static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t *r, unsigned m, unsigned n)
Implementation of Knuth's Algorithm D (Division of nonnegative integers) from "Art of Computer Progra...
static uint64_t * getClearedMemory(unsigned numWords)
A utility function for allocating memory, checking for allocation failures, and ensuring the contents...
This file implements a class to represent arbitrary precision integral constant values and operations...
static constexpr unsigned long long mask(BlockVerifier::State S)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_UNLIKELY(EXPR)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
static bool isNeg(Value *V)
Returns true if the operation is a negation of V, and it works for both integers and floats.
static bool isSigned(unsigned int Opcode)
This file defines a hash set that can be used to remove duplication of nodes in a graph.
static uint64_t clearUnusedBits(uint64_t Val, unsigned Size)
This file defines the SmallString class.
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
This file implements the C++20 <bit> header.
Class for arbitrary precision integers.
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
LLVM_ABI APInt usub_sat(const APInt &RHS) const
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
static LLVM_ABI void tcSetBit(WordType *, unsigned bit)
Set the given bit of a bignum. Zero-based.
static LLVM_ABI void tcSet(WordType *, WordType, unsigned)
Sets the least significant part of a bignum to the input value, and zeroes out higher parts.
LLVM_ABI unsigned nearestLogBase2() const
static LLVM_ABI void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
LLVM_ABI APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
static LLVM_ABI int tcExtractBit(const WordType *, unsigned bit)
Extract the given bit of a bignum; returns 0 or 1. Zero-based.
LLVM_ABI bool isAligned(Align A) const
Checks if this APInt -interpreted as an address- is aligned to the provided value.
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
uint64_t getZExtValue() const
Get zero extended value.
LLVM_ABI APInt truncUSat(unsigned width) const
Truncate to new width with unsigned saturation.
uint64_t * pVal
Used to store the >64 bits integer value.
static LLVM_ABI void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
static LLVM_ABI WordType tcAdd(WordType *, const WordType *, WordType carry, unsigned)
DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
static LLVM_ABI void tcExtract(WordType *, unsigned dstCount, const WordType *, unsigned srcBits, unsigned srcLSB)
Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to DST, of dstCOUNT parts,...
LLVM_ABI uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
unsigned getActiveBits() const
Compute the number of active bits in the value.
static LLVM_ABI unsigned getSufficientBitsNeeded(StringRef Str, uint8_t Radix)
Get the bits that are sufficient to represent the string value.
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
void toStringUnsigned(SmallVectorImpl< char > &Str, unsigned Radix=10) const
Considers the APInt to be unsigned and converts it into a string in the radix given.
LLVM_ABI APInt sshl_ov(const APInt &Amt, bool &Overflow) const
LLVM_ABI APInt smul_sat(const APInt &RHS) const
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
bool sgt(const APInt &RHS) const
Signed greater than comparison.
static LLVM_ABI int tcCompare(const WordType *, const WordType *, unsigned)
Comparison (unsigned) of two bignums.
LLVM_ABI APInt & operator++()
Prefix increment operator.
LLVM_ABI APInt usub_ov(const APInt &RHS, bool &Overflow) const
APInt(unsigned numBits, uint64_t val, bool isSigned=false, bool implicitTrunc=false)
Create a new APInt of numBits width, initialized as val.
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
LLVM_ABI void print(raw_ostream &OS, bool isSigned) const
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
static LLVM_ABI void tcAssign(WordType *, const WordType *, unsigned)
Assign one bignum to another.
static constexpr unsigned APINT_WORD_SIZE
Byte size of a word.
unsigned getBitWidth() const
Return the number of bits in the APInt.
static LLVM_ABI void tcShiftRight(WordType *, unsigned Words, unsigned Count)
Shift a bignum right Count bits.
static LLVM_ABI void tcFullMultiply(WordType *, const WordType *, const WordType *, unsigned, unsigned)
DST = LHS * RHS, where DST has width the sum of the widths of the operands.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
LLVM_ABI APInt sfloordiv_ov(const APInt &RHS, bool &Overflow) const
Signed integer floor division operation.
bool isSingleWord() const
Determine if this APInt just has one word to store value.
unsigned getNumWords() const
Get the number of words.
APInt()
Default constructor that creates an APInt with a 1-bit zero value.
bool isNegative() const
Determine sign of this APInt.
LLVM_ABI APInt sadd_ov(const APInt &RHS, bool &Overflow) const
APInt & operator<<=(unsigned ShiftAmt)
Left-shift assignment function.
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
double roundToDouble() const
Converts this unsigned APInt to a double value.
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
LLVM_ABI APInt reverseBits() const
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
LLVM_ABI APInt uadd_ov(const APInt &RHS, bool &Overflow) const
static LLVM_ABI void tcClearBit(WordType *, unsigned bit)
Clear the given bit of a bignum. Zero-based.
void negate()
Negate this APInt in place.
static WordType tcDecrement(WordType *dst, unsigned parts)
Decrement a bignum in-place. Return the borrow flag.
unsigned countr_zero() const
Count the number of trailing zero bits.
LLVM_ABI bool isSplat(unsigned SplatSizeInBits) const
Check if the APInt consists of a repeated bit pattern.
LLVM_ABI APInt & operator-=(const APInt &RHS)
Subtraction assignment operator.
bool isSignedIntN(unsigned N) const
Check if this APInt has an N-bits signed integer value.
LLVM_ABI APInt sdiv_ov(const APInt &RHS, bool &Overflow) const
LLVM_ABI APInt operator*(const APInt &RHS) const
Multiplication operator.
static LLVM_ABI unsigned tcLSB(const WordType *, unsigned n)
Returns the bit number of the least or most significant set bit of a number.
unsigned countl_zero() const
The APInt version of std::countl_zero.
static LLVM_ABI void tcShiftLeft(WordType *, unsigned Words, unsigned Count)
Shift a bignum left Count bits.
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
static constexpr WordType WORDTYPE_MAX
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
LLVM_ABI APInt ushl_ov(const APInt &Amt, bool &Overflow) const
static LLVM_ABI WordType tcSubtractPart(WordType *, WordType, unsigned)
DST -= RHS. Returns the carry flag.
static LLVM_ABI bool tcIsZero(const WordType *, unsigned)
Returns true if a bignum is zero, false otherwise.
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
static LLVM_ABI unsigned tcMSB(const WordType *parts, unsigned n)
Returns the bit number of the most significant set bit of a number.
static LLVM_ABI int tcDivide(WordType *lhs, const WordType *rhs, WordType *remainder, WordType *scratch, unsigned parts)
If RHS is zero LHS and REMAINDER are left unchanged, return one.
LLVM_DUMP_METHOD void dump() const
debug method
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
unsigned countl_one() const
Count the number of leading one bits.
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
unsigned logBase2() const
static LLVM_ABI int tcMultiplyPart(WordType *dst, const WordType *src, WordType multiplier, WordType carry, unsigned srcParts, unsigned dstParts, bool add)
DST += SRC * MULTIPLIER + PART if add is true DST = SRC * MULTIPLIER + PART if add is false.
static constexpr unsigned APINT_BITS_PER_WORD
Bits in a word.
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
static LLVM_ABI int tcMultiply(WordType *, const WordType *, const WordType *, unsigned)
DST = LHS * RHS, where DST has the same width as the operands and is filled with the least significan...
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
LLVM_ABI APInt & operator*=(const APInt &RHS)
Multiplication assignment operator.
uint64_t VAL
Used to store the <= 64 bits integer value.
static LLVM_ABI unsigned getBitsNeeded(StringRef str, uint8_t radix)
Get bits required for string value.
static LLVM_ABI WordType tcSubtract(WordType *, const WordType *, WordType carry, unsigned)
DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
LLVM_ABI APInt multiplicativeInverse() const
static LLVM_ABI void tcNegate(WordType *, unsigned)
Negate a bignum in-place.
bool getBoolValue() const
Convert APInt to a boolean value.
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
LLVM_ABI APInt smul_ov(const APInt &RHS, bool &Overflow) const
static WordType tcIncrement(WordType *dst, unsigned parts)
Increment a bignum in-place. Return the carry flag.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
APInt shl(unsigned shiftAmt) const
Left-shift function.
LLVM_ABI APInt byteSwap() const
LLVM_ABI APInt umul_sat(const APInt &RHS) const
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
LLVM_ABI APInt & operator+=(const APInt &RHS)
Addition assignment operator.
LLVM_ABI void flipBit(unsigned bitPosition)
Toggles a given bit to its opposite value.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
static LLVM_ABI WordType tcAddPart(WordType *, WordType, unsigned)
DST += RHS. Returns the carry flag.
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
LLVM_ABI void Profile(FoldingSetNodeID &id) const
Used to insert APInt objects, or objects that contain APInt objects, into FoldingSets.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
LLVM_ABI APInt ssub_ov(const APInt &RHS, bool &Overflow) const
LLVM_ABI APInt & operator--()
Prefix decrement operator.
bool isOne() const
Determine if this is a value of 1.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
int64_t getSExtValue() const
Get sign extended value.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
LLVM_ABI APInt sqrt() const
Compute the square root.
void setBitVal(unsigned BitPosition, bool BitValue)
Set a given bit to a given value.
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
void toStringSigned(SmallVectorImpl< char > &Str, unsigned Radix=10) const
Considers the APInt to be signed and converts it into a string in the radix given.
LLVM_ABI APInt truncSSat(unsigned width) const
Truncate to new width with signed saturation.
LLVM_ABI void toString(SmallVectorImpl< char > &Str, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false) const
Converts an APInt to a string and append it to Str.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringRef - Represent a constant reference to a string, i.e.
constexpr bool empty() const
empty - Check if the string is empty.
constexpr size_t size() const
size - Get the string size.
An opaque object representing a hash code.
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI std::optional< unsigned > GetMostSignificantDifferentBit(const APInt &A, const APInt &B)
Compare two values, and if they are different, return the position of the most significant bit that i...
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
LLVM_ABI APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A unsign-divided by B, rounded by the given rounding mode.
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
LLVM_ABI APInt muluExtended(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
LLVM_ABI APInt mulsExtended(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
LLVM_ABI APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A sign-divided by B, rounded by the given rounding mode.
LLVM_ABI APInt pow(const APInt &X, int64_t N)
Compute X^N for N>=0.
LLVM_ABI APInt RoundDoubleToAPInt(double Double, unsigned width)
Converts the given double value into a APInt.
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
LLVM_ABI std::optional< APInt > SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, unsigned RangeWidth)
Let q(n) = An^2 + Bn + C, and BW = bit width of the value range (e.g.
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
support::ulittle32_t Word
constexpr bool IsLittleEndianHost
This is an optimization pass for GlobalISel generic memory operations.
hash_code hash_value(const FixedPointSemantics &Val)
LLVM_ABI void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes)
StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst with the integer held in In...
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionAddr VTableAddr Count
int countl_one(T Value)
Count the number of ones from the most significant bit to the first zero bit.
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
@ Mod
The access may modify the value stored in memory.
To bit_cast(const From &from) noexcept
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned BitWidth
constexpr T reverseBits(T Val)
Reverse the bits in Val.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
unsigned Log2(Align A)
Returns the log2 of the alignment.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
constexpr uint64_t Make_64(uint32_t High, uint32_t Low)
Make a 64-bit integer from a high / low pair of 32-bit integers.
LLVM_ABI void LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, unsigned LoadBytes)
LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting from Src into IntVal,...
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
This struct is a compact representation of a valid (non-zero power of two) alignment.
An information struct used to provide DenseMap with the various necessary components for a given valu...
static uint64_t round(uint64_t Acc, uint64_t Input)