shader: lower v_cvt_pknorm_i16_f32 and v_cvt_pknorm_u16_f32 #98
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/uiliannn--VItoriaBr!98
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-98"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@lcg007xd-c on GitHub · 2026-11-25 22:37 UTC · original
lcg007xd-c:shader-pknorm-f32→main· +162 −0 · 2 files · 1 commits · openWhat is broken
v_cvt_pknorm_i16_f32(VOP3 0x368) andv_cvt_pknorm_u16_f32(VOP3 0x369) decode, but a program using either fails to translate withv_cvt_pknorm_i16_f32 is not supported(or theu16name). Mesa's ACO selects exactly these for NIR'spack_snorm_2x16/pack_unorm_2x16on 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 withunorm.The rounding is the one this translator already uses for
v_cvt_norm_i16_f16/v_cvt_norm_u16_f16(cvt_norm16):Mesa's constant folding (
_mesa_float_to_snorm) instead rounds the f32 product first, then rounds to an integer. The two differ only whenx·32767(orx·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)f32_to_norm16(bits, signed): the f32 version ofcvt_norm16, worked in integers on the float's bits.|x| ≥ 1saturates and NaN gives 0.valu_op): each source keeps its ownabs/neg(throughfsrc). 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 thelower.rschange 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.lli. I compared against the same f64 reference on 327 pairs: 300 scrambled values around [−2, 2], exact ties(k + ½)/scalefor both scales, denormals, and anegmodifier. All match.Run here (Linux, Rust 1.98.1):
cargo test -p victory-shader --lib --locked: 188 passed, 0 failed, 2 ignored.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
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]>View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.