"""(Triforce) Fast float regions in lifted code (A64_FASTFP at lift time; off by default), outside the vendored kit's method. A straight run of single-precision float math is lifted twice: a fast copy of plain SSE ops checked once by MXCSR, and the exact copy (the lifted code as before) as the fallback. The rules and the header the fast copies compile against are below (ff_region, FASTFP_HEADER); see the comment above FF_CALL. This module: - replaces a64_lift.Lifter.emit_function with emit_function below: a copy of the kit's, with the lines that gather a run of joinable instructions and emit it through ff_region added, each tagged "# (fastfp)". tests/test_fastfp_override.py checks that the copy without its tagged lines is the kit's method, line for line: a change of the kit's method fails it until this copy follows; - wraps a64_lift.Lifter.write: FASTFP_HEADER appended to a64_fpx.h after the kit writes it, and the regions' count printed. install() runs before nx2/lift_fixes.py's and nx2/poll.py's wrappers of emit_function (nx2/lift.py), so they wrap this one. The lift's emit processes (nx2/emit_pool.py) run nx2/lift.py's module level again, so they emit with it too. Triforce's rules on top: - off unless A64_FASTFP is set (1: every function; hot: the A64_HOT_FUNCS list only; check: both copies every time, compared, the exact one's result kept): it has not run in the game here. With it unset or 0 the kit's own method stays installed and the lift's C is the kit's to the byte; - no region at all in a function that holds an address the game's patches name (nx2/oot_patches.py and the other files that edit the lifted text after the lift, patch_sources): those edits match the lift's exact text, once, and a region writes its instructions twice, the fast copy's float ops under other names. An edit would change the exact copy alone and the fast copy, the one that runs, would keep the game's own arithmetic (the frame-rate fixes are such edits); - nx2/vecfp.py's helpers (A64_VECFP: a64_vfpx_q_...) end a run: they have not been checked inside the MXCSR window. """ import os import re import a64_lift import a64emit # (the kit's names the copied method reads; nothing in nx2 rebinds them: nx2/insn.py replaces a64_lift.classify and # a64emit.translate, which the method does not name, or names through the module. fp_is_frame is the kit's, its answers # kept below) from a64_lift import (COND, FLAG_REF, FP_STORE, FP_STORE_PRE, MG_NOT_STRAIGHT, REG_REF, SYNC_ALL, SYNC_BACK, SYNC_CALL, SYNC_RELOAD, SYNC_RET, drop_flag_stores, frame_stores, local_here, local_regs, localize_regs, pc_relative) _frame = {} def fp_is_frame(text): """a64_lift.fp_is_frame, kept by the text: the copied method below asks it of every instruction (14 million in a lift of OoT, two regex searches each), of far fewer distinct texts""" f = _frame.get(text) if f is None: f = _frame[text] = a64_lift.fp_is_frame(text) return f # (Triforce) Fast float regions (A64_FASTFP at lift time: off unless set; "1" on; "hot" the A64_HOT_FUNCS list only; "check" # both copies every time, compared: see ff_region). # Each float op on the exact path carries its own tests (~25 instructions around one SSE op). A straight run of # instructions with no label, call, branch or system access inside (and no guest load after a guest store: see # FF_STORE below; with A64_FASTFP_STORES=0 no store at all), holding at least A64_FASTFP_MIN (8) single- # precision adds, subtracts, multiplies, divides or square roots, is emitted twice: a fast copy whose ops are plain SSE, # with MXCSR's flags cleared before it and read once after, and the exact copy. A clean fast run is the Arm result: with # no invalid operation (no NaN made), no denormal operand and no underflow (FTZ on under FPCR.FZ: a tiny result is # flushed and flagged), IEEE single precision round-to-nearest is the same on both, except for a NaN result or a # product, quotient or multiply-add of exactly 2^-126, which raise the invalid flag on purpose (a64_ff_guard_; the ops # are asm with Arm's operand order all the same). Divide-by-zero and overflow give the same infinities; they and inexact # are or-ed into FPSR. On an invalid, denormal or underflow flag the registers the run names are put back and the exact # copy runs. Measured # 2026-11-11 on OoT's f_820e1d60 in game (both run per call, compared): 0 mismatches in 150,000 calls, 1.79x faster. # HOS_NO_FASTFP (run time, any value): the exact copies only, for comparisons. FF_CALL = re.compile(r"\b((?:recomp|a64|A64|RECOMP)_\w+)\s*\(") FF_PURE = {"recomp_load8", "recomp_load16", "recomp_load32", "recomp_load64", "recomp_ldp32", "recomp_ldp64", "a64_vset64", "a64_vset128", "a64_vset_lo", "a64_vst16", "a64_fcmp32", "a64_fcmp64", "a64_vfcmp", "recomp_fixed_to_fp", "recomp_fp_to_int", "recomp_fp_to_fixed", "recomp_umulh", "recomp_smulh", "recomp_fp_estimate", "recomp_aes_sbox", "RECOMP_LIKELY", "RECOMP_UNLIKELY", "RECOMP_FPX_OPEN", "RECOMP_FPX_FZ", "a64_fcmp32k", "a64_fcmp64k"} # ((Triforce) nx2/fastvec.py's whole-register moves and byte shuffles: LDR Q as a64_vld16 (a guest load: FF_LOAD), # DUP by element as a64_vdup, EXT as A64_EXT16 / A64_EXT8, TBL as a64_tbl1. Integer SSE on the registers, no float flag; # before those rewrites the same instructions were recomp_ldp64 and memcpy / a64_vst16 blocks, which joined. Named after FF_PURE was # written, they ended runs: in the lifted C of that lift, ~23,700 run ends within 6 statements of float ops on both # sides in the functions play runs (8,100 a64_vld16, 7,500 a64_vdup, 6,700 A64_EXT16). A64_FASTFP_VMOVES=0 at lift time: # they end a run again) FF_VMOVES = {"a64_vld16", "a64_vdup", "A64_EXT16", "A64_EXT8", "a64_tbl1"} FF_BAD = ("c->fpcr", "c->fpsr", "c->excl", "c->pc", "c->halted", "c->poll", "c->tpidr", "\x01", "goto ", "return", "switch", "A64_PTR", "A64_MARK") # (the exact blocks' own uses, safe in a region: FPCR read in a test (it doesn't change inside one: no MSR there), and # FPSR's cumulative bits or-ed in (put back with the registers when the region runs again exactly). An MSR, or an MRS of # FPSR, which would see a value the fast copy has not finished, still ends the run.) FF_FP_OK = re.compile(r"c->fpcr\s*&|c->fpsr\s*\|=") # ((Triforce) fma32_fma32: FMADD/FMSUB/FNMADD/FNMSUB and FMLA/FMLS S lanes, an FMA3 op where the build has one; see # a64_ff_fma32_fma32 in FASTFP_HEADER) FF_CONV = re.compile(r"\ba64_vfpx_(add32|sub32|mul32)\(|" r"\ba64_fpx_(add32_add32|sub32_sub32|mul32_fma32|fma32_fma32|div32_div32|sqrt32_sqrt32)\(") VREG_REF = re.compile(r"c->vreg\[(\d+)\]") def fastfp_mode(): """the mode: empty when A64_FASTFP is unset, empty or 0 (off: Triforce's default), else 1 (every function), hot or check; anything else stops the lift, so a typo cannot turn the regions on or off unnoticed""" v = os.environ.get("A64_FASTFP", "").strip().lower() if v in ("", "0", "off", "false", "no"): return "" if v in ("1", "on", "true", "yes"): return "1" if v in ("hot", "check"): return v raise SystemExit("A64_FASTFP=%s: 0, 1, hot or check" % os.environ.get("A64_FASTFP")) def ff_min(): v = os.environ.get("A64_FASTFP_MIN", "") return int(v) if v.isdigit() else 8 # (Triforce) Guest stores inside a region (A64_FASTFP_STORES at lift time: on unless "0"). A plain store (STR, STP, with # or without writeback, through any base: recomp_storeN / recomp_stpN and their _sp forms) may join a run, and the run is # cut before a guest load that follows a store in it (ff_split), so every load of a region comes before every store of # it. The exact copy is the lifted text as before. The fast copy does not store: each store's address and values go # into locals (_ffaK, _ffdK, and _ffeK for a pair's second value) where the store stood, and once a64_ff_end has passed # the stores run from them, in their order, with the same helpers (so the same page marks), before the jump past the # exact copy. On a flag that sends the run to its exact copy nothing has been stored, the registers are put back and the # exact copy stores as before. The guest's memory sees the same accesses in the same order either way (the region's # loads, then its stores), only later by the region's register work; another thread could see a store a few # instructions later, never a different one or out of order. A store through x29 that the function's frame stores would # make an unmarked _sp store (a64_lift.frame_stores) names its locals _ffp..., and ff_frame_stores does the same to the # deferred one. Not joined: STLR (a plain recomp_storeN(c,c->x[N],...), as mark_groups reads it), a store under a # condition (MG_NOT_STRAIGHT), and a text whose own load follows its store; exclusive, atomic and memcpy (A64_PTR) # stores stay out by FF_BAD. FF_STORE = re.compile(r"\brecomp_(store8|store16|store32|store64|stp32|stp64)(_sp)?\(") FF_STORE_SIZE = {"store8": 8, "store16": 16, "store32": 32, "store64": 64, "stp32": 32, "stp64": 64} FF_LOAD = re.compile(r"\b(?:recomp_(?:load(?:8|16|32|64)|ldp(?:32|64))|a64_vld16)\(") FF_STLR = re.compile(r"\brecomp_(?:store|stp)\d+(?:_sp)?\(c,c->x\[\d+\],") FF_FRAME = re.compile(r"\brecomp_(store8|store16|store32|store64|stp32|stp64)\(c,_ffp") def ff_stores(): """A64_FASTFP_STORES: guest stores may sit inside a region (on unless "0")""" return os.environ.get("A64_FASTFP_STORES", "1") != "0" def ff_args(text, at): """the top-level arguments of the call whose "(" is at text[at], and the index after its ")"; None if unbalanced""" depth, args, start = 0, [], at + 1 for j in range(at, len(text)): ch = text[j] if ch == "(": depth += 1 elif ch == ")": depth -= 1 if depth == 0: args.append(text[start:j].strip()) return args, j + 1 elif ch == "," and depth == 1: args.append(text[start:j].strip()) start = j + 1 return None def ff_store_ok(text): """a text with guest stores may join a region (ff_stores on): plain stores, each a whole statement of its own that always runs, no load after the first one""" if FF_STLR.search(text) or MG_NOT_STRAIGHT.search(text): return False first = None for m in FF_STORE.finditer(text): first = m.start() if first is None else first p = ff_args(text, m.end() - 1) if p is None or p[0][0] != "c" or len(p[0]) != (4 if m.group(1).startswith("stp") else 3): return False if not text[p[1]:].lstrip().startswith(";") or text[:m.start()].rstrip()[-1:] not in ("", ";", "{", "}"): return False return first is None or not FF_LOAD.search(text, first) def ff_vmoves(): """A64_FASTFP_VMOVES: nx2/fastvec.py's vector moves and shuffles (FF_VMOVES) may sit inside a region (on unless "0")""" return os.environ.get("A64_FASTFP_VMOVES", "1") != "0" def ff_eligible(text, stores=None): """an instruction's C text may sit inside a fast float region: no control flow or system state; only pure calls, and guest stores when stores (ff_stores() unless given) and ff_store_ok""" rest = FF_FP_OK.sub("", text) if any(b in rest for b in FF_BAD): return False names = FF_CALL.findall(text) if any(n.startswith("a64_vfpx_q_") for n in names): # ((Triforce) nx2/vecfp.py's fast paths: not inside a region) return False pure = FF_PURE | FF_VMOVES if ff_vmoves() else FF_PURE if all(n in pure or n.startswith(("a64_fpx_", "a64_vfpx_")) for n in names): return True if not (ff_stores() if stores is None else stores): return False return all(n in pure or n.startswith(("a64_fpx_", "a64_vfpx_")) or FF_STORE.match(n + "(") for n in names) and \ ff_store_ok(text) def ff_split(items): """a run cut into the parts a region may hold: a new part before each instruction with a guest load that follows a guest store of the part so far (no load after a store inside a region, so the stores can wait for its end)""" parts, cur, stored = [], [], False for item in items: t = item[1] if stored and ("recomp_l" in t or "a64_vld16" in t) and FF_LOAD.search(t): parts.append(cur) cur, stored = [], False cur.append(item) if "recomp_st" in t and FF_STORE.search(t): stored = True if cur: parts.append(cur) return parts def ff_defer(text, k, record=False): """one instruction's text with its guest stores written as assignments of their address and values to the region's locals, numbered from k: (the text, [(helper, locals' prefix, number, pair)]). record: the stores kept, each after its assignment (the check copy's exact text, A64_FASTFP=check)""" if "recomp_st" not in text: return text, [] out, stores, at = [], [], 0 for m in FF_STORE.finditer(text): if m.start() < at: continue args, end = ff_args(text, m.end() - 1) name = "recomp_%s%s" % (m.group(1), m.group(2) or "") frame = not m.group(2) and (FP_STORE.match(text, m.start()) is not None or any(p.end(1) == m.start() for p in FP_STORE_PRE.finditer(text))) pre = ("_ffp" if frame else "_ff") + ("o" if record else "") vals = ["%sa%d=(%s)" % (pre, k, args[1]), "%sd%d=(%s)" % (pre, k, args[2])] if len(args) == 4: vals.append("%se%d=(%s)" % (pre, k, args[3])) if record: vals.append("%s(c,%sa%d,%sd%d%s)" % (name, pre, k, pre, k, ",%se%d" % (pre, k) if len(args) == 4 else "")) out.append(text[at:m.start()] + "(" + ",".join(vals) + ")") stores.append((name, pre, k, len(args) == 4)) at, k = end, k + 1 out.append(text[at:]) return "".join(out), stores def ff_frame_stores(text): """the deferred stores through x29 (their locals named _ffp...) as frame_stores makes their exact copies: _sp, unmarked. Run where emit_function runs frame_stores""" return FF_FRAME.sub(r"recomp_\1_sp(c,_ffp", text) if "(c,_ffp" in text else text _patched_addrs = [None] # (set_patched: mentioned_addresses below, asked once a process) _patched = [] # (those addresses: [sorted list, set]; made on first use) def ff_patched(e, body, pcs): """the function at e (its body, pcs: its addresses in order) holds an address the game's patches name, its entry or an instruction's (patch_sources: an edit of the lifted text pins its place by one). (Triforce) Such a function gets no region: an edit's text must stay one text, in the one copy that runs""" if not _patched: addrs = sorted(_patched_addrs[0]()) if _patched_addrs[0] else [] _patched[:] = [addrs, set(addrs)] addrs, aset = _patched if not addrs or not pcs: return False if e in aset: return True import bisect for i in range(bisect.bisect_left(addrs, pcs[0]), bisect.bisect_right(addrs, pcs[-1])): if addrs[i] in body: return True return False def ff_regions(items, least=None, stores=None): """a run's C text, cut by ff_split and each part through ff_region: (text, regions made, float ops made fast). stores: the run may hold guest stores (ff_stores() unless given; emit_function: not in a function the game's patches edit)""" least = ff_min() if least is None else least if not (ff_stores() if stores is None else stores) or len(items) < 2 or \ not any("recomp_st" in t for _, t in items) or sum(ff_convs(t) for _, t in items) < least: text, n = ff_region(items, least) # (no store, or too few ops for any part of it: as before) return text, int(n > 0), n texts, regions, ops = [], 0, 0 for part in ff_split(items): text, n = ff_region(part, least) texts.append(text) regions, ops = regions + (n > 0), ops + n return "\n".join(texts), regions, ops # (the emit asks these of every instruction of every function, ~13 million times in a lift of OoT, but there are far fewer # distinct instruction texts: each answer is kept by its text. Before, ff_eligible ran its FF_FP_OK.sub once for each of # FF_BAD's 13 words, on every instruction, and emit and write took 106 s where it took ~46 s without the regions) _joins = {} _convs = {} def ff_joins(text, dead, stores=False): """ff_eligible of an instruction's text with the flag stores dead there (dead_flags' mask) dropped, by (text, dead, stores)""" j = _joins.get((text, dead, stores)) if j is None: j = _joins[(text, dead, stores)] = ff_eligible(drop_flag_stores(text, dead) if dead else text, stores) return j def ff_convs(text): """the float ops ff_region can make fast in one instruction's text (FF_CONV's matches)""" n = _convs.get(text) if n is None: n = _convs[text] = len(FF_CONV.findall(text)) return n def ff_region(items, least=None): """a run's C text ([(pc, text)], every text ff_eligible): the fast and exact copies, or the exact text alone when the run has too few float ops to pay for its check. (text, number of ops made fast). least: ff_min(), read once by the caller (emit_function: once a function, not for each of the 3.8 million runs of OoT)""" exact = "\n".join(t for _, t in items) # (FF_CONV's matches are in one instruction's text each, so the run's count is their sum: most runs are a few # instructions with no float op, and only a run with enough of them is searched again below) n = sum(ff_convs(t) for _, t in items) if n < (ff_min() if least is None else least): return exact, 0 # (guest stores, FF_STORE: the fast copy's assign their address and values to locals, stored once a64_ff_end passed) stores = [] if "recomp_st" in exact: fast_items = [] for _, t in items: t, s = ff_defer(t, len(stores)) fast_items.append(t) stores += s fast = "\n".join(fast_items) else: fast = exact fast = FF_CONV.sub(lambda m: "a64_ff_v%s(" % m.group(1) if m.group(1) else "a64_ff_%s(" % m.group(2), fast) xs = sorted({int(r) for r in REG_REF.findall(exact)}) vs = sorted({int(r) for r in VREG_REF.findall(exact)}) fl = sorted(set(FLAG_REF.findall(exact))) save = ("".join("const uint64_t _ffx%d = c->x[%d]; " % (r, r) for r in xs) + "".join("const uint64_t _ffv%d0 = c->vreg[%d][0], _ffv%d1 = c->vreg[%d][1]; " % (r, r, r, r) for r in vs) + "".join("const uint8_t _fff%s = c->%s; " % (f, f) for f in fl) + "const uint64_t _ffsr = c->fpsr; ") restore = ("".join("c->x[%d] = _ffx%d; " % (r, r) for r in xs) + "".join("c->vreg[%d][0] = _ffv%d0; c->vreg[%d][1] = _ffv%d1; " % (r, r, r, r) for r in vs) + "".join("c->%s = _fff%s; " % (f, f) for f in fl) + "c->fpsr = _ffsr; ") label = "L_ff_%08x" % items[0][0] # (the stores' locals, declared with the saved registers; and the stores, run once the fast copy is kept) temps = "".join("uint64_t %sa%d, %sd%d%s; " % (p, k, p, k, ", %se%d" % (p, k) if pair else "") for _, p, k, pair in stores) deferred = "".join("%s(c,%sa%d,%sd%d%s); " % (h, p, k, p, k, ",%se%d" % (p, k) if pair else "") for h, p, k, pair in stores) if fastfp_mode() == "check": # (A64_FASTFP=check: both copies every time, the exact one's result kept, the registers the run names compared, # and each store's address and values (the exact copy's noted as it stores, _ffo...); a64_ff_check counts and # prints the first mismatches with the run's guest address. The fast copy stores nothing here) keep = ("".join("const uint64_t _ffox%d = c->x[%d]; " % (r, r) for r in xs) + "".join("const uint64_t _ffov%d0 = c->vreg[%d][0], _ffov%d1 = c->vreg[%d][1]; " % (r, r, r, r) for r in vs) + "".join("const uint8_t _ffof%s = c->%s; " % (f, f) for f in fl)) same = " && ".join(["1"] + ["_ffox%d == c->x[%d]" % (r, r) for r in xs] + ["_ffov%d0 == c->vreg[%d][0] && _ffov%d1 == c->vreg[%d][1]" % (r, r, r, r) for r in vs] + ["_ffof%s == c->%s" % (f, f) for f in fl]) checked = exact if stores: checked, k = [], 0 for _, t in items: t, s = ff_defer(t, k, record=True) checked.append(t) k += len(s) checked = "\n".join(checked) temps += "".join("uint64_t %soa%d, %sod%d%s; " % (p, k, p, k, ", %soe%d" % (p, k) if pair else "") for _, p, k, pair in stores) for h, p, k, pair in stores: ty = "uint%d_t" % FF_STORE_SIZE[h[len("recomp_"):].replace("_sp", "")] same += " && %sa%d == %soa%d && (%s)%sd%d == (%s)%sod%d" % (p, k, p, k, ty, p, k, ty, p, k) if pair: same += " && (%s)%se%d == (%s)%soe%d" % (ty, p, k, ty, p, k) # (the "}" of its else not at the start of a line: one there closes the function, nx2/oot_patches.py and nx2/chunks.py) return ("{ if (A64_FF_ON(c)) { %s%sconst unsigned _ffm = a64_ff_begin(c);\n%s\n" "const int _ffok = a64_ff_end(c, _ffm);\n%s%s\n%s\na64_ff_check(0x%xu, _ffok, %s); } else {\n%s\n } }" % (save, temps, fast, keep, restore, checked, items[0][0], same, exact)), n if stores: return ("{ if (A64_FF_ON(c)) { %s%sconst unsigned _ffm = a64_ff_begin(c);\n%s\n" "if (RECOMP_LIKELY(a64_ff_end(c, _ffm))) { %sgoto %s; }\n%s}\n%s\n%s:; }" % (save, temps, fast, deferred, label, restore, exact, label)), n return ("{ if (A64_FF_ON(c)) { %sconst unsigned _ffm = a64_ff_begin(c);\n%s\n" "if (RECOMP_LIKELY(a64_ff_end(c, _ffm))) goto %s;\n%s}\n%s\n%s:; }" % (save, fast, label, restore, exact, label)), n FASTFP_HEADER = r""" /* ---- (Triforce) fast float regions (A64_FASTFP, ff_region in a64_lift.py) ---- */ #if RECOMP_FPX_HOST == 2 && defined(__x86_64__) && !defined(__EMSCRIPTEN__) #include #include static inline __attribute__((always_inline)) int a64_ff_enabled_(void) { static int v = -1; if (v < 0) v = !getenv("HOS_NO_FASTFP"); return v; } #define A64_FF_ON(c) (RECOMP_FPX_OPEN(c) && a64_ff_enabled_()) /* MXCSR: exceptions masked, round to nearest, DAZ off, FTZ as FPCR.FZ, flags cleared; the old value returned */ static inline __attribute__((always_inline)) unsigned a64_ff_begin(const Ctx *c) { unsigned m, n; __asm__ volatile("stmxcsr %0" : "=m"(m)); n = (m & ~0xE07Fu) | 0x1F80u | (RECOMP_FPX_FZ(c) ? 0x8000u : 0u); __asm__ volatile("ldmxcsr %0" : : "m"(n)); return m; } /* 0: an invalid, denormal or underflow flag (the exact copy runs); else inexact, overflow, divide-by-zero to FPSR */ static inline __attribute__((always_inline)) int a64_ff_end(Ctx *c, unsigned m) { unsigned f; __asm__ volatile("stmxcsr %0" : "=m"(f)); __asm__ volatile("ldmxcsr %0" : : "m"(m)); if (f & 0x13u) return 0; c->fpsr |= (f & 0x20u ? 0x10u : 0u) | (f & 0x08u ? 0x04u : 0u) | (f & 0x04u ? 0x02u : 0u); return 1; } /* (asm volatile: kept inside the MXCSR window and in Arm's operand order, see ff_region) */ #ifdef __AVX__ #define A64_FF_OP(ins, d, a, b) __asm__ volatile("v" ins " %2, %1, %0" : "=x"(d) : "x"(a), "x"(b)) #define A64_FF_SQRT(d, a) __asm__ volatile("vsqrtss %1, %1, %0" : "=x"(d) : "x"(a)) #define A64_FF_SPOIL "vcomiss %0, %0" #else #define A64_FF_OP(ins, d, a, b) do { (d) = (a); __asm__ volatile(ins " %1, %0" : "+x"(d) : "x"(b)); } while (0) #define A64_FF_SQRT(d, a) __asm__ volatile("sqrtss %1, %0" : "=x"(d) : "x"(a)) #define A64_FF_SPOIL "comiss %0, %0" #endif /* The invalid flag raised on purpose (a signalling compare of a NaN), sending the run to its exact copy, for two results whose value x86 gets right but not Arm's flags, or under FZ not Arm's value (found by tests/test_fastfp_fma.py): - a NaN: x86 raises no denormal flag for a subnormal next to a NaN operand, where Arm under FZ flushes it and sets IDC; and a multiply-add's 0 * inf + a quiet NaN gives that NaN with no flag (Arm: the default NaN, IOC); - exactly 2^-126, the smallest normal, from a multiply, divide or multiply-add: x86 detects tininess after rounding, Arm before, so a result rounded up to 2^-126 sets UFC on Arm, and is a zero under FZ. (A sum this small is exact.) The tests are integer compares on the bits: a float compare would raise the denormal flag on a subnormal result. */ static inline __attribute__((always_inline)) void a64_ff_spoil_(void) { __asm__ volatile(A64_FF_SPOIL : : "x"(_mm_castsi128_ps(_mm_cvtsi32_si128(0x7fc00000))) : "cc"); } static inline __attribute__((always_inline)) void a64_ff_guard_(uint64_t v, int tiny) { const uint32_t m = (uint32_t)v & 0x7fffffffu; if (__builtin_expect(m > 0x7f800000u || (tiny && m == 0x00800000u), 0)) a64_ff_spoil_(); } static inline __attribute__((always_inline)) void a64_ff_vguard_(__m128 r, int k, int tiny) { /* (the k lanes kept) */ const __m128i m = _mm_and_si128(_mm_castps_si128(r), _mm_set1_epi32(0x7fffffff)); __m128i bad = _mm_cmpgt_epi32(m, _mm_set1_epi32(0x7f800000)); if (tiny) bad = _mm_or_si128(bad, _mm_cmpeq_epi32(m, _mm_set1_epi32(0x00800000))); if (__builtin_expect(_mm_movemask_ps(_mm_castsi128_ps(bad)) & (k == 4 ? 15 : 3), 0)) a64_ff_spoil_(); } /* (2 lanes: the upper two are loaded as zeros, so they raise no flag of their own) */ static inline __attribute__((always_inline)) __m128 a64_ff_vld(const uint64_t *s, int k) { return k == 4 ? _mm_loadu_ps((const float *)s) : _mm_castsi128_ps(_mm_loadl_epi64((const __m128i *)s)); } static inline __attribute__((always_inline)) void a64_ff_vst(uint64_t *d, __m128 r, int k) { if (k == 4) _mm_storeu_ps((float *)d, r); else _mm_storeu_si128((__m128i *)d, _mm_move_epi64(_mm_castps_si128(r))); } #define A64_FF_V(name, ins, tiny) \ static inline __attribute__((always_inline)) void a64_ff_v##name(Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int k) { \ __m128 r; (void)c; A64_FF_OP(ins, r, a64_ff_vld(n, k), a64_ff_vld(m, k)); \ a64_ff_vguard_(r, k, tiny); a64_ff_vst(d, r, k); } A64_FF_V(add32, "addps", 0) A64_FF_V(sub32, "subps", 0) A64_FF_V(mul32, "mulps", 1) static inline __attribute__((always_inline)) __m128 a64_ff_ss(uint64_t a) { return _mm_castsi128_ps(_mm_cvtsi32_si128((int)(uint32_t)a)); } static inline __attribute__((always_inline)) uint64_t a64_ff_bits(__m128 r) { return (uint32_t)_mm_cvtsi128_si32(_mm_castps_si128(r)); } #define A64_FF_S(name, ins, tiny) \ static inline __attribute__((always_inline)) uint64_t a64_ff_##name(Ctx *c, uint64_t a, uint64_t b) { \ __m128 r; (void)c; A64_FF_OP(ins, r, a64_ff_ss(a), a64_ff_ss(b)); a64_ff_guard_(a64_ff_bits(r), tiny); return a64_ff_bits(r); } A64_FF_S(add32_add32, "addss", 0) A64_FF_S(sub32_sub32, "subss", 0) /* (A subnormal over a zero also takes the exact copy: x86 raises no denormal flag beside divide-by-zero and gives an infinity, where Arm under FZ flushes the subnormal first, 0 / 0: the default NaN and IOC.) */ static inline __attribute__((always_inline)) uint64_t a64_ff_div32_div32(Ctx *c, uint64_t a, uint64_t b) { __m128 r; (void)c; if (__builtin_expect(((uint32_t)b << 1) == 0 && ((uint32_t)a << 1) - 1u < 0x00ffffffu, 0)) a64_ff_spoil_(); A64_FF_OP("divss", r, a64_ff_ss(a), a64_ff_ss(b)); a64_ff_guard_(a64_ff_bits(r), 1); return a64_ff_bits(r); } A64_FF_S(mul32_, "mulss", 1) static inline __attribute__((always_inline)) uint64_t a64_ff_mul32_fma32(Ctx *c, uint64_t a, uint64_t b, uint64_t z) { (void)z; return a64_ff_mul32_(c, a, b); } static inline __attribute__((always_inline)) uint64_t a64_ff_sqrt32_sqrt32(Ctx *c, uint64_t a) { __m128 r; (void)c; A64_FF_SQRT(r, a64_ff_ss(a)); return a64_ff_bits(r); } /* (Triforce) FMADD/FMSUB/FNMADD/FNMSUB and FMLA/FMLS S lanes: a64_fpx_fma32_fma32(c, a, b, z) is a*b + z, the lifted code having already flipped the signs the op negates. FMA3's vfmadd231ss rounds once, as Arm does, and takes its NaN in Arm's order (the addend, then a, then b). Two cases differ, and both raise the invalid flag here (a64_ff_guard_), which sends the run to its exact copy: - 0 * inf + a quiet NaN: x86 returns that NaN with no flag, Arm the default NaN with IOC (so: any NaN result); - a result rounded up to the smallest normal, 2^-126: x86 detects tininess after rounding and raises no underflow, Arm detects it before, sets UFC, and under FZ returns a zero. Without FMA3 (x86-64-v2), the exact helper. */ #ifdef __FMA__ static inline __attribute__((always_inline)) uint64_t a64_ff_fma32_fma32(Ctx *c, uint64_t a, uint64_t b, uint64_t z) { __m128 r = a64_ff_ss(z); (void)c; __asm__ volatile("vfmadd231ss %2, %1, %0" : "+x"(r) : "x"(a64_ff_ss(a)), "x"(a64_ff_ss(b))); a64_ff_guard_(a64_ff_bits(r), 1); return a64_ff_bits(r); } #else #define a64_ff_fma32_fma32 a64_fpx_fma32_fma32 #endif /* (A64_FASTFP=check) a run's result: counts per file, and the first mismatches of a clean fast run (which must not happen) with the run's guest address, on stderr (the game's log) */ static volatile long long a64_ff_n_[3]; /* runs, clean, mismatched when clean */ static __attribute__((noinline, cold)) void a64_ff_check_print_(unsigned pc, long long bad) { if (bad < 16) fprintf(stderr, "fastfp check: MISMATCH in the run at %#x (%s)\n", pc, __BASE_FILE__); } static inline __attribute__((always_inline)) void a64_ff_check(unsigned pc, int clean, int same) { const long long n = __atomic_add_fetch(&a64_ff_n_[0], 1, __ATOMIC_RELAXED); if (clean) __atomic_add_fetch(&a64_ff_n_[1], 1, __ATOMIC_RELAXED); if (clean && !same) a64_ff_check_print_(pc, __atomic_fetch_add(&a64_ff_n_[2], 1, __ATOMIC_RELAXED)); if (!(n & ((1LL << 22) - 1))) fprintf(stderr, "fastfp check: %s: %lld runs, %lld clean, %lld mismatched when clean\n", __BASE_FILE__, n, a64_ff_n_[1], a64_ff_n_[2]); } #else /* (no x86 MXCSR here: the fast copies are never run, and name the exact helpers) */ #define A64_FF_ON(c) 0 #define a64_ff_begin(c) 0u #define a64_ff_end(c, m) 1 #define a64_ff_vadd32 a64_vfpx_add32 #define a64_ff_vsub32 a64_vfpx_sub32 #define a64_ff_vmul32 a64_vfpx_mul32 #define a64_ff_add32_add32 a64_fpx_add32_add32 #define a64_ff_sub32_sub32 a64_fpx_sub32_sub32 #define a64_ff_mul32_fma32 a64_fpx_mul32_fma32 #define a64_ff_fma32_fma32 a64_fpx_fma32_fma32 #define a64_ff_div32_div32 a64_fpx_div32_div32 #define a64_ff_sqrt32_sqrt32 a64_fpx_sqrt32_sqrt32 #define a64_ff_check(pc, clean, same) ((void)0) #endif """ _ff_only = [] def ff_only(): """A64_FASTFP=hot: the names of the hot functions (A64_HOT_FUNCS), whose runs alone are made regions; else None. Read here rather than kept on the lifter, so the lift's new processes, which get only its tables, decide as the lift does""" if not _ff_only: _ff_only.append(a64_lift.hot_functions() if fastfp_mode() == "hot" else None) return _ff_only[0] # ---- the kit's Lifter.emit_function, with the run gathering (a copy: see the module's note) ---- def emit_function(self, e, texts): """C text of one function, and the set of C names it calls.""" body, labels = self.funcs[e] self.cur = e self.refs = set() out = ["void f_%08x(Ctx *__restrict A64_NOESC c) {" % e] if e in self.names and self.comments: out[0] = "/* %s */\n" % self.names[e] + out[0] if e in self.plt: # a PLT stub reached indirectly: x16 = GOT slot, x17 = target, as the stub leaves them t, slot = self.plt[e] name = self.fname(self.callee(t)) self.refs.add(name) out.append("c->x[16] = 0x%xULL; c->x[17] = 0x%xULL; TAILCALL(%s);\n}" % (slot, t, name)) return "\n".join(out) + "\n", self.refs pcs = sorted(body) dead = {} if os.environ.get("A64_KEEP_FLAGS") else self.dead_flags(body, pcs, texts) frame = not os.environ.get("A64_MARK_FRAME") local = local_regs() and e not in self.no_local and local_here(self.fname(e)) dirty = self.dirty_regs(body, pcs, texts) if local else None # (the run of joinable instructions so far, [(pc, text)]: ff_region) # (fastfp) # ((Triforce) and none in a function the game's patches name: ff_patched) # (fastfp) ff = bool(fastfp_mode()) and (ff_only() is None or self.fname(e) in ff_only()) and not ff_patched(e, body, pcs) # (fastfp) least = ff_min() # (fastfp) # (guest stores may join a run (A64_FASTFP_STORES)) # (fastfp) stores = ff and ff_stores() # (fastfp) run = [] # (fastfp) def flush(): # (fastfp) if run: # (fastfp) text, k, n = ff_regions(run, least, stores) # (fastfp) out.append(text) # (fastfp) if n: # (fastfp) self.stats["ff_regions"] = self.stats.get("ff_regions", 0) + k # (fastfp) self.stats["ff_ops"] = self.stats.get("ff_ops", 0) + n # (fastfp) del run[:] # (fastfp) for i, pc in enumerate(pcs): self.cur_i = i (k, w) = body[pc] kind = k[0] # (an instruction joins the run when nothing can enter or leave between it and the next: no label, no patch after # (fastfp) # it, an op a64emit handled, whose text ff_eligible takes; what it appends below goes to the run instead) # (fastfp) joins = False # (fastfp) if ff and kind == "op" and pc not in self.after and not (pc in labels or (pc == e and e in labels)): # (fastfp) t, f = texts[(w, pc) if pc_relative(w) else w] # (fastfp) if not f & (a64emit.UNHANDLED | a64emit.TERMINATOR): # (fastfp) joins = ff_joins(t, dead.get(pc, 0), stores) # (fastfp) if not joins: # (fastfp) flush() # (fastfp) mark = len(out) # (fastfp) if pc in labels or (pc == e and e in labels): out.append("L_%08x:;" % pc) if self.comments: out.append("/* %08x: %08x */" % (pc, w)) nxt = pc + 4 falls = True if kind == "op": text, flags = texts[(w, pc) if pc_relative(w) else w] if flags & a64emit.UNHANDLED: self.unhandled[w] += 1 out.append(SYNC_ALL % i + "a64_trap(c, 0x%xULL, 0x%08xu); return;" % (pc, w)) falls = False elif flags & a64emit.TERMINATOR: raise SystemExit("a64emit closed a block at %#x (%08x): classify() must handle it" % (pc, w)) else: if frame and not fp_is_frame(text): frame = False if pc in dead: text = drop_flag_stores(text, dead[pc]) self.stats["dead_flag_stores"] += bin(dead[pc]).count("1") out.append(text.rstrip("\n")) if pc in self.after: out.append(self.after[pc]) self.stats["after_patches"] = self.stats.get("after_patches", 0) + 1 elif kind == "b": out.append(self.transfer(pc, k[1], labels, body)) falls = False elif kind == "bcond": out.append("if %s { %s }" % (COND[k[2]], self.transfer(pc, k[1], labels, body))) elif kind == "cbz": _, t, rt, sf, nz = k v = "0" if rt == 31 else ("c->x[%d]" % rt if sf else "(uint32_t)c->x[%d]" % rt) out.append("if (%s %s 0) { %s }" % (v, "!=" if nz else "==", self.transfer(pc, t, labels, body))) elif kind == "tbz": _, t, rt, bit, nz = k v = "0" if rt == 31 else "((c->x[%d] >> %d) & 1)" % (rt, bit) out.append("if (%s%s) { %s }" % ("" if nz else "!", v, self.transfer(pc, t, labels, body))) elif kind == "bl": t = self.callee(k[1]) if t in self.entries and t in getattr(self, "setjmp_owner", {}): # (hookshot) a call of a function that calls __setjmp: this frame takes a host setjmp. A guest # longjmp's BR to one of the owner's setjmp return addresses comes back here (host/hos_setjmp.c), # with the registers it restored, and runs the owner's code from there, as the owner would. # (recomp_sj_setjmp, not __builtin_setjmp: on Win64 clang's frame pointer sits above the frame # address the builtin saves, and its longjmp restored rbp to that, so the frame's spill slots # were read 128 bytes off: c and _sj came back as garbage) rets = (self.setjmp_owner[t] + [0, 0, 0, 0])[:4] out.append("c->x[30] = 0x%xULL; %s{ void **_sj = recomp_sj_push(c, 0x%xULL, 0x%xULL, 0x%xULL, 0x%xULL); " "if (recomp_sj_setjmp(_sj) == 0) %s(c); else a64_icall(c, recomp_sj_target(_sj)); " "recomp_sj_pop(_sj); }%s" % (nxt, SYNC_CALL % i, rets[0], rets[1], rets[2], rets[3], self.fname(t), SYNC_BACK % i)) self.refs.add(self.fname(t)) self.stats["setjmp_call_sites"] = self.stats.get("setjmp_call_sites", 0) + 1 elif t in self.entries: out.append("c->x[30] = 0x%xULL; %s%s(c);%s" % (nxt, SYNC_CALL % i, self.fname(t), SYNC_BACK % i)) self.refs.add(self.fname(t)) else: self.stats["bad_target"] += 1 out.append(SYNC_ALL % i + "a64_trap(c, 0x%xULL, 0x%08xu); return;" % (pc, w)) elif kind == "blr": usual = self.devirt.get(pc) if usual in self.entries: name = self.fname(self.callee(usual)) self.refs.add(name) self.stats["devirt_sites"] += 1 out.append("{ uint64_t _t = c->x[%d]; c->x[30] = 0x%xULL; %sif (RECOMP_LIKELY(_t == 0x%xULL)) %s(c); " "else A64_ICALL(c, _t); %s}" % (k[1], nxt, SYNC_CALL % i, usual, name, SYNC_BACK % i)) else: out.append("{ uint64_t _t = c->x[%d]; c->x[30] = 0x%xULL; %sA64_ICALL(c, _t); %s}" % (k[1], nxt, SYNC_CALL % i, SYNC_BACK % i)) elif kind == "ret": out.append(SYNC_RET % i + "return;") falls = False elif kind == "br": targets = self.jt.get(pc) if targets: cases = " ".join("case 0x%xULL: %s" % (t, self.transfer(pc, t, labels, body)) for t in sorted(set(targets))) out.append("switch (c->x[%d]) { %s default: %sA64_IJUMP(c, c->x[%d]); return; }" % (k[1], cases, SYNC_ALL % i, k[1])) else: self.ijump[pc] += 1 out.append(SYNC_ALL % i + "A64_IJUMP(c, c->x[%d]); return;" % k[1]) falls = False elif kind == "svc": out.append(SYNC_ALL % i + "a64_svc(c, %du);" % k[1] + SYNC_RELOAD % i) elif kind == "trap": out.append(SYNC_ALL % i + "a64_trap(c, 0x%xULL, 0x%08xu); return;" % (pc, w)) falls = False elif kind == "isb": out.append("recomp_barrier();") elif kind == "ic": out.append("/* ic ivau: no self-modifying code */") if joins: # (fastfp) run.extend((pc, t) for t in out[mark:]) # (fastfp) del out[mark:] # (fastfp) if falls and (i + 1 == len(pcs) or pcs[i + 1] != nxt): # (fastfp) flush() # (fastfp) if falls and (i + 1 == len(pcs) or pcs[i + 1] != nxt): out.append(self.transfer(pc, nxt, labels, body)) flush() # (fastfp) out.append("}") text = "\n".join(out) if frame: text = frame_stores(text) text = ff_frame_stores(text) # (fastfp) return localize_regs(text, local, dirty) + "\n", self.refs _installed = False def set_patched(addresses): """addresses(): the guest addresses the game's patches name (mentioned_addresses below; ff_patched)""" _patched_addrs[0] = addresses del _patched[:] # (Triforce) The files of nx2/ that edit the lifted C by its exact text after the lift (nx2/chunks.py edit_c runs them), or # splice C after an instruction at lift time: every guest code address one of them names is taken as a place an edit may # change. All of them are files of the lift key (nx2/make_game.py lift_key), so a change of one lifts again. # tests/test_fastfp_override.py holds this list to the files of nx2/ that name a lifted function or label. PATCH_SOURCES = ("oot_patches.py", "oot_native_resolution.py", "oot_wide.py", "oot_jitter.py", "oot_occlusion.py") _ADDRESS = re.compile(r"(?:f_|L_|0x)(8[0-9a-fA-F]{7})(?![0-9a-fA-F])") def mentioned_addresses(): """Every guest code address PATCH_SOURCES name: a function (f_8...), a label (L_8...), a hex address (a call's return address 0x8...ULL, a table of sites, an entry given as a number). An address named for another reason costs one function its regions""" here = os.path.dirname(os.path.abspath(__file__)) out = set() for name in PATCH_SOURCES: path = os.path.join(here, name) if os.path.exists(path): with open(path, encoding="utf-8") as f: out |= {int(a, 16) for a in _ADDRESS.findall(f.read())} return out _patched_addrs[0] = mentioned_addresses # (Triforce) The rule above goes by the addresses the patches' files name. The check below goes by what the patches did: a # function that has a region and that nx2/oot_patches.py's apply then changed stops the lift, with its name. An edit that # finds its place by text alone, in a function none of those files names, would otherwise change one copy of a region's # instructions and leave the other. The lift of the supported game is the same on every PC, so a lift that passes here # passes there. _FN_HEAD = re.compile(r"^void (f_[0-9a-f]{8})\(Ctx \*__restrict A64_NOESC c\) \{", re.M) def region_functions(gen): """{function name: a hash of its text} for the lifted functions of gen's code files that hold a region""" import glob import hashlib out = {} for path in sorted(glob.glob(os.path.join(glob.escape(gen), "code_*.c"))): with open(path, encoding="utf-8", newline="") as f: text = f.read() if "A64_FF_ON(c)" not in text: continue heads = [(m.start(), m.group(1)) for m in _FN_HEAD.finditer(text)] for (a, name), (b, _) in zip(heads, heads[1:] + [(len(text), None)]): if text.find("A64_FF_ON(c)", a, b) >= 0: out[name] = hashlib.sha1(text[a:b].encode("utf-8"), usedforsecurity=False).digest() return out def guard_patches(patches): """patches (the module nx2/oot_patches.py): its apply wrapped, for a lift with regions (nothing otherwise). Once""" if not fastfp_mode() or getattr(patches.apply, "_fastfp_guard", False): return base_apply = patches.apply def apply(gen, *args, **kw): before = region_functions(gen) result = base_apply(gen, *args, **kw) after = region_functions(gen) changed = sorted(n for n, h in before.items() if after.get(n) != h) if changed: raise SystemExit("fastfp: the game's patches changed %d function(s) that hold a fast float region (%s): an edit " "there reaches one copy of the region's instructions only. Name their addresses in a file of " "nx2/fastfp.py's PATCH_SOURCES (or lift with A64_FASTFP=0)" % (len(changed), ", ".join(changed[:8]))) print("fast float regions: %d functions hold one; the game's patches changed none of them" % len(before), flush=True) return result apply._fastfp_guard = True patches.apply = apply def install(): """emit_function above in place of the kit's, and Lifter.write wrapped (the header, the count). Once; before nx2/lift_fixes.py's and nx2/poll.py's install(). (Triforce) Nothing with A64_FASTFP unset or 0: the kit's own method stays, and the lift's C and a64_fpx.h are the kit's to the byte""" global _installed if _installed or not fastfp_mode(): return _installed = True a64_lift.Lifter.emit_function = emit_function base_write = a64_lift.Lifter.write def write(self, out_dir, chunk_bytes=a64_lift.CHUNK_BYTES): files = base_write(self, out_dir, chunk_bytes=chunk_bytes) mode = fastfp_mode() if mode: with open(os.path.join(out_dir, "a64_fpx.h"), "a", newline="\n") as f: # (after the kit's fpx and FCMP parts) f.write(FASTFP_HEADER) print("fast float regions (A64_FASTFP=%s): %d, %d float ops in them" % ( mode, self.stats.get("ff_regions", 0), self.stats.get("ff_ops", 0))) return files a64_lift.Lifter.write = write