Skip to content

Shaders: 16-bit integer sources take abs and neg on bit 15 - #3355

Open
Lyrnc wants to merge 3 commits into
Marvisak:mainfrom
Lyrnc:fix/cvt-f16-u16-abs-neg
Open

Lyrnc wants to merge 3 commits into
Marvisak:mainfrom
Lyrnc:fix/cvt-f16-u16-abs-neg

Conversation

@Lyrnc

@Lyrnc Lyrnc commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

What was wrong

VOP3 abs and neg on a 16-bit integer source act on its bit 15, the place of a sign bit: abs clears it, neg flips it, both set it. There is no integer negate or absolute value. main handled this three ways, all different from the hardware:

  1. v_cvt_f16_u16 and v_cvt_f16_i16 ignored the modifiers: the unmodified integer was converted, with no message.
  2. The u16/i16 compares (v_cmp_*_u16, v_cmp_*_i16) ignored them too.
  3. The 16-bit integer operations (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_F00D before (the high half is kept):

Instruction, inputs gfx1036 main
v_cvt_f16_u16_e64 v0, -v1, v1 = 0x0000_0000 0x0BAD_7800 (0x8000 = 32768.0) 0x0BAD_0000
v_cvt_f16_i16_e64 v0, |v1|, v1 = 0x007F_FFFF 0x0BAD_7800 (0x7fff) 0x0BAD_BC00 (-1.0)
v_cvt_f16_i16_e64 v0, -v1, v1 = 0x0000_0001 0x0BAD_F800 (0x8001 = -32767, rounded) 0x0BAD_3C00 (1.0)
v_add_nc_u16_e64 v0, -v1, v2, v1 = 0x0000_0000, v2 = 0xB8D1_B717 0x0BAD_3717 (0x8000 + 0xb717) refused
v_max_i16_e64 v0, -v1, v2, v1 = 0x0000_0001, v2 = 0x477F_EFFF 0x0BAD_EFFF (max(-32767, -4097)) refused
v_cmp_lt_u16_e64 s20, -v1, v2, v1 = 0x0000_0000, v2 = 0x0000_000A false (0x8000 < 10) true (0 < 10)
v_cmp_lt_i16_e64 s20, |v1|, v2, v1 = 0x0000_8000, v2 = 0x0000_0000 false (0 < 0) true (-32768 < 0)

This settles the open question of #2374 (closed, "evidence needed"), which lowered neg on 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 + v2 of 0 and 10 is 0x800a, of 0x8000 and 0 it is 0).

What changed

  • Lower::int16_mods(inst, k, x) applies abs / neg of source k to 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.
  • The refusal stays for clamp and omod on the 16-bit integer operations, and for abs / neg on the f16 source of v_cvt_u16_f16 / v_cvt_i16_f16 (Shaders: allow abs/neg source modifiers on v_cvt_{u16,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:

  • Hardware: the Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11. The raw VOP3 words run in a HIP kernel (assembled by the driver's amd_comgr), one input row per lane, in the translator's float model (IEEE off, f32 denormals flushed, f16 kept).
  • Inputs: 640 rows of f32/f16/integer edge values and random words.
  • Results, all bit for bit against the bit-15 model:
    • v_cvt_f16_u16, v_cvt_f16_i16 with abs, neg, both, and neg + 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_b16 with neg on either source, abs on 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_i16 with neg, abs and both on either source (36 forms): 640 of 640 rows each.
    • A second set of 8 rows chosen for the tests (0, 1, 0x7fff, 0x8000, 0xffff, 0x8001 against 10, 0, 5, 0x7fff...): 136 of 136 results. As predicted on Shaders: 16-bit integer operations take their neg modifiers #2374, -v1 + v2 of 0 and 10 is 0x800a, and of 0x8000 and 0 it is 0.
  • Translator: the same words through semantics_tests::program and lower::lower, on the same Radeon and on an RTX 5080 (driver 610.88). On main, 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, abs and 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_i16 and v_cmp_eq_u16 with 6 modifier forms on 8 rows, through v_cndmask_b32.

Results:

  • On 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.
  • With this change they pass on the RTX 5080 (driver 610.88) and on the Radeon iGPU (gfx1036, driver 32.0.21042.62; picked with a local adapter-name filter in hardware_adapter, not part of this PR), and on WARP, which run_cases runs too.
  • cargo test -p victory-shader --lib -- --test-threads=1 (Windows 11, default adapter): 299 passed, 5 failed, 3 ignored. main 63bcca4 on this machine: 296 passed and the same 5 failed (rect::tests::rectangles_get_their_fourth_corner, two stride_tests, synthetic_tests::gathers_at_a_level_match_the_hardware, tests::elements_sharing_a_row_keep_their_order). Both runs skip memory_tests::heap_direct_texture_reads_mark_their_heap_index, which ends the test process with an access violation on main here as well.
  • cargo clippy -p victory-shader --lib --tests -- -D warnings: clean. cargo xtask policy: 191 files ok. The new code adds no rustfmt differences (the two files have the same rustfmt --check output as on main).

Related

  • Shaders: allow abs/neg source modifiers on v_cvt_{u16,i16}_f16 #2963 (abs/neg on the f16 source of 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, only clamp and omod stay refused (if v.clamp || v.omod != 0); whichever lands second resolves that one line.
  • The op_sel fix for VOP1/VOP2 opcodes in VOP3 form (fix/vop3-promoted-16bit-opsel) edits half_src and write_half, not the lines here; lower.rs merges cleanly. Both add tests at the end of valu_tests.rs; keep both.

Not tested

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

Lyrnc and others added 3 commits November 26, 2026 21:05
In VOP3 form these two conversions apply the abs and neg source
modifiers to their 16-bit integer source as to a sign bit: abs clears
bit 15 and neg flips it, before the integer is converted. The lowering
ignored both, so v_cvt_f16_u16_e64 v0, -v1 of 0 gave 0 where RDNA2
gives 32768.0 (0x7800), and v_cvt_f16_i16_e64 v0, |v1| of -1 gave -1.0
where RDNA2 gives 32768.0 (0x7fff rounded).

Measured on a gfx1036 (RDNA2) iGPU: with abs, neg and both, on 512
input rows each, every result equals the plain conversion of the source
with bit 15 cleared, flipped or set; clamp is applied after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…it 15

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 <noreply@anthropic.com>
A GPU test for the compare half of the previous commit: v_cmp_lt_u16,
v_cmp_lt_i16 and v_cmp_eq_u16 in VOP3 form with neg, abs and both on
either source, on eight rows, with the results of a gfx1036 (RDNA2)
iGPU. On main the compares ignore the modifiers and the test fails:
v_cmp_lt_u16 with neg on src0, of 0 and 10, gives 1 there; the
hardware compares 0x8000 with 10 and gives 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — verified on RTX 5090 and VICTORY_WARP=1 (Windows / D3D12) against origin/main (63bcca4).

  • Applying abs (clear bit 15) and neg (toggle bit 15) on 16-bit integer sources across v_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 on VICTORY_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 fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — int16_mods cleanly applies abs (clear bit 15) before neg (flip bit 15) after half_src's 0xFFFF mask, 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, and u16/i16 compares mask to the selected 16 bits via half_src first, apply int16_mods, and then sign- or zero-extend, while keeping clamp/omod and v_cvt_{u16,i16}_f16 float-source modifiers refused.
  • crates/victory-shader/src/emit/lower.rs size: 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 both lower.rs and valu_tests.rs.
  • emit::valu_tests (RTX 5090 + WARP via run_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

  • Test coverage focuses on standard low-half VOP3 encodings (op_sel = 0), leaving op_sel = 1, v_cmpx_*_{u16,i16}, SDWA, and right shifts untested directly (and abs on the v_lshlrev_b16 shift count only reads bits 3:0 so a dropped modifier there wouldn't show up in that one row).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants