From 005abe2e121551cac42e6e9620f4446c7fd4e430 Mon Sep 17 00:00:00 2001 From: tqfx Date: Thu, 21 Mar 2024 23:09:09 +0800 Subject: [PATCH] fix BitScanReverse64 in x86 --- src/math.c | 6 +++--- test/math.h | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/math.c b/src/math.c index 2320237..64aab7c 100644 --- a/src/math.c +++ b/src/math.c @@ -136,12 +136,12 @@ static A_INLINE int A_U64_BSR(a_u64 x) #define A_U64_BSR(x) BitScanReverse64(x) static A_INLINE int A_U64_BSR(a_u64 x) { - unsigned long i = 0, hi = 0; - _BitScanReverse(&i, (a_u32)x); + unsigned long i = 0, hi; if (_BitScanReverse(&hi, x >> 32)) { - i += hi + 1; + return (int)hi + 32; } + _BitScanReverse(&i, (a_u32)x); return (int)i; } #endif /* A_U64_BSR */ diff --git a/test/math.h b/test/math.h index a76b838..0ba3d5f 100644 --- a/test/math.h +++ b/test/math.h @@ -19,6 +19,8 @@ static void test_u32_sqrt(void) static void test_u64_sqrt(void) { TEST_BUG(a_u64_sqrt(A_U64_C(~0)) == A_U32_C(0xFFFFFFFF)); + TEST_BUG(a_u64_sqrt(A_U64_C(0x10000)) == A_U32_C(0x100)); + TEST_BUG(a_u64_sqrt(A_U64_C(0xFFFFFFFF)) == A_U32_C(0xFFFF)); TEST_BUG(a_u64_sqrt(A_U64_C(0x100000000)) == A_U32_C(0x10000)); TEST_BUG(a_u64_sqrt(A_U64_C(0xFFFFFFFFFFFFFFFF)) == A_U32_C(0xFFFFFFFF)); } -- 2.11.4.GIT