16 #ifndef CIRCT_SUPPORT_FVINT_H
17 #define CIRCT_SUPPORT_FVINT_H
20 #include "llvm/ADT/APInt.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Support/raw_ostream.h"
26 template <
typename T,
typename Enable>
43 FVInt(
unsigned numBits, uint64_t
value,
bool isSigned =
false)
55 FVInt(APInt &&rawValue, APInt &&rawUnknown)
57 assert(rawValue.getBitWidth() == rawUnknown.getBitWidth());
62 return FVInt(APInt::getZero(numBits));
67 return FVInt(APInt::getAllOnes(numBits));
72 return FVInt(APInt::getZero(numBits), APInt::getAllOnes(numBits));
77 return FVInt(APInt::getAllOnes(numBits), APInt::getAllOnes(numBits));
89 return std::max(
value.getActiveBits(),
unknown.getActiveBits());
99 return std::max(
value.getSignificantBits(),
unknown.getSignificantBits());
113 if (unknownBitMapping)
200 value.setBitVal(index, (bit >> 0) & 1);
201 unknown.setBitVal(index, (bit >> 1) & 1);
224 template <
typename T>
231 template <
typename T>
238 template <
typename T>
245 template <
typename T>
253 value.clearAllBits();
265 value.clearAllBits();
317 template <
typename T>
368 template <
typename T>
377 template <
typename T>
404 template <
typename T>
413 template <
typename T>
438 template <
typename T>
446 template <
typename T>
481 template <
typename T>
489 template <
typename T>
506 template <
typename T>
514 template <
typename T>
531 template <
typename T>
539 template <
typename T>
561 template <
typename T>
571 template <
typename T>
573 return !((*this) == other);
590 static std::optional<FVInt>
tryFromString(StringRef str,
unsigned radix = 10);
596 assert(v.has_value() &&
"string is not a well-formed FVInt");
611 bool tryToString(SmallVectorImpl<char> &str,
unsigned radix = 10,
612 bool uppercase =
true)
const;
616 SmallString<16>
toString(
unsigned radix = 10,
bool uppercase =
true)
const {
620 assert(success &&
"radix cannot represent FVInt");
625 void print(raw_ostream &os)
const;
668 void printFVInt(AsmPrinter &p,
const FVInt &value);
685 ParseResult
parseFVInt(AsmParser &p, FVInt &result);
assert(baseType &&"element must be base type")
Four-valued arbitrary precision integers.
void setAllXIfAnyUnknown()
If any bits are X or Z, set the entire integer to X.
void setAllOne()
Set all bits to 1.
FVInt & operator^=(const FVInt &other)
Compute the logical XOR of this integer and another.
FVInt(APInt &&rawValue, APInt &&rawUnknown)
Construct an FVInt from two APInts used internally to store the bit data.
static FVInt getZero(unsigned numBits)
Construct an FVInt with all bits set to 0.
FVInt & operator|=(T other)
Compute the logical OR of this integer and a two-valued integer.
void setBit(unsigned index, bool val)
FVInt & operator<<=(const T &amount)
Perform a logical left-shift by a two-valued amount.
FVInt operator&(const T &other) const
Compute the logical AND.
void negate()
Compute the negation of this integer.
Bit
The value of an individual bit. Can be 0, 1, X, or Z.
void setAllZ()
Set all bits to Z.
FVInt(const APInt &value)
Construct an FVInt from an APInt. The result has no X or Z bits.
FVInt & operator&=(T other)
Compute the logical AND of this integer and a two-valued integer.
bool operator==(const T &other) const
Determine whether this integer is equal to a two-valued integer.
APInt getOneBits() const
Compute a mask of all the 1 bits in this integer.
Bit getBit(unsigned index) const
Get the value of an individual bit.
FVInt & operator+=(const T &other)
Compute the addition of this integer and a two-valued integer.
FVInt()
Default constructor that creates an zero-bit zero value.
FVInt & operator-=(const FVInt &other)
Compute the subtraction of this integer and another.
APInt getXBits() const
Compute a mask of all the X bits in this integer.
static FVInt getAllZ(unsigned numBits)
Construct an FVInt with all bits set to Z.
FVInt sextOrTrunc(unsigned bitWidth) const
Truncate or sign-extend to a target bit width.
void setAllXIfAnyUnknown(const FVInt &other)
If any bits in this integer or another integer are X or Z, set the entire integer to X.
bool operator==(const FVInt &other) const
Determine whether this integer is equal to another.
void setAllZero()
Set all bits to 0.
FVInt zextOrTrunc(unsigned bitWidth) const
Truncate or zero-extend to a target bit width.
APInt getZeroBits() const
Compute a mask of all the 0 bits in this integer.
FVInt operator|(const T &other) const
Compute the logical OR.
bool isNegative() const
Determine whether the integer interpreted as a signed number would be negative.
FVInt & operator<<=(const FVInt &amount)
Perform a logical left-shift.
FVInt & operator*=(const T &other)
Compute the multiplication of this integer and a two-valued integer.
FVInt operator*(const T &other) const
Compute a multiplication.
FVInt operator-() const
Compute the negation of this integer.
FVInt operator^(const T &other) const
Compute the logical XOR.
void setZeroBits(const T &mask)
Set the value of all bits in the mask to 0.
FVInt & operator+=(const FVInt &other)
Compute the addition of this integer and another.
bool isAllZ() const
Determine if all bits are Z. This is true for zero-width values.
FVInt & operator|=(const FVInt &other)
Compute the logical OR of this integer and another.
void setAllX()
Set all bits to X.
void replaceZWithX()
Replace all Z bits with X.
FVInt & operator&=(const FVInt &other)
Compute the logical AND of this integer and another.
void setOneBits(const T &mask)
Set the value of all bits in the mask to 1.
static std::optional< FVInt > tryFromString(StringRef str, unsigned radix=10)
Convert a string into an FVInt.
FVInt & operator*=(const FVInt &other)
Compute the multiplication of this integer and another.
bool isAllX() const
Determine if all bits are X. This is true for zero-width values.
FVInt operator-(const T &other) const
Compute an subtraction.
APInt getZBits() const
Compute a mask of all the Z bits in this integer.
static FVInt fromString(StringRef str, unsigned radix=10)
Convert a string into an FVInt.
static FVInt getAllOnes(unsigned numBits)
Construct an FVInt with all bits set to 1.
FVInt operator+(const T &other) const
Compute an addition.
const APInt & getRawUnknown() const
Return the underlying APInt used to store whether a bit is unknown (X or Z).
bool operator!=(const T &other) const
Determine whether this integer is not equal to a two-valued integer.
FVInt sext(unsigned bitWidth) const
Sign-extend the integer to a new bit width.
unsigned getSignificantBits() const
Compute the minimum bit width necessary to accurately represent this integer's value and sign.
void flipAllBits()
Compute the logical NOT of this integer.
SmallString< 16 > toString(unsigned radix=10, bool uppercase=true) const
Convert an FVInt to a string.
void setXBits(const T &mask)
Set the value of all bits in the mask to X.
static FVInt getAllX(unsigned numBits)
Construct an FVInt with all bits set to X.
void print(raw_ostream &os) const
Print an FVInt to an output stream.
void setBit(unsigned index, Bit bit)
Set the value of an individual bit.
bool hasUnknown() const
Determine if any bits are X or Z.
APInt toAPInt(bool unknownBitMapping) const
Convert the four-valued FVInt to a two-valued APInt by mapping X and Z bits to either 0 or 1.
FVInt & operator^=(const T &other)
Compute the logical XOR of this integer and a two-valued integer.
unsigned getActiveBits() const
Compute the number of active bits in the value.
bool isAllOnes() const
Determine if all bits are 1. This is true for zero-width values.
FVInt operator~() const
Compute the logical NOT.
FVInt zext(unsigned bitWidth) const
Zero-extend the integer to a new bit width.
FVInt & operator-=(const T &other)
Compute the subtraction of this integer and a two-valued integer.
APInt getUnknownBits() const
Compute a mask of all the X and Z bits in this integer.
unsigned getBitWidth() const
Return the number of bits this integer has.
bool isZero() const
Determine if all bits are 0. This is true for zero-width values.
FVInt(unsigned numBits, uint64_t value, bool isSigned=false)
Construct an FVInt from a 64-bit value. The result has no X or Z bits.
void setZBits(const T &mask)
Set the value of all bits in the mask to Z.
const APInt & getRawValue() const
Return the underlying APInt used to store whether a bit is 0/X or 1/Z.
bool tryToString(SmallVectorImpl< char > &str, unsigned radix=10, bool uppercase=true) const
Convert an FVInt to a string.
bool operator!=(const FVInt &other) const
Determine whether this integer is not equal to another.
FVInt trunc(unsigned bitWidth) const
Truncate the integer to a smaller bit width.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
FVInt operator^(uint64_t a, const FVInt &b)
FVInt operator|(uint64_t a, const FVInt &b)
inline ::llvm::hash_code hash_value(const FieldRef &fieldRef)
Get a hash code for a FieldRef.
FVInt operator*(uint64_t a, const FVInt &b)
void printFVInt(AsmPrinter &p, const FVInt &value)
Print a four-valued integer usign an AsmPrinter.
ParseResult parseFVInt(AsmParser &p, FVInt &result)
Parse a four-valued integer using an AsmParser.
FVInt operator&(uint64_t a, const FVInt &b)
FVInt operator-(uint64_t a, const FVInt &b)
bool operator==(uint64_t a, const FVInt &b)
bool operator!=(uint64_t a, const FVInt &b)
raw_ostream & operator<<(raw_ostream &os, const FVInt &value)
FVInt operator+(uint64_t a, const FVInt &b)
static circt::FVInt getEmptyKey()
static unsigned getHashValue(const circt::FVInt &Key)
static bool isEqual(const circt::FVInt &LHS, const circt::FVInt &RHS)
static circt::FVInt getTombstoneKey()