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

Manually merged
github merged 5 commits from gh-pr/209/head into gh-pr/209/base 2026-11-26 05:37:32 +00:00
Owner

@⁠KainanP on GitHub · 2026-11-25 15:14 UTC · original
KainanP:fix/f64-cvt-modifiers-fract-nan → main · +199 −20 · 7 files · 5 commits · merged 2026-11-25 15:41 UTC

Change

Fixes the two blocking points and the four non-blocking notes of @⁠Exorion1er's review of #139 (#139), 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

**@⁠KainanP** on GitHub · 2026-11-25 15:14 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/209) `KainanP:fix/f64-cvt-modifiers-fract-nan` → `main` · +199 −20 · 7 files · 5 commits · merged 2026-11-25 15:41 UTC ## Change Fixes the two blocking points and the four non-blocking notes of @⁠Exorion1er's review of #139 (https://git.gtahub.st/github/LooperSalty--AnyXboxX/pulls/139), 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)
The HLSL translation read the f32 source without its VOP3 abs/neg
modifiers, while the interpreter applies them (rdna2_exec_wide._modify):
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 (review of #139).

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, as in the interpreter. Measured while writing
the test: (double)asfloat(x) returns 0x7FFFFFFFE0000000 for every NaN on
the RTX 4060 (sign and payload lost). The translation no longer needs
doubles, so the parity test also runs on Intel UHD.

Test: 18 f32 sources (+-0, +-inf, NaN of both signs with payload,
denormals) under the four abs/neg combinations, byte for byte against
the interpreter; fails without the change on RTX 4060, WARP and Intel.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
_fract called math.floor(nan), which raises a Python ValueError instead
of returning NaN (review of #139; the f32 path is protected by
_float_op). Now, following the ISA formula S0 + -floor(S0):
- a NaN input comes out quiet with its sign and payload, +-infinity
  gives NaN;
- the result is at most the largest double below 1.0
  (0x3FEFFFFFFFFFFFFF), as LLVM models the instruction
  (ConstantFolding, amdgcn_fract: "prevent fract(-small) from returning
  1.0"), so v_fract_f64(-1e-20) no longer gives 1.0. Read in LLVM, not
  measured on hardware.

trunc, ceil, floor and rndne keep the sign of x (IEEE 754
roundToIntegral): trunc/ceil/rndne(-0.5) and floor(-0.0) give -0.0
instead of +0.0, and a NaN comes out quiet.

The module docstring now 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 result; none of them is translated to HLSL.

v_fract_f64 and the f64 roundings have no HLSL translation (no double
floor in HLSL): the recompiler names them, now tested for v_fract_f64.
Tests: 14 bit-exact edge cases (NaN of both signs, signalling NaN,
+-infinity, -1e-20, -0.0, -0.5); 10 fail without the change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Inline constant 248 read as a double was Python's 1/(2*math.pi),
0x3FC45F306DC9C883. The RDNA 2 ISA (November 2020, section 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 the interpreter
(rdna2_exec_cmp64, used by the f64 compares already on main and by the
f64 operations) and in the HLSL translation (rdna2_hlsl_wide). The f32
value was already 0x3e22f983. (Review of #139.)

Test: v_mul_f64 of 248 by 1.0 in the interpreter and the HLSL words of
248; both fail without the change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The review of #139 noted that the f32 paths (already on main) have the
same two issues as the f64 ones:
- trunc, ceil and rndne of -0.5 and floor of -0.0 gave +0.0 in the
  interpreter; IEEE 754 roundToIntegral keeps the sign. Measured: the
  RTX 4060, WARP and Intel UHD all return -0.0.
- v_fract_f32(-1e-20) gave 1.0; it is now at most 0x3F7FFFFF, as LLVM
  models v_fract (ConstantFolding, amdgcn_fract). Measured: WARP and
  Intel UHD's frac already return 0x3F7FFFFF, the RTX 4060 returns 1.0,
  so the HLSL clamps frac (a compare, not min, so NaN goes through).
  v_fract_f32(-0.0) now gives +0.0 (-0.0 + -floor(-0.0)), as on the three
  adapters.

NaN results, infinities fed to fract and denormal inputs are left out
of the GPU test: the NaN pattern and denormal flushing differ between
the three adapters (measured), a separate question.

Tests: 8 interpreter cases (6 fail without the change) and a GPU
parity test on 16 sources x 5 instructions, which fails without the
change on RTX 4060, WARP and Intel UHD, and on the RTX 4060 without
the HLSL clamp.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:37:31 +00:00
github manually merged commit 9b538a39f2 into gh-pr/209/base 2026-11-26 05:37:32 +00:00
Author
Owner

@⁠KainanP on GitHub · 2026-11-25 18:59 UTC · original

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

AI use: Claude Code (Claude Opus 5.5).

**@⁠KainanP** on GitHub · 2026-11-25 18:59 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/209#issuecomment-7171675927) Hardware measurement requested: https://github.com/akimfomin/PortBridge/issues/82 _AI use: Claude Code (Claude Opus 5.5)._
This discussion has been locked. Commenting is limited to contributors.
No description provided.