Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ir/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ struct Bits {
if (mask == 0) {
return 0; // trivially not a mask
}
// otherwise, see if adding one turns this into a 1-bit thing, 00011111 + 1
// => 00100000
if (PopCount(mask + 1) != 1) {
// otherwise, see if x & (x + 1) turns this into non-zero value
// 00011111 & (00011111 + 1) => 0
if (mask & (mask + 1)) {
return 0;
}
// this is indeed a mask
Expand Down
14 changes: 6 additions & 8 deletions src/support/bits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

namespace wasm {
Expand All @@ -36,16 +34,16 @@ template<> int PopCount<uint8_t>(uint8_t v) {
}

template<> int PopCount<uint16_t>(uint16_t v) {
#if __has_builtin(__builtin_popcount) || defined(__GNUC__) || defined(_MSC_VER)
return (int)__builtin_popcount(v);
#if __has_builtin(__builtin_popcount) || defined(__GNUC__)
return __builtin_popcount(v);
#else
return PopCount((uint8_t)(v & 0xFF)) + PopCount((uint8_t)(v >> 8));
#endif
}

template<> int PopCount<uint32_t>(uint32_t v) {
#if __has_builtin(__builtin_popcount) || defined(__GNUC__) || defined(_MSC_VER)
return (int)__builtin_popcount(v);
#if __has_builtin(__builtin_popcount) || defined(__GNUC__)
return __builtin_popcount(v);
#else
// See Stanford bithacks, counting bits set in parallel, "best method":
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
Expand All @@ -56,8 +54,8 @@ template<> int PopCount<uint32_t>(uint32_t v) {
}

template<> int PopCount<uint64_t>(uint64_t v) {
#if __has_builtin(__builtin_popcount) || defined(__GNUC__) || defined(_MSC_VER)
return (int)__builtin_popcountll(v);
#if __has_builtin(__builtin_popcount) || defined(__GNUC__)
return __builtin_popcountll(v);
#else
return PopCount((uint32_t)v) + PopCount((uint32_t)(v >> 32));
#endif
Expand Down