Skip to content

Commit

Permalink
const: fix comparison and hash
Browse files Browse the repository at this point in the history
  • Loading branch information
widlarizer committed Aug 14, 2024
1 parent 754a7b9 commit 3214b7b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
23 changes: 11 additions & 12 deletions kernel/rtlil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,15 @@ RTLIL::Const::~Const() {
check(false);
}

bool RTLIL::Const::operator <(const RTLIL::Const &other) const
bool RTLIL::Const::operator<(const RTLIL::Const &other) const
{
if (is_str() != other.is_str())
return decode_string() < other.decode_string();

if (get_if_str())
if (is_str() && other.is_str())
return str_ < other.str_;

bitvectype& bv = get_bits();
auto other_bv = other.get_bits();
// If either operand isn't a string, we have to construct a copy
// to bitvectorize without unpacking either operand
bitvectype &bv = is_str() ? Const(*this).bits() : get_bits();
bitvectype &other_bv = other.is_str() ? Const(other).bits() : other.get_bits();

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

bool RTLIL::Const::operator ==(const RTLIL::Const &other) const
{
if (is_str() != other.is_str())
return decode_string() == other.decode_string();

if (get_if_str())
if (is_str() && other.is_str())
return get_str() == other.get_str();

return get_bits() == other.get_bits();
bitvectype &bv = is_str() ? Const(*this).bits() : get_bits();
bitvectype &other_bv = other.is_str() ? Const(other).bits() : other.get_bits();

return bv == other_bv;
}

bool RTLIL::Const::operator !=(const RTLIL::Const &other) const
Expand Down
6 changes: 4 additions & 2 deletions kernel/rtlil.h
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,10 @@ struct RTLIL::Const
unsigned int h = mkhash_init;

if(is_str()) {
for (auto c : get_str())
h = mkhash(h, c);
// Copy to avoid unpacking `this`
Const c = Const(*this);
for (auto b : c.bits())
h = mkhash(h, b);
} else {
for (auto b : get_bits())
h = mkhash(h, b);
Expand Down

0 comments on commit 3214b7b

Please sign in to comment.