/// failure. The content is not zeroed.
inline static uint64_t* getMemory(unsigned numWords) {
uint64_t * result = new uint64_t[numWords];
/// failure. The content is not zeroed.
inline static uint64_t* getMemory(unsigned numWords) {
uint64_t * result = new uint64_t[numWords];
void APInt::initSlowCase(unsigned numBits, uint64_t val, bool isSigned) {
pVal = getClearedMemory(getNumWords());
pVal[0] = val;
void APInt::initSlowCase(unsigned numBits, uint64_t val, bool isSigned) {
pVal = getClearedMemory(getNumWords());
pVal[0] = val;
: BitWidth(numbits), VAL(0) {
assert(BitWidth && "Bitwidth too small");
fromString(numbits, Str, radix);
: BitWidth(numbits), VAL(0) {
assert(BitWidth && "Bitwidth too small");
fromString(numbits, Str, radix);
VAL = 0;
pVal = getMemory(RHS.getNumWords());
memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
VAL = 0;
pVal = getMemory(RHS.getNumWords());
memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
else if (RHS.isSingleWord()) {
delete [] pVal;
memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
else if (RHS.isSingleWord()) {
delete [] pVal;
/// Profile - This method 'profiles' an APInt for use with FoldingSet.
void APInt::Profile(FoldingSetNodeID& ID) const {
ID.AddInteger(BitWidth);
/// Profile - This method 'profiles' an APInt for use with FoldingSet.
void APInt::Profile(FoldingSetNodeID& ID) const {
ID.AddInteger(BitWidth);
/// "digit" integer array, x[]. x[] is modified to reflect the addition and
/// 1 is returned if there is a carry out, otherwise 0 is returned.
/// @returns the carry of the addition.
/// "digit" integer array, x[]. x[] is modified to reflect the addition and
/// 1 is returned if there is a carry out, otherwise 0 is returned.
/// @returns the carry of the addition.
++VAL;
else
add_1(pVal, pVal, getNumWords(), 1);
return clearUnusedBits();
}
++VAL;
else
add_1(pVal, pVal, getNumWords(), 1);
return clearUnusedBits();
}
-/// sub_1 - This function subtracts a single "digit" (64-bit word), y, from
-/// the multi-digit integer array, x[], propagating the borrowed 1 value until
+/// sub_1 - This function subtracts a single "digit" (64-bit word), y, from
+/// the multi-digit integer array, x[], propagating the borrowed 1 value until
/// no further borrowing is neeeded or it runs out of "digits" in x. The result
/// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted.
/// In other words, if y > x then this function returns 1, otherwise 0.
/// no further borrowing is neeeded or it runs out of "digits" in x. The result
/// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted.
/// In other words, if y > x then this function returns 1, otherwise 0.
-static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y,
+static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y,
/// Adds the RHS APint to this APInt.
/// @returns this, after addition of RHS.
/// Adds the RHS APint to this APInt.
/// @returns this, after addition of RHS.
APInt& APInt::operator+=(const APInt& RHS) {
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
APInt& APInt::operator+=(const APInt& RHS) {
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
VAL += RHS.VAL;
else {
add(pVal, pVal, RHS.pVal, getNumWords());
VAL += RHS.VAL;
else {
add(pVal, pVal, RHS.pVal, getNumWords());
-static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y,
+static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y,
/// Subtracts the RHS APInt from this APInt
/// @returns this, after subtraction
/// Subtracts the RHS APInt from this APInt
/// @returns this, after subtraction
APInt& APInt::operator-=(const APInt& RHS) {
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
APInt& APInt::operator-=(const APInt& RHS) {
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
VAL -= RHS.VAL;
else
sub(pVal, pVal, RHS.pVal, getNumWords());
VAL -= RHS.VAL;
else
sub(pVal, pVal, RHS.pVal, getNumWords());
/// @returns the carry out of the multiplication.
/// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer.
static uint64_t mul_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) {
/// @returns the carry out of the multiplication.
/// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer.
static uint64_t mul_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) {
// Determine if the add above introduces carry.
hasCarry = (dest[i] < carry) ? 1 : 0;
carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
// Determine if the add above introduces carry.
hasCarry = (dest[i] < carry) ? 1 : 0;
carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
// (2^32 - 1) + 2^32 = 2^64.
hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
carry += (lx * hy) & 0xffffffffULL;
dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
// (2^32 - 1) + 2^32 = 2^64.
hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
carry += (lx * hy) & 0xffffffffULL;
dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
/// the integer array dest. Note that dest's size must be >= xlen + ylen.
/// @brief Generalized multiplicate of integer arrays.
static void mul(uint64_t dest[], uint64_t x[], unsigned xlen, uint64_t y[],
/// the integer array dest. Note that dest's size must be >= xlen + ylen.
/// @brief Generalized multiplicate of integer arrays.
static void mul(uint64_t dest[], uint64_t x[], unsigned xlen, uint64_t y[],
resul = (carry << 32) | (resul & 0xffffffffULL);
dest[i+j] += resul;
carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
resul = (carry << 32) | (resul & 0xffffffffULL);
dest[i+j] += resul;
carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
// Get some bit facts about LHS and check for zero
unsigned lhsBits = getActiveBits();
unsigned lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
// Get some bit facts about LHS and check for zero
unsigned lhsBits = getActiveBits();
unsigned lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
unsigned n2 = RHS.getActiveBits();
// If the number of bits isn't the same, they aren't equal
unsigned n2 = RHS.getActiveBits();
// If the number of bits isn't the same, they aren't equal
// Otherwise, compare everything
for (int i = whichWord(n1 - 1); i >= 0; --i)
// Otherwise, compare everything
for (int i = whichWord(n1 - 1); i >= 0; --i)
// Otherwise, compare all words
unsigned topWord = whichWord(std::max(n1,n2)-1);
for (int i = topWord; i >= 0; --i) {
// Otherwise, compare all words
unsigned topWord = whichWord(std::max(n1,n2)-1);
for (int i = topWord; i >= 0; --i) {
/// Set the given bit to 0 whose position is given as "bitPosition".
/// @brief Set a given bit to 0.
APInt& APInt::clear(unsigned bitPosition) {
/// Set the given bit to 0 whose position is given as "bitPosition".
/// @brief Set a given bit to 0.
APInt& APInt::clear(unsigned bitPosition) {
pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
return *this;
}
/// @brief Toggle every bit to its opposite value.
pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
return *this;
}
/// @brief Toggle every bit to its opposite value.
// This is grossly inefficient but accurate. We could probably do something
// with a computation of roughly slen*64/20 and then adjust by the value of
// the first few digits. But, I'm not sure how accurate that could be.
// Compute a sufficient number of bits that is always large enough but might
// This is grossly inefficient but accurate. We could probably do something
// with a computation of roughly slen*64/20 and then adjust by the value of
// the first few digits. But, I'm not sure how accurate that could be.
// Compute a sufficient number of bits that is always large enough but might
- // be too large. This avoids the assertion in the constructor.
- unsigned sufficient = slen*64/18;
+ // be too large. This avoids the assertion in the constructor. This
+ // calculation doesn't work appropriately for the numbers 0-9, so just use 4
+ // bits in that case.
+ unsigned sufficient = slen == 1 ? 4 : slen * 64/18;
// Convert to the actual binary value.
APInt tmp(sufficient, StringRef(p, slen), radix);
// Convert to the actual binary value.
APInt tmp(sufficient, StringRef(p, slen), radix);
- // Compute how many bits are required.
- return isNegative + tmp.logBase2() + 1;
+ // Compute how many bits are required. If the log is infinite, assume we need
+ // just bit.
+ unsigned log = tmp.logBase2();
+ if (log == (unsigned)-1) {
+ return isNegative + 1;
+ } else {
+ return isNegative + log + 1;
+ }
/// LoBits - This function returns the low "numBits" bits of this APInt.
APInt APInt::getLoBits(unsigned numBits) const {
/// LoBits - This function returns the low "numBits" bits of this APInt.
APInt APInt::getLoBits(unsigned numBits) const {
APInt(width, mantissa >> (52 - exp));
// If the client didn't provide enough bits for us to shift the mantissa into
APInt(width, mantissa >> (52 - exp));
// If the client didn't provide enough bits for us to shift the mantissa into
/// | Sign Exponent Fraction Bias |
/// |-------------------------------------- |
/// | 1[63] 11[62-52] 52[51-00] 1023 |
/// | Sign Exponent Fraction Bias |
/// |-------------------------------------- |
/// | 1[63] 11[62-52] 52[51-00] 1023 |
double APInt::roundToDouble(bool isSigned) const {
// Handle the simple case where the value is contained in one uint64_t.
double APInt::roundToDouble(bool isSigned) const {
// Handle the simple case where the value is contained in one uint64_t.
return APInt(BitWidth, 0); // undefined
else {
unsigned SignBit = APINT_BITS_PER_WORD - BitWidth;
return APInt(BitWidth, 0); // undefined
else {
unsigned SignBit = APINT_BITS_PER_WORD - BitWidth;
for (unsigned i = 0; i < breakWord; ++i) {
// This combines the shifted corresponding word with the low bits from
// the next word (shifted into this word's high bits).
for (unsigned i = 0; i < breakWord; ++i) {
// This combines the shifted corresponding word with the low bits from
// the next word (shifted into this word's high bits).
if (isSingleWord()) {
if (shiftAmt == BitWidth)
return APInt(BitWidth, 0);
if (isSingleWord()) {
if (shiftAmt == BitWidth)
return APInt(BitWidth, 0);
return APInt(BitWidth, 0);
// If none of the bits are shifted out, the result is *this. This avoids
return APInt(BitWidth, 0);
// If none of the bits are shifted out, the result is *this. This avoids
unsigned breakWord = getNumWords() - offset -1;
for (unsigned i = 0; i < breakWord; ++i)
val[i] = (pVal[i+offset] >> wordShift) |
unsigned breakWord = getNumWords() - offset -1;
for (unsigned i = 0; i < breakWord; ++i)
val[i] = (pVal[i+offset] >> wordShift) |
// values using less than 52 bits, the value is converted to double and then
// the libc sqrt function is called. The result is rounded and then converted
// back to a uint64_t which is then used to construct the result. Finally,
// values using less than 52 bits, the value is converted to double and then
// the libc sqrt function is called. The result is rounded and then converted
// back to a uint64_t which is then used to construct the result. Finally,
/* 7-12 */ 3, 3, 3, 3, 3, 3,
/* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4,
/* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
/* 7-12 */ 3, 3, 3, 3, 3, 3,
/* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4,
/* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
// is a classical Babylonian method for computing the square root. This code
// was adapted to APINt from a wikipedia article on such computations.
// See http://www.wikipedia.org/ and go to the page named
// is a classical Babylonian method for computing the square root. This code
// was adapted to APINt from a wikipedia article on such computations.
// See http://www.wikipedia.org/ and go to the page named
unsigned nbits = BitWidth, i = 4;
APInt testy(BitWidth, 16);
APInt x_old(BitWidth, 1);
unsigned nbits = BitWidth, i = 4;
APInt testy(BitWidth, 16);
APInt x_old(BitWidth, 1);
// floating point representation after 192 bits. There are no discrepancies
// between this algorithm and pari/gp for bit widths < 192 bits.
APInt square(x_old * x_old);
// floating point representation after 192 bits. There are no discrepancies
// between this algorithm and pari/gp for bit widths < 192 bits.
APInt square(x_old * x_old);
APInt r[2] = { modulo, *this };
APInt t[2] = { APInt(BitWidth, 0), APInt(BitWidth, 1) };
APInt q(BitWidth, 0);
APInt r[2] = { modulo, *this };
APInt t[2] = { APInt(BitWidth, 0), APInt(BitWidth, 1) };
APInt q(BitWidth, 0);
const APInt& d = *this;
unsigned p;
APInt ad, anc, delta, q1, r1, q2, r2, t;
const APInt& d = *this;
unsigned p;
APInt ad, anc, delta, q1, r1, q2, r2, t;
mag.m = q2 + 1;
if (d.isNegative()) mag.m = -mag.m; // resulting magic number
mag.s = p - d.getBitWidth(); // resulting shift
mag.m = q2 + 1;
if (d.isNegative()) mag.m = -mag.m; // resulting magic number
mag.s = p - d.getBitWidth(); // resulting shift
- DEBUG(errs() << "KnuthDiv: m=" << m << " n=" << n << '\n');
- DEBUG(errs() << "KnuthDiv: original:");
- DEBUG(for (int i = m+n; i >=0; i--) errs() << " " << u[i]);
- DEBUG(errs() << " by");
- DEBUG(for (int i = n; i >0; i--) errs() << " " << v[i-1]);
- DEBUG(errs() << '\n');
+ DEBUG(dbgs() << "KnuthDiv: m=" << m << " n=" << n << '\n');
+ DEBUG(dbgs() << "KnuthDiv: original:");
+ DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
+ DEBUG(dbgs() << " by");
+ DEBUG(for (int i = n; i >0; i--) dbgs() << " " << v[i-1]);
+ DEBUG(dbgs() << '\n');
- // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
- // u and v by d. Note that we have taken Knuth's advice here to use a power
- // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
- // 2 allows us to shift instead of multiply and it is easy to determine the
+ // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
+ // u and v by d. Note that we have taken Knuth's advice here to use a power
+ // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
+ // 2 allows us to shift instead of multiply and it is easy to determine the
// shift amount from the leading zeros. We are basically normalizing the u
// and v so that its high bits are shifted to the top of v's range without
// overflow. Note that this can require an extra word in u so that u must
// shift amount from the leading zeros. We are basically normalizing the u
// and v so that its high bits are shifted to the top of v's range without
// overflow. Note that this can require an extra word in u so that u must
- DEBUG(errs() << "KnuthDiv: normal:");
- DEBUG(for (int i = m+n; i >=0; i--) errs() << " " << u[i]);
- DEBUG(errs() << " by");
- DEBUG(for (int i = n; i >0; i--) errs() << " " << v[i-1]);
- DEBUG(errs() << '\n');
+ DEBUG(dbgs() << "KnuthDiv: normal:");
+ DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
+ DEBUG(dbgs() << " by");
+ DEBUG(for (int i = n; i >0; i--) dbgs() << " " << v[i-1]);
+ DEBUG(dbgs() << '\n');
// Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
// Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
// Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
// qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test
// on v[n-2] determines at high speed most of the cases in which the trial
// Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
// Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
// Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
// qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test
// on v[n-2] determines at high speed most of the cases in which the trial
uint64_t qp = dividend / v[n-1];
uint64_t rp = dividend % v[n-1];
if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
uint64_t qp = dividend / v[n-1];
uint64_t rp = dividend % v[n-1];
if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
// D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
// (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
// consists of a simple multiplication by a one-place number, combined with
// D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
// (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
// consists of a simple multiplication by a one-place number, combined with
bool isNeg = false;
for (unsigned i = 0; i < n; ++i) {
uint64_t u_tmp = uint64_t(u[j+i]) | (uint64_t(u[j+i+1]) << 32);
uint64_t subtrahend = uint64_t(qp) * uint64_t(v[i]);
bool borrow = subtrahend > u_tmp;
bool isNeg = false;
for (unsigned i = 0; i < n; ++i) {
uint64_t u_tmp = uint64_t(u[j+i]) | (uint64_t(u[j+i+1]) << 32);
uint64_t subtrahend = uint64_t(qp) * uint64_t(v[i]);
bool borrow = subtrahend > u_tmp;
<< ", subtrahend == " << subtrahend
<< ", borrow = " << borrow << '\n');
<< ", subtrahend == " << subtrahend
<< ", borrow = " << borrow << '\n');
- DEBUG(errs() << "KnuthDiv: after subtraction:");
- DEBUG(for (int i = m+n; i >=0; i--) errs() << " " << u[i]);
- DEBUG(errs() << '\n');
- // The digits (u[j+n]...u[j]) should be kept positive; if the result of
- // this step is actually negative, (u[j+n]...u[j]) should be left as the
+ DEBUG(dbgs() << "KnuthDiv: after subtraction:");
+ DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
+ DEBUG(dbgs() << '\n');
+ // The digits (u[j+n]...u[j]) should be kept positive; if the result of
+ // this step is actually negative, (u[j+n]...u[j]) should be left as the
// true value plus b**(n+1), namely as the b's complement of
// the true value, and a "borrow" to the left should be remembered.
//
// true value plus b**(n+1), namely as the b's complement of
// the true value, and a "borrow" to the left should be remembered.
//
- DEBUG(errs() << "KnuthDiv: after complement:");
- DEBUG(for (int i = m+n; i >=0; i--) errs() << " " << u[i]);
- DEBUG(errs() << '\n');
+ DEBUG(dbgs() << "KnuthDiv: after complement:");
+ DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
+ DEBUG(dbgs() << '\n');
- // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
- // A carry will occur to the left of u[j+n], and it should be ignored
+ // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
+ // A carry will occur to the left of u[j+n], and it should be ignored
- DEBUG(errs() << "KnuthDiv: after correction:");
- DEBUG(for (int i = m+n; i >=0; i--) errs() <<" " << u[i]);
- DEBUG(errs() << "\nKnuthDiv: digit result = " << q[j] << '\n');
+ DEBUG(dbgs() << "KnuthDiv: after correction:");
+ DEBUG(for (int i = m+n; i >=0; i--) dbgs() <<" " << u[i]);
+ DEBUG(dbgs() << "\nKnuthDiv: digit result = " << q[j] << '\n');
// D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
// remainder may be obtained by dividing u[...] by d. If r is non-null we
// D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
// remainder may be obtained by dividing u[...] by d. If r is non-null we
for (int i = n-1; i >= 0; i--) {
r[i] = (u[i] >> shift) | carry;
carry = u[i] << (32 - shift);
for (int i = n-1; i >= 0; i--) {
r[i] = (u[i] >> shift) | carry;
carry = u[i] << (32 - shift);
- // and the the Knuth "classical algorithm" which requires there to be native
- // operations for +, -, and * on an m bit value with an m*2 bit result. We
- // can't use 64-bit operands here because we don't have native results of
- // 128-bits. Furthermore, casting the 64-bit values to 32-bit values won't
+ // and the the Knuth "classical algorithm" which requires there to be native
+ // operations for +, -, and * on an m bit value with an m*2 bit result. We
+ // can't use 64-bit operands here because we don't have native results of
+ // 128-bits. Furthermore, casting the 64-bit values to 32-bit values won't
// work on large-endian machines.
uint64_t mask = ~0ull >> (sizeof(unsigned)*CHAR_BIT);
unsigned n = rhsWords * 2;
// work on large-endian machines.
uint64_t mask = ~0ull >> (sizeof(unsigned)*CHAR_BIT);
unsigned n = rhsWords * 2;
// contain any zero words or the Knuth algorithm fails.
for (unsigned i = n; i > 0 && V[i-1] == 0; i--) {
n--;
// contain any zero words or the Knuth algorithm fails.
for (unsigned i = n; i > 0 && V[i-1] == 0; i--) {
n--;
} else {
assert(!Quotient->isSingleWord() && "Quotient APInt not large enough");
for (unsigned i = 0; i < lhsWords; ++i)
} else {
assert(!Quotient->isSingleWord() && "Quotient APInt not large enough");
for (unsigned i = 0; i < lhsWords; ++i)
} else {
assert(!Remainder->isSingleWord() && "Remainder APInt not large enough");
for (unsigned i = 0; i < rhsWords; ++i)
} else {
assert(!Remainder->isSingleWord() && "Remainder APInt not large enough");
for (unsigned i = 0; i < rhsWords; ++i)
unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
// Deal with some degenerate cases
unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
// Deal with some degenerate cases
else if (lhsWords < rhsWords || this->ult(RHS)) {
// X / Y ===> 0, iff X < Y
return APInt(BitWidth, 0);
else if (lhsWords < rhsWords || this->ult(RHS)) {
// X / Y ===> 0, iff X < Y
return APInt(BitWidth, 0);
APInt &Quotient, APInt &Remainder) {
// Get some size facts about the dividend and divisor
unsigned lhsBits = LHS.getActiveBits();
APInt &Quotient, APInt &Remainder) {
// Get some size facts about the dividend and divisor
unsigned lhsBits = LHS.getActiveBits();
unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
// Check the degenerate cases
unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
// Check the degenerate cases
if (lhsWords == 1 && rhsWords == 1) {
// There is only one word to consider so use the native versions.
uint64_t lhsValue = LHS.isSingleWord() ? LHS.VAL : LHS.pVal[0];
if (lhsWords == 1 && rhsWords == 1) {
// There is only one word to consider so use the native versions.
uint64_t lhsValue = LHS.isSingleWord() ? LHS.VAL : LHS.pVal[0];
}
assert((slen <= numbits || radix != 2) && "Insufficient bit width");
assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width");
assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width");
}
assert((slen <= numbits || radix != 2) && "Insufficient bit width");
assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width");
assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width");
- // Get a digit
- unsigned digit = 0;
- char cdigit = *p;
- if (radix == 16) {
- if (!isxdigit(cdigit))
- llvm_unreachable("Invalid hex digit in string");
- if (isdigit(cdigit))
- digit = cdigit - '0';
- else if (cdigit >= 'a')
- digit = cdigit - 'a' + 10;
- else if (cdigit >= 'A')
- digit = cdigit - 'A' + 10;
- else
- llvm_unreachable("huh? we shouldn't get here");
- } else if (isdigit(cdigit)) {
- digit = cdigit - '0';
- assert((radix == 10 ||
- (radix == 8 && digit != 8 && digit != 9) ||
- (radix == 2 && (digit == 0 || digit == 1))) &&
- "Invalid digit in string for given radix");
- } else {
- llvm_unreachable("Invalid character in digit string");
- }
+ unsigned digit = getDigit(*p, radix);
+ assert(digit < radix && "Invalid character in digit string");
bool Signed) const {
assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2) &&
"Radix should be 2, 8, 10, or 16!");
bool Signed) const {
assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2) &&
"Radix should be 2, 8, 10, or 16!");
if (Signed && isNegative()) {
// They want to print the signed version and it is a negative value
// Flip the bits and add one to turn it into the equivalent positive
if (Signed && isNegative()) {
// They want to print the signed version and it is a negative value
// Flip the bits and add one to turn it into the equivalent positive
// We insert the digits backward, then reverse them to get the right order.
unsigned StartDig = Str.size();
// We insert the digits backward, then reverse them to get the right order.
unsigned StartDig = Str.size();
-
- // For the 2, 8 and 16 bit cases, we can just shift instead of divide
- // because the number of bits per digit (1, 3 and 4 respectively) divides
+
+ // For the 2, 8 and 16 bit cases, we can just shift instead of divide
+ // because the number of bits per digit (1, 3 and 4 respectively) divides
// equaly. We just shift until the value is zero.
if (Radix != 10) {
// Just shift tmp right for each digit width until it becomes zero
unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
unsigned MaskAmt = Radix - 1;
// equaly. We just shift until the value is zero.
if (Radix != 10) {
// Just shift tmp right for each digit width until it becomes zero
unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
unsigned MaskAmt = Radix - 1;
- divide(Tmp, Tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2,
+ divide(Tmp, Tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2,
// Reverse the digits before returning.
std::reverse(Str.begin()+StartDig, Str.end());
}
// Reverse the digits before returning.
std::reverse(Str.begin()+StartDig, Str.end());
}
// This implements a variety of operations on a representation of
// arbitrary precision, two's-complement, bignum integer values.
// This implements a variety of operations on a representation of
// arbitrary precision, two's-complement, bignum integer values.
-/* Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe
- and unrestricting assumption. */
+// Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe
+// and unrestricting assumption.
#define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
COMPILE_TIME_ASSERT(integerPartWidth % 2 == 0);
#define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
COMPILE_TIME_ASSERT(integerPartWidth % 2 == 0);