x64_recomp: bt/bts/btr/btc, bsf/bsr, tzcnt/lzcnt, and popcnt emitters (X64-RECOMP-BITOPS) #1205
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1205"
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 ubiquitarianism/leonida. Originally posted by quietfold on 2026-11-24T20:15:27Z.
Claimed in the coordination issue as: X64-RECOMP-BITOPS (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7143260452)
What and why
Implements the remaining decoded-but-unemitted bit-manipulation instructions in
tools/x64_recomp.py(independent of#1174and#1202, and conflict-free with all openx64_recompPRs):Asm.rbit/Asm.clz: adds 32-bit and 64-bit bit-reverse and count-leading-zeros encodings (verified against Apple Clang / LLVM 21).bt/bts/btr/btc(_emit_bt):16/32/64), updateCFvia_set_cfwithout clobberingN/Z/V, and preserve upper bits on 16-bit writes.0F BA /4../7) mask the immediate modulo operand width; memory register forms (0F A3/AB/B3/BB) sign-extend the bit index from operand width (sxth/sxtw/64-bit) to index bytes before or after the base address (base + (idx >>s 3), bitidx & 7).flags_r = "C"dependency onbts/btr/btcin_classify.bsf/bsr: setsZF = (src == 0)and leavesdstunmodified whensrc == 0(matching x86-64 hardware); computes LSB/MSB index viarbit/clzfor 16/32/64-bit operands.tzcnt(F3 0F BC) /lzcnt(F3 0F BD): decodesF3-prefixed0F BC/0F BDseparately frombsf/bsr, returns operand width (16/32/64) whensrc == 0, and setsCF = (src == 0)andZF = (result == 0).popcnt(F3 0F B8): mapsF3-prefixedpopcnt_jmpetopopcnt(leaving unprefixed0F B8unhandled assys), computes 16/32/64-bit Hamming weight with pure-GPR SWAR (no FP/SIMD dependency), and setsZF = (src == 0)(N=C=V=0).Asm.li(rd, -1): when all four 16-bit chunks are0xFFFF,nzcwas empty andchunks[nzc[0]]raisedIndexError; now emitsmovn rd, #0.tests/test_x64_recomp_bitops.py): 9 tests covering decoding, LLVM 21–verified encodings, translation, and 5 end-to-end execution tests (BitopsExecTests).Tested
main:tests/test_x64_recomp_bitops.py(4 cross-platform passed, 5 exec cleanly skipped without QEMU),tests/test_x64_recomp.py,tests/test_recomp_census.py,tests/test_ci_registration.py,tests/test_text_write_line_endings.py,compileall,make_game.py --help,check_no_game_data.py: all passed.#1174and#1202in a scratch checkout):tests/test_x64_recomp_bitops.py9/9 passed,tests/test_x64_recomp_sse.py16/16 passed,tests/test_x64_recomp.py46/46 passed (71/71 total).git apply --check) against openx64_recompPRs#847,#950,#1120,#1121,#1174, and#1202.GPUs tested: n/a (CPU x86-64 -> arm64 recompiler; tested natively on Apple M5 arm64, macOS 27.2.1)
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed.Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T20:15:35Z.
Review: COMMENTED
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-25T23:51:13Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainat33bd07cf8into the branch locally (960 commits behind, no conflicts). Head tested:a49107929.Against a reference model the emitters match in 1,501 of 1,502 valid checks. The one that differs is
popcntleaving a stale carry.python -I tests/test_x64_recomp_bitops.py: 9/9 under QEMU. This file callsx.run_resultand gates onHAS_QEMU, so its exec tests really run.tests/test_x64_recomp.py33/33,tests/test_recomp_census.py11/11. Full standalone suite: 513 run, 509 pass; the 4 that fail aremain's 4.Beyond the file, I generated cases under QEMU and compared each against a small Python model:
bsf,bsr,tzcnt,lzcnt,popcntat 16, 32 and 64 bits over 14 source values (0, single bits at each width boundary, all ones, mixed): the full 64-bit destination (so the untouched upper bits, and the unchanged destination whenbsf/bsrsee 0), ZF, and CF fortzcnt/lzcnt/popcnt.bt,bts,btr,btcon a register at 16, 32 and 64 bits, register andimm8index, indices 0 to 65 and0x1234567f(so the modulo-width rule): value and CF.[rbx+8],imm8index and register index including 70, -1, -9 and -64 (so the signed bit offset that reaches outside the operand): the 16 surrounding bytes and CF.popcntafter a borrowingcmpleaves CF=1; x86 clears it.FLAG_WRITERS["popcnt"]is"Z", so the analysis still takes thecmpas the carry's producer and reads ARM C in subtract polarity, while the emitter'ststhas already set C=0. Afterstcthe same sequence gives 0, which is right only because the polarities happen to agree. Suggested fix: declarepopcntas writing C (and N and V, whichtstalso leaves at the x86 values) inFLAG_WRITERS. The table entry is onmain; this pull request is what makes it reachable.Six further mismatches were my model's mistake, not the code's: with a register index of 70 on
[rbx+8]the bit lands in the byte after the 16 I compared. CF was right in those cases and the 16 bytes were correctly unchanged.One thing I hit while writing the cases, for anyone probing this the same way: a
setzplaced betweenbtandsetc, in a function where nothing has produced ZF yet, makes thesetcread 0.stc; setz; setcdoes the same onmain, and it goes away as soon as any earlier instruction sets ZF, so it is a read of a flag that is live into the function, not something this change does.Not tested:
popcnt_jmpewithoutf3, the OF/SF/PF results of the scans, and unaligned memory operands (those wait on #2143). No game files used. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:47:29Z.
Verified by OreoFR
5cb45921e+ PR heada49107929python -I tests/test_x64_recomp_bitops.pyAI-assisted (Claude Code, Claude Opus 5.5).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T21:53:01Z.
Merged after OreoFR's red/green check (check) (RTX 4070 Laptop, driver 621.40, Windows 11) and a local re-check here. Merged after local re-verification: this PR merged onto
mainea90aabfcwithout conflict, and its test file(s) (tests/test_x64_recomp_bitops.py) pass there. Earlier today the same tests failed onmainwithout the PR (red/green) and a bundle with other open PRs showed no new failures.check_no_game_data.pyis clean. Windows 11, Python 3.14; no GPU needed for these tests.AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.