Shaders: 16-bit integer sources take abs and neg on bit 15 #3355
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3355
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3355"
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?
@Lyrnc on GitHub · 2026-11-26 20:18 UTC · original
Lyrnc:fix/cvt-f16-u16-abs-neg→main· +199 −4 · 2 files · 3 commits · openWhat was wrong
VOP3
absandnegon a 16-bit integer source act on its bit 15, the place of a sign bit:absclears it,negflips it, both set it. There is no integer negate or absolute value.mainhandled this three ways, all different from the hardware:v_cvt_f16_u16andv_cvt_f16_i16ignored the modifiers: the unmodified integer was converted, with no message.v_cmp_*_u16,v_cmp_*_i16) ignored them too.v_add_nc_u16,v_sub_nc_u16,v_mul_lo_u16, the 16-bit shifts, min and max) refused them ("modifiers on a 16-bit integer operation").Examples, v0 =
0x0BAD_F00Dbefore (the high half is kept):mainv_cvt_f16_u16_e64 v0, -v1, v1 =0x0000_00000x0BAD_7800(0x8000 = 32768.0)0x0BAD_0000v_cvt_f16_i16_e64 v0, |v1|, v1 =0x007F_FFFF0x0BAD_7800(0x7fff)0x0BAD_BC00(-1.0)v_cvt_f16_i16_e64 v0, -v1, v1 =0x0000_00010x0BAD_F800(0x8001 = -32767, rounded)0x0BAD_3C00(1.0)v_add_nc_u16_e64 v0, -v1, v2, v1 =0x0000_0000, v2 =0xB8D1_B7170x0BAD_3717(0x8000 + 0xb717)v_max_i16_e64 v0, -v1, v2, v1 =0x0000_0001, v2 =0x477F_EFFF0x0BAD_EFFF(max(-32767, -4097))v_cmp_lt_u16_e64 s20, -v1, v2, v1 =0x0000_0000, v2 =0x0000_000Av_cmp_lt_i16_e64 s20, |v1|, v2, v1 =0x0000_8000, v2 =0x0000_0000This settles the open question of #2374 (closed, "evidence needed"), which lowered
negon these operations as a two's-complement negation: on RDNA2 it is the bit-15 flip that a comment there predicted from the other measured integer forms (-v1 + v2of 0 and 10 is0x800a, of 0x8000 and 0 it is 0).What changed
Lower::int16_mods(inst, k, x)appliesabs/negof sourcekto bit 15 of a 16-bit integer source.v_cvt_f16_u16/v_cvt_f16_i16, the generic 16-bit integer operations and the u16/i16 compares use it, before any sign extension.clampandomodon the 16-bit integer operations, and forabs/negon the f16 source ofv_cvt_u16_f16/v_cvt_i16_f16(#2963 lowers those).How it was found and measured
A differential sweep of the translator against RDNA2 hardware, VOP3 conversions and 16-bit operations with every source modifier among the cases:
v_cvt_f16_u16,v_cvt_f16_i16withabs,neg, both, andneg+clamp: 640 of 640 rows each (512 of 512 in a first run).v_add_nc_u16,v_sub_nc_u16,v_mul_lo_u16,v_max_u16,v_min_u16,v_max_i16,v_lshlrev_b16withnegon either source,abson either source, and both on src1: 640 of 640 rows each, where a two's-complement model fails.v_cmp_lt_u16,v_cmp_ge_u16,v_cmp_eq_u16,v_cmp_lt_i16,v_cmp_gt_i16,v_cmp_ne_i16withneg,absand both on either source (36 forms): 640 of 640 rows each.-v1 + v2of 0 and 10 is0x800a, and of 0x8000 and 0 it is 0.semantics_tests::programandlower::lower, on the same Radeon and on an RTX 5080 (driver 610.88). Onmain, the 14 conversion forms and 36 of the 42 compare forms (all but the six without modifiers) differ from the hardware, and the 35 forms of the integer operations are refused. With this change all 85 match on both hosts, and none of the other ~3,600 cases of the sweep (VOP3 with modifiers, VOP3P, LDS, DPP16 and SDWA forms) changes on either host.This is PC RDNA2 silicon, not the console's GPU. No game data was used.
Tests
Three GPU tests in
valu_tests.rs, each on measured rows with the gfx1036 results as the expected values:v_cvt_f16_u16_and_i16_take_abs_and_neg_on_bit_15: both conversions with no modifier,neg,absand both, on 8 rows (low halves 0, 1, 0xffff, 0x8000, 0x8001, 0xfc00, 0x7fff, 0xff, junk in the high halves); the destination's high half is checked too.sixteen_bit_integer_operations_take_abs_and_neg_on_bit_15: 12 operation/modifier forms of 7 operations on 7 rows.sixteen_bit_integer_compares_take_abs_and_neg_on_bit_15:v_cmp_lt_u16,v_cmp_lt_i16andv_cmp_eq_u16with 6 modifier forms on 8 rows, throughv_cndmask_b32.Results:
main's lowering the three tests fail, on the Radeon iGPU and on the RTX 5080:hardware: v_cvt_f16_u16 (abs 0, neg 1) of 0x0000 = 0x0bad0000, want 0x0bad7800;build: Unsupported { offset: 40, what: "modifiers on a 16-bit integer operation" };hardware: v_cmp_lt_u16 (abs 0b00, neg 0b01) of 0x0000, 0x000a = 1, want 0.hardware_adapter, not part of this PR), and on WARP, whichrun_casesruns too.cargo test -p victory-shader --lib -- --test-threads=1(Windows 11, default adapter): 299 passed, 5 failed, 3 ignored.main63bcca4on this machine: 296 passed and the same 5 failed (rect::tests::rectangles_get_their_fourth_corner, twostride_tests,synthetic_tests::gathers_at_a_level_match_the_hardware,tests::elements_sharing_a_row_keep_their_order). Both runs skipmemory_tests::heap_direct_texture_reads_mark_their_heap_index, which ends the test process with an access violation onmainhere as well.cargo clippy -p victory-shader --lib --tests -- -D warnings: clean.cargo xtask policy: 191 files ok. The new code adds norustfmtdifferences (the two files have the samerustfmt --checkoutput as onmain).Related
v_cvt_u16_f16/v_cvt_i16_f16) changes the same refusal line the other way round: it lets the float-source conversions through and keeps refusing the integer ones. With both, onlyclampandomodstay refused (if v.clamp || v.omod != 0); whichever lands second resolves that one line.fix/vop3-promoted-16bit-opsel) editshalf_srcandwrite_half, not the lines here;lower.rsmerges cleanly. Both add tests at the end ofvalu_tests.rs; keep both.Not tested
neg/absbits; the comment on #2374 cites a gfx1036 measurement of the bit-15 rule for SDWAv_cvt_f16_u16/i16and SDWA u16/i16 compares (boykopovar/AnyPS5#19592), which I did not repeat.AI use: Claude Code (Claude Opus 5.5) wrote the hardware sweep, found this with it, wrote the change and the tests, and ran everything above.
🤖 Generated with Claude Code
The 16-bit integer operations (v_add_nc_u16, v_sub_nc_u16, v_mul_lo_u16, the 16-bit shifts and min/max) and the u16/i16 compares apply VOP3 abs and neg the same way as v_cvt_f16_u16/i16: to bit 15 of the source, which abs clears and neg flips. There is no integer negation: on RDNA2 v_add_nc_u16_e64 v0, -v1, v2 of 0 and 10 is 0x800a, not 10. The operations refused these modifiers ("modifiers on a 16-bit integer operation"); the compares ignored them. Both now go through int16_mods, which v_cvt_f16_u16/i16 use too. clamp and omod stay refused, and so do abs and neg on the f16 source of v_cvt_u16_f16 / v_cvt_i16_f16. Measured on a gfx1036 (RDNA2) iGPU: abs, neg and both, on each source of seven of these operations, give the bit 15 results on all 640 input rows. Co-Authored-By: Claude Opus 5.5 <[email protected]>@Dukestar69 on GitHub · 2026-11-26 20:30 UTC · original · approved these changes
LGTM — verified on RTX 5090 and
VICTORY_WARP=1(Windows / D3D12) againstorigin/main(63bcca4).abs(clear bit 15) andneg(toggle bit 15) on 16-bit integer sources acrossv_cvt_f16_{u16,i16}, 16-bit integer ALU ops, and 16-bit integer compares matches RDNA2 hardware behavior cleanly on both RTX 5090 and WARP.cargo test -p victory-shader --lib take_abs_and_neg_on_bit_15 -- --test-threads=1: 3 passed, 0 failed on RTX 5090 and 3 passed, 0 failed onVICTORY_WARP=1.cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@fuzeofficlal on GitHub · 2026-11-26 20:40 UTC · original · approved these changes
LGTM —
int16_modscleanly appliesabs(clear bit 15) beforeneg(flip bit 15) afterhalf_src's0xFFFFmask, without affecting float sources or leaking high-half bits.Verified
crates/victory-shader/src/emit/lower.rs(6837-6856,7077,7127-7155,8028): all 16-bit integer conversions (v_cvt_f16_{u16,i16}), 16-bit integer ops, andu16/i16compares mask to the selected 16 bits viahalf_srcfirst, applyint16_mods, and then sign- or zero-extend, while keepingclamp/omodandv_cvt_{u16,i16}_f16float-source modifiers refused.crates/victory-shader/src/emit/lower.rssize: 521,856 bytes (under the 524,288-byte limit).cargo clippy --locked -p victory-shader --all-targets -- -D warnings(default &--all-features): clean.rustfmt --edition 2024: clean on all PR-touched hunks in bothlower.rsandvalu_tests.rs.emit::valu_tests(RTX 5090 + WARP viarun_cases): 38 passed, 0 failed, including all three new tests (v_cvt_f16_u16_and_i16_take_abs_and_neg_on_bit_15,sixteen_bit_integer_operations_take_abs_and_neg_on_bit_15,sixteen_bit_integer_compares_take_abs_and_neg_on_bit_15).Minor non-blocking note
op_sel = 0), leavingop_sel = 1,v_cmpx_*_{u16,i16}, SDWA, and right shifts untested directly (andabson thev_lshlrev_b16shift count only reads bits3:0so a dropped modifier there wouldn't show up in that one row).@Jackster on GitHub · 2026-11-26 22:36 UTC · original
Windows 11,
63bcca4+ this PR, Radeon RX 7900 XTX (hardware backend):cargo test -p victory-shader --lib --features host sixteen_bit_: 3 passed (4.08 s) —v_cvt_f16_u16_and_i16_take_abs_and_neg_on_bit_15,sixteen_bit_integer_operations_take_abs_and_neg_on_bit_15,sixteen_bit_integer_compares_take_abs_and_neg_on_bit_15.cargo clippy -p victory-shader --all-targets -- -D warnings: clean.These go through
run_cases, so the results agree on both the hardware backend and WARP. Synthetic only. AI-assisted.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.