From 6d171e32577340e3224fc5d7803d3f673aa9232f Mon Sep 17 00:00:00 2001 From: acaracargo <58486686+acaracargo@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:42:09 +0300 Subject: [PATCH] Tests: integer-to-float conversions on the GPU v_cvt_f32_i32, v_cvt_f32_u32, v_cvt_f32_ubyte0..3 and v_cvt_f64_i32 / u32 / f32 had no edge-value GPU test (only ubyte1 ran). 16 inputs: the ends of the i32 and u32 ranges, 2^24 + 1 and the tie at 2^24 + 3 (round to nearest even), all byte lanes, and f32 infinities and NaN for the f64 conversion. The reference is Rust's `as`, which rounds to nearest even like the ISA's default mode. An f32 denormal is left out of v_cvt_f64_f32: whether it is flushed depends on the f32 denormal mode. This driver flushes it to 0. Measured: RTX 4060 (wave32), Windows 11 Pro 26300, Rust 1.100, debug. cargo test --workspace -- --test-threads=4: 678 passed, 0 failed, 41 ignored; SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware. Dropping the (int) cast of v_cvt_f64_i32 or reading the wrong byte for ubyte3 in emit.rs fails the test. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WsgnReN2iZK9RUNckZAhrv --- crates/xcl/src/gpu/recomp/tests.rs | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 596a74198..19cfc1aa7 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -1097,6 +1097,64 @@ fn gpu_clamp_and_omod_per_instruction() { } } +/// Integer → float conversions: i32 / u32 → f32 round to nearest even (2^24 + 1, a tie at +/// 2^24 + 3, the ends of both ranges), the four v_cvt_f32_ubyteN, and the exact conversions to f64 +/// (i32, u32, and f32 with infinities and NaN), against Rust's `as` (also nearest even). +#[test] +fn gpu_int_to_float_conversions() { + if !gpu_ready() { + return; + } + let xs: [u32; 16] = [ + 0, 1, 0xFFFF_FFFF, 0x7FFF_FFFF, 0x8000_0000, 0x0100_0001, 0x0100_0003, 0xFFFF_FF7F, 0x1234_5678, 0x3F80_0000, + 0x7F80_0000, 0xFF80_0000, 0x7FC0_0000, 0xDEAD_BEEF, 0xFF7F_FFFF, 0x8765_4321, + ]; + let one = |n: &str, d: u64| ins(n, "ENC_VOP1", &[("VDST", d), ("SRC0", V + 1)], None); + let code = program_of(&[ + one("V_CVT_F32_I32", 10), + one("V_CVT_F32_U32", 11), + one("V_CVT_F32_UBYTE0", 12), + one("V_CVT_F32_UBYTE1", 13), + one("V_CVT_F32_UBYTE2", 14), + one("V_CVT_F32_UBYTE3", 15), + one("V_CVT_F64_I32", 16), + one("V_CVT_F64_U32", 18), + one("V_CVT_F64_F32", 20), + ]); + let mut st = KernelState::default(); + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[1] = xs[l % xs.len()]; + } + let r = runner::run_code(&code, &st, &[]).unwrap(); + let mut bad = Vec::new(); + for (l, v) in r.vgpr.iter().enumerate().take(xs.len()) { + let x = xs[l]; + let d = |i: usize| v[i] as u64 | (v[i + 1] as u64) << 32; + let f = f32::from_bits(x) as f64; + let checks = [ + ("v_cvt_f32_i32", v[10] as u64, (x as i32 as f32).to_bits() as u64), + ("v_cvt_f32_u32", v[11] as u64, (x as f32).to_bits() as u64), + ("v_cvt_f32_ubyte0", v[12] as u64, ((x & 0xFF) as f32).to_bits() as u64), + ("v_cvt_f32_ubyte1", v[13] as u64, ((x >> 8 & 0xFF) as f32).to_bits() as u64), + ("v_cvt_f32_ubyte2", v[14] as u64, ((x >> 16 & 0xFF) as f32).to_bits() as u64), + ("v_cvt_f32_ubyte3", v[15] as u64, ((x >> 24) as f32).to_bits() as u64), + ("v_cvt_f64_i32", d(16), (x as i32 as f64).to_bits()), + ("v_cvt_f64_u32", d(18), (x as f64).to_bits()), + ("v_cvt_f64_f32", d(20), f.to_bits()), + ]; + for (n, got, want) in checks { + if n == "v_cvt_f64_f32" && f32::from_bits(x).is_subnormal() { + continue; // flushed or kept by the f32 denormal mode (this driver flushes it) + } + let nan = n == "v_cvt_f64_f32" && f.is_nan(); + if if nan { !f64::from_bits(got).is_nan() } else { got != want } { + bad.push(format!("{n}({x:#010x}) = {got:#x}, want {want:#x}")); + } + } + } + assert!(bad.is_empty(), "{}", bad.join("\n")); +} + #[test] fn gpu_bench_really_runs() { if !gpu_ready() {