Shaders: lower v_ldexp_f16 (VOP2 0x3b and VOP3 0x13b) #1223

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

@⁠ESAT586 on GitHub · 2026-11-24 16:31 UTC · original
ESAT586:shader/ldexp-f16 → main · +109 −1 · 2 files · 1 commits · open

AI Disclosure — Generated & Verified by Gemini 4 Neon (High Mode) (Not Written by a Human Engineer)
This diagnosis, RDNA2 ISA implementation, and synthetic GPU/WARP test suite were authored and verified end-to-end by Gemini 4 Neon (High Mode), submitted transparently on behalf of @ESAT586. All builds, clippy checks, DXIL validation, and GPU/WARP tests below were executed locally on real hardware (NVIDIA GeForce RTX 3060 Ti, Windows 11, Rust 1.98.1). No game data was used.

What's wrong

v_ldexp_f16 (VOP2 0x3b / VOP3 0x13b) is decoded in gfx10/opcodes.rs but not lowered in crates/victory-shader/src/emit/lower.rs, causing shaders scaling half-precision values by powers of two (HLSL ldexp on min16float/float16_t) to fail lowering.

Fix

  • Dispatch v_ldexp_f16 in valu16 / valu16_op:
    • Reject float abs/neg modifiers on S1 (which is a signed 16-bit integer exponent), while honoring abs[0]/neg[0]/opsel[0] on S0 via half_fsrc and opsel[1] on S1 via half_src (sign-extended from i16 to i32).
    • Clamp e to [-64, 64] and construct the exact f32 power of two (e + 127) << 23 before multiplying in f32:
      • Because 2^e is always a finite positive normal f32, ±0 * 2^e stays ±0 (never 0 * inf = NaN) and ±inf * 2^e stays ±inf (never inf * 0 = NaN).
      • Every finite non-zero f16 lies in [2^-24, 65504], so e >= 64 always overflows f16 to ±inf (2^-24 * 2^64 = 2^40) and e <= -64 always underflows f16 to ±0 (65504 * 2^-64 < 2^-47).
      • Because DXIL legacyF32ToF16 rounds toward zero on host drivers (saturating finite overflows to 0x7BFF instead of ±inf), promote finite results with |r| >= 65520.0 (the binary16 round-to-nearest-even overflow threshold) to signed ±inf before float_result.
      • All in-range normal and subnormal f16 results are exact in f32 (zero mantissa rounding on power-of-2 scale) and convert via float_result (legacyF32ToF16 + write_half, preserving the untouched half and honoring VOP3 clamp and opsel[3]).
  • Add emit::valu_tests::v_ldexp_f16_matches_rdna2 in crates/victory-shader/src/emit/valu_tests.rs testing VOP2 normals, subnormals (0x0001 * 2^24, 1.0 * 2^-15), overflow/underflow (i16::MAX, i16::MIN), ±0 * 2^32767, ±inf * 2^-32768, NaN, and VOP3 abs[0] + opsel:[1,0,0,1] + clamp.

Verification (Real GPU + WARP + CI Gates)

Tested on Windows 11, NVIDIA GeForce RTX 3060 Ti (driver 32.0.16.1656), Rust 1.98.1:

  • cargo test -p victory-shader --lib v_ldexp_f16_matches_rdna2: ok (dxil.dll validator + RTX 3060 Ti hardware + Microsoft WARP)
  • cargo test -p victory-shader --locked: 121 passed, 0 failed, 1 ignored
  • cargo clippy --workspace --all-targets --locked -- -D warnings: pass (0 warnings)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: pass (0 warnings)
  • cargo xtask policy: asset policy: 154 files ok

Scope & Non-Overlap

  • Checked open PRs and issues before writing: #157 covers v_ldexp_f32; no open PR or issue covers v_ldexp_f16.
  • Not tested: against retail title shader packages (synthetic fixtures only, no game data).
**@⁠ESAT586** on GitHub · 2026-11-24 16:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/1223) `ESAT586:shader/ldexp-f16` → `main` · +109 −1 · 2 files · 1 commits · open > **AI Disclosure — Generated & Verified by Gemini 4 Neon (High Mode) (Not Written by a Human Engineer)** > This diagnosis, RDNA2 ISA implementation, and synthetic GPU/WARP test suite were authored and verified end-to-end by **Gemini 4 Neon (High Mode)**, submitted transparently on behalf of `@ESAT586`. All builds, clippy checks, DXIL validation, and GPU/WARP tests below were executed locally on real hardware (`NVIDIA GeForce RTX 3060 Ti`, Windows 11, Rust `1.98.1`). **No game data was used.** ## What's wrong `v_ldexp_f16` (VOP2 `0x3b` / VOP3 `0x13b`) is decoded in `gfx10/opcodes.rs` but not lowered in `crates/victory-shader/src/emit/lower.rs`, causing shaders scaling half-precision values by powers of two (HLSL `ldexp` on `min16float`/`float16_t`) to fail lowering. ## Fix - Dispatch `v_ldexp_f16` in `valu16` / `valu16_op`: - Reject float `abs`/`neg` modifiers on `S1` (which is a signed 16-bit integer exponent), while honoring `abs[0]`/`neg[0]`/`opsel[0]` on `S0` via `half_fsrc` and `opsel[1]` on `S1` via `half_src` (sign-extended from `i16` to `i32`). - Clamp `e` to `[-64, 64]` and construct the exact f32 power of two `(e + 127) << 23` before multiplying in f32: - Because `2^e` is always a finite positive normal f32, `±0 * 2^e` stays `±0` (never `0 * inf = NaN`) and `±inf * 2^e` stays `±inf` (never `inf * 0 = NaN`). - Every finite non-zero f16 lies in `[2^-24, 65504]`, so `e >= 64` always overflows f16 to `±inf` (`2^-24 * 2^64 = 2^40`) and `e <= -64` always underflows f16 to `±0` (`65504 * 2^-64 < 2^-47`). - Because DXIL `legacyF32ToF16` rounds toward zero on host drivers (saturating finite overflows to `0x7BFF` instead of `±inf`), promote finite results with `|r| >= 65520.0` (the binary16 round-to-nearest-even overflow threshold) to signed `±inf` before `float_result`. - All in-range normal and subnormal f16 results are exact in f32 (zero mantissa rounding on power-of-2 scale) and convert via `float_result` (`legacyF32ToF16` + `write_half`, preserving the untouched half and honoring VOP3 `clamp` and `opsel[3]`). - Add `emit::valu_tests::v_ldexp_f16_matches_rdna2` in `crates/victory-shader/src/emit/valu_tests.rs` testing VOP2 normals, subnormals (`0x0001 * 2^24`, `1.0 * 2^-15`), overflow/underflow (`i16::MAX`, `i16::MIN`), `±0 * 2^32767`, `±inf * 2^-32768`, `NaN`, and VOP3 `abs[0] + opsel:[1,0,0,1] + clamp`. ## Verification (Real GPU + WARP + CI Gates) Tested on Windows 11, **NVIDIA GeForce RTX 3060 Ti** (driver `32.0.16.1656`), Rust `1.98.1`: - `cargo test -p victory-shader --lib v_ldexp_f16_matches_rdna2`: **ok** (`dxil.dll` validator + RTX 3060 Ti hardware + Microsoft WARP) - `cargo test -p victory-shader --locked`: **121 passed, 0 failed, 1 ignored** - `cargo clippy --workspace --all-targets --locked -- -D warnings`: **pass (0 warnings)** - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: **pass (0 warnings)** - `cargo xtask policy`: **`asset policy: 154 files ok`** ## Scope & Non-Overlap - Checked open PRs and issues before writing: `#157` covers `v_ldexp_f32`; no open PR or issue covers `v_ldexp_f16`. - **Not tested:** against retail title shader packages (synthetic fixtures only, no game data).
AI-generated & verified with Gemini 4 Neon (High Mode) — not written by a human engineer.

Lower `v_ldexp_f16` (`D.f16 = S0.f16 * 2^(S1.i16)`) in `valu16` / `valu16_op`:
- Widens `S0` via `half_fsrc` (exact for all f16 normals and subnormals, honoring
  `opsel[0]`, `abs[0]`, `neg[0]`) and sign-extends `S1` from 16 bits via `half_src`.
- Clamps the exponent to `[-64, 64]` and constructs `2^e` directly as a normal
  finite f32 (`(e + 127) << 23`), so `±0 * finite = ±0` (never `0 * inf = NaN`),
  `±inf * finite = ±inf` (never `inf * 0 = NaN`), and extreme exponents
  (`i16::MIN` / `i16::MAX`) overflow/underflow f16 cleanly without intermediate
  f32 faults.
- Writes back via `float_result` (honoring `clamp`, `opsel[3]`, and preserving
  the untouched half of the destination VGPR).

Verified with synthetic GPU + WARP + dxil.dll tests in `valu_tests.rs` on
NVIDIA GeForce RTX 3060 Ti (Windows 11, Rust 1.98.1). No game data used.

Co-Authored-By: Gemini 4 Neon (High Mode) <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:24:14 +00:00
github-bot force-pushed github-bot/gh-1223 from 423310ed21 to 921092c908 2026-11-26 06:30:19 +00:00 Compare
Author
Owner

@⁠pablogonz12 on GitHub · 2026-11-24 17:03 UTC · original

Setup: Linux, RX 9070 XT, RADV (Mesa 26.3.1), dxil-spirv master, spirv-val 1.3. Your head folded onto a base of origin/main ffe310e plus #1017 — main's host build is broken without it (E0425 host_resource in victory-gpu), so the base is that pair. Ran the four-stage offline check (translate, dxil-spirv, spirv-val, RADV pipeline) over 768 programs: the 751-program world dump plus a 52-program BVH capture, 35 duplicate containers skipped.

translate valid SPIR-V RADV pipeline
base 666 / 768 656 656
your head 666 / 768 656 656

No program gained or lost and the failure table is identical to the base, reason for reason: nothing in this capture reaches the class you lower, so this is a no-regression result only (same 10 SPIR-V validation failures as the base).

Not tested: in-game.

**@⁠pablogonz12** on GitHub · 2026-11-24 17:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/1223#issuecomment-7138616984) Setup: Linux, RX 9070 XT, RADV (Mesa 26.3.1), dxil-spirv master, spirv-val 1.3. Your head folded onto a base of origin/main `ffe310e` plus #1017 — main's host build is broken without it (`E0425 host_resource` in `victory-gpu`), so the base is that pair. Ran the four-stage offline check (translate, dxil-spirv, spirv-val, RADV pipeline) over 768 programs: the 751-program world dump plus a 52-program BVH capture, 35 duplicate containers skipped. | | translate | valid SPIR-V | RADV pipeline | |---|---|---|---| | base | 666 / 768 | 656 | 656 | | your head | 666 / 768 | 656 | 656 | No program gained or lost and the failure table is identical to the base, reason for reason: nothing in this capture reaches the class you lower, so this is a no-regression result only (same 10 SPIR-V validation failures as the base). Not tested: in-game.
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 17:59 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at ba53233: cargo test -p victory-shader passes, 121 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 17:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/1223#issuecomment-7140006144) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at ba53233: `cargo test -p victory-shader` passes, 121 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:19 UTC · original · edited 2026-11-26 05:35 UTC

Automated triage for the maintainers.

Merge candidate. The rebase is clean against current main. The PR lowers v_ldexp_f16 in the half-precision VALU path of Lower (victory-shader/src/emit/lower.rs), which main decodes (gfx10/opcodes.rs, 0x03b) but does not translate: it sign-extends the 16-bit exponent, clamps it to [-64, 64] (wide enough for every finite f16 result to overflow or underflow correctly, while 02^e and inf2^e never hit 0*inf), multiplies in f32 by an exact power of two and narrows once through float_result, so rounding, denormal mode and clamp follow the shader's MODE. It rejects abs/neg on the integer exponent operand. v_ldexp_f16_matches_rdna2 covers normal, subnormal, overflow, underflow, signed zero, infinity, NaN, and the VOP3 op_sel/abs/clamp forms with the other half of the register left alone; the expected values follow the public RDNA2 ISA definition (S0.f16 * 2^S1.i16).

CI: passing. Recommending a look, not approving.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/1223#issuecomment-7149427989) · edited 2026-11-26 05:35 UTC <!-- victory-triage v1 head=921092c9088645d176c76ae0ccc0c0906d2ee28d verdict=merge-candidate at=2026-11-26T05:35:06Z --> Automated triage for the maintainers. **Merge candidate.** The rebase is clean against current `main`. The PR lowers `v_ldexp_f16` in the half-precision VALU path of `Lower` (`victory-shader/src/emit/lower.rs`), which `main` decodes (`gfx10/opcodes.rs`, 0x03b) but does not translate: it sign-extends the 16-bit exponent, clamps it to [-64, 64] (wide enough for every finite f16 result to overflow or underflow correctly, while 0*2^e and inf*2^e never hit 0*inf), multiplies in f32 by an exact power of two and narrows once through `float_result`, so rounding, denormal mode and clamp follow the shader's MODE. It rejects abs/neg on the integer exponent operand. `v_ldexp_f16_matches_rdna2` covers normal, subnormal, overflow, underflow, signed zero, infinity, NaN, and the VOP3 op_sel/abs/clamp forms with the other half of the register left alone; the expected values follow the public RDNA2 ISA definition (`S0.f16 * 2^S1.i16`). CI: passing. Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠ESAT586 on GitHub · 2026-11-25 11:21 UTC · original

Rebased onto \main\ (\940d32f) at $sha. Verified on RTX 3060 Ti + WARP: \ _ldexp_f16_matches_rdna2\ passed, strict Clippy (--all-targets\ and --all-features) clean, \cargo xtask policy\ ok.

**@⁠ESAT586** on GitHub · 2026-11-25 11:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/1223#issuecomment-7160932893) Rebased onto \main\ (\940d32f\) at \$sha\. Verified on RTX 3060 Ti + WARP: \ _ldexp_f16_matches_rdna2\ passed, strict Clippy (\--all-targets\ and \--all-features\) clean, \cargo xtask policy\ ok.
Author
Owner

@⁠ESAT586 on GitHub · 2026-11-25 12:57 UTC · original

Addressed triage review at $sha: dropped the extra |r| >= 65520 -> ±inf\ override and outdated \legacyF32ToF16\ comment so \ loat_result\ →
arrow16\ handles overflow/rounding according to the shader's \MODE\ (keeping all overflow test cases). Verified on RTX 3060 Ti + WARP: \ _ldexp_f16_matches_rdna2\ passed, strict Clippy (--all-targets\ and --all-features) clean, \cargo xtask policy\ ok.

**@⁠ESAT586** on GitHub · 2026-11-25 12:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/1223#issuecomment-7163098079) Addressed triage review at \$sha\: dropped the extra \|r| >= 65520 -> ±inf\ override and outdated \legacyF32ToF16\ comment so \ loat_result\ → \ arrow16\ handles overflow/rounding according to the shader's \MODE\ (keeping all overflow test cases). Verified on RTX 3060 Ti + WARP: \ _ldexp_f16_matches_rdna2\ passed, strict Clippy (\--all-targets\ and \--all-features\) clean, \cargo xtask policy\ ok.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 15:26 UTC · original · approved these changes

The clamped power-of-two f32 scaling (emit/lower.rs:7003-7025) is exact for all finite f16 inputs with zero double rounding (verified exhaustively across all 63,490 non-NaN half encodings × 136 exponents = 8,634,640 combinations against direct half round-to-nearest-even) and preserves ±0, ±Inf, and NaNs across extreme signed i16 exponents while deferring rounding/overflow to float_result -> narrow16.

Non-blocking follow-up notes:

  • In valu_tests.rs:2297-2304, the e64 fixture exercises op_sel on VOP3-promoted 0x13b (v_ldexp_f16); on RDNA2 (section 6.2 / Table 21), half selection/preservation on promoted VOP2 ops is encoded via SDWA (WORD_1 + dst_unused:PRESERVE), so an SDWA fixture alongside legal e64 abs/clamp (and noting omod stays safely refused in float_result) would tighten coverage.
  • rustfmt --edition 2024 --check flags new lines valu_tests.rs:2307, 2337, 2348.
  • emit/lower.rs after merging onto origin/main is 522,152 B (2,136 B under cap; note stacking with #2160 would exceed 524,288 B by 1,082 B).

Local verification at 921092c9 (and cleanly merged onto origin/main at d86cca0e): xtask policy (191 files ok), cargo clippy -p victory-shader --all-targets (default and --all-features, 0 warnings), census_tests (1/1), and emit::valu_tests on both RTX 5090 and WARP (36/36, including v_ldexp_f16_matches_rdna2) all passed. Approving.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 15:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1223#pullrequestreview-6076734619) · **approved these changes** The clamped power-of-two f32 scaling (`emit/lower.rs:7003-7025`) is exact for all finite f16 inputs with zero double rounding (verified exhaustively across all 63,490 non-NaN half encodings × 136 exponents = 8,634,640 combinations against direct half round-to-nearest-even) and preserves `±0`, `±Inf`, and NaNs across extreme signed `i16` exponents while deferring rounding/overflow to `float_result` -> `narrow16`. Non-blocking follow-up notes: - In `valu_tests.rs:2297-2304`, the e64 fixture exercises `op_sel` on VOP3-promoted `0x13b` (`v_ldexp_f16`); on RDNA2 (section 6.2 / Table 21), half selection/preservation on promoted VOP2 ops is encoded via SDWA (`WORD_1` + `dst_unused:PRESERVE`), so an SDWA fixture alongside legal e64 `abs`/`clamp` (and noting `omod` stays safely refused in `float_result`) would tighten coverage. - `rustfmt --edition 2024 --check` flags new lines `valu_tests.rs:2307, 2337, 2348`. - `emit/lower.rs` after merging onto `origin/main` is **`522,152 B`** (`2,136 B` under cap; note stacking with `#2160` would exceed `524,288 B` by `1,082 B`). Local verification at `921092c9` (and cleanly merged onto `origin/main` at `d86cca0e`): `xtask policy` (`191 files ok`), `cargo clippy -p victory-shader --all-targets` (`default` and `--all-features`, 0 warnings), `census_tests` (`1/1`), and `emit::valu_tests` on both RTX 5090 and WARP (`36/36`, including `v_ldexp_f16_matches_rdna2`) all passed. Approving.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 19:11 UTC · original · approved these changes

LGTM — verified on RTX 5090 and VICTORY_WARP=1 (Windows / D3D12) against origin/main (63bcca4).

  • Sign-extending the 16-bit integer exponent, clamping to [-64, 64] so 2^e is always a normal finite f32 (preserving ±0, ±inf, and NaN semantics), rejecting float modifiers on the integer exponent operand, and multiplying in f32 before float_result cleanly handles both VOP2 0x3b and VOP3 0x13b.
  • cargo test -p victory-shader --lib emit::valu_tests::v_ldexp_f16 -- --test-threads=1: passed on RTX 5090 and VICTORY_WARP=1.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 19:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/1223#pullrequestreview-6079361179) · **approved these changes** LGTM — verified on RTX 5090 and `VICTORY_WARP=1` (Windows / D3D12) against `origin/main` (`63bcca4`). - Sign-extending the 16-bit integer exponent, clamping to `[-64, 64]` so `2^e` is always a normal finite f32 (preserving `±0`, `±inf`, and NaN semantics), rejecting float modifiers on the integer exponent operand, and multiplying in f32 before `float_result` cleanly handles both VOP2 `0x3b` and VOP3 `0x13b`. - `cargo test -p victory-shader --lib emit::valu_tests::v_ldexp_f16 -- --test-threads=1`: passed on RTX 5090 and `VICTORY_WARP=1`. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request can be merged automatically.
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/1223/head:github-bot/gh-1223'
git switch 'github-bot/gh-1223'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1223'
git switch 'github-bot/gh-1223'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1223'
git switch 'github-bot/gh-1223'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1223'
git switch 'main'
git merge --squash 'github-bot/gh-1223'
git switch 'main'
git merge --ff-only 'github-bot/gh-1223'
git switch 'main'
git merge 'github-bot/gh-1223'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1223
No description provided.