CIRCT
20.0.0git
|
Four-valued arbitrary precision integers. More...
#include <FVInt.h>
Public Types | |
enum | Bit { V0 = 0b00 , V1 = 0b01 , X = 0b10 , Z = 0b11 } |
The value of an individual bit. Can be 0, 1, X, or Z. More... | |
Public Member Functions | |
FVInt () | |
Default constructor that creates an zero-bit zero value. More... | |
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. More... | |
FVInt (const APInt &value) | |
Construct an FVInt from an APInt . The result has no X or Z bits. More... | |
FVInt (APInt &&rawValue, APInt &&rawUnknown) | |
Construct an FVInt from two APInt s used internally to store the bit data. More... | |
unsigned | getBitWidth () const |
Return the number of bits this integer has. More... | |
unsigned | getActiveBits () const |
Compute the number of active bits in the value. More... | |
unsigned | getSignificantBits () const |
Compute the minimum bit width necessary to accurately represent this integer's value and sign. More... | |
const APInt & | getRawValue () const |
Return the underlying APInt used to store whether a bit is 0/X or 1/Z. More... | |
const APInt & | getRawUnknown () const |
Return the underlying APInt used to store whether a bit is unknown (X or Z). More... | |
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. More... | |
FVInt | trunc (unsigned bitWidth) const |
Truncate the integer to a smaller bit width. More... | |
FVInt | zext (unsigned bitWidth) const |
Zero-extend the integer to a new bit width. More... | |
FVInt | sext (unsigned bitWidth) const |
Sign-extend the integer to a new bit width. More... | |
FVInt | zextOrTrunc (unsigned bitWidth) const |
Truncate or zero-extend to a target bit width. More... | |
FVInt | sextOrTrunc (unsigned bitWidth) const |
Truncate or sign-extend to a target bit width. More... | |
bool | hasUnknown () const |
Determine if any bits are X or Z. More... | |
bool | isZero () const |
Determine if all bits are 0. This is true for zero-width values. More... | |
bool | isAllOnes () const |
Determine if all bits are 1. This is true for zero-width values. More... | |
bool | isAllX () const |
Determine if all bits are X. This is true for zero-width values. More... | |
bool | isAllZ () const |
Determine if all bits are Z. This is true for zero-width values. More... | |
bool | isNegative () const |
Determine whether the integer interpreted as a signed number would be negative. More... | |
Bit | getBit (unsigned index) const |
Get the value of an individual bit. More... | |
void | setBit (unsigned index, Bit bit) |
Set the value of an individual bit. More... | |
void | setBit (unsigned index, bool val) |
APInt | getZeroBits () const |
Compute a mask of all the 0 bits in this integer. More... | |
APInt | getOneBits () const |
Compute a mask of all the 1 bits in this integer. More... | |
APInt | getXBits () const |
Compute a mask of all the X bits in this integer. More... | |
APInt | getZBits () const |
Compute a mask of all the Z bits in this integer. More... | |
APInt | getUnknownBits () const |
Compute a mask of all the X and Z bits in this integer. More... | |
template<typename T > | |
void | setZeroBits (const T &mask) |
Set the value of all bits in the mask to 0. More... | |
template<typename T > | |
void | setOneBits (const T &mask) |
Set the value of all bits in the mask to 1. More... | |
template<typename T > | |
void | setXBits (const T &mask) |
Set the value of all bits in the mask to X. More... | |
template<typename T > | |
void | setZBits (const T &mask) |
Set the value of all bits in the mask to Z. More... | |
void | setAllZero () |
Set all bits to 0. More... | |
void | setAllOne () |
Set all bits to 1. More... | |
void | setAllX () |
Set all bits to X. More... | |
void | setAllZ () |
Set all bits to Z. More... | |
void | replaceZWithX () |
Replace all Z bits with X. More... | |
void | setAllXIfAnyUnknown () |
If any bits are X or Z, set the entire integer to X. More... | |
void | setAllXIfAnyUnknown (const FVInt &other) |
If any bits in this integer or another integer are X or Z, set the entire integer to X. More... | |
FVInt & | operator<<= (const FVInt &amount) |
Perform a logical left-shift. More... | |
template<typename T > | |
FVInt & | operator<<= (const T &amount) |
Perform a logical left-shift by a two-valued amount. More... | |
void | flipAllBits () |
Compute the logical NOT of this integer. More... | |
FVInt | operator~ () const |
Compute the logical NOT. More... | |
FVInt & | operator&= (const FVInt &other) |
Compute the logical AND of this integer and another. More... | |
template<typename T > | |
FVInt & | operator&= (T other) |
Compute the logical AND of this integer and a two-valued integer. More... | |
template<typename T > | |
FVInt | operator& (const T &other) const |
Compute the logical AND. More... | |
FVInt & | operator|= (const FVInt &other) |
Compute the logical OR of this integer and another. More... | |
template<typename T > | |
FVInt & | operator|= (T other) |
Compute the logical OR of this integer and a two-valued integer. More... | |
template<typename T > | |
FVInt | operator| (const T &other) const |
Compute the logical OR. More... | |
FVInt & | operator^= (const FVInt &other) |
Compute the logical XOR of this integer and another. More... | |
template<typename T > | |
FVInt & | operator^= (const T &other) |
Compute the logical XOR of this integer and a two-valued integer. More... | |
template<typename T > | |
FVInt | operator^ (const T &other) const |
Compute the logical XOR. More... | |
void | negate () |
Compute the negation of this integer. More... | |
FVInt | operator- () const |
Compute the negation of this integer. More... | |
FVInt & | operator+= (const FVInt &other) |
Compute the addition of this integer and another. More... | |
template<typename T > | |
FVInt & | operator+= (const T &other) |
Compute the addition of this integer and a two-valued integer. More... | |
template<typename T > | |
FVInt | operator+ (const T &other) const |
Compute an addition. More... | |
FVInt & | operator-= (const FVInt &other) |
Compute the subtraction of this integer and another. More... | |
template<typename T > | |
FVInt & | operator-= (const T &other) |
Compute the subtraction of this integer and a two-valued integer. More... | |
template<typename T > | |
FVInt | operator- (const T &other) const |
Compute an subtraction. More... | |
FVInt & | operator*= (const FVInt &other) |
Compute the multiplication of this integer and another. More... | |
template<typename T > | |
FVInt & | operator*= (const T &other) |
Compute the multiplication of this integer and a two-valued integer. More... | |
template<typename T > | |
FVInt | operator* (const T &other) const |
Compute a multiplication. More... | |
bool | operator== (const FVInt &other) const |
Determine whether this integer is equal to another. More... | |
template<typename T > | |
bool | operator== (const T &other) const |
Determine whether this integer is equal to a two-valued integer. More... | |
bool | operator!= (const FVInt &other) const |
Determine whether this integer is not equal to another. More... | |
template<typename T > | |
bool | operator!= (const T &other) const |
Determine whether this integer is not equal to a two-valued integer. More... | |
bool | tryToString (SmallVectorImpl< char > &str, unsigned radix=10, bool uppercase=true) const |
Convert an FVInt to a string. More... | |
SmallString< 16 > | toString (unsigned radix=10, bool uppercase=true) const |
Convert an FVInt to a string. More... | |
void | print (raw_ostream &os) const |
Print an FVInt to an output stream. More... | |
Static Public Member Functions | |
static FVInt | getZero (unsigned numBits) |
Construct an FVInt with all bits set to 0. More... | |
static FVInt | getAllOnes (unsigned numBits) |
Construct an FVInt with all bits set to 1. More... | |
static FVInt | getAllX (unsigned numBits) |
Construct an FVInt with all bits set to X. More... | |
static FVInt | getAllZ (unsigned numBits) |
Construct an FVInt with all bits set to Z. More... | |
static std::optional< FVInt > | tryFromString (StringRef str, unsigned radix=10) |
Convert a string into an FVInt . More... | |
static FVInt | fromString (StringRef str, unsigned radix=10) |
Convert a string into an FVInt . More... | |
Private Attributes | |
APInt | value |
APInt | unknown |
Four-valued arbitrary precision integers.
Each bit of the integer can be 0, 1, X, or Z. Internally the bits are stored in a pair of APInt
s, one of which specifies the value of each bit (0/X or 1/Z), and the other whether the bit is unknown (X or Z).
enum circt::FVInt::Bit |
|
inlineexplicit |
|
inline |
|
inline |
|
inline |
Construct an FVInt
from two APInt
s used internally to store the bit data.
The first argument specifies whether each bit is 0/X or 1/Z. The second argument specifies whether each bit is 0/1 or X/Z. Both APInt
s must have the same bitwidth. The two arguments correspond to the results of getRawValue()
and getRawUnknown()
.
Definition at line 55 of file FVInt.h.
References assert().
|
inline |
Compute the logical NOT of this integer.
This implements the following bit-wise truth table:
Definition at line 336 of file FVInt.h.
References replaceZWithX(), and value.
Referenced by operator~().
|
inlinestatic |
Convert a string into an FVInt
.
Same as tryFromString
, but aborts if the string is malformed.
Definition at line 594 of file FVInt.h.
References assert(), and tryFromString().
|
inline |
Compute the number of active bits in the value.
This is the smallest bit width to which the value can be truncated without losing information in the most significant bits. Or put differently, the value truncated to its active bits and zero-extended back to its original width produces the original value.
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inline |
|
inline |
Return the number of bits this integer has.
Definition at line 81 of file FVInt.h.
References value.
Referenced by isNegative(), circt::operator-(), operator==(), circt::printFVInt(), sext(), sextOrTrunc(), trunc(), tryToString(), zext(), and zextOrTrunc().
|
inline |
Compute a mask of all the 1 bits in this integer.
Definition at line 212 of file FVInt.h.
References unknown, and value.
Referenced by operator|=().
|
inline |
Return the underlying APInt
used to store whether a bit is unknown (X or Z).
Definition at line 107 of file FVInt.h.
References unknown.
Referenced by circt::hash_value().
|
inline |
Return the underlying APInt
used to store whether a bit is 0/X or 1/Z.
Definition at line 103 of file FVInt.h.
References value.
Referenced by circt::hash_value().
|
inline |
Compute the minimum bit width necessary to accurately represent this integer's value and sign.
This is the smallest bit width to which the value can be truncated without losing information in the most significant bits and without flipping from negative to positive or vice versa. Or put differently, the value truncated to its significant bits and sign-extended back to its original width produces the original value.
|
inline |
|
inline |
|
inline |
|
inlinestatic |
Construct an FVInt
with all bits set to 0.
Definition at line 61 of file FVInt.h.
References FVInt().
Referenced by tryFromString().
|
inline |
Compute a mask of all the 0 bits in this integer.
Definition at line 209 of file FVInt.h.
References unknown, and value.
Referenced by operator&=().
|
inline |
Determine if any bits are X or Z.
Definition at line 164 of file FVInt.h.
References unknown.
Referenced by operator<<=(), operator==(), setAllXIfAnyUnknown(), and tryToString().
|
inline |
|
inline |
|
inline |
|
inline |
Determine whether the integer interpreted as a signed number would be negative.
Returns true if the sign bit is 1, and false if it is 0, X, or Z.
Definition at line 181 of file FVInt.h.
References getBitWidth(), unknown, and value.
Referenced by circt::printFVInt().
|
inline |
|
inline |
Compute the negation of this integer.
If any bits are unknown, the entire result is X.
Definition at line 459 of file FVInt.h.
References setAllXIfAnyUnknown(), and value.
Referenced by operator-().
|
inline |
|
inline |
|
inline |
|
inline |
Compute the logical AND of this integer and a two-valued integer.
Definition at line 369 of file FVInt.h.
References replaceZWithX(), unknown, and value.
|
inline |
Compute the multiplication of this integer and another.
If any bits in either integer are unknown, the entire result is X.
Definition at line 523 of file FVInt.h.
References setAllXIfAnyUnknown(), and value.
|
inline |
Compute the multiplication of this integer and a two-valued integer.
If any bit in the integer is unknown, the entire result is X.
Definition at line 532 of file FVInt.h.
References setAllXIfAnyUnknown(), and value.
|
inline |
Compute the addition of this integer and another.
If any bits in either integer are unknown, the entire result is X.
Definition at line 473 of file FVInt.h.
References setAllXIfAnyUnknown(), and value.
|
inline |
Compute the addition of this integer and a two-valued integer.
If any bit in the integer is unknown, the entire result is X.
Definition at line 482 of file FVInt.h.
References setAllXIfAnyUnknown(), and value.
|
inline |
|
inline |
Compute the subtraction of this integer and another.
If any bits in either integer are unknown, the entire result is X.
Definition at line 498 of file FVInt.h.
References setAllXIfAnyUnknown(), and value.
|
inline |
Compute the subtraction of this integer and a two-valued integer.
If any bit in the integer is unknown, the entire result is X.
Definition at line 507 of file FVInt.h.
References setAllXIfAnyUnknown(), and value.
|
inline |
|
inline |
Determine whether this integer is equal to another.
Note that this corresponds to SystemVerilog's ===
operator. Returns false if the two integers have different bit width.
Definition at line 553 of file FVInt.h.
References getBitWidth(), unknown, and value.
|
inline |
Determine whether this integer is equal to a two-valued integer.
Note that this corresponds to SystemVerilog's ===
operator.
Definition at line 562 of file FVInt.h.
References hasUnknown(), and value.
|
inline |
|
inline |
Compute the logical XOR of this integer and a two-valued integer.
Definition at line 439 of file FVInt.h.
References replaceZWithX(), and value.
|
inline |
|
inline |
Compute the logical OR of this integer and a two-valued integer.
Definition at line 405 of file FVInt.h.
References replaceZWithX(), unknown, and value.
|
inline |
void FVInt::print | ( | raw_ostream & | os | ) | const |
Print an FVInt
to an output stream.
Definition at line 135 of file FVInt.cpp.
References tryToString().
Referenced by circt::operator<<().
|
inline |
Replace all Z bits with X.
This is useful since most logic operations will treat X and Z bits the same way and produce an X bit in the output. By mapping Z bits to X, these operations can then just handle 0, 1, and X bits.
Definition at line 279 of file FVInt.h.
References unknown, and value.
Referenced by flipAllBits(), operator&=(), operator^=(), and operator|=().
|
inline |
|
inline |
Set all bits to X.
Definition at line 264 of file FVInt.h.
References unknown, and value.
Referenced by operator<<=(), and setAllXIfAnyUnknown().
|
inline |
If any bits are X or Z, set the entire integer to X.
Definition at line 287 of file FVInt.h.
References hasUnknown(), and setAllX().
Referenced by negate(), operator*=(), operator+=(), and operator-=().
|
inline |
If any bits in this integer or another integer are X or Z, set the entire integer to X.
This is useful for binary operators which want to set their result to X if either of the two inputs contained an X or Z bit.
Definition at line 295 of file FVInt.h.
References hasUnknown(), and setAllX().
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Sign-extend the integer to a new bit width.
The additional high-order bits are filled in with the sign bit (top-most bit) of the original number, also when that sign bit is X or Z. Zero-width integers are extended with zeros.
Definition at line 144 of file FVInt.h.
References assert(), FVInt(), getBitWidth(), unknown, and value.
Referenced by sextOrTrunc().
|
inline |
Truncate or sign-extend to a target bit width.
Definition at line 155 of file FVInt.h.
References getBitWidth(), sext(), and trunc().
|
inline |
|
inline |
Convert an FVInt
to a string.
Same as tryToString
, but directly returns the string and aborts if the conversion is unsuccessful.
Definition at line 616 of file FVInt.h.
References assert(), and tryToString().
|
inline |
Truncate the integer to a smaller bit width.
This simply discards the high-order bits. If the integer is truncated to a bit width less than its "active bits", information will be lost and the resulting integer will have a different value.
Definition at line 128 of file FVInt.h.
References assert(), FVInt(), getBitWidth(), unknown, and value.
Referenced by sextOrTrunc(), and zextOrTrunc().
|
static |
Convert a string into an FVInt
.
The radix can be 2, 8, 10, or 16. For radix 2, the input string may contain the characters x
or X
to indicate an unknown X bit, and z
or Z
to indicate an unknown Z bit. For radix 8, each X or Z counts as 3 bits. For radix 16, each X and Z counts as 4 bits. When radix is 10 the input cannot contain any X or Z.
Returns the parsed integer if the string is non-empty and a well-formed number, otherwise returns none.
Definition at line 17 of file FVInt.cpp.
References assert(), and getZero().
Referenced by fromString(), and circt::parseFVInt().
bool FVInt::tryToString | ( | SmallVectorImpl< char > & | str, |
unsigned | radix = 10 , |
||
bool | uppercase = true |
||
) | const |
Convert an FVInt
to a string.
The radix can be 2, 8, 10, or 16. For radix 8 or 16, the integer can only contain unknown bits in groups of 3 or 4, respectively, such that a X
or Z
can be printed for the entire group of bits. For radix 10, the integer cannot contain any unknown bits. In case the output contains letters, uppercase
specifies whether they are printed as uppercase letters.
Appends the output characters to str
and returns true if the integer could be printed with the given configuration. Otherwise returns false and leaves str
in its original state. Always succeeds for radix 2.
Definition at line 68 of file FVInt.cpp.
References assert(), getBitWidth(), hasUnknown(), unknown, and value.
Referenced by print(), circt::printFVInt(), and toString().
|
inline |
Zero-extend the integer to a new bit width.
The additional high-order bits are filled in with zero.
Definition at line 135 of file FVInt.h.
References assert(), FVInt(), getBitWidth(), unknown, and value.
Referenced by circt::parseFVInt(), and zextOrTrunc().
|
inline |
Truncate or zero-extend to a target bit width.
Definition at line 150 of file FVInt.h.
References getBitWidth(), trunc(), and zext().
|
private |
Definition at line 628 of file FVInt.h.
Referenced by getActiveBits(), getBit(), getOneBits(), getRawUnknown(), getSignificantBits(), getUnknownBits(), getXBits(), getZBits(), getZeroBits(), hasUnknown(), isAllOnes(), isAllX(), isAllZ(), isNegative(), isZero(), operator&=(), operator<<=(), operator==(), operator^=(), operator|=(), replaceZWithX(), setAllOne(), setAllX(), setAllZ(), setAllZero(), setBit(), setOneBits(), setXBits(), setZBits(), setZeroBits(), sext(), toAPInt(), trunc(), tryToString(), and zext().
|
private |
Definition at line 627 of file FVInt.h.
Referenced by flipAllBits(), getActiveBits(), getBit(), getBitWidth(), getOneBits(), getRawValue(), getSignificantBits(), getXBits(), getZBits(), getZeroBits(), isAllOnes(), isAllX(), isAllZ(), isNegative(), isZero(), negate(), operator&=(), operator*=(), operator+=(), operator-=(), operator<<=(), operator==(), operator^=(), operator|=(), replaceZWithX(), setAllOne(), setAllX(), setAllZ(), setAllZero(), setBit(), setOneBits(), setXBits(), setZBits(), setZeroBits(), sext(), toAPInt(), trunc(), tryToString(), Python.support.OpOperand::type(), and zext().