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

Open
github-bot wants to merge 1 commit from github-bot/gh-101 into main AGit
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 22:51 UTC · original
lcg007xd-c:shader-f64-cvt → main · +268 −0 · 2 files · 1 commits · open

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

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:51 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/101) `lcg007xd-c:shader-f64-cvt` → `main` · +268 −0 · 2 files · 1 commits · open ## 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)
VOP1 0x10, 0x04, 0x16, 0x03 and 0x15 (v_cvt_f64_f32, v_cvt_f64_i32,
v_cvt_f64_u32, v_cvt_i32_f64, v_cvt_u32_f64) decoded but failed to
translate ("v_cvt_f64_f32 is not supported"); v_cvt_f32_f64 already
lowers, on the bits and without host doubles. These now do the same.

To f64 the conversion is exact (AMD's RDNA2 ISA: 0 ULP, denormals
supported): an f32 is rebiased (a denormal normalized from its leading
one, a NaN made quiet with its payload), an integer's leading one
becomes the implicit bit. From f64 it truncates toward zero, saturates
out of range (infinities too) and gives 0 for NaN, as the ISA says; the
clamp bit there only selects the INEXACT exception.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:43:59 +00:00
Author
Owner

@⁠lmouz823-lang on GitHub · 2026-11-27 14:59 UTC · original

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** on GitHub · 2026-11-27 14:59 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/101#issuecomment-7221476525) 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.
Author
Owner

@⁠lmouz823-lang on GitHub · 2026-11-27 15:04 UTC · original

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.

**@⁠lmouz823-lang** on GitHub · 2026-11-27 15:04 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/101#issuecomment-7221597771) 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/101/head:github-bot/gh-101'
git switch 'github-bot/gh-101'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-101'
git switch 'github-bot/gh-101'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-101'
git switch 'github-bot/gh-101'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-101'
git switch 'main'
git merge --squash 'github-bot/gh-101'
git switch 'main'
git merge --ff-only 'github-bot/gh-101'
git switch 'main'
git merge 'github-bot/gh-101'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.