summaryrefslogtreecommitdiff
path: root/src/support/bits.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2020-08-06 16:15:55 -0700
committerGitHub <noreply@github.com>2020-08-06 16:15:55 -0700
commit449e7a410bdaee17e139c8121cdf125d82a73dff (patch)
treec2ac7a91a688a5e3bccced280c2814dec81115dd /src/support/bits.cpp
parente89b40149ff6327df9dcc47043528b0ddef6c377 (diff)
downloadbinaryen-449e7a410bdaee17e139c8121cdf125d82a73dff.tar.gz
binaryen-449e7a410bdaee17e139c8121cdf125d82a73dff.tar.bz2
binaryen-449e7a410bdaee17e139c8121cdf125d82a73dff.zip
Fix CountLeadingZeroes on MSVC (#3028)
We just had the logic there wrong - MSVC's intrinsic returns the bit index, not the number of leading zeros. That's identical when scanning forward but not in reverse... Fixes #2942
Diffstat (limited to 'src/support/bits.cpp')
-rw-r--r--src/support/bits.cpp7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/support/bits.cpp b/src/support/bits.cpp
index 63b744eb4..992b97955 100644
--- a/src/support/bits.cpp
+++ b/src/support/bits.cpp
@@ -116,7 +116,10 @@ template<> int CountLeadingZeroes<uint32_t>(uint32_t v) {
#elif defined(_MSC_VER)
unsigned long count;
_BitScanReverse(&count, v);
- return (int)count;
+ // BitScanReverse gives the bit position (0 for the LSB, then 1, etc.) of the
+ // first bit that is 1, when looking from the MSB. To count leading zeros, we
+ // need to adjust that.
+ return 31 - int(count);
#else
// See Stanford bithacks, find the log base 2 of an N-bit integer in
// O(lg(N)) operations with multiply and lookup:
@@ -142,7 +145,7 @@ template<> int CountLeadingZeroes<uint64_t>(uint64_t v) {
#elif defined(_MSC_VER) && defined(_M_X64)
unsigned long count;
_BitScanReverse64(&count, v);
- return (int)count;
+ return 63 - int(count);
#else
return v >> 32 ? CountLeadingZeroes((uint32_t)(v >> 32))
: 32 + CountLeadingZeroes((uint32_t)v);