From 18d1907fa85eebb2c1d1ac9457b2f3d5928970be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Povi=C5=A1er?= Date: Tue, 12 Dec 2023 09:52:35 +0100 Subject: [PATCH] cxxrtl: Assert well-formedness of input to `udivmod` --- backends/cxxrtl/runtime/cxxrtl/cxxrtl.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h b/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h index 04ed48eb3..2d5451287 100644 --- a/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h +++ b/backends/cxxrtl/runtime/cxxrtl/cxxrtl.h @@ -583,8 +583,9 @@ struct value : public expr_base> { value dividend = *this; if (dividend.ucmp(divisor)) return {/*quotient=*/value{0u}, /*remainder=*/dividend}; - uint32_t divisor_shift = divisor.ctlz() - dividend.ctlz(); - divisor = divisor.shl(value{divisor_shift}); + int64_t divisor_shift = divisor.ctlz() - dividend.ctlz(); + assert(divisor_shift >= 0); + divisor = divisor.shl(value{(chunk::type) divisor_shift}); for (size_t step = 0; step <= divisor_shift; step++) { quotient = quotient.shl(value{1u}); if (!dividend.ucmp(divisor)) {