Skip to content

Shaders: lower v_ldexp_f32 (VOP3 0x362) - #1341

Open
AscilCH wants to merge 1 commit into
Marvisak:mainfrom
AscilCH:fix/lower-v-ldexp-f32
Open

AscilCH wants to merge 1 commit into
Marvisak:mainfrom
AscilCH:fix/lower-v-ldexp-f32

Conversation

@AscilCH

@AscilCH AscilCH commented Nov 24, 2026

Copy link
Copy Markdown

Gap

v_ldexp_f32 (VOP3 0x362, computing src0.f32 * 2^src1.i32) was not lowered in crates/victory-shader/src/emit/lower.rs, failing shader translation with:

lower: Unsupported { offset: 36, what: "v_ldexp_f32" }

Fix

  • Lowered v_ldexp_f32 in crates/victory-shader/src/emit/lower.rs using exact IEEE-754 exponent adjustment in pure 32-bit integer arithmetic (requiring no INT64 shader feature flag):
    • Clamps the signed exponent src1 to [-512, 512] so biased_exp + n never overflows i32 (including i32::MIN and i32::MAX) while still handling full-span shifts such as 2^-126 * 2^253 = 2^127 without intermediate overflow.
    • Overflows to signed infinity, underflows to signed zero (matching RDNA2/D3D12 f32 FTZ), preserves ±0.0 and ±inf regardless of exponent, and quiets sNaN to qNaN.
    • Applies VOP3 source abs/neg (via fsrc on src0) and output omod/clamp (via float()).
  • Added hardware + WARP + dxil.dll test emit::valu_tests::v_ldexp_f32_matches_rdna2 covering normal positive/negative scaling, full-span scaling, overflow to ±inf, underflow to ±0.0, i32::MIN/i32::MAX exponents, ±0.0/±inf/sNaN/qNaN, and VOP3 abs/neg/clamp/omod.

Verification

Tested on Windows 11, NVIDIA RTX 3050 6GB Laptop GPU, WARP, and Windows SDK dxil.dll:

  • rustfmt --edition 2024 --check on valu_tests.rs: clean
  • cargo xtask policy: 154 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test -p victory-shader --locked -- v_ldexp_f32_matches_rdna2 --nocapture --test-threads=1: passed (hardware + WARP + dxil.dll)
  • VICTORY_WARP=1 cargo test -p victory-shader --locked -- --test-threads=1: 121 passed, 0 failed, 1 ignored

Lower v_ldexp_f32 (src0.f32 * 2^src1.i32) in crates/victory-shader/src/emit/lower.rs
using exact IEEE-754 exponent adjustment in 32-bit integer arithmetic
(without requiring INT64):
- Clamps the signed exponent to [-512, 512] so biased_exp + n never
  overflows i32 (including i32::MIN and i32::MAX), handling full-range
  scaling like 2^-126 * 2^253 = 2^127 without intermediate overflow.
- Overflows to signed infinity, underflows to signed zero (matching
  RDNA2/D3D12 f32 FTZ), preserves +/-0.0 and +/-inf regardless of
  exponent, and quiets sNaN to qNaN.
- Supports VOP3 source abs/neg and output omod/clamp modifiers.
- Adds hardware + WARP + dxil.dll test emit::valu_tests::v_ldexp_f32_matches_rdna2.
@marquezinii

Copy link
Copy Markdown
Contributor

Ran on an RTX 5070 (desktop, driver 621.40, the only adapter), Windows 11 26300, Rust 1.98.1 at 59e48eb. cargo test -p victory-shader --lib -- --test-threads=1 gives 121 passed, 0 failed, 1 ignored. v_ldexp_f32_matches_rdna2 passes on the hardware adapter and WARP.

One question on the denormal cases. The lowering returns a signed zero when the result exponent is <= 0, and it treats a denormal input (exp == 0) as zero. The test pins this down: 1.0 * 2^-127 should give 0. That is what RDNA2 does with f32 denormals flushed (MODE.FP_DENORM). With f32 denormals enabled, the hardware returns 0x0040_0000, and a denormal input is scaled rather than zeroed. Is flushing the intended mode for the console's shaders? If so, a comment saying it is an FTZ choice would help, because the test name suggests it matches RDNA2 in every mode.

@shortselow-byte

Copy link
Copy Markdown

On the denormal question above: #157 also lowers v_ldexp_f32 and takes the other side. It multiplies by three in-range powers of two so a denormal result is kept (its note: "a result that turns denormal before the last factor is rounded twice where denormals are kept"), where this PR flushes it to signed zero. So the choice of f32 denormal mode is open in two PRs at once, and is worth settling once for both.

@catminou4 catminou4 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.

Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and only run on Windows; macOS covers lowering/structure/IR unit tests.

Lowers v_ldexp_f32 (VOP3 0x362) with RDNA2-matched cases (v_ldexp_f32_matches_rdna2).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/valu_tests.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump, and the shader tests on Ada

Result: no count change against main.

Offline. I merged head 59e48eb onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on main, 21,724 with this PR.

No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on main. The count shows that the change rejects nothing. It does not show whether the output is right.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 121, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

@andrewpozdnakov7-jpg

Copy link
Copy Markdown

I rebased the v_ldexp_f32 lowering and its existing readback test onto main at 940d32f. The conflict was in where the test was inserted. The lowering and test body are unchanged, and GitHub attributes the rebased commit to @AscilCH.

The same test stops at Unsupported on 940d32f and passes on the rebased branch. All 29 VALU tests passed on Windows software WARP with DXIL validation. A separate local readback probe matched v_mul_f32 by 0.5 on four inputs at the underflow boundary, including a preserved negative zero and the smallest positive normal. Strict shader Clippy and the asset policy passed.

Claim. AI-assisted with Codex. Synthetic shaders only.

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.

7 participants