fix(relinker): lower VRSQRTPS and VRCPPS to correctly rounded results for --to-intel #464

Merged
oneandonlydean merged 1 commit from upstream-pr/464 into main 2026-10-04 01:45:23 +00:00
oneandonlydean commented 2026-10-03 14:01:51 +00:00 (Migrated from github.com)

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-intel now 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 in TechnicalDebt.md.

  • DecodeVexReciprocal takes VEX2 and VEX3 encodings in map 0F with no vvvv, 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).
  • A 4-byte site takes the following instructions into the stub like the other short sites. When there is no room for the jump (the site is followed by a branch target, a RIP-relative instruction, a control-flow instruction or the end of the segment), the native instruction is kept and reported as kept instead of failing the relink (5 VRSQRTPS sites in PPSA21564, so 14,746 of its register sites are lowered and 10 sites in all stay native). That is new for this lowering only (Amd64OnlyMatch::Optional); every other substitution still fails the relink as before.
  • The relinker prints one summary line per lowered instruction name for these (PPSA21564 has 14,746 sites, in 14,745 stubs: one 4-byte VRCPPS shares a stub with the VRCPPS after it) and a kept count in the conversion total.

Tested

  • Linux (CachyOS, x86-64, Intel i9-14900KF): full build with BUILD_TESTING=ON and ctest pass (126/126).
  • New cases in 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 a ret is 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.
  • PPSA21564: the hub crash no longer reproduces (65 s and more past the level start, where it crashed within about 6 s before), and the change has been on my integration branch through repeated passes of my visual regression check (checkpoint frames, flicker, shimmer, video tearing, FPS).

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_converter cases pass, plus an extra case where a vpbroadcastd 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

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #432 (RIP-relative operands of VEX instructions); please merge it first. This branch merges cleanly with #430, #431, #432, #433 and #434, alone and together.
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4727115012 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/464). Originally posted by [oneandonlydean](https://github.com/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-intel` now 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 in `TechnicalDebt.md`. - `DecodeVexReciprocal` takes VEX2 and VEX3 encodings in map 0F with no `vvvv`, 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). - A 4-byte site takes the following instructions into the stub like the other short sites. When there is no room for the jump (the site is followed by a branch target, a RIP-relative instruction, a control-flow instruction or the end of the segment), the native instruction is kept and reported as kept instead of failing the relink (5 VRSQRTPS sites in PPSA21564, so 14,746 of its register sites are lowered and 10 sites in all stay native). That is new for this lowering only (`Amd64OnlyMatch::Optional`); every other substitution still fails the relink as before. - The relinker prints one summary line per lowered instruction name for these (PPSA21564 has 14,746 sites, in 14,745 stubs: one 4-byte VRCPPS shares a stub with the VRCPPS after it) and a kept count in the conversion total. ### Tested - Linux (CachyOS, x86-64, Intel i9-14900KF): full build with `BUILD_TESTING=ON` and `ctest` pass (126/126). - New cases in `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 a `ret` is 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. - PPSA21564: the hub crash no longer reproduces (65 s and more past the level start, where it crashed within about 6 s before), and the change has been on my integration branch through repeated passes of my visual regression check (checkpoint frames, flicker, shimmer, video tearing, FPS). 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_converter` cases pass, plus an extra case where a `vpbroadcastd 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 <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #432 (RIP-relative operands of VEX instructions); please merge it first. This branch merges cleanly with #430, #431, #432, #433 and #434, alone and together. - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!464
No description provided.