fix: the Rust RDNA 2 interpreter sign-extends a literal for signed 64-bit scalar sources #403

Open
github-bot wants to merge 3 commits from github-bot/gh-403 into main AGit
Owner

@⁠Luke-Ervin on GitHub · 2026-11-26 17:32 UTC · original
Luke-Ervin:fix/rdna2-ref-signed64-literal → main · +103 −5 · 4 files · 3 commits · open

Change

The Rust reference interpreter (crates/anyxboxx-rdna2-ref/src/scalar.rs) now sign-extends a 32-bit literal read as S0 by s_ashr_i64, s_bfe_i64 and s_flbit_i32_i64, as the Python model does.

AMD's RDNA 2 ISA (November 2020), section 6.2.1, "Literal Expansion to 64 bits" (p. 43): "64-bit unsigned integer: zero extended to 64 bits; 64-bit signed integer: sign extended to 64 bits". The Python applies it through signed_literal_s0 (emu/rdna2_exec_bits.py); the Rust port had not carried it over and zero-extended every 64-bit literal. Before this change:

Instruction Rust Python
s_flbit_i32_i64 s51, 0xFFFFFFF0 32 60
s_ashr_i64 s[40:41], 0x80000000, 1 0x0000000040000000 0xFFFFFFFFC0000000
s_bfe_i64 of 0x80000000 (offset 4, width 40) 0x0000000008000000 0xFFFFFFFFF8000000

read_signed64_s0 sign-extends only when S0 is the literal code; a register pair or an inline constant is read unchanged. binary_signed64 is the two-operand helper for s_ashr_i64 and s_bfe_i64; the generic binary() is untouched, so no other instruction changes. The unsigned and bitwise 64-bit sources (s_bfe_u64, s_flbit_i32_b64, s_mov_b64…) keep zero-extending.

docs/rust-full-status.md and docs/fr/rust-full-status.md record the finding. No README row or test-count change.

A second commit corrects the French docs line only: "bitwise" is "bit à bit", not "binaires" (which reads as "two-operand"). Its message also corrects a count in the first commit's message (three unsigned or bitwise controls, not four), which was left as it is rather than rewritten on an open pull request.

Linked issue: none.

Verification

Run on Linux (Ubuntu, Python 3.13, Rust 1.98.1), on top of main at 47759e8 (and again with the second commit, same results; and merged with the newest main, 74dfb48, with #402 and the third fix: 1458 passed, 449 skipped), with anyxboxx-rdna2-ref and anyxboxx-pe built by cargo build --release -p anyxboxx-rdna2-ref -p anyxboxx-pe:

python -m pytest                → 1453 passed, 449 skipped
python -m pytest tests/test_rdna2_ref_parity.py → 41 passed
python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed!
cargo test -p anyxboxx-rdna2-ref → 136 passed; 0 failed
cargo clippy -p anyxboxx-rdna2-ref --all-targets -- -D warnings → no warnings
cargo fmt --check -p anyxboxx-rdna2-ref → clean

Not run (the 449 skipped tests): everything that needs Windows, the C++ runtime, dxc or a GPU, and the Rust crates that only build for Windows; cargo test --workspace for the same reason. No Windows machine was available. The change is plain integer arithmetic. No game was run.

Oracle and test data

  • Oracle: the Python model (test_signed_64_bit_sources_sign_extend_a_32_bit_literal in tests/test_rdna2_exec_bits.py pins its values against the ISA), through tests/test_rdna2_ref_parity.py.
  • How it was found: the parity generator of that file, run on 48,000 more seeds. One program ran s_flbit_i32_i64 on the literal 0xFACE5766 (Rust 0x20, Python 0x25). The 300 seeded programs never paired one of these three instructions with a literal whose bit 31 is set.
  • Test data: built in the tests (no external data).
  • Failure case: test_signed_64_bit_sources_sign_extend_a_literal_in_both_models fails on the old release binary. It compares the two interpreters on the three signed instructions and three unsigned or bitwise controls (s_bfe_u64, s_flbit_i32_b64, s_mov_b64) with five literals, and checks two values outright. The Rust unit test a_literal_is_sign_extended_for_a_signed_64_bit_source_only repeats the Python test's cases.

Checklist

  • The PR covers a single reviewable behaviour.
  • A test fails without this change and passes with it.
  • The commands and results of each check are given above.
  • An unimplemented function returns an error (E_NOTIMPL), never a fake success.
  • README: the same change in the English and French READMEs (test_readme_sync.py checks it). No README change needed.
  • No game, console or SDK file, no key, no dump, no download link.
  • Findings drawn from a title: none in this PR.
  • Code taken from another project: none in this PR.

This change was made with AI assistance (Claude Code): the bug was found, fixed and tested by the AI as described above. It was not tested on Windows.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CVSn4bUDfLBFvceKVLPSX5

**@⁠Luke-Ervin** on GitHub · 2026-11-26 17:32 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/403) `Luke-Ervin:fix/rdna2-ref-signed64-literal` → `main` · +103 −5 · 4 files · 3 commits · open ## Change The Rust reference interpreter (`crates/anyxboxx-rdna2-ref/src/scalar.rs`) now sign-extends a 32-bit literal read as S0 by `s_ashr_i64`, `s_bfe_i64` and `s_flbit_i32_i64`, as the Python model does. AMD's RDNA 2 ISA (November 2020), section 6.2.1, "Literal Expansion to 64 bits" (p. 43): "64-bit unsigned integer: zero extended to 64 bits; 64-bit signed integer: sign extended to 64 bits". The Python applies it through `signed_literal_s0` (`emu/rdna2_exec_bits.py`); the Rust port had not carried it over and zero-extended every 64-bit literal. Before this change: | Instruction | Rust | Python | |---|---|---| | `s_flbit_i32_i64 s51, 0xFFFFFFF0` | 32 | 60 | | `s_ashr_i64 s[40:41], 0x80000000, 1` | `0x0000000040000000` | `0xFFFFFFFFC0000000` | | `s_bfe_i64` of `0x80000000` (offset 4, width 40) | `0x0000000008000000` | `0xFFFFFFFFF8000000` | `read_signed64_s0` sign-extends only when S0 is the literal code; a register pair or an inline constant is read unchanged. `binary_signed64` is the two-operand helper for `s_ashr_i64` and `s_bfe_i64`; the generic `binary()` is untouched, so no other instruction changes. The unsigned and bitwise 64-bit sources (`s_bfe_u64`, `s_flbit_i32_b64`, `s_mov_b64`…) keep zero-extending. `docs/rust-full-status.md` and `docs/fr/rust-full-status.md` record the finding. No README row or test-count change. A second commit corrects the French docs line only: "bitwise" is "bit à bit", not "binaires" (which reads as "two-operand"). Its message also corrects a count in the first commit's message (three unsigned or bitwise controls, not four), which was left as it is rather than rewritten on an open pull request. Linked issue: none. ## Verification Run on Linux (Ubuntu, Python 3.13, Rust 1.98.1), on top of `main` at `47759e8` (and again with the second commit, same results; and merged with the newest `main`, `74dfb48`, with #402 and the third fix: 1458 passed, 449 skipped), with `anyxboxx-rdna2-ref` and `anyxboxx-pe` built by `cargo build --release -p anyxboxx-rdna2-ref -p anyxboxx-pe`: ``` python -m pytest → 1453 passed, 449 skipped python -m pytest tests/test_rdna2_ref_parity.py → 41 passed python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! cargo test -p anyxboxx-rdna2-ref → 136 passed; 0 failed cargo clippy -p anyxboxx-rdna2-ref --all-targets -- -D warnings → no warnings cargo fmt --check -p anyxboxx-rdna2-ref → clean ``` Not run (the 449 skipped tests): everything that needs Windows, the C++ runtime, `dxc` or a GPU, and the Rust crates that only build for Windows; `cargo test --workspace` for the same reason. No Windows machine was available. The change is plain integer arithmetic. No game was run. ## Oracle and test data - Oracle: the Python model (`test_signed_64_bit_sources_sign_extend_a_32_bit_literal` in `tests/test_rdna2_exec_bits.py` pins its values against the ISA), through `tests/test_rdna2_ref_parity.py`. - How it was found: the parity generator of that file, run on 48,000 more seeds. One program ran `s_flbit_i32_i64` on the literal `0xFACE5766` (Rust `0x20`, Python `0x25`). The 300 seeded programs never paired one of these three instructions with a literal whose bit 31 is set. - Test data: built in the tests (no external data). - Failure case: `test_signed_64_bit_sources_sign_extend_a_literal_in_both_models` fails on the old release binary. It compares the two interpreters on the three signed instructions and three unsigned or bitwise controls (`s_bfe_u64`, `s_flbit_i32_b64`, `s_mov_b64`) with five literals, and checks two values outright. The Rust unit test `a_literal_is_sign_extended_for_a_signed_64_bit_source_only` repeats the Python test's cases. ## Checklist - [x] The PR covers a single reviewable behaviour. - [x] A test fails without this change and passes with it. - [x] The commands and results of each check are given above. - [x] An unimplemented function returns an error (`E_NOTIMPL`), never a fake success. - [x] README: the same change in the English and French READMEs (`test_readme_sync.py` checks it). No README change needed. - [x] No game, console or SDK file, no key, no dump, no download link. - [x] Findings drawn from a title: none in this PR. - [x] Code taken from another project: none in this PR. This change was made with AI assistance (Claude Code): the bug was found, fixed and tested by the AI as described above. It was not tested on Windows. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01CVSn4bUDfLBFvceKVLPSX5
s_ashr_i64, s_bfe_i64 and s_flbit_i32_i64 read S0 as a signed 64-bit integer.
When S0 is the 32-bit literal, AMD's RDNA 2 ISA (November 2020, section 6.2.1,
"Literal Expansion to 64 bits", p. 43) says: "64-bit unsigned integer: zero
extended to 64 bits; 64-bit signed integer: sign extended to 64 bits".

The Python model has done this since the fix for these three instructions
(signed_literal_s0 in emu/rdna2_exec_bits.py, tested by
test_signed_64_bit_sources_sign_extend_a_32_bit_literal). The Rust port
(crates/anyxboxx-rdna2-ref/src/scalar.rs) did not carry it over: read_scalar
turns the literal into a u64 by zero extension for every 64-bit source, and
these three instructions used it unchanged. Results with a literal whose bit
31 is set were therefore wrong in Rust and different from the Python:

  s_flbit_i32_i64 s51, 0xFFFFFFF0     Rust 32, Python 60
  s_ashr_i64 s[40:41], 0x80000000, 1  Rust 0x0000000040000000,
                                      Python 0xFFFFFFFFC0000000
  s_bfe_i64 (offset 4, width 40)      Rust 0x0000000008000000,
                                      Python 0xFFFFFFFFF8000000

What changed:

- read_signed64_s0 reads S0 as before and sign-extends it from 32 bits only
  when S0 is the literal code. A register pair or an inline constant is read
  unchanged, as in the Python (which only rewrites the literal).
- binary_signed64 is the two-operand helper for s_ashr_i64 and s_bfe_i64
  (64-bit signed S0, 32-bit S1, 64-bit result, SCC from the result); the
  generic binary() is left alone, so no other instruction changes.
- s_flbit_i32_i64 uses read_signed64_s0 directly.

S1 keeps reading the plain 32-bit literal. In the Python, S1 shares the
rewritten literal, but both instructions only read its low 23 bits (shift
count, or offset and width), so the two agree for every value.

The unsigned and bitwise 64-bit sources (s_bfe_u64, s_flbit_i32_b64,
s_mov_b64, ...) keep zero-extending; the tests check that too.

Why the existing tests missed it: the 300 seeded parity programs never paired
one of these three instructions with a literal whose bit 31 is set. It was
found by running the same generator on 48,000 more seeds; one program ran
s_flbit_i32_i64 on the literal 0xFACE5766 (Rust 0x20, Python 0x25).

Tests:
- scalar.rs: a_literal_is_sign_extended_for_a_signed_64_bit_source_only, the
  cases of the Python test, plus a register pair read without extension.
- tests/test_rdna2_ref_parity.py:
  test_signed_64_bit_sources_sign_extend_a_literal_in_both_models compares
  the two interpreters on the three signed instructions and four unsigned or
  bitwise controls with five literals, and checks two values outright so that
  two models wrong in the same way cannot pass. It fails on the old binary.

docs/rust-full-status.md and its French version record the finding.

Not checked: Windows. The change is plain integer arithmetic.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CVSn4bUDfLBFvceKVLPSX5
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 17:36:26 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-26 17:32 UTC · original · edited 2026-11-26 17:33 UTC

Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T17:33:53.593789Z 3f7435a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@⁠codex review" or "@⁠codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-26 17:32 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/403#issuecomment-7199187572) · edited 2026-11-26 17:33 UTC <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"3f7435abd86e731a5d416b1ccb6df99d5ffc4867","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-26T17:33:53.593789Z">2026-11-26T17:33:53.593789Z</relative-time> | `3f7435a` | PR opened | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@⁠codex review" or "@⁠codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
The French line added by the previous commit translated "bitwise" as
"binaires", which in French reads as "two-operand" rather than "bit by bit".
"bit à bit" is the term the sentence means: the unsigned and bitwise 64-bit
sources (s_bfe_u64, s_flbit_i32_b64, s_mov_b64, ...) keep zero-extending.

Also a correction to the previous commit's message, which is left as it is
rather than rewritten on an open pull request: its "Tests" paragraph says the
parity test compares "four unsigned or bitwise controls". It compares three:
s_bfe_u64, s_flbit_i32_b64 and s_mov_b64.

Both found by an independent review of the change. No code or test changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CVSn4bUDfLBFvceKVLPSX5
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/403/head:github-bot/gh-403'
git switch 'github-bot/gh-403'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-403'
git switch 'github-bot/gh-403'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-403'
git switch 'github-bot/gh-403'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-403'
git switch 'main'
git merge --squash 'github-bot/gh-403'
git switch 'main'
git merge --ff-only 'github-bot/gh-403'
git switch 'main'
git merge 'github-bot/gh-403'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.