Skip to content

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

Closed
slopgobblercobbler wants to merge 1 commit into
ivall:mainfrom
slopgobblercobbler:fix/recomp-fp64-clamp
Closed

slopgobblercobbler wants to merge 1 commit into
ivall:mainfrom
slopgobblercobbler:fix/recomp-fp64-clamp

Conversation

@slopgobblercobbler

Copy link
Copy Markdown
Contributor

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.

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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.
  • recomp: omod on f64 and f32/f16 conversions, integer clamp on unsigned/packed adds and dots #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

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

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants