shader: lower the f64 conversions to and from f32, i32 and u32 #101

Closed
lcg007xd-c wants to merge 0 commits from upstream-pr/101 into main
lcg007xd-c commented 2026-11-25 22:51:00 +00:00 (Migrated from github.com)

Imported from uiliannn/VItoriaBr. Originally posted by lcg007xd-c on 2026-11-25T22:51:00Z.


What is broken

The f64 conversions decode, but a program using one fails to translate with v_cvt_f64_f32 is not supported (or the matching name):

VOP1 opcode Instruction
0x10 v_cvt_f64_f32
0x04 v_cvt_f64_i32
0x16 v_cvt_f64_u32
0x03 v_cvt_i32_f64
0x15 v_cvt_u32_f64

Only v_cvt_f32_f64 lowers today. It is built on the bits, without host doubles (f64_to_f32_bits), and these now follow the same approach.

Semantics (AMD's RDNA2 ISA, §12.8, opcodes 3, 4, 16, 21, 22)

  • To f64: exact ("0ULP accuracy, denormals are supported").
  • From f64: rounds toward zero, "out-of-range floating point values (including infinity) saturate", and NaN gives 0. The clamp bit only controls whether the INEXACT exception is raised, so the value is the same either way.

The change (emit/lower.rs)

  • f32_to_f64_bits: rebiases a normal f32. A denormal is normalized from its leading one (DXIL FirstbitHi). Zero and infinity carry over. A NaN keeps its payload and comes out quiet.
  • i32_to_f64_bits: the magnitude's leading one becomes the implicit bit, with exponent 1054 − lz. |i32::MIN| = 2³¹ is handled as unsigned.
  • f64_bits_to_i32: the 53-bit significand is shifted right by 1075 − e in i64. Below 1 the result is 0. 2³¹ and up (signed) or 2³² and up (unsigned), infinities included, saturate. Negatives give 0 for the unsigned form, and NaN gives 0.
  • None of these needs the DOUBLES feature. The f64→int path uses INT64.

Tests

  • emit::lower::tests::f64_conversions_translate (every host): all five decode, and a program that stores the result translates (for an f64 result, its high dword). Without the lower.rs change it fails: v_cvt_f64_f32 [7e0a2101, 7e000306]: GFX10 code at 0x24: v_cvt_f64_f32 is not supported.
  • valu_tests::f64_conversions_match_ieee (Windows/GPU): both dwords of each to-f64 conversion for 8 inputs (0, 1, i32::MIN, −1/u32::MAX, 1.5f, a denormal, inf, a pattern). The from-f64 conversions get 14 inputs (±0, ±2.9, the i32/u32 edges just inside and outside, ±inf, NaN, tiny), checked against Rust's as, which has the same truncate/saturate/NaN→0 rule.
  • CPU run: I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I compared both dwords against Rust's f64::from on 96 inputs (including f32 denormals and a signalling NaN, which comes out quiet as on x86), and against as on 81 f64 values. All match.

Run here (Linux, Rust 1.98.1):

  • cargo test -p victory-shader --lib --locked: 188 passed, 0 failed, 2 ignored.
  • Portable clippy and CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.

Not run here: the GPU case, since I have no Windows machine.

🤖 Generated with Claude Code

<!-- greyfinch-sync:uiliannn/VItoriaBr:issue:5500536087 --> Imported from [uiliannn/VItoriaBr](https://github.com/uiliannn/VitoriaBr2.0/pull/101). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T22:51:00Z. --- ## What is broken The f64 conversions decode, but a program using one fails to translate with `v_cvt_f64_f32 is not supported` (or the matching name): | VOP1 opcode | Instruction | |---|---| | 0x10 | `v_cvt_f64_f32` | | 0x04 | `v_cvt_f64_i32` | | 0x16 | `v_cvt_f64_u32` | | 0x03 | `v_cvt_i32_f64` | | 0x15 | `v_cvt_u32_f64` | Only `v_cvt_f32_f64` lowers today. It is built on the bits, without host doubles (`f64_to_f32_bits`), and these now follow the same approach. ## Semantics (AMD's RDNA2 ISA, §12.8, opcodes 3, 4, 16, 21, 22) - **To f64:** exact ("0ULP accuracy, denormals are supported"). - **From f64:** rounds toward zero, "out-of-range floating point values (including infinity) saturate", and NaN gives 0. The clamp bit only controls whether the INEXACT exception is raised, so the value is the same either way. ## The change (`emit/lower.rs`) - **`f32_to_f64_bits`:** rebiases a normal f32. A denormal is normalized from its leading one (DXIL `FirstbitHi`). Zero and infinity carry over. A NaN keeps its payload and comes out quiet. - **`i32_to_f64_bits`:** the magnitude's leading one becomes the implicit bit, with exponent `1054 − lz`. `|i32::MIN|` = 2³¹ is handled as unsigned. - **`f64_bits_to_i32`:** the 53-bit significand is shifted right by `1075 − e` in i64. Below 1 the result is 0. 2³¹ and up (signed) or 2³² and up (unsigned), infinities included, saturate. Negatives give 0 for the unsigned form, and NaN gives 0. - None of these needs the `DOUBLES` feature. The f64→int path uses `INT64`. ## Tests - **`emit::lower::tests::f64_conversions_translate`** (every host): all five decode, and a program that **stores** the result translates (for an f64 result, its high dword). Without the `lower.rs` change it fails: `v_cvt_f64_f32 [7e0a2101, 7e000306]: GFX10 code at 0x24: v_cvt_f64_f32 is not supported`. - **`valu_tests::f64_conversions_match_ieee`** (Windows/GPU): both dwords of each to-f64 conversion for 8 inputs (0, 1, `i32::MIN`, −1/`u32::MAX`, 1.5f, a denormal, inf, a pattern). The from-f64 conversions get 14 inputs (±0, ±2.9, the `i32`/`u32` edges just inside and outside, ±inf, NaN, tiny), checked against Rust's `as`, which has the same truncate/saturate/NaN→0 rule. - **CPU run:** I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I compared both dwords against Rust's `f64::from` on 96 inputs (including f32 denormals and a signalling NaN, which comes out quiet as on x86), and against `as` on 81 f64 values. All match. **Run here** (Linux, Rust 1.98.1): - `cargo test -p victory-shader --lib --locked`: 188 passed, 0 failed, 2 ignored. - Portable clippy and `CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings`: clean. **Not run here:** the GPU case, since I have no Windows machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
lmouz823-lang commented 2026-11-27 14:59:02 +00:00 (Migrated from github.com)

Imported from uiliannn/VItoriaBr. Originally posted by lmouz823-lang on 2026-11-27T14:59:02Z.


I can take the native Windows/NVIDIA validation of the two conversion tests at exact head 9e19fa0973b6239e06fbbb6746fb61ec627aef95: f64_conversions_translate and f64_conversions_match_ieee. I will run the unmodified head first on an RTX 4060 Laptop (8 GiB, driver 610.88), Windows 11/MSVC, with the SDK DXIL validator and strict test mode; I will report hardware and WARP execution and the tested input limits here.

AI-assisted with Codex.

<!-- greyfinch-sync:uiliannn/VItoriaBr:comment:7221476525 --> Imported from [uiliannn/VItoriaBr](https://github.com/uiliannn/VItoriaBr/pull/101#issuecomment-7221476525). Originally posted by [lmouz823-lang](https://github.com/lmouz823-lang) on 2026-11-27T14:59:02Z. --- I can take the native Windows/NVIDIA validation of the two conversion tests at exact head `9e19fa0973b6239e06fbbb6746fb61ec627aef95`: `f64_conversions_translate` and `f64_conversions_match_ieee`. I will run the unmodified head first on an RTX 4060 Laptop (8 GiB, driver 610.88), Windows 11/MSVC, with the SDK DXIL validator and strict test mode; I will report hardware and WARP execution and the tested input limits here. AI-assisted with Codex.
lmouz823-lang commented 2026-11-27 15:04:50 +00:00 (Migrated from github.com)

Imported from uiliannn/VItoriaBr. Originally posted by lmouz823-lang on 2026-11-27T15:04:50Z.


Completed native Windows/NVIDIA validation on the unchanged head 9e19fa0973b6239e06fbbb6746fb61ec627aef95. Both exact tests pass:

emit::lower::tests::f64_conversions_translate ... ok
test result: ok. 1 passed; 0 failed; 0 ignored

VICTORY HOST GPU: NVIDIA GeForce RTX 4060 Laptop GPU (vendor 0x10de)
VICTORY HOST GPU: Microsoft Basic Render Driver (vendor 0x1414)
emit::valu_tests::f64_conversions_match_ieee ... ok
test result: ok. 1 passed; 0 failed; 0 ignored

Environment: Windows 11 build 26200, RTX 4060 Laptop 8 GiB, driver 610.88, Rust 1.98.1 MSVC. VICTORY_STRICT_TESTS=1; VICTORY_WARP unset; VICTORY_DXIL explicitly set to the Windows SDK dxil.dll 1.8.2502.11. WARP is the additional backend exercised by the test; the physical NVIDIA adapter also executed. No device skips were printed. The helper's validator-loading branch remains optional; this reports the explicit configuration and actual GPU result, not a change to that helper.

Commands, in an isolated worktree after a package-only rebuild of victory-shader to avoid reusing another worktree's shared target artifact:

cargo clean --release -p victory-shader
cargo test --release --locked -p victory-shader --lib emit::lower::tests::f64_conversions_translate -- --exact --nocapture --test-threads=1
cargo test --release --locked -p victory-shader --lib emit::valu_tests::f64_conversions_match_ieee -- --exact --nocapture --test-threads=1

This checks the five conversion instructions and the authored input set (76 result-word comparisons per backend). It does not establish AMD behavior, source-register overlap cases, or all conversion boundary cases. No source patch was needed; I did not run the game or the full workspace suite.

AI-assisted validation with Codex.

<!-- greyfinch-sync:uiliannn/VItoriaBr:comment:7221597771 --> Imported from [uiliannn/VItoriaBr](https://github.com/uiliannn/VItoriaBr/pull/101#issuecomment-7221597771). Originally posted by [lmouz823-lang](https://github.com/lmouz823-lang) on 2026-11-27T15:04:50Z. --- Completed native Windows/NVIDIA validation on the **unchanged head `9e19fa0973b6239e06fbbb6746fb61ec627aef95`**. Both exact tests pass: ```text emit::lower::tests::f64_conversions_translate ... ok test result: ok. 1 passed; 0 failed; 0 ignored VICTORY HOST GPU: NVIDIA GeForce RTX 4060 Laptop GPU (vendor 0x10de) VICTORY HOST GPU: Microsoft Basic Render Driver (vendor 0x1414) emit::valu_tests::f64_conversions_match_ieee ... ok test result: ok. 1 passed; 0 failed; 0 ignored ``` Environment: Windows 11 build 26200, RTX 4060 Laptop 8 GiB, driver 610.88, Rust 1.98.1 MSVC. `VICTORY_STRICT_TESTS=1`; `VICTORY_WARP` unset; `VICTORY_DXIL` explicitly set to the Windows SDK `dxil.dll` 1.8.2502.11. WARP is the additional backend exercised by the test; the physical NVIDIA adapter also executed. No device skips were printed. The helper's validator-loading branch remains optional; this reports the explicit configuration and actual GPU result, not a change to that helper. Commands, in an isolated worktree after a package-only rebuild of `victory-shader` to avoid reusing another worktree's shared target artifact: ```text cargo clean --release -p victory-shader cargo test --release --locked -p victory-shader --lib emit::lower::tests::f64_conversions_translate -- --exact --nocapture --test-threads=1 cargo test --release --locked -p victory-shader --lib emit::valu_tests::f64_conversions_match_ieee -- --exact --nocapture --test-threads=1 ``` This checks the five conversion instructions and the authored input set (76 result-word comparisons per backend). It does not establish AMD behavior, source-register overlap cases, or all conversion boundary cases. No source patch was needed; I did not run the game or the full workspace suite. AI-assisted validation with Codex.

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
uiliannn/VItoriaBr!101
No description provided.