Skip to content

Translator: --native-rounding for AMD's own Floor/Ceil/Fract - #4064

Open
DaniSBR1 wants to merge 2 commits into
ItsNotRexed:mainfrom
DaniSBR1:pr-native-rounding
Open

DaniSBR1 wants to merge 2 commits into
ItsNotRexed:mainfrom
DaniSBR1:pr-native-rounding

Conversation

@DaniSBR1

@DaniSBR1 DaniSBR1 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

New SHADER item, claimed in #3918 (claim). Depends on: none. Related: #3299 (cs_round_f32_denorm, the GPU test for these ops on denormals).

What and why

v_floor_f32, v_ceil_f32 and v_fract_f32 go through Gen.round_f32_flushed, which gives RDNA2's results on any driver:

  • a denormal operand reads as a zero of its sign;
  • Fract's round-up to 1.0 becomes 0x3f7fffff;
  • NaNs pass through unchanged, signalling ones too;
  • fract(±inf) is 0xffc00000.

That is right everywhere, but on an RX 5700 XT it costs about six instructions per v_fract, and it keeps the driver from folding them. A pixel shader of 2,000 random fma/mul/add/min/max/fract ops takes 1.71 ms, against 0.33 ms for the same program written as native SPIR-V. Without fract the two are equal.

On AMD the driver's own Floor/Ceil/Fract compile to v_floor_f32/v_ceil_f32/v_fract_f32, so they already give those words. --native-rounding translates the three ops as the plain GLSL functions:

  • It is opt-in: make_game.py --native-rounding forwards it like the other translator flags and records it beside the translation.
  • LEONIDA_NATIVE_ROUNDING=1 turns it on for build.py/check.py and run_gpu_tests.py. The checks keep the same RDNA2 words, so a run says whether a driver's functions match the hardware.
  • f16 ops are unchanged.
  • docs/design.md gets a sentence in the min/max/med3/v_fract row, and docs/testing.md a short section.

Results

RX 5700 XT (RDNA1), RADV Mesa 26.0.8, main aa2988c:

default --native-rounding
Probe: 16 operands × the three ops in compute (denormals of both signs, -1e-10 and -1e-7 that round up, ±0, ±inf, quiet and signalling NaNs, 1e30, ordinary values) the RDNA2 words 48/48 the same words
The same 16 operands through v_fract_f32 in a pixel shader 16/16 the same
AMD's Windows driver (Adrenalin 26.11.2, same RX 5700 XT): the probe, and run_gpu_tests.py with and without LEONIDA_NATIVE_ROUNDING=1 48/48 and 16/16; both GPU runs 185/210, identical line for line, ps_fnan_fract passes
run_gpu_tests.py 184/210, 25 skipped the same results per test (ps_fnan_fract passes)
build.py + check.py 175/184 175/184, the same per test
#3299's cs_round_f32_denorm (gfx1036-measured words), its branch with this change pass pass
Pixel shader, 2000 ops with fract (native SPIR-V: 0.33 ms) 1.71 ms, 3947 instructions 0.53 ms, 1219 instructions; bit-identical image
fract-only pixel shader (native: 0.045 ms) 0.53 ms 0.045 ms
unittest discover -s tests/semantic 7458 run; the only failures are the 9 that fail on main aa2988c too (test_loop_ballot_barrier emulate cases, test_cmp_class_f64, test_mutations)

The one failure in both GPU runs is cs_signed_zero_nan, the known Mesa NIR issue from #166.

On llvmpipe, 10 of the 48 compute words differ under the flag: denormals through all three ops, Fract's round-up to 1.0, and signalling NaNs that get quieted. ps_fnan_fract and cs_round_f32_denorm fail there, while they pass without it. That is why the flag is opt-in. A later make_game.py "auto" could turn it on for AMD GPUs once AMD's Windows driver and other RADV generations are checked; the GPU tests with LEONIDA_NATIVE_ROUNDING=1 are the check.

New test: tests/semantic/test_native_rounding.py. With the flag the module holds only the GLSL function (no NaN, infinity or 0x3f7fffff constants in the body). On ordinary operands both translations give the same words in the interpreter.

AI-assisted (Claude Code): code, tests and this description. The tests ran on my own RX 5700 XT.

Tested f01af63 (on main aa2988c) on AMD Radeon RX 5700 XT (RDNA1, Navi 10), RADV Mesa 26.0.8-1ubuntu0.3, Vulkan 1.4.335, Ubuntu 26.04.1 (kernel 7.0.0-42), LLVM 21: pass

🤖 Generated with Claude Code

v_floor_f32, v_ceil_f32 and v_fract_f32 go through Gen.round_f32_flushed, which gives RDNA2's results for denormal
operands, Fract's round-up to 1.0, NaNs and infinities on any driver. That costs about six instructions per
v_fract on an RX 5700 XT and keeps the driver from folding them: a pixel shader of 2000 ops with v_fract among them
takes 1.71 ms against 0.33 ms for the same program in native SPIR-V.

On AMD the driver's own Floor/Ceil/Fract are the hardware instructions, so they already give those words. With
--native-rounding (opt-in; make_game.py --native-rounding, LEONIDA_NATIVE_ROUNDING=1 for the GPU test runners) the
three ops are plain GLSL Floor/Ceil/Fract. On an RX 5700 XT (RADV, Mesa 26.0.8):
- 48 of 48 edge-case words in compute (16 operands: denormals of both signs, -1e-10 and -1e-7 that round up,
  +-0, +-inf, quiet and signalling NaNs, large and ordinary values, through each op) and 16 of 16 v_fract words in
  a pixel shader equal the default translation;
- run_gpu_tests.py and check.py match the default per test (ps_fnan_fract passes), and so does ItsNotRexed#3299's
  cs_round_f32_denorm (gfx1036-measured words);
- the 2000-op shader goes from 1.71 ms to 0.53 ms, a v_fract-only one from 0.53 ms to 0.045 ms (= native).
On llvmpipe 5 of the 16 compute words differ, and ps_fnan_fract and cs_round_f32_denorm fail under the flag, which is
why it stays opt-in.

tests/semantic/test_native_rounding.py checks that the flag leaves only the GLSL function in the module and the
same words on ordinary operands.

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

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

Ran this at 192ab970 on Windows 11: python tests/semantic/test_native_rounding.py -> 2 tests, OK; compileall and git diff --check clean.

The tests pin both halves: with the flag, the module contains exactly the GLSL Floor/Ceil/Fract per value and no 0x3f7fffff/NaN/infinity constants; without it, the exact round_f32_flushed path remains, and ordinary operands produce the same interpreted words. The flag is opt-in through --native-rounding, make_game.py and the GPU runner, and the help/docs say it is exact only where the driver lowers to the hardware instructions (AMD/RADV), not generally. No blocking issue.

@TheMrBeez

Copy link
Copy Markdown

The check the description asks for before an "auto" mode: AMD's Windows driver on RDNA2. Tested 192ab970 against the native instructions on gfx1036 (the Ryzen 9 9950X3D iGPU, AMD driver 32.0.21030.2001), and through the translator on that iGPU and on an RTX 5080 (driver 621.01).

Inputs. 65,536 f32 operands:

  • both signs × all 256 exponents × 128 mantissas;
  • the mantissas are pseudo-random, plus 0, 0x7fffff and 0x400000;
  • so the sweep covers every denormal and NaN class, ±inf, ±0, and the tiny negatives whose fract rounds up.

Each operand goes through v_floor_f32, v_ceil_f32 and v_fract_f32, giving 196,608 result words.

Native reference (raw tools/hw_oracle kernels):

  • With the game's MODE (ieee = 0, denorm32 = 0), every NaN passes through unchanged, signalling ones included, as the description says.
  • With the oracle's default MODE (IEEE on), the hardware quiets the 126 signalling-NaN operands (0x7fbc6ef3 → 0x7ffc6ef3).
  • Those 126 are the only differences between the two modes.
  • Everything below is compared with the game-MODE words.
translation RTX 5080 Radeon gfx1036, AMD Windows driver
default 196,608 / 196,608 196,608 / 196,608
--native-rounding floor 65,156, ceil 65,156, fract 52,098 (of 65,536 each) 196,608 / 196,608

So on AMD's Windows driver for RDNA2, the driver's own Floor/Ceil/Fract give the RDNA2 word for every operand in this sweep:

  • denormals read as zero of their sign;
  • fract's round-up stays at 0x3f7fffff;
  • signalling NaNs pass through unchanged;
  • fract(±inf) = 0xffc00000.

That is one more driver beside your RADV result.

The 5080 differences are the cases the flag exists for, the same kinds you list for llvmpipe:

  • fract returns 1.0 for all 13,056 negative operands in the sweep with |x| ≤ 2⁻²⁵, the 127 negative denormals included (0x80800000 → 0x3f800000 where RDNA2 gives 0x3f7fffff);
  • positive denormals aren't flushed either (ceil(0x007fffff) = 1.0, fract keeps the denormal);
  • NaNs come back as 0x7fffffff, and so does fract(±inf).

Keeping it opt-in, or AMD-only for an "auto" mode, fits this.

run_gpu_tests.py on the gfx1036 (AMD Windows driver), without and with LEONIDA_NATIVE_ROUNDING=1:

Also: tests/semantic/test_native_rounding.py 2 OK, and spirv-val is clean for both translations.

Windows 11, Vulkan SDK 1.4.363, llvm-mc AMD LLVM 23.0.0git. Synthetic operands only, no game files. The sweep script is local. AI-assisted (Claude Code).

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

Reviewed at 192ab97 and independently ran the focused test file on Windows: 2/2 passed. The option is explicit and opt-in; the exact RDNA2 path remains the default, while the help and docs identify the native driver behavior as AMD-specific.

@ricardotanaka

Copy link
Copy Markdown

Tested 192ab970 (merged onto main aa2988cc) on AMD Radeon RX 7600 (RDNA3, Navi 33), AMD Windows driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11 26200.9457, MSVC 19.44 RelWithDebInfo, llvm-mc 23.1.6: pass, and the flag changes no result here. One more AMD generation on AMD's own Windows driver, next to @TheMrBeez's RDNA2 and the RDNA1/RADV in the description.

default LEONIDA_NATIVE_ROUNDING=1
build.py 234 translated, 234 pass spirv-val the same
check.py 181 of 184 pass, 3 skipped the same, line for line (184 of 184 identical)
run_gpu_tests.py 196/210 passed, 14 skipped the same, test for test (209 result lines identical)
ps_fnan_fract 4096/4096 pixels exact 4096/4096 pixels exact

tests/semantic/test_native_rounding.py (through unittest discover): 2 OK.

Not covered: the denormal and round-up-to-1.0 cases. On main the suite only reaches them through #3299's cs_round_f32_denorm, and #3299 conflicts with main now, so I couldn't run it beside this branch. What this run shows for RDNA3 is the NaN path (ps_fnan_fract) and that no other fixture moves under the flag; it isn't the 65,536-operand sweep TheMrBeez ran on RDNA2.

No game files. AI-assisted (Claude Code).

@iurysf

iurysf commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Code read of 192ab970 (GPU results from @TheMrBeez and the reviews above not repeated). Ran on Windows 11, Python 3.14, llvm-mc 23.1.6: test_native_rounding.py 2 OK, make_game.py --help lists the flag; with main's tools/rdna2spirv.py the new tests error (6). Mutation check: forcing if NATIVE_ROUNDING: to False or True is caught, but changing NATIVE_ROUNDING = a.native_rounding to False in main() is not, because the tests set the module global directly; nothing checks that the command-line option reaches the generator. build.py with LEONIDA_NATIVE_ROUNDING=1 does change the output (ps_fnan_fract: 489 lines instead of 506, both pass spirv-val), so the environment path works. A small test that runs the CLI with --native-rounding and compares with the default output would pin the plumbing.
AI-assisted code read (Claude).

The other tests set rdna2spirv.NATIVE_ROUNDING directly, so dropping the assignment from the command line in
main() went unnoticed (@iurysf's mutation check in ItsNotRexed#4064). This one runs rdna2spirv.py as a process with and
without the option and checks that only the default keeps the 0x3f7fffff clamp.

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

Copy link
Copy Markdown
Contributor Author

Two updates.

  1. A correction to the probe behind the first table rows. Its plain mode replaced only Fract, so Floor and Ceil went through the exact code in both runs, and their 32 words matched by construction. The Windows run below caught it. With all three ops plain, the RX 5700 XT on RADV still gives 48/48 in compute and 16/16 in the pixel shader. llvmpipe now differs on 10 of 48: Floor and Ceil of denormals and signalling NaNs too. I've corrected that row of the description. The GPU checks were never affected: ps_fnan_fract and Tests: GPU check for v_floor/ceil/fract_f32 and v_cvt_flr_i32_f32 of a denormal (TEST-515) #3299's cs_round_f32_denorm ran with the real --native-rounding, which covers all three ops.

  2. AMD's Windows driver, same RX 5700 XT (Adrenalin 26.11.2, Vulkan 1.4.315, main aa2988c): the corrected probe gives 48/48 and 16/16. run_gpu_tests.py on this branch, with and without LEONIDA_NATIVE_ROUNDING=1, gives 185/210, 25 skipped, 0 failed both times, identical line for line, and ps_fnan_fract passes (4096/4096 pixels). So both AMD drivers I can test give the hardware's words through plain Floor/Ceil/Fract. A make_game.py auto mode for AMD could follow once an RDNA2/3 card on RADV is checked too.

  3. @iurysf, good catch on the plumbing. The new commit adds test_the_command_line_option_reaches_the_translator, which runs rdna2spirv.py as a process with and without --native-rounding. With NATIVE_ROUNDING = a.native_rounding changed to False in main(), it now fails.

Thanks @TheMrBeez for the 65,536-operand sweep on RDNA2 with AMD's Windows driver, and @ricardotanaka for RDNA3.

AI-assisted (Claude Code); the runs are on my own RX 5700 XT.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested f01af63 merged onto main 03b09a0 on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed. main 03b09a0 gives 164/210, 46 skipped, 0 failed. All 214 test shaders translate and pass spirv-val.
  • tests/semantic/test_native_rounding.py: ran 3 tests in 0.958s; OK.

AI-assisted (Claude Code).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants