Shaders: v_cvt_rpi_i32_f32 rounds the exact sum; v_cvt_flr/rpi_i32_f32 saturate a NaN by its sign #605
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!605
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/605/head"
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?
@upipibi on GitHub · 2026-11-24 05:27 UTC · original
upipibi:fix/cvt-flr-rpi→main· +127 −8 · 2 files · 2 commits · closed without mergingBranch
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
@Lyrnc on GitHub · 2026-11-24 05:49 UTC · original · edited 2026-11-24 05:52 UTC
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.
@upipibi on GitHub · 2026-11-24 05:54 UTC · original
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.@Lyrnc on GitHub · 2026-11-24 06:59 UTC · original
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.
@slint6614-dev on GitHub · 2026-11-24 10:16 UTC · original
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.@upipibi on GitHub · 2026-11-24 10:45 UTC · original · edited 2026-11-24 13:07 UTC
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).
@upipibi on GitHub · 2026-11-24 23:32 UTC · original
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