diff --git a/crates/viport-shader/src/emit/lower.rs b/crates/viport-shader/src/emit/lower.rs index 05ccaeb..482a108 100644 --- a/crates/viport-shader/src/emit/lower.rs +++ b/crates/viport-shader/src/emit/lower.rs @@ -8259,6 +8259,44 @@ impl<'a> Lower<'a> { let m = self.s.m.bin(c, Bin::Mul, a, b); self.s.m.bin(c, Bin::Add, m, d) } + "v_sad_u8" | "v_sad_hi_u8" | "v_sad_u16" | "v_sad_u32" | "v_msad_u8" => { + // src2 plus the sum of the unsigned differences |a - b| of + // the operands' bytes, halves or whole dwords, wrapping at 32 + // bits. v_sad_hi_u8 adds the sum shifted to the high half; + // v_msad_u8 leaves out a byte whose mask byte (src1) is 0. + let (a, b, d) = (s(self, 0)?, s(self, 1)?, s(self, 2)?); + let (width, parts) = match name { + "v_sad_u16" => (16, 2), + "v_sad_u32" => (32, 1), + _ => (8, 4), + }; + let zero = k(self, 0); + let mut sum = zero; + for i in 0..parts { + let (mut x, mut y) = (a, b); + if width < 32 { + let (sh, mask) = (k(self, width * i), k(self, (1 << width) - 1)); + x = self.s.m.bin(c, Bin::LShr, x, sh); + x = self.s.m.bin(c, Bin::And, x, mask); + y = self.s.m.bin(c, Bin::LShr, y, sh); + y = self.s.m.bin(c, Bin::And, y, mask); + } + let above = self.s.m.cmp(c, pred::IUGT, x, y); + let hi = self.s.m.select(c, above, x, y); + let lo = self.s.m.select(c, above, y, x); + let mut diff = self.s.m.bin(c, Bin::Sub, hi, lo); + if name == "v_msad_u8" { + let masked = self.s.m.cmp(c, pred::NE, y, zero); + diff = self.s.m.select(c, masked, diff, zero); + } + sum = self.s.m.bin(c, Bin::Add, sum, diff); + } + if name == "v_sad_hi_u8" { + let k16 = k(self, 16); + sum = self.s.m.bin(c, Bin::Shl, sum, k16); + } + self.s.m.bin(c, Bin::Add, sum, d) + } "v_min3_i32" | "v_max3_i32" | "v_min3_u32" | "v_max3_u32" | "v_med3_i32" | "v_med3_u32" => { let (a, b, d) = (s(self, 0)?, s(self, 1)?, s(self, 2)?); @@ -11803,6 +11841,55 @@ mod tests { )); } + /// Lowers a one-thread compute program that copies four root constants + /// (s0..s3) into v1..v4, runs `body`, and stores v0 to a raw buffer from a + /// descriptor table, so the vector instructions in `body` have an effect. + fn lowers_valu(body: &[u32]) -> Result<(), crate::ShaderError> { + let mut words = vec![ + 0xBE85_0380, // s_mov_b32 s5, 0 + 0xF408_0202, // s_load_dwordx4 s[8:11], s[4:5], 0 + 0xFA00_0000, + ]; + // v_mov_b32 v1..v4, s0..s3 + words.extend((0..4u32).map(|i| 0x7E00_0200 | (i + 1) << 17 | i)); + words.extend_from_slice(body); + words.extend_from_slice(&[ + 0xE070_0000, // buffer_store_dword v0, off, s[8:11], 0 + 0x8002_0000, + S_ENDPGM, + ]); + let constants = RootParam { + sgprs: 4, + ..root(RootType::Constants, 0, 0) + }; + let buffer = UsedResource { + kind: ResourceType::Uav, + root: RootType::Table, + table_offset_dwords: 0, + lower_bound: 0, + stride: 0, + dwords: 4, + user_data: 4, + slot: 1, + }; + lower_with( + &words, + vec![constants, root(RootType::Table, 4, 1)], + vec![buffer], + ) + .map(|_| ()) + } + + #[test] + fn sums_of_absolute_differences_lower() { + // VOP3 `op v0, v1, v2, v3`: v_sad_u8, v_sad_hi_u8, v_sad_u16, + // v_sad_u32 and v_msad_u8. + for op in [0x15Au32, 0x15B, 0x15C, 0x15D, 0x171] { + let words = [0xD400_0000 | op << 16, 0x101 | 0x102 << 9 | 0x103 << 18]; + lowers_valu(&words).unwrap_or_else(|e| panic!("VOP3 {op:#x}: {e}")); + } + } + #[test] fn a_scalar_carry_chain_reaches_the_select() { // The high dword of a 64-bit add or subtract, then a select on the