From fc18a2458f7e98c4cf0ee027f2593a11c0ba44e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 13 Oct 2026 22:15:09 +0200 Subject: [PATCH] VFPU: Generate the VFPU's NaN in vbfy, vocp and vsocp on all cores These are sums, so a NaN result is 0x7F800001 like vadd's (cpu/vfpu/specials). Co-Authored-By: Claude Opus 5.5 --- Core/MIPS/IR/IRCompVFPU.cpp | 8 ++++++++ Core/MIPS/InterpreterVFPU.cpp | 19 +++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Core/MIPS/IR/IRCompVFPU.cpp b/Core/MIPS/IR/IRCompVFPU.cpp index 326687ba2f..0cc7a0434e 100644 --- a/Core/MIPS/IR/IRCompVFPU.cpp +++ b/Core/MIPS/IR/IRCompVFPU.cpp @@ -2695,10 +2695,13 @@ namespace MIPSComp { GetVectorRegsPrefixT(tregs, sz, _VS); GetVectorRegsPrefixD(dregs, sz, _VD); + // A NaN comes out as the VFPU's (cpu/vfpu/specials). if (IsVec4(sz, dregs) && IsVec4(sz, sregs) && IsVec4(sz, tregs)) { ir.Write(IROp::Vec4Add, dregs[0], tregs[0], sregs[0]); + ir.Write(IROp::Vec4VfpuNaN, dregs[0], dregs[0]); } else if (IsVec3of4(sz, dregs) && IsVec3of4(sz, sregs) && IsVec3of4(sz, tregs) && opts.preferVec4) { ir.Write(IROp::Vec4Add, IRVTEMP_0, tregs[0], sregs[0]); + ir.Write(IROp::Vec4VfpuNaN, IRVTEMP_0, IRVTEMP_0); ir.Write(IROp::Vec4Blend, dregs[0], dregs[0], IRVTEMP_0, 0x7); } else { u8 tempregs[4]; @@ -2712,6 +2715,7 @@ namespace MIPSComp { for (int i = 0; i < n; ++i) { ir.Write(IROp::FAdd, tempregs[i], tregs[i], sregs[i]); + ir.Write(IROp::FVfpuNaN, tempregs[i], tempregs[i]); } for (int i = 0; i < n; ++i) { if (dregs[i] != tempregs[i]) { @@ -2834,6 +2838,10 @@ namespace MIPSComp { INVALIDOP; } + // A NaN comes out as the VFPU's (cpu/vfpu/specials). + for (int i = 0; i < n; ++i) { + ir.Write(IROp::FVfpuNaN, tempregs[i], tempregs[i]); + } for (int i = 0; i < n; ++i) { if (tempregs[i] != dregs[i]) ir.Write(IROp::FMov, dregs[i], tempregs[i]); diff --git a/Core/MIPS/InterpreterVFPU.cpp b/Core/MIPS/InterpreterVFPU.cpp index 195d14cb7c..61cb873991 100644 --- a/Core/MIPS/InterpreterVFPU.cpp +++ b/Core/MIPS/InterpreterVFPU.cpp @@ -749,8 +749,8 @@ namespace MIPSInt ApplyPrefixST(mips, t, VFPURewritePrefix(mips, VFPU_CTRL_TPREFIX, tprefixRemove, tprefixAdd), sz); for (int i = 0; i < GetNumVectorElements(sz); i++) { - // Always positive NaN. Note that s is always negated from the registers. - d[i] = my_isnan(s[i]) ? fabsf(s[i]) : t[i] + s[i]; + // Note that s is always negated from the registers. + d[i] = VfpuSumNaN(t[i] + s[i]); } RetainInvalidSwizzleST(mips, d, sz); ApplyPrefixD(mips, d, sz); @@ -782,11 +782,11 @@ namespace MIPSInt ApplyPrefixST(mips, t, VFPURewritePrefix(mips, VFPU_CTRL_TPREFIX, tprefixRemove, tprefixAdd), outSize); // Essentially D prefix saturation is forced. - d[0] = nanclamp(t[0] + s[0], 0.0f, 1.0f); - d[1] = nanclamp(t[1] + s[1], 0.0f, 1.0f); + d[0] = nanclamp(VfpuSumNaN(t[0] + s[0]), 0.0f, 1.0f); + d[1] = nanclamp(VfpuSumNaN(t[1] + s[1]), 0.0f, 1.0f); if (outSize == V_Quad) { - d[2] = nanclamp(t[2] + s[2], 0.0f, 1.0f); - d[3] = nanclamp(t[3] + s[3], 0.0f, 1.0f); + d[2] = nanclamp(VfpuSumNaN(t[2] + s[2]), 0.0f, 1.0f); + d[3] = nanclamp(VfpuSumNaN(t[3] + s[3]), 0.0f, 1.0f); } ApplyPrefixD(mips, d, sz, true); WriteVector(mips, d, outSize, vd); @@ -1319,10 +1319,9 @@ namespace MIPSInt } } - d[0] = s[0] + t[0]; - d[1] = s[1] + t[1]; - d[2] = s[2] + t[2]; - d[3] = s[3] + t[3]; + for (int i = 0; i < 4; i++) { + d[i] = VfpuSumNaN(s[i] + t[i]); + } ApplyPrefixD(mips, d, sz); WriteVector(mips, d, sz, vd);