From 83d3d05107998927f48efe501584dacfd934c14c Mon Sep 17 00:00:00 2001 From: upipibi Date: Tue, 24 Nov 2026 19:13:42 -0600 Subject: [PATCH 1/2] Recompiler: D16 format loads of UNORM / SNORM data round once to nearest A D16 buffer format load packed a UNORM / SNORM channel with f32tof16 of the decoded f32: two roundings, the second toward zero on D3D12. RDNA2 rounds code / (2^w - 1) once to nearest even, so about half of all codes came back one unit in the last place low (16_UNORM 0x0801: 0x2800 for 0x2801). That value's binary expansion repeats the w-bit code, so its first 32 bits, floor((code << 32) / (2^w - 1)), decide the rounding and a tie cannot happen. Both paths; checked against exact rounding for every code at w = 2..16. #2026 (XCL-BT-020's third item). Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/lower_mem.rs | 94 ++++++++++++++++++++++++++-- crates/xcl/src/gpu/recomp/emit.rs | 26 +++++++- crates/xcl/src/gpu/recomp/tests.rs | 47 ++++++++++++++ 3 files changed, 160 insertions(+), 7 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index a1d034e74..eb025c76a 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -523,6 +523,88 @@ impl Lw { self.s.m.set_insert_before_term(prev_ins); r } + /// A D16 format load's non-integer channel `s` (decoded f32 bits) of DST_SEL `sel` as f16. + /// UNORM / SNORM data is `code / (2^w - 1)`, rounded once to nearest as RDNA2 does: its + /// binary expansion repeats the w-bit code, so its first 32 bits decide the rounding (a tie + /// cannot happen). Other channels: `f32tof16`, as before. + fn d16_fmt(&mut self, s: Value, sel: Value, info: Value) -> Value { + let f = self.f(s); + let plain = self.s.f32_to_f16(f); + let ty = self.lshrc(info, 3); + let ty = self.andc(ty, 7); + let four = self.c(4); + let k = self.sub(sel, four); + let k = self.andc(k, 3); + let six = self.c(6); + let k6 = self.mul(k, six); + let eight = self.c(8); + let shift = self.add(k6, eight); + let w = self.bin(BinOp::LShr, info, shift); + let w = self.andc(w, 63); + let snorm = self.cmpc(Pred::Eq, ty, 1); + let one = self.c(1); + let wm1 = self.sub(w, one); + let we = self.sel(snorm, wm1, w); + let dmax = self.bin(BinOp::Shl, one, we); + let dmax = self.sub(dmax, one); + // the code, back from the decoded value (within 2 ULP of exact) + let zf = self.fc(0.0); + let onef = self.fc(1.0); + let a = self.fabs(f); + let u = self.fmax(f, zf); + let a = self.sel(snorm, a, u); + let a = self.fmin(a, onef); + let df = self.uitof(dmax); + let y = self.fmul(a, df); + let y = self.un(crate::gpu::dxil::ops_core::op::ROUND_NE, y); + let y = self.ftoui(y); + // first 32 bits of y / dmax + let y64 = self.zext64(y); + let k32 = self.c64(32); + let y64 = self.bin(BinOp::Shl, y64, k32); + let d64 = self.zext64(dmax); + let p = self.bin(BinOp::UDiv, y64, d64); + let p = self.trunc32(p); + let lz = self.lzcnt(p); + let fourteen = self.c(14); + let e = self.sub(fourteen, lz); + // normal: 11 bits after the leading one and the round bit + let pn = self.bin(BinOp::Shl, p, lz); + let b = self.lshrc(pn, 20); + let half_ = self.lshrc(b, 1); + let rb = self.andc(b, 1); + let m = self.add(half_, rb); + let em1 = self.sub(e, one); + let eh = self.shlc(em1, 10); + let normal = self.add(eh, m); + // subnormal + let s8 = self.lshrc(p, 8); + let r7 = self.lshrc(p, 7); + let r7 = self.andc(r7, 1); + let sub = self.add(s8, r7); + let is_normal = self.cmp(Pred::SGe, e, one); + let h = self.sel(is_normal, normal, sub); + let zero = self.c(0); + let y0 = self.cmpc(Pred::Eq, y, 0); + let h = self.sel(y0, zero, h); + let full = self.cmp(Pred::UGe, y, dmax); + let h = self.selc(full, 0x3C00, h); + let neg = self.fcmp(Pred::FOLt, f, zf); + let sbit = self.c(0x8000); + let sgn = self.land(snorm, neg); + let sgn = self.sel(sgn, sbit, zero); + let h = self.or(h, sgn); + // which channels: UNORM / SNORM (ty 0 / 1) of 2..16 bits, a component (sel >= 4) + let is_norm = self.cmpc(Pred::ULe, ty, 1); + let comp = self.cmpc(Pred::UGe, sel, 4); + let w_lo = self.cmpc(Pred::UGe, w, 2); + let w_hi = self.cmpc(Pred::ULe, w, 16); + let use_n = self.land(is_norm, comp); + let use_n = self.land(use_n, w_lo); + let use_n = self.land(use_n, w_hi); + self.sel(use_n, h, plain) + } + fn fmt_desc(&mut self, d3: Value, mtbuf_fmt: Option) -> FmtDesc { let key = (d3, mtbuf_fmt); if let Some(&cached) = self.fmt_desc_cache.get(&key) { @@ -1297,8 +1379,8 @@ impl Lw { if let Some((_, _, high)) = d16_format { let cur0 = self.vreg(vdata); let lo16 = self.andc(s_val, 0xFFFF); - let sf = self.f(s_val); - let h16 = self.s.f32_to_f16(sf); + let sel0 = self.andc(d3, 7); + let h16 = self.d16_fmt(s_val, sel0, c1.desc.info); let h = self.sel(c1.desc.isint, lo16, h16); if high { let h = self.shlc(h, 16); @@ -1342,8 +1424,9 @@ impl Lw { let half = |lw: &mut Lw, comp: u64| -> Value { let s = decode_sel(lw, comp); let lo = lw.andc(s, 0xFFFF); - let sf = lw.f(s); - let h = lw.s.f32_to_f16(sf); + let sel = lw.lshrc(d3, 3 * comp as u32); + let sel = lw.andc(sel, 7); + let h = lw.d16_fmt(s, sel, desc.info); lw.sel(desc.isint, lo, h) }; for i in 0..count { @@ -1370,8 +1453,7 @@ impl Lw { let sel = lw.andc(sel, 7); let s = lw.dst_sel(&c, sel, desc.isint); let lo = lw.andc(s, 0xFFFF); - let sf = lw.f(s); - let h = lw.s.f32_to_f16(sf); + let h = lw.d16_fmt(s, sel, desc.info); lw.sel(desc.isint, lo, h) }; for i in 0..count { diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043..f1e4710a0 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -2167,7 +2167,8 @@ impl Em { if let Some((_, components, high)) = d16_format { let half = |component: u64| { let selected = format!("dst_sel({t}_c, ({d3} >> {}u) & 7u, {t}_int)", 3 * component); - format!("select({t}_int, {selected} & 0xFFFFu, f32tof16(asfloat({selected})))") + let sel = format!("(({d3} >> {}u) & 7u)", 3 * component); + format!("select({t}_int, {selected} & 0xFFFFu, d16_fmt({selected}, {sel}, {t}_info))") }; let mut assigns = Vec::new(); if high { @@ -5864,6 +5865,29 @@ void st_bytes(uint a, uint4 r, uint n) { st_part(b, m << s, r.x << s); if (s != 0u && s + 8u * n > 32u) st_part(b + 4u, m >> (32u - s), r.x >> (32u - s)); } +// code / (2^w - 1) as f16, rounded once to nearest (RDNA2's D16 of UNORM / SNORM data): the +// binary expansion repeats the w-bit code, so p (its first 32 bits) decides the rounding, and a +// tie cannot happen. f32tof16 of the decoded f32 rounds twice, the second time toward zero. +uint norm16_rep(uint y, uint w) { + uint d = (1u << w) - 1u; + if (y == 0u) return 0u; + if (y >= d) return 0x3C00u; + uint p = (uint)(((uint64_t)y << 32) / (uint64_t)d); + uint lz = 31u - firstbithigh(p); + int e = 14 - (int)lz; + if (e >= 1) { uint b = (p << lz) >> 20; return ((uint)(e - 1) << 10) + (b >> 1) + (b & 1u); } + return (p >> 8) + ((p >> 7) & 1u); +} +// a D16 format load's non-integer channel x (decoded f32 bits) of DST_SEL sel +uint d16_fmt(uint x, uint sel, uint info) { + uint ty = (info >> 3) & 7u; + uint w = (info >> (8u + 6u * ((sel - 4u) & 3u))) & 63u; + if (ty > 1u || sel < 4u || w < 2u || w > 16u) return f32tof16(asfloat(x)); + float f = asfloat(x); + if (ty == 0u) return norm16_rep((uint)round(saturate(f) * (float)((1u << w) - 1u)), w); + uint h = norm16_rep((uint)round(min(abs(f), 1.0f) * (float)((1u << (w - 1u)) - 1u)), w - 1u); + return h | select(f < 0.0f, 0x8000u, 0u); +} uint dst_sel(uint4 c, uint sel, bool isint) { return select(sel == 0u, 0u, select(sel == 1u, select(isint, 1u, 0x3F800000u), pick4(c, (sel - 4u) & 3u))); } "#; diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e64..83f3beb11 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -9027,3 +9027,50 @@ fn gpu_scalar_sext_mulhi_and_vector_i24_sub_co_ci_u64_shifts() { assert_eq!(v[22], subrev, "lane {l} v_subrev_co_ci_u32"); } } + +/// D16 format loads of 16_UNORM / 16_SNORM: RDNA2 rounds the normalised value to f16 once, to +/// nearest even (XCL-BT-020, gfx1031; AnyPS5#22750 over 256 16-bit codes, gfx1036). 64 lanes each +/// load their own code; the expected halves are the exact value rounded once. +#[test] +fn gpu_buffer_d16_loads_of_16_bit_norms_round_once() { + if !gpu_ready() { + return; + } + const UNORM: [u16; 64] = [0x0000, 0xFFFF, 0xFFAF, 0xFFEF, 0x0801, 0x0803, 0x0805, 0x0807, 0x8000, 0x7FFF, 0x1234, 0x4321, 0xEBBE, 0x78FA, 0xE20F, 0xF2BF, 0x4275, 0x8398, 0xBD51, 0x1BC1, 0xB57A, 0x7378, 0x64F0, 0x9631, 0xA93D, 0x1078, 0xE750, 0x620A, 0x03BF, 0x5971, 0x9EFA, 0xA070, 0xC8CB, 0x2DF2, 0xF2DD, 0xEEA9, 0x028F, 0xD55A, 0x6C19, 0x23A3, 0xE272, 0x61DF, 0x186A, 0xB09A, 0x0F62, 0xAD5F, 0x89C7, 0xDB80, 0x92C3, 0x507A, 0x6FF9, 0x5CFD, 0xF790, 0xCFC4, 0xB827, 0xE334, 0x70BB, 0xF609, 0x5ADA, 0x4EDA, 0xDEC6, 0xCA73, 0xAAE7, 0x0E6C]; + const UNORM_F16: [u16; 64] = [0x0000, 0x3C00, 0x3BFD, 0x3BFF, 0x2801, 0x2802, 0x2803, 0x2804, 0x3800, 0x3800, 0x2C8D, 0x3432, 0x3B5E, 0x3790, 0x3B10, 0x3B96, 0x3427, 0x381D, 0x39EB, 0x2EF0, 0x39AC, 0x3738, 0x364F, 0x38B2, 0x394A, 0x2C1E, 0x3B3B, 0x3621, 0x237E, 0x3597, 0x38F8, 0x3904, 0x3A46, 0x31BE, 0x3B97, 0x3B75, 0x211E, 0x3AAB, 0x36C2, 0x3074, 0x3B14, 0x361E, 0x2E1B, 0x3985, 0x2BB1, 0x396B, 0x384E, 0x3ADC, 0x3896, 0x3508, 0x3700, 0x35D0, 0x3BBD, 0x3A7E, 0x39C1, 0x3B1A, 0x370C, 0x3BB0, 0x35AE, 0x34EE, 0x3AF6, 0x3A54, 0x3957, 0x2B36]; + const SNORM: [u16; 64] = [0x0000, 0x7FFF, 0x8000, 0x8001, 0x7FF7, 0x8009, 0x0401, 0x0403, 0xFBFF, 0xC000, 0x4000, 0x0001, 0x0669, 0x5B5B, 0x93FA, 0x9DB5, 0xAA7E, 0x4B72, 0x6920, 0xC24D, 0x6F99, 0x4FDA, 0x507E, 0xE531, 0x1C88, 0x0D1A, 0x3151, 0xDB97, 0xDA66, 0xADB0, 0xF832, 0xE808, 0xFE62, 0xD349, 0xEFCC, 0xFC89, 0xFF9D, 0xAEB5, 0x3CAB, 0xA475, 0x1B07, 0xFCAC, 0x2B11, 0x067A, 0xA023, 0x872F, 0xAC57, 0x775A, 0x3629, 0x315A, 0xA1B8, 0x6C88, 0xA7C3, 0xD5E3, 0xECDC, 0x4E5C, 0x6CEB, 0xFB6D, 0x0ED9, 0x00B7, 0x0A51, 0x205A, 0x490A, 0x246B]; + const SNORM_F16: [u16; 64] = [0x0000, 0x3C00, 0xBC00, 0xBC00, 0x3BFF, 0xBBFF, 0x2801, 0x2803, 0xA801, 0xB800, 0x3800, 0x0200, 0x2A69, 0x39B6, 0xBAC0, 0xBA25, 0xB958, 0x38B7, 0x3A92, 0xB7B6, 0x3AFA, 0x38FE, 0x3908, 0xB2B4, 0x3322, 0x2E8D, 0x362A, 0xB48D, 0xB4B3, 0xB925, 0xABCE, 0xB1FE, 0xA278, 0xB597, 0xB00D, 0xA6EE, 0x9A30, 0xB915, 0x3795, 0xB9B9, 0x32C2, 0xA6A8, 0x3562, 0x2A7A, 0xB9FE, 0xBB8D, 0xB93B, 0x3B76, 0x36C5, 0x362B, 0xB9E5, 0x3AC9, 0xB984, 0xB544, 0xB0C9, 0x38E6, 0x3ACF, 0xA893, 0x2F6D, 0x1DB8, 0x2D29, 0x340B, 0x3891, 0x348D]; + let ident = 4 | 5 << 3 | 6 << 6 | 7 << 9; + let vs = |base: u32, fmt: u32| [base, 0, 0x100, ident | fmt << 12]; + let code = prog(&[ + ("BUFFER_LOAD_FORMAT_D16_X", "ENC_MUBUF", &[("VDATA", 10), ("VADDR", 1), ("SRSRC", 3), ("SOFFSET", 128), ("OFFEN", 1)], None), + ("BUFFER_LOAD_FORMAT_D16_X", "ENC_MUBUF", &[("VDATA", 11), ("VADDR", 1), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1)], None), + ]); + let mut mem = vec![0u32; 512]; + for l in 0..64 { + let put = |mem: &mut Vec, base: usize, v: u16| { + let (w, sh) = ((base + 2 * l) / 4, 16 * (l % 2)); + mem[w] |= (v as u32) << sh; + }; + put(&mut mem, 0x100, UNORM[l]); + put(&mut mem, 0x200, SNORM[l]); + } + let mut st = KernelState::default(); + st.sgpr[12..16].copy_from_slice(&vs(0x100, 7)); // 16_UNORM + st.sgpr[16..20].copy_from_slice(&vs(0x200, 8)); // 16_SNORM + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[1] = 2 * l as u32; + } + let r = runner::run_code(&code, &st, &mem).unwrap(); + let mut bad = Vec::new(); + for l in 0..64 { + let (u, s) = ((r.vgpr[l][10] & 0xFFFF) as u16, (r.vgpr[l][11] & 0xFFFF) as u16); + if u != UNORM_F16[l] { + bad.push(format!("16_UNORM {:#06x}: {u:#06x}, want {:#06x}", UNORM[l], UNORM_F16[l])); + } + if s != SNORM_F16[l] { + bad.push(format!("16_SNORM {:#06x}: {s:#06x}, want {:#06x}", SNORM[l], SNORM_F16[l])); + } + } + assert!(bad.is_empty(), "{} of 128 differ:\n{}", bad.len(), bad.join("\n")); +} From b0aae2ecbb5c92bda70990ab3cb75475e869fbeb Mon Sep 17 00:00:00 2001 From: upipibi Date: Tue, 24 Nov 2026 22:26:50 -0600 Subject: [PATCH 2/2] Recompiler: D16 format loads of float data truncate on every host d16_fmt passed float channels to f32tof16 / legacyF32ToF16, whose rounding is the host's: it truncates on the RTX 3070 and WARP but rounds to nearest under vkd3d-proton (Marvisak/VIctory#2006), where RDNA2 truncates (boykopovar/AnyPS5#22750). Use f16_rtz in both paths, as the format stores already do. gpu_buffer_d16_loads_of_32_bit_floats_truncate loads 32_FLOAT values that the two roundings tell apart (1 + 1.5 / 1.75 ulp, 65535, +/-70000, a denormal half-way, -1.5, +inf). Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/lower_mem.rs | 5 +++-- crates/xcl/src/gpu/recomp/emit.rs | 2 +- crates/xcl/src/gpu/recomp/tests.rs | 32 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index eb025c76a..60e9e7d90 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -526,10 +526,11 @@ impl Lw { /// A D16 format load's non-integer channel `s` (decoded f32 bits) of DST_SEL `sel` as f16. /// UNORM / SNORM data is `code / (2^w - 1)`, rounded once to nearest as RDNA2 does: its /// binary expansion repeats the w-bit code, so its first 32 bits decide the rounding (a tie - /// cannot happen). Other channels: `f32tof16`, as before. + /// cannot happen). Other channels truncate (`f16_rtz`), as RDNA2 does, whatever the host's + /// `legacyF32ToF16` rounds to. fn d16_fmt(&mut self, s: Value, sel: Value, info: Value) -> Value { let f = self.f(s); - let plain = self.s.f32_to_f16(f); + let plain = self.f16_rtz(f); let ty = self.lshrc(info, 3); let ty = self.andc(ty, 7); let four = self.c(4); diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index f1e4710a0..a58964c4f 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -5882,7 +5882,7 @@ uint norm16_rep(uint y, uint w) { uint d16_fmt(uint x, uint sel, uint info) { uint ty = (info >> 3) & 7u; uint w = (info >> (8u + 6u * ((sel - 4u) & 3u))) & 63u; - if (ty > 1u || sel < 4u || w < 2u || w > 16u) return f32tof16(asfloat(x)); + if (ty > 1u || sel < 4u || w < 2u || w > 16u) return f16_rtz(asfloat(x)); float f = asfloat(x); if (ty == 0u) return norm16_rep((uint)round(saturate(f) * (float)((1u << w) - 1u)), w); uint h = norm16_rep((uint)round(min(abs(f), 1.0f) * (float)((1u << (w - 1u)) - 1u)), w - 1u); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 83f3beb11..53b6ec2e3 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -9028,6 +9028,38 @@ fn gpu_scalar_sext_mulhi_and_vector_i24_sub_co_ci_u64_shifts() { } } +#[test] +fn gpu_buffer_d16_loads_of_32_bit_floats_truncate() { + // RDNA2 packs a D16 load of a 32-bit float toward zero (boykopovar/AnyPS5#22750). The host's + // legacyF32ToF16 truncates on some drivers and rounds to nearest on others (vkd3d-proton), so + // these values tell the two apart: 1 + 1.5 / 1.75 ulp, overflow, a denormal half-way. + if !gpu_ready() { + return; + } + const IN: [u32; 8] = [0x3F80_3000, 0x3F80_3800, 0x4788_B800, 0xC788_B800, 0x477F_FF00, 0x33C0_0000, 0xBFC0_0000, 0x7F80_0000]; + const WANT: [u16; 8] = [0x3C01, 0x3C01, 0x7BFF, 0xFBFF, 0x7BFF, 0x0001, 0xBE00, 0x7C00]; + let ident = 4 | 5 << 3 | 6 << 6 | 7 << 9; + let code = prog(&[("BUFFER_LOAD_FORMAT_D16_X", "ENC_MUBUF", &[("VDATA", 10), ("VADDR", 1), ("SRSRC", 3), ("SOFFSET", 128), ("OFFEN", 1)], None)]); + let mut mem = vec![0u32; 512]; + mem[0x40..0x48].copy_from_slice(&IN); + let mut st = KernelState::default(); + st.sgpr[12..16].copy_from_slice(&[0x100, 0, 0x100, ident | 22 << 12]); // 32_FLOAT + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[1] = 4 * (l % 8) as u32; + } + let r = runner::run_code(&code, &st, &mem).unwrap(); + let mut bad = Vec::new(); + for l in 0..64 { + let got = (r.vgpr[l][10] & 0xFFFF) as u16; + if got != WANT[l % 8] { + bad.push(format!("lane {l}: {:#010x} -> {got:#06x}, want {:#06x}", IN[l % 8], WANT[l % 8])); + } + } + assert!(bad.is_empty(), "{} of 64 differ: +{}", bad.len(), bad.join(" +")); +} + /// D16 format loads of 16_UNORM / 16_SNORM: RDNA2 rounds the normalised value to f16 once, to /// nearest even (XCL-BT-020, gfx1031; AnyPS5#22750 over 256 16-bit codes, gfx1036). 64 lanes each /// load their own code; the expected halves are the exact value rounded once.