rdna2spirv: preserve f16 denormals, NaN payloads, and bit-exact clamp in conversions (SHADER-CVT-F16-DENORM-AND-MODS) #2419

Open
github-bot wants to merge 1 commit from github-bot/gh-2419 into main AGit
Owner

@⁠Andretuta on GitHub · 2026-11-27 03:17 UTC · original
ubiquitarianism:fix/cvt-f16-denorm-and-clamp → main · +278 −44 · 6 files · 1 commits · open

Claimed in the coordination issue as: SHADER-CVT-F16-DENORM-AND-MODS (#1608)

Related: #2362 (independent — merges cleanly standalone on main; overlaps 4 lines in tools/rdna2spirv.py dpp_insn src0 neg/abs forwarding, keeping args[first] on rebase so destination-less v_cmpx stays intact)

What and why

  • Route v_cvt_f32_f16 through Gen.f16_in_bits instead of raw UnpackHalf2x16, preserving f16 subnormals, applying neg/abs to the half selected by SDWA or DPP (avoiding 32-bit modify on a 16-bit half), and preserving IEEE=0 NaN sign and payload bits out of driver float arithmetic.
  • Add Gen.clamp_f16_bits (3-instruction exact DX10 clamp on rounded f16 words: negative halves including -0 and NaNs map to +0, positive values cap at 1.0, positive subnormals are preserved bit-exactly) and reuse it in v_cvt_f16_f32, v_div_fixup_f16, v_frexp_mant_f16, and v_ldexp_f16.
  • Audited all 13 remaining UnpackHalf2x16 sites in tools/rdna2spirv.py.
  • Add CPU semantic suite tests/semantic/test_cvt_f16_denorm_mods.py and Vulkan GPU fixture runtime/test/shaders/cs_w32_cvt_f16_mods.asm (wired into runtime/test/shaders/check.py and scripts/run_gpu_tests.py), plus contract update in docs/design.md.

Tested

  • python -I tests/semantic/test_cvt_f16_denorm_mods.py: 13/13 OK (6 fail on unpatched main d1dff7949; validated with both spirv-as and spirv_asm.py + spirv-val --target-env vulkan1.2).
  • Full semantic suite: 501 modules, 6,385 passed, 3 skipped, 0 failed.
  • Vulkan GPU (scripts/run_gpu_tests.py, native and LEONIDA_W64_EMULATE=1): cs_w32_cvt_f16_mods before (d1dff7949): 7FFFFFFF 33800000 36800000 AAAA0001 AAAA03FF AAAA0000 AAAA0000 AAAA0001; after: FFC4A000 33800000 36800000 AAAA0001 AAAA03FF AAAA0000 AAAA0000 AAAA0001 (word 0 fixes v_cvt_f32_f16 DPP neg on NaN; exact 8/8 words in both modes); cs_w32_new_ops (8/8 exact) and ps_sdwa_f16_const (4,096/4,096 pixels exact) pass in both modes.
  • Corpus validation (aggregate counts only): 100/100 shaders containing both v_cvt_f32_f16 and v_cvt_f16_f32 translate and pass spirv-val (100/100 before and after).
  • python -I tests/test_run_gpu_tests.py (71/71 OK), doc/line-ending tests (25/25 OK), python -I scripts/check_no_game_data.py (exit 0).

GPUs tested: NVIDIA GeForce RTX 3060 12 GB, NVIDIA driver 617.14, Windows 10

AI-assisted: yes (Codex gpt-6.2-sol + Antigravity gemini-4-neon)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
**@⁠Andretuta** on GitHub · 2026-11-27 03:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2419) `ubiquitarianism:fix/cvt-f16-denorm-and-clamp` → `main` · +278 −44 · 6 files · 1 commits · open Claimed in the coordination issue as: SHADER-CVT-F16-DENORM-AND-MODS (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) Related: #2362 (independent — merges cleanly standalone on `main`; overlaps 4 lines in `tools/rdna2spirv.py` `dpp_insn` `src0` `neg`/`abs` forwarding, keeping `args[first]` on rebase so destination-less `v_cmpx` stays intact) ### What and why - Route `v_cvt_f32_f16` through `Gen.f16_in_bits` instead of raw `UnpackHalf2x16`, preserving f16 subnormals, applying `neg`/`abs` to the half selected by SDWA or DPP (avoiding 32-bit `modify` on a 16-bit half), and preserving `IEEE=0` NaN sign and payload bits out of driver float arithmetic. - Add `Gen.clamp_f16_bits` (3-instruction exact DX10 clamp on rounded f16 words: negative halves including `-0` and NaNs map to `+0`, positive values cap at `1.0`, positive subnormals are preserved bit-exactly) and reuse it in `v_cvt_f16_f32`, `v_div_fixup_f16`, `v_frexp_mant_f16`, and `v_ldexp_f16`. - Audited all 13 remaining `UnpackHalf2x16` sites in `tools/rdna2spirv.py`. - Add CPU semantic suite `tests/semantic/test_cvt_f16_denorm_mods.py` and Vulkan GPU fixture `runtime/test/shaders/cs_w32_cvt_f16_mods.asm` (wired into `runtime/test/shaders/check.py` and `scripts/run_gpu_tests.py`), plus contract update in `docs/design.md`. ### Tested - `python -I tests/semantic/test_cvt_f16_denorm_mods.py`: **13/13 OK** (6 fail on unpatched `main` `d1dff7949`; validated with both `spirv-as` and `spirv_asm.py` + `spirv-val --target-env vulkan1.2`). - Full semantic suite: **501 modules, 6,385 passed, 3 skipped, 0 failed**. - Vulkan GPU (`scripts/run_gpu_tests.py`, native and `LEONIDA_W64_EMULATE=1`): `cs_w32_cvt_f16_mods` before (`d1dff7949`): `7FFFFFFF 33800000 36800000 AAAA0001 AAAA03FF AAAA0000 AAAA0000 AAAA0001`; after: `FFC4A000 33800000 36800000 AAAA0001 AAAA03FF AAAA0000 AAAA0000 AAAA0001` (word 0 fixes `v_cvt_f32_f16` DPP `neg` on NaN; exact 8/8 words in both modes); `cs_w32_new_ops` (8/8 exact) and `ps_sdwa_f16_const` (4,096/4,096 pixels exact) pass in both modes. - Corpus validation (aggregate counts only): **100/100** shaders containing both `v_cvt_f32_f16` and `v_cvt_f16_f32` translate and pass `spirv-val` (100/100 before and after). - `python -I tests/test_run_gpu_tests.py` (71/71 OK), doc/line-ending tests (25/25 OK), `python -I scripts/check_no_game_data.py` (exit 0). GPUs tested: NVIDIA GeForce RTX 3060 12 GB, NVIDIA driver 617.14, Windows 10 AI-assisted: yes (Codex gpt-6.2-sol + Antigravity gemini-4-neon) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
Route v_cvt_f32_f16 through f16_in_bits so SDWA/DPP half selection, subnormals, neg/abs,
and IEEE=0 NaN sign/payload bits are preserved without raw UnpackHalf2x16 flush risk,
and clamp v_cvt_f16_f32 and v_div_fixup_f16 directly on rounded f16 bits via shared
clamp_f16_bits. Verified on RTX 3060 (driver 617.14, native + --wave64-emulate: fixed
first word 0x7FFFFFFF -> 0xFFC4A000 on cs_w32_cvt_f16_mods), 13/13 new semantic tests,
501-module semantic suite (6,385 passed, 0 failed), and 100/100 real corpus shaders
passing spirv-val.

AI-assisted: Codex gpt-6.2-sol + Antigravity gemini-4-neon.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:19:51 +00:00
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-27 03:48 UTC · original

On a Windows 11 RTX 4090 (NVIDIA 616.64), I get 0 wrong output words out of 786,432 at df294dd8a1, down from 75,766 on base d1dff7949b, using the same synthetic pixel shaders and bitwise expectations.

  • Seven 256×256 draws each supplied all 65,536 binary16 encodings at runtime. Plain widening, negation, absolute value, negated absolute value, clamp, negated SDWA WORD_1, and negated DPP horizontal swap all passed. On base, five forms each missed all 2,046 NaN encodings. The negated DPP swap missed every input, including finite numbers. For example, widening and negating the donor half 0x0001 returned 0x7fffffff on base and the expected 0xb3800000 on the PR. Clamped widening passed on both.
  • Five draws tested clamped narrowing over 327,680 binary32 input words, including values around the minimum half subnormal, the normal boundary, and 1.0. Both revisions matched the expected rounded half and preserved the high 16 destination bits. To avoid framebuffer handling of NaNs, each shader split its result into two unsigned 16-bit pieces and exported each as an exactly representable float. The readback also checked which pixel or DPP donor supplied each input. All modules passed Vulkan 1.2 SPIR-V validation.

For the rebase with #2362, I tested its general neg/abs forwarding on top of this PR. Store the shuffled value and its two modifier flags in args[first] throughout: first is 0 for destination-less v_cmpx and 1 for the conversion. Keeping the older branch's literal args[1] produced 57 failing compare subcases locally. Using args[first] passed all 30 tests across test_cvt_f16_denorm_mods, test_dpp_f16_mods, and test_dpp_compare, and the combined translator passed the same 786,432-word GPU check. This was a local integration test of cf622ce9c2 and df294dd8a1.

**@⁠ecox982745** on GitHub · 2026-11-27 03:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2419#issuecomment-7209283686) On a Windows 11 RTX 4090 (NVIDIA 616.64), I get **0 wrong output words out of 786,432** at `df294dd8a1`, down from **75,766** on base `d1dff7949b`, using the same synthetic pixel shaders and bitwise expectations. - Seven 256×256 draws each supplied all 65,536 binary16 encodings at runtime. Plain widening, negation, absolute value, negated absolute value, clamp, negated SDWA `WORD_1`, and negated DPP horizontal swap all passed. On base, five forms each missed all 2,046 NaN encodings. The negated DPP swap missed every input, including finite numbers. For example, widening and negating the donor half `0x0001` returned `0x7fffffff` on base and the expected `0xb3800000` on the PR. Clamped widening passed on both. - Five draws tested clamped narrowing over 327,680 binary32 input words, including values around the minimum half subnormal, the normal boundary, and 1.0. Both revisions matched the expected rounded half and preserved the high 16 destination bits. To avoid framebuffer handling of NaNs, each shader split its result into two unsigned 16-bit pieces and exported each as an exactly representable float. The readback also checked which pixel or DPP donor supplied each input. All modules passed Vulkan 1.2 SPIR-V validation. **For the rebase with #2362**, I tested its general neg/abs forwarding on top of this PR. Store the shuffled value and its two modifier flags in `args[first]` throughout: `first` is 0 for destination-less `v_cmpx` and 1 for the conversion. Keeping the older branch's literal `args[1]` produced 57 failing compare subcases locally. Using `args[first]` passed all 30 tests across `test_cvt_f16_denorm_mods`, `test_dpp_f16_mods`, and `test_dpp_compare`, and the combined translator passed the same 786,432-word GPU check. This was a local integration test of `cf622ce9c2` and `df294dd8a1`.
Author
Owner

@⁠Andretuta on GitHub · 2026-11-27 04:05 UTC · original

Thanks @⁠ecox982745 for the exhaustive RTX 4090 verification across all 65,536 binary16 encodings and 327,680 narrowing inputs (0 / 786,432 wrong words vs 75,766 on main), and for testing the integration with #2362!

Noted on args[first]: this branch already stores the shuffled operand and modifiers into args[first] (preserving first = 0 for destination-less v_cmpx). If #2362 lands first, I'll rebase keeping args[first] throughout so test_dpp_compare and test_dpp_f16_mods stay green; since #2419 merges cleanly standalone on main, I also updated the PR header note from Depends on: to Related: so it doesn't look blocked on #2362's rebase.

**@⁠Andretuta** on GitHub · 2026-11-27 04:05 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2419#issuecomment-7209515349) Thanks @⁠ecox982745 for the exhaustive RTX 4090 verification across all 65,536 binary16 encodings and 327,680 narrowing inputs (`0 / 786,432` wrong words vs `75,766` on `main`), and for testing the integration with #2362! Noted on `args[first]`: this branch already stores the shuffled operand and modifiers into `args[first]` (preserving `first = 0` for destination-less `v_cmpx`). If #2362 lands first, I'll rebase keeping `args[first]` throughout so `test_dpp_compare` and `test_dpp_f16_mods` stay green; since #2419 merges cleanly standalone on `main`, I also updated the PR header note from `Depends on:` to `Related:` so it doesn't look blocked on #2362's rebase.
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-27 05:47 UTC · original · approved these changes

Approving. Checked on df294dd8a merged onto current main (d1dff7949).

Translator semantic suite: 501 files, only main's known test_fspx_dot4_op_sel_disasm and test_int32_to_f64 failures. tests/test_translate.py: 721 passed, the one failure is main's ps_sdwa_f16_const.asm. GPU fixtures on llvmpipe under Wine: ps_outmods_f16, ps_fmac_f16_mods, ps_interp_mov_mods, ps_interp_f32_mods, the six med3 fixtures, cs_cvt_f32_sat, cs_cvt_pk_u8 and ps_cvt_off all pass. Against main's translator test_cvt_f16_denorm_mods fails 6 times.

Read the diff. clamp_f16_bits is right as an unsigned compare: everything above 0x7C00 (negatives, -0, positive NaNs) goes to +0, +inf caps to 0x3C00, positive subnormals survive, and it keeps clamp off the driver's float path, where an UnpackHalf2x16 could flush them. The DPP change is a real fix: before it, neg on a v_cvt_f32_f16_dpp source went through modify() on the 32-bit word, flipping bit 31 instead of the half's sign. On llvmpipe the fixture shows exactly that: against main, word 0 is 0x7FC4A000, with the PR it is 0xFFC4A000.

One suggestion: cs_w32_cvt_f16_mods is in WAVE32_LANE_DUMPS, so it skips on any device that can't give exactly 32 lanes (llvmpipe here: "can't run this shader at 32 lanes on request"). Its only cross-lane read is the quad_perm inside one quad, so it doesn't need 32. With that one line removed locally it runs at llvmpipe's 8 lanes and all eight words match CVT_F16_MODS_WORDS. Keeping it in WAVE32_DUMPS (wave32 translation) but out of the forced-size list would let it run on more devices. Not a blocker.

**@⁠tiguan26** on GitHub · 2026-11-27 05:47 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2419#pullrequestreview-6083995337) · **approved these changes** Approving. Checked on df294dd8a merged onto current main (d1dff7949). Translator semantic suite: 501 files, only main's known `test_fspx_dot4_op_sel_disasm` and `test_int32_to_f64` failures. `tests/test_translate.py`: 721 passed, the one failure is main's `ps_sdwa_f16_const.asm`. GPU fixtures on llvmpipe under Wine: `ps_outmods_f16`, `ps_fmac_f16_mods`, `ps_interp_mov_mods`, `ps_interp_f32_mods`, the six med3 fixtures, `cs_cvt_f32_sat`, `cs_cvt_pk_u8` and `ps_cvt_off` all pass. Against main's translator `test_cvt_f16_denorm_mods` fails 6 times. Read the diff. `clamp_f16_bits` is right as an unsigned compare: everything above 0x7C00 (negatives, -0, positive NaNs) goes to +0, +inf caps to 0x3C00, positive subnormals survive, and it keeps clamp off the driver's float path, where an UnpackHalf2x16 could flush them. The DPP change is a real fix: before it, `neg` on a `v_cvt_f32_f16_dpp` source went through `modify()` on the 32-bit word, flipping bit 31 instead of the half's sign. On llvmpipe the fixture shows exactly that: against main, word 0 is 0x7FC4A000, with the PR it is 0xFFC4A000. One suggestion: `cs_w32_cvt_f16_mods` is in `WAVE32_LANE_DUMPS`, so it skips on any device that can't give exactly 32 lanes (llvmpipe here: "can't run this shader at 32 lanes on request"). Its only cross-lane read is the quad_perm inside one quad, so it doesn't need 32. With that one line removed locally it runs at llvmpipe's 8 lanes and all eight words match `CVT_F16_MODS_WORDS`. Keeping it in `WAVE32_DUMPS` (wave32 translation) but out of the forced-size list would let it run on more devices. Not a blocker.
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/2419/head:github-bot/gh-2419'
git switch 'github-bot/gh-2419'

Merge

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