Files
Triforce-Source/third_party/a64-kit/lift/a64_rtgen.py
ruckus7382sfgsgdf 93eeb714c2 Triforce v0.4.0
What was merged from NX2navi (DLSS frame generation, AMD FSR 4 in Setup builds, texture detail by video memory, sprint, volume, frame caps, safer saves, the above-60 fixes), the ultrawide HUD at the screen edges, camera speed, movies without the AV1 Video Extension, the 5120x2880 + RenoDX HDR start fix, the renderer work of 11-14 and the first float changes of the lift.
2026-11-15 06:39:00 -06:00

95 lines
4.4 KiB
Python

"""Write suyu's runtime pieces next to the lifted code, adapted to a64-kit's guest model (rt/a64.h).
a64_suyu.h suyu's runtime header (FP scan/fixed-point helpers, FPX1 fast paths, prototypes) without its
GuestContext, host memory bridge and layout pins: rt/a64.h defines the context and the memory
accessors before including this, so the accessor prototypes here inherit their static inline.
a64_suyu.c the out-of-line pieces the translated code calls: FPX1 exact bodies (recomp_fpe_*), the FP
reciprocal estimates, SMULH/UMULH and the AES S-box.
Both are GPL-2.0-or-later (from suyu/arm64_to_c.h). They are regenerated on every lift, so they always match the
a64emit build that produced the code.
"""
import os
import re
import a64emit
def _cut(text, start_marker, end_marker):
a = text.find(start_marker)
b = text.find(end_marker, a)
if a < 0 or b < 0:
raise SystemExit("a64_rtgen: suyu runtime layout changed (%r .. %r not found): update a64_rtgen.py"
% (start_marker, end_marker))
return text[:a] + text[b:]
def runtime_h():
h = a64emit.runtime_text(0)
h = _cut(h, "typedef struct RecompHostMem {", "/* Where this module is actually loaded")
h = h.replace("extern uint64_t g_module_base;\n", "")
for proto in ("int recomp_save_init", "int recomp_save_write", "int recomp_save_read", "int recomp_save_delete",
"int recomp_save_exists", "int recomp_load_segments"):
h = re.sub(r"^%s\(.*\n" % re.escape(proto), "", h, flags=re.M)
# (every context parameter A64_NOESC, rt/a64.h: these functions keep no pointer to it)
h = re.sub(r"([(,]\s*)GuestContext\s*\*\s*(?=(?:\w+\s*)?[,)])", r"\1GuestContext *A64_NOESC ", h)
h = fma32_native(h)
return "/* Generated by a64-kit/lift/a64_rtgen.py from suyu/arm64_to_c.h (GPL-2.0-or-later). Do not edit. */\n" + h
# (triforce) The binary32 fused multiply-add's fast path on x86-64: suyu computes it in binary64 (three widenings, a multiply,
# an add, a narrowing and the midpoint test), because x86-64's baseline has no FMA. The game is built for x86-64-v3 where
# the CPU has it (nx2/build.py), and there one FMA instruction is the correctly rounded result, as on Arm. The tests that
# decide whether the native result is kept are unchanged. -DA64_FPX_NO_FMA32: the binary64 form.
FMA32_OLD = ("#if RECOMP_FPX_HOST == 1\n"
" const uint64_t t = recomp_fpx_fb(__builtin_fmaf(recomp_fpx_f(a), recomp_fpx_f(b), recomp_fpx_f(z)));\n"
"#elif RECOMP_FPX_HOST == 2\n"
" const double s = (double)recomp_fpx_f(a) * (double)recomp_fpx_f(b) + (double)recomp_fpx_f(z);\n")
FMA32_NEW = FMA32_OLD.replace("#if RECOMP_FPX_HOST == 1\n",
"#if RECOMP_FPX_HOST == 1 || (RECOMP_FPX_HOST == 2 && defined(__FMA__) && !defined(A64_FPX_NO_FMA32))\n")
def fma32_native(h):
if h.count(FMA32_OLD) != 1:
raise SystemExit("a64_rtgen: suyu's recomp_fpx_fma32 changed: update fma32_native")
return h.replace(FMA32_OLD, FMA32_NEW)
def _function(src, signature):
"""The text of one top-level C function, from its signature to its closing brace at column 0."""
a = src.find(signature)
if a < 0:
raise SystemExit("a64_rtgen: %r not found in suyu's runtime C" % signature)
b = src.find("\n}\n", a)
return src[a:b + 3]
def runtime_c():
rc = a64emit.runtime_text(1)
aes = rc[rc.find("static uint8_t recomp_gmul"):]
aes = aes[:aes.find("\n}\n", aes.find("const uint8_t* recomp_aes_sbox")) + 3]
parts = [
"/* Generated by a64-kit/lift/a64_rtgen.py from suyu/arm64_to_c.h (GPL-2.0-or-later). Do not edit. */",
'#define A64_RT_IMPL /* (the out-of-line definitions: rt/a64.h inlines them elsewhere) */',
'#include "a64.h"',
aes,
_function(rc, "uint64_t recomp_umulh(uint64_t a,uint64_t b){"),
_function(rc, "uint64_t recomp_smulh(uint64_t a,uint64_t b){"),
a64emit.runtime_text(2),
a64emit.runtime_text(3),
]
return "\n".join(parts)
def write(out_dir):
os.makedirs(out_dir, exist_ok=True)
for name, text in (("a64_suyu.h", runtime_h()), ("a64_suyu.c", runtime_c())):
path = os.path.join(out_dir, name)
if not os.path.exists(path) or open(path, encoding="utf-8").read() != text:
open(path, "w", encoding="utf-8", newline="\n").write(text)
if __name__ == "__main__":
import sys
write(sys.argv[1])