From 7040c17a736537e35f05199b4535a57a29019e5d Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 5 Sep 2022 01:34:36 +0200 Subject: [PATCH] count-leading-zeros: Fix a link error on 32-bit MSVC and a test failure. * lib/count-leading-zeros.h: Correct syntax for #pragma intrinsic. (COUNT_LEADING_ZEROS): Fix the return value. (count_leading_zeros_ll): Use two _BitScanReverse invocations instead of a _BitScanReverse64 invocation. --- ChangeLog | 8 ++++++++ lib/count-leading-zeros.h | 20 +++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index f7ddcc655b..883cd016a0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2022-09-04 Bruno Haible + + count-leading-zeros: Fix a link error on 32-bit MSVC and a test failure. + * lib/count-leading-zeros.h: Correct syntax for #pragma intrinsic. + (COUNT_LEADING_ZEROS): Fix the return value. + (count_leading_zeros_ll): Use two _BitScanReverse invocations instead + of a _BitScanReverse64 invocation. + 2022-09-04 Bruno Haible count-trailing-zeros: Fix a link error on 32-bit MSVC. diff --git a/lib/count-leading-zeros.h b/lib/count-leading-zeros.h index 354641af0a..4b4f5d4f9a 100644 --- a/lib/count-leading-zeros.h +++ b/lib/count-leading-zeros.h @@ -43,13 +43,17 @@ extern "C" { # define COUNT_LEADING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \ return x ? BUILTIN (x) : CHAR_BIT * sizeof x; #elif _MSC_VER -# pragma intrinsic _BitScanReverse -# pragma intrinsic _BitScanReverse64 +# pragma intrinsic (_BitScanReverse) +# if defined _M_X64 +# pragma intrinsic (_BitScanReverse64) +# endif # define COUNT_LEADING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \ do \ { \ unsigned long result; \ - return MSC_BUILTIN (&result, x) ? result : CHAR_BIT * sizeof x; \ + if (MSC_BUILTIN (&result, x)) \ + return CHAR_BIT * sizeof x - 1 - result; \ + return CHAR_BIT * sizeof x; \ } \ while (0) #else @@ -109,8 +113,18 @@ count_leading_zeros_l (unsigned long int x) COUNT_LEADING_ZEROS_INLINE int count_leading_zeros_ll (unsigned long long int x) { +#if (defined _MSC_VER && !defined __clang__) && !defined _M_X64 + /* 32-bit MSVC does not have _BitScanReverse64, only _BitScanReverse. */ + unsigned long result; + if (_BitScanReverse (&result, (unsigned long) (x >> 32))) + return CHAR_BIT * sizeof x - 1 - 32 - result; + if (_BitScanReverse (&result, (unsigned long) x)) + return CHAR_BIT * sizeof x - 1 - result; + return CHAR_BIT * sizeof x; +#else COUNT_LEADING_ZEROS (__builtin_clzll, _BitScanReverse64, unsigned long long int); +#endif } #ifdef __cplusplus -- 2.39.5