Shaders: v_cvt_rpi_i32_f32 rounds the exact sum; v_cvt_flr/rpi_i32_f32 saturate a NaN by its sign #605
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/605"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-24T05:27:48Z.
Branch
victory-fix/cvt-flr-rpi, two commits onmaind2a4bae. The second (NaN) can be dropped without the first.What
crates/victory-shader/src/emit/lower.rs, thev_cvt_*_i32_f32arm:v_cvt_rpi_i32_f32rounding (commit 1). The lowering added 0.5 as an f32, then floored. The f32 sum rounds to even before the floor, so:0x3EFFFFFF(0.5 - 2^-25) gave 1, where the nearest integer is 0;0x4B000001(2^23 + 1) gave0x00800002, and every odd integer from 2^23 to 2^24 gave the even one above it.It now floors first and adds 1 when
x - floor(x) >= 0.5. That difference is exact in f32, so no rounding is left.NaN (commit 2).
v_cvt_flr_i32_f32andv_cvt_rpi_i32_f32gave 0 for a NaN (the host'sfptosi). They now give0x7FFFFFFFfor a positive and0x80000000for a negative NaN, tested on the operand's bits.v_cvt_i32_f32andv_cvt_u32_f32are unchanged (NaN gives 0).Measured on RDNA2 since: @Lyrnc ran both opcodes on an RDNA2 GPU (gfx1036, AMD driver 32.0.21042.62), 43 inputs under all four FLOAT_MODE settings (comment). Rounding: floor of the exact
x + 0.5matches 43/43; the f32-sum model misses exactly the six rows commit 1 targets. NaN:rpiandflrgive0x7fffffff/0x80000000by the NaN's sign, quiet or signalling, 43/43;v_cvt_i32_f32keeps NaN → 0. Onlyflrof a negative denormal depends on the mode, which is why that case is not in the tests.Why
Rounding. RDNA2 ISA (30-November-2020), 12.8 "VOP1 Instructions", opcode 12, p. 138:
"Round to nearest integer" of 0.49999997 is 0 and of 8388609 is 8388609; a float sum that rounds first is off by one there.
Against this reading: LLVM's selection pattern (
llvm/lib/Target/AMDGPU/AMDGPUInstructions.td,cvt_rpi_i32_f32=fp_to_sint (ffloor_nnan (fadd $src, fpimm_half))) is written with an f32 add, i.e. it treats the instruction as the rounded sum. I took the ISA's "nearest integer, 0.5ULP" as deciding. No AMD hardware was measured.NaN. Three sources; none states the values as numbers:
V_CVT_U32_F32(opcode 7) andV_CVT_I32_F32(opcode 8) only. Opcodes 12 and 13 have no such sentence.V_CVT_FLR_I32_F32) and 12-95 (V_CVT_RPI_I32_F32), the same two opcodes: "Float magnitudes too great to be represented by an integer float (unbiased exponent > 30) saturate to max_int or -max_int." On p. 12-93V_CVT_I32_F32has the same sentence plus a special-case table with "NaN & -NaN & 0 & -0 -> 0"; FLR and RPI have no such table. A NaN's unbiased exponent is 128.08ad328ae2(2015-01-31): "R600/SI: Only select cvt_flr/cvt_rpi with no NaNs. These have different behavior from cvt_i32_f32 on NaN." Still so today (ffloor_nnanin the pattern above).So "not 0" is stated (LLVM). "By sign, to the largest or smallest integer" is my reading of the GCN3 sentence, and
-max_intis taken as0x80000000. If documented-only behaviour is preferred, drop commit 2.Where the expected values come from
v_cvt_rpi_i32_f32_rounds_to_nearestv_cvt_flr_i32_f32_rounds_downv_cvt_flr_and_rpi_i32_f32_saturate_nan_by_signDenormal inputs: the translator does not model the float MODE register (
lower.rs: "the float MODE register is not modelled"), so this change does not depend on whether a denormal is flushed. The tests use only denormal cases whose result is the same either way (rpi of a denormal is 0).Tests
Synthetic 1-thread compute programs from hand-written instruction words in
valu_tests.rs(the existing harness: each module goes throughdxil.dllvalidation, here the Windows SDK's, before it runs on the GPU and on WARP).cargo test -p victory-shader -- --test-threads=1:maind2a4bae: 69 passed.hardware: v_cvt_rpi_i32_f32(0x3effffff = 4.9999997e-1) = 0x00000001, want 0x00000000hardware: v_cvt_rpi_i32_f32(0x7fc00000 = NaN) = 0x00000000, want 0x7fffffffmain; I did not re-run it there.cargo xtask policy: ok (151 files).cargo build -p victoryandcargo build -p victory --profile diag --features diagnostics: both build;cargo clippy -p victory-shader --testsadds no warnings.GPU: NVIDIA RTX 3070, driver 616.56, Windows 11; and WARP. These runs show what the translated DXIL computes on a host, not what an AMD GPU does.
Not in r4tedx's
integration/testerstree (ca8067d), and no open PR touches these two opcodes (all 354 open PR diffs searched).No game files were used. AI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T05:49:03Z.
RDNA2 hardware measurement of both commits (the description notes that no AMD hardware was measured): both match the hardware.
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_rpi_i32_f32,v_cvt_flr_i32_f32, and for referencev_cvt_i32_f32/v_cvt_u32_f32, in a HIP kernel (assembled by the AMD driver's amd_comgr), 43 inputs, each run under four FLOAT_MODE settings: f32 denormals flushed or kept, times IEEE mode off or on. RDNA2 desktop silicon, not the console's. No game data.rpiflri320x3effffff0x000000000x000000000x000000000x3f0000000x000000010x000000000x000000000xbf0000000x000000000xffffffff0x000000000xbf0000010xffffffff0xffffffff0x000000000x4b0000010x008000010x008000010x008000010x4b0000030x008000030x008000030x008000030x4b7fffff0x00ffffff0x00ffffff0x00ffffff0xcb0000010xff7fffff0xff7fffff0xff7fffff0x4f0000000x7fffffff0x7fffffff0x7fffffff0xcf0000000x800000000x800000000x800000000x7f8000000x7fffffff0x7fffffff0x7fffffff0xff8000000x800000000x800000000x800000000x7fc000000x7fffffff0x7fffffff0x000000000xffc000000x800000000x800000000x000000000x7f8000010x7fffffff0x7fffffff0x000000000xffffffff0x800000000x800000000x00000000Model fit over all 43 inputs, in every mode:
rpi= floor of the exactx + 0.5: 43/43. The f32-sum model (main's lowering, and LLVM'sfaddpattern): 37/43, wrong on exactly the six rows the commit targets (0x3effffff,±(2^23 + 1),2^23 + 3,±(2^24 - 1)).rpiandflrgive0x7ffffffffor a positive NaN and0x80000000for a negative one, quiet or signalling: 43/43 for both. "NaN gives 0" fails the six NaN rows.v_cvt_i32_f32gives 0 for every NaN, asmainassumes. So-max_intis indeed0x80000000.The only rows that change with the mode are the negative denormals (
0x80000001,0x807fffff) throughflr:0x00000000with f32 denormals flushed,0xffffffffwith them kept. That is the case you dropped from the flr test because it assumed denormals are kept; the hardware confirms it depends on MODE, so dropping it was right.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.
Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-24T05:54:44Z.
Thanks @Lyrnc, that settles both commits. I added your measurement to the description, and the test values now have hardware behind them, not just the documents. The negative-denormal
flrcase stays out of the tests, since it depends on the mode as you showed.Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T06:59:32Z.
Follow-up from a differential check of the translator against the gfx1036 hardware (121 VALU opcodes as raw VOP3 words, 220 inputs, translator on the Radeon iGPU as host):
v_cvt_rpi_i32_f32andv_cvt_flr_i32_f32differ from the hardware in 6 of 220 cases each onmain, all of them NaN inputs (0where the hardware gives0x7fffffff/0x80000000). With this PR both are 0 of 220, and no other opcode changes.AI use: Claude Code (Claude Opus 5.5) ran the check and drafted this comment.
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T09:37:38Z.
Review: COMMENTED
Merges with conflicts in
crates/victory-shader/src/emit/lower.rsandcrates/victory-shader/src/emit/valu_tests.rsagainst current main (8e79c80) — both are hot files in this wave of shader PRs. I couldn't run the test suite on the merged tree; the PR head itself is untested by me so far.Please rebase on main and I'll re-run the macOS suite and finish the review. (The v_cvt_rpi/flr rounding semantics look like they'll need a careful check against RDNA2's round-to-nearest-even with saturation once it's mergeable.)
Imported from Marvisak/VIctory. Originally posted by slint6614-dev on 2026-11-24T10:16:51Z.
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_testsat the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept.v_cvt_rpi_i32_f32_rounds_to_nearestandv_cvt_flr_and_rpi_i32_f32_saturate_nan_by_signfail on both GPUs without the fix, so the tests do catch the change on AMD hardware.v_cvt_flr_i32_f32_rounds_downalso passes without the fix (main already floors correctly), so it guards against regressions but does not exercise this change.Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-24T10:45:01Z.
Rebased onto
mainffe310e(conflicts only in appended tests invalu_tests.rs; the change itself is unchanged) and pushed: new heade7f5738.cargo test -p victory-shader -- --test-threads=1gives 123 passed, 0 failed, 1 ignored on an RTX 3070 (driver 616.56), andcargo build -p victoryplus the diag build are clean. Thanks @slint6614-dev for the AMD runs with the negative control.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T13:51:29Z.
Review: APPROVED
Tested head
e7f5738(fast-forward onmainffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC) with NVIDIA GeForce RTX 5090 (Blackwell) and WARP:cargo test -p victory-shader valu_tests: 16 passed, 0 failed (includingv_cvt_rpi_i32_f32_rounds_to_nearest,v_cvt_flr_i32_f32_rounds_down, andv_cvt_flr_and_rpi_i32_f32_saturate_nan_by_signon both RTX 5090 and WARP)cargo clippy -p victory-shader --all-targets: 0 warningscargo xtask policy: okClean lowering: computing
x - floor(x) >= 0.5avoids the float double-rounding error infloor(x + 0.5)for inputs like0.5 - 2^-25and2^23 + 1, and saturating NaN by its sign bit matches RDNA2v_cvt_{flr,rpi}_i32_f32. Approving.AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T14:55:07Z.
Review: APPROVED
Verified the lowering logic carefully — this is a correct fix for a real rounding bug:
v_cvt_rpi_i32_f32: computingfloor(x)first and thenrest = x - floor(x)(exact by Sterbenz) withrest >= 0.5 → +1gives floor(exact x + 0.5) without the f32 addition rounding first. The test vectors pin the exact cases the old code got wrong (0.5 - 2^-25 → 0,2^23 + 1 → itself), plus ties going up on both signs (-0.5 → 0, -1.5 → -1), which matches the documented floor(x+0.5) semantics.flr/rpi(and its deliberate absence for the plaini32/u32forms) is implemented with integer compares on the bit pattern (mag > inffor NaN, sign for direction) — clean, and the threecheck_cvtgroups pin all six NaN encodings plus the plain form's NaN→0.Verified locally: test-merge with current main (
ffe310e) clean;cargo test -p victory-shader --locked(macOS arm64, rustc 1.98.1) → 96 passed / 0 failed / 1 ignored.Scope note: the hardware rounding/NaN claims live in
valu_tests.rs, which is#[cfg(all(test, windows))]— the Windows lane needs to confirm the expected dwords on real hardware (RX 6700 XT / WARP), but the lowering structure and the exact-arithmetic argument are platform-independent and sound.Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-24T23:32:45Z.
Closing as superseded:
mainnow has both parts of this, from77053a0("accuracy fixes for discards, f16 rounding, denormals, med3 and signed zeros").v_cvt_rpi_i32_f32adds the half exactly (floor of x + 0.5 via x − floor(x)), andv_cvt_flr/rpi_i32_f32saturate a NaN by its sign. Thanks to Lyrnc and slint6614-dev for the earlier runs here.Pull request closed