fix: f64/f32 points from the review of #139 (v_cvt_f64_f32 abs/neg in HLSL, v_fract NaN and bound, 1/(2*pi), sign of zero) #209

Merged
KainanP merged 5 commits from upstream-pr/209 into main 2026-11-25 15:41:29 +00:00
KainanP commented 2026-11-25 15:14:14 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-25T15:14:14Z.


Change

Fixes the two blocking points and the four non-blocking notes of @Exorion1er's review of #139 (https://github.com/LooperSalty/AnyXboxX/pull/139#pullrequestreview-6058973360), which were merged into main with the RDNA 2 stack before they could be fixed there.

Linked issue: Refs #139

Four commits, one per point:

  1. v_cvt_f64_f32 applies abs/neg in HLSL (blocking point 1). The f32 source went through source() without its VOP3 modifiers while the interpreter applies them: v_cvt_f64_f32_e64 v[0:1], -v2 with v2 = 1.0 gave -1.0 in the interpreter and +1.0 on the GPU. The conversion is now done with integers: abs/neg on bit 31, then the exact f32 → f64 conversion (ISA: 0 ULP, denormals kept), NaN made quiet with its sign and payload, like the interpreter. Measured while writing the test: (double)asfloat(x) returns 0x7FFFFFFFE0000000 for every NaN on the RTX 4060 (sign and payload lost), so keeping the old double cast with modifiers added would still have diverged. Side effect: the translation needs no doubles any more, so the parity test also runs on Intel UHD (no FP64).
  2. v_fract_f64 on NaN and ±infinity (blocking point 2). _fract called math.floor(nan) (Python ValueError). Now: NaN in → the same NaN, made quiet (sign and payload kept); ±infinity → NaN. Following the review's note, the result is also bounded by the largest double below 1.0 (0x3FEFFFFFFFFFFFFF): the ISA formula is S0 + -floor(S0), and LLVM models the instruction with that bound (ConstantFolding.cpp, amdgcn_fract: "prevent fract(-small) from returning 1.0"). Read in LLVM, not measured on hardware. In the same commit: trunc/ceil/floor/rndne_f64 keep the sign of zero (trunc(-0.5) = -0.0, IEEE 754 roundToIntegral), and the module docstring says that the ISA gives v_rcp_f64, v_rsq_f64 and v_sqrt_f64 to 2^29 ULP while the interpreter returns the correctly rounded value (none of them is translated to HLSL).
  3. Inline constant 248 as a double is 0x3FC45F306DC9C882. The RDNA 2 ISA (November 2020, §6.2 "Operands", p. 41) gives the exact value used: half 0x3118, single 0x3e22f983, double 0x3fc45f306dc9c882; LLVM uses the same double (AMDGPUBaseInfo.cpp, isInlinableLiteral64). Fixed in both places the review named: rdna2_exec_cmp64 (interpreter, f64 compares and f64 operations) and rdna2_hlsl_wide. The f32 value was already right.
  4. Same two notes for f32 (already on main before #139). Measured on the three adapters: trunc/ceil/rndne(-0.5) and floor(-0.0) return -0.0 on RTX 4060, WARP and Intel UHD, the interpreter gave +0.0; frac(-1e-20) returns 0x3F7FFFFF on WARP and Intel UHD but 1.0 on the RTX 4060. The interpreter now keeps the sign of zero and bounds v_fract_f32 below 1.0; the HLSL bounds frac with a compare (not min, so a NaN goes through unchanged). Left out on purpose: NaN results, fract(±inf) and denormal inputs, whose NaN pattern and flushing differ between the three adapters (measured: NaN 0x7FFFFFFF on RTX, input payload or 0xFFC00000 on WARP/Intel; f32 denormals flushed on all three). That is a separate question.

Verification

Windows 11 26200, Python 3.12, runtime built from this branch (VS 2022 Build Tools, CMake), on top of main at 488a71c:

python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed!
python -m pytest (RTX 4060)                → 1131 passed, 14 skipped
ANYXBOXX_GPU=warp python -m pytest         → 1131 passed, 14 skipped
ANYXBOXX_GPU=Intel python -m pytest        → 1129 passed, 16 skipped   (no FP64: the double-only GPU tests skip)

README: the test count stays 1100+ (each commit checked: README count = collected tests rounded down, RDNA 2 coverage numbers = rdna2_exec.coverage()).

Oracle and test data

  • HLSL: compared with the reference interpreter, byte for byte, on RTX 4060, WARP and Intel UHD: 18 f32 sources (±0, ±inf, NaN of both signs with payload, denormals, max/min normal) × the four abs/neg combinations for v_cvt_f64_f32; 16 sources × v_fract/trunc/ceil/rndne/floor_f32.
  • Interpreter: 14 bit-exact f64 cases (NaN of both signs, signalling NaN, ±infinity, -1e-20, -0.0, -0.5), 8 f32 cases, the inline constant 248; values from the ISA formulas, IEEE 754 and the ISA constant table.
  • v_fract_f64 and the f64 roundings have no HLSL translation (HLSL has no double floor): the recompiler names them (Untranslatable), now tested for v_fract_f64 too.
  • Failure case: with the source files of main and the tests of this branch, 20 tests fail on each of RTX 4060, WARP and Intel UHD.
  • All test data is built in the tests.

Checklist

  • The PR covers a single reviewable behaviour (the review of #139, one commit per point).
  • A test fails without this change and passes with it.
  • The commands and results of each check are given above.
  • An unimplemented function returns an error (E_NOTIMPL), never a fake success.
  • README: the same change in all 9 languages (no README change here).
  • No game, console or SDK file, no key, no dump, no download link.
  • Findings drawn from a title: none.
  • Code taken from another project: none (LLVM and the ISA are cited as sources, no code copied).

Made with AI assistance (Claude Code, Claude Opus 5.5); tests run on the three adapters above.

🤖 Generated with Claude Code

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:6607679832 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/209). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-25T15:14:14Z. --- ## Change Fixes the two blocking points and the four non-blocking notes of @Exorion1er's review of #139 (https://github.com/LooperSalty/AnyXboxX/pull/139#pullrequestreview-6058973360), which were merged into `main` with the RDNA 2 stack before they could be fixed there. Linked issue: Refs #139 Four commits, one per point: 1. **`v_cvt_f64_f32` applies abs/neg in HLSL** (blocking point 1). The f32 source went through `source()` without its VOP3 modifiers while the interpreter applies them: `v_cvt_f64_f32_e64 v[0:1], -v2` with v2 = 1.0 gave -1.0 in the interpreter and +1.0 on the GPU. The conversion is now done with integers: abs/neg on bit 31, then the exact f32 → f64 conversion (ISA: 0 ULP, denormals kept), NaN made quiet with its sign and payload, like the interpreter. Measured while writing the test: `(double)asfloat(x)` returns `0x7FFFFFFFE0000000` for **every** NaN on the RTX 4060 (sign and payload lost), so keeping the old double cast with modifiers added would still have diverged. Side effect: the translation needs no doubles any more, so the parity test also runs on Intel UHD (no FP64). 2. **`v_fract_f64` on NaN and ±infinity** (blocking point 2). `_fract` called `math.floor(nan)` (Python `ValueError`). Now: NaN in → the same NaN, made quiet (sign and payload kept); ±infinity → NaN. Following the review's note, the result is also bounded by the largest double below 1.0 (`0x3FEFFFFFFFFFFFFF`): the ISA formula is `S0 + -floor(S0)`, and LLVM models the instruction with that bound (`ConstantFolding.cpp`, `amdgcn_fract`: "prevent fract(-small) from returning 1.0"). Read in LLVM, not measured on hardware. In the same commit: `trunc`/`ceil`/`floor`/`rndne_f64` keep the sign of zero (`trunc(-0.5) = -0.0`, IEEE 754 roundToIntegral), and the module docstring says that the ISA gives `v_rcp_f64`, `v_rsq_f64` and `v_sqrt_f64` to 2^29 ULP while the interpreter returns the correctly rounded value (none of them is translated to HLSL). 3. **Inline constant 248 as a double is `0x3FC45F306DC9C882`.** The RDNA 2 ISA (November 2020, §6.2 "Operands", p. 41) gives the exact value used: half `0x3118`, single `0x3e22f983`, double `0x3fc45f306dc9c882`; LLVM uses the same double (`AMDGPUBaseInfo.cpp`, `isInlinableLiteral64`). Fixed in both places the review named: `rdna2_exec_cmp64` (interpreter, f64 compares and f64 operations) and `rdna2_hlsl_wide`. The f32 value was already right. 4. **Same two notes for f32** (already on `main` before #139). Measured on the three adapters: `trunc`/`ceil`/`rndne(-0.5)` and `floor(-0.0)` return -0.0 on RTX 4060, WARP and Intel UHD, the interpreter gave +0.0; `frac(-1e-20)` returns `0x3F7FFFFF` on WARP and Intel UHD but 1.0 on the RTX 4060. The interpreter now keeps the sign of zero and bounds `v_fract_f32` below 1.0; the HLSL bounds `frac` with a compare (not `min`, so a NaN goes through unchanged). Left out on purpose: NaN results, `fract(±inf)` and denormal inputs, whose NaN pattern and flushing differ between the three adapters (measured: NaN `0x7FFFFFFF` on RTX, input payload or `0xFFC00000` on WARP/Intel; f32 denormals flushed on all three). That is a separate question. ## Verification Windows 11 26200, Python 3.12, runtime built from this branch (VS 2022 Build Tools, CMake), on top of `main` at `488a71c`: ``` python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! python -m pytest (RTX 4060) → 1131 passed, 14 skipped ANYXBOXX_GPU=warp python -m pytest → 1131 passed, 14 skipped ANYXBOXX_GPU=Intel python -m pytest → 1129 passed, 16 skipped (no FP64: the double-only GPU tests skip) ``` README: the test count stays `1100+` (each commit checked: README count = collected tests rounded down, RDNA 2 coverage numbers = `rdna2_exec.coverage()`). ## Oracle and test data - HLSL: compared with the reference interpreter, byte for byte, on RTX 4060, WARP and Intel UHD: 18 f32 sources (±0, ±inf, NaN of both signs with payload, denormals, max/min normal) × the four abs/neg combinations for `v_cvt_f64_f32`; 16 sources × `v_fract/trunc/ceil/rndne/floor_f32`. - Interpreter: 14 bit-exact f64 cases (NaN of both signs, signalling NaN, ±infinity, -1e-20, -0.0, -0.5), 8 f32 cases, the inline constant 248; values from the ISA formulas, IEEE 754 and the ISA constant table. - `v_fract_f64` and the f64 roundings have no HLSL translation (HLSL has no double `floor`): the recompiler names them (`Untranslatable`), now tested for `v_fract_f64` too. - Failure case: with the source files of `main` and the tests of this branch, 20 tests fail on each of RTX 4060, WARP and Intel UHD. - All test data is built in the tests. ## Checklist - [x] The PR covers a single reviewable behaviour (the review of #139, one commit per point). - [x] A test fails without this change and passes with it. - [x] The commands and results of each check are given above. - [x] An unimplemented function returns an error (`E_NOTIMPL`), never a fake success. - [x] README: the same change in all 9 languages (no README change here). - [x] No game, console or SDK file, no key, no dump, no download link. - [x] Findings drawn from a title: none. - [x] Code taken from another project: none (LLVM and the ISA are cited as sources, no code copied). Made with AI assistance (Claude Code, Claude Opus 5.5); tests run on the three adapters above. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
KainanP commented 2026-11-25 18:59:33 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-25T18:59:33Z.


Hardware measurement requested: https://github.com/akimfomin/PortBridge/issues/82

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7171675927 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/209#issuecomment-7171675927). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-25T18:59:33Z. --- Hardware measurement requested: https://github.com/akimfomin/PortBridge/issues/82 _AI use: Claude Code (Claude Opus 5.5)._
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
LooperSalty/AnyXboxX!209
No description provided.