Translator: lower six scalar and vector ALU instructions #2165

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

@⁠PietroIacomino on GitHub · 2026-11-29 10:04 UTC · original
PietroIacomino:translator-missing-ops → main · +196 −69 · 2 files · 1 commits · open

What it changes

Console-compiled compute shaders use a handful of ALU opcodes that the lowering in emit/lower.rs did not handle, and any one of them was enough to reject the whole shader. This adds them, using only helpers already in lower.rs; no other file changes.

s_sext_i32_i8 and s_sext_i32_i16 shift the byte or half-word to the top of the register and back arithmetically. s_absdiff_i32 computes both a - b and b - a and lets a signed compare pick the non-negative one, wrapping like the hardware, with SCC following the usual "result is not zero" rule. v_mad_u32_u16 and v_mad_i32_i16 take their 16-bit multiplicands through half_src, so op_sel is honoured. The signed form sign-extends both before the multiply, and the 32-bit addend is added in full. v_ldexp_f32 has no DXIL equivalent: the exponent is clamped to ±300 with IMax/IMin and applied as two exp2 factors of half the exponent each, so that a small value with a large exponent, or the reverse, does not overflow the intermediate factor.

How it was tested

  • GPU and profile: not run in game for this PR; unit tests only.
  • Before: shaders containing any of these opcodes were rejected by the lowering.
  • After: in my local build, those shaders translate and pass dxil.dll validation. Their output on screen has not been checked.
  • Wave sizes: the new lowerings are per-lane and do not depend on the wave size; not separately tested at 32 and 64 lanes.
  • cargo test passed for the crates it touches: yes. cargo test -p victory-shader --release on 829188e (2026.11.29.01): 462 passed, 0 failed, 9 ignored.

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: none
**@⁠PietroIacomino** on GitHub · 2026-11-29 10:04 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2165) `PietroIacomino:translator-missing-ops` → `main` · +196 −69 · 2 files · 1 commits · open ## What it changes Console-compiled compute shaders use a handful of ALU opcodes that the lowering in `emit/lower.rs` did not handle, and any one of them was enough to reject the whole shader. This adds them, using only helpers already in `lower.rs`; no other file changes. `s_sext_i32_i8` and `s_sext_i32_i16` shift the byte or half-word to the top of the register and back arithmetically. `s_absdiff_i32` computes both `a - b` and `b - a` and lets a signed compare pick the non-negative one, wrapping like the hardware, with SCC following the usual "result is not zero" rule. `v_mad_u32_u16` and `v_mad_i32_i16` take their 16-bit multiplicands through `half_src`, so `op_sel` is honoured. The signed form sign-extends both before the multiply, and the 32-bit addend is added in full. `v_ldexp_f32` has no DXIL equivalent: the exponent is clamped to ±300 with IMax/IMin and applied as two `exp2` factors of half the exponent each, so that a small value with a large exponent, or the reverse, does not overflow the intermediate factor. ## How it was tested - GPU and profile: not run in game for this PR; unit tests only. - Before: shaders containing any of these opcodes were rejected by the lowering. - After: in my local build, those shaders translate and pass dxil.dll validation. Their output on screen has not been checked. - Wave sizes: the new lowerings are per-lane and do not depend on the wave size; not separately tested at 32 and 64 lanes. - `cargo test` passed for the crates it touches: yes. `cargo test -p victory-shader --release` on 829188e (2026.11.29.01): 462 passed, 0 failed, 9 ignored. ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: none
Console-compiled compute shaders use these opcodes, and any one of them
was enough for the lowering to reject the whole shader.

- s_sext_i32_i8, s_sext_i32_i16: shift the byte or half-word to the top
  of the register, then shift back arithmetically.
- s_absdiff_i32: |a - b| of signed operands, wrapping; a signed compare
  selects between a - b and b - a. SCC follows the usual "result is not
  zero" rule.
- v_mad_u32_u16, v_mad_i32_i16: 16-bit multiplicands (half_src honours
  op_sel), sign-extended for the signed form, plus a 32-bit addend.
- v_ldexp_f32: a * 2^n. DXIL has no ldexp, so n is clamped to +-300
  (IMax/IMin) and applied as two exp2 factors of half the exponent each,
  so a small value with a large exponent (or the reverse) does not
  overflow the intermediate factor.

Only helpers already in lower.rs are used; no other file changes.

Tested: cargo test -p victory-shader --release on 829188e
(2026.11.29.01): 462 passed, 0 failed, 9 ignored.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 10:06:44 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-29 10:25 UTC · original · reviewed

Tested locally merged onto main (829188e9).

  • Merge & Existing Tests: Clean merge in crates/victory-shader/src/emit/lower.rs; cargo test -p victory-shader --lib emit::lower::tests:: passed (25 passed; 0 failed; 1 ignored).

Two review notes before approval:

  1. v_ldexp_f32 exponent split overflows/underflows f32: Clamping n to [-300, 300] and splitting into two steps (half, rest) yields exponents up to ±150. Since f32 overflows to +Inf at 2^128 and becomes subnormal (flushing to 0.0 under FTZ) below 2^-126, cases like a = 2^-140, n = 260 compute a * exp2(130) = +Inf instead of 2^120, and a = 2^120, n = -256 can flush to 0.0 instead of 2^-136. Splitting n into three steps (each within [-100, 100]) guarantees every intermediate exp2 factor stays strictly normal.
  2. Test coverage: None of the 6 newly lowered instructions (s_sext_i32_i8, s_sext_i32_i16, s_absdiff_i32, v_ldexp_f32, v_mad_u32_u16, v_mad_i32_i16) have unit or GPU tests yet — adding a targeted test (especially for v_ldexp_f32 corner exponents and v_mad_i32_i16 sign-extension/op_sel) would lock down correctness.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-29 10:25 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2165#pullrequestreview-6102463842) · **reviewed** Tested locally merged onto `main` (`829188e9`). - **Merge & Existing Tests**: Clean merge in `crates/victory-shader/src/emit/lower.rs`; `cargo test -p victory-shader --lib emit::lower::tests::` passed (`25 passed; 0 failed; 1 ignored`). Two review notes before approval: 1. **`v_ldexp_f32` exponent split overflows/underflows `f32`**: Clamping `n` to `[-300, 300]` and splitting into two steps (`half`, `rest`) yields exponents up to `±150`. Since `f32` overflows to `+Inf` at `2^128` and becomes subnormal (flushing to `0.0` under FTZ) below `2^-126`, cases like `a = 2^-140, n = 260` compute `a * exp2(130) = +Inf` instead of `2^120`, and `a = 2^120, n = -256` can flush to `0.0` instead of `2^-136`. Splitting `n` into three steps (each within `[-100, 100]`) guarantees every intermediate `exp2` factor stays strictly normal. 2. **Test coverage**: None of the 6 newly lowered instructions (`s_sext_i32_i8`, `s_sext_i32_i16`, `s_absdiff_i32`, `v_ldexp_f32`, `v_mad_u32_u16`, `v_mad_i32_i16`) have unit or GPU tests yet — adding a targeted test (especially for `v_ldexp_f32` corner exponents and `v_mad_i32_i16` sign-extension/op_sel) would lock down correctness. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Review follow-up.

- v_ldexp_f32: the two exp2 halves of an exponent up to 300 reached
  2^150, past f32's range, so a tiny normal value with a large exponent
  came out as NaN (0 * inf) instead of its normal result. The clamped
  exponent is now applied as three factors, n/3, n/3 and the rest (at
  most 101 in size), each built exactly from its exponent bits instead
  of through exp2, so every factor is a normal f32. The running product
  moves monotonically from a towards the result, so it leaves the range
  only where the result does.
- s_absdiff_i32: follows the ISA's definition, the wrapped difference
  negated when negative (i32::MIN - 1 gives 0x7fffffff), instead of the
  mathematical |a - b|.
- valu_tests: one GPU test per instruction, run on the hardware adapter
  and WARP: s_sext_i32_i8/i16 at sign boundaries; s_absdiff_i32 with
  overflowing differences and its SCC; v_mad_u32_u16 and v_mad_i32_i16
  with op_sel on each source and negative halves; v_ldexp_f32 with
  exponents whose halves overflow a factor, past the clamp, infinity,
  zero, and the two cases from the review. The ldexp and absdiff tests
  fail on the previous commit.

Tested: cargo test -p victory-shader --release on 829188e plus this
branch: 466 passed, 0 failed, 9 ignored.
Author
Owner

@⁠PietroIacomino on GitHub · 2026-11-29 11:02 UTC · original

Thanks for the review. Both points are addressed in 196d406.

v_ldexp_f32: you were right, and on the old code your first case was worse than +Inf. Both halves overflowed, the tiny input flushed, and the result was NaN (0 * inf). The clamped exponent is now applied as three factors, n/3, n/3 and the remainder (at most 101 in size). Each factor is built exactly from its exponent bits rather than through exp2, so it is always a normal f32 and does not depend on exp2 precision. The running product moves monotonically from a towards the result, so it leaves the f32 range only where the result itself does.

While writing the tests I also found that s_absdiff_i32 computed the mathematical |a - b|. The ISA defines it as the wrapped difference, negated when negative, so i32::MIN - 1 gives 0x7fffffff, not 0x80000001. It now follows the ISA.

Tests: one GPU test per instruction in valu_tests.rs, run on the hardware adapter and on WARP:

  • s_sext_i32_i8/i16 at the sign boundaries;
  • s_absdiff_i32 with overflowing differences, plus its SCC;
  • v_mad_u32_u16 and v_mad_i32_i16 with op_sel on each source and negative halves;
  • v_ldexp_f32 with exponents whose halves overflow a factor, exponents past the clamp, infinity and zero, plus your two cases. Where the input or the result is denormal, a flushed zero of the right sign is accepted.

The ldexp and absdiff tests fail on the previous commit.

cargo test -p victory-shader --release: 466 passed, 0 failed, 9 ignored.

**@⁠PietroIacomino** on GitHub · 2026-11-29 11:02 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2165#issuecomment-7254834297) Thanks for the review. Both points are addressed in 196d406. v_ldexp_f32: you were right, and on the old code your first case was worse than +Inf. Both halves overflowed, the tiny input flushed, and the result was NaN (0 * inf). The clamped exponent is now applied as three factors, n/3, n/3 and the remainder (at most 101 in size). Each factor is built exactly from its exponent bits rather than through exp2, so it is always a normal f32 and does not depend on exp2 precision. The running product moves monotonically from a towards the result, so it leaves the f32 range only where the result itself does. While writing the tests I also found that s_absdiff_i32 computed the mathematical |a - b|. The ISA defines it as the wrapped difference, negated when negative, so i32::MIN - 1 gives 0x7fffffff, not 0x80000001. It now follows the ISA. Tests: one GPU test per instruction in valu_tests.rs, run on the hardware adapter and on WARP: - s_sext_i32_i8/i16 at the sign boundaries; - s_absdiff_i32 with overflowing differences, plus its SCC; - v_mad_u32_u16 and v_mad_i32_i16 with op_sel on each source and negative halves; - v_ldexp_f32 with exponents whose halves overflow a factor, exponents past the clamp, infinity and zero, plus your two cases. Where the input or the result is denormal, a flushed zero of the right sign is accepted. The ldexp and absdiff tests fail on the previous commit. cargo test -p victory-shader --release: 466 passed, 0 failed, 9 ignored.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-30 06:11 UTC · original

Checked the six lowerings against a Radeon gfx1036 (RDNA 2) running the same kernels natively, and ran the translator's output for main and for this PR on an RTX 5080 and on that Radeon through D3D12.

Setup

  • main at 41adb551 with this PR's head (196d406) merged locally, no conflict. The victory-shader test binary was built from that merged tree (own tree and target directory).
  • Kernels: hand-written GFX10 code, nothing from a title. Wave32, a 64-thread group (two waves); per-lane inputs from tables on the thread id; outputs v10.. stored per lane.
  • Hardware rows: the same machine code dispatched natively on the gfx1036, MODE IEEE on with f32 denormals kept, and once more with IEEE off and denormals flushed (that only changes ldexp lanes with a denormal input or result).
  • Host runs: super::emit (32-lane host), dxil.dll validation, dispatched on the 5080 and on the Radeon (VICTORY_ADAPTER=radeon). On main every case below except sext_scc (whose s_sext results are dead) is refused (s_sext_i32_i8 / s_absdiff_i32 / v_ldexp_f32 / v_mad_u32_u16 is not supported).

Cases

  • sext/scc0, sext/scc1: s_sext_i32_i8 and s_sext_i32_i16 of 8 values (0x7F, 0x80, 0xFF, 0x12345680, 0x8000, 0xFFFF7FFF, 0x7FFF, 0xABCD), SCC set to 0 or 1 before each. sext_scc: SCC read back after them.
  • absdiff: s_absdiff_i32 of 8 pairs with overflowing differences ((0x80000000, 1), (1, 0x80000000), (0, 0x80000000), (0x7FFFFFFF, 0xFFFFFFFF), ...), result and SCC. The gfx1036 gives the ISA's wrapped difference, negated when negative, and SCC = result != 0, in all 8 (0x80000000 - 1 gives 0x7FFFFFFF, 0 - 0x80000000 gives 0x80000000).
  • ldexp: v_ldexp_f32 of 64 (a, n) pairs: n from -1000 to 1000 (±127..151, 276, 277, 300 among them) and 0x7FFFFFFF, 0x80000000; a normal, denormal, ±0, ±inf, qNaN, sNaN, max. v10 = ldexp(a, n), v13 = ldexp(-a, n), v14 = ldexp(|a|, 3). The gfx1036 result is a times 2^n rounded once (to nearest even, denormals kept) in every finite lane; an sNaN comes back quiet.
  • ldexp_big: 16 pairs with normal a and |n| from 150 to 300, most with a normal result (2^-100 with n = 200, 2^100 with n = -200, 2^-126 with n = 240, 1.5 * 2^-110 with n = 230, -1.25 * 2^90 with n = -210, ...), and ±0 / ±inf with |n| of 200 to 300. v10 = ldexp(a, n), v11 = ldexp(-a, n).
  • mad16: v_mad_u32_u16 and v_mad_i32_i16 over 64 (a, b, d) combinations, with op_sel [0,0], [1,0], [0,1], [1,1], a literal multiplicand (0xFFFE), and with clamp. mad16_noclamp: the same without the two clamp instructions. With clamp the gfx1036 saturates: to 0xFFFFFFFF for the unsigned form, to 0x7FFFFFFF / 0x80000000 for the signed one (8 values in 6 lanes overflow).

Results (values that differ from the gfx1036; "refused" = the translator rejects the program)

case main, both cards this PR, 5080 this PR, Radeon
sext/scc0, sext/scc1 (16 outputs) refused exact exact
sext_scc (SCC kept) exact exact exact
absdiff (result and SCC) refused exact exact
mad16_noclamp (10 outputs) refused exact exact
mad16 (with the two clamp forms) refused refused (integer output modifiers) refused (the same)
ldexp v10, v13 (64 pairs) refused 40 each: 34 the flushed denormal, 6 other 38 each, all the flushed denormal
ldexp v14 (abs(a), n = 3) refused 18: 16 flushed, 2 other 17, all flushed
ldexp_big v10, v11 (16 pairs, 4 times over) refused 48 each: 11 of the 16 pairs wrong 4 each (one pair, a denormal result flushed)

"The flushed denormal": the value the gfx1036 gives for that lane with IEEE off and f32 denormals flushed. Every value that differs on the Radeon is one of these, so the Radeon gives exactly the hardware's FTZ results.

Verdict: s_sext_i32_i8 / _i16, s_absdiff_i32 and v_mad_u32_u16 / v_mad_i32_i16 without clamp are correct on both cards. With clamp the program is refused, which is safe (the gfx1036 saturates there: 8 values in 6 lanes overflow). v_ldexp_f32 is correct on the Radeon apart from denormals, and wrong on the 5080 for |n| of about 192 and more.

On the 5080 (and only there), with this PR:

a n gfx1036 5080
2^-100 (0x0D800000) 200 2^100 (0x71800000) +inf
2^-126 (0x00800000) 240 2^114 (0x78800000) +inf
1.5 * 2^-110 (0x08C00000) 230 0x7BC00000 +inf
1.5 * 2^-126 (0x00C00000) 250 0x7DC00000 +inf
2^100 (0x71800000) -200 2^-100 (0x0D800000) 0
2^126 (0x7E800000) -240 0x06800000 0
-1.25 * 2^90 (0xECA00000) -210 0x83A00000 -0
±0 300, 250 ±0 NaN (0x7FFFFFFF)
±inf -200, -300, -1000 ±inf NaN
0x00000001 276, 277, 300 0x7F000000, +inf, +inf (0 with denormals flushed) NaN
qNaN 0x7FC00001, sNaN 0x7F800001 3 0x7FC00001 0x7FFFFFFF

The Radeon gives the gfx1036 value in every row but the last two: for the denormal input it gives 0 (the hardware's FTZ value), and it passes the sNaN through unquieted (as the hardware does with IEEE off). The same DXIL is dispatched on both, so this is the NVIDIA compiler. The pattern fits it multiplying the two equal n/3 factors together first, a * (f * f) * g: f * f leaves the f32 range once 2 * (n / 3) passes 127 or -126, whatever a is. (2^-100 with n = 150 has three equal factors 2^50 and is exact, so it is not the full product of the factors.) Any order of exact power-of-two multiplies is open to this under fast-math. Adding n to the exponent field in integer arithmetic where a is normal and the result exponent stays in 1..254, and keeping the factors only for the rest, would not depend on the host's reordering.

Lanes with a denormal input or result come back flushed on both cards (34 / 38 values in ldexp, all equal to the gfx1036's FTZ value), as your tests allow. The PR's three-factor product would also round twice there (for example 0x00000005 with n = -3: one rounding gives 0x00000001, the three factors 0), but neither host keeps those denormals, so that does not show.

AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.

**@⁠Lyrnc** on GitHub · 2026-11-30 06:11 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2165#issuecomment-7270391807) Checked the six lowerings against a Radeon gfx1036 (RDNA 2) running the same kernels natively, and ran the translator's output for main and for this PR on an RTX 5080 and on that Radeon through D3D12. **Setup** - `main` at 41adb551 with this PR's head (196d406) merged locally, no conflict. The victory-shader test binary was built from that merged tree (own tree and target directory). - Kernels: hand-written GFX10 code, nothing from a title. Wave32, a 64-thread group (two waves); per-lane inputs from tables on the thread id; outputs v10.. stored per lane. - Hardware rows: the same machine code dispatched natively on the gfx1036, MODE IEEE on with f32 denormals kept, and once more with IEEE off and denormals flushed (that only changes `ldexp` lanes with a denormal input or result). - Host runs: `super::emit` (32-lane host), `dxil.dll` validation, dispatched on the 5080 and on the Radeon (`VICTORY_ADAPTER=radeon`). On main every case below except `sext_scc` (whose `s_sext` results are dead) is refused (`s_sext_i32_i8` / `s_absdiff_i32` / `v_ldexp_f32` / `v_mad_u32_u16 is not supported`). **Cases** - `sext/scc0`, `sext/scc1`: `s_sext_i32_i8` and `s_sext_i32_i16` of 8 values (0x7F, 0x80, 0xFF, 0x12345680, 0x8000, 0xFFFF7FFF, 0x7FFF, 0xABCD), SCC set to 0 or 1 before each. `sext_scc`: SCC read back after them. - `absdiff`: `s_absdiff_i32` of 8 pairs with overflowing differences ((0x80000000, 1), (1, 0x80000000), (0, 0x80000000), (0x7FFFFFFF, 0xFFFFFFFF), ...), result and SCC. The gfx1036 gives the ISA's wrapped difference, negated when negative, and SCC = result != 0, in all 8 (0x80000000 - 1 gives 0x7FFFFFFF, 0 - 0x80000000 gives 0x80000000). - `ldexp`: `v_ldexp_f32` of 64 (a, n) pairs: n from -1000 to 1000 (±127..151, 276, 277, 300 among them) and 0x7FFFFFFF, 0x80000000; a normal, denormal, ±0, ±inf, qNaN, sNaN, max. v10 = ldexp(a, n), v13 = ldexp(-a, n), v14 = ldexp(|a|, 3). The gfx1036 result is a times 2^n rounded once (to nearest even, denormals kept) in every finite lane; an sNaN comes back quiet. - `ldexp_big`: 16 pairs with normal a and |n| from 150 to 300, most with a normal result (2^-100 with n = 200, 2^100 with n = -200, 2^-126 with n = 240, 1.5 * 2^-110 with n = 230, -1.25 * 2^90 with n = -210, ...), and ±0 / ±inf with |n| of 200 to 300. v10 = ldexp(a, n), v11 = ldexp(-a, n). - `mad16`: `v_mad_u32_u16` and `v_mad_i32_i16` over 64 (a, b, d) combinations, with `op_sel` [0,0], [1,0], [0,1], [1,1], a literal multiplicand (0xFFFE), and with `clamp`. `mad16_noclamp`: the same without the two `clamp` instructions. With `clamp` the gfx1036 saturates: to 0xFFFFFFFF for the unsigned form, to 0x7FFFFFFF / 0x80000000 for the signed one (8 values in 6 lanes overflow). **Results** (values that differ from the gfx1036; "refused" = the translator rejects the program) | case | main, both cards | this PR, 5080 | this PR, Radeon | |---|---|---|---| | `sext/scc0`, `sext/scc1` (16 outputs) | refused | exact | exact | | `sext_scc` (SCC kept) | exact | exact | exact | | `absdiff` (result and SCC) | refused | exact | exact | | `mad16_noclamp` (10 outputs) | refused | exact | exact | | `mad16` (with the two `clamp` forms) | refused | refused (`integer output modifiers`) | refused (the same) | | `ldexp` v10, v13 (64 pairs) | refused | 40 each: 34 the flushed denormal, 6 other | 38 each, all the flushed denormal | | `ldexp` v14 (`abs(a)`, n = 3) | refused | 18: 16 flushed, 2 other | 17, all flushed | | `ldexp_big` v10, v11 (16 pairs, 4 times over) | refused | 48 each: 11 of the 16 pairs wrong | 4 each (one pair, a denormal result flushed) | "The flushed denormal": the value the gfx1036 gives for that lane with IEEE off and f32 denormals flushed. Every value that differs on the Radeon is one of these, so the Radeon gives exactly the hardware's FTZ results. **Verdict: `s_sext_i32_i8` / `_i16`, `s_absdiff_i32` and `v_mad_u32_u16` / `v_mad_i32_i16` without `clamp` are correct on both cards. With `clamp` the program is refused, which is safe (the gfx1036 saturates there: 8 values in 6 lanes overflow). `v_ldexp_f32` is correct on the Radeon apart from denormals, and wrong on the 5080 for |n| of about 192 and more.** On the 5080 (and only there), with this PR: | a | n | gfx1036 | 5080 | |---|---|---|---| | 2^-100 (0x0D800000) | 200 | 2^100 (0x71800000) | +inf | | 2^-126 (0x00800000) | 240 | 2^114 (0x78800000) | +inf | | 1.5 * 2^-110 (0x08C00000) | 230 | 0x7BC00000 | +inf | | 1.5 * 2^-126 (0x00C00000) | 250 | 0x7DC00000 | +inf | | 2^100 (0x71800000) | -200 | 2^-100 (0x0D800000) | 0 | | 2^126 (0x7E800000) | -240 | 0x06800000 | 0 | | -1.25 * 2^90 (0xECA00000) | -210 | 0x83A00000 | -0 | | ±0 | 300, 250 | ±0 | NaN (0x7FFFFFFF) | | ±inf | -200, -300, -1000 | ±inf | NaN | | 0x00000001 | 276, 277, 300 | 0x7F000000, +inf, +inf (0 with denormals flushed) | NaN | | qNaN 0x7FC00001, sNaN 0x7F800001 | 3 | 0x7FC00001 | 0x7FFFFFFF | The Radeon gives the gfx1036 value in every row but the last two: for the denormal input it gives 0 (the hardware's FTZ value), and it passes the sNaN through unquieted (as the hardware does with IEEE off). The same DXIL is dispatched on both, so this is the NVIDIA compiler. The pattern fits it multiplying the two equal `n/3` factors together first, a * (f * f) * g: f * f leaves the f32 range once 2 * (n / 3) passes 127 or -126, whatever a is. (2^-100 with n = 150 has three equal factors 2^50 and is exact, so it is not the full product of the factors.) Any order of exact power-of-two multiplies is open to this under fast-math. Adding n to the exponent field in integer arithmetic where a is normal and the result exponent stays in 1..254, and keeping the factors only for the rest, would not depend on the host's reordering. Lanes with a denormal input or result come back flushed on both cards (34 / 38 values in `ldexp`, all equal to the gfx1036's FTZ value), as your tests allow. The PR's three-factor product would also round twice there (for example 0x00000005 with n = -3: one rounding gives 0x00000001, the three factors 0), but neither host keeps those denormals, so that does not show. AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.
A product of power-of-two factors is not safe: a host compiler may multiply
the factors together first, and that product leaves the f32 range for large
n where the result does not (+inf, 0 and NaN for normal results on an
RTX 5080), and a running product that passes through the denormal range is
flushed (ldexp(0xbfffffff, -127) gave -0, not 0x80800000, on an RTX 4070 Ti).
n is now added to the exponent in integer arithmetic; a result below the
normal range is rounded once to nearest even from the full mantissa; zeros
and infinities pass through, a NaN comes back quieted; a denormal a is made
normal by an exact multiply by 2^64 first. abs and neg on its source are
applied to the sign bit, as the hardware does: the host's float negate gave
0x7fffffff for -0x7fc00001.

ldexp_matches_the_isa replaces ldexp_keeps_every_factor_normal: 25 values of
a by 42 of n by three forms (plain, neg, abs), against a times 2^n rounded
once; a zero of the right sign is taken only where the input or the result
is denormal.

Tested: cargo test -p victory-shader --release: 466 passed, 0 failed,
9 ignored (RTX 4070 Ti and WARP).
Author
Owner

@⁠PietroIacomino on GitHub · 2026-11-30 09:32 UTC · original

Thanks for the oracle runs. The diagnosis matches what I see: the factor product is only safe while the host keeps the multiplication order, so I replaced it.

v_ldexp_f32 now adds n to the exponent field in integer arithmetic. No float operation is left for a host compiler to reorder:

  • n is clamped to ±300 first.
  • Normal result: the sign and mantissa are kept, and the exponent is moved by n.
  • Exponent past 254: infinity with the sign of a.
  • Below the smallest normal: the denormal, rounded once to nearest even from the full 24-bit mantissa (a zero of a's sign once nothing is left). So the double rounding you pointed out (e.g. 0x00000005 with n = -3) is gone too.
  • Special values of a: ±0 and ±inf come back as themselves, and a NaN comes back quieted (payload kept, 0x7F800001 → 0x7FC00001).
  • Denormal a: it is made normal by an exact multiply by 2^64 and n is lowered by 64. A host that flushes denormal inputs gives a zero of a's sign there, which is the hardware's FTZ value.

abs/neg on src0 of v_ldexp_f32 are now applied to the sign bit alone. The float negate the other instructions use gave 0x7FFFFFFF for -0x7FC00001 on an RTX 4070 Ti, while the hardware only flips the sign bit. Your v13 (ldexp(-a, n)) and v14 (ldexp(|a|, 3)) rows go through this path.

Test. ldexp_matches_the_isa replaces ldexp_keeps_every_factor_normal:

  • Inputs: 25 values of a × 42 values of n × 3 forms (plain, neg, abs) = 3,150 cases.
    • a covers all the normal inputs from your table (2^-100, 2^-126, 1.5·2^-110, 1.5·2^-126, 2^100, 2^126, -1.25·2^90), the largest finite, 1 + 2^-23 for rounding at every denormal shift, denormals (0x1, 0x5, 0x7FFFFF, 0x80400000), ±0, ±inf, a quiet NaN and a signalling NaN.
    • n runs from i32::MIN and -1000 to 1000 and i32::MAX, including ±125…151, 200, 210, 230, 240, 250, 276, 277 and 300.
  • Reference: a times 2^n computed exactly in f64 and rounded once to f32; a NaN is expected quieted with its payload kept.
  • Tolerance: a zero of the right sign is accepted only where the input or the result is denormal.
  • Result: every case passes on an RTX 4070 Ti and on WARP.

The old implementation fails this test on the 4070 Ti as well, not only on the 5080. ldexp(0xBFFFFFFF, -127) gave 0x80000000 instead of 0x80800000: the running product went through the denormal range, was flushed, and the result, which is normal, came back as -0.

I could not run it on an RTX 5080 or a Radeon. If you can repeat your oracle runs (ldexp, ldexp_big) on this head, that would confirm the reorder case on the card where you saw it.

v_mad_u32_u16 / v_mad_i32_i16 with clamp are still refused, as before.

Tested: cargo test -p victory-shader --release: 466 passed, 0 failed, 9 ignored (RTX 4070 Ti, WARP).

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this comment.

**@⁠PietroIacomino** on GitHub · 2026-11-30 09:32 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2165#issuecomment-7274352494) Thanks for the oracle runs. The diagnosis matches what I see: the factor product is only safe while the host keeps the multiplication order, so I replaced it. **`v_ldexp_f32` now adds `n` to the exponent field in integer arithmetic.** No float operation is left for a host compiler to reorder: - `n` is clamped to ±300 first. - **Normal result:** the sign and mantissa are kept, and the exponent is moved by `n`. - **Exponent past 254:** infinity with the sign of `a`. - **Below the smallest normal:** the denormal, rounded once to nearest even from the full 24-bit mantissa (a zero of `a`'s sign once nothing is left). So the double rounding you pointed out (e.g. `0x00000005` with `n = -3`) is gone too. - **Special values of `a`:** ±0 and ±inf come back as themselves, and a NaN comes back quieted (payload kept, `0x7F800001` → `0x7FC00001`). - **Denormal `a`:** it is made normal by an exact multiply by 2^64 and `n` is lowered by 64. A host that flushes denormal inputs gives a zero of `a`'s sign there, which is the hardware's FTZ value. **`abs`/`neg` on `src0` of `v_ldexp_f32` are now applied to the sign bit alone.** The float negate the other instructions use gave `0x7FFFFFFF` for `-0x7FC00001` on an RTX 4070 Ti, while the hardware only flips the sign bit. Your `v13` (`ldexp(-a, n)`) and `v14` (`ldexp(|a|, 3)`) rows go through this path. **Test.** `ldexp_matches_the_isa` replaces `ldexp_keeps_every_factor_normal`: - **Inputs:** 25 values of `a` × 42 values of `n` × 3 forms (plain, `neg`, `abs`) = 3,150 cases. - `a` covers all the normal inputs from your table (2^-100, 2^-126, 1.5·2^-110, 1.5·2^-126, 2^100, 2^126, -1.25·2^90), the largest finite, 1 + 2^-23 for rounding at every denormal shift, denormals (`0x1`, `0x5`, `0x7FFFFF`, `0x80400000`), ±0, ±inf, a quiet NaN and a signalling NaN. - `n` runs from `i32::MIN` and -1000 to 1000 and `i32::MAX`, including ±125…151, 200, 210, 230, 240, 250, 276, 277 and 300. - **Reference:** `a` times 2^n computed exactly in f64 and rounded once to f32; a NaN is expected quieted with its payload kept. - **Tolerance:** a zero of the right sign is accepted only where the input or the result is denormal. - **Result:** every case passes on an RTX 4070 Ti and on WARP. The old implementation fails this test on the 4070 Ti as well, not only on the 5080. `ldexp(0xBFFFFFFF, -127)` gave `0x80000000` instead of `0x80800000`: the running product went through the denormal range, was flushed, and the result, which is normal, came back as -0. I could not run it on an RTX 5080 or a Radeon. If you can repeat your oracle runs (`ldexp`, `ldexp_big`) on this head, that would confirm the reorder case on the card where you saw it. `v_mad_u32_u16` / `v_mad_i32_i16` with clamp are still refused, as before. Tested: `cargo test -p victory-shader --release`: 466 passed, 0 failed, 9 ignored (RTX 4070 Ti, WARP). AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this comment.
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2165'
git switch 'github-bot/gh-2165'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2165'
git switch 'github-bot/gh-2165'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2165'
git switch 'main'
git merge --squash 'github-bot/gh-2165'
git switch 'main'
git merge --ff-only 'github-bot/gh-2165'
git switch 'main'
git merge 'github-bot/gh-2165'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!2165
No description provided.