Skip to content

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

Open
ESAT586 wants to merge 1 commit into
Marvisak:mainfrom
ESAT586:shader/ldexp-f16
Open

ESAT586 wants to merge 1 commit into
Marvisak:mainfrom
ESAT586:shader/ldexp-f16

Conversation

@ESAT586

@ESAT586 ESAT586 commented Nov 24, 2026

Copy link
Copy Markdown

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).

LAKSHYA-cmd-tech added a commit to LAKSHYA-cmd-tech/VIctory that referenced this pull request Nov 24, 2026
…dmask

Shaders: lower v_ldexp_f16 and resolve Structured::other in v_cndmask_b32 (Marvisak#1223, Marvisak#1221)
@pablogonz12

Copy link
Copy Markdown
Contributor

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.

@Zipettte

Copy link
Copy Markdown

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).

KainanP pushed a commit to KainanP/VIctory that referenced this pull request Nov 24, 2026
…fied together

Merges Marvisak#156 Marvisak#162 Marvisak#168 Marvisak#169 Marvisak#368 Marvisak#379 Marvisak#456 Marvisak#587 Marvisak#990 Marvisak#1021 Marvisak#1048 Marvisak#1052 Marvisak#1053
Marvisak#1095 Marvisak#1142 Marvisak#1190 Marvisak#1211 Marvisak#1223 Marvisak#1259 Marvisak#1293 on top of the bundle of Marvisak#1402
(which is on Marvisak#1319, on main ffe310e), each head unchanged. Hunks that
conflicted in emit/lower.rs, emit/valu_tests.rs and emit/diff_tests.rs were
merged by keeping both sides (additions next to each other); a PR was kept
only if cargo clippy -p victory-shader --all-targets -- -D warnings still
passed with it. No instruction name is handled in two places.

Marvisak#98 is not in this version: an offline measurement on a retail dump showed
it loses 31 programs that validate on Marvisak#1402.

cargo clippy --workspace --all-targets --locked -- -D warnings, with and
without --all-features, is clean and cargo test --workspace --locked passes
(381 passed, 6 ignored; main has 342 passed, 6 ignored).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@ESAT586

ESAT586 commented Nov 25, 2026

Copy link
Copy Markdown
Author

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

ESAT586 commented Nov 25, 2026

Copy link
Copy Markdown
Author

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.

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) <noreply@google.com>

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants