From b491124b73eafe6d65467e9772c605246e56029b Mon Sep 17 00:00:00 2001 From: ShannoonRuaaa Date: Sun, 22 Nov 2026 01:22:58 -0800 Subject: [PATCH] Check complete x86-64 levels before selecting game code --- .github/workflows/windows.yml | 2 ++ Makefile | 9 ++++++ runtime/game_cpu.h | 15 +++++++++ runtime/gamelib.c | 22 +++---------- tools/test_game_cpu.c | 60 +++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 17 deletions(-) create mode 100644 runtime/game_cpu.h create mode 100644 tools/test_game_cpu.c diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index d4e0f07ad..64c4c6926 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -39,3 +39,5 @@ jobs: mingw-w64-ucrt-x86_64-spirv-tools mingw-w64-ucrt-x86_64-pkgconf mingw-w64-ucrt-x86_64-nasm - name: Build run: make -j$(nproc) && ls -l build/savagewind.exe build/savagewind.dll.a + - name: CPU target checks (synthetic hardware) + run: make test-game-cpu diff --git a/Makefile b/Makefile index 6df1bf29c..6f93ab3a5 100644 --- a/Makefile +++ b/Makefile @@ -727,6 +727,15 @@ $(B)/test_game_file_size: tools/test_game_file_size.c runtime/game_file_size.c r @mkdir -p $(B) $(CC) -O1 -Wall $(OSFLAGS) -Iruntime -o $@ tools/test_game_file_size.c runtime/game_file_size.c +$(B)/test_game_cpu: tools/test_game_cpu.c runtime/game_cpu.h + @mkdir -p $(B) + $(CC) -O1 -Wall -Wextra -Werror -std=gnu11 -Iruntime -o $@ tools/test_game_cpu.c + +test-game-cpu: $(B)/test_game_cpu + $(B)/test_game_cpu + +.PHONY: test-game-cpu + test-game-file-size: $(B)/test_game_file_size $(B)/test_game_file_size diff --git a/runtime/game_cpu.h b/runtime/game_cpu.h new file mode 100644 index 000000000..ec1dc78ba --- /dev/null +++ b/runtime/game_cpu.h @@ -0,0 +1,15 @@ +// The compiler's complete x86-64 levels, not a subset of the instructions a game library may use. +#pragma once +#include +#include + +// Other GENARCH values and non-x86 targets keep their existing unchecked behaviour. +static inline bool game_cpu_runs(const char *arch) { +#if defined(__x86_64__) + __builtin_cpu_init(); + if (strstr(arch, "x86-64-v3")) return __builtin_cpu_supports("x86-64-v3"); + if (strstr(arch, "x86-64-v2")) return __builtin_cpu_supports("x86-64-v2"); +#endif + (void)arch; + return true; +} diff --git a/runtime/gamelib.c b/runtime/gamelib.c index 37e5c29e1..afd68304a 100644 --- a/runtime/gamelib.c +++ b/runtime/gamelib.c @@ -11,6 +11,7 @@ #include #endif +#include "game_cpu.h" #include "game_sites.h" #include "paths.h" #include "rt.h" @@ -26,25 +27,12 @@ static char g_error[256]; #define GAMELIB_ARCH "" #endif -// Whether this CPU runs code built for arch: the x86-64 levels the Makefile uses. Another -march (a build's own -// GENARCH) isn't checked. -static bool cpu_runs(const char *arch) { -#if defined(__x86_64__) - __builtin_cpu_init(); - if (strstr(arch, "x86-64-v3")) - return __builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma") && __builtin_cpu_supports("bmi2"); - if (strstr(arch, "x86-64-v2")) return __builtin_cpu_supports("sse4.2") && __builtin_cpu_supports("popcnt"); -#endif - (void)arch; - return true; -} - bool gamelib_compat(void) { #ifdef GAMELIB_ID_COMPAT static int compat = -1; if (compat < 0) { const char *e = getenv("SAVAGEWIND_COMPAT_CPU"); - compat = (e && *e && strcmp(e, "0")) || !cpu_runs(GAMELIB_ARCH); + compat = (e && *e && strcmp(e, "0")) || !game_cpu_runs(GAMELIB_ARCH); } return compat; #else @@ -61,12 +49,12 @@ const char *gamelib_id(void) { const char *gamelib_cpu_problem(void) { #ifdef GAMELIB_ID_COMPAT - if (!cpu_runs(GAMELIB_ARCH_COMPAT)) + if (!game_cpu_runs(GAMELIB_ARCH_COMPAT)) return "a CPU with SSE4.2 (Intel Nehalem / 2008 or newer, AMD Bulldozer / 2011 or newer)"; return NULL; #else - return cpu_runs(GAMELIB_ARCH) ? NULL - : "a CPU with AVX2 (Intel Haswell / 2013 or newer, AMD Excavator / Zen or newer)"; + return game_cpu_runs(GAMELIB_ARCH) ? NULL + : "a CPU with AVX2 (Intel Haswell / 2013 or newer, AMD Excavator / Zen or newer)"; #endif } diff --git a/tools/test_game_cpu.c b/tools/test_game_cpu.c new file mode 100644 index 000000000..49e856403 --- /dev/null +++ b/tools/test_game_cpu.c @@ -0,0 +1,60 @@ +// Synthetic CPU/OS feature sets exercise the production target selection without requiring old hardware. +// make test-game-cpu +#include +#include +#include + +#if defined(__x86_64__) +static const char *const features[] = { + "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "cx16", "lahf_lm", + "avx", "avx2", "bmi", "bmi2", "f16c", "fma", "lzcnt", "movbe", "os_ymm"}; +#define V2_MASK ((1u << 7) - 1) +#define V3_MASK ((1u << 16) - 1) +static unsigned present, initialised; +static void cpu_init(void) { ++initialised; } +static int cpu_supports(const char *name) { + assert(initialised); + // Non-one return values also match GCC's bitmask-valued builtin. + if (!strcmp(name, "x86-64-v2")) return (present & V2_MASK) == V2_MASK ? 16 : 0; + if (!strcmp(name, "x86-64-v3")) return (present & V3_MASK) == V3_MASK ? 32 : 0; + // Support the old partial checks too: removing F16C, MOVBE, etc. must catch their false positives. + for (unsigned i = 0; i < sizeof features / sizeof *features; ++i) + if (!strcmp(name, features[i])) return present & (1u << i); + assert(!"unexpected CPU feature queried"); + return 0; +} +#define __builtin_cpu_init cpu_init +#define __builtin_cpu_supports cpu_supports +#endif +#include "game_cpu.h" + +static unsigned checks; +#define CHECK(x) do { assert(x); ++checks; } while (0) +int main(void) { +#if defined(__x86_64__) + present = V3_MASK; + CHECK(game_cpu_runs("-march=x86-64-v3")); + CHECK(game_cpu_runs("-march=x86-64-v2")); + CHECK(initialised == 2); + for (unsigned i = 0; i < sizeof features / sizeof *features; ++i) { + present = V3_MASK & ~(1u << i); + CHECK(!game_cpu_runs("-march=x86-64-v3")); + CHECK(game_cpu_runs("-march=x86-64-v2") == (i >= 7)); + } + present = V2_MASK; + CHECK(game_cpu_runs("x86-64-v2")); + CHECK(!game_cpu_runs("x86-64-v3")); + present = 0; + CHECK(!game_cpu_runs("x86-64-v2")); + CHECK(!game_cpu_runs("x86-64-v3")); +#else + CHECK(game_cpu_runs("x86-64-v2")); + CHECK(game_cpu_runs("x86-64-v3")); +#endif + CHECK(game_cpu_runs("")); + CHECK(game_cpu_runs("-march=native")); + CHECK(game_cpu_runs("-march=haswell")); + puts("game CPU target checks passed"); + printf("%u checks\n", checks); + return 0; +}