Skip to content

clear_color_test: near-zero clears under every FP rounding mode (#521 review follow-up) - #5704

Open
marquezinii wants to merge 1 commit into
ItsNotRexed:mainfrom
marquezinii:test/clear-rounding-near-zero
Open

marquezinii wants to merge 1 commit into
ItsNotRexed:mainfrom
marquezinii:test/clear-rounding-near-zero

Conversation

@marquezinii

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #5581 (comment)

What and why

This follows up @Flyander's review on #521 (merged as c0a92ebc), which asked for round_half_even not to rely on x + 0.5 being exact for tiny values, and for a near-zero case in the rounding-mode coverage.

The code was already correct here; the comment overstated it:

  • x is a float times at most 65535, so it has at most 40 significant bits, and x + 0.5 is exact whenever |x| >= 2^-14.
  • Below that the sum does round with the mode (to just above 0.5 under FE_UPWARD). But floor() of anything in [0.5, 1) is 0, and r - x == 0.5 is false. So a tiny positive value gives code 0, not 1/255.
  • Tiny negative values (SNORM) land just below 0.5 and also give 0.

The comment now says this. clear_color_test gets the missing coverage: denorm_min, FLT_MIN, 1e-20 and a just-below-one-code value. They run on R8G8B8A8_UNORM, on R16G16B16A16_UNORM (including the 32767.5 tie) and on R8G8B8A8_SNORM, under FE_DOWNWARD, FE_UPWARD and FE_TOWARDZERO. Test and comment only; no runtime behaviour changes.

Tested

Windows 11 26H2, MSVC 14.52, RTX 5070, on main 561b65a8 + this:

  • clear_color_test: 518 checks passed.
  • Control: a round_half_even that rounds tiny values with the mode (nearbyint(x + 0.5)) fails the new block under FE_UPWARD (UNORM8 1 (0), UNORM16 1 (0), SNORM8 1 (0)). That is the failure described in the review, and the merged code doesn't have it.
  • Exhaustive check outside the tree: quantize_unorm / quantize_snorm, copied verbatim from main, against the exact nearest-even code. It covered every float in the clamped range, scales 255, 1023, 65535, 127 and 32767, under all four rounding modes. Result: 29,829,890,068 cases, 0 mismatches. The review's case (FE_UPWARD, denorm_min * 255) gives 0. With std::nearbyint in place of round_half_even, the same check finds 7.8 billion mismatches, so it does detect mode-dependent rounding.

AI-assisted.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)

Follow-up to the review of ItsNotRexed#521. round_half_even's comment said x + 0.5 is
exact, which holds only for |x| >= 2^-14 (x is a float times at most 65535,
so at most 40 significant bits). Below that the sum may round with the mode
(just above 0.5 under FE_UPWARD), but floor() still gives 0 and the tie test
fails, so the code is 0 in every mode. The comment now says so.

clear_color_test adds denorm_min, FLT_MIN, 1e-20 and a just-below-one-code
value on R8G8B8A8_UNORM, R16G16B16A16_UNORM (with the 32767.5 tie) and
R8G8B8A8_SNORM under FE_DOWNWARD, FE_UPWARD and FE_TOWARDZERO. A version that
rounds tiny values with the mode (nearbyint(x + 0.5)) fails it under
FE_UPWARD with code 1 instead of 0.

Checked outside the tree as well: quantize_unorm and quantize_snorm against
the exact nearest-even code for every float in range, scales 255, 1023,
65535, 127 and 32767, all four modes: 29.8 billion cases, 0 mismatches.
@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 9e6f8c6 on NVIDIA GeForce RTX 4080, driver 617.42: clear_color_test exits 0 with "clear_color_test: 518 full/rectangle integer, normalised and float replay checks passed". The test prints no per-case lines, and the failing control for the new near-zero cases was not run.

AI-assisted.

@Kam471

Kam471 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Head 9e6f8c6bc. git diff --check clean. Test + comment only; runtime/src/vulkan_replay.h changes no code.

Windows 11, build 561b65a83 + PR, LEONIDA_VK_DEVICE='RTX 5070 Ti':

  • clear_color_test.exe -> 518 full/rectangle integer, normalised and float replay checks passed, exit 0.
  • Base 561b65a83, same binary target -> 515 checks. So the new block adds exactly 3 checks (one per mode).
  • ctest -R '^clear_color$' -> 1/1 passed.

The test is CPU-only (no Vulkan loader, GPU, shaders or game data); LEONIDA_VK_DEVICE has no effect on it. I set it as asked, but the 518 count does not depend on the GPU.

Premise checked independently: under FE_UPWARD, denorm_min + 0.5 = 0.50000000000000011103, floor = 0, r - x != 0.5, so the result is code 0; fegetround() confirms the mode took effect (512). The old "x + 0.5 is exact" comment was indeed wrong for |x| < 2^-14, and the code was already right.

The 29.8-billion-case exhaustive claim is outside the tree and not reproducible here; the in-tree guard is the 3 mode checks.

Verdict: merge.

@Bj0rnis

Bj0rnis commented Nov 23, 2026

Copy link
Copy Markdown

Verified on Windows 11 (MSVC 19.44 / Ninja) at 9e6f8c6b:

  • clear_color PASS (0.06 s) — 518 full/rectangle integer, normalised and float replay checks passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants