diff --git a/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h b/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h index c9ddb815f..d861b7e07 100644 --- a/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h +++ b/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h @@ -511,7 +511,8 @@ struct value : public expr_base> { 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--) diff --git a/tests/cxxrtl/test_value.cc b/tests/cxxrtl/test_value.cc index c553f6112..4d68372bb 100644 --- a/tests/cxxrtl/test_value.cc +++ b/tests/cxxrtl/test_value.cc @@ -36,4 +36,10 @@ int main() cxxrtl::value<64> c = a.sshr(b); assert(c.get() == 0xffffffff8abcdef1u); } -} \ No newline at end of file + + { + // ctlz should work with Bits that are a multiple of chunk size + cxxrtl::value<32> a(0x00040000u); + assert(a.ctlz() == 13); + } +}