From 7aa66822e2ce5cc9abb08246b6ede6d576c0699f Mon Sep 17 00:00:00 2001 From: upipibi Date: Mon, 23 Nov 2026 23:57:42 -0600 Subject: [PATCH] Recompiler: v_cvt_rpi rounds the exact x + 0.5, rpi / flr saturate NaN by sign Measured on RDNA2 silicon (gfx1036, the same in all four FLOAT_MODE settings; https://github.com/Marvisak/VIctory/pull/605#issuecomment-7123302580): - v_cvt_rpi_i32_f32 is the floor of the exact x + 0.5. XCL's floor(x + 0.5f) rounds the sum first: 0.49999997 gave 1, 2^23 + 1 gave 2^23 + 2, 2^24 - 1 gave 2^24. - v_cvt_rpi_i32_f32 and v_cvt_flr_i32_f32 give 0x7FFFFFFF for a positive NaN and 0x80000000 for a negative one, quiet or signalling. XCL gave 0, which stays right for v_cvt_i32_f32 / v_cvt_u32_f32. cvt_rpi takes floor(x) and adds 1 when x - floor(x) >= 0.5, both exact in f32, below 2^23 (from there every f32 is an integer); nan_by_sign saturates a NaN on the bits. A flr of a negative denormal depends on the denormal mode and is left as XCL treats denormals. gpu_cvt_rpi_flr_round_exactly_and_saturate_nan_by_sign uses the measured table as expected values; it fails on main with 13 mismatches. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/emit.rs | 10 ++++-- crates/xcl/src/gpu/recomp/tests.rs | 54 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 608cbf6d23..3fe1abf918 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -3154,8 +3154,8 @@ impl Em { "v_cvt_f32_u32" => fr(format!("(float){a}")), "v_cvt_u32_f32" => format!("cvt_u32({})", fs(0)), "v_cvt_i32_f32" => format!("(uint)cvt_i32({})", fs(0)), - "v_cvt_rpi_i32_f32" => format!("(uint)cvt_i32(floor({} + 0.5f))", fs(0)), - "v_cvt_flr_i32_f32" => format!("(uint)cvt_i32(floor({}))", fs(0)), + "v_cvt_rpi_i32_f32" => format!("(uint)cvt_rpi({})", fs(0)), + "v_cvt_flr_i32_f32" => format!("(uint)cvt_flr({})", fs(0)), "v_cvt_f32_ubyte0" => fr(format!("(float)({a} & 0xFFu)")), "v_cvt_f32_ubyte1" => fr(format!("(float)(({a} >> 8) & 0xFFu)")), "v_cvt_f32_ubyte2" => fr(format!("(float)(({a} >> 16) & 0xFFu)")), @@ -5414,6 +5414,12 @@ int med3i(int a, int b, int c) { return max(min(a, b), min(max(a, b), c)); } float fract_f(float x) { float r = x - floor(x); return select(r >= 1.0f, asfloat(0x3F7FFFFFu), r); } uint cvt_u32(float f) { return select(or(isnan(f), f <= 0.0f), 0u, select(f >= 4294967296.0f, 0xFFFFFFFFu, (uint)f)); } int cvt_i32(float f) { return select(isnan(f), 0, select(f <= -2147483648.0f, (int)0x80000000u, select(f >= 2147483648.0f, 0x7FFFFFFF, (int)f))); } +// v_cvt_flr / v_cvt_rpi: unlike v_cvt_i32_f32, a NaN saturates by its sign (measured on RDNA2). +// rpi is floor(x) + 1 when x - floor(x) >= 0.5, both exact in f32: floor(x + 0.5f) rounds the sum +// first (0.49999997 would give 1). From 2^23 up every f32 is an integer. +int nan_by_sign(float x, int v) { uint u = asuint(x); return select((u & 0x7FFFFFFFu) > 0x7F800000u, (int)select((u >> 31) != 0u, 0x80000000u, 0x7FFFFFFFu), v); } +int cvt_flr(float x) { return nan_by_sign(x, cvt_i32(floor(x))); } +int cvt_rpi(float x) { float fl = floor(x); bool up = and((asuint(x) & 0x7FFFFFFFu) < 0x4B000000u, x - fl >= 0.5f); return nan_by_sign(x, cvt_i32(fl) + select(up, 1, 0)); } int i24(uint v) { return ((int)(v << 8)) >> 8; } uint bfe_u(uint v, uint o, uint w) { o &= 31u; w &= 31u; return select(w == 0u, 0u, (v >> o) & (0xFFFFFFFFu >> ((32u - w) & 31u))); } int bfe_i(uint v, uint o, uint w) { o &= 31u; w &= 31u; uint s = (32u - w) & 31u; return select(w == 0u, 0, ((int)(((uint)(((int)v) >> o)) << s)) >> s); } diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index a4f50929bb..fb2a80b61e 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -1000,6 +1000,60 @@ fn gpu_valu_float_and_int() { } } +/// v_cvt_rpi_i32_f32 rounds the exact x + 0.5 down, and v_cvt_rpi / v_cvt_flr saturate a NaN by +/// its sign, while v_cvt_i32_f32 gives 0 for any NaN. Expected values: measured on RDNA2 silicon +/// (gfx1036), the same in all four FLOAT_MODE settings, +/// https://github.com/Marvisak/VIctory/pull/605#issuecomment-7123302580 (negative denormals, +/// whose flr depends on the denormal mode, are left out). +#[test] +fn gpu_cvt_rpi_flr_round_exactly_and_saturate_nan_by_sign() { + if !gpu_ready() { + return; + } + // (input, rpi, flr, i32) + const ROWS: [(u32, u32, u32, u32); 16] = [ + (0x3EFF_FFFF, 0x0000_0000, 0x0000_0000, 0x0000_0000), + (0x3F00_0000, 0x0000_0001, 0x0000_0000, 0x0000_0000), + (0xBF00_0000, 0x0000_0000, 0xFFFF_FFFF, 0x0000_0000), + (0xBF00_0001, 0xFFFF_FFFF, 0xFFFF_FFFF, 0x0000_0000), + (0x4B00_0001, 0x0080_0001, 0x0080_0001, 0x0080_0001), + (0x4B00_0003, 0x0080_0003, 0x0080_0003, 0x0080_0003), + (0x4B7F_FFFF, 0x00FF_FFFF, 0x00FF_FFFF, 0x00FF_FFFF), + (0xCB00_0001, 0xFF7F_FFFF, 0xFF7F_FFFF, 0xFF7F_FFFF), + (0x4F00_0000, 0x7FFF_FFFF, 0x7FFF_FFFF, 0x7FFF_FFFF), + (0xCF00_0000, 0x8000_0000, 0x8000_0000, 0x8000_0000), + (0x7F80_0000, 0x7FFF_FFFF, 0x7FFF_FFFF, 0x7FFF_FFFF), + (0xFF80_0000, 0x8000_0000, 0x8000_0000, 0x8000_0000), + (0x7FC0_0000, 0x7FFF_FFFF, 0x7FFF_FFFF, 0x0000_0000), + (0xFFC0_0000, 0x8000_0000, 0x8000_0000, 0x0000_0000), + (0x7F80_0001, 0x7FFF_FFFF, 0x7FFF_FFFF, 0x0000_0000), + (0xFFFF_FFFF, 0x8000_0000, 0x8000_0000, 0x0000_0000), + ]; + let code = prog(&[ + ("V_CVT_RPI_I32_F32", "ENC_VOP1", &[("VDST", 2), ("SRC0", V + 1)], None), + ("V_CVT_FLR_I32_F32", "ENC_VOP1", &[("VDST", 3), ("SRC0", V + 1)], None), + ("V_CVT_I32_F32", "ENC_VOP1", &[("VDST", 4), ("SRC0", V + 1)], None), + ]); + let mut st = KernelState::default(); + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[1] = ROWS[l % ROWS.len()].0; + } + let r = runner::run_code(&code, &st, &[]).unwrap(); + let mut bad = Vec::new(); + for l in 0..64 { + let v = &r.vgpr[l]; + let (x, rpi, flr, i) = ROWS[l % ROWS.len()]; + for (name, got, want) in [("v_cvt_rpi_i32_f32", v[2], rpi), ("v_cvt_flr_i32_f32", v[3], flr), ("v_cvt_i32_f32", v[4], i)] { + if got != want { + bad.push(format!("{name}({x:#010X}) = {got:#010X}, want {want:#010X}")); + } + } + } + bad.sort(); + bad.dedup(); + assert!(bad.is_empty(), "{}", bad.join("\n")); +} + /// Output modifiers each instruction kind applies: omod on f64 (clamp stays in double /// precision) and on the f64 → f32 / f32 → f16 conversions, and the integer clamp (saturation) of the /// unsigned adds, the packed 16-bit adds and the integer dot products.