Translator: lower six scalar and vector ALU instructions #2165

Open
PietroIacomino wants to merge 1 commit from upstream-pr/2165 into main
PietroIacomino commented 2026-11-29 10:04:41 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by PietroIacomino on 2026-11-29T10:04:41Z.


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
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5549665963 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2165). Originally posted by [PietroIacomino](https://github.com/PietroIacomino) on 2026-11-29T10:04:41Z. --- ## 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
Dukestar69 commented 2026-11-29 10:25:25 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-29T10:25:25Z.

Review: COMMENTED


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).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6102463842 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2165#pullrequestreview-6102463842). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-29T10:25:25Z. **Review:** COMMENTED --- 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).
PietroIacomino commented 2026-11-29 11:02:05 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by PietroIacomino on 2026-11-29T11:02:05Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7254834297 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2165#issuecomment-7254834297). Originally posted by [PietroIacomino](https://github.com/PietroIacomino) on 2026-11-29T11:02:05Z. --- 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.
Lyrnc commented 2026-11-30 06:11:16 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Lyrnc on 2026-11-30T06:11:16Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7270391807 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2165#issuecomment-7270391807). Originally posted by [Lyrnc](https://github.com/Lyrnc) on 2026-11-30T06:11:16Z. --- 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.
PietroIacomino commented 2026-11-30 09:32:41 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by PietroIacomino on 2026-11-30T09:32:41Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7274352494 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2165#issuecomment-7274352494). Originally posted by [PietroIacomino](https://github.com/PietroIacomino) on 2026-11-30T09:32:41Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2165:upstream-pr/2165'
git switch 'upstream-pr/2165'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2165'
git switch 'upstream-pr/2165'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2165'
git switch 'upstream-pr/2165'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2165'
git switch 'main'
git merge --squash 'upstream-pr/2165'
git switch 'main'
git merge --ff-only 'upstream-pr/2165'
git switch 'main'
git merge 'upstream-pr/2165'
git push origin 'main'
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
trustno1-coder/VIctory-NVIDIA!2165
No description provided.