Skip to content

Shader tests: f16 conversion from u16, i16 and f32, and its rounding (#866) - #1

Open
P2xe1 wants to merge 1 commit into
mainfrom
shader/v-cvt-f16-from-u16-i16-test
Open

P2xe1 wants to merge 1 commit into
mainfrom
shader/v-cvt-f16-from-u16-i16-test

Conversation

@P2xe1

@P2xe1 P2xe1 commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Both opcodes now have a hardware test, plus the f32 → f16 step they share, and one correction to the table the issue proposes: on main the conversion truncates — and, truncating, it saturates instead of overflowing.

Branch: P2xe1/VIctory@shader/v-cvt-f16-from-u16-i16-test, commit b81ac80 on main ffe310e. Review PR against the fork's own main, since upstream CreatePullRequest still answers 404: #1

Tests (crates/victory-shader/src/emit/valu_tests.rs, +139 lines, no production code touched):

  • emit::valu_tests::v_cvt_f16_u16_and_i16_convert_the_low_half — 27 sources: everywhere below 0x8000, where the two forms must agree, and 0x8000, 0x8001, 0xC000, 0xFFEF, 0xFFF0, 0xFFF1, 0xFFFE, 0xFFFF, where they part ways. Each conversion runs twice, into a destination whose high half is zero and into one seeded with 0xDEAD0000, so the half a 16-bit write leaves alone is covered too.
  • emit::valu_tests::v_cvt_f16_f32_truncates_rather_than_rounding — the step every f16 result goes through, on the ties around 1.0 and at the top of the range.

The expected bits

source table above expects main gives
0x0803 (2051) 0x6802 (2052) 0x6801 (2050)
0x7FFF (32767) 0x7800 (32768) 0x77FF (32752)
0xFFF0 (65520) 0x7C00 (+inf) 0x7BFF (65504)

And the step is not only truncating, it never overflows: 65520, 65536 and 1e30 (0x7149F2CA) all give 0x7BFF (65504). v_cvt_f16_f32 (VOP1 0x00A) of 1 + 3·2⁻¹¹ (0x3F803000) gives 0x3C01, where nearest even would carry to 0x3C02.

So legacyF32ToF16 rounds toward zero and saturates, on both host backends, and every f16 result in the translator inherits that. This matches @fuzeofficlal's "the tie at 2,051 also goes from 2,050 (0x6801) on main to 2,052 (0x6802) with Marvisak#422": the tests record main, and Marvisak#422's merge has to update those rows. Whether RDNA2 truncates or rounds to nearest even is the open question — if it rounds, this is a defect of its own, wider than Marvisak#422 (which would leave the saturation as it is), and worth reporting separately.

@MichaelSmithAI's corrections to the encodings in the issue hold — 1 → 0x3C00, 15 → 0x4B80, 1,024 → 0x6400, −16 → 0xCC00, 2,049 → 2,048 — except the 2,051 one, which main rounds down rather than up, and the two unsigned rows at 65,520 and 65,535, which stay finite here.

Evidence

  • AMD RX 7600M XT (RDNA3, driver 32.0.31007.5012), Windows 11 10.0.28020, Rust 1.98.1 MSVC. The reports above were NVIDIA plus WARP; this is an AMD data point.
  • Hardware and WARP agree on every row of all three tables.
  • Mutation checks: Bin::AShr → Bin::LShr in the i16 extension fails at 0x8000 (0x7800, want 0xF800); adding that extension to the u16 form fails at 0x8000 too (0xF800, want 0x7800). Reverting both passes.
cargo test -p victory-shader --lib --locked                          122 passed, 0 failed, 1 ignored
cargo clippy -p victory-shader --all-targets --locked -- -D warnings  clean
rustfmt --edition 2024 --check                                        clean
cargo xtask policy                                                    154 files ok

AI-assisted; synthetic tests only, no game data.


Opened here against this fork's main ( fe310e) for review and cherry-pick; upstream PR creation is restricted to collaborators.

Two things about the s -> f16 step had no test: the VOP1 forms that convert
an integer (Marvisak#866), and the step itself (`legacyF32ToF16`), which every f16
result in the translator goes through.

`v_cvt_f16_u16` (0x050) and `v_cvt_f16_i16` (0x051) read the low 16 bits of
the source; the i16 form shifts left then arithmetically right by 16 first,
a path shared with no other tested instruction. The table holds 27 sources:
everywhere below 0x8000, where the two forms must agree, and 0x8000,
0x8001, 0xC000, 0xFFEF, 0xFFF0, 0xFFF1, 0xFFFE and 0xFFFF, where they part
ways. Each conversion runs twice, once into a destination whose high half is
zero and once into one seeded with 0xDEAD0000, so the half a 16-bit write
leaves alone is checked as well.

The expected bits are the truncating ones, not the nearest-even ones Marvisak#866
lists. The step rounds toward zero: 2051 gives 2050, 32767 gives 32752, and
65520, 65536 and 1e30 all give 65504, so no conversion produces an f16
infinity. Measured on an AMD RX 7600M XT (RDNA3, driver 32.0.31007.5012) and
on WARP, which agree on every row, and consistent with the NVIDIA figures
quoted in Marvisak#866 and with the "0x6801 on main" in Marvisak#422's note. Marvisak#422 moves the
ties to nearest even, so its merge has to update these rows.

Test-only: the lowering is unchanged. Flipping the i16 extension from an
arithmetic to a logical shift fails the test at 0x8000, and so does adding an
extension to the u16 form.

122 victory-shader tests pass, the one benchmark ignored; clippy with warnings
denied, rustfmt and `cargo xtask policy` (154 files) are clean. AI-assisted;
synthetic tests only, no game data.
@P2xe1
P2xe1 force-pushed the shader/v-cvt-f16-from-u16-i16-test branch from da4dd99 to b81ac80 Compare November 24, 2026 13:46
@P2xe1 P2xe1 changed the title Shader tests: v_cvt_f16_u16 and v_cvt_f16_i16 convert the low half (#866) Shader tests: f16 conversion from u16, i16 and f32, and its rounding (#866) Nov 24, 2026
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.

1 participant