From d7cb6981b55a15eee3d0d9be84446826a65c0403 Mon Sep 17 00:00:00 2001 From: Merry Date: Wed, 13 Dec 2023 12:15:12 +0000 Subject: [PATCH] cxxrtl: Fix value::ctlz --- backends/cxxrtl/runtime/cxxrtl/cxxrtl.h | 3 ++- tests/cxxrtl/test_value.cc | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) 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); + } +}