From 29c86bb03ab6d9d9bb51173cdd01dd5c5e380ac0 Mon Sep 17 00:00:00 2001 From: Anthony Pesch Date: Wed, 18 Oct 2017 17:37:10 -0400 Subject: [PATCH] avoid using popcnt intrinsics to support older processors such as the core 2 duo which are plenty fast enough to support --- src/core/math.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/core/math.h b/src/core/math.h index 0fb5ac85..9af7604f 100644 --- a/src/core/math.h +++ b/src/core/math.h @@ -17,14 +17,21 @@ static inline uint32_t bswap24(uint32_t v) { return ((v & 0xff) << 16) | (v & 0x00ff00) | ((v & 0xff0000) >> 16); } +static inline int popcnt32(uint32_t v) { + /* avoid using popcnt intrinsics to support older processors such as the + core 2 duo which are plenty fast enough to support */ + v = (v & 0x55555555) + ((v >> 1) & 0x55555555); + v = (v & 0x33333333) + ((v >> 2) & 0x33333333); + v = (v & 0x0f0f0f0f) + ((v >> 4) & 0x0f0f0f0f); + v = (v & 0x00ff00ff) + ((v >> 8) & 0x00ff00ff); + v = (v & 0x0000ffff) + ((v >> 16) & 0x0000ffff); + return (int)v; +} + #if COMPILER_MSVC #include -static inline int popcnt32(uint32_t v) { - return __popcnt(v); -} - static inline int clz32(uint32_t v) { unsigned long r = 0; _BitScanReverse(&r, v); @@ -55,10 +62,6 @@ static inline uint32_t bswap32(uint32_t v) { #else -static inline int popcnt32(uint32_t v) { - return __builtin_popcount(v); -} - static inline int clz32(uint32_t v) { return __builtin_clz(v); }