Shaders: narrow f32 to f16 to nearest even, as the console does #422

Closed
LouisRiverstone wants to merge 1 commit from upstream-pr/422 into main
LouisRiverstone commented 2026-11-24 03:12:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LouisRiverstone on 2026-11-24T03:12:20Z.


Problem. Every f16 result the translator produces goes through DXIL's f32-to-f16 conversion (legacyF32ToF16, op 130). I measured it on an RTX 4080 and on WARP, and it rounds toward zero:

f32 input host conversion nearest even (what the console's MODE default gives)
1 + 3/2048 (a tie) 0x3C01 0x3C02
6e-5 (subnormal) 0x03EE 0x03EF
3e-8 0x0000 0x0001
65520, 1e6 0x7BFF 0x7C00 (inf)

The code assumes round to nearest even ("Rounded to nearest even (the MODE default)" on v_cvt_f16_f32, "round once to f16" in valu16), so every f16 op could be one ulp off. The affected paths are the f16 ALU ops, v_cvt_f16_f32, v_fma_mix*, the packed f16 ops, f16 interpolants and d16 loads. A value past 65504 also stayed finite instead of becoming infinity.

Fix. A new f16_nearest does the narrowing exactly with integer ops on the bits:

  • a tie goes to the even half;
  • a finite value from 65520 up becomes infinity;
  • a result below 2^-14 stays subnormal and is rounded correctly;
  • NaN stays NaN (quiet, top payload bits kept).

All eight uses of op 130 now call it. f16_toward_zero (v_cvt_pkrtz_f16_f32) already stepped back from a nearest-even conversion. It now steps back from f16_nearest, so it gives the same results without depending on how the host rounds.

Test. f32_to_f16_rounds_to_nearest_even in valu_tests.rs runs on the host GPU and WARP:

  • v_cvt_f16_f32 on about 250 inputs: the midpoints between neighbouring halves, positive and negative, plus the f32 on either side of each, a spread from 2^-26 to past 65504, and ±inf, NaN, -0, 2^-25 and the f32 minimum normal. Each result is checked against a reference that finds the nearest half by search over the f16 values, so it shares no arithmetic with the lowering.
  • v_add_f16: a tie, 1 + 2^-10 plus 2^-11 gives 0x3C02; and 65504 + 32 gives inf.
  • v_cvt_pkrtz_f16_f32 still rounds toward zero.

Results:

  • Windows 11, RTX 4080 and WARP: passes, and DXIL validation with dxil.dll 10.0.26100 passes.
  • With main's lower.rs it fails: v_cvt_f16_f32(2.9802326e-8 = 0x33000001) = 0x0000, want 0x0001.
  • cargo test --workspace and cargo xtask policy pass. Clippy shows only the two warnings main already has.

Rounding follows the ISA and MODE's default (round to nearest even). I haven't measured on console hardware whether the title changes the f16 rounding mode.

No game files.

AI-assisted (Claude Code): I reviewed the change and ran the tests above.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573772893 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422). Originally posted by [LouisRiverstone](https://github.com/LouisRiverstone) on 2026-11-24T03:12:20Z. --- **Problem.** Every f16 result the translator produces goes through DXIL's f32-to-f16 conversion (`legacyF32ToF16`, op 130). I measured it on an RTX 4080 and on WARP, and it rounds **toward zero**: | f32 input | host conversion | nearest even (what the console's MODE default gives) | |---|---|---| | 1 + 3/2048 (a tie) | `0x3C01` | `0x3C02` | | 6e-5 (subnormal) | `0x03EE` | `0x03EF` | | 3e-8 | `0x0000` | `0x0001` | | 65520, 1e6 | `0x7BFF` | `0x7C00` (inf) | The code assumes round to nearest even ("Rounded to nearest even (the MODE default)" on `v_cvt_f16_f32`, "round once to f16" in `valu16`), so every f16 op could be one ulp off. The affected paths are the f16 ALU ops, `v_cvt_f16_f32`, `v_fma_mix*`, the packed f16 ops, f16 interpolants and d16 loads. A value past 65504 also stayed finite instead of becoming infinity. **Fix.** A new `f16_nearest` does the narrowing exactly with integer ops on the bits: - a tie goes to the even half; - a finite value from 65520 up becomes infinity; - a result below 2^-14 stays subnormal and is rounded correctly; - NaN stays NaN (quiet, top payload bits kept). All eight uses of op 130 now call it. `f16_toward_zero` (`v_cvt_pkrtz_f16_f32`) already stepped back from a nearest-even conversion. It now steps back from `f16_nearest`, so it gives the same results without depending on how the host rounds. **Test.** `f32_to_f16_rounds_to_nearest_even` in `valu_tests.rs` runs on the host GPU and WARP: - `v_cvt_f16_f32` on about 250 inputs: the midpoints between neighbouring halves, positive and negative, plus the f32 on either side of each, a spread from 2^-26 to past 65504, and ±inf, NaN, -0, 2^-25 and the f32 minimum normal. Each result is checked against a reference that finds the nearest half by search over the f16 values, so it shares no arithmetic with the lowering. - `v_add_f16`: a tie, 1 + 2^-10 plus 2^-11 gives `0x3C02`; and 65504 + 32 gives inf. - `v_cvt_pkrtz_f16_f32` still rounds toward zero. Results: - Windows 11, RTX 4080 and WARP: passes, and DXIL validation with `dxil.dll` 10.0.26100 passes. - With main's `lower.rs` it fails: `v_cvt_f16_f32(2.9802326e-8 = 0x33000001) = 0x0000, want 0x0001`. - `cargo test --workspace` and `cargo xtask policy` pass. Clippy shows only the two warnings main already has. Rounding follows the ISA and MODE's default (round to nearest even). I haven't measured on console hardware whether the title changes the f16 rounding mode. No game files. AI-assisted (Claude Code): I reviewed the change and ran the tests above.
towiice commented 2026-11-24 06:52:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by towiice on 2026-11-24T03:22:51Z.

Review: COMMENTED


Tested PR head 2661581 on Windows 11 x64 build 10.0.26300 (26H2), NVIDIA GeForce GTX 1650 SUPER (driver 610.88), Rust 1.100.0 (stable-x86_64-pc-windows-gnullvm):

  • cargo test -p victory-shader -- --test-threads=1: 70 passed, 0 failed (53.59 s).

All victory-shader tests (including f32-to-f16 round-to-nearest-even) pass cleanly.

<!-- greyfinch-sync:Marvisak/VIctory:review:6033188569 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#pullrequestreview-6033188569). Originally posted by [towiice](https://github.com/towiice) on 2026-11-24T03:22:51Z. **Review:** COMMENTED --- Tested PR head `2661581` on Windows 11 x64 build 10.0.26300 (26H2), NVIDIA GeForce GTX 1650 SUPER (driver 610.88), Rust 1.100.0 (`stable-x86_64-pc-windows-gnullvm`): - `cargo test -p victory-shader -- --test-threads=1`: **70 passed, 0 failed** (53.59 s). All `victory-shader` tests (including f32-to-f16 round-to-nearest-even) pass cleanly.
Lyrnc commented 2026-11-24 06:56:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T05:53:53Z.


RDNA2 hardware measurement of the conversion: f16_nearest matches the hardware's default mode on every input I tried, NaN payloads included. The host's toward-zero results are what the hardware gives only in round-toward-zero mode.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs v_cvt_f16_f32 on 34 inputs: your table's values, ties, ±65504/65519/65520/65536/1e6, the subnormal range, ±0, ±inf, and eight NaNs with different payloads. Each input ran with the FP16 round mode at nearest-even (0) and toward-zero (3), and with FP16 denormals kept (3) and flushed (0). The destination was preset to 0xdead0000. RDNA2 desktop silicon, not the console's. No game data.

f32 input round 0 (nearest even), denormals kept round 3 (toward zero)
0x3f803000 (1 + 3/2048, tie) 0x3c02 0x3c01
0x387ba882 (6e-5) 0x03ef 0x03ee
0x3300d959 (3e-8) 0x0001 0x0000
0x33000001 0x0001 0x0000
0x477ff000 (65520) 0x7c00 0x7bff
0x49742400 (1e6) 0x7c00 0x7bff
0x387fe000 (just under 2^-14) 0x0400 0x03ff
0x7f800001 (sNaN) 0x7e00 0x7e00
0x7f802000 (sNaN, payload) 0x7e01 0x7e01
0x7fa00000 (sNaN, payload) 0x7f00 0x7f00
0x7fffe000 0x7fff 0x7fff
0xffc00000 (-qNaN) 0xfe00 0xfe00
  • With the nearest-even mode and denormals kept, a Python transcription of f16_nearest matches the hardware on 34/34 inputs: ties to even, infinity from 65520, rounded subnormals, and NaN as 0x7e00 | mantissa >> 13 with the sign. The toward-zero column is the host behaviour you measured on the RTX 4080 and WARP.
  • v_add_f16 with the two cases in your test: 0x3c01 + 0x1000 = 0x3c02, and 0x7bff + 0x5000 = 0x7c00.
  • The high half of the destination is kept (0xdead) by v_cvt_f16_f32. Your lowering does not touch that, but it is now measured.

One mode note, as for the other MODE-dependent lowerings: with FP16 denormals flushed, every subnormal result in the set (5 distinct inputs) becomes 0x0000. This includes 0x387fe000, which rounds up to the smallest normal when denormals are kept but is flushed first when they are not. f16_nearest matches the denormals-kept mode.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123378172 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7123378172). Originally posted by [Lyrnc](https://github.com/Lyrnc) on 2026-11-24T05:53:53Z. --- RDNA2 hardware measurement of the conversion: **`f16_nearest` matches the hardware's default mode on every input I tried, NaN payloads included.** The host's toward-zero results are what the hardware gives only in round-toward-zero mode. **Setup.** AMD Radeon iGPU of a Ryzen 7 9800X3D, **gfx1036 (RDNA2)**, AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs `v_cvt_f16_f32` on 34 inputs: your table's values, ties, ±65504/65519/65520/65536/1e6, the subnormal range, ±0, ±inf, and eight NaNs with different payloads. Each input ran with the FP16 round mode at nearest-even (0) and toward-zero (3), and with FP16 denormals kept (3) and flushed (0). The destination was preset to `0xdead0000`. RDNA2 desktop silicon, not the console's. No game data. | f32 input | round 0 (nearest even), denormals kept | round 3 (toward zero) | |---|---|---| | `0x3f803000` (1 + 3/2048, tie) | `0x3c02` | `0x3c01` | | `0x387ba882` (6e-5) | `0x03ef` | `0x03ee` | | `0x3300d959` (3e-8) | `0x0001` | `0x0000` | | `0x33000001` | `0x0001` | `0x0000` | | `0x477ff000` (65520) | `0x7c00` | `0x7bff` | | `0x49742400` (1e6) | `0x7c00` | `0x7bff` | | `0x387fe000` (just under 2^-14) | `0x0400` | `0x03ff` | | `0x7f800001` (sNaN) | `0x7e00` | `0x7e00` | | `0x7f802000` (sNaN, payload) | `0x7e01` | `0x7e01` | | `0x7fa00000` (sNaN, payload) | `0x7f00` | `0x7f00` | | `0x7fffe000` | `0x7fff` | `0x7fff` | | `0xffc00000` (-qNaN) | `0xfe00` | `0xfe00` | - With the nearest-even mode and denormals kept, a Python transcription of `f16_nearest` matches the hardware on 34/34 inputs: ties to even, infinity from 65520, rounded subnormals, and NaN as `0x7e00 | mantissa >> 13` with the sign. The toward-zero column is the host behaviour you measured on the RTX 4080 and WARP. - `v_add_f16` with the two cases in your test: `0x3c01 + 0x1000` = `0x3c02`, and `0x7bff + 0x5000` = `0x7c00`. - The high half of the destination is kept (`0xdead`) by `v_cvt_f16_f32`. Your lowering does not touch that, but it is now measured. One mode note, as for the other MODE-dependent lowerings: with FP16 denormals flushed, every subnormal result in the set (5 distinct inputs) becomes `0x0000`. This includes `0x387fe000`, which rounds up to the smallest normal when denormals are kept but is flushed first when they are not. `f16_nearest` matches the denormals-kept mode. AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.
Lyrnc commented 2026-11-24 07:01:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T06:59:31Z.


End-to-end evidence for this PR from a differential check of the translator against RDNA2 hardware: every f16-narrowing difference I found goes away with it.

How. 121 of main's lowered VALU opcodes, each as a raw VOP3 word pair, run on 220 inputs (specials, random floats, random bits, random halves). One side runs on the gfx1036 hardware (hw-oracle, both f32/f16 denormal modes). The other is the same words through build_valu_cs and the host GPU (the Radeon iGPU via #94's VICTORY_ADAPTER). Each case is compared bit for bit, 16 bits for f16 results.

Opcode main mismatches with this PR
v_cvt_f16_f32 67 0
v_cvt_f16_u16 79 0
v_cvt_f16_i16 65 0
v_add_f16 69 0
v_sub_f16, v_subrev_f16 79, 79 0, 0
v_mul_f16 92 0
v_fma_f16 85 0
v_fract_f16 13 0

Packed and mixed ops too (second run, VOP3P words, 240 inputs, same hosts):

Opcode main mismatches with this PR
v_pk_add_f16 48 0
v_pk_mul_f16 53 0
v_pk_fma_f16 59 0 (+10 rows where several operands are NaN and the hardware passes on a different one, a payload choice)
v_fma_mixlo_f16 (op_sel_hi 0 / 7) 53 / 41 0 / 0

In the first run, 9 opcodes go from differing to matching the hardware on every row, and none gets worse: fully consistent opcodes go from 97 to 106 of 121. The rest of the remaining differences belong to other PRs: #605 (rpi/flr NaN), #384 (v_cvt_i16_f16), #284 (0x140). The others are IEEE-mode sNaN cases and a few ULPs on v_sin/v_cos_f32.

AI use: Claude Code (Claude Opus 5.5) built the check, ran it on the GPUs above and drafted this comment; the numbers come from those runs.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7124499353 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7124499353). Originally posted by [Lyrnc](https://github.com/Lyrnc) on 2026-11-24T06:59:31Z. --- End-to-end evidence for this PR from a differential check of the translator against RDNA2 hardware: **every f16-narrowing difference I found goes away with it.** **How.** 121 of `main`'s lowered VALU opcodes, each as a raw VOP3 word pair, run on 220 inputs (specials, random floats, random bits, random halves). One side runs on the gfx1036 hardware (hw-oracle, both f32/f16 denormal modes). The other is the same words through `build_valu_cs` and the host GPU (the Radeon iGPU via #94's `VICTORY_ADAPTER`). Each case is compared bit for bit, 16 bits for f16 results. | Opcode | `main` mismatches | with this PR | |---|---|---| | `v_cvt_f16_f32` | 67 | 0 | | `v_cvt_f16_u16` | 79 | 0 | | `v_cvt_f16_i16` | 65 | 0 | | `v_add_f16` | 69 | 0 | | `v_sub_f16`, `v_subrev_f16` | 79, 79 | 0, 0 | | `v_mul_f16` | 92 | 0 | | `v_fma_f16` | 85 | 0 | | `v_fract_f16` | 13 | 0 | **Packed and mixed ops too** (second run, VOP3P words, 240 inputs, same hosts): | Opcode | `main` mismatches | with this PR | |---|---|---| | `v_pk_add_f16` | 48 | 0 | | `v_pk_mul_f16` | 53 | 0 | | `v_pk_fma_f16` | 59 | 0 (+10 rows where several operands are NaN and the hardware passes on a different one, a payload choice) | | `v_fma_mixlo_f16` (`op_sel_hi` 0 / 7) | 53 / 41 | 0 / 0 | In the first run, 9 opcodes go from differing to matching the hardware on every row, and none gets worse: fully consistent opcodes go from 97 to 106 of 121. The rest of the remaining differences belong to other PRs: #605 (`rpi`/`flr` NaN), #384 (`v_cvt_i16_f16`), #284 (`0x140`). The others are IEEE-mode sNaN cases and a few ULPs on `v_sin/v_cos_f32`. AI use: Claude Code (Claude Opus 5.5) built the check, ran it on the GPUs above and drafted this comment; the numbers come from those runs.
slint6614-dev commented 2026-11-24 11:04:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by slint6614-dev on 2026-11-24T10:16:41Z.


AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of run_cases (local adapter-selection patch, not part of this PR); cargo test -p victory-shader --release valu_tests at the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept.

  • With this PR, f32_to_f16_rounds_to_nearest_even pass on both AMD GPUs.
  • Negative control: it fail on both GPUs without the fix, so the tests do catch the change on AMD hardware.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7128557793 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7128557793). Originally posted by [slint6614-dev](https://github.com/slint6614-dev) on 2026-11-24T10:16:41Z. --- AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of `run_cases` (local adapter-selection patch, not part of this PR); `cargo test -p victory-shader --release valu_tests` at the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept. - With this PR, `f32_to_f16_rounds_to_nearest_even` pass on both AMD GPUs. - Negative control: it fail on both GPUs without the fix, so the tests do catch the change on AMD hardware.
Halfect commented 2026-11-24 12:04:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Halfect on 2026-11-24T11:37:05Z.


Independent recheck of the latest head a746d53 on Windows 11:

  • cargo test --locked -p victory-shader --lib -- --test-threads=1: 121 passed, 0 failed, 1 ignored.
  • f32_to_f16_rounds_to_nearest_even: all three hardware-run groups passed on the default D3D12 adapter — nearest-even conversion, the two v_add_f16 rounding cases, and the two v_cvt_pkrtz_f16_f32 toward-zero controls. DXIL validation passed as part of the test.
  • WARP was unavailable for all three groups, so no WARP result is claimed in this recheck.
  • cargo clippy --locked -p victory-shader --lib --tests -- -D warnings: clean.
  • cargo xtask policy: 154 files ok.

No game data or title execution used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7130328617 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7130328617). Originally posted by [Halfect](https://github.com/Halfect) on 2026-11-24T11:37:05Z. --- Independent recheck of the latest head `a746d53` on Windows 11: - `cargo test --locked -p victory-shader --lib -- --test-threads=1`: **121 passed, 0 failed, 1 ignored**. - `f32_to_f16_rounds_to_nearest_even`: all three hardware-run groups passed on the default D3D12 adapter — nearest-even conversion, the two `v_add_f16` rounding cases, and the two `v_cvt_pkrtz_f16_f32` toward-zero controls. DXIL validation passed as part of the test. - WARP was unavailable for all three groups, so no WARP result is claimed in this recheck. - `cargo clippy --locked -p victory-shader --lib --tests -- -D warnings`: clean. - `cargo xtask policy`: **154 files ok**. No game data or title execution used.
mercuryVM commented 2026-11-24 13:01:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T12:40:23Z.

Review: APPROVED


Tested a746d53 on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 121 passed, including the new nearest-even f16 VALU tests on the GPU backend. cargo clippy -p victory-shader --all-targets is clean. victory translate --validate --pipelines on the local title executable still reports 12 of 12 programs.

I checked f16_nearest against the IEEE rules:

  • Normal path: adding 0xFFF + odd with the exponent rebiased by 112 << 23, then >> 13, is the standard round-half-even. A carry out of the mantissa correctly bumps the exponent.
  • Subnormal path: 126 - e is at least 14 for every input below 2^-14, and clamping at 31 sends f32 zero and denormals to 0. The remainder versus half comparison is tie-to-even on q.
  • Edges: from 0x477FF000 (65520) and up, including f32 inf, gives 0x7C00. NaN keeps its sign and the top payload bits, quietened with 0x7E00.
  • Small thing: the doc mentions MODE's default rounding. If a later PR models s_round_mode, this helper is the single place to switch.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6039347957 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#pullrequestreview-6039347957). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T12:40:23Z. **Review:** APPROVED --- Tested a746d53 on Windows 11, RTX 5070, MSVC: `cargo test -p victory-shader -- --test-threads=1` 121 passed, including the new nearest-even f16 VALU tests on the GPU backend. `cargo clippy -p victory-shader --all-targets` is clean. `victory translate --validate --pipelines` on the local title executable still reports 12 of 12 programs. I checked `f16_nearest` against the IEEE rules: - Normal path: adding `0xFFF + odd` with the exponent rebiased by 112 << 23, then `>> 13`, is the standard round-half-even. A carry out of the mantissa correctly bumps the exponent. - Subnormal path: `126 - e` is at least 14 for every input below 2^-14, and clamping at 31 sends f32 zero and denormals to 0. The remainder versus half comparison is tie-to-even on `q`. - Edges: from `0x477FF000` (65520) and up, including f32 inf, gives `0x7C00`. NaN keeps its sign and the top payload bits, quietened with `0x7E00`. - Small thing: the doc mentions MODE's default rounding. If a later PR models `s_round_mode`, this helper is the single place to switch. AI-assisted (Claude Code).
catminou4 commented 2026-11-24 14:03:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T13:20:39Z.

Review: APPROVED


This is the important kind of hardware-fidelity fix: the DXIL legacyF32ToF16 op rounds toward zero on NVIDIA and WARP (the comment documents the measured behavior: 1 + 3/2048 → 0x3C01, 1e6 → 0x7BFF), while the console's MODE default is round-to-nearest-even. Replacing all seven call sites with a software f16_nearest — pure bit manipulation, no DXIL op — makes f16 narrowing match the console.

The implementation handles the full range: normal rebias (127−15), subnormals with the 24-bit significand shift and clamp at 31, ties-to-even via the odd term, overflow to infinity at 65520, NaN quieting with payload preservation, and sign restoration. f16_toward_zero correctly keeps its semantics on top of nearest (one step back toward zero).

Test quality is the strongest I've seen in this queue: f16_nearest_reference is an independent search-based implementation that does NOT share the lowering's bit arithmetic, the test sweeps midpoint ties (both signs, ±1 ulp around each), a dense exponent spread, infinities, NaNs, −0.0, subnormals, and separately pins v_cvt_pkrtz_f16_f32 to toward-zero and v_add_f16 tie behavior.

Verified locally: test-merge with current main (ffe310e) clean; cargo test -p victory-shader --locked (macOS arm64, rustc 1.98.1) green — the new valu_tests execution cases are #[cfg(windows)]-gated like the rest of that suite, so they will run on the Windows CI lane, not here.: 96 passed / 0 failed / 1 ignored (execution tests Windows-gated; the bit-arithmetic itself I reviewed statically).

<!-- greyfinch-sync:Marvisak/VIctory:review:6039913908 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#pullrequestreview-6039913908). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T13:20:39Z. **Review:** APPROVED --- This is the important kind of hardware-fidelity fix: the DXIL `legacyF32ToF16` op rounds toward zero on NVIDIA and WARP (the comment documents the measured behavior: 1 + 3/2048 → 0x3C01, 1e6 → 0x7BFF), while the console's MODE default is round-to-nearest-even. Replacing all seven call sites with a software `f16_nearest` — pure bit manipulation, no DXIL op — makes f16 narrowing match the console. The implementation handles the full range: normal rebias (127−15), subnormals with the 24-bit significand shift and clamp at 31, ties-to-even via the `odd` term, overflow to infinity at 65520, NaN quieting with payload preservation, and sign restoration. `f16_toward_zero` correctly keeps its semantics on top of nearest (one step back toward zero). Test quality is the strongest I've seen in this queue: `f16_nearest_reference` is an independent search-based implementation that does NOT share the lowering's bit arithmetic, the test sweeps midpoint ties (both signs, ±1 ulp around each), a dense exponent spread, infinities, NaNs, −0.0, subnormals, and separately pins `v_cvt_pkrtz_f16_f32` to toward-zero and `v_add_f16` tie behavior. Verified locally: test-merge with current main (`ffe310e`) clean; `cargo test -p victory-shader --locked` (macOS arm64, rustc 1.98.1) green — the new `valu_tests` execution cases are `#[cfg(windows)]`-gated like the rest of that suite, so they will run on the Windows CI lane, not here.: **96 passed / 0 failed / 1 ignored** (execution tests Windows-gated; the bit-arithmetic itself I reviewed statically).
Yaekai commented 2026-11-24 17:04:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Yaekai on 2026-11-24T16:29:17Z.


Independent verification on Windows 11 + NVIDIA RTX 3070 (Rust 1.98.1) at head a746d53:

  • cargo test -p victory-shader --lib -- --skip valu_tests --skip waterfall_tests: 102 passed, 0 failed.
  • cargo clippy -p victory-shader -- -D warnings: 0 warnings.
  • Live GTA VI (1.0.2.1, --console series-s, 19-min world run): no regressions. Ready to merge!
<!-- greyfinch-sync:Marvisak/VIctory:comment:7137742351 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7137742351). Originally posted by [Yaekai](https://github.com/Yaekai) on 2026-11-24T16:29:17Z. --- Independent verification on Windows 11 + NVIDIA RTX 3070 (Rust 1.98.1) at head `a746d53`: - `cargo test -p victory-shader --lib -- --skip valu_tests --skip waterfall_tests`: 102 passed, 0 failed. - `cargo clippy -p victory-shader -- -D warnings`: 0 warnings. - Live GTA VI (`1.0.2.1`, `--console series-s`, 19-min world run): no regressions. Ready to merge!
bvtterbroot commented 2026-11-24 18:06:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by bvtterbroot on 2026-11-24T17:54:36Z.


AMD check at a746d53 (base ffe310e), on an RDNA3 and an RDNA2 GPU:

  • Windows 11, Rust 1.98.1. Radeon RX 7900 XTX (RDNA3, driver 32.0.32029.2010) and the Ryzen 7 7800X3D's iGPU (RDNA2, gfx1036, driver 32.0.21045.29003). I picked each as the hardware device of run_cases with a local adapter-selection patch that is not part of this PR. WARP runs in each pass as well.
  • cargo test -p victory-shader -p victory-gpu --locked: 171 passed, 0 failed, 2 ignored, on both GPUs. That includes its new test (f32_to_f16_rounds_to_nearest_even).

Synthetic tests only, no game files. Tested with AI assistance (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139880027 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7139880027). Originally posted by [bvtterbroot](https://github.com/bvtterbroot) on 2026-11-24T17:54:36Z. --- AMD check at `a746d53` (base `ffe310e`), on an RDNA3 and an RDNA2 GPU: - Windows 11, Rust 1.98.1. Radeon RX 7900 XTX (RDNA3, driver 32.0.32029.2010) and the Ryzen 7 7800X3D's iGPU (RDNA2, gfx1036, driver 32.0.21045.29003). I picked each as the hardware device of `run_cases` with a local adapter-selection patch that is not part of this PR. WARP runs in each pass as well. - `cargo test -p victory-shader -p victory-gpu --locked`: **171 passed, 0 failed**, 2 ignored, on both GPUs. That includes its new test (`f32_to_f16_rounds_to_nearest_even`). Synthetic tests only, no game files. _Tested with AI assistance (Claude Code)._
DovisLe commented 2026-11-24 20:03:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DovisLe on 2026-11-24T19:23:52Z.


Tested on AMD: Radeon 860M (RDNA 3.5, driver 32.0.32029.2010) and WARP, at a746d53. f32_to_f16_rounds_to_nearest_even passes, and the victory-shader lib suite passes (121 passed, 0 failed). AMD's legacyF32ToF16 truncates toward zero too, so this isn't NVIDIA-specific. An f16 ALU sweep (v_add/sub/mul/fma_f16, v_pk_*, v_cvt_f16_f32, 3,432 results against f64 rounded to nearest even; #1575) finds 22 mismatch groups on main and 0 with this PR, on both backends.

Heads-up: this conflicts with #1387 in lower.rs. Both add a helper right after f16_toward_zero, so keeping both resolves it.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7142037669 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7142037669). Originally posted by [DovisLe](https://github.com/DovisLe) on 2026-11-24T19:23:52Z. --- Tested on AMD: Radeon 860M (RDNA 3.5, driver 32.0.32029.2010) and WARP, at `a746d53`. `f32_to_f16_rounds_to_nearest_even` passes, and the `victory-shader` lib suite passes (121 passed, 0 failed). AMD's `legacyF32ToF16` truncates toward zero too, so this isn't NVIDIA-specific. An f16 ALU sweep (`v_add/sub/mul/fma_f16`, `v_pk_*`, `v_cvt_f16_f32`, 3,432 results against f64 rounded to nearest even; #1575) finds 22 mismatch groups on `main` and **0** with this PR, on both backends. Heads-up: this conflicts with #1387 in `lower.rs`. Both add a helper right after `f16_toward_zero`, so keeping both resolves it.
alexdeliu commented 2026-11-24 20:03:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T19:44:41Z.


Hardware evidence that this fix matters on AMD. On main @ ffe310eb, f16 results go through legacyF32ToF16. AMD's D3D12 driver rounds that conversion toward zero, on both an RX 9070 (32.0.32029.2010) and a Ryzen 7700 iGPU (gfx1036, 32.0.21045.29003). WARP rounds to nearest even. So on AMD every f16 op the translation computes in f32 comes out biased toward zero. Example:

v_add_f16(0xf8b6, 0x0001)
exact, round to nearest even (the console) 0xf8b6 (-38592)
WARP 0xf8b6
RX 9070 / gfx1036 iGPU 0xf8b5 (-38560)

A cross-device fuzzer ran the same 400 inputs per opcode (edge and random f16/f32 values) on all three devices:

main main + this PR (a746d533)
v_add_f16 15 differ 0
v_sub_f16 10 0
v_subrev_f16 13 0
v_fma_f16 2 0
v_cvt_pkrtz_f16_f32 8 0
v_mul_f16, v_cvt_f16_f32 0 0

With this PR, the RX 9070, the iGPU and WARP agree bit for bit on all of them.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7142527765 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7142527765). Originally posted by [alexdeliu](https://github.com/alexdeliu) on 2026-11-24T19:44:41Z. --- Hardware evidence that this fix matters on AMD. On `main` @ `ffe310eb`, f16 results go through `legacyF32ToF16`. **AMD's D3D12 driver rounds that conversion toward zero**, on both an RX 9070 (32.0.32029.2010) and a Ryzen 7700 iGPU (gfx1036, 32.0.21045.29003). WARP rounds to nearest even. So on AMD every f16 op the translation computes in f32 comes out biased toward zero. Example: | | `v_add_f16(0xf8b6, 0x0001)` | |---|---| | exact, round to nearest even (the console) | `0xf8b6` (-38592) | | WARP | `0xf8b6` | | RX 9070 / gfx1036 iGPU | **`0xf8b5`** (-38560) | A cross-device fuzzer ran the same 400 inputs per opcode (edge and random f16/f32 values) on all three devices: | | `main` | `main` + this PR (`a746d533`) | |---|---|---| | `v_add_f16` | 15 differ | 0 | | `v_sub_f16` | 10 | 0 | | `v_subrev_f16` | 13 | 0 | | `v_fma_f16` | 2 | 0 | | `v_cvt_pkrtz_f16_f32` | 8 | 0 | | `v_mul_f16`, `v_cvt_f16_f32` | 0 | 0 | With this PR, the RX 9070, the iGPU and WARP agree bit for bit on all of them.
Dufftwt commented 2026-11-24 20:03:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T19:56:36Z.


A heads-up on overlap: #169 adds one more legacyF32ToF16 call, in d16_channels (lower.rs), for the f16 channels of tbuffer_load_format_d16_*. Whichever of the two lands second should route it through f16_nearest. If #422 goes first, I'll rebase #169 onto it. alexdeliu's RX 9070 run of #169 also shows the AMD rounding you fix here: 65535 in 16_16_UNORM, which the host's division made 0.99999994, came out as 0x3BFF, truncated, where rounding to nearest gives 0x3C00.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7142816733 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7142816733). Originally posted by [Dufftwt](https://github.com/Dufftwt) on 2026-11-24T19:56:36Z. --- A heads-up on overlap: #169 adds one more `legacyF32ToF16` call, in `d16_channels` (`lower.rs`), for the f16 channels of `tbuffer_load_format_d16_*`. Whichever of the two lands second should route it through `f16_nearest`. If #422 goes first, I'll rebase #169 onto it. alexdeliu's RX 9070 run of #169 also shows the AMD rounding you fix here: 65535 in `16_16_UNORM`, which the host's division made 0.99999994, came out as `0x3BFF`, truncated, where rounding to nearest gives `0x3C00`.
alexdeliu commented 2026-11-24 21:03:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T20:11:48Z.


Follow-up: the packed and mixed-precision f16 ops have the same AMD rounding issue, and this PR fixes them too. Same three devices, 300 inputs per opcode, each f16 half compared on its own:

main main + #422
v_pk_add_f16 17 differ (both AMD GPUs) 0
v_pk_fma_f16 13 differ (the gfx1036 iGPU only) 0
v_fma_mixlo_f16 10 differ 0
v_fma_mixhi_f16 55 differ 0 (only NaN payloads still differ)
other v_pk_* 0 0

v_pk_fma_f16 is the one gfx1036 (RDNA2, the console's generation) gets wrong while the RX 9070 doesn't. RDNA2 hosts such as the RX 6700 XT should benefit from this PR the most.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7143175585 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7143175585). Originally posted by [alexdeliu](https://github.com/alexdeliu) on 2026-11-24T20:11:48Z. --- Follow-up: the packed and mixed-precision f16 ops have the same AMD rounding issue, and this PR fixes them too. Same three devices, 300 inputs per opcode, each f16 half compared on its own: | | `main` | `main` + #422 | |---|---|---| | `v_pk_add_f16` | 17 differ (both AMD GPUs) | 0 | | `v_pk_fma_f16` | 13 differ (the gfx1036 iGPU only) | 0 | | `v_fma_mixlo_f16` | 10 differ | 0 | | `v_fma_mixhi_f16` | 55 differ | 0 (only NaN payloads still differ) | | other `v_pk_*` | 0 | 0 | `v_pk_fma_f16` is the one gfx1036 (RDNA2, the console's generation) gets wrong while the RX 9070 doesn't. RDNA2 hosts such as the RX 6700 XT should benefit from this PR the most.
steven-seagull27 commented 2026-11-24 21:03:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by steven-seagull27 on 2026-11-24T20:46:14Z.


Your round-to-nearest-even change for FP16 narrowing covered the mismatches we saw on both the RTX 5080 and Microsoft WARP. I ran the half-precision sweep from #1575 (856f0ce) on native Windows, then repeated it with this PR (a746d53) and #1112 (4f5fa47) merged together. Each adapter went from 618 mismatches across 11 operation groups to zero, out of 2,076 values compared per adapter.

One example was v_add_f16(0x7bff, 0x7bff), adding the largest finite half to itself. Before the fixes, both adapters returned 0x7bff (65,504). In the combined run, both returned the expected 0x7c00 (positive infinity). NVIDIA driver 32.0.16.2124, Windows 11 build 26300.10075. Full results and reproduction command on #1575

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144032371 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7144032371). Originally posted by [steven-seagull27](https://github.com/steven-seagull27) on 2026-11-24T20:46:14Z. --- Your round-to-nearest-even change for FP16 narrowing covered the mismatches we saw on both the RTX 5080 and Microsoft WARP. I ran the half-precision sweep from #1575 (`856f0ce`) on native Windows, then repeated it with this PR (`a746d53`) and #1112 (`4f5fa47`) merged together. **Each adapter went from 618 mismatches across 11 operation groups to zero**, out of 2,076 values compared per adapter. One example was `v_add_f16(0x7bff, 0x7bff)`, adding the largest finite half to itself. Before the fixes, both adapters returned `0x7bff` (65,504). In the combined run, both returned the expected `0x7c00` (positive infinity). NVIDIA driver `32.0.16.2124`, Windows 11 build 26300.10075. [Full results and reproduction command on #1575](https://github.com/Marvisak/VIctory/pull/1575#issuecomment-7143699194)
alexdeliu commented 2026-11-24 22:02:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T21:56:46Z.


Correction to my explanation above; the data and the conclusion stand. Measurements in fxceless/victory-fork#136 (RTX 3070 Ti, Intel Iris Xe, WARP) show that legacyF32ToF16 truncates exact halfway cases on those devices too, so it isn't an AMD-only conversion. What is AMD-specific is that the f16tof32 → fadd → f32tof16 chain gives 0xf8b5 even where the f32 sum is exact (-38592 + 2⁻²⁴). That looks like a native f16 add rounding toward zero. Rounding to nearest even in the shader, as this PR does and as main now does (77053a0), fixes both.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7145598023 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/422#issuecomment-7145598023). Originally posted by [alexdeliu](https://github.com/alexdeliu) on 2026-11-24T21:56:46Z. --- Correction to my explanation above; the data and the conclusion stand. Measurements in fxceless/victory-fork#136 (RTX 3070 Ti, Intel Iris Xe, WARP) show that `legacyF32ToF16` truncates exact halfway cases on those devices too, so it isn't an AMD-only conversion. What is AMD-specific is that the `f16tof32 → fadd → f32tof16` chain gives `0xf8b5` even where the f32 sum is exact (-38592 + 2⁻²⁴). That looks like a native f16 add rounding toward zero. Rounding to nearest even in the shader, as this PR does and as `main` now does (`77053a0`), fixes both.
upstream-sync closed this pull request 2026-11-24 23:04:08 +00:00

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!422
No description provided.