startup: gfx2hlsl lowers f32 lg/nlg compares as unordered (wrong for NaN) #1913

Closed
opened 2026-11-24 21:13:11 +00:00 by MasterDomino · 1 comment
MasterDomino commented 2026-11-24 21:13:11 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-24T21:13:11Z.


tools/startup/gfx2hlsl.py compare() lowers the f32 conditions lg as a != b and nlg as !(a != b). In the gfx10 ISA:

  • V_CMP_LG_F32 is "less or greater", i.e. ordered and not equal: false when either operand is NaN.
  • V_CMP_NLG_F32 is its negation: true when either operand is NaN.

HLSL != is unordered (true with NaN), so both lowerings give the opposite result whenever an operand is NaN. This applies to the plain, _e64 and (with #1915) SDWA forms of v_cmp_lg_f32, v_cmpx_lg_f32, v_cmp_nlg_f32 and v_cmpx_nlg_f32. The other conditions in the table (eq, lt, le, gt, ge, neq, nlt, nle, ngt, nge, o, u) map correctly. One local shader cache on current main uses v_cmp_nlg_f32_e64 3 times.

Scope. Lower lg as (a < b || a > b) and nlg as its negation. Keep the other conditions unchanged. Check how pixel_aot lowers lg/nlg and keep the two consistent; if pixel_aot has the same issue, note it rather than mixing both into one PR.

Acceptance.

  • An exhaustive check of every f32 condition in the table over values including ±0, ±denormal, ±inf and quiet and signalling NaN in either operand, against a reference built from the ISA definitions.
  • A source-built draw on WARP and hardware where v_cmp_lg_f32 and v_cmp_nlg_f32 with a NaN operand decide pixel output, checked pixel by pixel.
  • A negative control restoring the != lowering must fail both.

I'd like to take this. It builds on #1915's restructured compare(), so I'll stack it on that branch, or rebase once #1915 lands.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6591527255 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1920). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-24T21:13:11Z. --- `tools/startup/gfx2hlsl.py` `compare()` lowers the f32 conditions `lg` as `a != b` and `nlg` as `!(a != b)`. In the gfx10 ISA: - `V_CMP_LG_F32` is "less or greater", i.e. ordered and not equal: false when either operand is NaN. - `V_CMP_NLG_F32` is its negation: true when either operand is NaN. HLSL `!=` is unordered (true with NaN), so both lowerings give the opposite result whenever an operand is NaN. This applies to the plain, `_e64` and (with #1915) SDWA forms of `v_cmp_lg_f32`, `v_cmpx_lg_f32`, `v_cmp_nlg_f32` and `v_cmpx_nlg_f32`. The other conditions in the table (`eq`, `lt`, `le`, `gt`, `ge`, `neq`, `nlt`, `nle`, `ngt`, `nge`, `o`, `u`) map correctly. One local shader cache on current `main` uses `v_cmp_nlg_f32_e64` 3 times. **Scope.** Lower `lg` as `(a < b || a > b)` and `nlg` as its negation. Keep the other conditions unchanged. Check how `pixel_aot` lowers `lg`/`nlg` and keep the two consistent; if `pixel_aot` has the same issue, note it rather than mixing both into one PR. **Acceptance.** - An exhaustive check of every f32 condition in the table over values including ±0, ±denormal, ±inf and quiet and signalling NaN in either operand, against a reference built from the ISA definitions. - A source-built draw on WARP and hardware where `v_cmp_lg_f32` and `v_cmp_nlg_f32` with a NaN operand decide pixel output, checked pixel by pixel. - A negative control restoring the `!=` lowering must fail both. I'd like to take this. It builds on #1915's restructured `compare()`, so I'll stack it on that branch, or rebase once #1915 lands. AI-assisted.
MasterDomino commented 2026-11-24 22:01:03 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-24T21:37:50Z.


PR: https://github.com/R0Y00/Xbox2Windows/pull/1930. lg is now ordered-and-unequal behind a bit-level NaN test of both source words, nlg its negation; other conditions unchanged. One finding that shaped it: DXC without -Gis marks the float compares fast, and on the RX 9070 XT a < b || a > b alone returned 1 for lg and nlg on all 156 NaN pairs — hence the bit test (pixel_aot already lowers lg this way). Checks: 510 f32 forms × 256 value pairs (±0, denormals, ±inf, quiet/signalling/payload NaN) against the ISA reference; draws with 88 unequal, 12 equal and 156 NaN pairs — FXC and DXC, WARP and RX 9070 XT, every pixel matches; the old != lowering fails every fixture. f32 SDWA compares stay refused.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7145183377 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1920#issuecomment-7145183377). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-24T21:37:50Z. --- PR: https://github.com/R0Y00/Xbox2Windows/pull/1930. lg is now ordered-and-unequal behind a bit-level NaN test of both source words, nlg its negation; other conditions unchanged. One finding that shaped it: DXC without `-Gis` marks the float compares fast, and on the RX 9070 XT `a < b || a > b` alone returned 1 for lg and nlg on all 156 NaN pairs — hence the bit test (pixel_aot already lowers lg this way). Checks: 510 f32 forms × 256 value pairs (±0, denormals, ±inf, quiet/signalling/payload NaN) against the ISA reference; draws with 88 unequal, 12 equal and 156 NaN pairs — FXC and DXC, WARP and RX 9070 XT, every pixel matches; the old `!=` lowering fails every fixture. f32 SDWA compares stay refused. AI-assisted.
Sign in to join this conversation.
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
R0Y00/Xbox2Windows#1913
No description provided.