VFPU: vsqrt, vrsq, vrcp and vnrcp are exact in every backend

These went through the host's sqrt and division everywhere except the
interpreter's vrcp and vnrcp (vsqrt and vrsq there only behind
USE_VFPU_SQRT, now gone). They now always give the PSP's bits: the IR
gets FVSqrt (FSqrt stays the FPU's IEEE sqrt.s), and FRSqrt and FRecip,
which only the VFPU emits, become vfpu_rsqrt and vfpu_rcp; the IR
interpreter and the x64, arm64, RISC-V and LoongArch backends call them.
The old JITs call them directly, the ARM ones keeping the lanes in
callee-saved registers across the calls. cpu/vfpu/exact now passes on every core.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-09-24 12:24:42 -06:00
1 parent 87ad32d578
commit a2b10778be
17 files changed
+225 -132

No files matched your search

+76 -23
View File
@@ -19,6 +19,7 @@
#if PPSSPP_ARCH(ARM)
#include <cmath>
#include <cstring>
#include "Common/CPUDetect.h"
#include "Common/Data/Convert/SmallDataConvert.h"
#include "Common/Math/math_util.h"
@@ -911,6 +912,12 @@ namespace MIPSComp
case 21: // d[i] = logf(s[i])/log(2.0f); break; //vlog2
DISABLE;
break;
case 16: // vrcp
case 17: // vrsq
case 22: // vsqrt
case 24: // vnrcp
CompVV2OpCall(op);
return;
case 26: // d[i] = -sinf((float)M_PI_2 * s[i]); break; // vnsin
DISABLE;
break;
@@ -1007,23 +1014,6 @@ namespace MIPSComp
VMOV(fpr.V(tempregs[i]), S1);
SetCC(CC_AL);
break;
case 16: // d[i] = 1.0f / s[i]; break; //vrcp
if (i == 0) {
MOVI2F(S0, 1.0f, SCRATCHREG1);
}
VDIV(fpr.V(tempregs[i]), S0, fpr.V(sregs[i]));
break;
case 17: // d[i] = 1.0f / sqrtf(s[i]); break; //vrsq
if (i == 0) {
MOVI2F(S0, 1.0f, SCRATCHREG1);
}
VSQRT(S1, fpr.V(sregs[i]));
VDIV(fpr.V(tempregs[i]), S0, S1);
break;
case 22: // d[i] = sqrtf(s[i]); break; //vsqrt
VSQRT(fpr.V(tempregs[i]), fpr.V(sregs[i]));
VABS(fpr.V(tempregs[i]), fpr.V(tempregs[i]));
break;
case 23: // d[i] = asinf(s[i] * (float)M_2_PI); break; //vasin
// Seems to work well enough but can disable if it becomes a problem.
// Should be easy enough to translate to NEON. There we can load all the constants
@@ -1052,12 +1042,6 @@ namespace MIPSComp
MOVI2F(S1, 1.0f / (M_PI / 2), SCRATCHREG1);
VMUL(fpr.V(tempregs[i]), fpr.V(tempregs[i]), S1);
break;
case 24: // d[i] = -1.0f / s[i]; break; // vnrcp
if (i == 0) {
MOVI2F(S0, -1.0f, SCRATCHREG1);
}
VDIV(fpr.V(tempregs[i]), S0, fpr.V(sregs[i]));
break;
default:
ERROR_LOG(Log::JIT, "case missing in vfpu vv2op");
DISABLE;
@@ -2120,6 +2104,75 @@ namespace MIPSComp
fpr.ReleaseSpillLocksAndDiscardTemps();
}
// Float bits in and out, so the calls look the same with softfp and hardfp.
static u32 CallWithBits(float (*func)(float), u32 x) {
float f;
memcpy(&f, &x, sizeof(f));
f = func(f);
memcpy(&x, &f, sizeof(x));
return x;
}
static u32 VRcpBits(u32 x) {
return CallWithBits(&vfpu_rcp, x);
}
static u32 VNRcpBits(u32 x) {
return CallWithBits(&vfpu_rcp, x) ^ 0x80000000;
}
static u32 VRSqrtBits(u32 x) {
return CallWithBits(&vfpu_rsqrt, x);
}
static u32 VSqrtBits(u32 x) {
return CallWithBits(&vfpu_sqrt, x);
}
// vrcp, vrsq, vsqrt and vnrcp call the exact functions. The lanes stay in S16-S19 across the
// calls (callee-saved), and the results are stored to the destinations' homes.
void ArmJit::CompVV2OpCall(MIPSOpcode op) {
// Like the interpreter, these apply the prefixes to the last lane only.
if (js.HasSPrefix() || (js.HasDPrefix() && GetVecSize(op) != V_Single)) {
DISABLE;
}
const int optype = (op >> 16) & 0x1f;
u32 (*func)(u32) = &VRcpBits;
if (optype == 17) {
func = &VRSqrtBits;
} else if (optype == 22) {
func = &VSqrtBits;
} else if (optype == 24) {
func = &VNRcpBits;
}
VectorSize sz = GetVecSize(op);
int n = GetNumVectorElements(sz);
u8 sregs[4], dregs[4];
GetVectorRegs(sregs, sz, _VS);
GetVectorRegs(dregs, sz, _VD);
gpr.FlushBeforeCall();
fpr.FlushAll();
for (int i = 0; i < n; i++) {
VLDR((ARMReg)(S16 + i), CTXREG, fpr.GetMipsRegOffsetV(sregs[i]));
}
for (int i = 0; i < n; i++) {
VMOV(R0, (ARMReg)(S16 + i));
// FlushBeforeCall saves R1.
QuickCallFunction(R1, func);
VMOV((ARMReg)(S16 + i), R0);
}
for (int i = 0; i < n; i++) {
VSTR((ARMReg)(S16 + i), CTXREG, fpr.GetMipsRegOffsetV(dregs[i]));
}
ApplyPrefixD(dregs, sz);
fpr.ReleaseSpillLocksAndDiscardTemps();
}
static double SinCos(float angle) {
union { struct { float sin; float cos; }; double out; } sincos;
vfpu_sincos(angle, sincos.sin, sincos.cos);