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"
26template <
typename T,
typename Enable>
43 FVInt(
unsigned numBits, uint64_t
value,
bool isSigned =
false)
59 FVInt(APInt &&rawValue, APInt &&rawUnknown)
61 assert(rawValue.getBitWidth() == rawUnknown.getBitWidth());
66 return FVInt(APInt::getZero(numBits));
71 return FVInt(APInt::getAllOnes(numBits));
76 return FVInt(APInt::getZero(numBits), APInt::getAllOnes(numBits));
81 return FVInt(APInt::getAllOnes(numBits), APInt::getAllOnes(numBits));
93 return std::max(
value.getActiveBits(),
unknown.getActiveBits());
103 return std::max(
value.getSignificantBits(),
unknown.getSignificantBits());
117 if (unknownBitMapping)
204 value.setBitVal(index, (bit >> 0) & 1);
205 unknown.setBitVal(index, (bit >> 1) & 1);
228 template <
typename T>
235 template <
typename T>
242 template <
typename T>
249 template <
typename T>
257 value.clearAllBits();
269 value.clearAllBits();
321 template <
typename T>
372 template <
typename T>
381 template <
typename T>
408 template <
typename T>
417 template <
typename T>
442 template <
typename T>
450 template <
typename T>
485 template <
typename T>
493 template <
typename T>
510 template <
typename T>
518 template <
typename T>
535 template <
typename T>
543 template <
typename T>
562 return value.udiv(other);
577 return value.sdiv(other);
595 template <
typename T>
605 template <
typename T>
607 return !((*this) == other);
624 static std::optional<FVInt>
tryFromString(StringRef str,
unsigned radix = 10);
630 assert(v.has_value() &&
"string is not a well-formed FVInt");
645 bool tryToString(SmallVectorImpl<char> &str,
unsigned radix = 10,
646 bool uppercase =
true)
const;
650 SmallString<16>
toString(
unsigned radix = 10,
bool uppercase =
true)
const {
654 assert(success &&
"radix cannot represent FVInt");
659 void print(raw_ostream &os)
const;
702void printFVInt(AsmPrinter &p,
const FVInt &value);
719ParseResult
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 T &other)
Compute the multiplication of this integer and a two-valued integer.
FVInt sdiv(int64_t other) const
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.
FVInt & operator&=(T other)
Compute the logical AND of this integer and a two-valued integer.
void setBit(unsigned index, bool val)
FVInt & operator*=(const FVInt &other)
Compute the multiplication of this integer and another.
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 & operator+=(const FVInt &other)
Compute the addition of this integer and another.
FVInt(const APInt &value)
Construct an FVInt from an APInt. The result has no X or Z bits.
SmallString< 16 > toString(unsigned radix=10, bool uppercase=true) const
Convert an FVInt to a string.
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 logical XOR of this integer and a two-valued integer.
FVInt()
Default constructor that creates an zero-bit zero value.
FVInt sdiv(const FVInt &other) const
Compute a signed division.
FVInt udiv(const FVInt &other) const
Compute an unsigned division.
FVInt & operator<<=(const T &amount)
Perform a logical left-shift by a two-valued amount.
APInt getXBits() const
Compute a mask of all the X bits in this integer.
FVInt & operator-=(const FVInt &other)
Compute the subtraction of this integer and another.
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.
FVInt & operator^=(const FVInt &other)
Compute the logical XOR of this integer and another.
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 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 udiv(uint64_t other) const
bool isAllZ() const
Determine if all bits are Z. This is true for zero-width values.
void setAllX()
Set all bits to X.
void replaceZWithX()
Replace all Z bits with X.
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 logical OR of this integer and another.
const APInt & getRawValue() const
Return the underlying APInt used to store whether a bit is 0/X or 1/Z.
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.
FVInt & operator<<=(const FVInt &amount)
Perform a logical left-shift.
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 & operator&=(const FVInt &other)
Compute the logical AND of this integer and another.
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.
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.
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.
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.
FVInt & operator+=(const T &other)
Compute the addition of this integer and a two-valued integer.
bool isZero() const
Determine if all bits are 0. This is true for zero-width values.
FVInt(APInt &&value)
Construct an FVInt from an APInt. The result has no X or Z bits.
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.
FVInt & operator-=(const T &other)
Compute the subtraction of this integer and a two-valued integer.
void setZBits(const T &mask)
Set the value of all bits in the mask to 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.
OS & operator<<(OS &os, const InnerSymTarget &target)
Printing InnerSymTarget's.
static bool operator==(const ModulePort &a, const ModulePort &b)
static llvm::hash_code hash_value(const ModulePort &port)
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)
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)
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()