[PR #1858] runtime: Depth24 CPU conversions independent of the thread's rounding mode (RT-D24-CPU-ROUNDEVEN) #1870

Open
opened 2026-11-26 21:19:14 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1858
Author: @thiefyzheng
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/d24-cpu-roundeven


📝 Commits (1)

  • 06c3b4a RT-D24-CPU-ROUNDEVEN: Depth24 CPU conversions independent of the thread's rounding mode

📊 Changes

3 files changed (+167 additions, -5 deletions)

View changed files

📝 runtime/CMakeLists.txt (+7 -0)
📝 runtime/src/depth24.h (+31 -5)
➕ runtime/test/depth24_cpu_test.cpp (+129 -0)

📄 Description

RT-D24-CPU-ROUNDEVEN

Claimed on #1608 (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163485212). Fixes @cheltyi's finding (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163281819): runtime/src/depth24.h's CPU-side Depth24 conversion followed the calling thread's rounding mode (float multiply + std::nearbyint), while depth24_convert.comp is roundEven of a correctly rounded product — so a thread with directed rounding (FE_DOWNWARD/FE_TOWARDZERO) quantized 0x1p-23f to 1 instead of 2 (~1 integer ULP off, beyond the documented 0.6 ULP tolerance), letting CPU clears (vulkan_replay.h depth24_clear_value) disagree with GPU uploads/copies. depth24_to_float had the same environment dependence.

What

  • depth24_detail::round_even: floor/fmod-based ties-to-even, exact in every rounding mode — no fesetround, no fenv dependence, holds under -ffast-math.
  • depth24_quantize: exact double(value) * 16777215.0 (24×24 bits fit a double), round to 24 significant bits via ldexp+round_even, then round to integer — the shader's two roundings.
  • depth24_to_float: the shader's integer formula ldexp(float(m+1), e-47), every step exact.
  • No existing helper fits (vulkan_replay.h's round_half_even rounds once and lives in a header that includes this one's users).
  • New runtime/test/depth24_cpu_test.cpp (ctest depth24_cpu, no GPU, no Vulkan loader; the clear_color_test pattern): runs both directions under FE_TONEAREST / FE_DOWNWARD / FE_UPWARD / FE_TOWARDZERO against a nearest-mode reference, with fixed cases (0x1p-23f -> 2, 0.5f -> 8388608 tie-to-even).

What was run

  • g++ -std=c++20 -O2 (also -frounding-math): 1,858,324 checks passed across all four rounding modes (all 2^24 codes round-trip in nearest and downward; every third float pattern in [0,1] quantized nearest-vs-towardzero); plus -ffast-math: 0 mismatches. Before the fix the old header fails immediately under FE_DOWNWARD (depth24_quantize(0x1p-23) = 1, expected 2).
  • MSVC on Windows (VS 2022 BuildTools, RelWithDebInfo, VS generator): depth24_cpu_test built and ctest -R depth24_cpu Passed (0.67 s).
  • Not run: the device-side tests (depth24_convert_test, d3d12_depth24_draw) — no Vulkan device here; the CPU/GPU cross-check on hardware would be the remaining confirmation, though both sides now implement the same documented formula.

Attribution

AI-assisted (Claude Sonnet 5.5 worker implementation, contributor verification incl. the winvm MSVC build, orchestration).


🔄 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/1858 **Author:** [@thiefyzheng](https://github.com/thiefyzheng) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/d24-cpu-roundeven` --- ### 📝 Commits (1) - [`06c3b4a`](https://github.com/ubiquitarianism/leonida/commit/06c3b4a6ac65d044faa7c99a10f46094cbfa9fc5) RT-D24-CPU-ROUNDEVEN: Depth24 CPU conversions independent of the thread's rounding mode ### 📊 Changes **3 files changed** (+167 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+7 -0) 📝 `runtime/src/depth24.h` (+31 -5) ➕ `runtime/test/depth24_cpu_test.cpp` (+129 -0) </details> ### 📄 Description ## RT-D24-CPU-ROUNDEVEN Claimed on #1608 (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163485212). Fixes @cheltyi's finding (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163281819): `runtime/src/depth24.h`'s CPU-side Depth24 conversion followed the calling thread's rounding mode (float multiply + `std::nearbyint`), while `depth24_convert.comp` is `roundEven` of a correctly rounded product — so a thread with directed rounding (FE_DOWNWARD/FE_TOWARDZERO) quantized `0x1p-23f` to 1 instead of 2 (~1 integer ULP off, beyond the documented 0.6 ULP tolerance), letting CPU clears (`vulkan_replay.h` `depth24_clear_value`) disagree with GPU uploads/copies. `depth24_to_float` had the same environment dependence. ### What - `depth24_detail::round_even`: floor/fmod-based ties-to-even, exact in every rounding mode — no `fesetround`, no fenv dependence, holds under `-ffast-math`. - `depth24_quantize`: exact `double(value) * 16777215.0` (24×24 bits fit a double), round to 24 significant bits via `ldexp`+`round_even`, then round to integer — the shader's two roundings. - `depth24_to_float`: the shader's integer formula `ldexp(float(m+1), e-47)`, every step exact. - No existing helper fits (`vulkan_replay.h`'s `round_half_even` rounds once and lives in a header that includes this one's users). - New `runtime/test/depth24_cpu_test.cpp` (ctest `depth24_cpu`, no GPU, no Vulkan loader; the `clear_color_test` pattern): runs both directions under FE_TONEAREST / FE_DOWNWARD / FE_UPWARD / FE_TOWARDZERO against a nearest-mode reference, with fixed cases (`0x1p-23f -> 2`, `0.5f -> 8388608` tie-to-even). ### What was run - g++ `-std=c++20 -O2` (also `-frounding-math`): **1,858,324 checks passed across all four rounding modes** (all 2^24 codes round-trip in nearest and downward; every third float pattern in [0,1] quantized nearest-vs-towardzero); plus `-ffast-math`: 0 mismatches. Before the fix the old header fails immediately under FE_DOWNWARD (`depth24_quantize(0x1p-23) = 1, expected 2`). - **MSVC on Windows (VS 2022 BuildTools, RelWithDebInfo, VS generator)**: `depth24_cpu_test` built and `ctest -R depth24_cpu` **Passed** (0.67 s). - **Not run**: the device-side tests (`depth24_convert_test`, `d3d12_depth24_draw`) — no Vulkan device here; the CPU/GPU cross-check on hardware would be the remaining confirmation, though both sides now implement the same documented formula. ### Attribution AI-assisted (Claude Sonnet 5.5 worker implementation, contributor verification incl. the winvm MSVC build, orchestration). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#1870
No description provided.