diff --git a/recomp/lifter.py b/recomp/lifter.py index 5dd506cd..b01873e1 100644 --- a/recomp/lifter.py +++ b/recomp/lifter.py @@ -352,7 +352,8 @@ class Lifter: dst = { (3, 3, 13, 0, 2): 'c->tpidr = {v};', (3, 3, 4, 2, 0): 'c->nzcv = {v} & 0xf0000000ull;', - (3, 3, 4, 4, 0): 'c->fpcr = {v} & 0x7c00000ull; rt_fpcr_sync(c);' if self.sc else 'rt_set_fpcr({v});', + (3, 3, 4, 4, 0): ('c->fpcr = {v} & 0x7c00000ull; rt_fpcr_sync(c); RT_FPCR_NOTE(c, c->x[30]);' if self.sc + else 'rt_set_fpcr({v});'), (3, 3, 4, 4, 1): 'c->fpsr = {v} & 0xf800009full;' if self.sc else 'rt_set_fpsr({v});', }.get(key) if dst is None: diff --git a/runtime/cpu.h b/runtime/cpu.h index 4d4291b0..7bacacad 100644 --- a/runtime/cpu.h +++ b/runtime/cpu.h @@ -62,6 +62,12 @@ static inline void rt_fpcr_sync(const Cpu *c) { if (cur != want) __builtin_ia32_ldmxcsr(want); } #endif +// MSR FPCR (recomp/lifter.py): the first guest write of a directed rounding mode (FPCR.RMode, bits 22-23) is logged +// once with the caller (main.c rt_fpcr_rmode), to learn whether the game ever leaves round-to-nearest. A test of the +// value just stored, at the MSR sites only. +void rt_fpcr_rmode(uint64_t fpcr, uint64_t lr); +#define RT_FPCR_NOTE(c, lr) \ + do { if (__builtin_expect((c)->fpcr & (3ull << 22), 0)) rt_fpcr_rmode((c)->fpcr, lr); } while (0) // ---- memory: guest addresses are host addresses typedef uint16_t __attribute__((aligned(1), may_alias)) u16u; diff --git a/runtime/main.c b/runtime/main.c index 780b6960..2d0e9827 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -161,6 +161,16 @@ const RtConfig *rt_config(void) { return &cfg; } volatile int g_fnprof = 0; // resolution.c switches it on until the game sets its render size (res_fn_hit) void fnprof_hit(uint64_t addr, uint64_t lr, uint64_t x0) { (void)lr, res_fn_hit(addr, x0); } +// cpu.h RT_FPCR_NOTE: diagnostic only, the write itself takes effect as before (runtime/simd.h slow paths). +void rt_fpcr_rmode(uint64_t fpcr, uint64_t lr) { + static int seen; + if (__atomic_exchange_n(&seen, 1, __ATOMIC_RELAXED)) return; + static const char *const mode[] = {"nearest", "+inf", "-inf", "zero"}; + char at[128]; + rt_log("fpcr: guest code set rounding mode %s (FPCR %#llx), in a function called from %s", mode[(fpcr >> 22) & 3], + (unsigned long long)fpcr, ldr_describe(lr, at, sizeof at)); +} + void rt_brk(Cpu *c, uint32_t imm, uint64_t pc) { char at[128], lr[128]; rt_fatal("brk #%#x at %s (lr %s)", imm, ldr_describe(pc, at, sizeof at), ldr_describe(c->x[30], lr, sizeof lr)); diff --git a/tests/diff/harness.c b/tests/diff/harness.c index 00be191b..a5c24a4a 100644 --- a/tests/diff/harness.c +++ b/tests/diff/harness.c @@ -69,6 +69,7 @@ void rt_udf(Cpu *c, uint64_t pc) { (void)c; (void)pc; abort(); } uint64_t rt_ticks(void) { return 0; } volatile int g_fnprof; void fnprof_hit(uint64_t addr, uint64_t lr, uint64_t x0) { (void)addr; (void)lr; (void)x0; } +void rt_fpcr_rmode(uint64_t fpcr, uint64_t lr) { (void)fpcr; (void)lr; } static void *map_at(uint64_t addr, size_t len) { len = (len + 0xffff) & ~(size_t)0xffff;