Shaders: lower v_cvt_norm_i16_f16 and v_cvt_norm_u16_f16 #376

Merged
LouisRiverstone merged 1 commit from upstream-pr/376 into main 2026-11-24 08:08:34 +00:00
LouisRiverstone commented 2026-11-24 02:43:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LouisRiverstone on 2026-11-24T02:43:58Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573432338 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/376). Originally posted by [LouisRiverstone](https://github.com/LouisRiverstone) on 2026-11-24T02:43:58Z. --- `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.
jeremygold02 commented 2026-11-24 06:55:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:10:57Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121782300 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/376#issuecomment-7121782300). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T04:10:57Z. --- 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 commented 2026-11-24 06:56:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T06:02:23Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123514901 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/376#issuecomment-7123514901). Originally posted by [Lyrnc](https://github.com/Lyrnc) on 2026-11-24T06:02:23Z. --- 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.
upstream-sync closed this pull request 2026-11-24 09:05:26 +00:00
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!376
No description provided.