shader: lower v_mad_u32_u16, v_mad_i32_i16, v_mad_u16 and v_mad_i16 #73
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!73
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-73"
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 21:41 UTC · original
lcg007xd-c:shader-mad-16bit→main· +144 −0 · 2 files · 1 commits · openWhat is broken
The 16-bit multiply-adds decode but do not translate:
v_mad_u32_u16v_mad_i32_i16v_mad_u16v_mad_i16A program using one fails with
v_mad_u32_u16 is not supported(and likewise for the others). I found them by translating a one-instruction program for every VOP3 opcode the decoder names. The 24-bit forms (v_mad_u32_u24,v_mad_i32_i24) already lower.Semantics (AMD's RDNA2 ISA, §12.12, opcodes 832, 862, 883, 885)
v_mad_u32_u16:D.u32 = S0.u16 * S1.u16 + S2.u32.v_mad_i32_i16is the same with signed 16-bit factors and a 32-bit sum.op_selpicks which half of src0 and src1 is read.v_mad_u16:D.u16 = S0.u16 * S1.u16 + S2.u16, which "supports saturation (… 16-bit integer domain)".v_mad_i16is the same with signed values. The 16-bit result goes to the halfop_sel[3]selects, and the other half is kept.The change (
emit/lower.rs)v_mad_u32_u16/v_mad_i32_i16(ininteger, beside the 24-bit forms): the two halves come fromhalf_src, which honoursop_sel, and are sign-extended for the signed form. The sum wraps. Clamp is still refused here, as for the other 32-bit integer operations.v_mad_u16/v_mad_i16(invalu16_op): three halves, sign-extended for_i16. The exact sum fits in 32 bits: at most0xFFFE0001 + 0xFFFFunsigned, and within ±2³⁰ + 2¹⁵ signed. So clamp compares the exact value with the 16-bit limits. Thenwrite_halfwrites it.Tests
emit::lower::tests::sixteen_bit_multiply_adds_translate(every host): each encoding decodes to its name, and a program that stores the result translates. The 16-bit-result forms are also tried with clamp. Without thelower.rschange it fails:v_mad_u32_u16 clamp 0: GFX10 code at 0x24: v_mad_u32_u16 is not supported.valu_tests::sixteen_bit_multiply_adds_match_rdna2(Windows/GPU): two hand-computed cases per opcode, the 16-bit-result ones with and without clamp. They cover0xFFFF * 0xFFFF, a wrapping 32-bit sum, −1 × −32768, and saturation at both 16-bit ends. Every source has junk in the high half it must not read.lli. I added threeop_selcases: high halves of src0/src1 for both 32-bit forms, andv_mad_i16reading src2's high half and writing v0's high half with clamp. I also checked clampedv_mad_i16/v_mad_u16against a Rust reference on a 9 × 11 grid. 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. The test and the GPU case sit at different places in their files from those of #65 and #71, so the three merge in any order.
🤖 Generated with Claude Code
VOP3 0x373, 0x375, 0x340 and 0x35E decoded but failed to translate ("v_mad_u32_u16 is not supported"). As AMD's RDNA2 ISA defines them: v_mad_u32_u16/v_mad_i32_i16 multiply two 16-bit halves (op_sel picks them; sign-extended for the signed one) and add a 32-bit src2, wrapping. v_mad_u16/v_mad_i16 add a 16-bit src2 and write a 16-bit half; with clamp they saturate to the 16-bit range. The exact sums fit in 32 bits, so the clamp compares the exact value. 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.