Skip to content

Recompiler: v_frexp_mant_f32 / _f16 keep the input's sign (#580) - #587

Open
requiemsnkrp wants to merge 1 commit into
xcl-dev:mainfrom
requiemsnkrp:recomp-frexp-mant-sign
Open

requiemsnkrp wants to merge 1 commit into
xcl-dev:mainfrom
requiemsnkrp:recomp-frexp-mant-sign

Conversation

@requiemsnkrp

@requiemsnkrp requiemsnkrp commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

frexp_mant returned HLSL frexp()'s mantissa, which is unsigned, so v_frexp_mant_f32 gave a positive mantissa for a negative input (−10.125 → +0.6328125) and +0 for −0. The ISA keeps the sign, as XCL's f64 path (frexp_mant_d) already does. The helper now ORs the input's sign bit back in; v_frexp_mant_f16 goes through the same helper. The diagnosis and the one-line fix are @joeypercia's (#580), who has no title for the regression run; they are credited with a Co-authored-by trailer. Fixes #580.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: frexp_mant keeps the input's sign
  • crates/xcl/src/gpu/recomp/tests.rs: gpu_frexp_mant_f32_f16_keep_the_sign runs v_frexp_mant_f32 (VOP1, and VOP3 with NEG), v_frexp_exp_i32_f32 and v_frexp_mant_f16 on negative inputs, −0, +0 and −inf, bit for bit

Tested

  • NVIDIA GeForce RTX 3060 12 GB, driver 621.24 (32-lane waves); AMD Ryzen 5 5600X; Windows 11 Pro 26200; Rust 1.100.0 msvc; DXC 1.9.2602.17; debug for the tests, release for the regression runs
  • the new test on main 9ce2070 without the fix: fails on the first negative input (v_frexp_mant_f32 0xc1220000: got 0x3f220000, want 0xbf220000); with the fix: 3 of 3 passed. The three frexp tests pass
  • cargo test --workspace -- --test-threads=4 on d6d7906: 685 passed, 0 failed, 41 ignored
  • SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware

Regression run

main:

xcl-smoke v1
commit: 9ce2070eccf436d7bd38601c90407da447df34f3
gpu: NVIDIA GeForce RTX 3060
title: 2c508222
seconds: 60
exit: 0x0
frames shown: 1531 (PresentX 1531)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14603 MB

PR:

xcl-smoke v1
commit: d6d790653f64c4a8805115c9dfbd69ffbb757506
gpu: NVIDIA GeForce RTX 3060
title: 2c508222
seconds: 60
exit: 0x0
frames shown: 1490 (PresentX 1490)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14823 MB

Not tested

  • the f16 case failing before the fix: the test stops at the first f32 assertion; with the fix the f16 assertions pass
  • whether the title's shaders use v_frexp_mant_f32 / _f16 on negative inputs in those 60 s
  • AMD / wave64 hardware (joeypercia's RDNA2 numbers in Recompiler: v_frexp_mant_f32 drops the sign of negative inputs #580 cover the fix there with their randomized test)
  • joeypercia's gpu_f32_math_and_bit_scans_random (21 more instructions) is not included; it belongs in its own test PR

AI use

  • Claude Code with Claude Opus 5.5: wrote the test and this description, applied joeypercia's fix, and ran the tests and the regression runs on the machine above; opened at my request.

`frexp_mant` returned HLSL frexp()'s mantissa, which is unsigned: a
negative input gave a positive mantissa (-10.125 -> +0.6328125) and -0
gave +0. The ISA keeps the sign, as XCL's f64 path (`frexp_mant_d`)
already does. The helper now ORs the input's sign bit back in;
v_frexp_mant_f16 goes through the same helper. Fix and diagnosis by
joeypercia in xcl-dev#580.

The test runs v_frexp_mant_f32 (also with the NEG modifier),
v_frexp_exp_i32_f32 and v_frexp_mant_f16 on negative inputs, -0, +0 and
-inf: before, it failed on the first negative input (+0.6328125 for
-10.125); after, it passes.

Fixes xcl-dev#580

Co-authored-by: joeypercia <81598667+joeypercia@users.noreply.github.com>
Assisted-by: Claude Code (Claude Opus 5.5)
@CryingLaughing

Copy link
Copy Markdown

I checked the f16 negative control listed under Not tested. On the RTX 4080, the same V_FREXP_MANT_F16 inputs lose their signs with the parent emitter and pass with the exact PR head d6d790653f64c4a8805115c9dfbd69ffbb757506.

f16 input bits Parent emitter result Expected mantissa
0xc910 0x3910 0xb910
0xbc00 0x3800 0xb800
0x8000 0x0000 0x8000

Tested

  • RTX 4080, NVIDIA driver 621.01, Core i5-13600K, Windows 11 25H2 build 26200.9974, Rust 1.100.0 MSVC x64, DXC 1.8.2502.11, debug. XCL_ADAPTER=4080, XCL_REQUIRE_GPU=1, synthetic fixtures, all XCL_SAMPLE_* variables unset. Separate build directories.
  • cargo test -p xcl --lib --locked -j 4 gpu_frexp_mant_f32_f16_keep_the_sign -- --show-output passed 1/1 on the clean PR head, including all f16 and f32 assertions.
  • The same command with emit.rs restored to parent 9ce2070eccf436d7bd38601c90407da447df34f3 reported 0 passed, 1 failed. The control evaluates all f16 outputs before the first f32 assertion, with identical shader instructions and input bits. The log showed sign loss for all three negative input cases and counted 48 mismatches across 64 lanes. Both focused runs executed without a capability skip.

Not tested

  • Full workspace for this PR head, denormals, other adapters, release builds, or retail smoke on this machine.

AI use

  • OpenAI Codex (GPT-6 family, exact variant unavailable) prepared the local control, ran the commands, and drafted this report.

@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of d6d790653f64c4a8805115c9dfbd69ffbb757506 on AMD RDNA4 (native wave64); the PR and the follow-up check were on NVIDIA. The branch is based on 9ce2070, so I merged it onto current main cb868b2 locally (clean merge, emit.rs and tests.rs) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4) on the merge: 686 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 646 passed, 0 failed, 21 ignored
  • 0 SKIPPED; gpu_frexp_mant_f32_f16_keep_the_sign passes, as do gpu_frexp_f64_exp_and_mant and frexp_f64_instructions
  • negative control on this adapter: with only frexp_mant in emit.rs back to frexp(x, e) (not committed), the new test fails at lane 0 v_frexp_mant_f32 0xc1220000 with 0x3F220000 instead of 0xBF220000, the same failure the description reports on NVIDIA

Review: ORing the input's sign bit into HLSL frexp()'s mantissa gives the ISA result for every case the test covers: negative normals, −0 (→ −0), +0, and ±inf / NaN, which skip frexp and pass through as before. The VOP3 NEG case checks the modifier is applied before the helper, and the f16 path reaches the same helper through its f32 conversion, so −0 and the signs survive there too. It matches what frexp_mant_d already does for f64. I found nothing to change. Not covered by the test (and not touched by the change): denormal inputs and the sign of a NaN.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the PR's head on its old base was not run, only its merge onto main.

Run and report by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

@alpz1000

Copy link
Copy Markdown

Independent check of the f16 before-fix case listed under Not tested. RTX 3060 12 GB (driver 610.88), Core i5-8400, Windows 11 Home 26200.9974, Rust 1.100.0 x86_64-pc-windows-msvc, SDK DXC 1.9.2602.17, debug; XCL_ADAPTER=3060.

  • Unmodified PR head d6d790653f64c4a8805115c9dfbd69ffbb757506: cargo test --locked -p xcl --lib frexp -- --test-threads=1 --nocapture passed all 3 tests, no skips.
  • On the same head, I temporarily restored only emit.rs from its parent and removed the three f32 assertions from the new test so the f16 assertion could run first. cargo test --locked -p xcl --lib gpu_frexp_mant_f32_f16_keep_the_sign -- --test-threads=1 --nocapture failed on lane 0 for v_frexp_mant_f16 input 0xc910: got 0x3910 (14608), expected 0xb910 (47376). This independently reproduces the sign loss for f16 as well. Temporary changes were reverted.

Not tested: full workspace or retail titles. AI use: OpenAI Codex (GPT-6) ran both checks and drafted this report.

@zhouzhidong3-hash

zhouzhidong3-hash commented Nov 24, 2026 •

Copy link
Copy Markdown

Independent RTX 5070 verification of XCL PR #587

Tested on Windows 10 build 19045, Intel i7-12700KF, NVIDIA RTX 5070 driver 591.86, Rust 1.100.0 MSVC debug build and DXC 1.8.2502.11 with a local D3D12 Agility SDK.

Base implementation: 9ce2070. Used the PR regression harness on the parent emitter. For the separate half-precision run, only the preceding f32 assertions were gated; the program and GPU input state were identical.

Instruction Input bits Base actual Expected
v_frexp_mant_f32 0xc1220000 0x3f220000 0xbf220000
v_frexp_mant_f16 0xc910 0x3910 0xb910

Head: d6d7906. With the original, unmodified test and emitter restored, the combined bitwise GPU regression passed (1 passed, 0 failed, no skip). No tracked source differences from HEAD after verification.

Workspace: cargo build --locked --workspace succeeded. cargo test --locked --workspace -- --test-threads=4 --nocapture reported 685 passed, 0 failed, 41 ignored. The harness counts runtime skips among passes. Two wave64 tests and two D3D12 debug-layer tests explicitly reported SKIPPED. Optional sample-dependent tests lacked retail inputs. No retail game smoke run was performed. This evidence covers instruction semantics on this GPU.

Logs: base-f32.log, base-f16.log, head-frexp.log, head-build.log, head-suite.log. Local harness: verify.py. AI assistance: Codex (GPT-6.2 Astra) prepared the local assertion gate, executed the comparison and summarized the logs.

Additional edge observations on the same RTX 5070 and PR head. A synthetic kernel executed the unmodified VOP1 f32 and f16 mantissa paths on eight input pairs repeated over 64 lanes. Every repeated lane agreed. Hex digits below are raw bits.

Inputs f32 result f16 result (low 16 bits)
Smallest positive / negative subnormal 00000000 / 80000000 3800 / b800
Largest positive / negative subnormal 00000000 / 80000000 3bfe / bbfe
Smallest positive / negative normal 3f000000 / bf000000 3800 / b800
Quiet NaNs (f32 7fc01234 / ffc01234, f16 7e11 / fe11) 7fc01234 / ffc01234 7fff / 7fff

These are observations of this build. No comparison with native guest instructions or alternate denormal modes was performed, so this alone does not establish a regression or an ISA mismatch.

Parent-emitter control for the edge table: same synthetic kernel and inputs on the same adapter, with only emit.rs switched between 9ce2070 and d6d7906. Both runs executed. For each precision, only the three negative finite inputs changed (smallest and largest subnormal, smallest normal), and each output changed by exactly the sign bit. Both f16 quiet NaNs already returned 0x7fff on the parent. The f32 subnormal magnitudes were already zero there. This isolates the PR effect on these inputs.

@milesah3ad

Copy link
Copy Markdown

The direct DXIL backend counterpart of this fix, from @regiumlepidi's parity sweep on #1269: gpu_frexp_mant_f32_f16_keep_the_sign pass with XCL_DIRECT_DXIL=1 with the patch below. Without it they fail on the direct path.

frexp_mant in gpu/dxil/prelude.rs takes its mantissa from frexp_dxc, which, like DXC's frexp, drops the sign. So the direct path gave 0x3F220000 for 0xC1220000 (your test's lane 0) where the console gives 0xBF220000. The patch ORs the input's sign back in, as your HLSL change does and as frexp_mant_d already does for f64. The f16 form goes through the same helper.

How to take it: this PR predates the direct backend on main, so rebase it onto main first (your commit rebases cleanly onto 7994cd1), then git am the patch below. Feel free to fold it into your PR with a Co-Authored-By: milesah3ad <360542007+milesah3ad@users.noreply.github.com> line. I didn't open a separate PR, so this doesn't add another review.

patch (1 commit, for git am)
From af7b26c7057f24ba2d1529efded73b027fe3e426 Mon Sep 17 00:00:00 2001
From: milesah3ad <360542007+milesah3ad@users.noreply.github.com>
Date: Tue, 24 Nov 2026 18:51:25 -0600
Subject: [PATCH] Direct DXIL: v_frexp_mant_f32 / _f16 keep the input's sign
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

frexp_mant took its mantissa from frexp_dxc, which, like DXC's frexp,
drops the sign, so a negative input came back positive (0xC1220000 gave
0x3F220000, not 0xBF220000). The ISA's mantissa keeps the sign, as
frexp_mant_d already does for f64 and as #587 makes the HLSL path do.
The sign bit is ORed back in; ┬▒inf and NaN still return the input.

gpu_frexp_mant_f32_f16_keep_the_sign now passes with XCL_DIRECT_DXIL=1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YPhfiWsdJcDBLYSS8edLyS
---
 crates/xcl/src/gpu/dxil/prelude.rs | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/crates/xcl/src/gpu/dxil/prelude.rs b/crates/xcl/src/gpu/dxil/prelude.rs
index a171fd0..9b2e8e0 100644
--- a/crates/xcl/src/gpu/dxil/prelude.rs
+++ b/crates/xcl/src/gpu/dxil/prelude.rs
@@ -84,8 +84,14 @@ impl Lw {
         let mf = self.sel(nz, mf, z);
         (mf, ef)
     }
+    /// The ISA's mantissa keeps the input's sign (as `frexp_mant_d` does); DXC's `frexp` drops it.
     pub fn frexp_mant(&mut self, x: Value) -> Value {
         let (m, _) = self.frexp_dxc(x);
+        let mb = self.bits(m);
+        let xb = self.bits(x);
+        let sign = self.andc(xb, 0x8000_0000);
+        let mb = self.or(mb, sign);
+        let m = self.f(mb);
         let a = self.isinf(x);
         let b = self.isnan(x);
         let s = self.lor(a, b);
-- 
2.48.1.windows.1

Tested

Not tested: wave64 hardware (the wave64 cases SKIP here), AMD, any title, the full workspace suite (lib only).

AI use: Claude Code (Claude Opus 5.5) wrote the fix and ran the tests on the machine above. I checked the results.

@raylitebull

Copy link
Copy Markdown
Member

Holding merge on d6d7906 until the direct-DXIL counterpart pointed out by @milesah3ad (frexp_mant in crates/xcl/src/gpu/dxil/prelude.rs also drops the sign bit under XCL_DIRECT_DXIL=1) is folded in after a quick rebase onto main.

@zhouzhidong3-hash

Copy link
Copy Markdown

RTX 5070 reproduction of the Direct DXIL sign issue, using a modern tree.

Base tree: 7a1f5a2e6541c9fa0ef921e319fa81a2fdc2e42f (#1994). This is not a test of a rebased #587 head. Temporarily copied the unchanged gpu_frexp_mant_f32_f16_keep_the_sign test from #587 d6d7906 into that tree. Then applied only #587's HLSL frexp_mant expression change in gpu/recomp/emit.rs, leaving gpu/dxil/prelude.rs unchanged.

Windows x64, RTX 5070 driver 591.86, Rust 1.100.0 MSVC debug, DXC/DXIL 1.8.2502.11, local Agility SDK, explicit XCL_ADAPTER=5070.

Command: cargo test --locked -p xcl --lib gpu_frexp_mant_f32_f16_keep_the_sign -- --test-threads=1 --nocapture.

Temporary runtime configuration HLSL (XCL_DIRECT_DXIL=0) Direct DXIL (all)
Original modern tree, added test only 0 passed, 1 failed 0 passed, 1 failed
Same tree plus #587 HLSL fix only 1 passed, 0 failed 0 passed, 1 failed

All three failing runs stop at lane 0, v_frexp_mant_f32 input 0xc1220000: actual 0x3f220000, expected 0xbf220000. No test skips. The Direct DXIL run still returns the positive mantissa after the HLSL-only change makes its control pass.

The failing assertion precedes the f16 assertions, so these failing runs do not establish f16 behavior. No proposed Direct DXIL fix, full suite, Release, native RDNA2 or retail validation in this experiment. All temporary source edits were restored and git diff --exit-code returned 0.

Local logs: sign-0.log, sign-all.log, sign-hlslfix-0.log, sign-hlslfix-all.log under I:\neospace\work\xcl-1994-5070.

AI assistance: OpenAI Codex moved the existing regression test temporarily, applied the existing HLSL expression as a control, ran the tests, restored source and drafted this report.

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.

Recompiler: v_frexp_mant_f32 drops the sign of negative inputs

7 participants