From 0d379e9016dd3a468906d8a6dd024531203b5a2c Mon Sep 17 00:00:00 2001 From: Rangi Date: Tue, 21 Jul 2026 13:56:57 -0400 Subject: [PATCH] Assume `clz`/`ctz` argument is nonzero in our fallback implementations --- include/helpers.hpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/include/helpers.hpp b/include/helpers.hpp index 8e7ff439..e12d350e 100644 --- a/include/helpers.hpp +++ b/include/helpers.hpp @@ -50,42 +50,40 @@ static inline void unreachable_() { #pragma intrinsic(_BitScanReverse, _BitScanForward) static inline int ctz(unsigned int x) { - unsigned long cnt; - assume(x != 0); - _BitScanForward(&cnt, x); - return cnt; + unsigned long count; + _BitScanForward(&count, x); + return count; } static inline int clz(unsigned int x) { - unsigned long cnt; - assume(x != 0); - _BitScanReverse(&cnt, x); - return 31 - cnt; + unsigned long count; + _BitScanReverse(&count, x); + return 31 - count; } #else #include static inline int ctz(unsigned int x) { - int cnt = 0; - + assume(x != 0); + int count = 0; while (!(x & 1)) { x >>= 1; - ++cnt; + ++count; } - return cnt; + return count; } static inline int clz(unsigned int x) { - int cnt = 0; - + assume(x != 0); + int count = 0; while (x <= UINT_MAX / 2) { x <<= 1; - ++cnt; + ++count; } - return cnt; + return count; } #endif