aom: Add 4-tap path for av1_convolve_2d_horiz_sr_neon_dotprod

From 99a75dfcf8ff48d4f06875e208bc9c8681d6fac0 Mon Sep 17 00:00:00 2001
From: Salome Thirot <[EMAIL REDACTED]>
Date: Thu, 25 Apr 2024 16:50:46 +0100
Subject: [PATCH] Add 4-tap path for av1_convolve_2d_horiz_sr_neon_dotprod

Add 4-tap specialization for the horizontal pass of
av1_convolve_2d_sr_neon_dotprod. This gives up to 10% uplift over using
the 8-tap path.

Change-Id: I49f674b8e525b7f4de8fc3ff5073f0dd30981b49
---
 av1/common/arm/convolve_neon_dotprod.c | 267 ++++++++++++++++---------
 1 file changed, 174 insertions(+), 93 deletions(-)

diff --git a/av1/common/arm/convolve_neon_dotprod.c b/av1/common/arm/convolve_neon_dotprod.c
index 393f2e81f..3c85f3cb4 100644
--- a/av1/common/arm/convolve_neon_dotprod.c
+++ b/av1/common/arm/convolve_neon_dotprod.c
@@ -1162,26 +1162,141 @@ static INLINE void convolve_2d_sr_horiz_12tap_neon_dotprod(
   }
 }
 
-static INLINE int16x4_t convolve4_4_2d_h(uint8x16_t samples,
+static INLINE int16x4_t convolve4_4_2d_h(const uint8x16_t samples,
                                          const int8x8_t filters,
-                                         const int32x4_t correction,
-                                         const uint8x16_t range_limit,
-                                         const uint8x16_t permute_tbl) {
-  // Clamp sample range to [-128, 127] for 8-bit signed dot product.
-  int8x16_t clamped_samples =
-      vreinterpretq_s8_u8(vsubq_u8(samples, range_limit));
+                                         const uint8x16_t permute_tbl,
+                                         const int32x4_t correction) {
+  // Transform sample range to [-128, 127] for 8-bit signed dot product.
+  int8x16_t samples_128 =
+      vreinterpretq_s8_u8(vsubq_u8(samples, vdupq_n_u8(128)));
 
   // Permute samples ready for dot product.
   // { 0,  1,  2,  3,  1,  2,  3,  4,  2,  3,  4,  5,  3,  4,  5,  6 }
-  int8x16_t permuted_samples = vqtbl1q_s8(clamped_samples, permute_tbl);
+  int8x16_t perm_samples = vqtbl1q_s8(samples_128, permute_tbl);
 
-  // Accumulate dot product into 'correction' to account for range clamp.
-  int32x4_t sum = vdotq_lane_s32(correction, permuted_samples, filters, 0);
+  // Accumulate into 'correction' to account for range transform.
+  int32x4_t sum = vdotq_lane_s32(correction, perm_samples, filters, 0);
 
   // We halved the convolution filter values so -1 from the right shift.
   return vshrn_n_s32(sum, ROUND0_BITS - 1);
 }
 
+static INLINE int16x8_t convolve4_8_2d_h(const uint8x16_t samples,
+                                         const int8x8_t filters,
+                                         const uint8x16x2_t permute_tbl,
+                                         const int32x4_t correction) {
+  // Transform sample range to [-128, 127] for 8-bit signed dot product.
+  int8x16_t samples_128 =
+      vreinterpretq_s8_u8(vsubq_u8(samples, vdupq_n_u8(128)));
+
+  // Permute samples ready for dot product.
+  // { 0,  1,  2,  3,  1,  2,  3,  4,  2,  3,  4,  5,  3,  4,  5,  6 }
+  // { 4,  5,  6,  7,  5,  6,  7,  8,  6,  7,  8,  9,  7,  8,  9, 10 }
+  int8x16_t perm_samples[2] = { vqtbl1q_s8(samples_128, permute_tbl.val[0]),
+                                vqtbl1q_s8(samples_128, permute_tbl.val[1]) };
+
+  // Accumulate into 'correction' to account for range transform.
+  int32x4_t sum0123 = vdotq_lane_s32(correction, perm_samples[0], filters, 0);
+  int32x4_t sum4567 = vdotq_lane_s32(correction, perm_samples[1], filters, 0);
+
+  // Narrow and re-pack.
+  // We halved the filter values so -1 from right shift.
+  return vcombine_s16(vshrn_n_s32(sum0123, ROUND0_BITS - 1),
+                      vshrn_n_s32(sum4567, ROUND0_BITS - 1));
+}
+
+static INLINE void convolve_2d_sr_horiz_4tap_neon_dotprod(
+    const uint8_t *src, ptrdiff_t src_stride, int16_t *dst,
+    ptrdiff_t dst_stride, int w, int h, const int16_t *filter_x) {
+  const int bd = 8;
+  const int16x4_t x_filter = vld1_s16(filter_x + 2);
+  // All 4-tap and bilinear filter values are even, so halve them to reduce
+  // intermediate precision requirements.
+  const int8x8_t filter = vshrn_n_s16(vcombine_s16(x_filter, vdup_n_s16(0)), 1);
+
+  // Adding a shim of 1 << (ROUND0_BITS - 1) enables us to use non-rounding
+  // shifts - which are generally faster than rounding shifts on modern CPUs.
+  const int32_t horiz_const =
+      ((1 << (bd + FILTER_BITS - 1)) + (1 << (ROUND0_BITS - 1)));
+  // Accumulate into 128 << FILTER_BITS to account for range transform.
+  // Halve the total because we will halve the filter values.
+  const int32x4_t correction =
+      vdupq_n_s32(((128 << FILTER_BITS) + horiz_const) / 2);
+
+  if (w == 4) {
+    const uint8x16_t permute_tbl = vld1q_u8(kDotProdPermuteTbl);
+
+    do {
+      uint8x16_t s0, s1, s2, s3;
+      load_u8_16x4(src, src_stride, &s0, &s1, &s2, &s3);
+
+      int16x4_t d0 = convolve4_4_2d_h(s0, filter, permute_tbl, correction);
+      int16x4_t d1 = convolve4_4_2d_h(s1, filter, permute_tbl, correction);
+      int16x4_t d2 = convolve4_4_2d_h(s2, filter, permute_tbl, correction);
+      int16x4_t d3 = convolve4_4_2d_h(s3, filter, permute_tbl, correction);
+
+      store_s16_4x4(dst, dst_stride, d0, d1, d2, d3);
+
+      src += 4 * src_stride;
+      dst += 4 * dst_stride;
+      h -= 4;
+    } while (h > 4);
+
+    do {
+      uint8x16_t s0 = vld1q_u8(src);
+      int16x4_t d0 = convolve4_4_2d_h(s0, filter, permute_tbl, correction);
+      vst1_s16(dst, d0);
+
+      src += src_stride;
+      dst += dst_stride;
+    } while (--h != 0);
+  } else {
+    const uint8x16x2_t permute_tbl = vld1q_u8_x2(kDotProdPermuteTbl);
+    do {
+      const uint8_t *s = src;
+      int16_t *d = dst;
+      int width = w;
+
+      do {
+        uint8x16_t s0, s1, s2, s3;
+        load_u8_16x4(s, src_stride, &s0, &s1, &s2, &s3);
+
+        int16x8_t d0 = convolve4_8_2d_h(s0, filter, permute_tbl, correction);
+        int16x8_t d1 = convolve4_8_2d_h(s1, filter, permute_tbl, correction);
+        int16x8_t d2 = convolve4_8_2d_h(s2, filter, permute_tbl, correction);
+        int16x8_t d3 = convolve4_8_2d_h(s3, filter, permute_tbl, correction);
+
+        store_s16_8x4(d, dst_stride, d0, d1, d2, d3);
+
+        s += 8;
+        d += 8;
+        width -= 8;
+      } while (width != 0);
+      src += 4 * src_stride;
+      dst += 4 * dst_stride;
+      h -= 4;
+    } while (h > 4);
+
+    do {
+      const uint8_t *s = src;
+      int16_t *d = dst;
+      int width = w;
+
+      do {
+        uint8x16_t s0 = vld1q_u8(s);
+        int16x8_t d0 = convolve4_8_2d_h(s0, filter, permute_tbl, correction);
+        vst1q_s16(d, d0);
+
+        s += 8;
+        d += 8;
+        width -= 8;
+      } while (width != 0);
+      src += src_stride;
+      dst += dst_stride;
+    } while (--h != 0);
+  }
+}
+
 static INLINE int16x8_t convolve8_8_2d_h(uint8x16_t samples,
                                          const int8x8_t filters,
                                          const int32x4_t correction,
@@ -1215,7 +1330,7 @@ static INLINE int16x8_t convolve8_8_2d_h(uint8x16_t samples,
                       vshrn_n_s32(sum[1], ROUND0_BITS - 1));
 }
 
-static INLINE void convolve_2d_sr_horiz_neon_dotprod(
+static INLINE void convolve_2d_sr_horiz_8tap_neon_dotprod(
     const uint8_t *src, int src_stride, int16_t *im_block, int im_stride, int w,
     int im_h, const int16_t *x_filter_ptr) {
   const int bd = 8;
@@ -1235,97 +1350,57 @@ static INLINE void convolve_2d_sr_horiz_neon_dotprod(
   int dst_stride = im_stride;
   int height = im_h;
 
-  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);
+  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(x_filter_s16, 1);
 
-    src_ptr += 2;
+  do {
+    const uint8_t *s = src_ptr;
+    int16_t *d = dst_ptr;
+    int width = w;
 
     do {
       uint8x16_t s0, s1, s2, s3;
-      load_u8_16x4(src_ptr, src_stride, &s0, &s1, &s2, &s3);
-
-      int16x4_t d0 =
-          convolve4_4_2d_h(s0, x_filter, correction, range_limit, permute_tbl);
-      int16x4_t d1 =
-          convolve4_4_2d_h(s1, x_filter, correction, range_limit, permute_tbl);
-      int16x4_t d2 =
-          convolve4_4_2d_h(s2, x_filter, correction, range_limit, permute_tbl);
-      int16x4_t d3 =
-          convolve4_4_2d_h(s3, x_filter, correction, range_limit, permute_tbl);
-
-      store_s16_4x4(dst_ptr, dst_stride, d0, d1, d2, d3);
-
-      src_ptr += 4 * src_stride;
-      dst_ptr += 4 * dst_stride;
-      height -= 4;
-    } while (height > 4);
-
-    do {
-      uint8x16_t s0 = vld1q_u8(src_ptr);
-      int16x4_t d0 =
-          convolve4_4_2d_h(s0, x_filter, correction, range_limit, permute_tbl);
-      vst1_s16(dst_ptr, d0);
-
-      src_ptr += src_stride;
-      dst_ptr += dst_stride;
-    } while (--height != 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(x_filter_s16, 1);
-
-    do {
-      const uint8_t *s = src_ptr;
-      int16_t *d = dst_ptr;
-      int width = w;
+      load_u8_16x4(s, src_stride, &s0, &s1, &s2, &s3);
 
-      do {
-        uint8x16_t s0, s1, s2, s3;
-        load_u8_16x4(s, src_stride, &s0, &s1, &s2, &s3);
+      int16x8_t d0 =
+          convolve8_8_2d_h(s0, x_filter, correction, range_limit, permute_tbl);
+      int16x8_t d1 =
+          convolve8_8_2d_h(s1, x_filter, correction, range_limit, permute_tbl);
+      int16x8_t d2 =
+          convolve8_8_2d_h(s2, x_filter, correction, range_limit, permute_tbl);
+      int16x8_t d3 =
+          convolve8_8_2d_h(s3, x_filter, correction, range_limit, permute_tbl);
 
-        int16x8_t d0 = convolve8_8_2d_h(s0, x_filter, correction, range_limit,
-                                        permute_tbl);
-        int16x8_t d1 = convolve8_8_2d_h(s1, x_filter, correction, range_limit,
-                                        permute_tbl);
-        int16x8_t d2 = convolve8_8_2d_h(s2, x_filter, correction, range_limit,
-                                        permute_tbl);
-        int16x8_t d3 = convolve8_8_2d_h(s3, x_filter, correction, range_limit,
-                                        permute_tbl);
+      store_s16_8x4(d, dst_stride, d0, d1, d2, d3);
 
-        store_s16_8x4(d, dst_stride, d0, d1, d2, d3);
+      s += 8;
+      d += 8;
+      width -= 8;
+    } while (width != 0);
+    src_ptr += 4 * src_stride;
+    dst_ptr += 4 * dst_stride;
+    height -= 4;
+  } while (height > 4);
 
-        s += 8;
-        d += 8;
-        width -= 8;
-      } while (width != 0);
-      src_ptr += 4 * src_stride;
-      dst_ptr += 4 * dst_stride;
-      height -= 4;
-    } while (height > 4);
+  do {
+    const uint8_t *s = src_ptr;
+    int16_t *d = dst_ptr;
+    int width = w;
 
     do {
-      const uint8_t *s = src_ptr;
-      int16_t *d = dst_ptr;
-      int width = w;
-
-      do {
-        uint8x16_t s0 = vld1q_u8(s);
-        int16x8_t d0 = convolve8_8_2d_h(s0, x_filter, correction, range_limit,
-                                        permute_tbl);
-        vst1q_s16(d, d0);
+      uint8x16_t s0 = vld1q_u8(s);
+      int16x8_t d0 =
+          convolve8_8_2d_h(s0, x_filter, correction, range_limit, permute_tbl);
+      vst1q_s16(d, d0);
 
-        s += 8;
-        d += 8;
-        width -= 8;
-      } while (width != 0);
-      src_ptr += src_stride;
-      dst_ptr += dst_stride;
-    } while (--height != 0);
-  }
+      s += 8;
+      d += 8;
+      width -= 8;
+    } while (width != 0);
+    src_ptr += src_stride;
+    dst_ptr += dst_stride;
+  } while (--height != 0);
 }
 
 void av1_convolve_2d_sr_neon_dotprod(const uint8_t *src, int src_stride,
@@ -1343,6 +1418,7 @@ void av1_convolve_2d_sr_neon_dotprod(const uint8_t *src, int src_stride,
   }
 
   const int y_filter_taps = get_filter_tap(filter_params_y, subpel_y_qn);
+  const int x_filter_taps = get_filter_tap(filter_params_x, subpel_x_qn);
   const int clamped_y_taps = y_filter_taps < 6 ? 6 : y_filter_taps;
   const int im_h = h + clamped_y_taps - 1;
   const int im_stride = MAX_SB_SIZE;
@@ -1374,8 +1450,13 @@ void av1_convolve_2d_sr_neon_dotprod(const uint8_t *src, int src_stride,
     DECLARE_ALIGNED(16, int16_t,
                     im_block[(MAX_SB_SIZE + SUBPEL_TAPS - 1) * MAX_SB_SIZE]);
 
-    convolve_2d_sr_horiz_neon_dotprod(src_ptr, src_stride, im_block, im_stride,
-                                      w, im_h, x_filter_ptr);
+    if (x_filter_taps <= 4) {
+      convolve_2d_sr_horiz_4tap_neon_dotprod(src_ptr + 2, src_stride, im_block,
+                                             im_stride, w, im_h, x_filter_ptr);
+    } else {
+      convolve_2d_sr_horiz_8tap_neon_dotprod(src_ptr, src_stride, im_block,
+                                             im_stride, w, im_h, x_filter_ptr);
+    }
 
     const int16x8_t y_filter = vld1q_s16(y_filter_ptr);