shader: lower the four double arithmetic forms #141

Closed
xiaotu030202 wants to merge 1 commit from upstream-pr/141 into main
xiaotu030202 commented 2026-11-25 14:11:03 +00:00 (Migrated from github.com)
First-time contributor

Imported from Zettrox/ztrx. Originally posted by xiaotu030202 on 2026-11-25T14:04:46Z.


What this adds

v_add_f64, v_mul_f64, v_min_f64, v_max_f64 were all refused. They became reachable when #138/#140 put a double in the emitter — before that, all six double instructions in gfx10/opcodes.rs were unreachable together.

Nothing in #132's log contains any of these. This is the rest of what the conversion pair unlocked, not a fix for a reported failure.

max_min cannot be reused, and the validator says why

The f32 min/max go through max_min, which calls dx.op.binary.f32 with i32 bitcasts to fix the -0 ordering. Handing it a double is refused in one line:

'dx.op.binary.f32' is not a DXILOpFuncition for DXILOpcode 'FMin'
  at '%33 = call float @dx.op.binary.f32(i32 36, double %27, double %32)'

FMin/FMax exist only for f32/f16. So f64_max_min is a compare and a select, keeping the hardware's -0 rule — of two zeros the maximum is +0 unless both are −0, the minimum is −0 unless neither is — with i64 masks (0x7FFF_FFFF_FFFF_FFFF) instead of i32 ones.

A bug in my own test, caught by the test

The first run failed with v_add_f64(1.0, 2.0) -> 1.0. The lowering was fine; the test was wrong. build_cs hands its four input dwords to v1..v4, and I widened the first input into v2 — clobbering the second input before it was read. The sum really was a + garbage.

Temporaries now start at v8. A translate-only assertion would have passed and shipped a test that measured nothing; this is the second time in this pair of features that value-checking earned its place.

Tests

the_double_arithmetic_forms_compute_in_double runs all four forms on the hardware adapter and WARP, over 1.0/2.0, 2.0/3.0, −2.0/1.0, 10.0/10.0, comparing against the same arithmetic in Rust at f64. Inputs are widened with v_cvt_f64_f32 and the result narrowed with v_cvt_f32_f64, so it exercises #138 and #140 too.

swap v_min_f64 and v_max_f64
  FAILED  "v_min_f64 case 0: (1, 2) -> 0x40000000, want 0x3f800000"

Verification

Windows 11, RTX 5070 Ti, Rust 1.98.1 (pinned), MSVC 14.44.35207, Windows SDK 10.0.26100.0, VICTORY_DXIL set. No game files.

$ cargo test -p victory-shader --lib -- --test-threads=1
test result: ok. 216 passed; 0 failed; 2 ignored

$ cargo clippy -p victory-shader --all-targets
clean

$ cargo xtask policy
asset policy: 172 files ok

What is not claimed

No title has been seen failing on any of these. They are tested in isolation, not against a real shader.

Seven double instructions still do not lower: v_fma_f64, v_rcp_f64, v_rsq_f64, v_sqrt_f64, v_frexp_*_f64, v_div_scale/fmas/fixup_f64, the v_cmp_*_f64 family, and the ds_*_f64 LDS forms. Each needs its own reading of the ISA — v_sqrt_f64 is a two-instruction sequence on some hardware — and none are here.

The stack, and an offer

Three branches that build on each other:

PR adds applies to main alone?
#138 TypeDef::Double, Types::f64, Cast::FPTrunc, v_cvt_f32_f64 yes
#140 Cast::FPExt, v_cvt_f64_f32 no, needs #138
this four arithmetic forms, f64_max_min no, needs both

Say the word and I will squash all three into one branch with one description. Three PRs for one feature is worse to review than one, and I only split them because each was independently verifiable as I went.

<!-- greyfinch-sync:Zettrox/ztrx:issue:6606294501 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/141). Originally posted by [xiaotu030202](https://github.com/xiaotu030202) on 2026-11-25T14:04:46Z. --- ## What this adds `v_add_f64`, `v_mul_f64`, `v_min_f64`, `v_max_f64` were all refused. They became reachable when #138/#140 put a `double` in the emitter — before that, **all six** double instructions in `gfx10/opcodes.rs` were unreachable together. Nothing in #132's log contains any of these. This is the rest of what the conversion pair unlocked, not a fix for a reported failure. ## `max_min` cannot be reused, and the validator says why The `f32` min/max go through `max_min`, which calls `dx.op.binary.f32` with `i32` bitcasts to fix the `-0` ordering. Handing it a `double` is refused in one line: ``` 'dx.op.binary.f32' is not a DXILOpFuncition for DXILOpcode 'FMin' at '%33 = call float @dx.op.binary.f32(i32 36, double %27, double %32)' ``` `FMin`/`FMax` exist only for `f32`/`f16`. So `f64_max_min` is a compare and a select, keeping the hardware's `-0` rule — of two zeros the maximum is `+0` unless both are `−0`, the minimum is `−0` unless neither is — with `i64` masks (`0x7FFF_FFFF_FFFF_FFFF`) instead of `i32` ones. ## A bug in my own test, caught by the test The first run failed with `v_add_f64(1.0, 2.0) -> 1.0`. **The lowering was fine; the test was wrong.** `build_cs` hands its four input dwords to `v1..v4`, and I widened the first input into `v2` — clobbering the second input before it was read. The sum really was `a + garbage`. Temporaries now start at `v8`. A translate-only assertion would have passed and shipped a test that measured nothing; this is the second time in this pair of features that value-checking earned its place. ## Tests `the_double_arithmetic_forms_compute_in_double` runs all four forms on **the hardware adapter and WARP**, over 1.0/2.0, 2.0/3.0, −2.0/1.0, 10.0/10.0, comparing against the same arithmetic in Rust at `f64`. Inputs are widened with `v_cvt_f64_f32` and the result narrowed with `v_cvt_f32_f64`, so it exercises #138 and #140 too. ``` swap v_min_f64 and v_max_f64 FAILED "v_min_f64 case 0: (1, 2) -> 0x40000000, want 0x3f800000" ``` ## Verification Windows 11, RTX 5070 Ti, Rust 1.98.1 (pinned), MSVC 14.44.35207, Windows SDK 10.0.26100.0, `VICTORY_DXIL` set. **No game files.** ``` $ cargo test -p victory-shader --lib -- --test-threads=1 test result: ok. 216 passed; 0 failed; 2 ignored $ cargo clippy -p victory-shader --all-targets clean $ cargo xtask policy asset policy: 172 files ok ``` ## What is not claimed **No title has been seen failing on any of these.** They are tested in isolation, not against a real shader. **Seven double instructions still do not lower**: `v_fma_f64`, `v_rcp_f64`, `v_rsq_f64`, `v_sqrt_f64`, `v_frexp_*_f64`, `v_div_scale/fmas/fixup_f64`, the `v_cmp_*_f64` family, and the `ds_*_f64` LDS forms. Each needs its own reading of the ISA — `v_sqrt_f64` is a two-instruction sequence on some hardware — and none are here. ## The stack, and an offer Three branches that build on each other: | PR | adds | applies to `main` alone? | |---|---|---| | #138 | `TypeDef::Double`, `Types::f64`, `Cast::FPTrunc`, `v_cvt_f32_f64` | yes | | #140 | `Cast::FPExt`, `v_cvt_f64_f32` | no, needs #138 | | this | four arithmetic forms, `f64_max_min` | no, needs both | **Say the word and I will squash all three into one branch with one description.** Three PRs for one feature is worse to review than one, and I only split them because each was independently verifiable as I went.
upstream-sync closed this pull request 2026-11-25 14:11:03 +00:00
xiaotu030202 commented 2026-11-25 14:11:06 +00:00 (Migrated from github.com)
Author
First-time contributor

Imported from Zettrox/ztrx. Originally posted by xiaotu030202 on 2026-11-25T14:10:41Z.


Superseded by #142, which is all three of these in one branch.

They were split because each was verifiable on its own, but two of them cannot apply to main without the first — so reviewing them apart means reviewing a branch that does not build. #142 has the same content with one description and one diff.

Closing rather than leaving three open PRs for one feature.

<!-- greyfinch-sync:Zettrox/ztrx:comment:7164875482 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/141#issuecomment-7164875482). Originally posted by [xiaotu030202](https://github.com/xiaotu030202) on 2026-11-25T14:10:41Z. --- Superseded by #142, which is all three of these in one branch. They were split because each was verifiable on its own, but two of them cannot apply to `main` without the first — so reviewing them apart means reviewing a branch that does not build. #142 has the same content with one description and one diff. Closing rather than leaving three open PRs for one feature.

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
Zettrox/ztrx!141
No description provided.