1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-23 15:52:55 +01:00

Make UtilBitScanForward a header implementation (#22406)

This commit is contained in:
Aaron van Geffen
2024-07-28 21:43:07 +02:00
committed by GitHub
parent 51816d2bd5
commit 6b72aaed0c
2 changed files with 48 additions and 56 deletions

View File

@@ -25,16 +25,6 @@
#include <ctime>
#include <random>
#ifdef _WIN32
# ifndef NOMINMAX
# define NOMINMAX
# endif
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
#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

View File

@@ -19,13 +19,59 @@
#include <type_traits>
#include <vector>
#ifdef _MSC_VER
# include <intrin.h>
#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);