shader: lower v_trunc_f64, v_floor_f64, v_ceil_f64 and v_rndne_f64 #102

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

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

What is broken

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

VOP1 opcode Instruction
0x17 v_trunc_f64
0x18 v_ceil_f64
0x19 v_rndne_f64
0x1A v_floor_f64

They round a binary64 to an integral value (AMD's RDNA2 ISA, §12.8, opcodes 23–26). DXIL's Round_* operations take only half and float, so there is no direct host operation for doubles.

The change (emit/lower.rs)

New helper f64_round(lo, hi, name), worked on the bits in 64-bit integers, with no host doubles:

  • Units bit: with u the unbiased exponent, bit 52 − u of the 63-bit magnitude is the units bit, and the bits below it are the fraction. Clearing them truncates.
  • Rounding away from zero: adding the units bit steps the magnitude by exactly one. A carry into the exponent field is how IEEE's encoding represents the next binade, so no special case is needed. The step is applied:
    • for floor of a negative value with a fraction;
    • for ceil of a positive one;
    • for rndne past one half, or at exactly one half when the units bit is odd.
  • |x| < 1: 0, or 1 where the rounding goes away from zero (rndne: above 0.5). The sign is kept, so ceil(−0.3) = −0 and floor(−0.3) = −1.
  • |x| ≥ 2⁵², infinities and NaNs: already integral, left unchanged. A signalling NaN comes out quiet.
  • Modifiers: abs/neg come through f64_pair, and output modifiers are refused.

Tests

  • emit::lower::tests::f64_roundings_translate (every host): all four decode, and a program that stores the result translates. Without the lower.rs change it fails: v_trunc_f64 [7e0a2f01, 7e000306]: GFX10 code at 0x24: v_trunc_f64 is not supported.

  • valu_tests::f64_roundings_match_ieee (Windows/GPU): both dwords of each result for 16 inputs against Rust's trunc/ceil/floor/round_ties_even. The inputs are ±0, ±0.3, ±0.5, 0.7, ties 1.5/2.5/−2.5, ±3.7, 2⁵² − 0.5, 1e300 and ±inf.

  • CPU run: I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I compared against the same Rust functions on 148 values:

    • 119 scrambled across exponents 2⁻¹⁰ to 2⁴⁰;
    • the 2⁵¹/2⁵² boundaries, the doubles just below and above 0.5, 1 − ε/2, denormals and f64::MAX;
    • a signalling NaN (made quiet) and neg on the source (floor(−2.5) = −3).

    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. v_fract_f64 is left out because it needs a double subtraction and RDNA2's clamp below 1.0.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:55 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/102) `lcg007xd-c:shader-f64-round` → `main` · +188 −0 · 2 files · 1 commits · open ## What is broken The f64 roundings decode, but a program using one fails to translate with `v_trunc_f64 is not supported` (or the matching name): | VOP1 opcode | Instruction | |---|---| | 0x17 | `v_trunc_f64` | | 0x18 | `v_ceil_f64` | | 0x19 | `v_rndne_f64` | | 0x1A | `v_floor_f64` | They round a binary64 to an integral value (AMD's RDNA2 ISA, §12.8, opcodes 23–26). DXIL's `Round_*` operations take only half and float, so there is no direct host operation for doubles. ## The change (`emit/lower.rs`) New helper `f64_round(lo, hi, name)`, worked on the bits in 64-bit integers, with no host doubles: - **Units bit:** with `u` the unbiased exponent, bit `52 − u` of the 63-bit magnitude is the units bit, and the bits below it are the fraction. Clearing them truncates. - **Rounding away from zero:** adding the units bit steps the magnitude by exactly one. A carry into the exponent field is how IEEE's encoding represents the next binade, so no special case is needed. The step is applied: - for **floor** of a negative value with a fraction; - for **ceil** of a positive one; - for **rndne** past one half, or at exactly one half when the units bit is odd. - **`|x| < 1`:** 0, or 1 where the rounding goes away from zero (rndne: above 0.5). The sign is kept, so `ceil(−0.3) = −0` and `floor(−0.3) = −1`. - **`|x| ≥ 2⁵²`, infinities and NaNs:** already integral, left unchanged. A signalling NaN comes out quiet. - **Modifiers:** `abs`/`neg` come through `f64_pair`, and output modifiers are refused. ## Tests - **`emit::lower::tests::f64_roundings_translate`** (every host): all four decode, and a program that **stores** the result translates. Without the `lower.rs` change it fails: `v_trunc_f64 [7e0a2f01, 7e000306]: GFX10 code at 0x24: v_trunc_f64 is not supported`. - **`valu_tests::f64_roundings_match_ieee`** (Windows/GPU): both dwords of each result for 16 inputs against Rust's `trunc`/`ceil`/`floor`/`round_ties_even`. The inputs are ±0, ±0.3, ±0.5, 0.7, ties 1.5/2.5/−2.5, ±3.7, `2⁵² − 0.5`, 1e300 and ±inf. - **CPU run:** I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I compared against the same Rust functions on 148 values: - 119 scrambled across exponents 2⁻¹⁰ to 2⁴⁰; - the 2⁵¹/2⁵² boundaries, the doubles just below and above 0.5, `1 − ε/2`, denormals and `f64::MAX`; - a signalling NaN (made quiet) and `neg` on the source (`floor(−2.5) = −3`). 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. `v_fract_f64` is left out because it needs a double subtraction and RDNA2's clamp below 1.0. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
VOP1 0x17..=0x1A decoded but failed to translate ("v_trunc_f64 is not
supported"). DXIL's rounding operations take no doubles, so these work
on the binary64's bits in 64-bit integers: with u the unbiased exponent,
bit 52 - u of the magnitude is the units bit; the bits below it are the
fraction, cleared to truncate, and adding the units bit (carrying into
the exponent, as IEEE's encoding allows) steps away from zero for floor
of a negative, ceil of a positive, or round-to-nearest-even past half
(or at half with an odd units bit). Below 1 the result is 0 or 1 with
the sign kept; 2^52 and up, infinities and NaNs (made quiet) are already
integral.

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

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

I can take the native Windows/NVIDIA validation at exact head 3491b2d40d6e2315a50f403c6b384eed0904722a: f64_roundings_translate and f64_roundings_match_ieee. I will run the unmodified head first on Windows 11/MSVC with an RTX 4060 Laptop (8 GiB, driver 610.88), the SDK DXIL validator and strict test mode, and report both physical-GPU and WARP execution plus the actual input coverage here.

AI-assisted with Codex.

**@⁠lmouz823-lang** on GitHub · 2026-11-27 14:59 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/102#issuecomment-7221489300) I can take the native Windows/NVIDIA validation at exact head `3491b2d40d6e2315a50f403c6b384eed0904722a`: `f64_roundings_translate` and `f64_roundings_match_ieee`. I will run the unmodified head first on Windows 11/MSVC with an RTX 4060 Laptop (8 GiB, driver 610.88), the SDK DXIL validator and strict test mode, and report both physical-GPU and WARP execution plus the actual input coverage here. AI-assisted with Codex.
Author
Owner

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

Native Windows/NVIDIA validation completed on exact head 3491b2d40d6e2315a50f403c6b384eed0904722a (Windows 11 build 26200, RTX 4060 Laptop 8 GiB, driver 610.88, Rust 1.98.1 MSVC).

Both tests pass on the untouched head:

  • emit::lower::tests::f64_roundings_translate: 1 passed.
  • emit::valu_tests::f64_roundings_match_ieee: 1 passed, 4.23 s; the existing 16 inputs, four roundings and both result dwords ran on hardware and WARP.

The log identifies both actual backends:

VICTORY HOST GPU: NVIDIA GeForce RTX 4060 Laptop GPU (vendor 0x10de)
VICTORY HOST GPU: Microsoft Basic Render Driver (vendor 0x1414)

I set VICTORY_STRICT_TESTS=1 (no device skips), unset VICTORY_WARP, and explicitly set VICTORY_DXIL to SDK 10.0.26100.0's dxil.dll 1.8.2502.11. The existing validator-load branch remains optional; these results establish actual device execution, not a change to that helper. I cleaned only the victory-shader release package before the first build to prevent a shared target directory from reusing a sibling PR's test binary, and checked each exact filter ran one test.

I also tested the small test-only addition below for the minimum binary64 subnormals, +2^-1074 and -2^-1074. Expected bits are explicit, so the check includes the sign of zero:

operation positive input negative input
trunc +0 -0
ceil +1 -0
rndne +0 -0
floor +0 -1

The added case passed on the RTX 4060 and WARP (1 test, 1.75 s), and both original tests passed again with it present. cargo run --release --locked -p xtask -- policy reported 195 files ok; git diff --check is clean. No production code was changed.

Commands (each test uses -- --exact --nocapture --test-threads=1):

cargo test --release --locked -p victory-shader --lib emit::lower::tests::f64_roundings_translate -- --exact --nocapture --test-threads=1
cargo test --release --locked -p victory-shader --lib emit::valu_tests::f64_roundings_match_ieee -- --exact --nocapture --test-threads=1
cargo test --release --locked -p victory-shader --lib emit::valu_tests::f64_roundings_keep_smallest_subnormal_direction_and_zero_sign -- --exact --nocapture --test-threads=1

This covers these synthetic inputs on NVIDIA and WARP. AMD hardware, NaNs/signalling NaNs and dynamic MODE behavior remain untested; I have not used this result to claim game behavior or performance.

Optional subnormal regression test, against 3491b2d (not committed)
diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs
index 08d7c70..d56f246 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -1524,6 +1524,30 @@ fn f64_roundings_match_ieee() {
     }
 }
 
+#[test]
+fn f64_roundings_keep_smallest_subnormal_direction_and_zero_sign() {
+    // Exact binary64 inputs +2^-1074 and -2^-1074. Check the direction
+    // explicitly: ceil(+tiny) is +1, floor(-tiny) is -1, and the other
+    // results are zero with the input's sign. No host float arithmetic.
+    let inputs = [[1, 0, 0, 0], [1, 0x8000_0000, 0, 0]];
+    let negative_zero = 0x8000_0000_0000_0000u64;
+    let ops = [
+        (0x17, "v_trunc_f64", [0, negative_zero]),
+        (0x18, "v_ceil_f64", [0x3ff0_0000_0000_0000, negative_zero]),
+        (0x19, "v_rndne_f64", [0, negative_zero]),
+        (0x1A, "v_floor_f64", [0, 0xbff0_0000_0000_0000]),
+    ];
+    for (op, name, expected) in ops {
+        for (h, src) in [(0, 5u8), (1, 6)] {
+            let shader = build_valu_cs(&[0x7E00_0000 | 5 << 17 | op << 9 | 0x101, vop1(0x001, 0, vgpr(src))]);
+            run_cases(&shader, &inputs, |backend, i, _, got| {
+                let want = (expected[i] >> (32 * h)) as u32;
+                assert_eq!(got, want, "{backend}: {name} subnormal case {i} dword {h} = {got:#010x}, want {want:#010x}");
+            });
+        }
+    }
+}
+
 #[test]
 fn whole_wave_f64_sum_and_cvt_f32_f64() {
     let s_or_saveexec_b32 =

AI-assisted local validation with Codex.

**@⁠lmouz823-lang** on GitHub · 2026-11-27 15:09 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/102#issuecomment-7221690625) Native Windows/NVIDIA validation completed on exact head `3491b2d40d6e2315a50f403c6b384eed0904722a` (Windows 11 build 26200, RTX 4060 Laptop 8 GiB, driver 610.88, Rust 1.98.1 MSVC). **Both tests pass on the untouched head:** - `emit::lower::tests::f64_roundings_translate`: 1 passed. - `emit::valu_tests::f64_roundings_match_ieee`: 1 passed, 4.23 s; the existing 16 inputs, four roundings and both result dwords ran on hardware and WARP. The log identifies both actual backends: ```text VICTORY HOST GPU: NVIDIA GeForce RTX 4060 Laptop GPU (vendor 0x10de) VICTORY HOST GPU: Microsoft Basic Render Driver (vendor 0x1414) ``` I set `VICTORY_STRICT_TESTS=1` (no device skips), unset `VICTORY_WARP`, and explicitly set `VICTORY_DXIL` to SDK 10.0.26100.0's `dxil.dll` 1.8.2502.11. The existing validator-load branch remains optional; these results establish actual device execution, not a change to that helper. I cleaned only the `victory-shader` release package before the first build to prevent a shared target directory from reusing a sibling PR's test binary, and checked each exact filter ran one test. I also tested the small **test-only** addition below for the minimum binary64 subnormals, `+2^-1074` and `-2^-1074`. Expected bits are explicit, so the check includes the sign of zero: | operation | positive input | negative input | | --- | --- | --- | | trunc | +0 | -0 | | ceil | +1 | -0 | | rndne | +0 | -0 | | floor | +0 | -1 | The added case passed on the RTX 4060 and WARP (1 test, 1.75 s), and both original tests passed again with it present. `cargo run --release --locked -p xtask -- policy` reported `195 files ok`; `git diff --check` is clean. No production code was changed. Commands (each test uses `-- --exact --nocapture --test-threads=1`): ```text cargo test --release --locked -p victory-shader --lib emit::lower::tests::f64_roundings_translate -- --exact --nocapture --test-threads=1 cargo test --release --locked -p victory-shader --lib emit::valu_tests::f64_roundings_match_ieee -- --exact --nocapture --test-threads=1 cargo test --release --locked -p victory-shader --lib emit::valu_tests::f64_roundings_keep_smallest_subnormal_direction_and_zero_sign -- --exact --nocapture --test-threads=1 ``` This covers these synthetic inputs on NVIDIA and WARP. AMD hardware, NaNs/signalling NaNs and dynamic MODE behavior remain untested; I have not used this result to claim game behavior or performance. <details> <summary>Optional subnormal regression test, against 3491b2d (not committed)</summary> ```diff diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs index 08d7c70..d56f246 100644 --- a/crates/victory-shader/src/emit/valu_tests.rs +++ b/crates/victory-shader/src/emit/valu_tests.rs @@ -1524,6 +1524,30 @@ fn f64_roundings_match_ieee() { } } +#[test] +fn f64_roundings_keep_smallest_subnormal_direction_and_zero_sign() { + // Exact binary64 inputs +2^-1074 and -2^-1074. Check the direction + // explicitly: ceil(+tiny) is +1, floor(-tiny) is -1, and the other + // results are zero with the input's sign. No host float arithmetic. + let inputs = [[1, 0, 0, 0], [1, 0x8000_0000, 0, 0]]; + let negative_zero = 0x8000_0000_0000_0000u64; + let ops = [ + (0x17, "v_trunc_f64", [0, negative_zero]), + (0x18, "v_ceil_f64", [0x3ff0_0000_0000_0000, negative_zero]), + (0x19, "v_rndne_f64", [0, negative_zero]), + (0x1A, "v_floor_f64", [0, 0xbff0_0000_0000_0000]), + ]; + for (op, name, expected) in ops { + for (h, src) in [(0, 5u8), (1, 6)] { + let shader = build_valu_cs(&[0x7E00_0000 | 5 << 17 | op << 9 | 0x101, vop1(0x001, 0, vgpr(src))]); + run_cases(&shader, &inputs, |backend, i, _, got| { + let want = (expected[i] >> (32 * h)) as u32; + assert_eq!(got, want, "{backend}: {name} subnormal case {i} dword {h} = {got:#010x}, want {want:#010x}"); + }); + } + } +} + #[test] fn whole_wave_f64_sum_and_cvt_f32_f64() { let s_or_saveexec_b32 = ``` </details> AI-assisted local 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/102/head:github-bot/gh-102'
git switch 'github-bot/gh-102'

Merge

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