Skip to content

Recompiler: v_frexp_mant_f32 drops the sign of negative inputs #580

Done

Description

@joeypercia

v_frexp_mant_f32 returns the mantissa without its sign: for a negative input the result is positive (−10.125 gives +0.6328125 / 0x3f220000, want −0.6328125 / 0xbf220000), and −0 gives +0. The RDNA2 instruction keeps the sign, as XCL's own f64 path does (frexp_mant_d in emit.rs and the "CPU mirror of the ISA semantics" in gpu_frexp_f64_exp_and_mant both set it).

Cause: the f32 helper in emit.rs (main 508a0c7, line 5467) returns HLSL frexp()'s mantissa, which is unsigned:

float frexp_mant(float x) { float e; return (isinf(x) || isnan(x)) ? x : frexp(x, e); }

v_frexp_mant_f16 calls the same helper, so it probably has the same bug (not tested).

Fix, one line:

float frexp_mant(float x) { float e; return (isinf(x) || isnan(x)) ? x : asfloat(asuint(frexp(x, e)) | (asuint(x) & 0x80000000u)); }

How it was found: a new randomized GPU test, gpu_f32_math_and_bit_scans_random in gpu/recomp/tests.rs (145 lines, modelled on gpu_f64_math_random). It checks 22 instructions that no test named so far against the CPU: v_floor/ceil/trunc/rndne/fract_f32, v_cvt_i32/flr_i32/rpi_i32_f32, v_frexp_mant/exp_i32_f32 bit for bit; v_rcp/sqrt/rsq_f32 within 4 ulps; v_exp/log/sin/cos_f32 within a tolerance that only catches a wrong formula; v_ffbh_u32, v_ffbl_b32, v_ffbh_i32, v_bfrev_b32, v_bcnt_u32_b32 exactly. Only frexp_mant fails; the other 21 match.

Measured on main 508a0c7, Windows 11 Pro 26200, Ryzen 7 7800X3D, Rust 1.100.0 MSVC, debug, DXC 1.8.2502.11 (SDK 10.0.26100):

RTX 4070 Ti (616.56, wave32) Radeon iGPU, RDNA2 (26.11.2, wave64 + wave32)
new test on main fails: 660 distinct results, all frexp_mant fails: 1,267 distinct results, all frexp_mant
new test + fix, XCL_FUZZ_ITERS=200 0 failures (6,398 lanes) 0 failures (12,796 wave64 + 6,398 wave32 lanes)
cargo test --workspace --no-fail-fast -- --test-threads=4, test + fix 681 passed, 0 failed, 41 ignored 681 passed, 0 failed, 41 ignored
SKIPPED 2 (no wave64) 2 (no mesh shaders)

Worst errors with the fix: rcp / sqrt / rsq 1 ulp on both; exp / log relative ≤ 1.6e-7; sin / cos absolute ≤ 9.6e-5 (NVIDIA), 1.2e-7 (AMD).

Not tested: a regression run on a title (I have none, which is why this is an issue and not a PR: the fix is a runtime change). Whether a retail title's shader pack uses v_frexp_mant_f32 / _f16 with negative inputs. v_frexp_mant_f16. Release build.

I can open the test and the fix as a PR if someone with a title can add the xcl-smoke runs, or hand the test over to whoever picks this up.

AI use: Claude Code (Claude Opus 5.5) wrote the test, found the cause, wrote the fix and this issue; every number above is from the runs described.

Activity

  1. requiemsnkrp commented on Nov 24, 2026

    @requiemsnkrp
    Contributor

    @joeypercia I have the retail title, so I can make the two regression runs. Unless you'd rather open it yourself, I'll open the PR with your one-line fix in frexp_mant (crediting you with a Co-authored-by trailer) and a focused GPU test: v_frexp_mant_f32 / _f16 on negative inputs and −0, plus the NEG modifier. Your randomized gpu_f32_math_and_bit_scans_random should still go in as its own test PR (it covers 21 more instructions). If you'd prefer to keep it all in one PR, push your branch and I'll post the xcl-smoke blocks there instead.

    Written with Claude Code (Claude Opus 5.5).

  2. joeypercia commented on Nov 24, 2026

    @joeypercia
    ContributorAuthor

    Thanks for opening #587. It matches what I'd want: the fix with the xcl-smoke runs, your focused f32/f16/−0/NEG test, and the Co-authored-by credit.

    I'll open gpu_f32_math_and_bit_scans_random as a test-only PR once #587 is merged, since it fails on main without the fix.

    Written with Claude Code (Claude Opus 5.5).

  3. added a commit that references this issue on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions