Shaders: lower v_cvt_norm_i16_f16 and v_cvt_norm_u16_f16 #376

Manually merged
github merged 1 commit from gh-pr/376/head into gh-pr/376/base 2026-11-26 04:01:37 +00:00
Owner

@⁠LouisRiverstone on GitHub · 2026-11-24 02:43 UTC · original
LouisRiverstone:shader-cvt-norm-f16 → main · +127 −0 · 2 files · 1 commits · merged 2026-11-24 08:08 UTC

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.

**@⁠LouisRiverstone** on GitHub · 2026-11-24 02:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/376) `LouisRiverstone:shader-cvt-norm-f16` → `main` · +127 −0 · 2 files · 1 commits · merged 2026-11-24 08:08 UTC `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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:01:36 +00:00
github manually merged commit ded334bb91 into gh-pr/376/base 2026-11-26 04:01:37 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:10 UTC · original

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.

**@⁠jeremygold02** on GitHub · 2026-11-24 04:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/376#issuecomment-7121782300) 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.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 06:02 UTC · original

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

**@⁠Lyrnc** on GitHub · 2026-11-24 06:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/376#issuecomment-7123514901) 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 #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.
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!376
No description provided.