From 920988a244bdb19e3020838fa62999b576cc87b7 Mon Sep 17 00:00:00 2001 From: startewho Date: Sat, 15 Jun 2024 21:04:42 +0800 Subject: [PATCH 1/3] fix the windows vs x86 build --- cutils.h | 10 +++++++++- tests/test_conv.c | 11 ++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/cutils.h b/cutils.h index a5da60db7..332ece57a 100644 --- a/cutils.h +++ b/cutils.h @@ -202,10 +202,18 @@ static inline int clz32(unsigned int a) /* WARNING: undefined if a = 0 */ static inline int clz64(uint64_t a) { -#if defined(_MSC_VER) && !defined(__clang__) +#if defined(_MSC_VER)&& defined(_WIN64) && !defined(__clang__) unsigned long index; _BitScanReverse64(&index, a); return 63 - index; +#elif(_MSC_VER) +unsigned long i = 0, hi = 0; + if (_BitScanReverse(&hi, a>> 32)) + { + return (int)hi + 32; + } + _BitScanReverse(&i, (uint32_t)a); + return (int)i; #else return __builtin_clzll(a); #endif diff --git a/tests/test_conv.c b/tests/test_conv.c index 9761b30d0..3e38c7015 100644 --- a/tests/test_conv.c +++ b/tests/test_conv.c @@ -90,13 +90,22 @@ static inline int clz32(unsigned int a) #endif } +/* WARNING: undefined if a = 0 */ /* WARNING: undefined if a = 0 */ static inline int clz64(uint64_t a) { -#if defined(_MSC_VER) && !defined(__clang__) +#if defined(_MSC_VER)&& defined(_WIN64) && !defined(__clang__) unsigned long index; _BitScanReverse64(&index, a); return 63 - index; +#elif(_MSC_VER) +unsigned long i = 0, hi = 0; + if (_BitScanReverse(&hi, a>> 32)) + { + return (int)hi + 32; + } + _BitScanReverse(&i, (uint32_t)a); + return (int)i; #else return __builtin_clzll(a); #endif From 6cbfa888383ddae926e70c6d8b3165cdc5c14b8c Mon Sep 17 00:00:00 2001 From: startewho Date: Sat, 15 Jun 2024 21:07:23 +0800 Subject: [PATCH 2/3] Update cutils.h --- cutils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cutils.h b/cutils.h index 332ece57a..ad9492459 100644 --- a/cutils.h +++ b/cutils.h @@ -202,7 +202,7 @@ static inline int clz32(unsigned int a) /* WARNING: undefined if a = 0 */ static inline int clz64(uint64_t a) { -#if defined(_MSC_VER)&& defined(_WIN64) && !defined(__clang__) +#if defined(_MSC_VER) && defined(_WIN64) && !defined(__clang__) unsigned long index; _BitScanReverse64(&index, a); return 63 - index; From 13a7a00e0400e7e3401ad5c27529b769fb72975f Mon Sep 17 00:00:00 2001 From: startewho Date: Sun, 16 Jun 2024 09:11:52 +0800 Subject: [PATCH 3/3] update method --- cutils.h | 17 ++++++++--------- tests/test_conv.c | 19 ++++++++----------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/cutils.h b/cutils.h index ad9492459..26436d4f2 100644 --- a/cutils.h +++ b/cutils.h @@ -202,18 +202,17 @@ static inline int clz32(unsigned int a) /* WARNING: undefined if a = 0 */ static inline int clz64(uint64_t a) { -#if defined(_MSC_VER) && defined(_WIN64) && !defined(__clang__) +#if defined(_MSC_VER) && !defined(__clang__) +#if INTPTR_MAX == INT64_MAX unsigned long index; _BitScanReverse64(&index, a); return 63 - index; -#elif(_MSC_VER) -unsigned long i = 0, hi = 0; - if (_BitScanReverse(&hi, a>> 32)) - { - return (int)hi + 32; - } - _BitScanReverse(&i, (uint32_t)a); - return (int)i; +#else + if (a >> 32) + return clz32((unsigned)(a >> 32)); + else + return clz32((unsigned)a) + 32; +#endif #else return __builtin_clzll(a); #endif diff --git a/tests/test_conv.c b/tests/test_conv.c index 3e38c7015..b0678ae5c 100644 --- a/tests/test_conv.c +++ b/tests/test_conv.c @@ -90,27 +90,24 @@ static inline int clz32(unsigned int a) #endif } -/* WARNING: undefined if a = 0 */ /* WARNING: undefined if a = 0 */ static inline int clz64(uint64_t a) { -#if defined(_MSC_VER)&& defined(_WIN64) && !defined(__clang__) +#if defined(_MSC_VER) && !defined(__clang__) +#if INTPTR_MAX == INT64_MAX unsigned long index; _BitScanReverse64(&index, a); return 63 - index; -#elif(_MSC_VER) -unsigned long i = 0, hi = 0; - if (_BitScanReverse(&hi, a>> 32)) - { - return (int)hi + 32; - } - _BitScanReverse(&i, (uint32_t)a); - return (int)i; +#else + if (a >> 32) + return clz32((unsigned)(a >> 32)); + else + return clz32((unsigned)a) + 32; +#endif #else return __builtin_clzll(a); #endif } - // prototypes for final functions extern char const digits36[36]; size_t u32toa(char buf[minimum_length(11)], uint32_t n);