12#include <llvm/ADT/ArrayRef.h>
13#include <llvm/Support/raw_ostream.h>
20static DynamicAPInt po2(
const DynamicAPInt &e) {
23 assert(e <= std::numeric_limits<unsigned>::max() );
25 APSInt p = APSInt::get(1) << shiftAmt;
29static DynamicAPInt fromBigEndian(
const std::vector<bool> &bits) {
30 APSInt rawInt(bits.size(),
false);
31 for (
unsigned i = 0; i < bits.size(); ++i) {
32 rawInt.setBitVal(i, bits[i]);
38binaryBitOp(
const DynamicAPInt &lhs,
const DynamicAPInt &rhs, function_ref<
bool(
bool,
bool)> fn) {
39 DynamicAPInt a = lhs, b = rhs;
40 std::vector<bool> bits;
41 while (a != 0 || b != 0) {
43 bool abit = a != 0 ? bool(int64_t(a % 2)) : lhs < 0;
44 bool bbit = b != 0 ? bool(int64_t(b % 2)) : rhs < 0;
45 bits.push_back(fn(abit, bbit));
51 bits.push_back(fn(lhs < 0, rhs < 0));
52 return fromBigEndian(bits);
57DynamicAPInt
operator&(
const DynamicAPInt &lhs,
const DynamicAPInt &rhs) {
58 auto fn = [](
bool a,
bool b) {
return a && b; };
59 return binaryBitOp(lhs, rhs, fn);
62DynamicAPInt
operator|(
const DynamicAPInt &lhs,
const DynamicAPInt &rhs) {
63 auto fn = [](
bool a,
bool b) {
return a || b; };
64 return binaryBitOp(lhs, rhs, fn);
67DynamicAPInt
operator^(
const DynamicAPInt &lhs,
const DynamicAPInt &rhs) {
68 auto fn = [](
bool a,
bool b) {
return a ^ b; };
69 return binaryBitOp(lhs, rhs, fn);
72DynamicAPInt
operator<<(
const DynamicAPInt &lhs,
const DynamicAPInt &rhs) {
return lhs * po2(rhs); }
74DynamicAPInt
operator>>(
const DynamicAPInt &lhs,
const DynamicAPInt &rhs) {
76 return lhs / po2(rhs);
79 DynamicAPInt divisor = po2(rhs);
80 if (lhs % divisor == 0) {
83 return (lhs - (divisor - 1)) / divisor;
89 APSInt parsedInt(str);
94 if (i.getBitWidth() <= 64) {
96 return DynamicAPInt(i.isNegative() ? i.getSExtValue() : i.getZExtValue());
99 DynamicAPInt res(0), po2(1);
103 APSInt raw = i < 0 ? -i : i;
104 for (
unsigned b = 0; b < raw.getActiveBits(); b++) {
105 DynamicAPInt bitSet(raw[b]);
106 res += (bitSet * po2);
109 if (i.isNegative() && res > 0) {
116 if (numeric_limits<int64_t>::min() <= i && i <= numeric_limits<int64_t>::max()) {
118 return APSInt::get(int64_t(i));
125 llvm::raw_string_ostream ss(repr);
131 res = res.extend(res.getBitWidth() + 1);
132 res.setIsSigned(
true);
This file implements helper methods for constructing DynamicAPInts.
DynamicAPInt toDynamicAPInt(StringRef str)
Interval operator<<(const Interval &lhs, const Interval &rhs)
DynamicAPInt operator|(const DynamicAPInt &lhs, const DynamicAPInt &rhs)
Interval operator>>(const Interval &lhs, const Interval &rhs)
Interval operator&(const Interval &lhs, const Interval &rhs)
APSInt toAPSInt(const DynamicAPInt &i)
DynamicAPInt operator^(const DynamicAPInt &lhs, const DynamicAPInt &rhs)