From 46b6ff35a04650a2f44b078c4f3985210784f7c6 Mon Sep 17 00:00:00 2001 From: Daniel Kamil Kozar Date: Mon, 31 Oct 2016 00:45:30 +0100 Subject: [PATCH] Initialise the pointer to bitcount_fn in a new early initialisation function In order to avoid the overhead of checking whether the function pointer to bitcount's actual implementation has been initialised every time bitcount is called, initialise it at application startup. --- src/platform/platform.h | 3 +++ src/platform/posix.c | 2 ++ src/platform/shared.c | 5 +++++ src/platform/windows.c | 6 ++++++ src/util/util.c | 12 +++++++----- src/util/util.h | 1 + 6 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/platform/platform.h b/src/platform/platform.h index 0c249c370b..b127969ec8 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -196,6 +196,9 @@ bool platform_check_steam_overlay_attached(); datetime64 platform_get_datetime_now_utc(); +// Called very early in the program before parsing commandline arguments. +void core_init(); + // Windows specific definitions #ifdef __WINDOWS__ #ifndef WIN32_LEAN_AND_MEAN diff --git a/src/platform/posix.c b/src/platform/posix.c index c549f55a47..75673e3e6a 100644 --- a/src/platform/posix.c +++ b/src/platform/posix.c @@ -51,6 +51,8 @@ utf8 _openrctDataDirectoryPath[MAX_PATH] = { 0 }; */ int main(int argc, const char **argv) { + core_init(); + int run_game = cmdline_run(argv, argc); if (run_game == 1) { diff --git a/src/platform/shared.c b/src/platform/shared.c index 395afdc2f7..7e2db30792 100644 --- a/src/platform/shared.c +++ b/src/platform/shared.c @@ -782,3 +782,8 @@ uint8 platform_get_currency_value(const char *currCode) { return CURRENCY_POUNDS; } + +void core_init() +{ + bitcount_init(); +} diff --git a/src/platform/windows.c b/src/platform/windows.c index 82fe20673b..32fe6fb427 100644 --- a/src/platform/windows.c +++ b/src/platform/windows.c @@ -61,6 +61,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine { _dllModule = hInstance; + core_init(); + int argc; char ** argv = (char**)windows_get_command_line_args(&argc); int runGame = cmdline_run((const char **)argv, argc); @@ -86,6 +88,8 @@ int main(int argc, char *argv[]) HINSTANCE hInstance = GetModuleHandle(NULL); _dllModule = hInstance; + core_init(); + int runGame = cmdline_run((const char **)argv, argc); if (runGame == 1) { openrct2_launch(); @@ -124,6 +128,8 @@ __declspec(dllexport) int StartOpenRCT(HINSTANCE hInstance, HINSTANCE hPrevInsta _dllModule = GetModuleHandleA(OPENRCT2_DLL_MODULE_NAME); } + core_init(); + // argv = CommandLineToArgvA(lpCmdLine, &argc); argv = (char**)windows_get_command_line_args(&argc); runGame = cmdline_run((const char **)argv, argc); diff --git a/src/util/util.c b/src/util/util.c index e988d7ec5a..1cb56a3511 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -253,13 +253,15 @@ static int bitcount_lut(int source) BitsSetTable256[source >> 24]; } +static int(*bitcount_fn)(int); + +void bitcount_init(void) +{ + bitcount_fn = bitcount_available() ? bitcount_popcnt : bitcount_lut; +} + int bitcount(int source) { - static int(*bitcount_fn)(int); - if(bitcount_fn == 0) - { - bitcount_fn = bitcount_available() ? bitcount_popcnt : bitcount_lut; - } return bitcount_fn(source); } diff --git a/src/util/util.h b/src/util/util.h index 261596ba3f..46e931b4f2 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -38,6 +38,7 @@ void path_end_with_separator(utf8 *path, size_t size); bool readentirefile(const utf8 *path, void **outBuffer, size_t *outLength); int bitscanforward(int source); +void bitcount_init(void); int bitcount(int source); bool strequals(const char *a, const char *b, int length, bool caseInsensitive); int strcicmp(char const *a, char const *b);