Skip to content

rdna2spirv: the binary64 roundings keep a NaN's bits and round a subnormal without the driver (SHADER-ROUND-F64-SPECIALS) - #1047

Merged
aslan2142 merged 8 commits into
ProjectLeonida:mainfrom
milk4sale:shader/round-f64-specials
Nov 24, 2026
Merged

aslan2142 merged 8 commits into
ProjectLeonida:mainfrom
milk4sale:shader/round-f64-specials

Conversation

@milk4sale

@milk4sale milk4sale commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-ROUND-F64-SPECIALS (#14 (comment))

Depends on: #1034, which depends on #1012 (this branch is one commit on top of them; the last commit is the one to review. #1034 adds the fixture that found this and that this makes exact.)

What and why

v_trunc_f64, v_ceil_f64, v_rndne_f64 and v_floor_f64 bitcast their source pair to %double and call the driver's GLSL.std.450 Trunc, Ceil, RoundEven or Floor. For two kinds of source the driver's answer is not RDNA2's (the finding linked above, measured with cs_w32_round_f64):

  • A NaN. Vulkan lets a driver return any NaN for a NaN operand. A signalling NaN came back quiet, sign and payload kept (0x7ff0000000000001 gave 0x7ff8000000000001), on an RTX 4070 Ti (NVIDIA 620.70.06) and on llvmpipe. The hardware returns a NaN unchanged (row in docs/design.md, gfx1036, IEEE=0).
  • A subnormal. The module declares no DenormPreserve for 64 bits, so a driver may read one as zero, and llvmpipe did: v_ceil_f64 of 0x0000000000000001 gave +0 and v_floor_f64 of its negative -0, where the hardware gives 1 and -1. Declaring the mode is not a way out: neither device reports shaderDenormPreserveFloat64, and adding it by hand changed nothing on either.

The translator now selects those two sources with integer ops on the words, after the source modifiers and before the output ones:

  • a NaN's two words pass through;
  • a subnormal gives a zero of its sign, or 1 with its sign where the op rounds that sign away from zero (v_ceil_f64 of a positive one, v_floor_f64 of a negative one);
  • every other source takes the driver's result, as before.

With that, cs_w32_round_f64 drops the two allowances #1034 gave it and compares all 1152 heap words exactly. tests/semantic/test_f64_round.py gets F64RoundSpecialSources, which runs the four ops in the interpreter under a model of a driver that quiets a NaN and reads a subnormal as zero. The row in docs/design.md describes the rule and how it was confirmed, and no longer lists the approximation #1034 recorded.

Tested

At this head, llvm-mc 23.1.4, SPIRV-Tools v2026.4:

  • python -I scripts/run_gpu_tests.py --build runtime/build --filter cs_w32_round_f64 --require-pass: PASS ("1152 words exact") on the RTX 4070 Ti natively and with LEONIDA_W64_EMULATE=1, and on llvmpipe. One commit below, compared exactly, 15 of the 512 results differ on the 4070 Ti and 20 on llvmpipe.
  • --filter f64 on the 4070 Ti: 7/7 (cs_div_fmas_f64, cs_div_fmas_f64_rt, cs_f64_add, cs_w32_cmp_f64, cs_w32_round_f64, ps_f64_mods, ps_f64_round). The module passes spirv-val.
  • Rules broken one at a time, fixture rebuilt, on both devices. Each of these failed on both: a NaN left to the driver (word 784), the NaN and subnormal tests ignoring the low dword, v_ceil_f64 lifting the negative subnormal (word 34), v_floor_f64 lifting the positive one (word 38), a subnormal's result losing its sign (word 40), a NaN's low word dropped (word 784), rndne lowered as floor(x + 0.5) (word 20), trunc or ceil lowered as Floor, the neg or the abs source modifier ignored (words 8 and 26), the two result words swapped (word 8), the fixture without its v_cmpx (word 30). Two are not caught everywhere and are not counted: a subnormal left to the driver fails on llvmpipe and passes on the 4070 Ti, which keeps binary64 denormals, and RoundEven replaced by GLSL Round passes on both, since both send a tie to the even integer.
  • python -m unittest discover -s tests/semantic -p test_f64_round.py: 10 tests. With tools/rdna2spirv.py put back to the commit below, F64RoundSpecialSources fails in 18 subtests (the signalling NaNs for all four ops, ceil and floor of a subnormal) and the other tests pass.
  • Whole translator suite, python -I scripts/run_semantic_tests.py tests/semantic -j 8: 470 files, 6,169 tests, 0 files failed.
  • Corpus: none of the 25,734 disassembly listings in my work folder contains one of the four ops (5 contain any _f64 op), so no game shader's translation changes.
  • tests/test_gpu_round_f64.py (14 tests), tests/test_shader_checks.py (87), tests/test_run_gpu_tests.py (64), tests/test_semantic_translator_use.py, tests/test_docs_design_rows.py, tests/test_docs_links.py, tests/test_docs_repo_paths.py, tests/test_text_write_line_endings.py: OK. python -m compileall -q make_game.py tools tests and scripts/check_no_game_data.py on the changed files: OK.

Not run: the whole GPU suite at this head (the change is confined to the four ops, and every fixture that uses a binary64 op was run); llvm-mc 19; Windows; AMD hardware. The hardware's words are the row's gfx1036 measurement, which this does not repeat: the 4070 Ti and llvmpipe runs show that the translated module now gives those words on two drivers that used to differ.

Merge of #1034's 60c2ed910 (88785d0f3). The red linux-gpu-tools here was #1034's fixture on a runner without AVX-512, where llvmpipe computes Trunc and RoundEven of a double with its float code (48 of the 512 results; details in #1034). #1034 now lets llvmpipe, and only llvmpipe, return what that path gives for v_trunc_f64 and v_rndne_f64. This branch merges that commit; the one conflict was in round_f64_problem, where the judge stays exact for every other word and device, a NaN and a subnormal included, and takes that one allowance. The translator change is as reviewed at 26d010b41. On the merge: the fixture passes on llvmpipe (Mesa 26.2.6) natively and under GALLIUM_OVERRIDE_CPU_CAPS=avx and sse2, and on the RTX 4070 Ti natively and with LEONIDA_W64_EMULATE=1; tests/test_gpu_round_f64.py, tests/test_shader_checks.py, tests/test_run_gpu_tests.py and tests/semantic/test_f64_round.py pass, also on the tree merged with main 4a50caf.

GPUs tested: NVIDIA GeForce RTX 4070 Ti, driver 620.70.06, Linux (CachyOS, kernel 7.3.3); Mesa llvmpipe 26.2.6 (LLVM 23.1.4), same machine

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🤖 Generated with Claude Code

milk4sale and others added 2 commits November 24, 2026 11:04
…64, v_rndne_f64 and v_floor_f64 (GPU-ROUND-F64)

The translator gives the four ops to the driver's double-precision Trunc, Ceil, RoundEven and Floor, and no
fixture in runtime/test/shaders ran them, so what a driver does with ties, the largest value below 0.5, values at
and above 2^52, signed zeros, subnormals, infinities and NaNs had never gone through the GPU suite.

cs_w32_round_f64 is memory-fed, like cs_w32_shift64 and cs_w32_cmp_f64: check.py seeds 64 inputs and each thread
loads one at run time and stores eight results: the four roundings, v_trunc_f64 with a neg source, v_ceil_f64 with
an abs source, a v_floor_f64 whose destination pair overlaps its source pair, and a v_rndne_f64 run with the odd
threads turned off, whose destination must keep the pair it held. The expected words come from Python floats
(exact IEEE-754, the result keeping its source's sign) and are checked against exact rational rounding in
tests/test_gpu_round_f64.py.

Two kinds of input are judged by what Vulkan lets a driver do, not by RDNA2: a NaN may come back as another NaN,
and a subnormal may be read as a zero of its sign, since the module declares no DenormPreserve for 64 bits. That is
what the fixture measured: on an RTX 4070 Ti a signalling NaN comes back quiet, and on llvmpipe the smallest
subnormals are also read as zero. The row in docs/design.md records both as an approximation. dump_test takes an
optional judge for such a readback.

No translator or runtime change.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ormal without the driver (SHADER-ROUND-F64-SPECIALS)

v_trunc_f64, v_ceil_f64, v_rndne_f64 and v_floor_f64 hand their source to the driver's double-precision Trunc,
Ceil, RoundEven and Floor. Vulkan lets a driver return any NaN for a NaN operand and, with no DenormPreserve for
64 bits, read a subnormal as zero, and cs_w32_round_f64 measured both: a signalling NaN came back quiet on an RTX
4070 Ti (NVIDIA 620.70.06) and on llvmpipe, and llvmpipe read the smallest subnormals as zero, so v_ceil_f64 of the
positive one gave +0. RDNA2 returns a NaN unchanged and keeps subnormals: that ceil is 1.

The two kinds of source are now selected with integer ops on the words, after the source modifiers and before the
output ones. A NaN's words pass through. A subnormal gives a zero of its sign, or 1 with its sign where the op
rounds that sign away from zero (ceil of a positive one, floor of a negative one). Every other source still takes
the driver's result.

cs_w32_round_f64 now compares all 1152 words exactly, on both devices. tests/semantic/test_f64_round.py runs the
four ops under a model of a driver that quiets a NaN and reads a subnormal as zero; without the change that class
fails for the signalling NaNs and for ceil and floor of a subnormal. The row in docs/design.md describes the rule
and how it was confirmed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@whoalin1

Copy link
Copy Markdown
Contributor

Independent hardware verification from a second Windows box (Ryzen 5 7600 RDNA2 iGPU, gfx1035, AMD proprietary driver 24.10.24, Vulkan 1.3.280, 128-byte push constants, LEONIDA_VK_VALIDATION=1, LEONIDA_PC_TAIL_UBO=1; plus NVIDIA RTX 4070 SUPER, driver 610.88):

  • run_gpu_tests.py --filter round_f64 on the AMD iGPU against this PR's SPIR-V: PASS, 1152/1152 words exact (cs_w32_round_f64).
  • The unmodified origin/main translator output on the same AMD iGPU also passes bit-exact — AMD's driver already implements RDNA2 f64 rounding semantics (NaN passthrough, subnormals kept), so the deviation does not reproduce on AMD hardware.
  • On the NVIDIA RTX 4070 SUPER, main's lowering reproduces the reported bug exactly: word 784 (thread 49): trunc of 0x7ff0000000000001 gave 0x7ff8000000000001, expected 0x7ff0000000000001 (sNaN quieted). With this PR's fixed SPIR-V the same check passes on NVIDIA.
  • python -m unittest discover -s tests/semantic -p test_f64_round.py: 10 tests OK (1 skip, optional spirv-tools absent).

So the finding is a driver-conformance gap (NVIDIA quiets sNaNs; llvmpipe also flushes f64 subnormals), not an RDNA2 hardware bug, and this PR removes the driver dependence. AMD RDNA2 + NVIDIA both verified clean. AI-assisted verification.

@iFeyz

iFeyz commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

AMD run (the "Windows; AMD hardware" gap): RX 9070 XT (RDNA4, GFX1201), AMD proprietary Windows driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11, scripts/run_gpu_tests.py --filter _f64 with the test programs of a current main build.

  • Head 26d010b41: 7 of 7 pass, native and with LEONIDA_W64_EMULATE=1. cs_w32_round_f64: 1152 words exact. cs_w32_cmp_f64: 1536 words exact. tests/semantic/test_f64_round.py: 10 tests OK.
  • The AMD driver needs this fix too. I ran tests: GPU readback for the binary64 roundings, v_trunc_f64, v_ceil_f64, v_rndne_f64 and v_floor_f64 (GPU-ROUND-F64) #1034's translation (355c8c9c2, no translator change) under this PR's strict round_f64_problem. It fails at word 784: trunc of the signalling NaN 0x7ff0000000000001 gives 0x7ff8000000000001, the same quieting you measured on NVIDIA. Native and wave64 alike. So without this PR the proprietary AMD driver does not give RDNA2's words for a signalling NaN either, and with it every word matches.

milk4sale and others added 2 commits November 24, 2026 15:55
…undEven (GPU-ROUND-F64)

linux-gpu-tools failed this fixture with `word 164 (thread 10): rndne of 0x3fe0000000000001 gave
0x0000000000000000`, on some runners and not on others. The cause is llvmpipe, not the translation: its double
vectors are twice its native width (512 bits at the usual 256), and it has the exact Trunc and RoundEven only for a
width the CPU has a vector extension for. Without AVX-512 the two ops go through the code it has for a float, where
a magnitude above 2^24 comes back unchanged and RoundEven of a smaller one can give the other neighbouring integer.
That is 48 of the 512 results, the same 48 with Mesa 25.2.8 (LLVM 20.1.2) on a runner and with Mesa 26.2.6
(LLVM 23.1.4) under GALLIUM_OVERRIDE_CPU_CAPS=avx, sse4.1 or sse2 on a Ryzen 9 7900X; Floor and Ceil are exact on
that path, and with AVX-512 (or AVX at LP_NATIVE_VECTOR_WIDTH=128) so are all four.

On llvmpipe the judge now lets v_trunc_f64 and v_rndne_f64 return what that path can give: the source itself above
2^24, either neighbouring integer with the source's sign from 0.5 up to it. Everything else stays exact there, and no other
device gets the allowance (ROUND_F64_FLOAT_PATH, keyed like UNJUDGED_WORDS). dump_test hands the test program's
output to a judge, for its device line.

Tested: the fixture passes on llvmpipe natively and under each of the three overrides, at
LP_NATIVE_VECTOR_WIDTH=128, and on the RTX 4070 Ti (NVIDIA 620.70.06) natively and with LEONIDA_W64_EMULATE=1; the
previous judge fails under each override at word 164. tests/test_gpu_round_f64.py rebuilds the 48 measured results
and checks that nothing wider passes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…dge keeps llvmpipe's float-path allowance

ProjectLeonida#1034 gained a commit: on llvmpipe, v_trunc_f64 and v_rndne_f64 may return what its float code gives a double on a
CPU without AVX-512 (the cause of this branch's red linux-gpu-tools, `word 164 (thread 10): rndne of
0x3fe0000000000001 gave 0x0000000000000000`). The judge here stays exact for every other word and device, a NaN and
a subnormal included, and takes that one allowance; docs/testing.md says so next to "compared exactly".

Tested on this merge: the fixture passes on llvmpipe (Mesa 26.2.6) natively and under
GALLIUM_OVERRIDE_CPU_CAPS=avx and sse2, and on the RTX 4070 Ti (NVIDIA 620.70.06) natively and with
LEONIDA_W64_EMULATE=1.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
milk4sale and others added 4 commits November 24, 2026 16:31
Brings the branch up to date with main 68d1331. No conflicts; the merged tree was tested before the push (every standalone
test file under tests/ passes except tests/test_ci_registration.py, which fails on main alone since ProjectLeonida#930).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Brings the branch up to date with main 68d1331. No conflicts; the merged tree was tested before the push (every standalone
test file under tests/ passes except tests/test_ci_registration.py, which fails on main alone since ProjectLeonida#930).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Brings the branch up to date with main 6558d53, which now holds ProjectLeonida#1012 (GPU-CMP-F64), the pull request this one was
stacked on. Git merged it without conflicts (git merge-tree, exit 0). GitHub had reported a conflict since ProjectLeonida#1012
merged, with the branch and main on two merge bases (68d1331 and 5040782); main is an ancestor of this commit.

The merged tree was tested before the push: every standalone test file under tests/ passes except
tests/test_ci_registration.py, which fails on main 6558d53 alone (ProjectLeonida#1365 fixes it).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… with ProjectLeonida#1012 merged

Takes the updated head of ProjectLeonida#1034's branch, which holds main 6558d53 (ProjectLeonida#1012, GPU-CMP-F64, is merged there). Git merged
it without conflicts (git merge-tree, exit 0), and the tree is the one a direct merge of main gives. GitHub had
reported a conflict since ProjectLeonida#1012 merged, with the branch and main on two merge bases (68d1331 and 5040782). Merging
ProjectLeonida#1034's head instead of main leaves this branch with one merge base with main when ProjectLeonida#1034 merges.

The merged tree was tested before the push: every standalone test file under tests/ passes except
tests/test_ci_registration.py, which fails on main 6558d53 alone (ProjectLeonida#1365 fixes it).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@aslan2142
aslan2142 merged commit e0b08c7 into ProjectLeonida:main Nov 24, 2026
4 of 5 checks passed
@aslan2142

Copy link
Copy Markdown
Collaborator

Merged, after #1012 and #1034. Checked locally on main 3a73ce11a + 7fb019f22:

  • Claim: valid. SHADER-ROUND-F64-SPECIALS was posted at 17:04 UTC, before this PR, and is unedited.
  • The rule is right: NaN and subnormal sources are detected on the words after neg/abs and before the output modifiers. A subnormal goes to ±0 under trunc and rndne; ceil gives 1 for a positive one and −0 for a negative one; floor gives +0 and −1. That is IEEE 754. Passing a signalling NaN through unchanged follows the gfx1036 measurement in the row, as the PR says.
  • tests/semantic/test_f64_round.py: F64RoundSpecialSources fails in 18 subtests on main's translator and passes with this change.
  • GPU, --filter f64: 7 of 7 on llvmpipe (Mesa 26.3.1) and on an RDNA2 iGPU (RADV). Without the change, llvmpipe fails cs_w32_round_f64 at word 34 (ceil of the smallest subnormal), while RADV already returns the hardware's words.
  • On the merge: the semantic suite passes (475 files, 6,216 tests, 0 files failed); test_gpu_round_f64, test_run_gpu_tests, test_shader_checks pass; the 13 docs and writer tests pass; check_no_game_data.py is clean on the 8 changed files.
  • CI had not finished on this head. The reports here from whoalin1 and iFeyz (an RTX 4070 SUPER and an RX 9070 XT failing without the fix and passing with it) back the runs above.

AI-assisted (Claude Code, Claude Fable 5.2 and Claude Opus 5.5).

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.

4 participants