shader: lower v_cvt_pknorm_i16_f32 and v_cvt_pknorm_u16_f32 #98

Open
github-bot wants to merge 1 commit from github-bot/gh-98 into main AGit
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 22:37 UTC · original
lcg007xd-c:shader-pknorm-f32 → main · +162 −0 · 2 files · 1 commits · open

What is broken

v_cvt_pknorm_i16_f32 (VOP3 0x368) and v_cvt_pknorm_u16_f32 (VOP3 0x369) decode, but a program using either fails to translate with v_cvt_pknorm_i16_f32 is not supported (or the u16 name). Mesa's ACO selects exactly these for NIR's pack_snorm_2x16/pack_unorm_2x16 on f32 sources (aco_select_nir_alu.cpp). That is how a shader writes two SNORM16/UNORM16 values in one dword, for example a packed normal or UV.

Semantics

AMD's RDNA2 ISA (§12.12, opcodes 872 and 873) gives D.i16_lo = (snorm)S0.f32; D.i16_hi = (snorm)S1.f32, and the same with unorm.

The rounding is the one this translator already uses for v_cvt_norm_i16_f16/v_cvt_norm_u16_f16 (cvt_norm16):

  • clamp to [−1, 1] or [0, 1];
  • scale by 32767 or 65535;
  • round to nearest even once, from the exact product;
  • NaN gives 0.

Mesa's constant folding (_mesa_float_to_snorm) instead rounds the f32 product first, then rounds to an integer. The two differ only when x·32767 (or x·65535) lies within about 2⁻¹⁰ of a half-integer. I kept the existing f16 rule so the f16 and f32 forms agree. I could not settle which one RDNA2 hardware does without a card.

The change (emit/lower.rs)

  • New helper f32_to_norm16(bits, signed): the f32 version of cvt_norm16, worked in integers on the float's bits.
    • The significand (24 bits, with the implicit bit unless denormal) times the scale is formed in i64. It has up to 40 significant bits, which an f32 product would round.
    • It is shifted by the exponent and rounded to nearest even from the remainder.
    • |x| ≥ 1 saturates and NaN gives 0.
    • No float arithmetic is involved, so host denormal flushing and fast-math cannot change the result.
  • Instruction arm (in valu_op): each source keeps its own abs/neg (through fsrc). The two conversions are packed, src0 low.

Tests

  • emit::lower::tests::f32_norm_packs_translate (every host): both decode, and a program that stores the result translates. Without the lower.rs change it fails: v_cvt_pknorm_i16_f32 [d7680000, 00020501]: GFX10 code at 0x24: v_cvt_pknorm_i16_f32 is not supported.
  • valu_tests::v_cvt_pknorm_i16_and_u16_f32_pack_two_norm_conversions (Windows/GPU): 19 pairs, checked against an f64 reference of the rule above. They include ±0, ±1, out-of-range values, NaN, ±inf, exact ties for each scale, a value just below 1, and a denormal.
  • CPU run: I ran this on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I compared against the same f64 reference on 327 pairs: 300 scrambled values around [−2, 2], exact ties (k + ½)/scale for both scales, denormals, and a neg modifier. All match.

Run here (Linux, Rust 1.98.1):

  • cargo test -p victory-shader --lib --locked: 188 passed, 0 failed, 2 ignored.
  • Portable clippy and CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.

Not run here: the GPU case, since I have no Windows machine, and anything on RDNA2 hardware (the rounding question above).

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:37 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/98) `lcg007xd-c:shader-pknorm-f32` → `main` · +162 −0 · 2 files · 1 commits · open ## What is broken `v_cvt_pknorm_i16_f32` (VOP3 **0x368**) and `v_cvt_pknorm_u16_f32` (VOP3 **0x369**) decode, but a program using either fails to translate with `v_cvt_pknorm_i16_f32 is not supported` (or the `u16` name). Mesa's ACO selects exactly these for NIR's `pack_snorm_2x16`/`pack_unorm_2x16` on f32 sources (`aco_select_nir_alu.cpp`). That is how a shader writes two SNORM16/UNORM16 values in one dword, for example a packed normal or UV. ## Semantics AMD's RDNA2 ISA (§12.12, opcodes 872 and 873) gives `D.i16_lo = (snorm)S0.f32; D.i16_hi = (snorm)S1.f32`, and the same with `unorm`. The rounding is the one this translator already uses for `v_cvt_norm_i16_f16`/`v_cvt_norm_u16_f16` (`cvt_norm16`): - clamp to [−1, 1] or [0, 1]; - scale by 32767 or 65535; - **round to nearest even once, from the exact product**; - NaN gives 0. Mesa's constant folding (`_mesa_float_to_snorm`) instead rounds the f32 product first, then rounds to an integer. The two differ only when `x·32767` (or `x·65535`) lies within about 2⁻¹⁰ of a half-integer. I kept the existing f16 rule so the f16 and f32 forms agree. I could not settle which one RDNA2 hardware does without a card. ## The change (`emit/lower.rs`) - **New helper `f32_to_norm16(bits, signed)`:** the f32 version of `cvt_norm16`, worked in integers on the float's bits. - The significand (24 bits, with the implicit bit unless denormal) times the scale is formed in i64. It has up to 40 significant bits, which an f32 product would round. - It is shifted by the exponent and rounded to nearest even from the remainder. - `|x| ≥ 1` saturates and NaN gives 0. - No float arithmetic is involved, so host denormal flushing and fast-math cannot change the result. - **Instruction arm** (in `valu_op`): each source keeps its own `abs`/`neg` (through `fsrc`). The two conversions are packed, src0 low. ## Tests - **`emit::lower::tests::f32_norm_packs_translate`** (every host): both decode, and a program that **stores** the result translates. Without the `lower.rs` change it fails: `v_cvt_pknorm_i16_f32 [d7680000, 00020501]: GFX10 code at 0x24: v_cvt_pknorm_i16_f32 is not supported`. - **`valu_tests::v_cvt_pknorm_i16_and_u16_f32_pack_two_norm_conversions`** (Windows/GPU): 19 pairs, checked against an f64 reference of the rule above. They include ±0, ±1, out-of-range values, NaN, ±inf, exact ties for each scale, a value just below 1, and a denormal. - **CPU run:** I ran this on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I compared against the same f64 reference on 327 pairs: 300 scrambled values around [−2, 2], exact ties `(k + ½)/scale` for both scales, denormals, and a `neg` modifier. All match. **Run here** (Linux, Rust 1.98.1): - `cargo test -p victory-shader --lib --locked`: 188 passed, 0 failed, 2 ignored. - Portable clippy and `CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings`: clean. **Not run here:** the GPU case, since I have no Windows machine, and anything on RDNA2 hardware (the rounding question above). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
VOP3 0x368 and 0x369 decoded but failed to translate
("v_cvt_pknorm_i16_f32 is not supported"). Each packs two f32 values
converted to normalized 16-bit integers (src0 low, src1 high; AMD's
RDNA2 ISA), the f32 form of v_cvt_pknorm_*16_f16. Mesa's ACO selects
them for NIR's pack_snorm_2x16/pack_unorm_2x16 on f32 sources.

The conversion is the one v_cvt_norm_*16_f16 make here (clamped,
scaled by 32767 or 65535, rounded to nearest even once from the exact
product, NaN gives 0), worked in integers on the f32's bits: a 24-bit
significand times the scale has up to 40 significant bits, more than an
f32 product keeps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:43:55 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/98/head:github-bot/gh-98'
git switch 'github-bot/gh-98'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-98'
git switch 'github-bot/gh-98'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-98'
git switch 'github-bot/gh-98'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-98'
git switch 'main'
git merge --squash 'github-bot/gh-98'
git switch 'main'
git merge --ff-only 'github-bot/gh-98'
git switch 'main'
git merge 'github-bot/gh-98'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.