From 7b1e170e7ca2407e2d35a043dc34af29551ef7c2 Mon Sep 17 00:00:00 2001 From: Ash Vardanian <1983160+ashvardanian@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:08:48 -0700 Subject: [PATCH] Improve: BitScan dispatch on Windows --- stringzilla/stringzilla.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/stringzilla/stringzilla.h b/stringzilla/stringzilla.h index 57d17b89..136d93c5 100644 --- a/stringzilla/stringzilla.h +++ b/stringzilla/stringzilla.h @@ -714,19 +714,26 @@ inline static sz_u64_t sz_u64_byte_reverse(sz_u64_t val) { inline static sz_size_t sz_log2i(sz_size_t n) { if (n == 0) return 0; -#if defined(__LP64__) || defined(_WIN64) // 64-bit +#ifdef _WIN64 #ifdef _MSC_VER unsigned long index; - _BitScanReverse64(&index, n); - return index; + if (_BitScanReverse64(&index, n)) return index; + return 0; // This line might be redundant due to the initial check, but it's safer to include it. #else return 63 - __builtin_clzll(n); #endif -#else // 32-bit +#elif defined(_WIN32) #ifdef _MSC_VER unsigned long index; - _BitScanReverse(&index, n); - return index; + if (_BitScanReverse(&index, n)) return index; + return 0; // Same note as above. +#else + return 31 - __builtin_clz(n); +#endif +#else +// Handle non-Windows platforms. You can further differentiate between 32-bit and 64-bit if needed. +#if defined(__LP64__) + return 63 - __builtin_clzll(n); #else return 31 - __builtin_clz(n); #endif