Lines Matching +full:mid +full:- +full:scale

1 //===-- APInt.cpp - Implement APInt class ---------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
21 #include "llvm/Config/llvm-config.h"
53 r = cdigit - '0'; in getDigit()
57 r = cdigit - 'A'; in getDigit()
58 if (r <= radix - 11U) in getDigit()
61 r = cdigit - 'a'; in getDigit()
62 if (r <= radix - 11U) in getDigit()
68 r = cdigit - '0'; in getDigit()
186 APInt& APInt::operator--() { in operator --()
188 --U.VAL; in operator --()
217 APInt& APInt::operator-=(const APInt& RHS) { in operator -=()
220 U.VAL -= RHS.U.VAL; in operator -=()
226 APInt& APInt::operator-=(uint64_t RHS) { in operator -=()
228 U.VAL -= RHS; in operator -=()
285 return U.VAL < RHS.U.VAL ? -1 : U.VAL > RHS.U.VAL; in compare()
295 return lhsSext < rhsSext ? -1 : lhsSext > rhsSext; in compareSigned()
303 return lhsNeg ? -1 : 1; in compareSigned()
306 // numbers compare correctly this way if both have the same signed-ness. in compareSigned()
321 uint64_t hiMask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - hiShiftAmt); in setBitsSlowCase()
337 // Complement a bignum in-place.
351 /// (this->zext(NewWidth) << NewLSB.getBitWidth()) | NewLSB.zext(NewWidth)
364 assert(bitPosition < BitWidth && "Out of the bit-width range!"); in flipBit()
384 uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - subBitWidth); in insertBits()
392 unsigned hi1Word = whichWord(bitPosition + subBitWidth - 1); in insertBits()
396 uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - subBitWidth); in insertBits()
412 uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - remainingBits); in insertBits()
414 U.pVal[hi1Word] |= subBits.getWord(subBitWidth - 1); in insertBits()
419 // General case - set/clear individual bits in dst based on src. in insertBits()
420 // TODO - there is scope for optimization here, but at the moment this code in insertBits()
437 unsigned hiWord = whichWord(bitPosition + numBits - 1); in insertBits()
449 U.pVal[hiWord] &= ~(maskBits >> (wordBits - loBit)); in insertBits()
450 U.pVal[hiWord] |= subBits >> (wordBits - loBit); in insertBits()
462 unsigned hiWord = whichWord(bitPosition + numBits - 1); in extractBits()
471 return APInt(numBits, ArrayRef(U.pVal + loWord, 1 + hiWord - loWord)); in extractBits()
473 // General case - shift + copy source words directly into place. in extractBits()
483 DestPtr[word] = (w0 >> loBit) | (w1 << (APINT_BITS_PER_WORD - loBit)); in extractBits()
501 unsigned hiWord = whichWord(bitPosition + numBits - 1); in extractBitsAsZExtValue()
508 retBits |= U.pVal[hiWord] << (wordBits - loBit); in extractBitsAsZExtValue()
519 if (Str[0] == '-' || Str[0] == '+') { in getSufficientBitsNeeded()
520 IsNegative = Str[0] == '-'; in getSufficientBitsNeeded()
521 StrLen--; in getSufficientBitsNeeded()
525 // For radixes of power-of-two values, the bits required is accurately and in getSufficientBitsNeeded()
536 // calculation doesn't work appropriately for the numbers 0-9, so just use 4 in getSufficientBitsNeeded()
562 unsigned isNegative = *p == '-'; in getBitsNeeded()
563 if (*p == '-' || *p == '+') { in getBitsNeeded()
565 slen--; in getBitsNeeded()
577 if (log == (unsigned)-1) { in getBitsNeeded()
609 return this->lshr(BitWidth - numBits); in getHiBits()
632 for (int i = getNumWords()-1; i >= 0; --i) { in countLeadingZerosSlowCase()
643 Count -= Mod > 0 ? APINT_BITS_PER_WORD - Mod : 0; in countLeadingZerosSlowCase()
654 shift = APINT_BITS_PER_WORD - highWordBits; in countLeadingOnesSlowCase()
656 int i = getNumWords() - 1; in countLeadingOnesSlowCase()
659 for (i--; i >= 0; --i) { in countLeadingOnesSlowCase()
723 Tmp1 >>= (64 - BitWidth); in byteSwap()
729 Result.U.pVal[I] = llvm::byteswap<uint64_t>(U.pVal[N - I - 1]); in byteSwap()
731 Result.lshrInPlace(Result.BitWidth - BitWidth); in byteSwap()
760 --S; in reverseBits()
768 // Fast-path a common case. in GreatestCommonDivisor()
781 A.lshrInPlace(Pow2_A - Pow2_B); in GreatestCommonDivisor()
784 B.lshrInPlace(Pow2_B - Pow2_A); in GreatestCommonDivisor()
793 // gcd(a, b) = gcd(|a - b| / 2^i, min(a, b)) in GreatestCommonDivisor()
799 A -= B; in GreatestCommonDivisor()
800 A.lshrInPlace(A.countr_zero() - Pow2); in GreatestCommonDivisor()
802 B -= A; in GreatestCommonDivisor()
803 B.lshrInPlace(B.countr_zero() - Pow2); in GreatestCommonDivisor()
816 // Get the 11-bit exponent and adjust for the 1023 bit bias in RoundDoubleToAPInt()
817 int64_t exp = ((I >> 52) & 0x7ff) - 1023; in RoundDoubleToAPInt()
828 return isNeg ? -APInt(width, mantissa >> (52 - exp)) : in RoundDoubleToAPInt()
829 APInt(width, mantissa >> (52 - exp)); in RoundDoubleToAPInt()
833 if (width <= exp - 52) in RoundDoubleToAPInt()
838 Tmp <<= (unsigned)exp - 52; in RoundDoubleToAPInt()
839 return isNeg ? -Tmp : Tmp; in RoundDoubleToAPInt()
844 /// --------------------------------------
846 /// |-------------------------------------- |
847 /// | 1[63] 11[62-52] 52[51-00] 1023 |
848 /// --------------------------------------
862 bool isNeg = isSigned ? (*this)[BitWidth-1] : false; in roundToDouble()
865 APInt Tmp(isNeg ? -(*this) : (*this)); in roundToDouble()
880 return -std::numeric_limits<double>::infinity(); in roundToDouble()
887 unsigned hiWord = whichWord(n-1); in roundToDouble()
891 mantissa >>= n - 52; // shift down, we want the top 52 bits. in roundToDouble()
894 uint64_t hibits = Tmp.U.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD); in roundToDouble()
895 uint64_t lobits = Tmp.U.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD); in roundToDouble()
900 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0; in roundToDouble()
923 unsigned bits = (0 - width) % APINT_BITS_PER_WORD; in trunc()
969 Result.U.pVal[getNumWords() - 1] = in sext()
970 SignExtend64(Result.U.pVal[getNumWords() - 1], in sext()
971 ((BitWidth - 1) % APINT_BITS_PER_WORD) + 1); in sext()
974 std::memset(Result.U.pVal + getNumWords(), isNegative() ? -1 : 0, in sext()
975 (Result.getNumWords() - getNumWords()) * APINT_WORD_SIZE); in sext()
997 (Result.getNumWords() - getNumWords()) * APINT_WORD_SIZE); in zext()
1018 /// Arithmetic right-shift this APInt by shiftAmt.
1019 /// Arithmetic right-shift function.
1024 /// Arithmetic right-shift this APInt by shiftAmt.
1025 /// Arithmetic right-shift function.
1027 // Don't bother performing a no-op shift. in ashrSlowCase()
1034 // WordShift is the inter-part shift; BitShift is intra-part shift. in ashrSlowCase()
1038 unsigned WordsToMove = getNumWords() - WordShift; in ashrSlowCase()
1041 U.pVal[getNumWords() - 1] = SignExtend64( in ashrSlowCase()
1042 U.pVal[getNumWords() - 1], ((BitWidth - 1) % APINT_BITS_PER_WORD) + 1); in ashrSlowCase()
1049 for (unsigned i = 0; i != WordsToMove - 1; ++i) in ashrSlowCase()
1051 (U.pVal[i + WordShift + 1] << (APINT_BITS_PER_WORD - BitShift)); in ashrSlowCase()
1054 U.pVal[WordsToMove - 1] = U.pVal[WordShift + WordsToMove - 1] >> BitShift; in ashrSlowCase()
1056 U.pVal[WordsToMove - 1] = in ashrSlowCase()
1057 SignExtend64(U.pVal[WordsToMove - 1], APINT_BITS_PER_WORD - BitShift); in ashrSlowCase()
1062 std::memset(U.pVal + WordsToMove, Negative ? -1 : 0, in ashrSlowCase()
1067 /// Logical right-shift this APInt by shiftAmt.
1068 /// Logical right-shift function.
1073 /// Logical right-shift this APInt by shiftAmt.
1074 /// Logical right-shift function.
1079 /// Left-shift this APInt by shiftAmt.
1080 /// Left-shift function.
1117 return shl(rotateAmt) | lshr(BitWidth - rotateAmt); in rotl()
1130 return lshr(rotateAmt) | shl(BitWidth - rotateAmt); in rotr()
1147 return U.VAL - 1; in nearestLogBase2()
1153 // The non-zero case is handled by computing: in nearestLogBase2()
1155 // nearestLogBase2(x) = logBase2(x) + x[logBase2(x)-1]. in nearestLogBase2()
1159 return lg + unsigned((*this)[lg - 1]); in nearestLogBase2()
1162 // Square Root - this method computes and returns the square root of "this".
1179 /* 1- 2 */ 1, 1, in sqrt()
1180 /* 3- 6 */ 2, 2, 2, 2, in sqrt()
1181 /* 7-12 */ 3, 3, 3, 3, 3, 3, in sqrt()
1182 /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4, in sqrt()
1183 /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, in sqrt()
1212 if (i >= nbits || this->ule(testy)) { in sqrt()
1219 x_new = (this->udiv(x_old) + x_old).udiv(two); in sqrt()
1227 // off-by-one discrepancy with results from pari/gp. That discrepancy has been in sqrt()
1233 if (this->ult(square)) in sqrt()
1235 assert(this->ule(nextSquare) && "Error in APInt::sqrt computation"); in sqrt()
1236 APInt midpoint((nextSquare - square).udiv(two)); in sqrt()
1237 APInt offset(*this - square); in sqrt()
1252 Factor *= 2 - std::move(T); in multiplicativeInverse()
1281 DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]); in KnuthDiv()
1283 DEBUG_KNUTH(for (int i = n; i > 0; i--) dbgs() << " " << v[i - 1]); in KnuthDiv()
1285 // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of in KnuthDiv()
1287 // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of in KnuthDiv()
1293 unsigned shift = llvm::countl_zero(v[n - 1]); in KnuthDiv()
1298 uint32_t u_tmp = u[i] >> (32 - shift); in KnuthDiv()
1303 uint32_t v_tmp = v[i] >> (32 - shift); in KnuthDiv()
1311 DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]); in KnuthDiv()
1313 DEBUG_KNUTH(for (int i = n; i > 0; i--) dbgs() << " " << v[i - 1]); in KnuthDiv()
1321 // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q') in KnuthDiv()
1322 // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r') in KnuthDiv()
1323 // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease in KnuthDiv()
1324 // qp by 1, increase rp by v[n-1], and repeat this test if rp < b. The test in KnuthDiv()
1325 // on v[n-2] determines at high speed most of the cases in which the trial in KnuthDiv()
1328 uint64_t dividend = Make_64(u[j+n], u[j+n-1]); in KnuthDiv()
1330 uint64_t qp = dividend / v[n-1]; in KnuthDiv()
1331 uint64_t rp = dividend % v[n-1]; in KnuthDiv()
1332 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) { in KnuthDiv()
1333 qp--; in KnuthDiv()
1334 rp += v[n-1]; in KnuthDiv()
1335 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2])) in KnuthDiv()
1336 qp--; in KnuthDiv()
1340 // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with in KnuthDiv()
1341 // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation in KnuthDiv()
1342 // consists of a simple multiplication by a one-place number, combined with in KnuthDiv()
1351 int64_t subres = int64_t(u[j+i]) - borrow - Lo_32(p); in KnuthDiv()
1353 borrow = Hi_32(p) - Hi_32(subres); in KnuthDiv()
1358 u[j+n] -= Lo_32(borrow); in KnuthDiv()
1361 DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]); in KnuthDiv()
1371 q[j]--; in KnuthDiv()
1372 // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]). in KnuthDiv()
1384 DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]); in KnuthDiv()
1388 } while (--j >= 0); in KnuthDiv()
1391 DEBUG_KNUTH(for (int i = m; i >= 0; i--) dbgs() << " " << q[i]); in KnuthDiv()
1395 // remainder may be obtained by dividing u[...] by d. If r is non-null we in KnuthDiv()
1404 for (int i = n-1; i >= 0; i--) { in KnuthDiv()
1406 carry = u[i] << (32 - shift); in KnuthDiv()
1410 for (int i = n-1; i >= 0; i--) { in KnuthDiv()
1424 // First, compose the values into an array of 32-bit words instead of in divide()
1425 // 64-bit words. This is a necessity of both the "short division" algorithm in divide()
1427 // operations for +, -, and * on an m bit value with an m*2 bit result. We in divide()
1428 // can't use 64-bit operands here because we don't have native results of in divide()
1429 // 128-bits. Furthermore, casting the 64-bit values to 32-bit values won't in divide()
1430 // work on large-endian machines. in divide()
1432 unsigned m = (lhsWords * 2) - n; in divide()
1481 for (unsigned i = n; i > 0 && V[i-1] == 0; i--) { in divide()
1482 n--; in divide()
1485 for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--) in divide()
1486 m--; in divide()
1490 // and faster because we are certain that we can divide a 64-bit quantity in divide()
1491 // by a 32-bit quantity at hardware speed and short division is simply a in divide()
1498 for (int i = m; i >= 0; i--) { in divide()
1511 remainder = Lo_32(partial_dividend - (Q[i] * divisor)); in divide()
1565 if (lhsWords < rhsWords || this->ult(RHS)) in udiv()
1573 return APInt(BitWidth, this->U.pVal[0] / RHS.U.pVal[0]); in udiv()
1598 if (this->ult(RHS)) in udiv()
1606 return APInt(BitWidth, this->U.pVal[0] / RHS); in udiv()
1617 return (-(*this)).udiv(-RHS); in sdiv()
1618 return -((-(*this)).udiv(RHS)); in sdiv()
1621 return -(this->udiv(-RHS)); in sdiv()
1622 return this->udiv(RHS); in sdiv()
1628 return (-(*this)).udiv(-RHS); in sdiv()
1629 return -((-(*this)).udiv(RHS)); in sdiv()
1632 return -(this->udiv(-RHS)); in sdiv()
1633 return this->udiv(RHS); in sdiv()
1658 if (lhsWords < rhsWords || this->ult(RHS)) in urem()
1690 if (this->ult(RHS)) in urem()
1709 return -((-(*this)).urem(-RHS)); in srem()
1710 return -((-(*this)).urem(RHS)); in srem()
1713 return this->urem(-RHS); in srem()
1714 return this->urem(RHS); in srem()
1720 return -((-(*this)).urem(-RHS)); in srem()
1721 return -((-(*this)).urem(RHS)); in srem()
1724 return this->urem(-RHS); in srem()
1725 return this->urem(RHS); in srem()
1794 (getNumWords(BitWidth) - lhsWords) * APINT_WORD_SIZE); in udivrem()
1796 (getNumWords(BitWidth) - rhsWords) * APINT_WORD_SIZE); in udivrem()
1857 (getNumWords(BitWidth) - lhsWords) * APINT_WORD_SIZE); in udivrem()
1864 APInt::udivrem(-LHS, -RHS, Quotient, Remainder); in sdivrem()
1866 APInt::udivrem(-LHS, RHS, Quotient, Remainder); in sdivrem()
1871 APInt::udivrem(LHS, -RHS, Quotient, Remainder); in sdivrem()
1883 APInt::udivrem(-LHS, -RHS, Quotient, R); in sdivrem()
1885 APInt::udivrem(-LHS, RHS, Quotient, R); in sdivrem()
1888 R = -R; in sdivrem()
1890 APInt::udivrem(LHS, -RHS, Quotient, R); in sdivrem()
1912 APInt Res = *this - RHS; in ssub_ov()
1919 APInt Res = *this-RHS; in usub_ov()
1925 // MININT/-1 --> overflow. in sdiv_ov()
1992 return quotient - 1; in sfloordiv_ov()
2092 bool isNeg = *p == '-'; in fromString()
2093 if (*p == '-' || *p == '+') { in fromString()
2095 slen--; in fromString()
2099 assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width"); in fromString()
2100 assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width"); in fromString()
2101 assert((((slen-1)*64)/22 <= numbits || radix != 10) && in fromString()
2131 this->negate(); in fromString()
2145 // Binary literals are a non-standard extension added in gcc 4.3: in toString()
2146 // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Binary-constants.html in toString()
2191 Str.push_back('-'); in toString()
2192 N = -(uint64_t)I; in toString()
2204 *--BufPtr = '\''; in toString()
2205 *--BufPtr = Digits[N % Radix]; in toString()
2218 // value and put a '-' in the result. in toString()
2220 Str.push_back('-'); in toString()
2237 unsigned MaskAmt = Radix - 1; in toString()
2270 this->toStringUnsigned(U); in dump()
2271 this->toStringSigned(S); in dump()
2279 this->toString(S, 10, isSigned, /* formatAsCLiteral = */false); in print()
2284 // arbitrary precision, two's-complement, bignum integer values.
2295 return ~(APInt::WordType) 0 >> (APInt::APINT_BITS_PER_WORD - bits); in lowBitMask()
2364 --n; in tcMSB()
2379 /// significant bit of DST. All high bits above srcBITS in DST are zero-filled.
2384 unsigned dstParts = (srcBits + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; in tcExtract()
2393 // We now have (dstParts * APINT_BITS_PER_WORD - shift) bits from SRC in tcExtract()
2396 unsigned n = dstParts * APINT_BITS_PER_WORD - shift; in tcExtract()
2398 WordType mask = lowBitMask (srcBits - n); in tcExtract()
2399 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask) in tcExtract()
2403 dst[dstParts - 1] &= lowBitMask (srcBits % APINT_BITS_PER_WORD); in tcExtract()
2446 /// DST -= RHS + C where C is zero or one. Returns the carry flag.
2454 dst[i] -= rhs[i] + 1; in tcSubtract()
2457 dst[i] -= rhs[i]; in tcSubtract()
2465 /// This function subtracts a single "word" (64-bit word), src, from
2466 /// the multi-word integer array, dst[], propagating the borrowed 1 value until
2476 dst[i] -= src; in tcSubtractPart()
2485 /// Negate a bignum in-place.
2514 // (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1) in tcMultiplyPart()
2517 WordType low, mid, high; in tcMultiplyPart() local
2525 mid = lowHalf(srcPart) * highHalf(multiplier); in tcMultiplyPart()
2526 high += highHalf(mid); in tcMultiplyPart()
2527 mid <<= APINT_BITS_PER_WORD / 2; in tcMultiplyPart()
2528 if (low + mid < low) in tcMultiplyPart()
2530 low += mid; in tcMultiplyPart()
2532 mid = highHalf(srcPart) * lowHalf(multiplier); in tcMultiplyPart()
2533 high += highHalf(mid); in tcMultiplyPart()
2534 mid <<= APINT_BITS_PER_WORD / 2; in tcMultiplyPart()
2535 if (low + mid < low) in tcMultiplyPart()
2537 low += mid; in tcMultiplyPart()
2568 // non-zero. This is true if any remaining src parts are non-zero in tcMultiplyPart()
2569 // and the multiplier is non-zero. in tcMultiplyPart()
2593 tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts, parts - i, i != 0); in tcMultiply()
2635 shiftCount = parts * APINT_BITS_PER_WORD - shiftCount; in tcDivide()
2655 shiftCount--; in tcDivide()
2658 mask = (WordType) 1 << (APINT_BITS_PER_WORD - 1); in tcDivide()
2659 n--; in tcDivide()
2666 /// Shift a bignum left Count bits in-place. Shifted in bits are zero. There are
2669 // Don't bother performing a no-op shift. in tcShiftLeft()
2673 // WordShift is the inter-part shift; BitShift is the intra-part shift. in tcShiftLeft()
2679 std::memmove(Dst + WordShift, Dst, (Words - WordShift) * APINT_WORD_SIZE); in tcShiftLeft()
2681 while (Words-- > WordShift) { in tcShiftLeft()
2682 Dst[Words] = Dst[Words - WordShift] << BitShift; in tcShiftLeft()
2685 Dst[Words - WordShift - 1] >> (APINT_BITS_PER_WORD - BitShift); in tcShiftLeft()
2693 /// Shift a bignum right Count bits in-place. Shifted in bits are zero. There
2696 // Don't bother performing a no-op shift. in tcShiftRight()
2700 // WordShift is the inter-part shift; BitShift is the intra-part shift. in tcShiftRight()
2704 unsigned WordsToMove = Words - WordShift; in tcShiftRight()
2712 Dst[i] |= Dst[i + WordShift + 1] << (APINT_BITS_PER_WORD - BitShift); in tcShiftRight()
2724 parts--; in tcCompare()
2726 return (lhs[parts] > rhs[parts]) ? 1 : -1; in tcCompare()
2760 // We want to check whether the non-integer part of the mathematical value in RoundingSDiv()
2761 // is negative or not. If the non-integer part is negative, we need to round in RoundingSDiv()
2766 return Quo - 1; in RoundingSDiv()
2799 // so it can actually lose high bits. A product of two n-bit integers needs in SolveQuadraticEquationWrap()
2800 // 2n-1 bits to represent the full value. in SolveQuadraticEquationWrap()
2831 // exceeds kR, while |q(x-1)| for the same k does not. in SolveQuadraticEquationWrap()
2852 auto RoundUp = [] (const APInt &V, const APInt &A) -> APInt { in SolveQuadraticEquationWrap()
2857 return V.isNegative() ? V+T : V+(A-T); in SolveQuadraticEquationWrap()
2860 // The vertex of the parabola is at -B/2A, but since A > 0, it's negative in SolveQuadraticEquationWrap()
2864 // order to have a non-negative solution we need to pick k that makes in SolveQuadraticEquationWrap()
2865 // C-kR negative. To satisfy all the requirements for the solution in SolveQuadraticEquationWrap()
2869 C -= R; in SolveQuadraticEquationWrap()
2874 // to exist, the discriminant must be non-negative. This means that in SolveQuadraticEquationWrap()
2875 // C-kR <= B^2/4A is a necessary condition for k, i.e. there is a in SolveQuadraticEquationWrap()
2876 // lower bound on values of k: kR >= C - B^2/4A. in SolveQuadraticEquationWrap()
2877 APInt LowkR = C - SqrB.udiv(2*TwoA); // udiv because all values > 0. in SolveQuadraticEquationWrap()
2882 // C-kR > 0, there will be two positive real number solutions of in SolveQuadraticEquationWrap()
2884 // C-kR closest to 0, (i.e. pick maximum k such that C-kR > 0). in SolveQuadraticEquationWrap()
2889 C -= -RoundUp(-C, R); // C = C - RoundDown(C, R) in SolveQuadraticEquationWrap()
2893 // If C-kR < 0 for all potential k's, it means that one solution in SolveQuadraticEquationWrap()
2896 // Pick the kR closest to the lower bound (i.e. make C-kR closest in SolveQuadraticEquationWrap()
2899 // Since LowkR is itself a multiple of R, simply take C-LowkR. in SolveQuadraticEquationWrap()
2900 C -= LowkR; in SolveQuadraticEquationWrap()
2909 APInt D = SqrB - 4*A*C; in SolveQuadraticEquationWrap()
2915 // The calculated SQ may actually be greater than the exact (non-integer) in SolveQuadraticEquationWrap()
2918 SQ -= 1; in SolveQuadraticEquationWrap()
2930 APInt::sdivrem(-B - (SQ+InexactSQ), TwoA, X, Rem); in SolveQuadraticEquationWrap()
2932 APInt::sdivrem(-B + SQ, TwoA, X, Rem); in SolveQuadraticEquationWrap()
2937 assert(X.isNonNegative() && "Solution should be non-negative"); in SolveQuadraticEquationWrap()
2975 return A.getBitWidth() - ((A ^ B).countl_zero() + 1); in GetMostSignificantDifferentBit()
2998 unsigned Scale = NewBitWidth / OldBitWidth; in ScaleBitMask() local
3001 NewA.setBits(i * Scale, (i + 1) * Scale); in ScaleBitMask()
3003 unsigned Scale = OldBitWidth / NewBitWidth; in ScaleBitMask() local
3006 if (A.extractBits(Scale, i * Scale).isAllOnes()) in ScaleBitMask()
3009 if (!A.extractBits(Scale, i * Scale).isZero()) in ScaleBitMask()
3018 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
3026 // Little-endian host - the source is ordered from LSB to MSB. Order the in StoreIntToMemory()
3030 // Big-endian host - the source is an array of 64 bit words ordered from in StoreIntToMemory()
3034 StoreBytes -= sizeof(uint64_t); in StoreIntToMemory()
3040 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes); in StoreIntToMemory()
3044 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
3053 // Little-endian host - the destination must be ordered from LSB to MSB. in LoadIntFromMemory()
3057 // Big-endian - the destination is an array of 64 bit words ordered from in LoadIntFromMemory()
3062 LoadBytes -= sizeof(uint64_t); in LoadIntFromMemory()
3068 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes); in LoadIntFromMemory()
3084 return (C1 | C2) - (C1 ^ C2).ashr(1); in avgCeilS()
3089 return (C1 | C2) - (C1 ^ C2).lshr(1); in avgCeilU()