typedef uint64_t integerPart;
const unsigned int host_char_bit = 8;
- const unsigned int integerPartWidth = host_char_bit * sizeof(integerPart);
+ const unsigned int integerPartWidth = host_char_bit *
+ static_cast<unsigned int>(sizeof(integerPart));
//===----------------------------------------------------------------------===//
// APInt Class
/// This enum is used to hold the constants we needed for APInt.
enum {
- APINT_BITS_PER_WORD = sizeof(uint64_t) * 8, ///< Bits in a word
- APINT_WORD_SIZE = sizeof(uint64_t) ///< Byte size of a word
+ /// Bits in a word
+ APINT_BITS_PER_WORD = static_cast<unsigned int>(sizeof(uint64_t)) * 8,
+ /// Byte size of a word
+ APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
};
/// This constructor is used only internally for speed of construction of
// the word size (64).
return *this;
- // Mask out the hight bits.
+ // Mask out the high bits.
uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
if (isSingleWord())
VAL &= mask;
return shl(Bits);
}
+ APInt operator<<(const APInt &Bits) const {
+ return shl(Bits);
+ }
+
/// Arithmetic right-shift this APInt by shiftAmt.
/// @brief Arithmetic right-shift function.
APInt ashr(uint32_t shiftAmt) const;
/// @brief Rotate right by rotateAmt.
APInt rotr(uint32_t rotateAmt) const;
+ /// Arithmetic right-shift this APInt by shiftAmt.
+ /// @brief Arithmetic right-shift function.
+ APInt ashr(const APInt &shiftAmt) const;
+
+ /// Logical right-shift this APInt by shiftAmt.
+ /// @brief Logical right-shift function.
+ APInt lshr(const APInt &shiftAmt) const;
+
+ /// Left-shift this APInt by shiftAmt.
+ /// @brief Left-shift function.
+ APInt shl(const APInt &shiftAmt) const;
+
+ /// @brief Rotate left by rotateAmt.
+ APInt rotl(const APInt &rotateAmt) const;
+
+ /// @brief Rotate right by rotateAmt.
+ APInt rotr(const APInt &rotateAmt) const;
+
/// Perform an unsigned divide operation on this APInt by RHS. Both this and
/// RHS are treated as unsigned quantities for purposes of this division.
/// @returns a new APInt value containing the division result
return this->urem(RHS);
}
- /// Sometimes it is convenient to divide two APInt values and obtain both
- /// the quotient and remainder. This function does both operations in the
- /// same computation making it a little more efficient.
+ /// Sometimes it is convenient to divide two APInt values and obtain both the
+ /// quotient and remainder. This function does both operations in the same
+ /// computation making it a little more efficient. The pair of input arguments
+ /// may overlap with the pair of output arguments. It is safe to call
+ /// udivrem(X, Y, X, Y), for example.
/// @brief Dual division/remainder interface.
static void udivrem(const APInt &LHS, const APInt &RHS,
APInt &Quotient, APInt &Remainder);
/// Computes the minimum bit width for this APInt while considering it to be
/// a signed (and probably negative) value. If the value is not negative,
- /// this function returns the same value as getActiveBits(). Otherwise, it
+ /// this function returns the same value as getActiveBits()+1. Otherwise, it
/// returns the smallest bit width that will retain the negative value. For
/// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
/// for -1, this function will always return 1.
return toString(radix, false);
}
- /// Considers the APInt to be unsigned and converts it into a string in the
+ /// Considers the APInt to be signed and converts it into a string in the
/// radix given. The radix can be 2, 8, 10 or 16.
/// @returns a character interpretation of the APInt
- /// @brief Convert unsigned APInt to string representation.
+ /// @brief Convert signed APInt to string representation.
std::string toStringSigned(uint8_t radix = 10) const {
return toString(radix, true);
}
return *this;
}
- /// @}
+ /// @returns the multiplicative inverse for a given modulo.
+ APInt multiplicativeInverse(const APInt& modulo) const;
/// @}
/// @name Building-block Operations for APInt and APFloat
/// DST += RHS + CARRY where CARRY is zero or one. Returns the
/// carry flag.
static integerPart tcAdd(integerPart *, const integerPart *,
- integerPart carry, unsigned);
+ integerPart carry, unsigned);
/// DST -= RHS + CARRY where CARRY is zero or one. Returns the
/// carry flag.
static integerPart tcSubtract(integerPart *, const integerPart *,
- integerPart carry, unsigned);
+ integerPart carry, unsigned);
/// DST += SRC * MULTIPLIER + PART if add is true
/// DST = SRC * MULTIPLIER + PART if add is false
/// parts were zero return zero, otherwise overflow occurred and
/// return one.
static int tcMultiplyPart(integerPart *dst, const integerPart *src,
- integerPart multiplier, integerPart carry,
- unsigned int srcParts, unsigned int dstParts,
- bool add);
+ integerPart multiplier, integerPart carry,
+ unsigned int srcParts, unsigned int dstParts,
+ bool add);
/// DST = LHS * RHS, where DST has the same width as the operands
/// and is filled with the least significant parts of the result.
/// Returns one if overflow occurred, otherwise zero. DST must be
/// disjoint from both operands.
static int tcMultiply(integerPart *, const integerPart *,
- const integerPart *, unsigned);
+ const integerPart *, unsigned);
/// DST = LHS * RHS, where DST has width the sum of the widths of
/// the operands. No overflow occurs. DST must be disjoint from
/// both operands. Returns the number of parts required to hold the
/// result.
static unsigned int tcFullMultiply(integerPart *, const integerPart *,
- const integerPart *, unsigned, unsigned);
+ const integerPart *, unsigned, unsigned);
/// If RHS is zero LHS and REMAINDER are left unchanged, return one.
/// Otherwise set LHS to LHS / RHS with the fractional part
/// and are destroyed. LHS, REMAINDER and SCRATCH must be
/// distinct.
static int tcDivide(integerPart *lhs, const integerPart *rhs,
- integerPart *remainder, integerPart *scratch,
- unsigned int parts);
+ integerPart *remainder, integerPart *scratch,
+ unsigned int parts);
/// Shift a bignum left COUNT bits. Shifted in bits are zero.
/// There are no restrictions on COUNT.
static void tcShiftLeft(integerPart *, unsigned int parts,
- unsigned int count);
+ unsigned int count);
/// Shift a bignum right COUNT bits. Shifted in bits are zero.
/// There are no restrictions on COUNT.
static void tcShiftRight(integerPart *, unsigned int parts,
- unsigned int count);
+ unsigned int count);
/// The obvious AND, OR and XOR and complement operations.
static void tcAnd(integerPart *, const integerPart *, unsigned int);
/// Comparison (unsigned) of two bignums.
static int tcCompare(const integerPart *, const integerPart *,
- unsigned int);
+ unsigned int);
/// Increment a bignum in-place. Return the carry flag.
static integerPart tcIncrement(integerPart *, unsigned int);
/// Set the least significant BITS and clear the rest.
static void tcSetLeastSignificantBits(integerPart *, unsigned int,
- unsigned int bits);
+ unsigned int bits);
/// @brief debug method
void dump() const;
/// @returns true if the argument APInt value is a sequence of ones
/// starting at the least significant bit with the remainder zero.
inline bool isMask(uint32_t numBits, const APInt& APIVal) {
- return APIVal.getBoolValue() && ((APIVal + APInt(numBits,1)) & APIVal) == 0;
+ return numBits <= APIVal.getBitWidth() &&
+ APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
}
/// @returns true if the argument APInt value contains a sequence of ones
}
/// GreatestCommonDivisor - This function returns the greatest common
-/// divisor of the two APInt values using Enclid's algorithm.
+/// divisor of the two APInt values using Euclid's algorithm.
/// @returns the greatest common divisor of Val1 and Val2
/// @brief Compute GCD of two APInt values.
APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);