From 7fed7eb50b2b95ac4eeb3d29af3ce7b7d500032a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?= Date: Fri, 10 Jan 2020 19:24:59 +0900 Subject: [PATCH] fix Windows breakage Fixing typo revealed that _BitScanReverse is BSR, which behaves differently than LZCNT. What we want here is LZCNT so we have to emulate. --- internal/bits.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/bits.h b/internal/bits.h index 802a8af7eb..9d4e71fc8f 100644 --- a/internal/bits.h +++ b/internal/bits.h @@ -215,7 +215,7 @@ nlz_int32(uint32_t x) #elif defined(_MSC_VER) && defined(_WIN64) /* &&! defined(__AVX2__) */ unsigned long r; - return _BitScanReverse(&r, x) ? (int)r : 32; + return _BitScanReverse(&r, x) ? (31 - (int)r) : 32; #elif __has_builtin(__builtin_clz) STATIC_ASSERT(sizeof_int, sizeof(int) * CHAR_BIT == 32); @@ -244,7 +244,7 @@ nlz_int64(uint64_t x) #elif defined(_MSC_VER) && defined(_WIN64) /* &&! defined(__AVX2__) */ unsigned long r; - return _BitScanReverse64(&r, x) ? (unsigned int)r : 64; + return _BitScanReverse64(&r, x) ? (63u - (unsigned int)r) : 64; #elif __has_builtin(__builtin_clzl) if (x == 0) {