mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-25 14:05:57 +00:00
The FP fast paths of the lifted code check FPCR.RMode inside the result compare and pass vector slow-path operands through the Cpu
The result check of every scalar and vector FP op compares |r| against a per-thread threshold in the Cpu (fp_th_s, fp_th_d): the smallest normal in round-to-nearest, NaN under a directed RMode, so a directed mode sends every result to the redo path and the separate load and test of c->fpcr per op goes. rt_fpcr_threshold sets it; rt_fpcr_sync calls it on every host, and the Cpu inits and the fiber switch's fpcr copy call it too. The vector slow path FN(vredo) takes its operands from c->fp_arg instead of three stack copies per call site, which takes ___chkstk_ms out of the large lifted functions (275 call sites in main to 5). It reruns only the lanes that failed the check. A NaN result in round-to-nearest goes from redo straight to fix with the FZ-flushed operands. The static FP helpers use preserve_most, so their cold call sites do not spill the caller-saved registers. Results are bit-identical to the old headers (equivalence harness of build/v143-genko/static2, 200M cases). On the 42 hottest 1.4.3 functions: instructions -8.2%, bytes -8.8%, hot-path instructions -4.3%. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
94eb3d4628
commit
5c48f62fca
8 files changed
+89
-12
No files matched your search
+23
-3
@@ -18,7 +18,11 @@ typedef struct __attribute__((aligned(16))) Cpu {
|
||||
uint64_t fpcr; // 320: guest FPCR/FPSR (portable FP/SIMD lowering only; the asm path uses the host's)
|
||||
uint64_t fpsr; // 328
|
||||
uint64_t excl_val_hi; // 336: upper half of a 128-bit exclusive pair (LDXP/LDAXP of two X registers)
|
||||
uint8_t _pad[512 - 344];
|
||||
uint64_t _pad0; // 344
|
||||
float fp_th_s[4]; // 352: FP result check threshold, every lane (rt_fpcr_threshold, runtime/simd_fp.h FOKT)
|
||||
double fp_th_d[2]; // 368
|
||||
__uint128_t fp_arg[3]; // 384: operands of a vector FP op handed to its slow path (runtime/simd_fp.h FN(vredo))
|
||||
uint8_t _pad[512 - 432];
|
||||
__uint128_t v[32]; // 512
|
||||
} Cpu;
|
||||
|
||||
@@ -27,6 +31,8 @@ _Static_assert(offsetof(Cpu, pc) == 264, "pc");
|
||||
_Static_assert(offsetof(Cpu, nzcv) == 272, "nzcv");
|
||||
_Static_assert(offsetof(Cpu, fpcr) == 320, "fpcr");
|
||||
_Static_assert(offsetof(Cpu, fpsr) == 328, "fpsr");
|
||||
_Static_assert(offsetof(Cpu, fp_th_s) == 352 && offsetof(Cpu, fp_th_d) == 368, "fp_th");
|
||||
_Static_assert(offsetof(Cpu, fp_arg) == 384, "fp_arg");
|
||||
_Static_assert(offsetof(Cpu, v) == 512, "v");
|
||||
|
||||
typedef void (*GuestFn)(Cpu *);
|
||||
@@ -39,12 +45,24 @@ void rt_brk(Cpu *c, uint32_t imm, uint64_t pc) __attribute__((noreturn));
|
||||
void rt_udf(Cpu *c, uint64_t pc) __attribute__((noreturn));
|
||||
uint64_t rt_ticks(void);
|
||||
|
||||
// The FP fast paths of runtime/simd_fp.h test FPCR.RMode and the result in one compare: a result is good when its
|
||||
// magnitude differs from c->fp_th_*. In round-to-nearest the threshold is the smallest normal, so NaN and +-smallest
|
||||
// normal fail as before; under a directed RMode it is NaN, both compares are unordered and every result goes through
|
||||
// the redo path, which rounds in the FPCR mode. It must follow every change of c->fpcr: rt_fpcr_sync sets it, and so
|
||||
// does code that creates a Cpu or copies fpcr into one. A zeroed threshold is wrong, not merely slow: it passes
|
||||
// +-smallest normal.
|
||||
static inline void rt_fpcr_threshold(Cpu *c) {
|
||||
int directed = (c->fpcr & (3u << 22)) != 0;
|
||||
for (int i = 0; i < 4; i++) c->fp_th_s[i] = directed ? __builtin_nanf("") : 0x1p-126f;
|
||||
for (int i = 0; i < 2; i++) c->fp_th_d[i] = directed ? __builtin_nan("") : 0x1p-1022;
|
||||
}
|
||||
|
||||
#if defined(__aarch64__)
|
||||
static inline uint64_t rt_get_fpcr(void) { uint64_t v; __asm__ volatile("mrs %0, fpcr" : "=r"(v)); return v; }
|
||||
static inline uint64_t rt_get_fpsr(void) { uint64_t v; __asm__ volatile("mrs %0, fpsr" : "=r"(v)); return v; }
|
||||
static inline void rt_set_fpcr(uint64_t v) { __asm__ volatile("msr fpcr, %0" :: "r"(v)); }
|
||||
static inline void rt_set_fpsr(uint64_t v) { __asm__ volatile("msr fpsr, %0" :: "r"(v)); }
|
||||
static inline void rt_fpcr_sync(const Cpu *c) { (void)c; }
|
||||
static inline void rt_fpcr_sync(Cpu *c) { rt_fpcr_threshold(c); }
|
||||
#else
|
||||
// Host FP state is not the guest's here: code lifted with --simd=c keeps FPCR/FPSR in Cpu (c->fpcr, zeroed
|
||||
// with the Cpu), so the host-side reset in thread start-up has nothing to do.
|
||||
@@ -56,7 +74,9 @@ static inline void rt_set_fpsr(uint64_t v) { (void)v; }
|
||||
// the slow paths of runtime/simd.h, which switch RC around one operation). The FP helpers rely on it without checking,
|
||||
// so it is set wherever FPCR or the host FP context changes: MSR FPCR (recomp/lifter.py), rt_call (thread and fiber
|
||||
// entry, host code calling guest code), fiber resume (runtime/kernel.c fiber_resumed; fibers keep their own MXCSR).
|
||||
static inline void rt_fpcr_sync(const Cpu *c) {
|
||||
// It also sets the FP result check threshold (rt_fpcr_threshold).
|
||||
static inline void rt_fpcr_sync(Cpu *c) {
|
||||
rt_fpcr_threshold(c);
|
||||
unsigned cur = __builtin_ia32_stmxcsr();
|
||||
unsigned want = (cur & ~0xE040u) | (c->fpcr & (1u << 24) ? 0x8040u : 0); // FTZ 0x8000, DAZ 0x40, RC 0x6000
|
||||
if (cur != want) __builtin_ia32_ldmxcsr(want);
|
||||
|
||||
Reference in new issue
Block a user