Run the game on x86-64-v2 CPUs without AVX2

-march=x86-64-v3 kept the port and the installer to CPUs with AVX2 and FMA. --cpu v2 compiles the generated code and the runtime for SSE4.2 instead. Without FMA3 the FP lowering calls fma/fmaf, so runtime/fma.c provides correctly rounded ones (musl, MIT) that honour MXCSR's rounding mode, FTZ and DAZ; the UCRT's software fallback rounds incorrectly by up to half an ulp. simd.h's fast paths keep their assumptions.

The installer's host check asks for x86-64-v2 and selects libtotkrt.a or libtotkrt-v2.a; both release scripts build and ship the pair. The differential tests compile fma.c as well, so a --cflags=-march=x86-64-v2 run tests it.

Tested on an i7-3770K (no AVX2, FMA or BMI) with an RTX 3060 Ti, game 1.4.3: the installer ran end to end, --selftest passed and play holds 40-60 fps at 1080p. fma and fmaf matched 152,288 exact vectors over two seeds, including ties, subnormals and every rounding mode, FTZ and DAZ combination. The v3 build is unchanged: fma.c is an empty translation unit there.
This commit is contained in:
CHEN Feng committed 2026-11-01 07:09:13 +08:00
1 parent 7d62cbfa85
commit 34b4faee7c
19 files changed
+402 -58

No files matched your search

+14
View File
@@ -92,9 +92,20 @@ NINJA = UCRT / 'ninja.exe' if WIN else on_path('ninja')
SDL3_DLLS = [UCRT / 'SDL3.dll', UCRT / 'libiconv-2.dll', UCRT / 'libopus-0.dll'] if WIN else []
# Flags for generated code as LAS Recomp builds it (Makefile GENFLAGS, Windows), plus -march for the FP/SIMD lowering.
# --cpu v2 replaces the level for CPUs without AVX2/FMA; runtime/fma.c then supplies the correctly rounded fma the
# lowering needs (runtime/simd.h).
GEN_FLAGS = ['-O1', '-fno-strict-aliasing', '-Wno-return-type', '-Wno-unused-label', '-fvisibility=hidden',
'-march=x86-64-v3']
RT_FLAGS = ['-O2', '-g', '-Wall', '-fno-strict-aliasing', '-march=x86-64-v3']
def set_cpu(cpu):
"""Target x86-64-v3 (the default) or x86-64-v2. RT_CPU_LEVEL lets runtime/main.c's self-test check the same
instruction set the build was compiled for."""
march = f'-march=x86-64-{cpu}'
GEN_FLAGS[GEN_FLAGS.index('-march=x86-64-v3')] = march
RT_FLAGS[RT_FLAGS.index('-march=x86-64-v3')] = march
RT_FLAGS.append(f'-DRT_CPU_LEVEL={3 if cpu == "v3" else 2}')
if WIN:
# _WIN32_WINNT: Windows 10, the port's minimum. MSYS2's headers default to it, llvm-mingw's (the release toolchain)
# to Windows 7, which hides the per-monitor DPI functions gfx_window_win.c and dpi_probe_win.c call.
@@ -698,6 +709,8 @@ def mib(n):
def main():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument('--version', default='v121', help='game version (default v121)')
ap.add_argument('--cpu', choices=('v2', 'v3'), default='v3',
help='target CPU level: v3 (AVX2/FMA, default) or v2 (SSE4.2, for older CPUs; slower)')
ap.add_argument('--gen', type=Path, help='generated C directory (default generated/<version>)')
ap.add_argument('--out', type=Path, help='build directory (default build/<version>)')
ap.add_argument('--modules', help='comma-separated subset of the layout modules (default all)')
@@ -732,6 +745,7 @@ def main():
ap.add_argument('--expected', type=Path,
help='expected hashes (default tools/data/expected-<display version>.json)')
args = ap.parse_args()
set_cpu(args.cpu)
gen = (args.gen or ROOT / 'generated' / args.version).resolve()
out = (args.out or ROOT / 'build' / args.version).resolve()
+19 -12
View File
@@ -10,8 +10,9 @@ cached in toolchain/release-dl/ and checked against the sha256 in PINS; a mismat
files come out of the python.org installer's own component MSI, unpacked with msiexec /a and expand.exe;
the installer is never run)
llvm/ llvm-mingw, x86_64 only, the files clang and lld need to compile and link totk.exe
runtime/ libtotkrt.a (build_game.py --make-runtime-lib with llvm/), the runtime headers, romfs.c, patches.c and
ov_zstd.c (compiled per install), deps/ (headers and import libraries of SDL3, Opus, libvpx, Vulkan and VMA)
runtime/ libtotkrt.a and libtotkrt-v2.a (build_game.py --make-runtime-lib with llvm/, x86-64-v3 and v2), the
runtime headers, romfs.c, patches.c and ov_zstd.c (compiled per install), deps/ (headers and import
libraries of SDL3, Opus, libvpx, Vulkan and VMA)
bin/ totk_xlate.exe (+ .id), hactool.exe, SDL3.dll (upstream), libopus-0.dll, ninja.exe
recomp/, tools/, data/, installer/, licenses/, THIRD_PARTY_NOTICES.md, totk.ini.example
VERSION the repository's VERSION plus the commit, 0.1.0+973c626; the installer's release string
@@ -342,15 +343,18 @@ def lifted_version():
def runtime_lib(out, llvm):
"""Build libtotkrt.a with the folder's llvm/ and copy it, the headers and the install-side sources into runtime/."""
build = MAIN_ROOT / 'build' / 'release-check' / 'rtlib'
"""Build libtotkrt.a (x86-64-v3) and libtotkrt-v2.a (x86-64-v2) with the folder's llvm/, and copy them, the headers
and the install-side sources into runtime/."""
version = lifted_version()
gen = MAIN_ROOT / 'generated' / version
run([sys.executable, ROOT / 'tools' / 'build_game.py', '--make-runtime-lib', '--toolchain', llvm, '--version', version,
'--deps', out / 'runtime' / 'deps', '--ninja', out / 'bin' / 'ninja.exe', '--gen', gen, '--out', build],
env=clean_env(llvm / 'bin', out / 'bin'))
rt = out / 'runtime'
shutil.copy2(build / 'libtotkrt.a', rt / 'libtotkrt.a')
for cpu, name in (('v3', 'libtotkrt.a'), ('v2', 'libtotkrt-v2.a')):
build = MAIN_ROOT / 'build' / 'release-check' / f'rtlib-{cpu}'
run([sys.executable, ROOT / 'tools' / 'build_game.py', '--make-runtime-lib', '--toolchain', llvm, '--cpu', cpu,
'--version', version, '--deps', out / 'runtime' / 'deps', '--ninja', out / 'bin' / 'ninja.exe',
'--gen', gen, '--out', build],
env=clean_env(llvm / 'bin', out / 'bin'))
shutil.copy2(build / 'libtotkrt.a', rt / name)
for p in sorted((ROOT / 'runtime').rglob('*')):
if p.suffix in RUNTIME_SUFFIXES and p.is_file():
(rt / p.relative_to(ROOT / 'runtime')).parent.mkdir(parents=True, exist_ok=True)
@@ -477,7 +481,7 @@ def write_zip(out, path):
print(f'{path}: {mib(path.stat().st_size)}')
def prove(out):
def prove(out, cpu='v3'):
"""Compile the generated C and link totk.exe with the folder's tools only, then run the probes next to it."""
out = out.resolve()
version = lifted_version()
@@ -486,8 +490,9 @@ def prove(out):
chk = MAIN_ROOT / 'build' / 'release-check'
env = clean_env(out / 'python', out / 'llvm' / 'bin', out / 'bin')
t0 = time.time()
run([out / 'python' / 'python.exe', out / 'tools' / 'build_game.py', '--toolchain', out / 'llvm',
'--deps', out / 'runtime' / 'deps', '--runtime', out / 'runtime', '--runtime-lib', out / 'runtime' / 'libtotkrt.a',
run([out / 'python' / 'python.exe', out / 'tools' / 'build_game.py', '--toolchain', out / 'llvm', '--cpu', cpu,
'--deps', out / 'runtime' / 'deps', '--runtime', out / 'runtime',
'--runtime-lib', out / 'runtime' / ('libtotkrt-v2.a' if cpu == 'v2' else 'libtotkrt.a'),
'--ninja', out / 'bin' / 'ninja.exe', '--version', version, '--gen', gen, '--out', chk, '--exefs', ext / 'exefs',
'--romfs', ext / 'romfs', '--profile', chk / 'profile'], env=env)
print(f'prove: build {time.time() - t0:.0f} s')
@@ -510,10 +515,12 @@ def main():
ap.add_argument('--out', type=Path, required=True, help='release folder (replaced)')
ap.add_argument('--zip', type=Path, help='also write the folder as this zip')
ap.add_argument('--prove', action='store_true', help='only run the compile-and-probe check on an existing folder')
ap.add_argument('--cpu', choices=('v2', 'v3'), default='v3',
help='--prove: build and run the check for this CPU level (default v3)')
args = ap.parse_args()
out = args.out.resolve()
if args.prove:
prove(out)
prove(out, args.cpu)
return
if out.exists():
shutil.rmtree(out)
+18 -13
View File
@@ -14,9 +14,9 @@ the .NET 10 SDK and Python 3.12+ with Pillow (.venv). The folder holds:
file puts the folder, tools/ and recomp/ on sys.path
llvm/ LLVM 23 (clang, lld, llvm-ar, the clang resource headers) and llvm/sysroot: the files of the sniper SDK
sysroot the game's compile and link read, nothing else (recorded while building the runtime library)
runtime/ libtotkrt.a (build_game.py --make-runtime-lib --sysroot), the runtime headers, romfs.c, patches.c and
ov_zstd.c (compiled per install), totk.png (the window icon), deps/ (Vulkan headers 1.4.363, VMA, zstd
built static; SDL3, Opus and libvpx come from the sysroot)
runtime/ libtotkrt.a and libtotkrt-v2.a (build_game.py --make-runtime-lib --sysroot, x86-64-v3 and v2), the
runtime headers, romfs.c, patches.c and ov_zstd.c (compiled per install), totk.png (the window icon),
deps/ (Vulkan headers 1.4.363, VMA, zstd built static; SDL3, Opus and libvpx come from the sysroot)
bin/ totk_xlate (+ .id), hactool, ninja, libSDL3.so.0 (the sysroot's; the game finds it beside itself)
recomp/, tools/, data/, installer/, licenses/, THIRD_PARTY_NOTICES.md, totk.ini.example, VERSION
AppRun, totk-setup.desktop, totk-setup.png the AppImage entry: python/bin/python3 -m installer.gui
@@ -197,14 +197,16 @@ def build_bin(out, llvm, sysroot):
# ---- runtime/ ----------------------------------------------------------------------------------------------------------
def runtime_lib(out, llvm, sysroot):
build = CHECK / 'rtlib'
shutil.rmtree(build, ignore_errors=True)
"""libtotkrt.a (x86-64-v3) and libtotkrt-v2.a (x86-64-v2), so a machine without AVX2 can install too."""
version = mr.lifted_version()
run([sys.executable, ROOT / 'tools' / 'build_game.py', '--make-runtime-lib', '--toolchain', llvm, '--sysroot', sysroot,
'--deps', out / 'runtime' / 'deps', '--ninja', out / 'bin' / 'ninja', '--version', version,
'--gen', MAIN_ROOT / 'generated' / version, '--out', build])
rt = out / 'runtime'
shutil.copy2(build / 'libtotkrt.a', rt / 'libtotkrt.a')
for cpu, name in (('v3', 'libtotkrt.a'), ('v2', 'libtotkrt-v2.a')):
build = CHECK / f'rtlib-{cpu}'
shutil.rmtree(build, ignore_errors=True)
run([sys.executable, ROOT / 'tools' / 'build_game.py', '--make-runtime-lib', '--toolchain', llvm,
'--sysroot', sysroot, '--cpu', cpu, '--deps', out / 'runtime' / 'deps', '--ninja', out / 'bin' / 'ninja',
'--version', version, '--gen', MAIN_ROOT / 'generated' / version, '--out', build])
shutil.copy2(build / 'libtotkrt.a', rt / name)
for p in sorted((ROOT / 'runtime').rglob('*')):
if p.suffix in mr.RUNTIME_SUFFIXES and p.is_file():
(rt / p.relative_to(ROOT / 'runtime')).parent.mkdir(parents=True, exist_ok=True)
@@ -459,7 +461,7 @@ def check_bundle(out):
print('bundle check: the window built every screen in both languages and closed')
def prove(out):
def prove(out, cpu='v3'):
"""Compile the generated C and link totk with the folder's tools only, then run the probes beside it."""
out = out.resolve()
version = mr.lifted_version()
@@ -468,9 +470,10 @@ def prove(out):
chk = CHECK / 'game'
env = dict(os.environ, PATH=os.pathsep.join([str(out / 'llvm' / 'bin'), str(out / 'bin'), '/usr/bin', '/bin']))
t0 = time.time()
run([out / 'python' / 'bin' / 'python3', out / 'tools' / 'build_game.py', '--toolchain', out / 'llvm',
run([out / 'python' / 'bin' / 'python3', out / 'tools' / 'build_game.py', '--toolchain', out / 'llvm', '--cpu', cpu,
'--sysroot', out / 'llvm' / 'sysroot', '--deps', out / 'runtime' / 'deps', '--runtime', out / 'runtime',
'--runtime-lib', out / 'runtime' / 'libtotkrt.a', '--ninja', out / 'bin' / 'ninja', '--version', version,
'--runtime-lib', out / 'runtime' / ('libtotkrt-v2.a' if cpu == 'v2' else 'libtotkrt.a'),
'--ninja', out / 'bin' / 'ninja', '--version', version,
'--gen', gen, '--out', chk, '--exefs', ext / 'exefs', '--romfs', ext / 'romfs', '--profile', chk / 'profile'],
env=env)
print(f'prove: build {time.time() - t0:.0f} s')
@@ -497,10 +500,12 @@ def main():
ap.add_argument('--out', type=Path, required=True, help='release folder (replaced)')
ap.add_argument('--appimage', type=Path, help='also pack the folder as this AppImage')
ap.add_argument('--prove', action='store_true', help='only run the compile-and-probe check on an existing folder')
ap.add_argument('--cpu', choices=('v2', 'v3'), default='v3',
help='--prove: build and run the check for this CPU level (default v3)')
args = ap.parse_args()
out = args.out.resolve()
if args.prove:
prove(out)
prove(out, args.cpu)
return
if out.exists():
shutil.rmtree(out)