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

Closed
marcus-4 wants to merge 1 commit from upstream-pr/546 into main
marcus-4 commented 2026-11-28 02:48:01 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by marcus-4 on 2026-11-28T02:48:01Z.


Refs #531

This is the v_cvt_f32_f64 part of #531 ("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 #521, #522 and #357. 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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5535942138 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/546). Originally posted by [marcus-4](https://github.com/marcus-4) on 2026-11-28T02:48:01Z. --- Refs #531 This is the `v_cvt_f32_f64` part of #531 ("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 #521, #522 and #357. 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.
alexballvideos-netizen commented 2026-11-28 04:56:00 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by alexballvideos-netizen on 2026-11-28T04:56:00Z.


Swept #546 (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 #546
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 #531 probably comes from a part of the game this corpus doesn't reach, or the opcode sits behind an earlier refusal.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7234349752 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/546#issuecomment-7234349752). Originally posted by [alexballvideos-netizen](https://github.com/alexballvideos-netizen) on 2026-11-28T04:56:00Z. --- Swept #546 (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 | #546 | |---|---|---| | 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 #531 probably comes from a part of the game this corpus doesn't reach, or the opcode sits behind an earlier refusal.
marcus-4 commented 2026-11-28 05:17:56 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by marcus-4 on 2026-11-28T05:17:56Z.


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" (#531)? With it, anyone holding a corpus from that part of the game could check that this PR translates it.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7234581814 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/546#issuecomment-7234581814). Originally posted by [marcus-4](https://github.com/marcus-4) on 2026-11-28T05:17:56Z. --- 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" (#531)? With it, anyone holding a corpus from that part of the game could check that this PR translates it.
marcus-4 commented 2026-11-28 08:13:32 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by marcus-4 on 2026-11-28T08:13:32Z.


Closing: this change is already in.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7236444958 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/546#issuecomment-7236444958). Originally posted by [marcus-4](https://github.com/marcus-4) on 2026-11-28T08:13:32Z. --- Closing: this change is already in.

Pull request closed

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
LakshanSivakumar/Native-Victory-macOS!546
No description provided.