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

Closed
opened 2026-11-26 20:20:02 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1047
Author: @milk4sale
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/25/2026
Merged by: @aslan2142

Base: main ← Head: shader/round-f64-specials


📝 Commits (8)

  • 355c8c9 tests: GPU readback for the binary64 roundings, v_trunc_f64, v_ceil_f64, v_rndne_f64 and v_floor_f64 (GPU-ROUND-F64)
  • 26d010b rdna2spirv: the binary64 roundings keep a NaN's bits and round a subnormal without the driver (SHADER-ROUND-F64-SPECIALS)
  • 60c2ed9 tests: cs_w32_round_f64 allows llvmpipe's float path for Trunc and RoundEven (GPU-ROUND-F64)
  • 88785d0 Merge gpu-test/round-f64 into shader/round-f64-specials: the exact judge keeps llvmpipe's float-path allowance
  • e040dce Merge main into gpu-test/round-f64
  • 7998d65 Merge main into shader/round-f64-specials
  • 69935f8 Merge main into gpu-test/round-f64
  • 7fb019f Merge gpu-test/round-f64 into shader/round-f64-specials: main 6558d5318, with #1012 merged

📊 Changes

9 files changed (+724 additions, -7 deletions)

View changed files

📝 docs/design.md (+1 -1)
📝 docs/testing.md (+42 -0)
📝 runtime/test/shaders/check.py (+138 -2)
➕ runtime/test/shaders/cs_w32_round_f64.asm (+56 -0)
📝 scripts/run_gpu_tests.py (+5 -0)
📝 tests/semantic/test_f64_round.py (+109 -0)
➕ tests/test_gpu_round_f64.py (+320 -0)
📝 tests/test_shader_checks.py (+10 -2)
📝 tools/rdna2spirv.py (+43 -2)

📄 Description

Claimed in the coordination issue as: SHADER-ROUND-F64-SPECIALS (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7138652249)

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 4a50cafc7.

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1047 **Author:** [@milk4sale](https://github.com/milk4sale) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/25/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `shader/round-f64-specials` --- ### 📝 Commits (8) - [`355c8c9`](https://github.com/ubiquitarianism/leonida/commit/355c8c9c2da01a9898df5061d266ca1f5567cdc9) tests: GPU readback for the binary64 roundings, v_trunc_f64, v_ceil_f64, v_rndne_f64 and v_floor_f64 (GPU-ROUND-F64) - [`26d010b`](https://github.com/ubiquitarianism/leonida/commit/26d010b41dc042bc36698f673ad588a1e6d04f07) rdna2spirv: the binary64 roundings keep a NaN's bits and round a subnormal without the driver (SHADER-ROUND-F64-SPECIALS) - [`60c2ed9`](https://github.com/ubiquitarianism/leonida/commit/60c2ed910b90aef3cf5af1df2366a28a25afbe64) tests: cs_w32_round_f64 allows llvmpipe's float path for Trunc and RoundEven (GPU-ROUND-F64) - [`88785d0`](https://github.com/ubiquitarianism/leonida/commit/88785d0f3d503c798a631f45e29404ea939e859d) Merge gpu-test/round-f64 into shader/round-f64-specials: the exact judge keeps llvmpipe's float-path allowance - [`e040dce`](https://github.com/ubiquitarianism/leonida/commit/e040dce1f09d42f21a8fed75668c53c0d49fb57a) Merge main into gpu-test/round-f64 - [`7998d65`](https://github.com/ubiquitarianism/leonida/commit/7998d654009ad50fe89561007116e709935f1126) Merge main into shader/round-f64-specials - [`69935f8`](https://github.com/ubiquitarianism/leonida/commit/69935f84ee8d670626594c3c5e4a171b2e20fcf0) Merge main into gpu-test/round-f64 - [`7fb019f`](https://github.com/ubiquitarianism/leonida/commit/7fb019f2202c91a6f1fe6337d69cacd1028daca7) Merge gpu-test/round-f64 into shader/round-f64-specials: main 6558d5318, with #1012 merged ### 📊 Changes **9 files changed** (+724 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) 📝 `docs/testing.md` (+42 -0) 📝 `runtime/test/shaders/check.py` (+138 -2) ➕ `runtime/test/shaders/cs_w32_round_f64.asm` (+56 -0) 📝 `scripts/run_gpu_tests.py` (+5 -0) 📝 `tests/semantic/test_f64_round.py` (+109 -0) ➕ `tests/test_gpu_round_f64.py` (+320 -0) 📝 `tests/test_shader_checks.py` (+10 -2) 📝 `tools/rdna2spirv.py` (+43 -2) </details> ### 📄 Description Claimed in the coordination issue as: SHADER-ROUND-F64-SPECIALS (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7138652249) 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` 4a50cafc7. 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:20:02 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1084
No description provided.