Skip to content

Commit

Permalink
cxxrtl: Fix value::ctlz
Browse files Browse the repository at this point in the history
  • Loading branch information
merryhime committed Dec 13, 2023
1 parent ded63be commit d7cb698
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion backends/cxxrtl/runtime/cxxrtl/cxxrtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ struct value : public expr_base<value<Bits>> {
for (size_t n = 0; n < chunks; n++) {
chunk::type x = data[chunks - 1 - n];
// First add to `count` as if the chunk is zero
count += (n == 0 ? Bits % chunk::bits : chunk::bits);
constexpr size_t msb_chunk_bits = Bits % chunk::bits != 0 ? Bits % chunk::bits : chunk::bits;
count += (n == 0 ? msb_chunk_bits : chunk::bits);
// If the chunk isn't zero, correct the `count` value and return
if (x != 0) {
for (; x != 0; count--)
Expand Down
8 changes: 7 additions & 1 deletion tests/cxxrtl/test_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ int main()
cxxrtl::value<64> c = a.sshr(b);
assert(c.get<uint64_t>() == 0xffffffff8abcdef1u);
}
}

{
// ctlz should work with Bits that are a multiple of chunk size
cxxrtl::value<32> a(0x00040000u);
assert(a.ctlz() == 13);
}
}

0 comments on commit d7cb698

Please sign in to comment.