Commit 801ae24d authored by Harris Hancock's avatar Harris Hancock

Add kj::popCount to abstract over compiler differences

parent e780e3ed
...@@ -483,7 +483,7 @@ size_t computeUnpackedSizeInWords(kj::ArrayPtr<const byte> packedBytes) { ...@@ -483,7 +483,7 @@ size_t computeUnpackedSizeInWords(kj::ArrayPtr<const byte> packedBytes) {
size_t total = 0; size_t total = 0;
while (ptr < end) { while (ptr < end) {
uint tag = *ptr; uint tag = *ptr;
size_t count = __builtin_popcount(tag); size_t count = kj::popCount(tag);
total += 1; total += 1;
KJ_REQUIRE(end - ptr >= count, "invalid packed data"); KJ_REQUIRE(end - ptr >= count, "invalid packed data");
ptr += count + 1; ptr += count + 1;
......
...@@ -85,6 +85,10 @@ ...@@ -85,6 +85,10 @@
#undef _GLIBCXX_HAVE_GETS #undef _GLIBCXX_HAVE_GETS
#endif #endif
#if defined(_MSC_VER)
#include <intrin.h> // __popcnt
#endif
// ======================================================================================= // =======================================================================================
namespace kj { namespace kj {
...@@ -597,6 +601,15 @@ float nan(); ...@@ -597,6 +601,15 @@ float nan();
inline constexpr bool isNaN(float f) { return f != f; } inline constexpr bool isNaN(float f) { return f != f; }
inline constexpr bool isNaN(double f) { return f != f; } inline constexpr bool isNaN(double f) { return f != f; }
inline int popCount(unsigned int x) {
#if defined(_MSC_VER)
return __popcnt(x);
// Note: __popcnt returns unsigned int, but the value is clearly guaranteed to fit into an int
#else
return __builtin_popcount(x);
#endif
}
// ======================================================================================= // =======================================================================================
// Useful fake containers // Useful fake containers
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment