aom: Add Neon USMMLA impl. for 6-tap dist_wtd_convolve_x_dist_wtd_avg

From f570a1c98b51baeaf1813644a636e5c413eb86dd Mon Sep 17 00:00:00 2001
From: Jonathan Wright <[EMAIL REDACTED]>
Date: Wed, 14 Aug 2024 17:43:12 +0100
Subject: [PATCH] Add Neon USMMLA impl. for 6-tap
 dist_wtd_convolve_x_dist_wtd_avg

Use a USMMLA implementation for the 6-tap and 4-tap dist_wtd averaging
case in av1_dist_wtd_convolve_x_neon_i8mm. The rationale is similar to
previous patches adding USMMLA code paths:

By permuting the input samples and the 6-tap filter we can use the
Armv8.6 I8MM USMMLA matrix multiply instructions to accelerate
horizontal 6-tap convolutions. The 2x8 by 8x2 matrix multiply
instruction does twice the work of a USDOT dot product instruction.

We use this new USMMLA 6-tap path for 4-tap filters as well since it
uses exactly the same number of instructions as the previous USDOT
implementation.

Change-Id: I5b294ea5e0e6712896b90cb9417d0b049d2c2c60
---
 av1/common/arm/compound_convolve_neon_i8mm.c | 234 +++++++++++--------
 1 file changed, 136 insertions(+), 98 deletions(-)

diff --git a/av1/common/arm/compound_convolve_neon_i8mm.c b/av1/common/arm/compound_convolve_neon_i8mm.c
index 685f13fcc..0589dfb15 100644
--- a/av1/common/arm/compound_convolve_neon_i8mm.c
+++ b/av1/common/arm/compound_convolve_neon_i8mm.c
@@ -328,21 +328,44 @@ void av1_dist_wtd_convolve_2d_neon_i8mm(
   }
 }
 
-static inline uint16x4_t convolve4_4_x(uint8x16_t samples,
-                                       const int8x8_t x_filter,
+static inline uint16x4_t convolve6_4_x(uint8x16_t samples,
+                                       const int8x16_t x_filter,
                                        const uint8x16_t permute_tbl,
                                        const int32x4_t round_offset) {
-  // Permute samples ready for dot product.
-  // { 0,  1,  2,  3,  1,  2,  3,  4,  2,  3,  4,  5,  3,  4,  5,  6 }
+  // Permute samples ready for matrix multiply.
+  // { 0,  1,  2,  3,  4,  5,  6,  7,  2,  3,  4,  5,  6,  7,  8,  9 }
   uint8x16_t permuted_samples = vqtbl1q_u8(samples, permute_tbl);
 
-  // First 4 output values.
-  int32x4_t sum = vusdotq_lane_s32(round_offset, permuted_samples, x_filter, 0);
+  // These instructions multiply a 2x8 matrix (samples) by an 8x2 matrix
+  // (filter), destructively accumulating into the destination register.
+  int32x4_t sum = vusmmlaq_s32(round_offset, permuted_samples, x_filter);
 
   // We halved the convolution filter values so -1 from the right shift.
   return vreinterpret_u16_s16(vshrn_n_s32(sum, ROUND0_BITS - 1));
 }
 
+static inline uint16x8_t convolve6_8_x(uint8x16_t samples,
+                                       const int8x16_t x_filter,
+                                       const uint8x16x2_t permute_tbl,
+                                       const int32x4_t round_offset) {
+  // Permute samples ready for matrix multiply.
+  // { 0,  1,  2,  3,  4,  5,  6,  7,  2,  3,  4,  5,  6,  7,  8,  9 }
+  // { 4,  5,  6,  7,  8,  9, 10, 11,  6,  7,  8,  9, 10, 11, 12, 13 }
+  uint8x16_t permuted_samples[2] = { vqtbl1q_u8(samples, permute_tbl.val[0]),
+                                     vqtbl1q_u8(samples, permute_tbl.val[1]) };
+
+  // These instructions multiply a 2x8 matrix (samples) by an 8x2 matrix
+  // (filter), destructively accumulating into the destination register.
+  int32x4_t sum0123 = vusmmlaq_s32(round_offset, permuted_samples[0], x_filter);
+  int32x4_t sum4567 = vusmmlaq_s32(round_offset, permuted_samples[1], x_filter);
+
+  // Narrow and re-pack.
+  // We halved the convolution filter values so -1 from the right shift.
+  int16x8_t res = vcombine_s16(vshrn_n_s32(sum0123, ROUND0_BITS - 1),
+                               vshrn_n_s32(sum4567, ROUND0_BITS - 1));
+  return vreinterpretq_u16_s16(res);
+}
+
 static inline uint16x8_t convolve8_8_x(uint8x16_t samples,
                                        const int8x8_t x_filter,
                                        const uint8x16x3_t permute_tbl,
@@ -372,10 +395,10 @@ static inline uint16x8_t convolve8_8_x(uint8x16_t samples,
   return vreinterpretq_u16_s16(res);
 }
 
-static inline void dist_wtd_convolve_x_dist_wtd_avg_neon_i8mm(
-    const uint8_t *src, int src_stride, uint8_t *dst8, int dst8_stride, int w,
-    int h, const InterpFilterParams *filter_params_x, const int subpel_x_qn,
-    ConvolveParams *conv_params) {
+static inline void dist_wtd_convolve_x_dist_wtd_avg_6tap_neon_i8mm(
+    const uint8_t *src, int src_stride, uint16_t *dst, int dst_stride,
+    uint8_t *dst8, int dst8_stride, int w, int h, const int16_t *x_filter_ptr,
+    const uint16_t fwd_offset, const uint16_t bck_offset) {
   assert(w % 4 == 0);
   assert(h % 4 == 0);
 
@@ -390,66 +413,49 @@ static inline void dist_wtd_convolve_x_dist_wtd_avg_neon_i8mm(
   const int32x4_t round_offset_shim = vdupq_n_s32(
       (round_offset << (ROUND0_BITS - 1)) + (1 << ((ROUND0_BITS - 1) - 1)));
 
-  const uint16_t fwd_offset = conv_params->fwd_offset;
-  const uint16_t bck_offset = conv_params->bck_offset;
-
-  // Horizontal filter.
-  const int16_t *x_filter_ptr = av1_get_interp_filter_subpel_kernel(
-      filter_params_x, subpel_x_qn & SUBPEL_MASK);
-
-  const int horiz_offset = filter_params_x->taps / 2 - 1;
-  const uint8_t *src_ptr = src - horiz_offset;
-  CONV_BUF_TYPE *dst_ptr = conv_params->dst;
-  uint8_t *dst8_ptr = dst8;
-  int dst_stride = conv_params->dst_stride;
-  int height = h;
+  // Filter values are even, so halve to reduce intermediate precision reqs.
+  const int8x8_t x_filter_s8 = vshrn_n_s16(vld1q_s16(x_filter_ptr), 1);
+  // Stagger the filter for use with the matrix multiply instructions.
+  // { f0, f1, f2, f3, f4, f5,  0,  0,  0, f0, f1, f2, f3, f4, f5,  0 }
+  const int8x16_t x_filter =
+      vcombine_s8(vext_s8(x_filter_s8, x_filter_s8, 1), x_filter_s8);
 
   if (w == 4) {
-    const uint8x16_t permute_tbl = vld1q_u8(kDotProdPermuteTbl);
-    // 4-tap filters are used for blocks having width <= 4.
-    // Filter values are even, so halve to reduce intermediate precision reqs.
-    const int8x8_t x_filter =
-        vshrn_n_s16(vcombine_s16(vld1_s16(x_filter_ptr + 2), vdup_n_s16(0)), 1);
-
-    src_ptr += 2;
-
+    const uint8x16_t permute_tbl = vld1q_u8(kMatMulPermuteTbl);
     do {
       uint8x16_t s0, s1, s2, s3;
-      load_u8_16x4(src_ptr, src_stride, &s0, &s1, &s2, &s3);
+      load_u8_16x4(src, src_stride, &s0, &s1, &s2, &s3);
 
       uint16x4_t d0 =
-          convolve4_4_x(s0, x_filter, permute_tbl, round_offset_shim);
+          convolve6_4_x(s0, x_filter, permute_tbl, round_offset_shim);
       uint16x4_t d1 =
-          convolve4_4_x(s1, x_filter, permute_tbl, round_offset_shim);
+          convolve6_4_x(s1, x_filter, permute_tbl, round_offset_shim);
       uint16x4_t d2 =
-          convolve4_4_x(s2, x_filter, permute_tbl, round_offset_shim);
+          convolve6_4_x(s2, x_filter, permute_tbl, round_offset_shim);
       uint16x4_t d3 =
-          convolve4_4_x(s3, x_filter, permute_tbl, round_offset_shim);
+          convolve6_4_x(s3, x_filter, permute_tbl, round_offset_shim);
 
       uint16x4_t dd0, dd1, dd2, dd3;
-      load_u16_4x4(dst_ptr, dst_stride, &dd0, &dd1, &dd2, &dd3);
+      load_u16_4x4(dst, dst_stride, &dd0, &dd1, &dd2, &dd3);
 
       uint8x8_t d01_u8, d23_u8;
       compute_dist_wtd_avg_4x4(dd0, dd1, dd2, dd3, d0, d1, d2, d3, fwd_offset,
                                bck_offset, round_offset_vec, &d01_u8, &d23_u8);
 
-      store_u8x4_strided_x2(dst8_ptr + 0 * dst8_stride, dst8_stride, d01_u8);
-      store_u8x4_strided_x2(dst8_ptr + 2 * dst8_stride, dst8_stride, d23_u8);
+      store_u8x4_strided_x2(dst8 + 0 * dst8_stride, dst8_stride, d01_u8);
+      store_u8x4_strided_x2(dst8 + 2 * dst8_stride, dst8_stride, d23_u8);
 
-      src_ptr += 4 * src_stride;
-      dst_ptr += 4 * dst_stride;
-      dst8_ptr += 4 * dst8_stride;
-      height -= 4;
-    } while (height != 0);
+      src += 4 * src_stride;
+      dst += 4 * dst_stride;
+      dst8 += 4 * dst8_stride;
+      h -= 4;
+    } while (h != 0);
   } else {
-    const uint8x16x3_t permute_tbl = vld1q_u8_x3(kDotProdPermuteTbl);
-    // Filter values are even, so halve to reduce intermediate precision reqs.
-    const int8x8_t x_filter = vshrn_n_s16(vld1q_s16(x_filter_ptr), 1);
-
+    const uint8x16x2_t permute_tbl = vld1q_u8_x2(kMatMulPermuteTbl);
     do {
-      const uint8_t *s = src_ptr;
-      CONV_BUF_TYPE *d = dst_ptr;
-      uint8_t *d_u8 = dst8_ptr;
+      const uint8_t *s = src;
+      uint16_t *d = dst;
+      uint8_t *d_u8 = dst8;
       int width = w;
 
       do {
@@ -457,13 +463,13 @@ static inline void dist_wtd_convolve_x_dist_wtd_avg_neon_i8mm(
         load_u8_16x4(s, src_stride, &s0, &s1, &s2, &s3);
 
         uint16x8_t d0 =
-            convolve8_8_x(s0, x_filter, permute_tbl, round_offset_shim);
+            convolve6_8_x(s0, x_filter, permute_tbl, round_offset_shim);
         uint16x8_t d1 =
-            convolve8_8_x(s1, x_filter, permute_tbl, round_offset_shim);
+            convolve6_8_x(s1, x_filter, permute_tbl, round_offset_shim);
         uint16x8_t d2 =
-            convolve8_8_x(s2, x_filter, permute_tbl, round_offset_shim);
+            convolve6_8_x(s2, x_filter, permute_tbl, round_offset_shim);
         uint16x8_t d3 =
-            convolve8_8_x(s3, x_filter, permute_tbl, round_offset_shim);
+            convolve6_8_x(s3, x_filter, permute_tbl, round_offset_shim);
 
         uint16x8_t dd0, dd1, dd2, dd3;
         load_u16_8x4(d, dst_stride, &dd0, &dd1, &dd2, &dd3);
@@ -480,50 +486,75 @@ static inline void dist_wtd_convolve_x_dist_wtd_avg_neon_i8mm(
         d_u8 += 8;
         width -= 8;
       } while (width != 0);
-      src_ptr += 4 * src_stride;
-      dst_ptr += 4 * dst_stride;
-      dst8_ptr += 4 * dst8_stride;
-      height -= 4;
-    } while (height != 0);
+      src += 4 * src_stride;
+      dst += 4 * dst_stride;
+      dst8 += 4 * dst8_stride;
+      h -= 4;
+    } while (h != 0);
   }
 }
 
-static inline uint16x4_t convolve6_4_x(uint8x16_t samples,
-                                       const int8x16_t x_filter,
-                                       const uint8x16_t permute_tbl,
-                                       const int32x4_t round_offset) {
-  // Permute samples ready for matrix multiply.
-  // { 0,  1,  2,  3,  4,  5,  6,  7,  2,  3,  4,  5,  6,  7,  8,  9 }
-  uint8x16_t permuted_samples = vqtbl1q_u8(samples, permute_tbl);
+static inline void dist_wtd_convolve_x_dist_wtd_avg_8tap_neon_i8mm(
+    const uint8_t *src, int src_stride, uint16_t *dst, int dst_stride,
+    uint8_t *dst8, int dst8_stride, int w, int h, const int16_t *x_filter_ptr,
+    const uint16_t fwd_offset, const uint16_t bck_offset) {
+  assert(w % 4 == 0);
+  assert(h % 4 == 0);
 
-  // These instructions multiply a 2x8 matrix (samples) by an 8x2 matrix
-  // (filter), destructively accumulating into the destination register.
-  int32x4_t sum = vusmmlaq_s32(round_offset, permuted_samples, x_filter);
+  const int bd = 8;
+  const int offset_bits = bd + 2 * FILTER_BITS - ROUND0_BITS;
+  const int16_t round_offset = (1 << (offset_bits - COMPOUND_ROUND1_BITS)) +
+                               (1 << (offset_bits - COMPOUND_ROUND1_BITS - 1));
+  const int16x8_t round_offset_vec = vdupq_n_s16(round_offset);
+  // A shim of 1 << ((ROUND0_BITS - 1) - 1) enables us to use non-rounding
+  // shifts - which are generally faster than rounding shifts on modern CPUs.
+  // (The extra -1 is needed because we halved the filter values.)
+  const int32x4_t round_offset_shim = vdupq_n_s32(
+      (round_offset << (ROUND0_BITS - 1)) + (1 << ((ROUND0_BITS - 1) - 1)));
 
-  // We halved the convolution filter values so -1 from the right shift.
-  return vreinterpret_u16_s16(vshrn_n_s32(sum, ROUND0_BITS - 1));
-}
+  const uint8x16x3_t permute_tbl = vld1q_u8_x3(kDotProdPermuteTbl);
+  // Filter values are even, so halve to reduce intermediate precision reqs.
+  const int8x8_t x_filter = vshrn_n_s16(vld1q_s16(x_filter_ptr), 1);
 
-static inline uint16x8_t convolve6_8_x(uint8x16_t samples,
-                                       const int8x16_t x_filter,
-                                       const uint8x16x2_t permute_tbl,
-                                       const int32x4_t round_offset) {
-  // Permute samples ready for matrix multiply.
-  // { 0,  1,  2,  3,  4,  5,  6,  7,  2,  3,  4,  5,  6,  7,  8,  9 }
-  // { 4,  5,  6,  7,  8,  9, 10, 11,  6,  7,  8,  9, 10, 11, 12, 13 }
-  uint8x16_t permuted_samples[2] = { vqtbl1q_u8(samples, permute_tbl.val[0]),
-                                     vqtbl1q_u8(samples, permute_tbl.val[1]) };
+  do {
+    const uint8_t *s = src;
+    uint16_t *d = dst;
+    uint8_t *d_u8 = dst8;
+    int width = w;
 
-  // These instructions multiply a 2x8 matrix (samples) by an 8x2 matrix
-  // (filter), destructively accumulating into the destination register.
-  int32x4_t sum0123 = vusmmlaq_s32(round_offset, permuted_samples[0], x_filter);
-  int32x4_t sum4567 = vusmmlaq_s32(round_offset, permuted_samples[1], x_filter);
+    do {
+      uint8x16_t s0, s1, s2, s3;
+      load_u8_16x4(s, src_stride, &s0, &s1, &s2, &s3);
 
-  // Narrow and re-pack.
-  // We halved the convolution filter values so -1 from the right shift.
-  int16x8_t res = vcombine_s16(vshrn_n_s32(sum0123, ROUND0_BITS - 1),
-                               vshrn_n_s32(sum4567, ROUND0_BITS - 1));
-  return vreinterpretq_u16_s16(res);
+      uint16x8_t d0 =
+          convolve8_8_x(s0, x_filter, permute_tbl, round_offset_shim);
+      uint16x8_t d1 =
+          convolve8_8_x(s1, x_filter, permute_tbl, round_offset_shim);
+      uint16x8_t d2 =
+          convolve8_8_x(s2, x_filter, permute_tbl, round_offset_shim);
+      uint16x8_t d3 =
+          convolve8_8_x(s3, x_filter, permute_tbl, round_offset_shim);
+
+      uint16x8_t dd0, dd1, dd2, dd3;
+      load_u16_8x4(d, dst_stride, &dd0, &dd1, &dd2, &dd3);
+
+      uint8x8_t d0_u8, d1_u8, d2_u8, d3_u8;
+      compute_dist_wtd_avg_8x4(dd0, dd1, dd2, dd3, d0, d1, d2, d3, fwd_offset,
+                               bck_offset, round_offset_vec, &d0_u8, &d1_u8,
+                               &d2_u8, &d3_u8);
+
+      store_u8_8x4(d_u8, dst8_stride, d0_u8, d1_u8, d2_u8, d3_u8);
+
+      s += 8;
+      d += 8;
+      d_u8 += 8;
+      width -= 8;
+    } while (width != 0);
+    src += 4 * src_stride;
+    dst += 4 * dst_stride;
+    dst8 += 4 * dst8_stride;
+    h -= 4;
+  } while (h != 0);
 }
 
 static inline void dist_wtd_convolve_x_avg_6tap_neon_i8mm(
@@ -820,14 +851,23 @@ void av1_dist_wtd_convolve_x_neon_i8mm(
   const int filter_taps =
       get_filter_tap(filter_params_x, subpel_x_qn & SUBPEL_MASK);
 
+  src -= (SUBPEL_TAPS / 2 - 1);
+
   if (conv_params->do_average) {
     if (UNLIKELY(conv_params->use_dist_wtd_comp_avg)) {
-      dist_wtd_convolve_x_dist_wtd_avg_neon_i8mm(
-          src, src_stride, dst8, dst8_stride, w, h, filter_params_x,
-          subpel_x_qn, conv_params);
-    } else {
-      src -= (SUBPEL_TAPS / 2 - 1);
+      if (filter_taps < 8) {
+        dist_wtd_convolve_x_dist_wtd_avg_6tap_neon_i8mm(
+            src + 1, src_stride, conv_params->dst, conv_params->dst_stride,
+            dst8, dst8_stride, w, h, x_filter_ptr, conv_params->fwd_offset,
+            conv_params->bck_offset);
+        return;
+      }
 
+      dist_wtd_convolve_x_dist_wtd_avg_8tap_neon_i8mm(
+          src, src_stride, conv_params->dst, conv_params->dst_stride, dst8,
+          dst8_stride, w, h, x_filter_ptr, conv_params->fwd_offset,
+          conv_params->bck_offset);
+    } else {
       if (filter_taps < 8) {
         dist_wtd_convolve_x_avg_6tap_neon_i8mm(
             src + 1, src_stride, conv_params->dst, conv_params->dst_stride,
@@ -840,8 +880,6 @@ void av1_dist_wtd_convolve_x_neon_i8mm(
                                              dst8_stride, w, h, x_filter_ptr);
     }
   } else {
-    src -= (SUBPEL_TAPS / 2 - 1);
-
     if (filter_taps < 8) {
       dist_wtd_convolve_x_6tap_neon_i8mm(src + 1, src_stride, conv_params->dst,
                                          conv_params->dst_stride, w, h,