diff --git a/crates/metal2vulkan/src/tests/engine_arith.rs b/crates/metal2vulkan/src/tests/engine_arith.rs index 8b7f9c7d9..7d96f7e79 100644 --- a/crates/metal2vulkan/src/tests/engine_arith.rs +++ b/crates/metal2vulkan/src/tests/engine_arith.rs @@ -94,6 +94,44 @@ fn on(name: &str, bits: u8, b: u32) -> String { ) } +/// `%name` = an opaque `` with the bits of `lanes`, each an opaque word ([`oi`]) +/// inserted in turn. +fn ov(name: &str, lanes: &[u32]) -> String { + let n = lanes.len(); + let mut s = String::new(); + let mut prev = "zeroinitializer".to_owned(); + for (i, &b) in lanes.iter().enumerate() { + s += &oi(&format!("{name}.{i}"), b); + let next = if i + 1 == n { + format!("%{name}") + } else { + format!("%{name}.v{i}") + }; + s += &format!(" {next} = insertelement <{n} x i32> {prev}, i32 %{name}.{i}, i32 {i}\n"); + prev = next; + } + s +} + +/// `%r` = the `xor` of the lanes of ` %v`. +fn xor_lanes(v: &str, lanes: usize) -> String { + let mut s = String::new(); + for i in 0..lanes { + s += &format!(" %{v}.e{i} = extractelement <{lanes} x i32> %{v}, i32 {i}\n"); + } + let mut acc = format!("%{v}.e0"); + for i in 1..lanes { + let next = if i + 1 == lanes { + "%r".to_owned() + } else { + format!("%{v}.x{i}") + }; + s += &format!(" {next} = xor i32 {acc}, %{v}.e{i}\n"); + acc = next; + } + s +} + fn k(name: &'static str, body: String, want: u32) -> Kernel { Kernel { name, @@ -667,6 +705,60 @@ fn kernels() -> Vec { // 15 * 15 - 100 = 125; 2^32 + 7 saturates at 0xFFFFFFFF. 125 ^ 0xFFFF_FFFF, )); + // `mul24` and `mad24` multiply the operands' low 24 bits, zero-extended (`u`) or + // sign-extended (`s`), keep the product's low 32 bits and add all 32 bits of the addend, as + // the M3 Max answers in and outside the specification's range (validation cases + // `int24-products-in-range` and `int24-products-outside-24-bits`). The 32-bit product of the + // whole operands answers 0xFD000001 and 0x01800000 for the first two. + v.push(k( + "mul24_u_takes_the_low_24_bits", + format!( + "{}{} %r = call i32 @air.mul24.u.i32(i32 %a, i32 %b)\n", + oi("a", 0x01FF_FFFF), + oi("b", 0x00FF_FFFF) + ), + 0xFE00_0001, + )); + v.push(k( + "mul24_s_sign_extends_bit_23", + format!( + "{}{} %r = call i32 @air.mul24.s.i32(i32 %a, i32 %b)\n", + oi("a", 0x0080_0000), + oi("b", 0x7F00_0003) + ), + 0xFE80_0000, + )); + v.push(k( + "mad24_u_adds_all_32_bits", + format!( + "{}{} %r = call i32 @air.mad24.u.i32(i32 %a, i32 %b, i32 -1)\n", + oi("a", 0xFF00_0002), + oi("b", 7) + ), + 13, + )); + // Lanes: (-2)(-1) + 5, 5 * (-2) + 2^31, (-2^23) * 2 + 1, 3 * 2^22 - 1. + v.push(k( + "mad24_s_v4i32_per_lane", + format!( + "{}{} %m = call <4 x i32> @air.mad24.s.v4i32(<4 x i32> %a, <4 x i32> %b, <4 x i32> )\n{}", + ov("a", &[0x00FF_FFFE, 0x8000_0005, 0x0180_0000, 3]), + ov("b", &[0xFFFF_FFFF, 0xFFFF_FFFE, 2, 0x0040_0000]), + xor_lanes("m", 4) + ), + 7 ^ 0x7FFF_FFF6 ^ 0xFF00_0001 ^ 0x00BF_FFFF, + )); + // Lanes: 1 * 2, 0xFFFFFF * 2, 7 * 3. + v.push(k( + "mul24_u_v3i32_per_lane", + format!( + "{}{} %m = call <3 x i32> @air.mul24.u.v3i32(<3 x i32> %a, <3 x i32> %b)\n{}", + ov("a", &[0x0100_0001, 0x00FF_FFFF, 7]), + ov("b", &[0x0200_0002, 2, 0xFF00_0003]), + xor_lanes("m", 3) + ), + 2 ^ 0x01FF_FFFE ^ 21, + )); v.push(Kernel { name: "extract_bits_on_i64", body: format!( @@ -932,6 +1024,11 @@ declare i8 @air.mad_sat.s.i8(i8, i8, i8) declare i32 @air.mad_sat.u.i32(i32, i32, i32) declare i64 @air.mad_sat.s.i64(i64, i64, i64) declare i64 @air.mad_sat.u.i64(i64, i64, i64) +declare i32 @air.mul24.u.i32(i32, i32) +declare i32 @air.mul24.s.i32(i32, i32) +declare i32 @air.mad24.u.i32(i32, i32, i32) +declare <4 x i32> @air.mad24.s.v4i32(<4 x i32>, <4 x i32>, <4 x i32>) +declare <3 x i32> @air.mul24.u.v3i32(<3 x i32>, <3 x i32>) declare <5 x i64> @llvm.umax.v5i64(<5 x i64>, <5 x i64>) declare i64 @air.extract_bits.s.i64(i64, i32, i32) declare i32 @air.pack.unorm4x8.v4f32(<4 x float>)