diff --git a/src/openrct2/util/Util.cpp b/src/openrct2/util/Util.cpp index 2eca67ef06..3e1e8dd4da 100644 --- a/src/openrct2/util/Util.cpp +++ b/src/openrct2/util/Util.cpp @@ -25,16 +25,6 @@ #include #include -#ifdef _WIN32 -# ifndef NOMINMAX -# define NOMINMAX -# endif -# ifndef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN -# endif -# include -#endif // _WIN32 - int32_t SquaredMetresToSquaredFeet(int32_t squaredMetres) { // 1 metre squared = 10.7639104 feet squared @@ -62,50 +52,6 @@ int32_t MphToDmps(int32_t mph) return (mph * 73243) >> 14; } -int32_t UtilBitScanForward(uint32_t source) -{ -#if defined(_MSC_VER) && (_MSC_VER >= 1400) // Visual Studio 2005 - DWORD i; - uint8_t success = _BitScanForward(&i, source); - return success != 0 ? i : -1; -#elif defined(__GNUC__) - int32_t success = __builtin_ffs(source); - return success - 1; -#else -# pragma message("Falling back to iterative bitscan forward, consider using intrinsics") - // This is a low-hanging optimisation boost, check if your compiler offers - // any intrinsic. - // cf. https://github.com/OpenRCT2/OpenRCT2/pull/2093 - for (int32_t i = 0; i < 32; i++) - if (source & (1u << i)) - return i; - - return -1; -#endif -} - -int32_t UtilBitScanForward(uint64_t source) -{ -#if defined(_MSC_VER) && (_MSC_VER >= 1400) && defined(_M_X64) // Visual Studio 2005 - DWORD i; - uint8_t success = _BitScanForward64(&i, source); - return success != 0 ? i : -1; -#elif defined(__GNUC__) - int32_t success = __builtin_ffsll(source); - return success - 1; -#else -# pragma message("Falling back to iterative bitscan forward, consider using intrinsics") - // This is a low-hanging optimisation boost, check if your compiler offers - // any intrinsic. - // cf. https://github.com/OpenRCT2/OpenRCT2/pull/2093 - for (int32_t i = 0; i < 64; i++) - if (source & (1uLL << i)) - return i; - - return -1; -#endif -} - /* Case insensitive logical compare */ // Example: // - Guest 10 diff --git a/src/openrct2/util/Util.h b/src/openrct2/util/Util.h index d50d9716e5..17c3f14506 100644 --- a/src/openrct2/util/Util.h +++ b/src/openrct2/util/Util.h @@ -19,13 +19,59 @@ #include #include +#ifdef _MSC_VER +# include +#endif + int32_t SquaredMetresToSquaredFeet(int32_t squaredMetres); int32_t MetresToFeet(int32_t metres); int32_t MphToKmph(int32_t mph); int32_t MphToDmps(int32_t mph); -int32_t UtilBitScanForward(uint32_t source); -int32_t UtilBitScanForward(uint64_t source); +inline int32_t UtilBitScanForward(uint32_t source) +{ +#if defined(_MSC_VER) && (_MSC_VER >= 1400) // Visual Studio 2005 + unsigned long i; + uint8_t success = _BitScanForward(&i, source); + return success != 0 ? i : -1; +#elif defined(__GNUC__) + int32_t success = __builtin_ffs(source); + return success - 1; +#else +# pragma message("Falling back to iterative bitscan forward, consider using intrinsics") + // This is a low-hanging optimisation boost, check if your compiler offers + // any intrinsic. + // cf. https://github.com/OpenRCT2/OpenRCT2/pull/2093 + for (int32_t i = 0; i < 32; i++) + if (source & (1u << i)) + return i; + + return -1; +#endif +} + +inline int32_t UtilBitScanForward(uint64_t source) +{ +#if defined(_MSC_VER) && (_MSC_VER >= 1400) && defined(_M_X64) // Visual Studio 2005 + unsigned long i; + uint8_t success = _BitScanForward64(&i, source); + return success != 0 ? i : -1; +#elif defined(__GNUC__) + int32_t success = __builtin_ffsll(source); + return success - 1; +#else +# pragma message("Falling back to iterative bitscan forward, consider using intrinsics") + // This is a low-hanging optimisation boost, check if your compiler offers + // any intrinsic. + // cf. https://github.com/OpenRCT2/OpenRCT2/pull/2093 + for (int32_t i = 0; i < 64; i++) + if (source & (1uLL << i)) + return i; + + return -1; +#endif +} + int32_t StrLogicalCmp(char const* a, char const* b); char* SafeStrCpy(char* destination, const char* source, size_t num); char* SafeStrCat(char* destination, const char* source, size_t size);