shader: v_cvt_f32_f64 rounds a double on its bits (#531) #550

Closed
github-bot wants to merge 1 commit from github-bot/gh-546 into main AGit
Owner

@⁠marcus-4 on GitHub · 2026-11-28 02:48 UTC · original
marcus-4:fix/cvt-f32-f64 → main · +333 −1 · 5 files · 1 commits · closed without merging

Refs #535

This is the v_cvt_f32_f64 part of #535 ("one opcode"). The rectangle stage-in part and logging a reason for every dropped draw are not in this PR.

What changes

  • Apple GPUs run no doubles, so v_cvt_f32_f64 was refused ("v_cvt_f32_f64 is not supported"). It is now lowered on the double's bits as 64-bit integers, in Lower::f64_bits_to_f32_bits:
    • rounds to nearest even, including a mantissa that carries into the next exponent;
    • gives infinity past the largest float;
    • keeps float denormals;
    • quiets a NaN, keeping its sign and the top 22 bits of its payload.
  • Lower::src_f64 reads a double source. neg and abs act on bit 63, an inline float constant is read as that double, and a literal as the double's high dword. src64 keeps refusing float modifiers on the integer operations that use it.
  • docs/native-host.md: a new "Doubles" section, which marks the rounding mode as a guess. The console's MODE could flush f32 denormal results. This PR follows the IEEE default, which is DXIL's fptrunc, and is also what Leonida's translator (ProjectLeonida/mandy) chose for the same instruction.

Tests

  • victory-gpu double_tests::a_double_rounds_to_the_nearest_float_with_its_modifiers (Metal):
    • inputs: about 60 edge doubles with each sign (ties to even, the denormal boundary, overflow to infinity, NaN payloads) and 4,096 random doubles over every float exponent;
    • each one plain, negated, absolute and clamped, compared bit for bit against Rust's f64 as f32.
    • The clamped row expects what the shared float clamp already does: a denormal or -0 gives +0.
  • victory-shader valu_tests::v_cvt_f32_f64_rounds_a_double_to_the_nearest_float (Windows hardware GPU and WARP, so it runs in CI).

Checked locally (M1 Pro, macOS 27.2):

  • clippy for x86_64-pc-windows-gnullvm with -D warnings and cargo xtask policy pass;
  • the arm64 tests of every crate except victory-gpu and victory-metal pass, and so do the x86_64 tests of victory-hle, victory-loader and victory;
  • the new Metal test passes when run on its own (several runs).

Checked locally (M1 Pro, macOS 27.2), each GPU test in its own process, one at a time:

  • victory-gpu: 456 passed.
  • victory-metal: 48 passed.

A first, parallel run (10 test threads) of victory-gpu on this branch hung this 16 GB M1 Pro's GPU within its first second: IOGPUFamily logged 80 command queues for the test binary, then nothing; WindowServer's Metal submission blocked in the driver for 120 s, and the kernel's watchdog panicked. No test hangs on its own, this one included (about 1 s). So it takes many tests' GPU work at once, which is the class of failure in #525, #526 and #360. That parallel run is a separate matter from this change, and I'll open an issue for it with the reports if that would help.

**@⁠marcus-4** on GitHub · 2026-11-28 02:48 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/546) `marcus-4:fix/cvt-f32-f64` → `main` · +333 −1 · 5 files · 1 commits · closed without merging Refs #535 This is the `v_cvt_f32_f64` part of #535 ("one opcode"). The rectangle stage-in part and logging a reason for every dropped draw are not in this PR. ## What changes - Apple GPUs run no doubles, so `v_cvt_f32_f64` was refused ("v_cvt_f32_f64 is not supported"). It is now lowered on the double's bits as 64-bit integers, in `Lower::f64_bits_to_f32_bits`: - rounds to nearest even, including a mantissa that carries into the next exponent; - gives infinity past the largest float; - keeps float denormals; - quiets a NaN, keeping its sign and the top 22 bits of its payload. - `Lower::src_f64` reads a double source. `neg` and `abs` act on bit 63, an inline float constant is read as that double, and a literal as the double's high dword. `src64` keeps refusing float modifiers on the integer operations that use it. - `docs/native-host.md`: a new "Doubles" section, which marks the rounding mode as a guess. The console's MODE could flush f32 denormal results. This PR follows the IEEE default, which is DXIL's `fptrunc`, and is also what Leonida's translator (ProjectLeonida/mandy) chose for the same instruction. ## Tests - victory-gpu `double_tests::a_double_rounds_to_the_nearest_float_with_its_modifiers` (Metal): - inputs: about 60 edge doubles with each sign (ties to even, the denormal boundary, overflow to infinity, NaN payloads) and 4,096 random doubles over every float exponent; - each one plain, negated, absolute and clamped, compared bit for bit against Rust's `f64 as f32`. - The clamped row expects what the shared float clamp already does: a denormal or -0 gives +0. - victory-shader `valu_tests::v_cvt_f32_f64_rounds_a_double_to_the_nearest_float` (Windows hardware GPU and WARP, so it runs in CI). Checked locally (M1 Pro, macOS 27.2): - clippy for `x86_64-pc-windows-gnullvm` with `-D warnings` and `cargo xtask policy` pass; - the arm64 tests of every crate except victory-gpu and victory-metal pass, and so do the x86_64 tests of victory-hle, victory-loader and victory; - the new Metal test passes when run on its own (several runs). Checked locally (M1 Pro, macOS 27.2), each GPU test in its own process, one at a time: - victory-gpu: 456 passed. - victory-metal: 48 passed. A first, parallel run (10 test threads) of victory-gpu on this branch hung this 16 GB M1 Pro's GPU within its first second: IOGPUFamily logged 80 command queues for the test binary, then nothing; WindowServer's Metal submission blocked in the driver for 120 s, and the kernel's watchdog panicked. No test hangs on its own, this one included (about 1 s). So it takes many tests' GPU work at once, which is the class of failure in #525, #526 and #360. That parallel run is a separate matter from this change, and I'll open an issue for it with the reports if that would help.
Apple GPUs run no doubles, so v_cvt_f32_f64 was refused ("v_cvt_f32_f64
is not supported"). It is now lowered on 64-bit integers: round to
nearest even with the carry into the exponent, infinity past the largest
float, float denormals kept, a NaN quieted with its sign and the top of
its payload. Source neg and abs act on bit 63; inline float constants
read as doubles and a literal as the high dword.

The rounding follows the IEEE default (DXIL's fptrunc), as Leonida's
translator (ProjectLeonida/mandy) chose; whether the console's MODE
flushes f32 denormal results is marked as a guess in native-host.md.

Tests: victory-gpu double_tests (Metal, edge doubles and 4,096 random
ones, plain/neg/abs/clamp, bit for bit against Rust's conversion) and
victory-shader valu_tests (Windows hardware GPU and WARP).

Refs #531
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 02:49:21 +00:00
Author
Owner

@⁠alexballvideos-netizen on GitHub · 2026-11-28 04:56 UTC · original

Swept #550 (head 508ef3c3, merged onto main 61f55a49) over a 21,579-shader world corpus (Series X, 1.0.2.1) with examples/sweep -j 4, on an M3 Max.

main #550
translated 20,022 20,022
refused 1,557 1,557
  • Newly translated: 0
  • Lost: 0
  • Output changed: 0 (every fingerprint is identical)

No shader in this corpus is refused first for v_cvt_f32_f64, so this is a no-regression check, not a coverage measurement. The one shader from #535 probably comes from a part of the game this corpus doesn't reach, or the opcode sits behind an earlier refusal.

**@⁠alexballvideos-netizen** on GitHub · 2026-11-28 04:56 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/546#issuecomment-7234349752) Swept #550 (head `508ef3c3`, merged onto main `61f55a49`) over a 21,579-shader world corpus (Series X, 1.0.2.1) with `examples/sweep -j 4`, on an M3 Max. | | main | #550 | |---|---|---| | translated | 20,022 | 20,022 | | refused | 1,557 | 1,557 | - **Newly translated:** 0 - **Lost:** 0 - **Output changed:** 0 (every fingerprint is identical) No shader in this corpus is refused first for `v_cvt_f32_f64`, so this is a no-regression check, not a coverage measurement. The one shader from #535 probably comes from a part of the game this corpus doesn't reach, or the opcode sits behind an earlier refusal.
Author
Owner

@⁠marcus-4 on GitHub · 2026-11-28 05:17 UTC · original

Thanks, @⁠alexballvideos-netizen. A clean no-regression sweep is what this needed. Agreed that it says nothing about coverage.

@⁠LakshanSivakumar, do you have the hash of the shader your run refused with "v_cvt_f32_f64 is not supported" (#535)? With it, anyone holding a corpus from that part of the game could check that this PR translates it.

**@⁠marcus-4** on GitHub · 2026-11-28 05:17 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/546#issuecomment-7234581814) Thanks, @⁠alexballvideos-netizen. A clean no-regression sweep is what this needed. Agreed that it says nothing about coverage. @⁠LakshanSivakumar, do you have the hash of the shader your run refused with "v_cvt_f32_f64 is not supported" (#535)? With it, anyone holding a corpus from that part of the game could check that this PR translates it.
github-bot closed this pull request 2026-11-28 08:20:35 +00:00
Author
Owner

@⁠marcus-4 on GitHub · 2026-11-28 08:13 UTC · original

Closing: this change is already in.

**@⁠marcus-4** on GitHub · 2026-11-28 08:13 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/546#issuecomment-7236444958) Closing: this change is already in.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.