shader: lower v_add_nc_i32/v_sub_nc_i32/v_add_nc_i16/v_sub_nc_i16 (saturating under clamp) #71
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/71"
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?
Imported from uiliannn/VItoriaBr. Originally posted by lcg007xd-c on 2026-11-25T21:37:40Z.
What is broken
The signed adds decode but do not translate:
v_add_nc_i32v_sub_nc_i32v_add_nc_i16v_sub_nc_i16A program using one fails with
v_add_nc_i32 is not supported(and likewise for the others). I found them by translating a one-instruction program for every VOP3 opcode the decoder names.Semantics
D.i = S0.i ± S1.iand says each "supports saturation (signed … integer domain)". That is the VOP3 clamp bit.clampforllvm.sadd.satandllvm.ssub.saton gfx10 (v_add_nc_i32 … clamp,v_sub_nc_i16 … clamp).The change (
emit/lower.rs)valu_op): add or subtract. With clamp, overflow is detected from signs: the result's sign differs froma's, andb's sign agrees witha's for an add or differs for a subtract. On overflow the result is the limit ona's side (0x80000000or0x7FFFFFFF). This needs no 64-bit integers. An output modifier is still refused.valu16_op, besidev_add_nc_u16): the sign-extended halves are added in i32, where they cannot overflow. With clamp the result is limited to [−32768, 32767]. Thenwrite_halfwrites the selected half and keeps the other.Tests
emit::lower::tests::signed_adds_translate_with_and_without_clamp(every host): each encoding decodes to its name, and a program that stores the result translates both clamped and unclamped. Without thelower.rschange it fails:v_add_nc_i32 clamp 0: GFX10 code at 0x24: v_add_nc_i32 is not supported.valu_tests::signed_adds_wrap_and_saturate_under_clamp_like_rdna2(Windows/GPU): three cases per opcode, each run with and without clamp. They cover overflow at both ends (wrapping vs saturating) and an ordinary negative result. The 16-bit inputs have junk in their high halves.lli. I also compared against Rust'ssaturating_add/saturating_subandclampon 56 edge pairs for each 32-bit form, and on a 16 × 22 grid of 16-bit pairs forv_sub_nc_i16clamp. 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.
🤖 Generated with Claude Code
Pull request closed