From 1f3fd65c4c72fb1ccdcbf1f312d7728978c3f752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20=C3=85stholm?= Date: Sat, 14 Sep 2024 00:06:38 +0200 Subject: [PATCH] cpuinfo: Rename SDL_GetCPUCount to SDL_GetNumLogicalCPUCores This was the only API that broke the "GetNumThings" convention used elsewhere, so renaming it helps with consistency. Adding "logical cores" to the name also makes it a bit more immediately obvious what the count actually represents. --- build-scripts/SDL_migration.cocci | 5 +++++ docs/README-migration.md | 1 + include/SDL3/SDL_cpuinfo.h | 4 ++-- include/SDL3/SDL_oldnames.h | 2 ++ src/cpuinfo/SDL_cpuinfo.c | 26 +++++++++++++------------- src/dynapi/SDL_dynapi.sym | 2 +- src/dynapi/SDL_dynapi_overrides.h | 2 +- src/dynapi/SDL_dynapi_procs.h | 2 +- test/testautomation_platform.c | 8 ++++---- test/testffmpeg.c | 2 +- test/testplatform.c | 2 +- 11 files changed, 32 insertions(+), 24 deletions(-) diff --git a/build-scripts/SDL_migration.cocci b/build-scripts/SDL_migration.cocci index 145760f33..8b4e12d20 100644 --- a/build-scripts/SDL_migration.cocci +++ b/build-scripts/SDL_migration.cocci @@ -3667,3 +3667,8 @@ identifier func =~ "^(SDL_AddEventWatch|SDL_AddHintCallback|SDL_AddSurfaceAltern @@ - SDL_NUM_SCANCODES + SDL_SCANCODE_COUNT +@@ +@@ +- SDL_GetCPUCount ++ SDL_GetNumLogicalCPUCores + (...) diff --git a/docs/README-migration.md b/docs/README-migration.md index 2a303c2fa..171823a01 100644 --- a/docs/README-migration.md +++ b/docs/README-migration.md @@ -341,6 +341,7 @@ SDL_HasRDTSC() has been removed; there is no replacement. Don't use the RDTSC op SDL_SIMDAlloc(), SDL_SIMDRealloc(), and SDL_SIMDFree() have been removed. You can use SDL_aligned_alloc() and SDL_aligned_free() with SDL_GetSIMDAlignment() to get the same functionality. The following functions have been renamed: +* SDL_GetCPUCount() => SDL_GetNumLogicalCPUCores() * SDL_SIMDGetAlignment() => SDL_GetSIMDAlignment() ## SDL_endian.h diff --git a/include/SDL3/SDL_cpuinfo.h b/include/SDL3/SDL_cpuinfo.h index e7ced26d8..f9921add4 100644 --- a/include/SDL3/SDL_cpuinfo.h +++ b/include/SDL3/SDL_cpuinfo.h @@ -54,7 +54,7 @@ extern "C" { #define SDL_CACHELINE_SIZE 128 /** - * Get the number of CPU cores available. + * Get the number of logical CPU cores available. * * \returns the total number of logical CPU cores. On CPUs that include * technologies such as hyperthreading, the number of logical cores @@ -62,7 +62,7 @@ extern "C" { * * \since This function is available since SDL 3.0.0. */ -extern SDL_DECLSPEC int SDLCALL SDL_GetCPUCount(void); +extern SDL_DECLSPEC int SDLCALL SDL_GetNumLogicalCPUCores(void); /** * Determine the L1 cache line size of the CPU. diff --git a/include/SDL3/SDL_oldnames.h b/include/SDL3/SDL_oldnames.h index 9cb132453..7638922f5 100644 --- a/include/SDL3/SDL_oldnames.h +++ b/include/SDL3/SDL_oldnames.h @@ -74,6 +74,7 @@ #define SDL_NewAudioStream SDL_CreateAudioStream /* ##SDL_cpuinfo.h */ +#define SDL_GetCPUCount SDL_GetNumLogicalCPUCores #define SDL_SIMDGetAlignment SDL_GetSIMDAlignment /* ##SDL_endian.h */ @@ -703,6 +704,7 @@ #define SDL_NewAudioStream SDL_NewAudioStream_renamed_SDL_CreateAudioStream /* ##SDL_cpuinfo.h */ +#define SDL_GetCPUCount SDL_GetCPUCount_renamed_SDL_GetNumLogicalCPUCores #define SDL_SIMDGetAlignment SDL_SIMDGetAlignment_renamed_SDL_GetSIMDAlignment /* ##SDL_endian.h */ diff --git a/src/cpuinfo/SDL_cpuinfo.c b/src/cpuinfo/SDL_cpuinfo.c index d87e3f3d6..63f25ea62 100644 --- a/src/cpuinfo/SDL_cpuinfo.c +++ b/src/cpuinfo/SDL_cpuinfo.c @@ -620,35 +620,35 @@ static int CPU_haveAVX512F(void) } #endif -static int SDL_CPUCount = 0; +static int SDL_NumLogicalCPUCores = 0; -int SDL_GetCPUCount(void) +int SDL_GetNumLogicalCPUCores(void) { - if (!SDL_CPUCount) { + if (!SDL_NumLogicalCPUCores) { #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) - if (SDL_CPUCount <= 0) { - SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN); + if (SDL_NumLogicalCPUCores <= 0) { + SDL_NumLogicalCPUCores = (int)sysconf(_SC_NPROCESSORS_ONLN); } #endif #ifdef HAVE_SYSCTLBYNAME - if (SDL_CPUCount <= 0) { - size_t size = sizeof(SDL_CPUCount); - sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0); + if (SDL_NumLogicalCPUCores <= 0) { + size_t size = sizeof(SDL_NumLogicalCPUCores); + sysctlbyname("hw.ncpu", &SDL_NumLogicalCPUCores, &size, NULL, 0); } #endif #if defined(SDL_PLATFORM_WINDOWS) - if (SDL_CPUCount <= 0) { + if (SDL_NumLogicalCPUCores <= 0) { SYSTEM_INFO info; GetSystemInfo(&info); - SDL_CPUCount = info.dwNumberOfProcessors; + SDL_NumLogicalCPUCores = info.dwNumberOfProcessors; } #endif // There has to be at least 1, right? :) - if (SDL_CPUCount <= 0) { - SDL_CPUCount = 1; + if (SDL_NumLogicalCPUCores <= 0) { + SDL_NumLogicalCPUCores = 1; } } - return SDL_CPUCount; + return SDL_NumLogicalCPUCores; } #ifdef __e2k__ diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym index a4eb8b719..6b3825675 100644 --- a/src/dynapi/SDL_dynapi.sym +++ b/src/dynapi/SDL_dynapi.sym @@ -239,7 +239,7 @@ SDL3_0.0.0 { SDL_GetBasePath; SDL_GetBooleanProperty; SDL_GetCPUCacheLineSize; - SDL_GetCPUCount; + SDL_GetNumLogicalCPUCores; SDL_GetCameraDriver; SDL_GetCameraFormat; SDL_GetCameraID; diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index 1b4705a87..322127180 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -264,7 +264,7 @@ #define SDL_GetBasePath SDL_GetBasePath_REAL #define SDL_GetBooleanProperty SDL_GetBooleanProperty_REAL #define SDL_GetCPUCacheLineSize SDL_GetCPUCacheLineSize_REAL -#define SDL_GetCPUCount SDL_GetCPUCount_REAL +#define SDL_GetNumLogicalCPUCores SDL_GetNumLogicalCPUCores_REAL #define SDL_GetCameraDriver SDL_GetCameraDriver_REAL #define SDL_GetCameraFormat SDL_GetCameraFormat_REAL #define SDL_GetCameraID SDL_GetCameraID_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 423d99485..ab406b4f5 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -284,7 +284,7 @@ SDL_DYNAPI_PROC(int,SDL_GetAudioStreamQueued,(SDL_AudioStream *a),(a),return) SDL_DYNAPI_PROC(const char*,SDL_GetBasePath,(void),(),return) SDL_DYNAPI_PROC(SDL_bool,SDL_GetBooleanProperty,(SDL_PropertiesID a, const char *b, SDL_bool c),(a,b,c),return) SDL_DYNAPI_PROC(int,SDL_GetCPUCacheLineSize,(void),(),return) -SDL_DYNAPI_PROC(int,SDL_GetCPUCount,(void),(),return) +SDL_DYNAPI_PROC(int,SDL_GetNumLogicalCPUCores,(void),(),return) SDL_DYNAPI_PROC(const char*,SDL_GetCameraDriver,(int a),(a),return) SDL_DYNAPI_PROC(SDL_bool,SDL_GetCameraFormat,(SDL_Camera *a, SDL_CameraSpec *b),(a,b),return) SDL_DYNAPI_PROC(SDL_CameraID,SDL_GetCameraID,(SDL_Camera *a),(a),return) diff --git a/test/testautomation_platform.c b/test/testautomation_platform.c index 585e27f00..d19314c8c 100644 --- a/test/testautomation_platform.c +++ b/test/testautomation_platform.c @@ -120,7 +120,7 @@ static int SDLCALL platform_testEndianessAndSwap(void *arg) /** * Tests SDL_GetXYZ() functions * \sa SDL_GetPlatform - * \sa SDL_GetCPUCount + * \sa SDL_GetNumLogicalCPUCores * \sa SDL_GetRevision * \sa SDL_GetCPUCacheLineSize */ @@ -142,10 +142,10 @@ static int SDLCALL platform_testGetFunctions(void *arg) (int)len); } - ret = SDL_GetCPUCount(); - SDLTest_AssertPass("SDL_GetCPUCount()"); + ret = SDL_GetNumLogicalCPUCores(); + SDLTest_AssertPass("SDL_GetNumLogicalCPUCores()"); SDLTest_AssertCheck(ret > 0, - "SDL_GetCPUCount(): expected count > 0, was: %i", + "SDL_GetNumLogicalCPUCores(): expected count > 0, was: %i", ret); ret = SDL_GetCPUCacheLineSize(); diff --git a/test/testffmpeg.c b/test/testffmpeg.c index 15fff8fc8..ecad2ae9e 100644 --- a/test/testffmpeg.c +++ b/test/testffmpeg.c @@ -476,7 +476,7 @@ static AVCodecContext *OpenVideoStream(AVFormatContext *ic, int stream, const AV context->strict_std_compliance = -2; /* Enable threaded decoding, VVC decode is slow */ - context->thread_count = SDL_GetCPUCount(); + context->thread_count = SDL_GetNumLogicalCPUCores(); context->thread_type = (FF_THREAD_FRAME | FF_THREAD_SLICE); } diff --git a/test/testplatform.c b/test/testplatform.c index 55cb0134a..09a3064e3 100644 --- a/test/testplatform.c +++ b/test/testplatform.c @@ -389,7 +389,7 @@ static int Test64Bit(SDL_bool verbose) static int TestCPUInfo(SDL_bool verbose) { if (verbose) { - SDL_Log("CPU count: %d\n", SDL_GetCPUCount()); + SDL_Log("Number of logical CPU cores: %d\n", SDL_GetNumLogicalCPUCores()); SDL_Log("CPU cache line size: %d\n", SDL_GetCPUCacheLineSize()); SDL_Log("AltiVec %s\n", SDL_HasAltiVec() ? "detected" : "not detected"); SDL_Log("MMX %s\n", SDL_HasMMX() ? "detected" : "not detected");