tombox-xshader: v_cvt_rpi_i32_f32 rounds x + 0.5 in f32, and rpi/flr map NaN to 0; gfx1036 gives the exact floor and a NaN saturated by its sign #374

Open
opened 2026-11-26 06:20:42 +00:00 by github-bot · 0 comments
Owner

@⁠KainanP on GitHub · 2026-11-24 18:18 UTC · original

crates/tombox-xshader/src/hlsl.rs (main):

"v_cvt_flr_i32_f32" => format!("tb_cvt_i(tb_flr({}))", fa()?),
"v_cvt_rpi_i32_f32" => format!("tb_cvt_i(floor({} + 0.5))", fa()?),

tb_cvt_i returns 0 for a NaN. The reference in tests/exec.rs (vector_vop1) also uses cvt_i((x + 0.5).floor()) and NaN → 0, so the test cannot catch either difference.

What RDNA 2 silicon does. Lyrnc measured this on a gfx1036 with inline assembly, the same in all four FLOAT_MODE settings: https://github.com/Marvisak/VIctory/pull/605#issuecomment-7123302580. A second gfx1036 run (and RDNA 4) agrees: https://github.com/xcl-dev/xcl/pull/639#issuecomment-7127563021

  • v_cvt_rpi_i32_f32 is the floor of the exact x + 0.5. 0.49999997 (0x3EFFFFFF) gives 0, not 1. 8388609.0 gives 8388609, not 8388610. -8388609.0 gives -8388609.
  • v_cvt_rpi_i32_f32 and v_cvt_flr_i32_f32 of a NaN give 0x7FFFFFFF for a positive NaN and 0x80000000 for a negative one, quiet or signalling.
  • v_cvt_i32_f32 / v_cvt_u32_f32 of a NaN still give 0.

Prior art. xcl-dev/xcl#1020 fixes the same two points. rpi becomes r = floor(x); r + (x - r >= 0.5), which is exact in f32 below 2^23; from 2^23 up x is already an integer. NaN goes through a sign-bit select. It was verified on gfx1036 and RDNA 4 and caught 13 mismatches before the fix. VIctory#605 does the same.

Suggested test rows: 0x3EFFFFFF, 0x4B000001, 0x4B000003, 0xCB000001, and ±qNaN / ±sNaN for both rpi and flr, with the expected values above.

Not covered: console hardware (gfx1036 is a desktop RDNA 2 APU of the same generation), and flr of a negative denormal, which depends on the denormal mode.

AI-assisted (Claude Code). I read the code at main; the hardware values are quoted from the linked threads.

**@⁠KainanP** on GitHub · 2026-11-24 18:18 UTC · [original](https://github.com/omonkeyz/xbox-emulator/issues/374) `crates/tombox-xshader/src/hlsl.rs` (main): ``` "v_cvt_flr_i32_f32" => format!("tb_cvt_i(tb_flr({}))", fa()?), "v_cvt_rpi_i32_f32" => format!("tb_cvt_i(floor({} + 0.5))", fa()?), ``` `tb_cvt_i` returns 0 for a NaN. The reference in `tests/exec.rs` (`vector_vop1`) also uses `cvt_i((x + 0.5).floor())` and NaN → 0, so the test cannot catch either difference. **What RDNA 2 silicon does.** Lyrnc measured this on a gfx1036 with inline assembly, the same in all four FLOAT_MODE settings: https://github.com/Marvisak/VIctory/pull/605#issuecomment-7123302580. A second gfx1036 run (and RDNA 4) agrees: https://github.com/xcl-dev/xcl/pull/639#issuecomment-7127563021 - `v_cvt_rpi_i32_f32` is the floor of the **exact** x + 0.5. 0.49999997 (`0x3EFFFFFF`) gives 0, not 1. 8388609.0 gives 8388609, not 8388610. -8388609.0 gives -8388609. - `v_cvt_rpi_i32_f32` and `v_cvt_flr_i32_f32` of a NaN give `0x7FFFFFFF` for a positive NaN and `0x80000000` for a negative one, quiet or signalling. - `v_cvt_i32_f32` / `v_cvt_u32_f32` of a NaN still give 0. **Prior art.** xcl-dev/xcl#1020 fixes the same two points. `rpi` becomes `r = floor(x); r + (x - r >= 0.5)`, which is exact in f32 below 2^23; from 2^23 up x is already an integer. NaN goes through a sign-bit select. It was verified on gfx1036 and RDNA 4 and caught 13 mismatches before the fix. VIctory#605 does the same. **Suggested test rows:** `0x3EFFFFFF`, `0x4B000001`, `0x4B000003`, `0xCB000001`, and ±qNaN / ±sNaN for both rpi and flr, with the expected values above. Not covered: console hardware (gfx1036 is a desktop RDNA 2 APU of the same generation), and `flr` of a negative denormal, which depends on the denormal mode. _AI-assisted (Claude Code). I read the code at main; the hardware values are quoted from the linked threads._
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:20:42 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.