xshader: lower exact f64 rounding and f64/int32 conversions #99

Manually merged
github merged 1 commit from gh-pr/99/head into gh-pr/99/base 2026-11-26 06:31:04 +00:00
Owner

@⁠JohnVonWells on GitHub · 2026-11-24 22:17 UTC · original
xshader/exact-f64-rounding → main · +5017 −0 · 5 files · 1 commits · merged 2026-11-24 22:19 UTC

Follow-up to #95, from the same synthetic opcode sweep. This PR takes the double-precision instructions whose results are exactly defined:

Kind Instructions
Round to integral v_trunc_f64, v_floor_f64, v_ceil_f64, v_rndne_f64
f64 to int32 v_cvt_i32_f64, v_cvt_u32_f64
int32 to f64 v_cvt_f64_i32, v_cvt_f64_u32

Semantics. LLVM's gfx1030 backend selects exactly these single instructions for llvm.floor/ceil/trunc/roundeven.f64, llvm.fptosi.sat/fptoui.sat.i32.f64 and sitofp/uitofp i32 to double. So rounding is IEEE (ties to even for rndne), and the conversions to int32 truncate, saturate and give 0 for NaN.

Lowering (new hlsl/float64.rs)

  • Like the existing f64 comparisons in compare.rs, it works on uint2 (low, high) pairs with bit operations. No host FP64 is needed and every result is bit-exact.
  • Rounding honours the f64 input denormal bit of the mode register (tb_fp_mode & 0x40), the same convention as compare_float.
  • NaN results are quieted with their payload kept. Infinities pass through.
  • Conservative scope: VGPR operands only, VOP1 or VOP3 without abs, neg, clamp or omod, compute shaders only.
  • Pair bookkeeping: live_in now counts the high register of an f64 source pair, and pair results mark both destination registers written, both in the live-in analysis and in the symbolic VGPR map.

Verification

  • Unit tests decode LLVM gfx1030 words and check both encodings, the denormal-mode path and the refusals.
  • tools/check_f64_exact_gpu.py runs three LLVM-assembled programs (VOP1, VOP3, and VOP1 after s_denorm_mode 0):
    • Every destination high register holds a stale value first, and one v_rndne_f64 aliases its source pair.
    • Inputs: 62 edge values (±0, denormals, ties, 2^52/2^53 boundaries, int32 limits ±½, ±inf, quiet and signalling NaNs) plus random patterns.
    • The reference is an independent model on Python floats (math.floor/ceil/trunc, round), not the HLSL bit algorithm. Hand-computed values for it are in tests/test_f64_exact_gpu.py.
  • Result on an AMD Radeon RX 6950 XT (1002:73a5, RADV): 576 inputs, 9,216 results, 27,648 GPU words, zero mismatches. The receipt is evidence/f64-exact-control.json.
  • The emitter before this change refuses all three programs (--baseline). For every instruction, a negated or clamped form and an SGPR source are refused.
  • Negative control: I rebuilt the emitter with rndne ties always rounding up. 16 of 72 dispatches then failed, only in the two rndne output columns (for example 2.5 → 3.0 instead of 2.0).
  • Unchanged: all 128 shader-compiler tests and all 190 Python tests pass.

Assumption: NaN quieting (signalling NaN in, quiet NaN with the same payload out) follows IEEE-754 and LLVM's constant folding. I could not observe the hardware's own NaN output for these instructions.

AI-assisted.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NhDbTJdTcjebsL5u7pwFoY

**@⁠JohnVonWells** on GitHub · 2026-11-24 22:17 UTC · [original](https://github.com/brcly/TuxBox/pull/99) `xshader/exact-f64-rounding` → `main` · +5017 −0 · 5 files · 1 commits · merged 2026-11-24 22:19 UTC Follow-up to #95, from the same synthetic opcode sweep. This PR takes the double-precision instructions whose results are exactly defined: | Kind | Instructions | |---|---| | Round to integral | `v_trunc_f64`, `v_floor_f64`, `v_ceil_f64`, `v_rndne_f64` | | f64 to int32 | `v_cvt_i32_f64`, `v_cvt_u32_f64` | | int32 to f64 | `v_cvt_f64_i32`, `v_cvt_f64_u32` | **Semantics.** LLVM's gfx1030 backend selects exactly these single instructions for `llvm.floor/ceil/trunc/roundeven.f64`, `llvm.fptosi.sat`/`fptoui.sat.i32.f64` and `sitofp`/`uitofp i32 to double`. So rounding is IEEE (ties to even for `rndne`), and the conversions to int32 truncate, saturate and give 0 for NaN. **Lowering** (new `hlsl/float64.rs`) - Like the existing f64 comparisons in `compare.rs`, it works on `uint2` (low, high) pairs with bit operations. No host FP64 is needed and every result is bit-exact. - Rounding honours the f64 input denormal bit of the mode register (`tb_fp_mode & 0x40`), the same convention as `compare_float`. - NaN results are quieted with their payload kept. Infinities pass through. - **Conservative scope:** VGPR operands only, VOP1 or VOP3 without abs, neg, clamp or omod, compute shaders only. - **Pair bookkeeping:** `live_in` now counts the high register of an f64 source pair, and pair results mark both destination registers written, both in the live-in analysis and in the symbolic VGPR map. **Verification** - Unit tests decode LLVM gfx1030 words and check both encodings, the denormal-mode path and the refusals. - `tools/check_f64_exact_gpu.py` runs three LLVM-assembled programs (VOP1, VOP3, and VOP1 after `s_denorm_mode 0`): - Every destination high register holds a stale value first, and one `v_rndne_f64` aliases its source pair. - Inputs: 62 edge values (±0, denormals, ties, `2^52`/`2^53` boundaries, int32 limits ±½, ±inf, quiet and signalling NaNs) plus random patterns. - The reference is an independent model on Python floats (`math.floor`/`ceil`/`trunc`, `round`), not the HLSL bit algorithm. Hand-computed values for it are in `tests/test_f64_exact_gpu.py`. - **Result on an AMD Radeon RX 6950 XT** (`1002:73a5`, RADV): 576 inputs, 9,216 results, 27,648 GPU words, zero mismatches. The receipt is `evidence/f64-exact-control.json`. - The emitter before this change refuses all three programs (`--baseline`). For every instruction, a negated or clamped form and an SGPR source are refused. - **Negative control:** I rebuilt the emitter with `rndne` ties always rounding up. 16 of 72 dispatches then failed, only in the two `rndne` output columns (for example 2.5 → 3.0 instead of 2.0). - Unchanged: all 128 shader-compiler tests and all 190 Python tests pass. **Assumption:** NaN quieting (signalling NaN in, quiet NaN with the same payload out) follows IEEE-754 and LLVM's constant folding. I could not observe the hardware's own NaN output for these instructions. AI-assisted. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01NhDbTJdTcjebsL5u7pwFoY
Add v_trunc_f64, v_floor_f64, v_ceil_f64, v_rndne_f64, v_cvt_i32_f64,
v_cvt_u32_f64, v_cvt_f64_i32 and v_cvt_f64_u32. Their results are
exact: LLVM gfx1030 selects exactly these instructions for
llvm.floor/ceil/trunc/roundeven.f64, llvm.fptosi/fptoui.sat.i32.f64 and
sitofp/uitofp i32 to double.

Like the f64 comparisons, the lowering works on (low, high) uint2 pairs
with bit operations, so no host FP64 support is needed. Rounding honours
the f64 input denormal bit of the mode register, as the comparisons do.
NaN results are quieted; conversions to int32 saturate and give 0 for
NaN. VGPR operands only, VOP1 or VOP3 without modifiers, compute shaders
only.

The live-in analysis now counts the high register of an f64 source
pair, and pair results invalidate both destination registers.

tools/check_f64_exact_gpu.py runs VOP1, VOP3 and flushed-denormal
variants on a GPU against a model built on Python floats, with stale
destination high registers and an aliased source pair.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NhDbTJdTcjebsL5u7pwFoY
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:31:02 +00:00
github manually merged commit f80dff36ff into gh-pr/99/base 2026-11-26 06:31:04 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.