diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 49ddfd4cd..82357bc71 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -723,6 +723,40 @@ bool RTLIL::Const::is_onehot(int *pos) const return found; } +Hasher RTLIL::Const::hash_into(Hasher h) const +{ + if (auto str = get_if_str()) + return hashlib::hash_ops::hash_into(*str, h); + + // If the bits are all 0/1, hash packed bits using the string hash. + // Otherwise hash the leading packed bits with the rest of the bits individually. + bitvectype &bv = get_bits(); + int size = GetSize(bv); + std::string packed; + int packed_size = (size + 7) >> 3; + packed.resize(packed_size, 0); + for (int bi = 0; bi < packed_size; ++bi) { + char ch = 0; + int end = std::min((bi + 1)*8, size); + for (int i = bi*8; i < end; ++i) { + RTLIL::State b = bv[i]; + if (b > RTLIL::State::S1) { + // Hash the packed bits we've seen so far, plus the remaining bits. + h = hashlib::hash_ops::hash_into(packed, h); + h = hashlib::hash_ops::hash_into(ch, h); + for (; i < size; ++i) { + h = hashlib::hash_ops::hash_into(bv[i], h); + } + h.eat(size); + return h; + } + ch |= static_cast(b) << (i & 7); + } + packed[packed_size - 1 - bi] = ch; + } + return hashlib::hash_ops::hash_into(packed, h); +} + RTLIL::Const RTLIL::Const::extract(int offset, int len, RTLIL::State padding) const { bitvectype ret_bv; ret_bv.reserve(len); diff --git a/kernel/rtlil.h b/kernel/rtlil.h index 77e3540ac..d2caf81f7 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -953,12 +953,7 @@ public: resize(width, empty() ? RTLIL::State::Sx : back()); } - [[nodiscard]] Hasher hash_into(Hasher h) const { - h.eat(size()); - for (auto b : *this) - h.eat(b); - return h; - } + [[nodiscard]] Hasher hash_into(Hasher h) const; }; struct RTLIL::AttrObject diff --git a/tests/unit/kernel/rtlilTest.cc b/tests/unit/kernel/rtlilTest.cc index b2b2e7076..4d8736ea3 100644 --- a/tests/unit/kernel/rtlilTest.cc +++ b/tests/unit/kernel/rtlilTest.cc @@ -222,6 +222,18 @@ namespace RTLIL { EXPECT_NE(Const(v1), Const("a")); } + static Hasher::hash_t hash(const Const &c) { + Hasher h; + h = c.hash_into(h); + return h.yield(); + } + + TEST_F(KernelRtlilTest, ConstEqualHashStrBits) { + std::vector v1 = {S0, S0, S0, S0, S0, S1, S0, S0}; + EXPECT_EQ(hash(Const(v1)), hash(Const(" "))); + EXPECT_NE(hash(Const(v1)), hash(Const("a"))); + } + class WireRtlVsHdlIndexConversionTest : public KernelRtlilTest, public testing::WithParamInterface>