fix: the Rust RDNA 2 interpreter keeps the model's NaN for the unfused multiply-adds #484

Merged
Luke-Ervin merged 1 commit from upstream-pr/484 into main 2026-11-27 12:57:25 +00:00
Luke-Ervin commented 2026-11-27 04:11:14 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by Luke-Ervin on 2026-11-27T04:11:14Z.


Change

The Rust reference interpreter (crates/anyxboxx-rdna2-ref) now returns the same NaN as the Python model for the unfused multiply-adds v_mac_f32, v_madmk_f32 and v_madak_f32.

They were written as the plain expression a * b + c (vector.rs). Which NaN such an expression returns is not fixed by the language: with both a factor and the addend NaN, the compiler may evaluate the addition with its operands swapped, and here it did. The Rust returned the addend's NaN where the model (_mul_add, x86-64 arithmetic) returns the product's:

Instruction Rust Python
v_madak_f32 v4, v1 (1.5), v2 (0x7FC00B0B), K = 0x7FC00C0C 0x7FC00C0C 0x7FC00B0B
v_madmk_f32 v4, v1 (1.5), K = 0x7FC00C0C, v2 (0x7FC00B0B) 0x7FC00B0B 0x7FC00C0C
v_mac_f32 v2 (acc 0x7FC00B0B), v3 (1.0), v1 (0x7FC00A0A) 0x7FC00B0B 0x7FC00A0A
  • float::mul_add(a, b, c) writes the model's order out: the first NaN factor (a, then b), or the NaN the product itself makes (∞ × 0), and only then a NaN addend. With no NaN operand it computes a * b + c as before (∞ − ∞ gives the processor's NaN, as in the model). The product's own NaN has to come before a NaN addend, as in (a * b) + c; a plain "first NaN operand" rule would get ∞ × 0 + NaN wrong.
  • v_mac_f32, v_madmk_f32 (a, K, b) and v_madak_f32 (a, b, K) use it.
  • v_mad_f32 (vop3.rs) uses it too. It gave the model's NaN in this build, but only by the compiler's choice, and CI builds the crate for Windows at a different optimisation level, so it is written out rather than left to chance. Its results do not change here.

The fused forms (v_fma_f32, v_fmac_f32, v_fmamk_f32, v_fmaak_f32) are untouched: they go through float::fma, which already picks the NaN explicitly.

As with the port's other NaN rules (float::fma, the float atomics, v_interp), this follows the model's host arithmetic, not a measured RDNA 2 result: the ISA does not say which NaN an unfused multiply-add returns.

docs/rust-full-status.md and docs/fr/rust-full-status.md record the finding. No README row or test-count change (1908 collected on main with this change, still 1900+).

Linked issue: none.

Verification

Run on Linux (Ubuntu, Python 3.13, Rust 1.98.1), on top of main at b62053f, and again merged with the newest main and with #402, #403 and #437 (1459 passed, 452 skipped), with anyxboxx-rdna2-ref and anyxboxx-pe built by cargo build --release -p anyxboxx-rdna2-ref -p anyxboxx-pe:

python -m pytest                → 1456 passed, 452 skipped
python -m pytest tests/test_rdna2_ref_parity.py → 42 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 452 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 new helper only compares and selects values, so its result no longer depends on how the compiler orders the operations. No game was run.

Oracle and test data

  • Oracle: the Python model, through tests/test_rdna2_ref_parity.py.
  • How it was found: the parity generator of that file, run on 60,000 more seeds on main with #402, #403 and #437 merged in. Five programs ended with a different NaN in a register; the three traced were all a v_madak_f32 with a NaN K. With this change the same 60,000 programs end with the same values in both models.
  • Test data: built in the tests (no external data).
  • Failure case: test_unfused_multiply_add_nans_match_the_python_model fails on the old binary. It runs v_mad_f32, v_mac_f32 (VOP2 and VOP3), v_madak_f32 and v_madmk_f32 with each of the three operand positions finite, +inf, +0 or a NaN whose payload names its position: 320 programs, all of which run in both models, with NaN results covering every position and the processor's NaN. It also checks two values outright, so that two models wrong in the same way cannot pass. Rust unit test: mul_add_keeps_the_nan_of_the_product_before_the_addend (float.rs).

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

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5521058438 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/484). Originally posted by [Luke-Ervin](https://github.com/Luke-Ervin) on 2026-11-27T04:11:14Z. --- ## Change The Rust reference interpreter (`crates/anyxboxx-rdna2-ref`) now returns the same NaN as the Python model for the unfused multiply-adds `v_mac_f32`, `v_madmk_f32` and `v_madak_f32`. They were written as the plain expression `a * b + c` (`vector.rs`). Which NaN such an expression returns is not fixed by the language: with both a factor and the addend NaN, the compiler may evaluate the addition with its operands swapped, and here it did. The Rust returned the addend's NaN where the model (`_mul_add`, x86-64 arithmetic) returns the product's: | Instruction | Rust | Python | |---|---|---| | `v_madak_f32 v4, v1 (1.5), v2 (0x7FC00B0B)`, K = `0x7FC00C0C` | `0x7FC00C0C` | `0x7FC00B0B` | | `v_madmk_f32 v4, v1 (1.5)`, K = `0x7FC00C0C`, `v2 (0x7FC00B0B)` | `0x7FC00B0B` | `0x7FC00C0C` | | `v_mac_f32 v2 (acc 0x7FC00B0B), v3 (1.0), v1 (0x7FC00A0A)` | `0x7FC00B0B` | `0x7FC00A0A` | - `float::mul_add(a, b, c)` writes the model's order out: the first NaN factor (`a`, then `b`), or the NaN the product itself makes (∞ × 0), and only then a NaN addend. With no NaN operand it computes `a * b + c` as before (∞ − ∞ gives the processor's NaN, as in the model). The product's own NaN has to come before a NaN addend, as in `(a * b) + c`; a plain "first NaN operand" rule would get ∞ × 0 + NaN wrong. - `v_mac_f32`, `v_madmk_f32` (a, K, b) and `v_madak_f32` (a, b, K) use it. - `v_mad_f32` (`vop3.rs`) uses it too. It gave the model's NaN in this build, but only by the compiler's choice, and CI builds the crate for Windows at a different optimisation level, so it is written out rather than left to chance. Its results do not change here. The fused forms (`v_fma_f32`, `v_fmac_f32`, `v_fmamk_f32`, `v_fmaak_f32`) are untouched: they go through `float::fma`, which already picks the NaN explicitly. As with the port's other NaN rules (`float::fma`, the float atomics, `v_interp`), this follows the model's host arithmetic, not a measured RDNA 2 result: the ISA does not say which NaN an unfused multiply-add returns. `docs/rust-full-status.md` and `docs/fr/rust-full-status.md` record the finding. No README row or test-count change (1908 collected on `main` with this change, still `1900+`). Linked issue: none. ## Verification Run on Linux (Ubuntu, Python 3.13, Rust 1.98.1), on top of `main` at `b62053f`, and again merged with the newest `main` and with #402, #403 and #437 (1459 passed, 452 skipped), with `anyxboxx-rdna2-ref` and `anyxboxx-pe` built by `cargo build --release -p anyxboxx-rdna2-ref -p anyxboxx-pe`: ``` python -m pytest → 1456 passed, 452 skipped python -m pytest tests/test_rdna2_ref_parity.py → 42 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 452 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 new helper only compares and selects values, so its result no longer depends on how the compiler orders the operations. No game was run. ## Oracle and test data - Oracle: the Python model, through `tests/test_rdna2_ref_parity.py`. - How it was found: the parity generator of that file, run on 60,000 more seeds on `main` with #402, #403 and #437 merged in. Five programs ended with a different NaN in a register; the three traced were all a `v_madak_f32` with a NaN K. With this change the same 60,000 programs end with the same values in both models. - Test data: built in the tests (no external data). - Failure case: `test_unfused_multiply_add_nans_match_the_python_model` fails on the old binary. It runs `v_mad_f32`, `v_mac_f32` (VOP2 and VOP3), `v_madak_f32` and `v_madmk_f32` with each of the three operand positions finite, +inf, +0 or a NaN whose payload names its position: 320 programs, all of which run in both models, with NaN results covering every position and the processor's NaN. It also checks two values outright, so that two models wrong in the same way cannot pass. Rust unit test: `mul_add_keeps_the_nan_of_the_product_before_the_addend` (`float.rs`). ## 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)
chatgpt-codex-connector[bot] commented 2026-11-27 04:11:27 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by chatgpt-codex-connector[bot] on 2026-11-27T04:11:27Z.


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-27T04:13:11.961640Z 4de8438 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.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7209594532 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/484#issuecomment-7209594532). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/apps/chatgpt-codex-connector) on 2026-11-27T04:11:27Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"4de8438ffd7c02d9c71bda33df09c645eb62e4bd","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-27T04:13:11.961640Z">2026-11-27T04:13:11.961640Z</relative-time> | `4de8438` | 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>
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
LooperSalty/AnyXboxX!484
No description provided.