startup: lower NEG on an immediate source of v_cndmask_b32_e64 from its encoding #1925

Merged
Leyukaka merged 1 commit from upstream-pr/1932 into main 2026-11-24 21:53:41 +00:00
Leyukaka commented 2026-11-24 21:39:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-24T21:39:58Z.


Refs #1723 (row "v_cndmask with NEG on an immediate", new refusal since #1710).

Change

v_cndmask_b32 is a bitwise select, so the - modifier on a source flips that source's bit 31 (#1710). For immediate sources the printed operand is ambiguous: -1 can be the inline constant -1 (0xffffffff) or inline 1 with NEG (0x80000001). decode() in tools/startup/gfx2hlsl.py therefore refused NEG on an immediate, as kept in #1710 (dfc73a1b).

This lifts that refusal by resolving the value from the encoding. For v_cndmask_b32_e64, when a source field with its NEG bit set holds an immediate, decode() takes the source word and rewrites that operand as an unambiguous hex literal with bit 31 flipped. The source word is:

  • for an integer inline constant (128..208), its two's-complement word;
  • for a float inline constant (240..248), its binary32 bits (the values are duplicated from INLINE_FLOAT_LITERALS in tools/disasm_rdna2.py);
  • for a literal (255), the following dword.

The handler then sees a plain literal. For example, inline 1 with NEG becomes 0x80000001u while plain inline -1 stays 0xffffffffu, and NEG of 0.5 becomes 0xbf000000u.

Unchanged:

  • plain immediates, which keep the values fixed in #1710;
  • register sources with NEG;
  • the instruction length (the literal form consumes its dword);
  • the refusals for clamp, mul:2, ABS on register sources and negated masks.

ABS on an immediate, with or without NEG, is refused in the decoder. A literal form without its dword is also refused, but that case is not exercised by the test.

The existing docs/experiments.md paragraph of #1710 is updated; CTest startup_gfx2hlsl_cndmask_neg is unchanged and there is no new registration.

Verification

On 3ef8e9bd (based on main at b96960b7), Windows 11 build 26300, Python 3.14.10:

python -B tests/startup_gfx2hlsl_cndmask_neg_check.py

38 checks, PASS. The test encodes and decodes the select and checks:

  • NEG on src0 at every boundary encoding (128, 129, 192, 193, 208 and 240..248), with the exact word for each;
  • NEG on a literal in src1, and that the next instruction starts after the literal dword;
  • NEG on both sources;
  • plain immediates and register NEG unchanged;
  • the existing refusals;
  • ABS on an inline immediate, and ABS with NEG on an inline immediate and on a literal.

An in-process control restores the old refusal and requires both the inline and the literal case to be refused again. On main (b96960b7) the script stops at its first NEG-on-immediate case with an uncaught Unsupported: v_cndmask_b32_e64 NEG on an immediate.

Also run on this branch, each python -B tests/<name> exiting 0:

  • startup_uniform_lane_check.py, startup_gfx2hlsl_op_refusals_check.py, startup_gfx2hlsl_cvt_alu_check.py, startup_gfx2hlsl_operand_count_check.py, startup_gfx2hlsl_bit_alu_check.py;
  • startup_gfx2hlsl_mad_check.py, startup_gfx2hlsl_smem_offset_check.py, startup_gfx2hlsl_scalar_m0_check.py, startup_gfx2hlsl_vopc_sdwa_check.py, startup_translator_check.py, startup_flat_param_check.py, startup_inline_sampler_check.py;
  • startup_buffer_address_check.py, startup_sampler_heap_check.py, startup_stream_fetch_check.py, startup_image_2d_array_check.py, startup_image_load_mip_check.py;
  • docs_links_check.py, bidi_controls_check.py.

CTest was not run for this change.

Evidence and follow-up

  • Source-built fixture (MIT), no title or package needed.
  • The bit-31 flip for a negated immediate follows the register case from #1710 and the #1710 review. It was not measured on hardware here, and the emitted HLSL is compared as text only.
  • Not covered: whether locally refused shaders now translate, which needs local inputs. ABS on an immediate stays refused because its hardware result is not established here.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6591951724 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1932). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-24T21:39:58Z. --- Refs #1723 (row "`v_cndmask` with NEG on an immediate", new refusal since #1710). ## Change `v_cndmask_b32` is a bitwise select, so the `-` modifier on a source flips that source's bit 31 (#1710). For immediate sources the printed operand is ambiguous: `-1` can be the inline constant -1 (`0xffffffff`) or inline 1 with NEG (`0x80000001`). `decode()` in `tools/startup/gfx2hlsl.py` therefore refused NEG on an immediate, as kept in #1710 (`dfc73a1b`). This lifts that refusal by resolving the value from the encoding. For `v_cndmask_b32_e64`, when a source field with its NEG bit set holds an immediate, `decode()` takes the source word and rewrites that operand as an unambiguous hex literal with bit 31 flipped. The source word is: - for an integer inline constant (128..208), its two's-complement word; - for a float inline constant (240..248), its binary32 bits (the values are duplicated from `INLINE_FLOAT_LITERALS` in `tools/disasm_rdna2.py`); - for a literal (255), the following dword. The handler then sees a plain literal. For example, inline 1 with NEG becomes `0x80000001u` while plain inline -1 stays `0xffffffffu`, and NEG of 0.5 becomes `0xbf000000u`. Unchanged: - plain immediates, which keep the values fixed in #1710; - register sources with NEG; - the instruction length (the literal form consumes its dword); - the refusals for clamp, `mul:2`, ABS on register sources and negated masks. ABS on an immediate, with or without NEG, is refused in the decoder. A literal form without its dword is also refused, but that case is not exercised by the test. The existing `docs/experiments.md` paragraph of #1710 is updated; CTest `startup_gfx2hlsl_cndmask_neg` is unchanged and there is no new registration. ## Verification On `3ef8e9bd` (based on `main` at `b96960b7`), Windows 11 build 26300, Python 3.14.10: ```text python -B tests/startup_gfx2hlsl_cndmask_neg_check.py ``` 38 checks, `PASS`. The test encodes and decodes the select and checks: - NEG on src0 at every boundary encoding (128, 129, 192, 193, 208 and 240..248), with the exact word for each; - NEG on a literal in src1, and that the next instruction starts after the literal dword; - NEG on both sources; - plain immediates and register NEG unchanged; - the existing refusals; - ABS on an inline immediate, and ABS with NEG on an inline immediate and on a literal. An in-process control restores the old refusal and requires both the inline and the literal case to be refused again. On `main` (`b96960b7`) the script stops at its first NEG-on-immediate case with an uncaught `Unsupported: v_cndmask_b32_e64 NEG on an immediate`. Also run on this branch, each `python -B tests/<name>` exiting 0: - `startup_uniform_lane_check.py`, `startup_gfx2hlsl_op_refusals_check.py`, `startup_gfx2hlsl_cvt_alu_check.py`, `startup_gfx2hlsl_operand_count_check.py`, `startup_gfx2hlsl_bit_alu_check.py`; - `startup_gfx2hlsl_mad_check.py`, `startup_gfx2hlsl_smem_offset_check.py`, `startup_gfx2hlsl_scalar_m0_check.py`, `startup_gfx2hlsl_vopc_sdwa_check.py`, `startup_translator_check.py`, `startup_flat_param_check.py`, `startup_inline_sampler_check.py`; - `startup_buffer_address_check.py`, `startup_sampler_heap_check.py`, `startup_stream_fetch_check.py`, `startup_image_2d_array_check.py`, `startup_image_load_mip_check.py`; - `docs_links_check.py`, `bidi_controls_check.py`. CTest was not run for this change. ## Evidence and follow-up - Source-built fixture (MIT), no title or package needed. - The bit-31 flip for a negated immediate follows the register case from #1710 and the #1710 review. It was not measured on hardware here, and the emitted HLSL is compared as text only. - Not covered: whether locally refused shaders now translate, which needs local inputs. ABS on an immediate stays refused because its hardware result is not established here. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
upstream-sync closed this pull request 2026-11-24 22:00:56 +00:00
tedin7 commented 2026-11-24 22:00:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:49:56Z.

Review: COMMENTED


Review of head 3ef8e9bdfd

Result: no P1, no P2, one P3.

Commands run locally, with results:

  • git merge-tree --write-tree --name-only origin/main HEAD: clean, no conflicts.
  • gh pr checks 1932: Linux Python checks, gfx1013 fixtures and Windows build all pass. The /O2 job and the chart job were skipped.
  • git diff --check origin/main...HEAD: clean.
  • python3 tests/bidi_controls_check.py: pass.
  • python3 tests/docs_links_check.py: pass.
  • python3 tests/startup_gfx2hlsl_cndmask_neg_check.py: PASS.
  • All tests/startup_gfx2hlsl*_check.py: pass.
  • No C++ or CMake files are touched, so the Windows node was not used.

Read of the change:

  • Inline integer mapping is right: 128 is 0, 129..192 is 1..64, 193..208 is -1..-16.
  • The float table matches the RDNA 2 order for 240..248, including 1/(2*pi).
  • The literal dword is read from offset 8 only when the instruction is 12 bytes. A missing dword is refused.
  • ABS on an immediate is refused, with or without NEG. Register operands are unaffected.
  • Only the two data operands are rewritten. The mask operand and any trailing modifiers are kept.
  • The tests assert exact words at every integer edge and for every inline float. They also cover NEG on two immediates at once and the next instruction offset after a literal.

Mutation: in tools/startup/gfx2hlsl.py I changed if source <= 192: to if source < 192:. The test failed with NEG on src0 encoding 192: MISMATCH. I then ran git checkout -- ., so the tree is clean.

P1: none.

P2: none.

P3:

  • tests/startup_gfx2hlsl_cndmask_neg_check.py, section 9 (the "restore the old refusal" block): it re-creates removed behavior and asserts that the old code would reject the new inputs. That guards the past, not the current behavior, and the exact-word checks above already cover it. I suggest deleting it.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6047964770 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1932#pullrequestreview-6047964770). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T21:49:56Z. **Review:** COMMENTED --- Review of head 3ef8e9bdfdeb378c981f867ec1d1cebb63ea73e3 Result: no P1, no P2, one P3. Commands run locally, with results: - `git merge-tree --write-tree --name-only origin/main HEAD`: clean, no conflicts. - `gh pr checks 1932`: Linux Python checks, gfx1013 fixtures and Windows build all pass. The /O2 job and the chart job were skipped. - `git diff --check origin/main...HEAD`: clean. - `python3 tests/bidi_controls_check.py`: pass. - `python3 tests/docs_links_check.py`: pass. - `python3 tests/startup_gfx2hlsl_cndmask_neg_check.py`: PASS. - All `tests/startup_gfx2hlsl*_check.py`: pass. - No C++ or CMake files are touched, so the Windows node was not used. Read of the change: - Inline integer mapping is right: 128 is 0, 129..192 is 1..64, 193..208 is -1..-16. - The float table matches the RDNA 2 order for 240..248, including 1/(2*pi). - The literal dword is read from offset 8 only when the instruction is 12 bytes. A missing dword is refused. - ABS on an immediate is refused, with or without NEG. Register operands are unaffected. - Only the two data operands are rewritten. The mask operand and any trailing modifiers are kept. - The tests assert exact words at every integer edge and for every inline float. They also cover NEG on two immediates at once and the next instruction offset after a literal. Mutation: in `tools/startup/gfx2hlsl.py` I changed `if source <= 192:` to `if source < 192:`. The test failed with `NEG on src0 encoding 192: MISMATCH`. I then ran `git checkout -- .`, so the tree is clean. P1: none. P2: none. P3: - `tests/startup_gfx2hlsl_cndmask_neg_check.py`, section 9 (the "restore the old refusal" block): it re-creates removed behavior and asserts that the old code would reject the new inputs. That guards the past, not the current behavior, and the exact-word checks above already cover it. I suggest deleting it. AI-assisted; commands listed were executed locally.
ChazY11 commented 2026-11-24 22:01:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T21:49:03Z.


Ran it, broke it four ways, and checked the duplicated table separately

Windows 11 build 26300.9974, device-free:

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_cndmask_neg_check.py
PASS      exit 0

Mutations to tools/startup/gfx2hlsl.py, each reverted and re-run:

mutation result
bits ^ 0x80000000 → bits (NEG stops flipping bit 31) caught
bits = source - 128 → source - 127 (integer inline constant off by one) caught
CNDMASK_INLINE_FLOAT_BITS[source - 240] → [source - 241] (table shifted) caught
raise Unsupported('… ABS on an immediate') → pass caught

The third is the one I wanted to know about: the new table duplicates nine words that already exist in
tools/disasm_rdna2.py, so an off-by-one there would silently produce wrong literals for eight of the nine encodings
while every "did it stop refusing" case still passed. Your fixture notices.

Independent check of the table itself

Because a duplicated constant table is exactly the kind of thing that drifts, I checked it against the encoding rather than
against your expectations: for each encoding 240–248 with NEG set on source 0, translate and require
literal == CNDMASK_INLINE_FLOAT_BITS[enc - 240] ^ 0x80000000:

  240  literal=0xbf000000u  table=3f000000  expect=bf000000  ok
  241  literal=0x3f000000u  table=bf000000  expect=3f000000  ok
  242  literal=0xbf800000u  table=3f800000  expect=bf800000  ok
  ...
  248  literal=0xbe22f983u  table=3e22f983  expect=be22f983  ok
  mismatches: 0

Nine of nine, including the 1/(2*pi) entry at 248 that is easy to leave out. Note what this does not prove: my probe
reuses your encode_vop2_e64, so it confirms the table agrees with the flip arithmetic, not that the encoder sets NEG at bit
29+i. Your ABS/NEG refusal cases and the v30, -v30 path cover that separately.

One observation on the wording

The description says the float entries are "duplicated from INLINE_FLOAT_LITERALS in tools/disasm_rdna2.py". That dict is
keyed by bits with string values, so the copy is not mechanical — the encoding order has to be reconstructed by hand. Worth
a comment at CNDMASK_INLINE_FLOAT_BITS saying the order is 240…248 rather than "the same as that dict", which a reader may
try to diff and fail. (If the decoder ever grows a 240 + index table, this copy should read from it.)

Verdict

Reviewed. The refusal lift is narrow, the bit-flip path is pinned, and the copied table is correct on all nine entries.

Notes

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7145426884 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1932#issuecomment-7145426884). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T21:49:03Z. --- ## Ran it, broke it four ways, and checked the duplicated table separately Windows 11 build 26300.9974, device-free: ```powershell out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_cndmask_neg_check.py PASS exit 0 ``` Mutations to `tools/startup/gfx2hlsl.py`, each reverted and re-run: | mutation | result | |---|---| | `bits ^ 0x80000000` → `bits` (NEG stops flipping bit 31) | **caught** | | `bits = source - 128` → `source - 127` (integer inline constant off by one) | **caught** | | `CNDMASK_INLINE_FLOAT_BITS[source - 240]` → `[source - 241]` (table shifted) | **caught** | | `raise Unsupported('… ABS on an immediate')` → `pass` | **caught** | The third is the one I wanted to know about: the new table duplicates nine words that already exist in `tools/disasm_rdna2.py`, so an off-by-one there would silently produce wrong literals for eight of the nine encodings while every "did it stop refusing" case still passed. Your fixture notices. ## Independent check of the table itself Because a duplicated constant table is exactly the kind of thing that drifts, I checked it against the encoding rather than against your expectations: for each encoding 240–248 with NEG set on source 0, translate and require `literal == CNDMASK_INLINE_FLOAT_BITS[enc - 240] ^ 0x80000000`: ```text 240 literal=0xbf000000u table=3f000000 expect=bf000000 ok 241 literal=0x3f000000u table=bf000000 expect=3f000000 ok 242 literal=0xbf800000u table=3f800000 expect=bf800000 ok ... 248 literal=0xbe22f983u table=3e22f983 expect=be22f983 ok mismatches: 0 ``` Nine of nine, including the `1/(2*pi)` entry at 248 that is easy to leave out. Note what this does **not** prove: my probe reuses your `encode_vop2_e64`, so it confirms the table agrees with the flip arithmetic, not that the encoder sets NEG at bit `29+i`. Your ABS/NEG refusal cases and the `v30, -v30` path cover that separately. ## One observation on the wording The description says the float entries are "duplicated from `INLINE_FLOAT_LITERALS` in `tools/disasm_rdna2.py`". That dict is keyed by **bits** with string values, so the copy is not mechanical — the encoding order has to be reconstructed by hand. Worth a comment at `CNDMASK_INLINE_FLOAT_BITS` saying the order is 240…248 rather than "the same as that dict", which a reader may try to diff and fail. (If the decoder ever grows a `240 + index` table, this copy should read from it.) ## Verdict Reviewed. The refusal lift is narrow, the bit-flip path is pinned, and the copied table is correct on all nine entries. ## Notes - AI-assisted review.
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
R0Y00/Xbox2Windows!1925
No description provided.