Shaders: v_cvt_f16_u16 (0x050) and v_cvt_f16_i16 (0x051) have no hardware test #866

Closed
opened 2026-11-26 04:14:05 +00:00 by github-bot · 3 comments
Owner

@⁠kss306 on GitHub · 2026-11-24 12:58 UTC · original

v_cvt_f16_u16 (VOP1 0x050) and v_cvt_f16_i16 (VOP1 0x051) are implemented in crates/victory-shader/src/emit/lower.rs but have no hardware test in crates/victory-shader/src/emit/valu_tests.rs. Their reverse-direction siblings v_cvt_u16_f16 (0x052) and v_cvt_i16_f16 (0x053) are exhaustively tested in v_cvt_u16_and_i16_f16_saturate with 16 cases.

Implementation evidence

lower.rs, valu16_op(), around line 4388:

"v_cvt_f16_u16" | "v_cvt_f16_i16" => {
    let mut x = self.half_src(inst, 0)?;
    let op = if name == "v_cvt_f16_i16" {
        let k16 = self.s.m.i32(16);
        let t = self.s.m.bin(c, Bin::Shl, x, k16);
        x = self.s.m.bin(c, Bin::AShr, t, k16); // sign-extend i16 → i32
        Cast::SiToFp
    } else {
        Cast::UiToFp  // zero-extend u16 → u32
    };
    self.s.m.cast(c, op, x, f32t)
}

The Shl 16 + AShr 16 sign-extension for v_cvt_f16_i16 is unique to this instruction path.

Missing test

valu_tests.rs contains fn v_cvt_u16_and_i16_f16_saturate() (line 674) using opcodes 0x052 / 0x053. Opcodes 0x050 and 0x051 do not appear anywhere in the test file.

The sibling test structure with vop1(0x001, 0, 128) to clear the high half applies directly.

Interesting edge cases

Input v_cvt_f16_u16 v_cvt_f16_i16 Reason
0x0000 0x0000 0x0000 zero
0x0001 0x0400 (subnormal) 0x0400 1 → 2^-14 × 1
0x7FFF 0x7800 0x7800 32767 rounds to 32768.0
0x8000 0x7800 0xF800 u16: 32768.0 / i16: -32768.0
0xFFFF 0x7C00 (inf) 0xBC00 u16: 65535 > max f16 / i16: -1

The case v_cvt_f16_u16(0xFFFF) is the one u16 value where the rounding mode changes the output: before PR #422 it rounds toward zero to 65504.0 (0x7BFF); after PR #422 it rounds to nearest even to infinity (0x7C00). The sign-extension case v_cvt_f16_i16(0x8000) = convert -32768 is exactly representable and not rounding-sensitive.

Suggested test

Add fn v_cvt_f16_u16_and_i16_match_rdna2() to valu_tests.rs mirroring the existing sibling test:

#[test]
fn v_cvt_f16_u16_and_i16_match_rdna2() {
    // v_mov_b32 v0, 0; v_cvt_f16_u16 / v_cvt_f16_i16 v0, v1 (VOP1 0x050 / 0x051).
    // Clear the high half first (same preamble as v_cvt_u16_and_i16_f16_saturate).

    // (u16/i16 bit input, expected f16 from u16, expected f16 from i16)
    let cases: &[(u16, u16, u16)] = &[
        (0x0000, 0x0000, 0x0000), // 0 → 0.0
        (0x0001, 0x0400, 0x0400), // 1 → 2^-14 (smallest f16 normal)
        (0x000F, 0x0C00, 0x0C00), // 15
        (0x0400, 0x5400, 0x5400), // 1024
        // The u16/i16 split at 0x8000:
        (0x8000, 0x7800, 0xF800), // u16=32768.0 / i16=-32768.0 (exactly representable)
        (0x7FFF, 0x7800, 0x7800), // 32767: rounds to 32768.0 for both (nearest even)
        // Negative i16 cases (sign-extension path):
        (0xFFFF, 0x7C00, 0xBC00), // u16=65535->inf (after #422); i16=-1->-1.0
        (0xFFF0, 0x7BFF, 0xC800), // u16=65520->65504.0 (round down); i16=-16->-16.0
        (0x8001, 0x7800, 0xF800), // u16=32769 / i16=-32767 → -32768.0 (nearest even)
    ];
    for (op, what, pick) in [
        (0x050u16, "v_cvt_f16_u16", 0usize),
        (0x051,    "v_cvt_f16_i16", 1),
    ] {
        let shader = build_valu_cs(&[vop1(0x001, 0, 128), vop1(op, 0, vgpr(1))]);
        let inputs: Vec<[u32; 4]> = cases.iter().map(|c| [u32::from(c.0), 0, 0, 0]).collect();
        run_cases(&shader, &inputs, |backend, i, _, got| {
            let want = u32::from(if pick == 0 { cases[i].1 } else { cases[i].2 });
            assert_eq!(
                got & 0xFFFF, want,
                "{backend}: {what}({:#06x}) = {got:#010x}, want {want:#06x}",
                cases[i].0,
            );
        });
    }
}

Note: the expected value for 0xFFFF → v_cvt_f16_u16 is 0x7C00 (infinity, nearest-even rounding per PR #422). If the test is written before #422 merges, change that case to 0x7BFF and update after the PR lands.

The test skips gracefully when no SM 6.6 device is available and runs on WARP. No game data needed.

AI-assisted: found with Kiro (Claude) and independently confirmed by five separate analysis passes without cross-contamination; no hardware run, no game data.

**@⁠kss306** on GitHub · 2026-11-24 12:58 UTC · [original](https://github.com/Marvisak/VIctory/issues/866) `v_cvt_f16_u16` (VOP1 0x050) and `v_cvt_f16_i16` (VOP1 0x051) are implemented in `crates/victory-shader/src/emit/lower.rs` but have no hardware test in `crates/victory-shader/src/emit/valu_tests.rs`. Their reverse-direction siblings `v_cvt_u16_f16` (0x052) and `v_cvt_i16_f16` (0x053) are exhaustively tested in `v_cvt_u16_and_i16_f16_saturate` with 16 cases. ## Implementation evidence `lower.rs`, `valu16_op()`, around line 4388: ```rust "v_cvt_f16_u16" | "v_cvt_f16_i16" => { let mut x = self.half_src(inst, 0)?; let op = if name == "v_cvt_f16_i16" { let k16 = self.s.m.i32(16); let t = self.s.m.bin(c, Bin::Shl, x, k16); x = self.s.m.bin(c, Bin::AShr, t, k16); // sign-extend i16 → i32 Cast::SiToFp } else { Cast::UiToFp // zero-extend u16 → u32 }; self.s.m.cast(c, op, x, f32t) } ``` The `Shl 16` + `AShr 16` sign-extension for `v_cvt_f16_i16` is unique to this instruction path. ## Missing test `valu_tests.rs` contains `fn v_cvt_u16_and_i16_f16_saturate()` (line 674) using opcodes `0x052` / `0x053`. Opcodes `0x050` and `0x051` do not appear anywhere in the test file. The sibling test structure with `vop1(0x001, 0, 128)` to clear the high half applies directly. ## Interesting edge cases | Input | v_cvt_f16_u16 | v_cvt_f16_i16 | Reason | |---|---|---|---| | `0x0000` | `0x0000` | `0x0000` | zero | | `0x0001` | `0x0400` (subnormal) | `0x0400` | 1 → 2^-14 × 1 | | `0x7FFF` | `0x7800` | `0x7800` | 32767 rounds to 32768.0 | | `0x8000` | `0x7800` | `0xF800` | u16: 32768.0 / i16: -32768.0 | | `0xFFFF` | `0x7C00` (inf) | `0xBC00` | u16: 65535 > max f16 / i16: -1 | The case `v_cvt_f16_u16(0xFFFF)` is the one u16 value where the rounding mode changes the output: before PR #422 it rounds toward zero to 65504.0 (`0x7BFF`); after PR #422 it rounds to nearest even to infinity (`0x7C00`). The sign-extension case `v_cvt_f16_i16(0x8000)` = convert -32768 is exactly representable and not rounding-sensitive. ## Suggested test Add `fn v_cvt_f16_u16_and_i16_match_rdna2()` to `valu_tests.rs` mirroring the existing sibling test: ```rust #[test] fn v_cvt_f16_u16_and_i16_match_rdna2() { // v_mov_b32 v0, 0; v_cvt_f16_u16 / v_cvt_f16_i16 v0, v1 (VOP1 0x050 / 0x051). // Clear the high half first (same preamble as v_cvt_u16_and_i16_f16_saturate). // (u16/i16 bit input, expected f16 from u16, expected f16 from i16) let cases: &[(u16, u16, u16)] = &[ (0x0000, 0x0000, 0x0000), // 0 → 0.0 (0x0001, 0x0400, 0x0400), // 1 → 2^-14 (smallest f16 normal) (0x000F, 0x0C00, 0x0C00), // 15 (0x0400, 0x5400, 0x5400), // 1024 // The u16/i16 split at 0x8000: (0x8000, 0x7800, 0xF800), // u16=32768.0 / i16=-32768.0 (exactly representable) (0x7FFF, 0x7800, 0x7800), // 32767: rounds to 32768.0 for both (nearest even) // Negative i16 cases (sign-extension path): (0xFFFF, 0x7C00, 0xBC00), // u16=65535->inf (after #422); i16=-1->-1.0 (0xFFF0, 0x7BFF, 0xC800), // u16=65520->65504.0 (round down); i16=-16->-16.0 (0x8001, 0x7800, 0xF800), // u16=32769 / i16=-32767 → -32768.0 (nearest even) ]; for (op, what, pick) in [ (0x050u16, "v_cvt_f16_u16", 0usize), (0x051, "v_cvt_f16_i16", 1), ] { let shader = build_valu_cs(&[vop1(0x001, 0, 128), vop1(op, 0, vgpr(1))]); let inputs: Vec<[u32; 4]> = cases.iter().map(|c| [u32::from(c.0), 0, 0, 0]).collect(); run_cases(&shader, &inputs, |backend, i, _, got| { let want = u32::from(if pick == 0 { cases[i].1 } else { cases[i].2 }); assert_eq!( got & 0xFFFF, want, "{backend}: {what}({:#06x}) = {got:#010x}, want {want:#06x}", cases[i].0, ); }); } } ``` Note: the expected value for `0xFFFF → v_cvt_f16_u16` is `0x7C00` (infinity, nearest-even rounding per PR #422). If the test is written before #422 merges, change that case to `0x7BFF` and update after the PR lands. The test skips gracefully when no SM 6.6 device is available and runs on WARP. No game data needed. **AI-assisted:** found with Kiro (Claude) and independently confirmed by five separate analysis passes without cross-contamination; no hardware run, no game data.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:05 +00:00
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 13:05 UTC · original

Melissa ran both opcodes with 12 input words each. A few expected binary16 encodings in the proposed table need updating: 1 → 0x3c00, 15 → 0x4b80, 1,024 → 0x6400, and signed −16 → 0xcc00. Unsigned 65,519 rounds to the largest finite half (0x7bff); 65,520 is the halfway point and rounds to infinity (0x7c00) under nearest-even. The test also checks 2,049 rounding down to 2,048 and 2,051 rounding up to 2,052.

All 24 conversions passed with the destination’s upper half preserved on both ffe310e and #422 a746d53 on RTX 4090 under Wine/vkd3d-proton 3.2. In particular, 65,535 already returned infinity on ffe310e here. Wine's WARP selection resolved to the same adapter. Native Windows and RDNA2 are still open. Test PR on the #422 branch

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 13:05 UTC · [original](https://github.com/Marvisak/VIctory/issues/866#issuecomment-7132327622) Melissa ran both opcodes with 12 input words each. A few expected binary16 encodings in the proposed table need updating: 1 → `0x3c00`, 15 → `0x4b80`, 1,024 → `0x6400`, and signed −16 → `0xcc00`. Unsigned 65,519 rounds to the largest finite half (`0x7bff`); 65,520 is the halfway point and rounds to infinity (`0x7c00`) under nearest-even. The test also checks 2,049 rounding down to 2,048 and 2,051 rounding up to 2,052. All 24 conversions passed with the destination’s upper half preserved on both ffe310e and #422 a746d53 on RTX 4090 under Wine/vkd3d-proton 3.2. In particular, 65,535 already returned infinity on ffe310e here. Wine's WARP selection resolved to the same adapter. Native Windows and RDNA2 are still open. [Test PR on the #422 branch](https://github.com/LouisRiverstone/VIctory/pull/1)
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-24 13:10 UTC · original

I ran the same public test on native Windows with an RTX 5090 and software WARP (Microsoft Basic Render Driver, checked by adapter LUID).

  • ffe310e with only the test transplanted: 17/24 on each backend.
  • 015a089 (#422 a746d53 plus the test): 24/24 on each backend.

Both versions' two shaders passed DXIL 1.8.2502.11 validation. On main, the compute readback gives unsigned 65,520 and 65,535 as 0x7bff on both backends. The #422 build returns 0x7c00 for both. The tie at 2,051 also goes from 2,050 (0x6801) on main to 2,052 (0x6802) with #422. This reproduces a native Windows rounding difference for the cases that already passed through vkd3d-proton.

**@⁠fuzeofficlal** on GitHub · 2026-11-24 13:10 UTC · [original](https://github.com/Marvisak/VIctory/issues/866#issuecomment-7132456365) I ran the same public test on native Windows with an RTX 5090 and software WARP (Microsoft Basic Render Driver, checked by adapter LUID). - `ffe310e` with only the test transplanted: 17/24 on each backend. - `015a089` (#422 `a746d53` plus the test): 24/24 on each backend. Both versions' two shaders passed DXIL 1.8.2502.11 validation. On main, the compute readback gives unsigned 65,520 and 65,535 as `0x7bff` on both backends. The #422 build returns `0x7c00` for both. The tie at 2,051 also goes from 2,050 (`0x6801`) on main to 2,052 (`0x6802`) with #422. This reproduces a native Windows rounding difference for the cases that already passed through vkd3d-proton.
Author
Owner

@⁠P2xe1 on GitHub · 2026-11-24 13:45 UTC · original · edited 2026-11-24 13:46 UTC

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: https://github.com/P2xe1/VIctory/pull/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 #422": the tests record main, and #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 #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.

**@⁠P2xe1** on GitHub · 2026-11-24 13:45 UTC · [original](https://github.com/Marvisak/VIctory/issues/866#issuecomment-7133282949) · edited 2026-11-24 13:46 UTC 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`](https://github.com/P2xe1/VIctory/tree/shader/v-cvt-f16-from-u16-i16-test), commit [`b81ac80`](https://github.com/P2xe1/VIctory/commit/b81ac808eb1c46cabe17b97992ddb9889cc444c0) on `main` `ffe310e`. Review PR against the fork's own `main`, since upstream `CreatePullRequest` still answers 404: https://github.com/P2xe1/VIctory/pull/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 #422": the tests record `main`, and #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 #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.
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#866
No description provided.