Skip to content

Shaders: lower v_cvt_norm_i16_f16 and v_cvt_norm_u16_f16 - #376

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
LouisRiverstone:shader-cvt-norm-f16
Nov 24, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
LouisRiverstone:shader-cvt-norm-f16

Conversation

@LouisRiverstone

Copy link
Copy Markdown
Contributor

v_cvt_norm_i16_f16 and v_cvt_norm_u16_f16 (VOP1 0x63/0x64) were not lowered, so any program using them failed to translate.

  • The f16 source, with its abs/neg modifiers, is clamped to [-1, 1] (i16) or [0, 1] (u16). It is multiplied by 32767 or 65535 and rounded to nearest even once, from the exact product. NaN gives 0. The result goes to the 16-bit destination like the other 16-bit ops (write_half).
  • The product is computed in integers from the f16's bits. In f32 it can have up to 26 significant bits, so it would round before the rounding to an integer. For example, a value just above k + 0.5 would come out as k for an even k.
  • Rounding and NaN follow the ISA's flt16_to_snorm16 / flt16_to_unorm16, the same rule as the f32 pknorm conversions. I haven't measured these two ops on console hardware.

Test: v_cvt_norm_i16_and_u16_f16_round_the_exact_product in valu_tests.rs runs both ops on the host GPU and WARP. It covers 287 f16 patterns: every 251st pattern, plus ±0, subnormals, ±0.5 (the tie), the values next to 1, ±1, ±inf and NaNs. Each result is checked against an f64 reference.

  • Windows 11, RTX 4080 and WARP: passes, and DXIL validation with dxil.dll 10.0.26100 passes.
  • Without the lower.rs change it fails with Unsupported { what: "v_cvt_norm_i16_f16" }.
  • cargo test --workspace, clippy and cargo xtask policy are clean.

No game files.

AI-assisted (Claude Code): I reviewed the change and ran the tests above.

The f16 source (with abs/neg) is clamped to [-1, 1] or [0, 1], scaled by
32767 or 65535 and rounded to nearest even once from the exact product;
NaN gives 0. The product is formed in integers from the f16's bits,
because in f32 it has up to 26 significant bits and would round first.
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremygold02

Copy link
Copy Markdown
Contributor

Thanks @LouisRiverstone for the exact-product conversion work. I tested head ded334bb9172865a9be7acdb8a2047358a30e4db with:

cargo test --locked -p victory-shader --lib v_cvt_norm_i16_and_u16_f16_round_the_exact_product -- --test-threads=1

Result: 1 passed, 0 failed (69 filtered out). This exercises both signed and unsigned normalized f16 conversions against the test’s f64 reference over the synthetic edge-pattern set. It does not measure behavior on console hardware.

@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

RDNA2 hardware measurement over all 65,536 f16 patterns: the exact-product, round-to-nearest-even rule matches every one, in the FP16 mode that keeps input denormals.

AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs v_cvt_norm_i16_f16 and v_cvt_norm_u16_f16 on every half (junk in the source's high half, 0xdead0000 preset in the destination), under FP16 denormals kept and flushed. RDNA2 desktop silicon, not the console's. No game data.

FP16 denormal mode v_cvt_norm_i16_f16 v_cvt_norm_u16_f16
kept (denorm16 = 3) 65536/65536 65536/65536
flushed (denorm16 = 0) 64002/65536 64641/65536
  • The model: clamp to [-1, 1] / [0, 1], multiply by 32767 / 65535 exactly, round to nearest even once; NaN gives 0. That is your lowering's rule.
  • The differences under flush are the subnormal inputs (from 0x0101 for i16 and 0x0081 for u16 up), which flush to 0 before scaling. Your lowering matches the denormals-kept mode, the same choice as the pknorm forms in Shaders: lower v_cvt_pknorm_{i16,u16}_{f32,f16} with exact rounding #596.
  • The destination's high half stays 0xdead on all patterns, consistent with write_half.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

@Marvisak
Marvisak merged commit ebf3615 into Marvisak:main Nov 24, 2026
suirentakamura referenced this pull request in suirentakamura/victory-suiren Nov 24, 2026
Shaders: lower v_cvt_norm_i16_f16 and v_cvt_norm_u16_f16
itsonlysimbo pushed a commit to itsonlysimbo/VIctory that referenced this pull request Nov 24, 2026
Shaders: lower v_cvt_norm_i16_f16 and v_cvt_norm_u16_f16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants