Skip to content

Commit

Permalink
rtlil: access Const variant alternatives directly
Browse files Browse the repository at this point in the history
  • Loading branch information
widlarizer committed Aug 2, 2024
1 parent 04d9ec6 commit 9aa6a6b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 56 deletions.
56 changes: 22 additions & 34 deletions kernel/rtlil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ RTLIL::Const::Const(int val, int width)
{
flags = RTLIL::CONST_FLAG_NONE;
backing = bitvectype();
bitvectype& bv = assert_get_bits("Const::Const(int, int)");
bitvectype& bv = std::get<bitvectype>(backing);
bv.reserve(width);
for (int i = 0; i < width; i++) {
bv.push_back((val & 1) != 0 ? State::S1 : State::S0);
Expand All @@ -222,7 +222,7 @@ RTLIL::Const::Const(RTLIL::State bit, int width)
{
flags = RTLIL::CONST_FLAG_NONE;
backing = bitvectype();
bitvectype& bv = assert_get_bits("Const::Const(State, int)");
bitvectype& bv = std::get<bitvectype>(backing);
bv.reserve(width);
for (int i = 0; i < width; i++)
bv.push_back(bit);
Expand All @@ -232,33 +232,22 @@ RTLIL::Const::Const(const std::vector<bool> &bits)
{
flags = RTLIL::CONST_FLAG_NONE;
backing = bitvectype();
bitvectype& bv = assert_get_bits("Const::Const(std::vector<bool>)");
bitvectype& bv = std::get<bitvectype>(backing);
bv.reserve(bits.size());
for (const auto &b : bits)
bv.emplace_back(b ? State::S1 : State::S0);
}

[[nodiscard]] RTLIL::Const::bitvectype& RTLIL::Const::assert_get_bits(const char* ctx) const {
// return assert_get<bitvectype, decltype(backing)>(&backing, ctx);
return std::get<bitvectype>(backing);
}

[[nodiscard]] std::string& RTLIL::Const::assert_get_str(const char* ctx) const {
// return assert_get<std::string, decltype(backing)>(&backing, ctx);
return std::get<std::string>(backing);
}

bool RTLIL::Const::operator <(const RTLIL::Const &other) const
{
const char* ctx = "operator<";
if (std::get_if<std::string>(&backing) != std::get_if<std::string>(&other.backing))
return decode_string() < other.decode_string();

if (std::get_if<std::string>(&backing))
return assert_get_str(ctx) < other.assert_get_str(ctx);
return std::get<std::string>(backing) < std::get<std::string>(other.backing);

bitvectype& bv = assert_get_bits(ctx);
auto other_bv = other.assert_get_bits(ctx);
bitvectype& bv = std::get<bitvectype>(backing);
auto other_bv = std::get<bitvectype>(other.backing);

if (bv.size() != other_bv.size())
return bv.size() < other_bv.size();
Expand All @@ -271,14 +260,13 @@ bool RTLIL::Const::operator <(const RTLIL::Const &other) const

bool RTLIL::Const::operator ==(const RTLIL::Const &other) const
{
const char* ctx = "operator==";
if (std::get_if<std::string>(&backing) != std::get_if<std::string>(&other.backing))
return decode_string() == other.decode_string();

if (std::get_if<std::string>(&backing))
return assert_get_str(ctx) == other.assert_get_str(ctx);
return std::get<std::string>(backing) == std::get<std::string>(other.backing);

return assert_get_bits(ctx) == other.assert_get_bits(ctx);
return std::get<bitvectype>(backing) == std::get<bitvectype>(other.backing);
}

bool RTLIL::Const::operator !=(const RTLIL::Const &other) const
Expand All @@ -289,13 +277,13 @@ bool RTLIL::Const::operator !=(const RTLIL::Const &other) const
std::vector<RTLIL::State>& RTLIL::Const::bits()
{
bitvectorize();
return assert_get_bits("Const::bits()");
return std::get<bitvectype>(backing);
}

const std::vector<RTLIL::State>& RTLIL::Const::bits() const
{
bitvectorize();
return assert_get_bits("Const::bits()");
return std::get<bitvectype>(backing);
}


Expand All @@ -304,7 +292,7 @@ std::vector<RTLIL::State> RTLIL::Const::to_bits() const
if (auto bv = std::get_if<bitvectype>(&backing)) {
return *bv;
}
auto str = assert_get_str("Const::to_bits");
auto& str = std::get<std::string>(backing);
bitvectype b;
b.reserve(str.size() * 8);
for (int i = str.size()-1; i >= 0; i--) {
Expand Down Expand Up @@ -334,7 +322,7 @@ std::string RTLIL::Const::pretty_fmt_undef() const {
bool RTLIL::Const::as_bool() const
{
bitvectorize();
bitvectype& bv = assert_get_bits("Const::as_bool");
bitvectype& bv = std::get<bitvectype>(backing);
for (size_t i = 0; i < bv.size(); i++)
if (bv[i] == State::S1)
return true;
Expand All @@ -344,7 +332,7 @@ bool RTLIL::Const::as_bool() const
int RTLIL::Const::as_int(bool is_signed) const
{
bitvectorize();
bitvectype& bv = assert_get_bits("Const::as_int");
bitvectype& bv = std::get<bitvectype>(backing);
int32_t ret = 0;
for (size_t i = 0; i < bv.size() && i < 32; i++)
if (bv[i] == State::S1)
Expand All @@ -358,7 +346,7 @@ int RTLIL::Const::as_int(bool is_signed) const
std::string RTLIL::Const::as_string(std::string any) const
{
bitvectorize();
bitvectype& bv = assert_get_bits("Const::as_bool");
bitvectype& bv = std::get<bitvectype>(backing);
std::string ret;
ret.reserve(bv.size());
for (size_t i = bv.size(); i > 0; i--)
Expand All @@ -377,7 +365,7 @@ RTLIL::Const RTLIL::Const::from_string(const std::string &str)
{
Const c;
c.backing = bitvectype();
bitvectype& bv = c.assert_get_bits("Const::from_string");
bitvectype& bv = std::get<bitvectype>(c.backing);
bv.reserve(str.size());
for (auto it = str.rbegin(); it != str.rend(); it++)
switch (*it) {
Expand All @@ -397,7 +385,7 @@ std::string RTLIL::Const::decode_string() const
return *str;

bitvectorize();
bitvectype& bv = assert_get_bits("Const::decode_string");
bitvectype& bv = std::get<bitvectype>(backing);
const int n = GetSize(bv);
const int n_over_8 = n / 8;
std::string s;
Expand Down Expand Up @@ -430,7 +418,7 @@ std::string RTLIL::Const::decode_string() const
bool RTLIL::Const::is_fully_zero() const
{
bitvectorize();
bitvectype& bv = assert_get_bits("Const::decode_string");
bitvectype& bv = std::get<bitvectype>(backing);
cover("kernel.rtlil.const.is_fully_zero");

for (const auto &bit : bv)
Expand All @@ -443,7 +431,7 @@ bool RTLIL::Const::is_fully_zero() const
bool RTLIL::Const::is_fully_ones() const
{
bitvectorize();
bitvectype& bv = assert_get_bits("Const::decode_string");
bitvectype& bv = std::get<bitvectype>(backing);
cover("kernel.rtlil.const.is_fully_ones");

for (const auto &bit : bv)
Expand All @@ -458,7 +446,7 @@ bool RTLIL::Const::is_fully_def() const
cover("kernel.rtlil.const.is_fully_def");

bitvectorize();
bitvectype& bv = assert_get_bits("Const::decode_string");
bitvectype& bv = std::get<bitvectype>(backing);

for (const auto &bit : bv)
if (bit != RTLIL::State::S0 && bit != RTLIL::State::S1)
Expand All @@ -472,7 +460,7 @@ bool RTLIL::Const::is_fully_undef() const
cover("kernel.rtlil.const.is_fully_undef");

bitvectorize();
bitvectype& bv = assert_get_bits("Const::decode_string");
bitvectype& bv = std::get<bitvectype>(backing);

for (const auto &bit : bv)
if (bit != RTLIL::State::Sx && bit != RTLIL::State::Sz)
Expand All @@ -486,7 +474,7 @@ bool RTLIL::Const::is_fully_undef_x_only() const
cover("kernel.rtlil.const.is_fully_undef_x_only");

bitvectorize();
bitvectype& bv = assert_get_bits("Const::decode_string");
bitvectype& bv = std::get<bitvectype>(backing);

for (const auto &bit : bv)
if (bit != RTLIL::State::Sx)
Expand All @@ -500,7 +488,7 @@ bool RTLIL::Const::is_onehot(int *pos) const
cover("kernel.rtlil.const.is_onehot");

bitvectorize();
bitvectype& bv = assert_get_bits("Const::decode_string");
bitvectype& bv = std::get<bitvectype>(backing);

bool found = false;
for (int i = 0; i < GetSize(*this); i++) {
Expand Down
33 changes: 11 additions & 22 deletions kernel/rtlil.h
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,6 @@ struct RTLIL::Const
bool operator ==(const RTLIL::Const &other) const;
bool operator !=(const RTLIL::Const &other) const;

bitvectype& assert_get_bits(const char* ctx) const;
std::string& assert_get_str(const char* ctx) const;

const std::vector<RTLIL::State>& bits() const;
std::vector<RTLIL::State>& bits();
bool as_bool() const;
Expand All @@ -706,21 +703,21 @@ struct RTLIL::Const
if (auto str = std::get_if<std::string>(&backing))
return 8 * str->size();
else
return assert_get_bits("Const::size").size();
return std::get<bitvectype>(backing).size();
}

inline bool empty() const {
if (auto str = std::get_if<std::string>(&backing))
return str->empty();
else
return assert_get_bits("Const::empty").empty();
return std::get<bitvectype>(backing).empty();
}

void bitvectorize() const {
if (std::get_if<bitvectype>(&backing))
return;

std::string& str = assert_get_str("Const::bitvectorize");
auto& str = std::get<std::string>(backing);
bitvectype bits;
bits.reserve(str.size() * 8);
for (int i = str.size() - 1; i >= 0; i--) {
Expand All @@ -734,20 +731,16 @@ struct RTLIL::Const
}

inline RTLIL::State &operator[](int index) {
bitvectorize();
return assert_get_bits("Const::operator[]").at(index);
return bits().at(index);
}
inline const RTLIL::State &operator[](int index) const {
bitvectorize();
return assert_get_bits("const Const::operator[]").at(index);
return bits().at(index);
}
inline bitvectype::iterator begin() {
bitvectorize();
return assert_get_bits("Const bit iterator begin()").begin();
return bits().begin();
}
inline bitvectype::iterator end() {
bitvectorize();
return assert_get_bits("Const bit iterator end()").end();
return bits().end();
}

bool is_fully_zero() const;
Expand All @@ -758,8 +751,7 @@ struct RTLIL::Const
bool is_onehot(int *pos = nullptr) const;

inline RTLIL::Const extract(int offset, int len = 1, RTLIL::State padding = RTLIL::State::S0) const {
bitvectorize();
bitvectype& bv = assert_get_bits("Const::extract");
auto& bv = bits();
bitvectype ret_bv;
ret_bv.reserve(len);
for (int i = offset; i < offset + len; i++)
Expand All @@ -768,13 +760,11 @@ struct RTLIL::Const
}

void extu(int width) {
bitvectorize();
assert_get_bits("Const::extu").resize(width, RTLIL::State::S0);
bits().resize(width, RTLIL::State::S0);
}

void exts(int width) {
bitvectorize();
bitvectype& bv = assert_get_bits("Const::exts");
bitvectype& bv = bits();
bv.resize(width, bv.empty() ? RTLIL::State::Sx : bv.back());
}

Expand All @@ -785,8 +775,7 @@ struct RTLIL::Const
for (auto c : *str)
h = mkhash(h, c);
} else {
bitvectype& bv = assert_get_bits("Const::hash");
for (auto b : bv)
for (auto b : bits())
h = mkhash(h, b);
}
return h;
Expand Down

0 comments on commit 9aa6a6b

Please sign in to comment.