[PR #1027] [CLOSED] rdna2spirv: float min and max order -0 below +0 on the GPU too (SHADER-MINMAX-SIGNED-ZERO) #1064

Closed
opened 2026-11-26 20:19:58 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1027
Author: @oscarlosu-lab
Created: 11/24/2026
Status: ❌ Closed

Base: main ← Head: pr/minmax-signed-zero


📝 Commits (1)

  • 9bc384b rdna2spirv: float min and max order -0 below +0 on the GPU too (SHADER-MINMAX-SIGNED-ZERO)

📊 Changes

7 files changed (+118 additions, -15 deletions)

View changed files

📝 docs/design.md (+1 -1)
📝 runtime/test/shaders/check.py (+4 -0)
➕ runtime/test/shaders/ps_fminmax_signed_zero.asm (+20 -0)
📝 scripts/run_gpu_tests.py (+4 -0)
➕ tests/semantic/fixtures/disasm/ps_fminmax_signed_zero.s (+11 -0)
📝 tests/semantic/test_float_nan_fract.py (+19 -0)
📝 tools/rdna2spirv.py (+59 -14)

📄 Description

Claimed in the coordination issue as: SHADER-MINMAX-SIGNED-ZERO

Draft: the fix is not demonstrated on the GPU I have. On an NVIDIA RTX 3060 (Windows 11, driver 621.35) the new fixture passes both before and after the change, native and with LEONIDA_W64_EMULATE=1: that driver already returns +0 for NMax(-0, +0). I'm asking for a run on AMD (RADV and the proprietary driver) and Intel before this leaves draft; if none of them returns the first zero either, this can be closed and only the fixture kept.

Why

tools/rdna2spirv.py lowers v_max_f32/v_min_f32 and their f16, packed, three-operand and med3 forms to GLSL.std.450 NMax/NMin. GLSL.std.450 defines those as "y if x < y, otherwise x" (the other operand for a NaN). -0 and +0 compare equal, so on a driver that follows that literally, with two zeros of opposite sign the result is the first operand: max(-0, +0) is -0 and min(+0, -0) is +0. The modules declare SignedZeroInfNanPreserve 32, so the sign is meant to survive. RDNA2 gives +0 for max and -0 for min in either order (docs/design.md, Float min/max row: "-0 < +0", measured on gfx1030). A -0 that should be +0 turns, for example, a following v_rsq's +inf into -inf.

The CPU interpreter in tests/semantic already orders -0 below +0 for NMax/NMin, so no CPU test could see the difference. Marvisak/VIctory (GPL-3.0) commit bf84134b2 found and fixed the same thing in its DXIL lowering (DXIL's FMax/FMin are a compare and select there).

Change

  • Gen.glsl_minmax emits NMax/NMin and, when both operands are zeros (magnitude bits 0), takes the result from their sign bits: AND for max, OR for min. Every f32, f16, packed, min3/max3 and med3 lowering goes through it. med3 skips it when at most one operand can be a zero (a constant that is not one), so an all-constant med3 still emits no select (test_v_med3_f32_all_constants_needs_no_select). NaN handling is unchanged. Cost: 8 instructions per min/max.
  • runtime/test/shaders/ps_fminmax_signed_zero.asm: max and min of -0 and +0 in both orders, read from memory; in check.py and scripts/run_gpu_tests.py, with its translator fixture.
  • tests/semantic/test_float_nan_fract.py: the expected words, and the sign-bit select after each min/max.
  • docs/design.md: the Float min/max row says how the order is kept.

Tested

  • scripts/run_semantic_tests.py tests/semantic: passes except test_image_cube_store_glc, which cannot start llvm-mc on this host (the Windows path splitting claimed as TEST-CUBE-WINPATH), unrelated.
  • regen_fixtures.py --check, check_no_game_data.py, tests/test_docs_links.py: pass.
  • GPU: NVIDIA RTX 3060, Windows 11, driver 621.35: run_gpu_tests.py --filter ps_fminmax_signed_zero passes before and after (see above).

AI-assisted (Claude Code). No game files.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1027 **Author:** [@oscarlosu-lab](https://github.com/oscarlosu-lab) **Created:** 11/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `pr/minmax-signed-zero` --- ### 📝 Commits (1) - [`9bc384b`](https://github.com/ubiquitarianism/leonida/commit/9bc384b8be1f9a5cc3359249d8315b5bf06e8f85) rdna2spirv: float min and max order -0 below +0 on the GPU too (SHADER-MINMAX-SIGNED-ZERO) ### 📊 Changes **7 files changed** (+118 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) 📝 `runtime/test/shaders/check.py` (+4 -0) ➕ `runtime/test/shaders/ps_fminmax_signed_zero.asm` (+20 -0) 📝 `scripts/run_gpu_tests.py` (+4 -0) ➕ `tests/semantic/fixtures/disasm/ps_fminmax_signed_zero.s` (+11 -0) 📝 `tests/semantic/test_float_nan_fract.py` (+19 -0) 📝 `tools/rdna2spirv.py` (+59 -14) </details> ### 📄 Description Claimed in the coordination issue as: SHADER-MINMAX-SIGNED-ZERO **Draft: the fix is not demonstrated on the GPU I have.** On an NVIDIA RTX 3060 (Windows 11, driver 621.35) the new fixture passes both before and after the change, native and with `LEONIDA_W64_EMULATE=1`: that driver already returns +0 for `NMax(-0, +0)`. I'm asking for a run on AMD (RADV and the proprietary driver) and Intel before this leaves draft; if none of them returns the first zero either, this can be closed and only the fixture kept. ### Why `tools/rdna2spirv.py` lowers `v_max_f32`/`v_min_f32` and their f16, packed, three-operand and med3 forms to GLSL.std.450 `NMax`/`NMin`. GLSL.std.450 defines those as "y if x < y, otherwise x" (the other operand for a NaN). -0 and +0 compare equal, so on a driver that follows that literally, with two zeros of opposite sign the result is the first operand: `max(-0, +0)` is -0 and `min(+0, -0)` is +0. The modules declare `SignedZeroInfNanPreserve 32`, so the sign is meant to survive. RDNA2 gives +0 for max and -0 for min in either order (`docs/design.md`, Float min/max row: "-0 < +0", measured on gfx1030). A -0 that should be +0 turns, for example, a following `v_rsq`'s +inf into -inf. The CPU interpreter in `tests/semantic` already orders -0 below +0 for `NMax`/`NMin`, so no CPU test could see the difference. Marvisak/VIctory (GPL-3.0) commit `bf84134b2` found and fixed the same thing in its DXIL lowering (DXIL's FMax/FMin are a compare and select there). ### Change - `Gen.glsl_minmax` emits `NMax`/`NMin` and, when both operands are zeros (magnitude bits 0), takes the result from their sign bits: AND for max, OR for min. Every f32, f16, packed, min3/max3 and med3 lowering goes through it. med3 skips it when at most one operand can be a zero (a constant that is not one), so an all-constant med3 still emits no select (`test_v_med3_f32_all_constants_needs_no_select`). NaN handling is unchanged. Cost: 8 instructions per min/max. - `runtime/test/shaders/ps_fminmax_signed_zero.asm`: max and min of -0 and +0 in both orders, read from memory; in `check.py` and `scripts/run_gpu_tests.py`, with its translator fixture. - `tests/semantic/test_float_nan_fract.py`: the expected words, and the sign-bit select after each min/max. - `docs/design.md`: the Float min/max row says how the order is kept. ### Tested - `scripts/run_semantic_tests.py tests/semantic`: passes except `test_image_cube_store_glc`, which cannot start `llvm-mc` on this host (the Windows path splitting claimed as TEST-CUBE-WINPATH), unrelated. - `regen_fixtures.py --check`, `check_no_game_data.py`, `tests/test_docs_links.py`: pass. - GPU: NVIDIA RTX 3060, Windows 11, driver 621.35: `run_gpu_tests.py --filter ps_fminmax_signed_zero` passes before and after (see above). AI-assisted (Claude Code). No game files. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:19:58 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#1064
No description provided.