Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions runtime/game_cpu.h
Original file line number Diff line number Diff line change
@@ -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 <stdbool.h>
#include <string.h>

// 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;
}
22 changes: 5 additions & 17 deletions runtime/gamelib.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <dlfcn.h>
#endif

#include "game_cpu.h"
#include "game_sites.h"
#include "paths.h"
#include "rt.h"
Expand All @@ -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
Expand All @@ -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
}

Expand Down
60 changes: 60 additions & 0 deletions tools/test_game_cpu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Synthetic CPU/OS feature sets exercise the production target selection without requiring old hardware.
// make test-game-cpu
#include <assert.h>
#include <stdio.h>
#include <string.h>

#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;
}