shader: lower v_ceil_f64, v_floor_f64, v_trunc_f64 and v_rndne_f64 in direct compute #3632

Manually merged
github merged 5 commits from github-bot/gh-3630 into main 2026-12-01 03:13:20 +00:00 AGit
Owner

@⁠Ammmae0x on GitHub · 2026-11-30 17:47 UTC · original
Ammmae0x:shader/direct-round-f64 → main · +964 −1 · 6 files · 5 commits · merged 2026-12-01 03:12 UTC

Refs #2935, #3520

Lowers the four binary64 rounding instructions (_e32 and _e64) in the direct C++ compute path with pure 32-bit integer arithmetic only, following the v_*_f16 rounding shape and the v_cmp_*_f64 pair rules.

What changed

  • src/gfx1013_direct_compute.inl (+132): new lowering block after the f16 rounding block. Modifier gate (pixel_stage, graphics_input, i.subword, i.dpp_control, permuted, i.clamp, i.scale, op_sel, modifiers on S1/S2), MODE gate (half_mode_known && !((mode_bits ^ 0xf0u) & 0x8000ccu)), destination must be a VGPR pair (256 <= dst < 511), S0 accepts VGPR pairs, aligned even SGPR pairs, integer 0 (128 -> +0.0) and inline binary64 constants 240..247 (lo = 0); abs then neg act on bit 63 before rounding. Result selected by biased exponent: exp >= 1075 passes the pair unchanged (NaN payload included); exp < 1023 rounds to a signed zero or one (the nonzero test looks at lo too, so a tiny subnormal is not mistaken for zero; rndne |x| == 0.5 rounds to even ±0.0, |x| > 0.5 rounds to ±1.0, including 0x3fe00000_00000001); in between, the fraction sits in lo (1044..1074, carry on wrap), straddles the word boundary (1043, half 0x80000000, evenness hi & 1, carry +1 to hi), or splits across hi and lo (1023..1042, half/sticky comparison across both words, evenness base_hi & unit_hi). All DXIL shift amounts stay in [0, 31] in every lane (clamped selects outside the range). No DXIL double, no i64 in @main(), no fcmp, neither DoublePrecisionFloatShaderOps nor Int64ShaderOps.
  • MODE loop: all four opcodes set half_binary = true, so a conditional denorm-mode write before them refuses.
  • src/gfx1013_compute_native.cpp: the four names join the pair condition in clear_vector_spills, so writing v[d:d+1] invalidates lane spills on both d and d+1.
  • tests/native_round_f64_check.py (model/ir/controls/hardware) + tests/cmake/native_round_f64.cmake + tests/checks/python-linux/native_round_f64.txt + docs/experiments/native-direct-round-f64.md.

Tests

Linux (this host cannot compile the translation unit or run Windows CTest; the C++ executions run in the WIN-VERIFY round):

/tmp/pe-env/bin/python -B tests/native_round_f64_check.py model
native_round_f64 model: 112 values x 4 ops x abs/neg matched math/Fraction; 18 self-mutations killed
/tmp/pe-env/bin/python -B tests/form_coverage_check.py
form coverage checks passed
/tmp/pe-env/bin/python -B tests/docs_links_check.py
Checked 189 Markdown files and 740 links.
/tmp/pe-env/bin/python -B tests/bidi_controls_check.py
Checked 2745 tracked source, script and build/CI files: no bidirectional controls.
/tmp/pe-env/bin/python -B tools/run_checks_parallel.py --from-dir tests/checks/python-linux
all 72 checks passed

git diff --check clean, git merge-tree --write-tree origin/main HEAD exits 0, zero personal paths in the diff.

Mutation audit

Every branch, guard and constant added is pinned by a test that kills its mutation: the modifier and MODE gates by the controls refusals (26 forms, exact substrings + non-zero exit); the half_binary MODE-loop addition by the conditional s_denorm_mode refusal; the destination/source pair acceptance by the controls refusals and the ir e64 chunk (SGPR pairs, inline 128/240..247, abs/neg, abs+neg); abs/neg by the ir modifier cases and the hardware e64 program; the 1075 passthrough, 1023 sub-one bound and the lo-aware nonzero test by the ir boundary values and the model big/small/nz_lo mutations; the lo-range unit, half, evenness and carry by the exp = 1044/1074 values and the model lo_unit/lo_half/lo_even/lo_carry mutations; the word-boundary half and evenness by the exp = 1043 ties and the model mid_half/mid_even mutations; the hi-range unit, half, sticky lo and evenness by the exp = 1023..1042 values and the model hi_unit/hi_half/hi_sticky/hi_even mutations; the sub-one ±1.0/±0.0 results and the sign by the ±0.5 ± 1ulp and subnormal values and the model rndne_half/trunc_sub/floor_sign/ceil_sign/sign_lose mutations. The clear_vector_spills addition joins the pair list for parity with v_cvt_f64_*; it is not what lets a pair write succeed over a symbolic lane spill. write() checks has_spill(r) on the state before clear_vector_spills runs, so any numeric vector write to a VGPR holding symbolic lanes intentionally refuses, and the controls spill refusals pin that both vector_write(i.dst) and vector_write(i.dst + 1) execute write(): a lane spilled into v10 refuses with write to a VGPR holding symbolic lanes: v10, and one spilled into v11 refuses with ...: v11, for each of the four ops.

Notes

  • An SDWA-encoded form refuses in the decoder, which has no SDWA profile for these ops.
  • Hardware readbacks compare NaN results by class, in case hardware quiets sNaNs.
  • The ir mode splits the wave32 program into dumps of at most 1024 stores each, since F.store masks offsets to 12 bits.

Windows Verification (ca0f1c09182b4ea25bdea32950956752a449f593)

Commit: ca0f1c09182b4ea25bdea32950956752a449f593 (merge-tree upstream/main HEAD -> 7512efd3, exit 0; git diff --check clean)
Adapter: NVIDIA GeForce RTX 4050 Laptop GPU (vendor=10de device=28e1, driver=32.0.15.9186, XBOX2WINDOWS_ADAPTER=NVIDIA)
dev: cmake --build --preset dev --target compile_dxil native_compute_ir_dump native_store_replay && ctest --preset dev --no-tests=error -R '^native_(round|fract)_f64_' --output-on-failure -> 6/6 passed (8.24s; round: ir 1.19s, controls 1.57s, hardware 1.30s; fract: ir 0.99s, controls 1.55s, hardware 1.51s)
release: cmake --build --preset release --target compile_dxil native_compute_ir_dump native_store_replay && ctest --preset release --no-tests=error -R '^native_round_f64_' --output-on-failure -> 3/3 passed (3.52s; ir 0.66s, controls 1.35s, hardware 1.38s)
Checks: native_round_f64_check.py model (112x4x4 matched, 18 mutations killed), form_coverage_check.py, docs_links_check.py (199 files, 765 links), bidi_controls_check.py (2793 files) -> passed

AI-assisted.

**@⁠Ammmae0x** on GitHub · 2026-11-30 17:47 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3630) `Ammmae0x:shader/direct-round-f64` → `main` · +964 −1 · 6 files · 5 commits · merged 2026-12-01 03:12 UTC Refs #2935, #3520 Lowers the four binary64 rounding instructions (`_e32` and `_e64`) in the direct C++ compute path with pure 32-bit integer arithmetic only, following the `v_*_f16` rounding shape and the `v_cmp_*_f64` pair rules. ## What changed - `src/gfx1013_direct_compute.inl` (+132): new lowering block after the f16 rounding block. Modifier gate (`pixel_stage`, `graphics_input`, `i.subword`, `i.dpp_control`, `permuted`, `i.clamp`, `i.scale`, `op_sel`, modifiers on S1/S2), MODE gate (`half_mode_known && !((mode_bits ^ 0xf0u) & 0x8000ccu)`), destination must be a VGPR pair (`256 <= dst < 511`), S0 accepts VGPR pairs, aligned even SGPR pairs, integer `0` (`128` -> `+0.0`) and inline binary64 constants `240..247` (`lo = 0`); abs then neg act on bit 63 before rounding. Result selected by biased exponent: `exp >= 1075` passes the pair unchanged (NaN payload included); `exp < 1023` rounds to a signed zero or one (the nonzero test looks at `lo` too, so a tiny subnormal is not mistaken for zero; `rndne` `|x| == 0.5` rounds to even `±0.0`, `|x| > 0.5` rounds to `±1.0`, including `0x3fe00000_00000001`); in between, the fraction sits in `lo` (`1044..1074`, carry on wrap), straddles the word boundary (`1043`, half `0x80000000`, evenness `hi & 1`, carry `+1` to `hi`), or splits across `hi` and `lo` (`1023..1042`, half/sticky comparison across both words, evenness `base_hi & unit_hi`). All DXIL shift amounts stay in `[0, 31]` in every lane (clamped selects outside the range). No DXIL `double`, no `i64` in `@main()`, no `fcmp`, neither `DoublePrecisionFloatShaderOps` nor `Int64ShaderOps`. - MODE loop: all four opcodes set `half_binary = true`, so a conditional denorm-mode write before them refuses. - `src/gfx1013_compute_native.cpp`: the four names join the `pair` condition in `clear_vector_spills`, so writing `v[d:d+1]` invalidates lane spills on both `d` and `d+1`. - `tests/native_round_f64_check.py` (model/ir/controls/hardware) + `tests/cmake/native_round_f64.cmake` + `tests/checks/python-linux/native_round_f64.txt` + `docs/experiments/native-direct-round-f64.md`. ## Tests Linux (this host cannot compile the translation unit or run Windows CTest; the C++ executions run in the WIN-VERIFY round): ``` /tmp/pe-env/bin/python -B tests/native_round_f64_check.py model native_round_f64 model: 112 values x 4 ops x abs/neg matched math/Fraction; 18 self-mutations killed /tmp/pe-env/bin/python -B tests/form_coverage_check.py form coverage checks passed /tmp/pe-env/bin/python -B tests/docs_links_check.py Checked 189 Markdown files and 740 links. /tmp/pe-env/bin/python -B tests/bidi_controls_check.py Checked 2745 tracked source, script and build/CI files: no bidirectional controls. /tmp/pe-env/bin/python -B tools/run_checks_parallel.py --from-dir tests/checks/python-linux all 72 checks passed ``` `git diff --check` clean, `git merge-tree --write-tree origin/main HEAD` exits 0, zero personal paths in the diff. ## Mutation audit Every branch, guard and constant added is pinned by a test that kills its mutation: the modifier and MODE gates by the `controls` refusals (26 forms, exact substrings + non-zero exit); the `half_binary` MODE-loop addition by the conditional `s_denorm_mode` refusal; the destination/source pair acceptance by the `controls` refusals and the `ir` e64 chunk (SGPR pairs, inline `128`/`240..247`, abs/neg, abs+neg); `abs`/`neg` by the `ir` modifier cases and the `hardware` e64 program; the `1075` passthrough, `1023` sub-one bound and the `lo`-aware nonzero test by the `ir` boundary values and the model `big`/`small`/`nz_lo` mutations; the lo-range unit, half, evenness and carry by the `exp = 1044`/`1074` values and the model `lo_unit`/`lo_half`/`lo_even`/`lo_carry` mutations; the word-boundary half and evenness by the `exp = 1043` ties and the model `mid_half`/`mid_even` mutations; the hi-range unit, half, sticky `lo` and evenness by the `exp = 1023..1042` values and the model `hi_unit`/`hi_half`/`hi_sticky`/`hi_even` mutations; the sub-one `±1.0`/`±0.0` results and the sign by the `±0.5 ± 1ulp` and subnormal values and the model `rndne_half`/`trunc_sub`/`floor_sign`/`ceil_sign`/`sign_lose` mutations. The `clear_vector_spills` addition joins the pair list for parity with `v_cvt_f64_*`; it is not what lets a pair write succeed over a symbolic lane spill. `write()` checks `has_spill(r)` on the state before `clear_vector_spills` runs, so any numeric vector write to a VGPR holding symbolic lanes intentionally refuses, and the `controls` spill refusals pin that both `vector_write(i.dst)` and `vector_write(i.dst + 1)` execute `write()`: a lane spilled into `v10` refuses with `write to a VGPR holding symbolic lanes: v10`, and one spilled into `v11` refuses with `...: v11`, for each of the four ops. ## Notes - An SDWA-encoded form refuses in the decoder, which has no SDWA profile for these ops. - Hardware readbacks compare NaN results by class, in case hardware quiets sNaNs. - The `ir` mode splits the wave32 program into dumps of at most 1024 stores each, since `F.store` masks offsets to 12 bits. ### Windows Verification (`ca0f1c09182b4ea25bdea32950956752a449f593`) ```text Commit: ca0f1c09182b4ea25bdea32950956752a449f593 (merge-tree upstream/main HEAD -> 7512efd3, exit 0; git diff --check clean) Adapter: NVIDIA GeForce RTX 4050 Laptop GPU (vendor=10de device=28e1, driver=32.0.15.9186, XBOX2WINDOWS_ADAPTER=NVIDIA) dev: cmake --build --preset dev --target compile_dxil native_compute_ir_dump native_store_replay && ctest --preset dev --no-tests=error -R '^native_(round|fract)_f64_' --output-on-failure -> 6/6 passed (8.24s; round: ir 1.19s, controls 1.57s, hardware 1.30s; fract: ir 0.99s, controls 1.55s, hardware 1.51s) release: cmake --build --preset release --target compile_dxil native_compute_ir_dump native_store_replay && ctest --preset release --no-tests=error -R '^native_round_f64_' --output-on-failure -> 3/3 passed (3.52s; ir 0.66s, controls 1.35s, hardware 1.38s) Checks: native_round_f64_check.py model (112x4x4 matched, 18 mutations killed), form_coverage_check.py, docs_links_check.py (199 files, 765 links), bidi_controls_check.py (2793 files) -> passed ``` AI-assisted. <!-- WIN-VERIFY: passed | sha=ca0f1c09182b4ea25bdea32950956752a449f593 | targets=compile_dxil native_compute_ir_dump native_store_replay | ctest=^native_round_f64_ | release=true -->
Refs #2935, #3518

Pure 32-bit integer lowering of the four binary64 rounding ops (_e32 and
_e64) in the direct C++ compute path, following the v_*_f16 rounding shape
and the v_cmp_*_f64 pair rules: no DXIL double, no i64 in @main(), no fcmp,
and no DoublePrecisionFloatShaderOps / Int64ShaderOps. The result is
selected by the biased exponent: exp >= 1075 passes the pair unchanged,
exp < 1023 rounds to a signed zero or one (the nonzero test looks at lo
too), and in between the fraction sits in lo (1044..1074), straddles the
word boundary (1043), or splits across hi and lo (1023..1042); rndne
rounds to nearest even at the unit position. abs then neg act on bit 63
before rounding. Sources are VGPR pairs, aligned even SGPR pairs, integer
0 (128) and the inline binary64 constants 240..247. MODE is restricted to
the default for the fields gfx10 shares between binary16 and binary64, and
the four ops join half_binary tracking. clear_vector_spills invalidates
lane spills on both words of a v[d:d+1] write.

Tests: tests/native_round_f64_check.py model|ir|controls|hardware, wired
into tests/checks/python-linux/native_round_f64.txt and
tests/cmake/native_round_f64.cmake. Linux model passed (112 values x 4 ops
x abs/neg, 18 self-mutations killed); full run_checks_parallel 72/72.
Windows ir/controls/hardware verification is pending.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 17:48:06 +00:00
1. MSVC C2561: the rounding block is inside bool float_alu(...), so end it
   with 'return true;' instead of 'return;'.
2. v_floor_f64 sub-one high word was 3221225472 (0xC0000000 = -2.0); it is
   3220176896 (0xBFF00000 = -1.0). The ir needle follows the translator's
   verbatim constant ('3220176896'); the controls dxc -dumpbin needle uses
   the signed form ('-1074790400').
3. Hardware stores: each case stores 4 ops x 8 bytes = 32 bytes, so the store
   stride is 32 * index, not 16 * index, in both hw_program() branches.
4. The ir spill dump was broken: write() checks has_spill(r) on the state
   BEFORE clear_vector_spills runs, so any numeric vector write to a VGPR
   holding symbolic lanes intentionally refuses. Remove the spill chunk;
   instead the controls refusals pin that both vector_write(i.dst) and
   vector_write(i.dst + 1) execute write(), with a spill into v10 refusing
   ': v10' and into v11 refusing ': v11' for each op. The pair entry in
   clear_vector_spills stays for parity with v_cvt_f64_*.
5. Wave64 ir: pure per-lane VALU ops never emit @waveExchange, and the
   wave64 EXEC setup uses @dx.op.waveActiveOp.i32, which F.interpret()
   does not implement. Check the dump text only (returncode 0,
   NumThreads=(64,1,1), no double/i64/fcmp, no exchange, needles present).
   Also add a combined abs+neg MOD_CASES entry to verify abs applies
   before neg.
native_compute_ir_dump prints raw LLVM IR metadata ('!6 = !{i32 64, i32 1,
i32 1}'); 'NumThreads=(64,1,1)' is only formatted by 'dxc -dumpbin'. Check
the metadata form instead.
Author
Owner

@⁠lukaslw-lab on GitHub · 2026-11-30 20:23 UTC · original · approved these changes

Reviewed 7503e733 by reading the diff; not built or run.

Verdict: approve. I traced each exponent range of the lowering and found no wrong result. No MUST.

  • NIT The hardware check compares NaNs by class, but the path has no float operation, so exp >= 1075 passes the pair bit-for-bit, sNaN payload included. An exact compare would pin that on a device too. (Same point as on #3636.)
  • Checked, no finding:
    • Below 1023 (src/gfx1013_direct_compute.inl:2162-2171): floor gives −1 and ceil +1 only for a nonzero input of the right sign; otherwise ±0 keeps its sign. rndne gives ±1 only above 0.5, and the nz test reads lo, so a lo-only subnormal is not taken for zero.
    • lo range 1044..1074 (odd = base_lo & unit_lo, wrapped carry into hi); word boundary 1043 (half 0x80000000, LSB hi & 1); hi range 1023..1042 (lo sticky; at 1023 the odd bit is the exponent LSB, right for [1, 2)). Out-of-range lanes shift by 1, so every shift stays in [0, 31].
    • Sources, MODE gate, half_binary and the spill pair entry match v_ldexp_f64. In-place pairs are safe.
    • The rules match the pixel lowering in #3645 (Inf/NaN and |x| ≥ 2^52 pass unchanged, ties to even, signed zeros kept), so the two paths agree.

Tests: model checks the model against Fraction round-to-nearest-even, with 18 self-mutations. ir runs the C++ lowering on rows at exponents 1042/1043/1044/1074 (every word-split branch), a sweep, and e64 forms. controls pins 26 refusals plus the spill refusals on v[d] and v[d+1]. CI ran only model (Linux checks); the native_round_f64_* CTest entries are not in the Windows pr preset filter. ir/controls/hardware have only the author's run at this head (RTX 4050, dev + release).

**@⁠lukaslw-lab** on GitHub · 2026-11-30 20:23 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3630#pullrequestreview-6119898036) · **approved these changes** Reviewed `7503e733` by reading the diff; not built or run. **Verdict: approve.** I traced each exponent range of the lowering and found no wrong result. No MUST. - **NIT** The hardware check compares NaNs by class, but the path has no float operation, so `exp >= 1075` passes the pair bit-for-bit, sNaN payload included. An exact compare would pin that on a device too. (Same point as on #3636.) - Checked, no finding: - Below 1023 (`src/gfx1013_direct_compute.inl:2162-2171`): floor gives −1 and ceil +1 only for a nonzero input of the right sign; otherwise ±0 keeps its sign. rndne gives ±1 only above 0.5, and the `nz` test reads `lo`, so a lo-only subnormal is not taken for zero. - lo range 1044..1074 (odd = `base_lo & unit_lo`, wrapped carry into `hi`); word boundary 1043 (half `0x80000000`, LSB `hi & 1`); hi range 1023..1042 (`lo` sticky; at 1023 the odd bit is the exponent LSB, right for [1, 2)). Out-of-range lanes shift by 1, so every shift stays in [0, 31]. - Sources, MODE gate, `half_binary` and the spill pair entry match `v_ldexp_f64`. In-place pairs are safe. - The rules match the pixel lowering in #3645 (Inf/NaN and |x| ≥ 2^52 pass unchanged, ties to even, signed zeros kept), so the two paths agree. Tests: `model` checks the model against `Fraction` round-to-nearest-even, with 18 self-mutations. `ir` runs the C++ lowering on rows at exponents 1042/1043/1044/1074 (every word-split branch), a sweep, and e64 forms. `controls` pins 26 refusals plus the spill refusals on `v[d]` and `v[d+1]`. CI ran only `model` (Linux checks); the `native_round_f64_*` CTest entries are not in the Windows `pr` preset filter. `ir`/`controls`/`hardware` have only the author's run at this head (RTX 4050, dev + release).
Resolve the single P3 conflict in src/gfx1013_direct_compute.inl
(half_binary scan loop) as a union: keep upstream's
v_frexp_exp_i32_f64 / v_frexp_mant_f64 alongside this branch's
v_ceil_f64 / v_floor_f64 / v_trunc_f64 / v_rndne_f64 (plus the
shared v_min_f64 / v_max_f64).
github manually merged commit c6b16a7a3f into main 2026-12-01 03:13:19 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-01 03:10 UTC · original · approved these changes

Reviewed the direct C++ binary64 rounding family and the refusal guards for MODE and unsupported operands. ctest --test-dir <build> --no-tests=error -j1 -R '^native_round_f64_(ir|controls|hardware)$' --output-on-failure passed IR, refusal controls and the RTX 4060 Laptop readback.

**@⁠R0Y00** on GitHub · 2026-12-01 03:10 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3630#pullrequestreview-6124496583) · **approved these changes** Reviewed the direct C++ binary64 rounding family and the refusal guards for MODE and unsupported operands. `ctest --test-dir <build> --no-tests=error -j1 -R '^native_round_f64_(ir|controls|hardware)$' --output-on-failure` passed IR, refusal controls and the RTX 4060 Laptop readback.
This discussion has been locked. Commenting is limited to contributors.
No description provided.