Console CPU instructions: SSE4a INSERTQ / EXTRQ are emulated #209
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!209
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/209/head"
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?
@ShyVortex on GitHub · 2026-11-23 21:03 UTC · original
ShyVortex:cpu/sse4a-insertq→main· +82 −4 · 2 files · 1 commits · closed without mergingSummary
Zen 2 has SSE4a, which no Intel CPU has (the possible further
#UDsource noted in the review of #151). A retail game usesINSERTQ: 19 of them, all the immediate form, in its float-to-half packing. On an Intel CPU, with #151 in place, it ran for about two minutes and presented 900+ frames. Then, when its first menu reacted to input, it stopped withSTATUS_ILLEGAL_INSTRUCTIONatinsertq xmm0, xmm1, 16, 16(F2 0F 78 C1 10 10).This adds SSE4a's immediate forms to #151's decoder and emulation:
insn::decode): now tracks theF2prefix. It decodesF2 0F 78 /r ib ib(insertq xmm, xmm, len, idx) and66 0F 78 /0 ib ib(extrq xmm, len, idx), with REX.R / REX.B, register operands only (the encoding has no memory form).0F 78without a prefix (vmread) is left alone.eh::emulate_console_only): works on the low quadword of the XMM registers in the exception's context. It inserts or extracts a field oflenbits atidx, length 0 meaning 64, as AMD's manual (APM vol. 4) defines them. The upper quadword, undefined on the hardware, is left as it was.A scan of the game's code sections found no
EXTRQand no SSE4a register forms (0F 79) orMOVNTSS/MOVNTSD.EXTRQ's immediate form is included since it shares the opcode.Testing
cargo test --release --workspace: all pass. New:insn::tests::decodes_sse4a(the game's encodings, with REX.R and REX.R + REX.B, plus what must not decode) andinsn::tests::sse4a_semantics.main+ this: the game runs past theINSERTQand shows its loading screen with text.🤖 Generated with Claude Code
@mastrr9 on GitHub · 2026-11-23 21:05 UTC · original
Independent static code & AMD64 ISA audit of
08c81c629(code inspection only —cargo testnot run here; no game files):Summary: Looks solid for immediate SSE4a
INSERTQ/EXTRQinsn.rs:32-49):field_mask(len)handleslen & 63 == 0→u64::MAX(64 bits), and maskingidx & 63prevents any Rust shift-by-64 overflow panic even on undefinedlen + idx > 64encodings.insertq/extrqmatch AMD64 APM Vol. 4 on valid fields, and leaving the upper quadword ofXmmRegisters[dst & 15]untouched matches the hardware-undefined upper half.insn.rs:108-119,eh.rs:304-311):REX.R((rex & 4) << 1) andREX.B((rex & 1) << 3) decodexmm0..xmm15accurately,m >> 6 == 3properly rejects memory operands,(m >> 3) & 7 == 0enforces/0forEXTRQ, andctx.Rip += len as u64advances by the full decoded length (6or7bytes; the match-armlenbinding doesn't shadow it).Minor non-blocking notes
insn.rs:108):(0x78, Some(m)) if m >> 6 == 3 && (pf2 || p66)acceptsF3 66 0F 78,F3 F2 0F 78, and66 F2 0F 78, which are#UDon x86-64 rather than validINSERTQ/EXTRQ. Tightening the guard to!pf3 && (pf2 ^ p66)keeps invalid prefix combinations rejected.F2 0F 79 /r,66 0F 79 /r) are not decoded here (expected since only immediate0F 78forms were seen, just noting in case a title uses0F 79later).(AI-assisted)
@Kalmarv on GitHub · 2026-11-23 21:15 UTC · original
Review of
08c81c6, merged ontomain6da33fa(merge51ac82a, no conflicts). Tested on a Ryzen 9 9950X (Zen 5, Windows 11 26200), which runs SSE4a natively. That allows a direct comparison between the hardware and the emulation. Synthetic data only.Result: on every valid encoding, the emulated low quadword equals the CPU's, including
insertq xmm0, xmm1, 16, 16(F2 0F 78 C1 10 10, the instruction in #216). Three differences, none in the instruction the game uses:The CPU zeroes the upper quadword of the destination; the emulation leaves it unchanged. This held for all 32,779 instructions run, INSERTQ and EXTRQ alike. AMD's manual calls it undefined. Setting
.High = 0would match the hardware measured here. I can't run a Zen 2, so I can't say it matches the console, but nothing points the other way.The F3 prefix combinations are decoded wrong. On the CPU, the last of F2/F3 decides, and 66 is ignored next to them:
66 F2,F2 66andF3 F2run as INSERTQ, and the PR also emulates them correctly. So the earlier suggestion here to reject them would turn valid instructions into crashes.F2 F3,F3 66and66 F3raise #UD on the CPU, but the PR emulates the first as INSERTQ and the other two as EXTRQ.A fix: track which of F2/F3 came last. Return
Nonewhen it is F3; decode INSERTQ when it is F2, and EXTRQ when there is neither and 66 is present. Compilers don't emit these combinations, so this is for correctness only.Encodings the CPU runs but the PR leaves undecoded, so they still crash:
67 F2 …,F2 67 …,67 66 …). This is the #151 gap; adding 0x67 to the ignored prefixes covers it.41 F2 0F 78 …: the CPU ignores that REX).F2 0F 79and66 0F 79(already noted above; the PR says the game has none).Method. A test-only probe, not part of the PR.
cpp_exception_logger, with a STATUS_ILLEGAL_INSTRUCTION whose CONTEXT holds the same XMM values.len×idx(0–63 × 0–63) with random data. INSERTQ used three register pairs: xmm0/xmm1, xmm9/xmm7 (REX.R) and xmm11/xmm12 (REX.R+B), with and without REX.W. EXTRQ used xmm2 and xmm10.FltSave.XmmRegisters.F2 0F 78 C1 10 10(#216)When len+idx > 64 (undefined per AMD), the CPU drops the bits past 63, exactly like the PR's
mask << idx. Length 0 means 64 on the CPU too.Edge encodings: the CPU's behaviour, and what the PR's handler does (✓ means they agree):
66 F2,F2 66,F3 F20F 78 C1 10 10F2 F3 0F 78 C1 …F3 66 0F 78 C1 …,66 F3 0F 78 C0 …F3 0F 78;66 0F 78/1, /2, /7; memory operand2E F2 …,F2 48 …,66 48 …(REX.W)67 F2 …,F2 67 …,67 66 …41 F2 0F 78 C1 …(REX before F2)F2 0F 79 C1,66 0F 79 C1Control: the same probe on
main6da33fa. The handler emulates none of these (handled = 0 in every row), the probe's assertion fails, and the low quadword matches only where the instruction happens not to change it. The edge rows give the same CPU results.The #151 gaps besides 0x67 are untouched by this PR, as expected: CLZERO still checks that the line is readable (not writable), and MWAITX does not pause.
Tested
08c81c6merged onto6da33fa(merge51ac82a):insn::3/3 pass (includingdecodes_sse4a,sse4a_semantics);eh::2/2 pass (1 ignored, the probe).6da33fa:insn::1/1 andeh::2/2 pass; the probe fails as expected.Not tested
AI use: Claude Code, Claude Opus 5.5
@ivall on GitHub · 2026-11-23 21:17 UTC · original
Thanks - this crossed with the same fix on main for #216 (
0cd0a53, written before I saw this PR), so the branch now conflicts; closing it as superseded rather than merging a second copy. Your Intel run (i7-12700H: past the INSERTQ, loading screen with text) is the confirmation main was missing, and @Kalmarv's hardware comparison is now on main too (af20b4b): the destination's high quadword is zeroed as the Zen 5 does in all 32 779 cases, the last of F2 / F3 decides (F2 F3, F3 66, 66 F3 stay #UD; 66 F2, F2 66, F3 F2 are INSERTQ), 0x67 and a REX before a legacy prefix are ignored (for CLZERO and the rest too), and the register forms 0F 79 are emulated. Checked on an RX 9070 host:cargo test --workspacegreen (583 unit tests); the emulation path itself cannot fire on an AMD CPU, so a run of current main on your Intel machine would be welcome.@tactileteamrob on GitHub · 2026-11-23 21:24 UTC · original
I tested the experimental SSE4a build again.
The new log confirms that INSERTQ at game_scarlett_master.exe+0x2e7ff1 is now being emulated successfully.
The previous illegal-instruction crash no longer appears in the log. The game continues running and records over 1,000 translated draws, but I still get extremely stuttery narration audio, followed by a green screen and severely distorted audio.
I've attached the new log and report.json.
System: Intel i9-12900K, RTX 5070, 32GB RAM, Windows 11 25H2.
report.json
xcl.log
@ShyVortex on GitHub · 2026-11-23 21:26 UTC · original
Ran current
main(af20b4b) on the Intel machine as asked: i7-12700H + RTX 3080 Ti Laptop GPU (16 GB), Windows 11 26300,XCL_NETWORK=0.cargo test --release -p xcl --lib: 583 passed, 0 failed.Clzeroat+0x4e8aa70(6.9 s),Monitorxat+0x4fb0bd7(17 s),Mwaitxat+0x4fb0c00(24 s), andInsertq { dst: 0, src: 1, len: 16, idx: 16 }at+0x2e7ff1(52.5 s), the one from #216. No illegal-instruction stop. Stepping through the menus with Enter, it reached the loading screen (600 frames by 114 s, ~6 fps).Pull request closed