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

Closed
slopgobblercobbler wants to merge 1 commit from upstream-pr/246 into main
slopgobblercobbler commented 2026-11-23 21:47:34 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by slopgobblercobbler on 2026-11-23T21:47:34Z.


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.
<!-- greyfinch-sync:ivall/xcl:issue:6569496119 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/246). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-11-23T21:47:34Z. --- 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.
upstream-sync closed this pull request 2026-11-24 06:57:49 +00:00
Kalmarv commented 2026-11-24 07:04:29 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T22:00:15Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7115199741 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/246#issuecomment-7115199741). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T22:00:15Z. --- 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
slopgobblercobbler commented 2026-11-24 07:04:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by slopgobblercobbler on 2026-11-23T22:09:27Z.


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!

<!-- greyfinch-sync:ivall/xcl:comment:7115397556 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/246#issuecomment-7115397556). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-11-23T22:09:27Z. --- 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!
PHeitor1 commented 2026-11-24 07:04:32 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by PHeitor1 on 2026-11-23T22:22:21Z.


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).

<!-- greyfinch-sync:ivall/xcl:comment:7115669872 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/246#issuecomment-7115669872). Originally posted by [PHeitor1](https://github.com/PHeitor1) on 2026-11-23T22:22:21Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
xcl-dev/xcl!243
No description provided.