fix(relinker): lower VRSQRTPS and VRCPPS to correctly rounded results for --to-intel #464
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/464"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-03T14:01:51Z.
What
RSQRTPS and RCPPS are approximations, and the low bits of the approximation differ between Intel and AMD. Intel returns 0x3F7FF000 for 1/sqrt(1.0), and two Newton-Raphson steps on that settle on 0.99999994 instead of 1.0. PPSA21564 renormalises its spring quaternions that way: an identity quaternion keeps w at 0.99999994 with a zero axis, its axis-angle conversion then normalises the zero axis into NaNs, and the NaN bone poses reach keyframed physics bodies. A NaN body later moves to an invalid broadphase cell and the game crashes a few seconds into its hub level on Intel hosts (a SIGSEGV, or its own heap assert). The console's AMD approximation does not show this.
--to-intelnow replaces the VEX.128 register forms of VRSQRTPS and VRCPPS, and only those, with an out-of-line stub that computes 1/sqrt(x) with SQRTPS and DIVPS, and 1/x with DIVPS. The stub keeps the upper half of the destination cleared, as VEX.128 does, and spills and restores its scratch register. The results are the exact IEEE single-precision results (correctly rounded), not bit-identical to Zen 2's approximation, whose tables are not public: refined results now reach the exact value, while a title that uses the raw approximation can still see different low bits than on the console. This is listed inTechnicalDebt.md.DecodeVexReciprocaltakes VEX2 and VEX3 encodings in map 0F with novvvv, W0 and no SIMD prefix, register operands only. The scalar (VRSQRTSS/VRCPSS), memory-operand, 256-bit and legacy SSE forms are not lowered and still run natively, with Intel's approximation. Of these, PPSA21564's code has only the memory form, at 5 sites (3 VRSQRTPS, 2 VRCPPS); all its other sites are VEX.128 register forms (14,751: 10,995 VRSQRTPS, 3,756 VRCPPS).Amd64OnlyMatch::Optional); every other substitution still fails the relink as before.Tested
BUILD_TESTING=ONandctestpass (126/126).amd64_only_converter:reciprocalOperands: VEX2 and VEX3 forms with low and high registers decode; 256-bit, scalar, memory, legacy and VSQRTPS encodings do not.converterReciprocal: a VRSQRTPS absorbs the following VMULPS into its stub and runs it before the return jump; a VRCPPS before aretis kept; a branch to the site itself still allows a stub, a branch into the absorbed instruction keeps the site native; a following RIP-relative load keeps it native.reciprocalExecution(x86-64 Linux): the stubs, with distinct and equal operands, return the correctly rounded 1/sqrt(x) and 1/x for small, large, zero, negative, infinite and near-1 inputs, and VRSQRTPS of 1.0 is exactly 1.0.These use the new decoder and lowering, so they do not build without this change.
The stub copies the instructions it absorbs, so it relies on the decoder reporting RIP-relative operands of VEX instructions, which main's decoder does not do. #432 fixes that in the decoder; until it lands, a VEX instruction with a RIP-relative operand right after a 4-byte VRSQRTPS would be copied into the stub with its displacement unchanged. My integration branch first carried its own check for this, and an early build crashed on exactly that case before the check was added. On a local merge of this branch with #430 and #432, all
amd64_only_convertercases pass, plus an extra case where avpbroadcastd xmm2, [rip+0x10]follows a 4-byte VRSQRTPS and the site is kept native; on this branch alone that extra case fails. I can add that case here once #432 is in.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)