shader: lower v_fract_f64 in direct compute #3639

Manually merged
github merged 3 commits from github-bot/gh-3637 into main 2026-12-01 01:19:30 +00:00 AGit
Owner

@⁠Ammmae0x on GitHub · 2026-11-30 19:02 UTC · original
Ammmae0x:shader/direct-fract-f64 → main · +930 −2 · 6 files · 3 commits · merged 2026-12-01 01:17 UTC

Refs #2935, #3520

Lowers v_fract_f64 (VOP1 0x3e, VOP3 0x1be) in the direct C++ compute path with pure 32-bit integer operations only: no DXIL double, no i64 in @main(), no fcmp, and neither DoublePrecisionFloatShaderOps nor Int64ShaderOps.

What changed

  • src/gfx1013_direct_compute.inl: new float_alu handler for v_fract_f64, ending with return true;. fract(x) = x - floor(x) is always in [0, 1): NaN is quieted (hi | 0x80000), ±Inf gives +qNaN (0, 0x7FF80000), ±0.0 and |x| >= 2^52 give +0.0 (-0.0 becomes +0.0, matching v_fract_f16), and a positive |x| < 1 passes unchanged (including subnormals). A negative |x| < 1 gives RNE(1 - |x|) clamped strictly below 1.0 (0x3FEFFFFFFFFFFFFF): for |x| <= 2^-54 RNE(1 - |x|) rounds to 1.0 and clamps to it, and for 2^-54 < |x| < 2^-53 RNE(1 - |x|) is already 0x3FEFFFFFFFFFFFFF, so all exp < 970 return it; for 970 <= exp <= 1021 the 53-bit significand is shifted right by r = 1022 - exp ([1, 52], split wide = r >= 32 like v_ldexp_f64), rounded to nearest 2^-53, then subtracted from 2^53 (biased exponent always 1022); for exp == 1022 the exact diff (0x00100000, 0) - (field, lo) is normalized left. For 1023 <= exp <= 1074 the bottom 1075 - exp fractional bits are extracted across hi/lo/masked lo; an exact integer gives +0.0, a positive fraction is normalized directly, and a negative fraction first takes the exact 64-bit unit - frac and then shares the same 6-step {32, 16, 8, 4, 2, 1} normalizer (biased exponent exp - steps). Every DXIL shift amount is clamped to [0, 31] in every lane.
  • Modifier gate refuses pixel_stage, graphics_input, i.subword, i.dpp_control, permuted, i.clamp, i.scale and the wide-form S1/S2 bits; MODE gate refuses a non-default rounding/denormal mode, with v_fract_f64 added to half_binary in the MODE scan loop; S0/D must be 64-bit register pairs (VGPR pairs, aligned even SGPR pairs, integer 0 (128), inline f64 constants 240..247); abs then neg act on bit 63 before the split.
  • src/gfx1013_compute_native.cpp: v_fract_f64 added to the pair list in clear_vector_spills, so a symbolic lane spilled into either half refuses the pair write.

Tests

New tests/native_fract_f64_check.py (model/ir/controls/hardware) + tests/cmake/native_fract_f64.cmake + tests/checks/python-linux/native_fract_f64.txt + docs/experiments/native-direct-fract-f64.md.

  • model: 74 boundary values (±0.0, ±MIN_SUB, ±2^-55, ±2^-54, ±(2^-54 + 1ulp), ±2^-53, ±(2^-53 + 2^-54), ±(2^-53 + 1ulp), ±(0.5 - 2^-54), ±(0.5 - 2^-53), ±0.5, ±(0.5 + 1ulp), ±0.75, ±(1 - 1ulp), ±1.0, ±1.25, ±1.5, ±(1 + 2^-52), cross-word exp = 1042/1043/1044, ±(2^52 - 1), ±(2^52 - 0.5), ±2^52, ±DBL_MAX, ±Inf, ±sNaN, ±qNaN with payloads, ±(2^-52 + 2^-54)) x plain/abs/neg/abs+neg against an independent exact Fraction RNE reference (min(round_exact(Fraction(x) - floor(Fraction(x))), 0x3FEFFFFFFFFFFFFF)); 12 self-mutations killed (clamp, neg_zero, tiny_round, rndne_tie, exp1022, int_zero, neg_sub, unit_hi, unit_lo, pos_neg, inf_nan, absneg). Spot checks: -(0.5 - 2^-54) gives 0.5, -(2^-53 + 2^-54) gives 1.0 - 2^-52.
  • ir: boundary values + seeded sweep of 100 random doubles in one e32 dump (348 stores), e64 forms (SGPR pairs, 128, 240..247, abs/neg/abs+neg) in a second dump, and the boundary values as a wave64 container (text-only: {i32 64, i32 1, i32 1}, no @waveExchange, no double/i64/fcmp); exact DXIL needles (1072693247, 1071644672, 2146959360, 2097152, 1048575, 2147483648).
  • controls: compiles the four hardware programs and checks @main() has no double, i64 or fcmp; 20 forms refused with exact error substrings, including symbolic spills in v10 and v11; the truncating (x - trunc(x)), flushing-subnormal and uncapped-1.0 references each differ.
  • hardware: 4 programs x 4 cases x 2 words on a 32-byte case stride, NaNs compared by class, 77 on missing caps.

Linux verification

  • tests/native_fract_f64_check.py model: 74 values x abs/neg/absneg matched the Fraction reference; 12 self-mutations killed.
  • The C++ lowering was additionally transliterated to Python and checked against the same Fraction reference over every exponent (0..2046 x 4 random mantissas) and 20,000 random doubles, 0 mismatches, with every DXIL shift amount asserted in [0, 31] in every lane.
  • tests/form_coverage_check.py, tests/docs_links_check.py (188 files / 740 links), tests/bidi_controls_check.py (2744 files): pass.
  • tools/run_checks_parallel.py --from-dir tests/checks/python-linux: 72/72.
  • git merge-tree --write-tree origin/main HEAD exits 0; git diff --check clean; zero personal paths in the diff.

The C++ executions (ir/controls/hardware) run in the WIN-VERIFY round; this host cannot compile the TU or run Windows CTest.

WIN-VERIFY fix round (b56e7b99)

Windows verification of 2a5d110e passed native_fract_f64_controls and caught 2 bugs, both fixed here:

  1. Wrong clamped high word. 0x3FEFFFFF (1.0 - 1ulp high word) is 1072693247, not 1071644671 (0x3FDFFFFF, ~0.5). Fixed in sub_neg_hi (src/gfx1013_direct_compute.inl), in the ir/wave64/controls needles (tests/native_fract_f64_check.py) and in docs/experiments/native-direct-fract-f64.md, with a comment pinning the value ((0xFFFFFFFF, 0x3FEFFFFF) = 1 - 2^-53). The Python model already used the correct hex constant, which is why the Linux model passed while the emitted DXIL constant was wrong.
  2. Hardware readback buffer size. native_store_replay --direct returns an 8256-byte (2064-DWORD) buffer (8 header guards + 64 * 32 + 8 tail words). OUTPUT_WORDS is now 2064 (tests/native_fract_f64_check.py:46).

With these two fixes, Windows dev and release both pass 3/3 including hardware.

Merge with upstream/main (1de4c6b3)

Windows verification of b56e7b99 passed dev and release 3/3, including native_fract_f64_hardware on RTX 4050, and all Python checks. PR #3636 then merged into upstream/main (d5970858), causing a 1-line conflict in clear_vector_spills (src/gfx1013_compute_native.cpp): the merge keeps both v_frexp_mant_f64 (upstream) and v_fract_f64 (this branch) in the pair list. src/gfx1013_direct_compute.inl merged cleanly; the v_fract_f64 lowering is untouched. Linux on the merge: model passes, form/docs/bidi pass (193 files/745 links, 2766 files), diff --check clean, merge-tree exits 0.

Windows Verification (1de4c6b3)

  • Commit: 1de4c6b3f864ddb07d1f232102b9a5dd8cb18979 (git merge-tree --write-tree upstream/main HEAD exit 0, git diff --check exit 0).
  • Adapter: NVIDIA GeForce RTX 4050 Laptop GPU (vendor=10de device=28e1, driver 32.0.15.9186).
  • Commands (XBOX2WINDOWS_ADAPTER=NVIDIA):
cmake --build --preset dev --target compile_dxil native_compute_ir_dump native_store_replay --parallel
ctest --preset dev --no-tests=error -R "^native_fract_f64_" --output-on-failure
cmake --build --preset release --target compile_dxil native_compute_ir_dump native_store_replay --parallel
ctest --preset release --no-tests=error -R "^native_fract_f64_" -E "cpu_native_archive_" --output-on-failure
  • dev: 3/3 passed in 4.16 s (native_fract_f64_ir 0.83 s, native_fract_f64_controls 1.45 s, native_fract_f64_hardware 1.77 s — 4/4 dispatches matched 2064 words each).
  • release: 3/3 passed in 4.27 s (native_fract_f64_ir 0.62 s, native_fract_f64_controls 1.42 s, native_fract_f64_hardware 2.05 s).
  • Checks: native_fract_f64_check.py model (74 values x abs/neg/absneg, 12 mutations killed), form_coverage_check.py, docs_links_check.py (193 files, 745 links), bidi_controls_check.py (2764 files) all passed.
  • AI-assisted.
**@⁠Ammmae0x** on GitHub · 2026-11-30 19:02 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3637) `Ammmae0x:shader/direct-fract-f64` → `main` · +930 −2 · 6 files · 3 commits · merged 2026-12-01 01:17 UTC Refs #2935, #3520 Lowers `v_fract_f64` (VOP1 `0x3e`, VOP3 `0x1be`) in the direct C++ compute path with pure 32-bit integer operations only: no DXIL `double`, no `i64` in `@main()`, no `fcmp`, and neither `DoublePrecisionFloatShaderOps` nor `Int64ShaderOps`. ## What changed - `src/gfx1013_direct_compute.inl`: new `float_alu` handler for `v_fract_f64`, ending with `return true;`. `fract(x) = x - floor(x)` is always in `[0, 1)`: NaN is quieted (`hi | 0x80000`), `±Inf` gives `+qNaN` (`0, 0x7FF80000`), `±0.0` and `|x| >= 2^52` give `+0.0` (`-0.0` becomes `+0.0`, matching `v_fract_f16`), and a positive `|x| < 1` passes unchanged (including subnormals). A negative `|x| < 1` gives `RNE(1 - |x|)` clamped strictly below `1.0` (`0x3FEFFFFFFFFFFFFF`): for `|x| <= 2^-54` `RNE(1 - |x|)` rounds to `1.0` and clamps to it, and for `2^-54 < |x| < 2^-53` `RNE(1 - |x|)` is already `0x3FEFFFFFFFFFFFFF`, so all `exp < 970` return it; for `970 <= exp <= 1021` the 53-bit significand is shifted right by `r = 1022 - exp` (`[1, 52]`, split `wide = r >= 32` like `v_ldexp_f64`), rounded to nearest `2^-53`, then subtracted from `2^53` (biased exponent always `1022`); for `exp == 1022` the exact diff `(0x00100000, 0) - (field, lo)` is normalized left. For `1023 <= exp <= 1074` the bottom `1075 - exp` fractional bits are extracted across `hi`/`lo`/masked `lo`; an exact integer gives `+0.0`, a positive fraction is normalized directly, and a negative fraction first takes the exact 64-bit `unit - frac` and then shares the same 6-step `{32, 16, 8, 4, 2, 1}` normalizer (biased exponent `exp - steps`). Every DXIL shift amount is clamped to `[0, 31]` in every lane. - Modifier gate refuses `pixel_stage`, `graphics_input`, `i.subword`, `i.dpp_control`, `permuted`, `i.clamp`, `i.scale` and the wide-form S1/S2 bits; MODE gate refuses a non-default rounding/denormal mode, with `v_fract_f64` added to `half_binary` in the MODE scan loop; S0/D must be 64-bit register pairs (VGPR pairs, aligned even SGPR pairs, integer `0` (`128`), inline f64 constants `240..247`); `abs` then `neg` act on bit 63 before the split. - `src/gfx1013_compute_native.cpp`: `v_fract_f64` added to the `pair` list in `clear_vector_spills`, so a symbolic lane spilled into either half refuses the pair write. ## Tests New `tests/native_fract_f64_check.py` (`model`/`ir`/`controls`/`hardware`) + `tests/cmake/native_fract_f64.cmake` + `tests/checks/python-linux/native_fract_f64.txt` + `docs/experiments/native-direct-fract-f64.md`. - `model`: 74 boundary values (`±0.0`, `±MIN_SUB`, `±2^-55`, `±2^-54`, `±(2^-54 + 1ulp)`, `±2^-53`, `±(2^-53 + 2^-54)`, `±(2^-53 + 1ulp)`, `±(0.5 - 2^-54)`, `±(0.5 - 2^-53)`, `±0.5`, `±(0.5 + 1ulp)`, `±0.75`, `±(1 - 1ulp)`, `±1.0`, `±1.25`, `±1.5`, `±(1 + 2^-52)`, cross-word `exp = 1042/1043/1044`, `±(2^52 - 1)`, `±(2^52 - 0.5)`, `±2^52`, `±DBL_MAX`, `±Inf`, `±sNaN`, `±qNaN` with payloads, `±(2^-52 + 2^-54)`) x plain/abs/neg/abs+neg against an independent exact `Fraction` RNE reference (`min(round_exact(Fraction(x) - floor(Fraction(x))), 0x3FEFFFFFFFFFFFFF)`); 12 self-mutations killed (`clamp`, `neg_zero`, `tiny_round`, `rndne_tie`, `exp1022`, `int_zero`, `neg_sub`, `unit_hi`, `unit_lo`, `pos_neg`, `inf_nan`, `absneg`). Spot checks: `-(0.5 - 2^-54)` gives `0.5`, `-(2^-53 + 2^-54)` gives `1.0 - 2^-52`. - `ir`: boundary values + seeded sweep of 100 random doubles in one e32 dump (348 stores), e64 forms (SGPR pairs, `128`, `240..247`, abs/neg/abs+neg) in a second dump, and the boundary values as a wave64 container (text-only: `{i32 64, i32 1, i32 1}`, no `@waveExchange`, no `double`/`i64`/`fcmp`); exact DXIL needles (`1072693247`, `1071644672`, `2146959360`, `2097152`, `1048575`, `2147483648`). - `controls`: compiles the four hardware programs and checks `@main()` has no `double`, `i64 ` or `fcmp`; 20 forms refused with exact error substrings, including symbolic spills in `v10` and `v11`; the truncating (`x - trunc(x)`), flushing-subnormal and uncapped-`1.0` references each differ. - `hardware`: 4 programs x 4 cases x 2 words on a 32-byte case stride, NaNs compared by class, 77 on missing caps. ## Linux verification - `tests/native_fract_f64_check.py model`: 74 values x abs/neg/absneg matched the Fraction reference; 12 self-mutations killed. - The C++ lowering was additionally transliterated to Python and checked against the same Fraction reference over every exponent (0..2046 x 4 random mantissas) and 20,000 random doubles, 0 mismatches, with every DXIL shift amount asserted in `[0, 31]` in every lane. - `tests/form_coverage_check.py`, `tests/docs_links_check.py` (188 files / 740 links), `tests/bidi_controls_check.py` (2744 files): pass. - `tools/run_checks_parallel.py --from-dir tests/checks/python-linux`: 72/72. - `git merge-tree --write-tree origin/main HEAD` exits 0; `git diff --check` clean; zero personal paths in the diff. The C++ executions (`ir`/`controls`/`hardware`) run in the WIN-VERIFY round; this host cannot compile the TU or run Windows CTest. ## WIN-VERIFY fix round (`b56e7b99`) Windows verification of `2a5d110e` passed `native_fract_f64_controls` and caught 2 bugs, both fixed here: 1. **Wrong clamped high word.** `0x3FEFFFFF` (`1.0 - 1ulp` high word) is `1072693247`, not `1071644671` (`0x3FDFFFFF`, ~0.5). Fixed in `sub_neg_hi` (`src/gfx1013_direct_compute.inl`), in the `ir`/`wave64`/`controls` needles (`tests/native_fract_f64_check.py`) and in `docs/experiments/native-direct-fract-f64.md`, with a comment pinning the value (`(0xFFFFFFFF, 0x3FEFFFFF) = 1 - 2^-53`). The Python model already used the correct hex constant, which is why the Linux model passed while the emitted DXIL constant was wrong. 2. **Hardware readback buffer size.** `native_store_replay --direct` returns an 8256-byte (2064-DWORD) buffer (8 header guards + `64 * 32` + 8 tail words). `OUTPUT_WORDS` is now `2064` (`tests/native_fract_f64_check.py:46`). With these two fixes, Windows `dev` and `release` both pass 3/3 including hardware. ## Merge with upstream/main (`1de4c6b3`) Windows verification of `b56e7b99` passed `dev` and `release` 3/3, including `native_fract_f64_hardware` on RTX 4050, and all Python checks. PR #3636 then merged into `upstream/main` (`d5970858`), causing a 1-line conflict in `clear_vector_spills` (`src/gfx1013_compute_native.cpp`): the merge keeps both `v_frexp_mant_f64` (upstream) and `v_fract_f64` (this branch) in the `pair` list. `src/gfx1013_direct_compute.inl` merged cleanly; the `v_fract_f64` lowering is untouched. Linux on the merge: model passes, form/docs/bidi pass (193 files/745 links, 2766 files), `diff --check` clean, merge-tree exits 0. ### Windows Verification (`1de4c6b3`) - Commit: `1de4c6b3f864ddb07d1f232102b9a5dd8cb18979` (`git merge-tree --write-tree upstream/main HEAD` exit `0`, `git diff --check` exit `0`). - Adapter: `NVIDIA GeForce RTX 4050 Laptop GPU` (`vendor=10de device=28e1`, driver `32.0.15.9186`). - Commands (`XBOX2WINDOWS_ADAPTER=NVIDIA`): ```text cmake --build --preset dev --target compile_dxil native_compute_ir_dump native_store_replay --parallel ctest --preset dev --no-tests=error -R "^native_fract_f64_" --output-on-failure cmake --build --preset release --target compile_dxil native_compute_ir_dump native_store_replay --parallel ctest --preset release --no-tests=error -R "^native_fract_f64_" -E "cpu_native_archive_" --output-on-failure ``` - `dev`: **3/3 passed** in `4.16 s` (`native_fract_f64_ir` `0.83 s`, `native_fract_f64_controls` `1.45 s`, `native_fract_f64_hardware` `1.77 s` — 4/4 dispatches matched 2064 words each). - `release`: **3/3 passed** in `4.27 s` (`native_fract_f64_ir` `0.62 s`, `native_fract_f64_controls` `1.42 s`, `native_fract_f64_hardware` `2.05 s`). - Checks: `native_fract_f64_check.py model` (74 values x abs/neg/absneg, 12 mutations killed), `form_coverage_check.py`, `docs_links_check.py` (193 files, 745 links), `bidi_controls_check.py` (2764 files) all passed. - AI-assisted. <!-- WIN-VERIFY: passed | sha=1de4c6b3f864ddb07d1f232102b9a5dd8cb18979 | targets=compile_dxil native_compute_ir_dump native_store_replay | ctest=^native_fract_f64_ | release=true -->
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 19:04:38 +00:00
The clamped high word for exp < 970 was 1071644671 (0x3FDFFFFF, ~0.5),
not 1072693247 (0x3FEFFFFF, 1.0 - 1ulp). Fix it in the lowering, the
ir/wave64/controls needles and the docs note, and pin it with a comment
((0xFFFFFFFF, 0x3FEFFFFF) = 1 - 2^-53). The hardware readback buffer is
2064 words (8 header guards + 64 * 32 + 8 tail words), not 2056.
Also clarify in the docs note that for |x| <= 2^-54 RNE(1 - |x|) rounds
to 1.0 and clamps, and for 2^-54 < |x| < 2^-53 it is already
0x3FEFFFFFFFFFFFFF, so all exp < 970 return it.
Resolves the 1-line conflict in clear_vector_spills (src/gfx1013_compute_native.cpp):
keep both v_frexp_mant_f64 (from upstream/main, PR #3634) and v_fract_f64
(from this branch) in the pair list. src/gfx1013_direct_compute.inl merges
cleanly; the v_fract_f64 lowering is untouched.
github manually merged commit cd1391eac0 into main 2026-12-01 01:19:30 +00:00
Author
Owner

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

Reviewed binary64 fract through the default MODE guard and paired 32-bit integer lowering. The fixture covers signs, the negative clamp below one, special values, paired register bounds, and the refusal paths. ctest --test-dir <build> --no-tests=error -j1 -R '^native_fract_f64_(ir|controls|hardware)$' --output-on-failure passed IR, refusal controls and RTX 4060 Laptop readback.

**@⁠R0Y00** on GitHub · 2026-12-01 01:14 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3637#pullrequestreview-6123433466) · **approved these changes** Reviewed binary64 fract through the default MODE guard and paired 32-bit integer lowering. The fixture covers signs, the negative clamp below one, special values, paired register bounds, and the refusal paths. `ctest --test-dir <build> --no-tests=error -j1 -R '^native_fract_f64_(ir|controls|hardware)$' --output-on-failure` passed IR, refusal controls and RTX 4060 Laptop readback.
This discussion has been locked. Commenting is limited to contributors.
No description provided.