Recompiler: v_frexp_mant_f32 drops the sign of negative inputs #575

Closed
opened 2026-11-24 02:02:43 +00:00 by joeypercia · 2 comments
joeypercia commented 2026-11-24 02:02:43 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by joeypercia on 2026-11-24T02:02:43Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6572918411 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/580). Originally posted by [joeypercia](https://github.com/joeypercia) on 2026-11-24T02:02:43Z. --- `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: ```hlsl 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: ```hlsl 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.
requiemsnkrp commented 2026-11-24 07:05:28 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by requiemsnkrp on 2026-11-24T02:04:31Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7119729164 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/580#issuecomment-7119729164). Originally posted by [requiemsnkrp](https://github.com/requiemsnkrp) on 2026-11-24T02:04:31Z. --- @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)._
joeypercia commented 2026-11-24 07:05:35 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by joeypercia on 2026-11-24T02:20:36Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7120041323 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/580#issuecomment-7120041323). Originally posted by [joeypercia](https://github.com/joeypercia) on 2026-11-24T02:20:36Z. --- 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).
Sign in to join this conversation.
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#575
No description provided.