Commit Graph

1 Commits

Author SHA1 Message Date
06c3b4a6ac RT-D24-CPU-ROUNDEVEN: Depth24 CPU conversions independent of the thread's rounding mode
depth24_quantize multiplied in float and rounded with std::nearbyint, and depth24_to_float divided in double and
narrowed to float; all three follow the calling thread's rounding mode, while depth24_convert.comp is roundEven of a
correctly rounded product and integer arithmetic. Under FE_DOWNWARD or FE_TOWARDZERO 0x1p-23f quantised to 1, not 2,
so CPU clears (vulkan_replay.h depth24_clear_value) could disagree with GPU uploads and copies.

No fesetround (a library must not change the caller's environment, and fenv access can be compiled out). No existing
helper fits (vulkan_replay.h's round_half_even is in a header that includes this one's users and rounds only once), so
depth24_detail::round_even is floor/fmod based like it: exact in every mode. The float product is formed exactly in a
double (24 x 24 bits), rounded to 24 significant bits by scaling with ldexp and round_even, then rounded to the
integer, which is the shader's two roundings. depth24_to_float uses the shader's integer formula,
float(m + 1) * 2^(e-47), every step exact.

depth24_cpu_test (no GPU, no Vulkan loader; same pattern as clear_color_test) runs the conversions under
FE_TONEAREST, FE_DOWNWARD, FE_UPWARD and FE_TOWARDZERO and compares with a float multiply + nearbyint reference run
under the default mode, with fixed cases (0x1p-23f -> 2, 0.5f -> 8388608 tie to even).

AI-assisted. Checked with g++ -std=c++20 -O2 (also -frounding-math, -ffast-math); not built with MSVC.
2026-11-25 13:16:42 +00:00