Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)")),
Expand Down Expand Up @@ -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); }
Expand Down
54 changes: 54 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading