Skip to content

Shaders: lower v_cvt_pknorm_{i16,u16}_{f32,f16} with exact rounding - #596

Open
mercuryVM wants to merge 4 commits into
Marvisak:mainfrom
mercuryVM:impl/cvt-pknorm
Open

mercuryVM wants to merge 4 commits into
Marvisak:mainfrom
mercuryVM:impl/cvt-pknorm

Conversation

@mercuryVM

@mercuryVM mercuryVM commented Nov 24, 2026 •

Copy link
Copy Markdown

What

v_cvt_pknorm_i16_f32, v_cvt_pknorm_u16_f32, v_cvt_pknorm_i16_f16 and v_cvt_pknorm_u16_f16 (VOP3 0x368, 0x369, 0x312, 0x313) were refused, so any program that used one failed to translate. They now lower:

  • Each source, with its abs/neg modifiers (the f16 forms read the half op_sel picks), is clamped to [-1, 1] (i16) or [0, 1] (u16), multiplied by 32767 or 65535 and rounded to nearest even once, from the exact product. NaN gives 0, infinities saturate, -1.0 gives -32767. src0 goes to the low half of the result and src1 to the high half.
  • The product is formed in 64-bit integers from the float's bits (mantissa × scale, then a shift with a round-to-nearest-even remainder test). In f32, x * 32767 can need up to 39 significant bits, so it can round onto a tie before the rounding to an integer. That is why Shaders: an audit of which opcodes lower, and 100 more that do #163 left these refused ("needs the console rounding and NaN rules").
  • f32 denormals give 0: below 2^-126 the scaled value is far under one half, so flushing or not does not change the result. f16 denormals do not give 0: they can scale to 1 or 2. The half is widened to f32 exactly, so they are kept, which matches RDNA2 with FP16 input denormals allowed. The console's MODE is not modelled.

One new helper, Lower::norm16, in crates/victory-shader/src/emit/lower.rs. It sets the INT64 flag, as src64 already does.

Effect on the title: none

Measured by @Parakeet-Wheat on the retail 1.0.2.1 shaders, in a comment below: the title contains 0 v_cvt_pknorm_* (and no unpacked v_cvt_norm_*); its only packing conversion is v_cvt_pk_u8_f32. victory translate --list --validate gives the same outcome on main and on this PR: 21,736 validated, 4,003 failed, 162 rejected. So this PR only completes the instruction set. It doesn't unblock anything in the game, and it can wait behind work that does.

Overlap

I searched open and closed PRs and issues: #376 lowers the unpacked v_cvt_norm_{i16,u16}_f16, and #163 lists all six as refused. No PR lowers the pknorm forms. This PR does not touch #376's opcodes, and either can land first without a conflict in the match.

Tested

Windows 11, NVIDIA RTX 5070, AMD CPU. No game data.

  • Two new GPU tests in valu_tests.rs, v_cvt_pknorm_f32_rounds_the_exact_product and v_cvt_pknorm_f16_rounds_the_exact_product. They run hand-assembled compute programs on the hardware device and on WARP, validate them with dxil.dll (10.0.26100), and compare against an f64 reference (the product is exact in f64). Inputs: ±0, ±1, ±2, ±inf, ±NaN, 0.5 (a tie in both scales), denormals, the smallest normal, values just below 1, and inputs found by search where an f32 product lands exactly on a tie but the exact product does not. The test asserts that those inputs really tell an f32 implementation apart from the exact one. The f16 cases put junk in the unread high halves and include large denormal halves; 0x83d6 and 0x823f are checked against the hardware's results (0xfffe, 0xffff).
  • Mutation check: shifting the exponent bias by one makes both tests fail on hardware (for example v_cvt_pknorm_i16_f32(NaN, 0.5) = 0x7fff0000, want 0x40000000).
  • cargo test --workspace: all passed (victory-shader 71 passed).
  • rustfmt --edition 2024 --check on both changed files: clean. cargo clippy -p victory-shader --all-targets: no warnings. cargo xtask policy: 151 files ok.

Not tested

  • The title was not run, and I have no shader package, so I cannot say how many of the title's programs use these opcodes or what they stop at next. The title's executable embeds only 12 programs, and none of them uses these opcodes.
  • Measured on RDNA2 desktop silicon by @Lyrnc (gfx1036, 422 inputs per form, comment below): the hardware rounds the exact product once, as this lowering does, in all four forms; an f32 product would miss 60 and 40 of the f32 inputs.
  • No console hardware. The semantics come from AMD's RDNA 2 ISA reference (D = {(snorm/unorm)S1, (snorm/unorm)S0}) and the D3D float-to-norm rules (NaN → 0, round to nearest even). If the console multiplies in f32 before it rounds, results differ by one step on the rare inputs the trap search finds. In [0, 1] the only exact tie is 0.5, so nearest-even and half-up agree on every exact tie.
  • No 64-lane (AMD) host GPU.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

The four packed-normalized conversions were refused by name, so any
program using one failed to translate. Each source (abs/neg applied; the
f16 forms read their op_sel half) is clamped to [-1, 1] or [0, 1],
multiplied by 32767 or 65535 and rounded to nearest even once, from the
exact product; NaN gives 0. src0 goes to the low half, src1 to the high.

The product is formed in 64-bit integers from the float's bits: in f32,
x * 32767 needs up to 39 significant bits and can round onto a tie
before the rounding to an integer.

GPU tests run the four opcodes on the host GPU and WARP against an f64
reference, including inputs where an f32 product lands on a tie.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@upipibi upipibi mentioned this pull request Nov 24, 2026
@Lyrnc

Lyrnc commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

RDNA2 hardware measurement of the rounding rule (the description lists "no console hardware"): the hardware rounds the exact product once, as this PR does. One note on f16 denormals below.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written v_cvt_pknorm_{i16,u16}_{f32,f16} in a HIP kernel (assembled by the AMD driver's amd_comgr). 422 inputs, the same value in src0 and src1: ±0, ±1, ±2, ±inf, NaNs, ±0.5, denormals, the smallest normal, one ulp below ±1, 300 random values in ±[2^-31, 1), and 100 inputs found by search where the f32 product lands exactly on .5 but the exact product does not. The f16 forms got the same values as halves, with junk in the unread high halves. RDNA2 desktop silicon, not the console's. No game data.

Form exact product, then nearest-even f32 product, then nearest-even
v_cvt_pknorm_i16_f32 422/422 362/422
v_cvt_pknorm_u16_f32 422/422 382/422
v_cvt_pknorm_i16_f16 (FP16 input denormals kept) 422/422 422/422
v_cvt_pknorm_u16_f16 (FP16 input denormals kept) 422/422 422/422

For example, 0x3f7334e6 gives 0x79997999 (i16) on the hardware, where an f32 product would round to 0x799a799a. 0x3e2abaab gives u16 0x2aaf2aaf, not 0x2aae2aae. The f32 forms give the same results with f32 denormals flushed or kept, as the PR says. Other values: 1.0 and +inf give 0x7fff7fff / 0xffffffff; -1.0 and -inf give 0x80018001 (-32767) / 0; NaN gives 0; 0.5 gives 0x40004000 / 0x80008000.

f16 denormals. "Denormals give 0" holds for the f32 forms. A denormal half can be as large as ±6.1e-5, though, and times 32767 that is up to about 2. With the FP16 denormal mode allowing input denormals (denorm16 = 1 or 3), the hardware returns the scaled value. For example, half 0x83d6 gives i16 0xfffe and 0x823f gives 0xffff, exactly what this PR computes, since it widens the half to f32 exactly. With input denormals flushed (denorm16 = 0 or 2), the same inputs give 0 (26 of the 422 rows). The new f16 test has only 0x0001 / 0x8001, where the two agree. Since the translator does not model MODE, this only says which FP16 mode the lowering matches (input denormals kept). A half like 0x03d6 in the test would pin that choice.

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.

Large f16 denormals (up to about 6.1e-5) scale to 1 or 2 in snorm and
unorm, so they tell the input-denormal modes apart; the lowering widens
the half to f32 exactly and keeps them, as RDNA2 does with FP16 input
denormals allowed (measured on gfx1036 and reported on the pull request:
half 0x83d6 gives i16 0xfffe, 0x823f gives 0xffff). The test now covers
such halves and checks those two results.

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

Copy link
Copy Markdown
Author

@Lyrnc thank you for measuring this on RDNA2, especially the f32-product traps. I added your suggestion in dab7a58: the f16 test now has large denormal halves (0x03d6, 0x83d6, 0x823f, 0x03ff), and it checks that 0x83d6 gives i16 0xfffe and 0x823f gives 0xffff, as your run with FP16 input denormals kept did. That pins the lowering to that mode; the console's MODE is not modelled, which the description now says.

AI-assisted (Claude Code).

# Conflicts:
#	crates/victory-shader/src/emit/valu_tests.rs
@mercuryVM

Copy link
Copy Markdown
Author

Merged current main (58fed37) in a421407. Only the end of valu_tests.rs conflicted. #376 has since landed with cvt_norm16 for the unpacked f16 forms. This PR's norm16 covers f32 and f16 sources in 64-bit integers, so the two don't overlap, and either could later serve both. victory-shader: 91 passed on the RTX 5070.

Note for testing: cargo test -p victory-shader does not compile on current main (#719, a call to lowers without its new rsrc2 argument; @LAKSHYA-cmd-tech has a fix branch). I ran the suite with that one-line fix applied locally, not committed here.

AI-assisted (Claude Code).

@catminou4 catminou4 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.

Merges cleanly with main; macOS arm64, rustc 1.98.1, cargo test -p victory-shader --locked with the PR merged into main 8e79c80 → 70 passed / 0 failed / 1 ignored.

Forming the product in integers from the float bits (mantissa × 32767/65535, then one round-to-nearest-even at the right shift) avoids the double rounding an f32 x * scale would introduce — that's the correct way to get exact v_cvt_pknorm_*. I checked the edge handling: saturation at |x| ≥ 1, denormals → 0, NaN → 0, sign applied only for the signed forms, and the s-cap at 63 keeps shifts defined while preserving the result. The f64 reference in the tests is the right oracle.

Note: the execution tests are Windows-gated (host device), so macOS coverage here is the lowering/compile path; Windows CI runs the real checks.

@slint6614-dev

Copy link
Copy Markdown

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, v_cvt_pknorm_f32_rounds_the_exact_product and v_cvt_pknorm_f16_rounds_the_exact_product pass on both AMD GPUs.
  • Negative control: both fail on both GPUs without the fix, so the tests do catch the change on AMD hardware.

@mercuryVM

Copy link
Copy Markdown
Author

@slint6614-dev thank you for the RDNA4 and RDNA2 runs, and for the negative control.

@Parakeet-Wheat if you have a moment: your count on #707 showed that PR doesn't touch the title. Could you run the same check here? That's the number of v_cvt_pknorm_{i16,u16}_{f32,f16} in victory shader-info --disasm on the retail shaders, and whether translate --list --validate changes any outcome. Counts only. If it's zero, I'll say so in the description, as on #707.

AI-assisted (Claude Code).

@Parakeet-Wheat

Copy link
Copy Markdown

@mercuryVM here are the counts for be92188, merged locally onto main ffe310e (clean):

  • Setup: Windows 11 26300, NVIDIA GeForce RTX 3070 (8 GB, driver 610.88), Ryzen 5 5600X, Rust 1.98.1 (x86_64-pc-windows-msvc), dxil.dll 1.8.2502, title 1.0.2.1.
  • victory shader-info --disasm over shaders.rpf and the executable: 0 v_cvt_pknorm_*, and no unpacked v_cvt_norm_* either. The only packing conversion is v_cvt_pk_u8_f32 (2,606).
  • victory translate <exe> shaders.rpf --list --validate, main vs this PR: no program changes outcome (21,736 validated, 4,003 failed, 162 rejected on both).
  • cargo test -p victory-shader --locked: 122 passed, 0 failed, 1 ignored; both v_cvt_pknorm_* tests pass on hardware and WARP.

Offline translation and validation only; no shader contents or extracted data included. AI-assisted (Claude Code); run on my machine.

@mercuryVM

Copy link
Copy Markdown
Author

@Parakeet-Wheat thank you. Zero in the title: I've put that at the top of the description. This PR is instruction-set completeness only.

AI-assisted (Claude Code).

@DenRakEiw DenRakEiw 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.

Tested merged cleanly onto main (ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC) with NVIDIA GeForce RTX 5090 (Blackwell) and WARP:

  • cargo test -p victory-shader valu_tests: 15 passed, 0 failed (including v_cvt_pknorm_f16_rounds_the_exact_product and v_cvt_pknorm_f32_rounds_the_exact_product on both RTX 5090 and WARP)
  • cargo clippy -p victory-shader --all-targets: 0 warnings
  • cargo xtask policy: ok

Very thorough integer-bit exact-product rounding (norm16) avoiding f32 intermediate rounding errors, with clean NaN/denormal/saturation handling and solid GPU verification. Approving.

AI-assisted (Gemini 4 Neon).

nazmi pushed a commit to nazmi/VIctory that referenced this pull request Nov 24, 2026
Merged by hand: its norm16 helper landed beside two_sum/f16_nearest, and its tests were appended to the module header and tail, which had gained dot2 and thread-group-info tests since its base. The PR tip is a merge of upstream main; only its feature change differs from the merge base.

Applied as the net diff of upstream PR Marvisak#596 (head be92188), 2 commits.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/lower.rs, crates/victory-shader/src/emit/valu_tests.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants