From 55f1d3aac64ab7ae3ae593e046916a3f22f49fa6 Mon Sep 17 00:00:00 2001 From: Daniel Kamil Kozar Date: Tue, 1 Nov 2016 11:42:36 +0100 Subject: [PATCH] Fixes to the new bitcount implementation Use Intel-standardized _mm_popcnt_u32 instead of Microsoft-specific __popcnt, replace assert with openrct2_assert, replace bitcount's argument with uint32. --- src/util/util.c | 14 +++++++------- src/util/util.h | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/util/util.c b/src/util/util.c index 0c89f91d1d..ca79080d65 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -199,7 +199,7 @@ int bitscanforward(int source) #include #define OpenRCT2_POPCNT_GNUC #elif defined(_MSC_VER) && (_MSC_VER >= 1500) && (defined(_M_X64) || defined(_M_IX86)) // VS2008 - #include + #include #define OpenRCT2_POPCNT_MSVC #endif @@ -221,7 +221,7 @@ static bool bitcount_popcnt_available() #endif } -static int bitcount_popcnt(int source) +static int bitcount_popcnt(uint32 source) { #if defined(OpenRCT2_POPCNT_GNUC) // use asm directly in order to actually emit the instruction : using @@ -230,14 +230,14 @@ static int bitcount_popcnt(int source) asm volatile ("popcnt %1,%0" : "=r"(rv) : "rm"(source) : "cc"); return rv; #elif defined(OpenRCT2_POPCNT_MSVC) - return __popcnt(source); + return _mm_popcnt_u32(source); #else - assert(false && "bitcount_popcnt() called, without support compiled in"); + openrct2_assert(false, "bitcount_popcnt() called, without support compiled in"); return INT_MAX; #endif } -static int bitcount_lut(int source) +static int bitcount_lut(uint32 source) { // https://graphics.stanford.edu/~seander/bithacks.html static const unsigned char BitsSetTable256[256] = @@ -253,14 +253,14 @@ static int bitcount_lut(int source) BitsSetTable256[source >> 24]; } -static int(*bitcount_fn)(int); +static int(*bitcount_fn)(uint32); void bitcount_init() { bitcount_fn = bitcount_popcnt_available() ? bitcount_popcnt : bitcount_lut; } -int bitcount(int source) +int bitcount(uint32 source) { return bitcount_fn(source); } diff --git a/src/util/util.h b/src/util/util.h index 0132acf728..f6ffdc66f5 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -39,7 +39,7 @@ bool readentirefile(const utf8 *path, void **outBuffer, size_t *outLength); int bitscanforward(int source); void bitcount_init(); -int bitcount(int source); +int bitcount(uint32 source); bool strequals(const char *a, const char *b, int length, bool caseInsensitive); int strcicmp(char const *a, char const *b); int strlogicalcmp(char const *a, char const *b);