victory-shader: med3 with NaN, fract near 1.0 and signed bfe past bit 31 differ from RDNA2 hardware #2

Closed
opened 2026-11-26 03:46:09 +00:00 by github-bot · 2 comments
Owner

@⁠Daazd on GitHub · 2026-11-23 22:35 UTC · original

I compared victory-shader's VALU lowering (crates/victory-shader/src/emit/lower.rs at 015d45b) against RDNA2 behaviour that the Leonida project measured on hardware. Leonida translates the same instruction set to SPIR-V, and records each measurement in its docs/design.md contract table. Three lowerings give different results from the hardware. All inputs below are synthetic. I have no Windows machine here, so none of this is run through host.rs yet. I'm filing it as an issue first in case you'd rather fix it your own way.

1. v_med3_f32 / v_med3_f16 with a NaN operand

lower.rs:3627 (and :4224 for f16) builds med3 as max(min(a,b), min(max(a,b), c)). On RDNA2, any NaN operand makes med3 return min3(s0, s1, s2) (RX 6800 gfx1030 and gfx1035/1036, all NaN positions, wave32 and wave64).

inputs lowering hardware
med3(1, 2, NaN) 2 1
med3(NaN, 1, 0) 1 0
med3(NaN, 0, 1) (the usual clamp order) 0 0

So the common clamp(x, 0, 1) form happens to agree, but any other operand order with a NaN does not. A fix: when any operand is NaN, select min(min(a,b),c); otherwise use the current median.

2. v_fract_f32 / v_fract_f16 near 1.0

lower.rs:3652 maps to DXIL Frc (x − floor x). The hardware returns the largest value below 1.0 (0x3f7fffff; f16 0x3bff) where that would round to 1.0, e.g. fract(-1e-10): hardware 0x3f7fffff, Frc gives 1.0. fract(±inf) is NaN on both. A fix: select(r == 1.0, 0x3f7fffff, r).

3. v_bfe_i32 / s_bfe_i32 when the field runs past bit 31

bfe() (lower.rs:1360) shifts right logically before masking, so a signed field cut off at bit 31 is not sign-extended from bit 31. Measured on gfx1036 (Leonida, row "v_bfe_u32 / v_bfe_i32 past bit 31"):

op a offset width lowering hardware
v_bfe_i32 0xd2f90159 28 16 0x0000000d 0xfffffffd
v_bfe_i32 0x80000000 31 4 0x00000008 0xffffffff

A fix: use Bin::AShr instead of Bin::LShr for shifted when signed. The mask and the sign-extension step then give the hardware result in every case, and the unsigned path is unchanged. s_bfe_i32 follows the same rule (the same row for s_bfe).

Lower priority, for completeness

  • v_sin/v_cos: scaling by 2π before Sin/Cos loses precision for large angles (about 1.2e-4 rad near 255 turns). Reducing in turns first (x - round_even(x), then × 2π) fixes it. The hardware also gives exactly +0 at half turns (sin) and quarter turns (cos), where the result above gives about ±8.7e-8.
  • legacy_mul: the hardware reads a denormal operand as zero, so the +0 rule also applies to it. The current == 0.0 test leaves that to the driver's denorm mode.

The v_bfe width masking (m31), the shift-amount masking, v_ffbh/s_ff1/s_flbit, and v_cvt_pkrtz_f16_f32 overflow (stepping back to 65504) already match.

If any of these are welcome as a PR, I'm happy to send one (starting with #3, the smallest). Since I can't run host.rs tests, it would come with the expected words for a Windows test.

**@⁠Daazd** on GitHub · 2026-11-23 22:35 UTC · [original](https://github.com/Marvisak/VIctory/issues/2) I compared `victory-shader`'s VALU lowering (`crates/victory-shader/src/emit/lower.rs` at `015d45b`) against RDNA2 behaviour that the Leonida project measured on hardware. Leonida translates the same instruction set to SPIR-V, and records each measurement in its [`docs/design.md`](https://github.com/ItsNotRexed/Leonida/blob/main/docs/design.md) contract table. Three lowerings give different results from the hardware. All inputs below are synthetic. I have no Windows machine here, so none of this is run through `host.rs` yet. I'm filing it as an issue first in case you'd rather fix it your own way. ### 1. `v_med3_f32` / `v_med3_f16` with a NaN operand `lower.rs:3627` (and `:4224` for f16) builds med3 as `max(min(a,b), min(max(a,b), c))`. On RDNA2, **any NaN operand makes med3 return `min3(s0, s1, s2)`** (RX 6800 gfx1030 and gfx1035/1036, all NaN positions, wave32 and wave64). | inputs | lowering | hardware | |---|---|---| | `med3(1, 2, NaN)` | 2 | 1 | | `med3(NaN, 1, 0)` | 1 | 0 | | `med3(NaN, 0, 1)` (the usual clamp order) | 0 | 0 | So the common `clamp(x, 0, 1)` form happens to agree, but any other operand order with a NaN does not. A fix: when any operand is NaN, select `min(min(a,b),c)`; otherwise use the current median. ### 2. `v_fract_f32` / `v_fract_f16` near 1.0 `lower.rs:3652` maps to DXIL `Frc` (x − floor x). The hardware returns the largest value below 1.0 (`0x3f7fffff`; f16 `0x3bff`) where that would round to 1.0, e.g. **`fract(-1e-10)`: hardware `0x3f7fffff`, `Frc` gives 1.0**. `fract(±inf)` is NaN on both. A fix: `select(r == 1.0, 0x3f7fffff, r)`. ### 3. `v_bfe_i32` / `s_bfe_i32` when the field runs past bit 31 `bfe()` (`lower.rs:1360`) shifts right logically before masking, so a signed field cut off at bit 31 is not sign-extended from bit 31. Measured on gfx1036 (Leonida, row "`v_bfe_u32` / `v_bfe_i32` past bit 31"): | op | a | offset | width | lowering | hardware | |---|---|---|---|---|---| | `v_bfe_i32` | `0xd2f90159` | 28 | 16 | `0x0000000d` | `0xfffffffd` | | `v_bfe_i32` | `0x80000000` | 31 | 4 | `0x00000008` | `0xffffffff` | A fix: use `Bin::AShr` instead of `Bin::LShr` for `shifted` when `signed`. The mask and the sign-extension step then give the hardware result in every case, and the unsigned path is unchanged. `s_bfe_i32` follows the same rule (the same row for `s_bfe`). ### Lower priority, for completeness - `v_sin/v_cos`: scaling by 2π before `Sin`/`Cos` loses precision for large angles (about 1.2e-4 rad near 255 turns). Reducing in turns first (`x - round_even(x)`, then × 2π) fixes it. The hardware also gives exactly +0 at half turns (sin) and quarter turns (cos), where the result above gives about ±8.7e-8. - `legacy_mul`: the hardware reads a denormal operand as zero, so the +0 rule also applies to it. The current `== 0.0` test leaves that to the driver's denorm mode. The `v_bfe` width masking (`m31`), the shift-amount masking, `v_ffbh`/`s_ff1`/`s_flbit`, and `v_cvt_pkrtz_f16_f32` overflow (stepping back to 65504) already match. If any of these are welcome as a PR, I'm happy to send one (starting with #3, the smallest). Since I can't run `host.rs` tests, it would come with the expected words for a Windows test.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:46:09 +00:00
Author
Owner

@⁠MiguelMatMar on GitHub · 2026-11-23 22:44 UTC · original

Windows results for this, on an NVIDIA RTX 4080 SUPER (driver 621.24, Windows 11), at 015d45b. AI-assisted (Claude Code), synthetic inputs only.

cargo test --workspace: everything passes (about 220 tests; 3 ignored, the ones that need files outside the repo). The diff_tests skip without the console dxc, so they don't cover this.

I can't drive lower.rs without a console container, so I checked the host side instead. A throwaway local test (not committed) compiled the same expressions with the Windows SDK dxc (cs_6_5) and ran them through Host::compute_pipeline_with + Host::dispatch. Inputs came from root constants, so nothing was constant-folded.

expression (what the lowering emits) RTX 4080 SUPER WARP RDNA2 (from the issue)
frac(-1e-10) (DXIL Frc) 0x3f800000 (1.0) 0x3f7fffff 0x3f7fffff
frac(-1e-8) 0x3f800000 0x3f7fffff –
max(min(1,2), min(max(1,2), NaN)) (med3 1,2,NaN) 2 2 1
med3 lowering (NaN, 1, 0) 1 1 0
med3 lowering (NaN, 0, 1) 0 0 0
sin(255.5 * 2π) (scaled before Sin) 9.57e-5 -8.74e-8 exactly +0
  • fract: Frc returning 1.0 depends on the driver. NVIDIA gives 1.0 and WARP already clamps. The proposed select(r == 1.0, 0x3f7fffff, r) makes them agree.
  • med3: both host devices follow IEEE minNum/maxNum, so the current lowering gives the median of the non-NaN pair, not RDNA2's min3. The issue is confirmed on both.
  • sin/cos: on NVIDIA the error at 255.5 turns is about 1e-4, the same order the issue gives. Reducing in turns first should fix it.
  • v_bfe_i32: I traced bfe() (lower.rs:1360) by hand. LShr before the mask drops the sign. One correction to the table: for a = 0x80000000, off 31, width 4 the current lowering gives 0x00000001, not 0x00000008: the shift leaves 1, the mask keeps it, and a 4-bit field 0001 sign-extends to 1. Hardware gives 0xffffffff. With AShr the shift gives 0xffffffff, the mask gives 0xf, and sign-extension gives 0xffffffff, which matches.

Happy to run a PR's test on this GPU if one goes up.

**@⁠MiguelMatMar** on GitHub · 2026-11-23 22:44 UTC · [original](https://github.com/Marvisak/VIctory/issues/2#issuecomment-7116121625) Windows results for this, on an NVIDIA RTX 4080 SUPER (driver 621.24, Windows 11), at `015d45b`. AI-assisted (Claude Code), synthetic inputs only. `cargo test --workspace`: everything passes (about 220 tests; 3 ignored, the ones that need files outside the repo). The `diff_tests` skip without the console `dxc`, so they don't cover this. I can't drive `lower.rs` without a console container, so I checked the host side instead. A throwaway local test (not committed) compiled the same expressions with the Windows SDK `dxc` (cs_6_5) and ran them through `Host::compute_pipeline_with` + `Host::dispatch`. Inputs came from root constants, so nothing was constant-folded. | expression (what the lowering emits) | RTX 4080 SUPER | WARP | RDNA2 (from the issue) | |---|---|---|---| | `frac(-1e-10)` (DXIL `Frc`) | `0x3f800000` (1.0) | `0x3f7fffff` | `0x3f7fffff` | | `frac(-1e-8)` | `0x3f800000` | `0x3f7fffff` | – | | `max(min(1,2), min(max(1,2), NaN))` (med3 1,2,NaN) | 2 | 2 | 1 | | med3 lowering `(NaN, 1, 0)` | 1 | 1 | 0 | | med3 lowering `(NaN, 0, 1)` | 0 | 0 | 0 | | `sin(255.5 * 2π)` (scaled before `Sin`) | `9.57e-5` | `-8.74e-8` | exactly +0 | - **fract:** `Frc` returning 1.0 depends on the driver. NVIDIA gives 1.0 and WARP already clamps. The proposed `select(r == 1.0, 0x3f7fffff, r)` makes them agree. - **med3:** both host devices follow IEEE minNum/maxNum, so the current lowering gives the median of the non-NaN pair, not RDNA2's `min3`. The issue is confirmed on both. - **sin/cos:** on NVIDIA the error at 255.5 turns is about 1e-4, the same order the issue gives. Reducing in turns first should fix it. - **`v_bfe_i32`:** I traced `bfe()` (`lower.rs:1360`) by hand. `LShr` before the mask drops the sign. One correction to the table: for `a = 0x80000000, off 31, width 4` the current lowering gives `0x00000001`, not `0x00000008`: the shift leaves 1, the mask keeps it, and a 4-bit field `0001` sign-extends to 1. Hardware gives `0xffffffff`. With `AShr` the shift gives `0xffffffff`, the mask gives `0xf`, and sign-extension gives `0xffffffff`, which matches. Happy to run a PR's test on this GPU if one goes up.
Author
Owner

@⁠9hvn9yt2vk-netizen on GitHub · 2026-11-24 01:10 UTC · original

I think this is fixed on main: commit 36eb0b4 ("Shaders: med3 with NaN, fract below 1.0, signed bit fields past bit 31") and PR #37 (fract(NaN) on NVIDIA, plus synthetic GPU VALU tests) are both merged. Can this be closed?

**@⁠9hvn9yt2vk-netizen** on GitHub · 2026-11-24 01:10 UTC · [original](https://github.com/Marvisak/VIctory/issues/2#issuecomment-7118836512) I think this is fixed on main: commit 36eb0b4 ("Shaders: med3 with NaN, fract below 1.0, signed bit fields past bit 31") and PR #37 (fract(NaN) on NVIDIA, plus synthetic GPU VALU tests) are both merged. Can this be closed?
This discussion has been locked. Commenting is limited to contributors.
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/Marvisak--VIctory#2
No description provided.