14llvm::APSInt
expandingAdd(
const llvm::APSInt &lhs,
const llvm::APSInt &rhs) {
15 unsigned requiredBits = std::max(lhs.getActiveBits(), rhs.getActiveBits()) + 1;
16 unsigned newBitwidth = std::max({requiredBits, lhs.getBitWidth(), rhs.getBitWidth()});
17 return lhs.extend(newBitwidth) + rhs.extend(newBitwidth);
20llvm::APSInt
expandingSub(
const llvm::APSInt &lhs,
const llvm::APSInt &rhs) {
21 unsigned requiredBits = std::max(lhs.getActiveBits(), rhs.getActiveBits()) + 1;
22 unsigned newBitwidth = std::max({requiredBits, lhs.getBitWidth(), rhs.getBitWidth()});
23 return lhs.extend(newBitwidth) - rhs.extend(newBitwidth);
26llvm::APSInt
expandingMul(
const llvm::APSInt &lhs,
const llvm::APSInt &rhs) {
27 unsigned requiredBits = lhs.getActiveBits() + rhs.getActiveBits();
28 unsigned newBitwidth = std::max({requiredBits, lhs.getBitWidth(), rhs.getBitWidth()});
29 return lhs.extend(newBitwidth) * rhs.extend(newBitwidth);
32std::strong_ordering
safeCmp(
const llvm::APSInt &lhs,
const llvm::APSInt &rhs) {
33 unsigned requiredBits = std::max(lhs.getBitWidth(), rhs.getBitWidth());
34 auto a = lhs.extend(requiredBits), b = rhs.extend(requiredBits);
36 return std::strong_ordering::less;
38 return std::strong_ordering::greater;
40 return std::strong_ordering::equal;
This file defines helpers for manipulating APInts/APSInts for large numbers and operations over those...
llvm::APSInt expandingSub(const llvm::APSInt &lhs, const llvm::APSInt &rhs)
Safely subtract lhs and rhs, expanding the width of the result as necessary.
std::strong_ordering safeCmp(const llvm::APSInt &lhs, const llvm::APSInt &rhs)
Compares lhs and rhs, regardless of the bitwidth of lhs and rhs.
llvm::APSInt expandingAdd(const llvm::APSInt &lhs, const llvm::APSInt &rhs)
Safely add lhs and rhs, expanding the width of the result as necessary.
llvm::APSInt expandingMul(const llvm::APSInt &lhs, const llvm::APSInt &rhs)
Safely multiple lhs and rhs, expanding the width of the result as necessary.