Recomp: clamp 64-bit float results to [0.0, 1.0] instead of saturate (#105) #246

Closed
github-bot wants to merge 1 commit from gh-pr/246/head into gh-pr/246/base
Owner

@⁠slopgobblercobbler on GitHub · 2026-11-23 21:47 UTC · original
slopgobblercobbler:fix/recomp-fp64-clamp → main · +20 −2 · 2 files · 1 commits · closed without merging

Addresses #105 Item 4 (Recompiler 64-bit float clamp precision):
In crates/xcl/src/gpu/recomp/emit.rs, valu64 emitted saturate(double) when CLAMP=1. HLSL SM 6.6 does not have an intrinsic saturate overload for double, narrowing the argument to float and losing 29 bits of mantissa precision.

Replaces saturate(...) with clamp(..., 0.0, 1.0) on 64-bit floats, preserving full double precision across 64-bit float instructions (V_ADD_F64, V_MUL_F64, V_FMA_F64).

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs:
    • valu64 clamp emission uses clamp(expr, 0.0, 1.0).
  • crates/xcl/src/gpu/recomp/tests.rs:
    • valu64_clamp_and_omod_hlsl testing clamp and omod generation and validating compilation with DXC.

Tested

  • cargo test -p xcl -- recomp (172 passed, 0 failed).
  • Full workspace test suite: cargo test --workspace (603 passed, 0 failed).
  • Windows 11 x64, MSVC toolchain.

Not tested

  • Subnormal double denorm flushing modes.

AI use

  • Developed with Antigravity swarm coordinator & dedicated author/verifier agents (author-recomp-fp64, verify-recomp-fp64). Verified and reviewed independently.
**@⁠slopgobblercobbler** on GitHub · 2026-11-23 21:47 UTC · [original](https://github.com/xcl-dev/xcl/pull/246) `slopgobblercobbler:fix/recomp-fp64-clamp` → `main` · +20 −2 · 2 files · 1 commits · closed without merging Addresses #105 Item 4 (Recompiler 64-bit float clamp precision): In `crates/xcl/src/gpu/recomp/emit.rs`, `valu64` emitted `saturate(double)` when `CLAMP=1`. HLSL SM 6.6 does not have an intrinsic `saturate` overload for `double`, narrowing the argument to `float` and losing 29 bits of mantissa precision. Replaces `saturate(...)` with `clamp(..., 0.0, 1.0)` on 64-bit floats, preserving full double precision across 64-bit float instructions (`V_ADD_F64`, `V_MUL_F64`, `V_FMA_F64`). **What is in it** - `crates/xcl/src/gpu/recomp/emit.rs`: - `valu64` clamp emission uses `clamp(expr, 0.0, 1.0)`. - `crates/xcl/src/gpu/recomp/tests.rs`: - `valu64_clamp_and_omod_hlsl` testing clamp and omod generation and validating compilation with DXC. **Tested** - `cargo test -p xcl -- recomp` (172 passed, 0 failed). - Full workspace test suite: `cargo test --workspace` (603 passed, 0 failed). - Windows 11 x64, MSVC toolchain. **Not tested** - Subnormal double denorm flushing modes. **AI use** - Developed with Antigravity swarm coordinator & dedicated author/verifier agents (`author-recomp-fp64`, `verify-recomp-fp64`). Verified and reviewed independently.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:17:25 +00:00
github-bot closed this pull request 2026-11-26 04:17:25 +00:00
Author
Owner

@⁠Kalmarv on GitHub · 2026-11-23 22:00 UTC · original

The premise doesn't hold on the DXC versions I tried: saturate on a double compiles to the double-precision DXIL op, with no narrowing to float. Compiling double s = saturate(d); double c = clamp(d, 0.0, 1.0); as cs_6_6:

%12 = call double @dx.op.unary.f64(i32 7, double %11)  ; Saturate(value)
%13 = call double @dx.op.binary.f64(i32 35, double %11, double 0.000000e+00)  ; FMax(a,b)
%14 = call double @dx.op.binary.f64(i32 36, double %13, double 1.000000e+00)  ; FMin(a,b)
  • That listing is from SDK DXC 1.7.2308.16; the release DXC 1.9.2609.5 gives identical lines.
  • Neither has an fptrunc.
  • #119's description says the same: "DXIL has a double Saturate", and gpu_clamp_and_omod_per_instruction checks that the f64 clamp keeps double precision on main.
  • The two forms also agree on NaN. The DXIL spec gives Saturate(NaN) = 0, and FMax returns the non-NaN operand, so clamp(NaN, 0.0, 1.0) = FMin(0, 1) = 0. The change is harmless, but it doesn't change any result.
  • On the test: its DXC line also passes without the change. The same three-instruction program translated on main 60565c4 emits saturate(...) and compiles with both DXC versions, and so does this PR's clamp(..., 0.0, 1.0). Only the string assertions tell the two apart.

Tested

  • Windows 11, SDK DXC 1.7.2308.16 (what the module loads) and the official DXC 1.9.2609.5 release.
  • The test's program (V_ADD_F64 CLAMP, V_MUL_F64 OMOD, V_FMA_F64 OMOD + CLAMP), translated on main 60565c4 and on this PR's e120969:
errors Saturate.f64 FMax / FMin.f64 fptrunc
main, both compilers 0 2 0 0
this PR, both compilers 0 0 4 0
  • On an RTX 5070 Ti (610.74), main 60565c4 with #202 and this PR merged on top passes gpu::recomp::: 187 passed, 0 failed, 18 ignored, gpu_clamp_and_omod_per_instruction included.

Not tested

  • DXC 1.8.
  • A GPU comparison of the two forms on NaN, infinity or subnormal inputs. The NaN result above comes from the DXIL spec, not from a run.
  • AMD hardware and vkd3d-proton.

🤖 Claude Opus 5.5

**@⁠Kalmarv** on GitHub · 2026-11-23 22:00 UTC · [original](https://github.com/xcl-dev/xcl/pull/246#issuecomment-7115199741) The premise doesn't hold on the DXC versions I tried: `saturate` on a `double` compiles to the double-precision DXIL op, with no narrowing to float. Compiling `double s = saturate(d); double c = clamp(d, 0.0, 1.0);` as `cs_6_6`: ``` %12 = call double @dx.op.unary.f64(i32 7, double %11) ; Saturate(value) %13 = call double @dx.op.binary.f64(i32 35, double %11, double 0.000000e+00) ; FMax(a,b) %14 = call double @dx.op.binary.f64(i32 36, double %13, double 1.000000e+00) ; FMin(a,b) ``` - That listing is from SDK DXC 1.7.2308.16; the release DXC 1.9.2609.5 gives identical lines. - Neither has an `fptrunc`. - #119's description says the same: "DXIL has a double `Saturate`", and `gpu_clamp_and_omod_per_instruction` checks that the f64 clamp keeps double precision on main. - The two forms also agree on NaN. The DXIL spec gives `Saturate(NaN)` = 0, and `FMax` returns the non-NaN operand, so `clamp(NaN, 0.0, 1.0)` = `FMin(0, 1)` = 0. The change is harmless, but it doesn't change any result. - On the test: its DXC line also passes without the change. The same three-instruction program translated on main 60565c4 emits `saturate(...)` and compiles with both DXC versions, and so does this PR's `clamp(..., 0.0, 1.0)`. Only the string assertions tell the two apart. **Tested** - Windows 11, SDK DXC 1.7.2308.16 (what the module loads) and the official DXC 1.9.2609.5 release. - The test's program (`V_ADD_F64` CLAMP, `V_MUL_F64` OMOD, `V_FMA_F64` OMOD + CLAMP), translated on main 60565c4 and on this PR's e120969: | | errors | `Saturate.f64` | `FMax` / `FMin.f64` | `fptrunc` | |---|---|---|---|---| | main, both compilers | 0 | 2 | 0 | 0 | | this PR, both compilers | 0 | 0 | 4 | 0 | - On an RTX 5070 Ti (610.74), main 60565c4 with #202 and this PR merged on top passes `gpu::recomp::`: 187 passed, 0 failed, 18 ignored, `gpu_clamp_and_omod_per_instruction` included. **Not tested** - DXC 1.8. - A GPU comparison of the two forms on NaN, infinity or subnormal inputs. The NaN result above comes from the DXIL spec, not from a run. - AMD hardware and vkd3d-proton. 🤖 Claude Opus 5.5
Author
Owner

@⁠slopgobblercobbler on GitHub · 2026-11-23 22:09 UTC · original

Thanks @⁠Kalmarv for the thorough DXIL disassembly audit across SDK DXC 1.7 and DXC 1.9.

You're completely right—@⁠dx.op.unary.f64(i32 7, ...)\ emits a native 64-bit \Saturate\ in DXIL without narrowing, which is cleaner and produces 1 DXIL op instead of 2 (\FMax\ + \FMin.f64). We investigated the note from #105, but your DXIL trace clearly demonstrates that the existing \saturate\ emission on \main\ is already optimal and precise.

Closing this PR in light of your findings. Thank you!

**@⁠slopgobblercobbler** on GitHub · 2026-11-23 22:09 UTC · [original](https://github.com/xcl-dev/xcl/pull/246#issuecomment-7115397556) Thanks @⁠Kalmarv for the thorough DXIL disassembly audit across SDK DXC 1.7 and DXC 1.9. You're completely right—\@⁠dx.op.unary.f64(i32 7, ...)\ emits a native 64-bit \Saturate\ in DXIL without narrowing, which is cleaner and produces 1 DXIL op instead of 2 (\FMax\ + \FMin.f64\). We investigated the note from #105, but your DXIL trace clearly demonstrates that the existing \saturate\ emission on \main\ is already optimal and precise. Closing this PR in light of your findings. Thank you!
Author
Owner

@⁠PHeitor1 on GitHub · 2026-11-23 22:22 UTC · original

Tested on RTX 5060 Ti 8 GB, driver 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native), Rust 1.100.0 msvc, debug. cargo test --workspace --no-fail-fast -- --test-threads=4 on the head e120969: 622 passed, 0 failed, 40 ignored. SKIPPED: only the usual two no wave64 (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware). Note: the branch is 52 commits behind main (base 6da33fa), so the count is lower than main's 651; a rebase would make the numbers comparable.

Run and written with Claude Code (Claude Opus 5.5).

**@⁠PHeitor1** on GitHub · 2026-11-23 22:22 UTC · [original](https://github.com/xcl-dev/xcl/pull/246#issuecomment-7115669872) Tested on RTX 5060 Ti 8 GB, driver 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native), Rust 1.100.0 msvc, debug. `cargo test --workspace --no-fail-fast -- --test-threads=4` on the head `e120969`: **622 passed, 0 failed, 40 ignored**. SKIPPED: only the usual two `no wave64` (`gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`). Note: the branch is 52 commits behind `main` (base `6da33fa`), so the count is lower than main's 651; a rebase would make the numbers comparable. _Run and written with Claude Code (Claude Opus 5.5)._

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.