aom: Add SVE2 implementation of HBD convolve_2d_sr for 4-tap filters

From bc4379ce937053c9a1d9dc5839002cfe978bc22a Mon Sep 17 00:00:00 2001
From: Salome Thirot <[EMAIL REDACTED]>
Date: Wed, 28 Feb 2024 17:01:49 +0000
Subject: [PATCH] Add SVE2 implementation of HBD convolve_2d_sr for 4-tap
 filters

Add SVE2 implementation of av1_highbd_convolve_2d_sr for 4-tap filters.

Change-Id: I032605fe3b18e09f7b2e22066604fefc8388148e
---
 av1/common/arm/highbd_convolve_sve2.c | 273 +++++++++++++++++++++++++-
 1 file changed, 263 insertions(+), 10 deletions(-)

diff --git a/av1/common/arm/highbd_convolve_sve2.c b/av1/common/arm/highbd_convolve_sve2.c
index acd9d798a..1cb1086b5 100644
--- a/av1/common/arm/highbd_convolve_sve2.c
+++ b/av1/common/arm/highbd_convolve_sve2.c
@@ -1162,6 +1162,105 @@ static INLINE void highbd_convolve_2d_sr_horiz_8tap_sve2(
   } while (height > 0);
 }
 
+static INLINE uint16x4_t convolve4_4_2d_h(int16x8_t s0, int16x8_t filter,
+                                          int64x2_t offset, int32x4_t shift,
+                                          uint16x8x2_t permute_tbl) {
+  int16x8_t permuted_samples0 = aom_tbl_s16(s0, permute_tbl.val[0]);
+  int16x8_t permuted_samples1 = aom_tbl_s16(s0, permute_tbl.val[1]);
+
+  int64x2_t sum01 = aom_svdot_lane_s16(offset, permuted_samples0, filter, 0);
+  int64x2_t sum23 = aom_svdot_lane_s16(offset, permuted_samples1, filter, 0);
+
+  int32x4_t sum0123 = vcombine_s32(vmovn_s64(sum01), vmovn_s64(sum23));
+  sum0123 = vqrshlq_s32(sum0123, shift);
+  return vqmovun_s32(sum0123);
+}
+
+static INLINE uint16x8_t convolve4_8_2d_h(int16x8_t s0[8], int16x8_t filter,
+                                          int64x2_t offset, int32x4_t shift,
+                                          uint16x8_t tbl) {
+  int64x2_t sum04 = aom_svdot_lane_s16(offset, s0[0], filter, 0);
+  int64x2_t sum15 = aom_svdot_lane_s16(offset, s0[1], filter, 0);
+  int64x2_t sum26 = aom_svdot_lane_s16(offset, s0[2], filter, 0);
+  int64x2_t sum37 = aom_svdot_lane_s16(offset, s0[3], filter, 0);
+
+  int32x4_t sum0123 = vcombine_s32(vmovn_s64(sum04), vmovn_s64(sum15));
+  int32x4_t sum4567 = vcombine_s32(vmovn_s64(sum26), vmovn_s64(sum37));
+
+  sum0123 = vqrshlq_s32(sum0123, shift);
+  sum4567 = vqrshlq_s32(sum4567, shift);
+
+  uint16x8_t res = vcombine_u16(vqmovun_s32(sum0123), vqmovun_s32(sum4567));
+  return aom_tbl_u16(res, tbl);
+}
+
+static INLINE void highbd_convolve_2d_sr_horiz_4tap_sve2(
+    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride,
+    int width, int height, const int16_t *x_filter_ptr,
+    ConvolveParams *conv_params, const int x_offset) {
+  const int64x2_t offset = vdupq_n_s64(x_offset);
+  const int32x4_t shift = vdupq_n_s32(-conv_params->round_0);
+
+  const int16x4_t x_filter = vld1_s16(x_filter_ptr + 2);
+  const int16x8_t filter = vcombine_s16(x_filter, vdup_n_s16(0));
+
+  if (width == 4) {
+    const int16_t *s = (const int16_t *)(src);
+
+    uint16x8x2_t permute_tbl = vld1q_u16_x2(kDotProdTbl);
+
+    do {
+      int16x8_t s0, s1, s2, s3;
+      load_s16_8x4(s, src_stride, &s0, &s1, &s2, &s3);
+
+      uint16x4_t d0 = convolve4_4_2d_h(s0, filter, offset, shift, permute_tbl);
+      uint16x4_t d1 = convolve4_4_2d_h(s1, filter, offset, shift, permute_tbl);
+      uint16x4_t d2 = convolve4_4_2d_h(s2, filter, offset, shift, permute_tbl);
+      uint16x4_t d3 = convolve4_4_2d_h(s3, filter, offset, shift, permute_tbl);
+
+      store_u16_4x4(dst, dst_stride, d0, d1, d2, d3);
+
+      s += 4 * src_stride;
+      dst += 4 * dst_stride;
+      height -= 4;
+    } while (height > 0);
+  } else {
+    uint16x8_t idx = vld1q_u16(kDeinterleaveTbl);
+
+    do {
+      const int16_t *s = (const int16_t *)(src);
+      uint16_t *d = dst;
+      int w = width;
+
+      do {
+        int16x8_t s0[8], s1[8], s2[8], s3[8];
+        load_s16_8x8(s + 0 * src_stride, 1, &s0[0], &s0[1], &s0[2], &s0[3],
+                     &s0[4], &s0[5], &s0[6], &s0[7]);
+        load_s16_8x8(s + 1 * src_stride, 1, &s1[0], &s1[1], &s1[2], &s1[3],
+                     &s1[4], &s1[5], &s1[6], &s1[7]);
+        load_s16_8x8(s + 2 * src_stride, 1, &s2[0], &s2[1], &s2[2], &s2[3],
+                     &s2[4], &s2[5], &s2[6], &s2[7]);
+        load_s16_8x8(s + 3 * src_stride, 1, &s3[0], &s3[1], &s3[2], &s3[3],
+                     &s3[4], &s3[5], &s3[6], &s3[7]);
+
+        uint16x8_t d0 = convolve4_8_2d_h(s0, filter, offset, shift, idx);
+        uint16x8_t d1 = convolve4_8_2d_h(s1, filter, offset, shift, idx);
+        uint16x8_t d2 = convolve4_8_2d_h(s2, filter, offset, shift, idx);
+        uint16x8_t d3 = convolve4_8_2d_h(s3, filter, offset, shift, idx);
+
+        store_u16_8x4(d, dst_stride, d0, d1, d2, d3);
+
+        s += 8;
+        d += 8;
+        w -= 8;
+      } while (w != 0);
+      src += 4 * src_stride;
+      dst += 4 * dst_stride;
+      height -= 4;
+    } while (height > 0);
+  }
+}
+
 static INLINE uint16x4_t highbd_convolve12_4_2d_v(
     int16x8_t s0[2], int16x8_t s1[2], int16x8_t s2[2], int16x8_t filter_0_7,
     int16x8_t filter_4_11, int32x4_t shift, int64x2_t offset, uint16x4_t max) {
@@ -1480,6 +1579,145 @@ void highbd_convolve_2d_sr_vert_8tap_sve2(const uint16_t *src,
   }
 }
 
+static INLINE uint16x4_t highbd_convolve4_4_2d_v(int16x8_t samples[2],
+                                                 int16x8_t filter,
+                                                 int32x4_t shift,
+                                                 int64x2_t offset,
+                                                 uint16x4_t max) {
+  int64x2_t sum01 = aom_svdot_lane_s16(offset, samples[0], filter, 0);
+  int64x2_t sum23 = aom_svdot_lane_s16(offset, samples[1], filter, 0);
+
+  int32x4_t sum0123 = vcombine_s32(vmovn_s64(sum01), vmovn_s64(sum23));
+  sum0123 = vshlq_s32(sum0123, shift);
+
+  uint16x4_t res = vqmovun_s32(sum0123);
+  return vmin_u16(res, max);
+}
+
+static INLINE uint16x8_t highbd_convolve4_8_2d_v(int16x8_t samples[4],
+                                                 int16x8_t filter,
+                                                 int32x4_t shift,
+                                                 int64x2_t offset,
+                                                 uint16x8_t max) {
+  int64x2_t sum01 = aom_svdot_lane_s16(offset, samples[0], filter, 0);
+  int64x2_t sum23 = aom_svdot_lane_s16(offset, samples[1], filter, 0);
+  int64x2_t sum45 = aom_svdot_lane_s16(offset, samples[2], filter, 0);
+  int64x2_t sum67 = aom_svdot_lane_s16(offset, samples[3], filter, 0);
+
+  int32x4_t sum0123 = vcombine_s32(vmovn_s64(sum01), vmovn_s64(sum23));
+  int32x4_t sum4567 = vcombine_s32(vmovn_s64(sum45), vmovn_s64(sum67));
+
+  sum0123 = vshlq_s32(sum0123, shift);
+  sum4567 = vshlq_s32(sum4567, shift);
+
+  uint16x8_t res = vcombine_u16(vqmovun_s32(sum0123), vqmovun_s32(sum4567));
+  return vminq_u16(res, max);
+}
+
+void highbd_convolve_2d_sr_vert_4tap_sve2(const uint16_t *src,
+                                          ptrdiff_t src_stride, uint16_t *dst,
+                                          ptrdiff_t dst_stride, int width,
+                                          int height, const int16_t *filter_y,
+                                          ConvolveParams *conv_params, int bd,
+                                          const int y_offset) {
+  assert(w >= 4 && h >= 4);
+  const int64x2_t offset = vdupq_n_s64(y_offset);
+  const int32x4_t shift = vdupq_n_s32(-conv_params->round_1);
+
+  const int16x8_t y_filter =
+      vcombine_s16(vld1_s16(filter_y + 2), vdup_n_s16(0));
+
+  if (width == 4) {
+    const uint16x4_t max = vdup_n_u16((1 << bd) - 1);
+    int16_t *s = (int16_t *)(src);
+
+    int16x4_t s0, s1, s2;
+    load_s16_4x3(s, src_stride, &s0, &s1, &s2);
+    s += 3 * src_stride;
+
+    do {
+      int16x4_t s3, s4, s5, s6;
+      load_s16_4x4(s, src_stride, &s3, &s4, &s5, &s6);
+
+      // This operation combines a conventional transpose and the sample permute
+      // required before computing the dot product.
+      int16x8_t s0123[2], s1234[2], s2345[2], s3456[2];
+      transpose_concat_4x4(s0, s1, s2, s3, s0123);
+      transpose_concat_4x4(s1, s2, s3, s4, s1234);
+      transpose_concat_4x4(s2, s3, s4, s5, s2345);
+      transpose_concat_4x4(s3, s4, s5, s6, s3456);
+
+      uint16x4_t d0 =
+          highbd_convolve4_4_2d_v(s0123, y_filter, shift, offset, max);
+      uint16x4_t d1 =
+          highbd_convolve4_4_2d_v(s1234, y_filter, shift, offset, max);
+      uint16x4_t d2 =
+          highbd_convolve4_4_2d_v(s2345, y_filter, shift, offset, max);
+      uint16x4_t d3 =
+          highbd_convolve4_4_2d_v(s3456, y_filter, shift, offset, max);
+
+      store_u16_4x4(dst, dst_stride, d0, d1, d2, d3);
+
+      // Shuffle everything up four rows.
+      s0 = s4;
+      s1 = s5;
+      s2 = s6;
+
+      s += 4 * src_stride;
+      dst += 4 * dst_stride;
+      height -= 4;
+    } while (height != 0);
+  } else {
+    const uint16x8_t max = vdupq_n_u16((1 << bd) - 1);
+
+    do {
+      int h = height;
+      int16_t *s = (int16_t *)(src);
+      uint16_t *d = dst;
+
+      int16x8_t s0, s1, s2;
+      load_s16_8x3(s, src_stride, &s0, &s1, &s2);
+      s += 3 * src_stride;
+
+      do {
+        int16x8_t s3, s4, s5, s6;
+        load_s16_8x4(s, src_stride, &s3, &s4, &s5, &s6);
+
+        // This operation combines a conventional transpose and the sample
+        // permute required before computing the dot product.
+        int16x8_t s0123[4], s1234[4], s2345[4], s3456[4];
+        transpose_concat_8x4(s0, s1, s2, s3, s0123);
+        transpose_concat_8x4(s1, s2, s3, s4, s1234);
+        transpose_concat_8x4(s2, s3, s4, s5, s2345);
+        transpose_concat_8x4(s3, s4, s5, s6, s3456);
+
+        uint16x8_t d0 =
+            highbd_convolve4_8_2d_v(s0123, y_filter, shift, offset, max);
+        uint16x8_t d1 =
+            highbd_convolve4_8_2d_v(s1234, y_filter, shift, offset, max);
+        uint16x8_t d2 =
+            highbd_convolve4_8_2d_v(s2345, y_filter, shift, offset, max);
+        uint16x8_t d3 =
+            highbd_convolve4_8_2d_v(s3456, y_filter, shift, offset, max);
+
+        store_u16_8x4(d, dst_stride, d0, d1, d2, d3);
+
+        // Shuffle everything up four rows.
+        s0 = s4;
+        s1 = s5;
+        s2 = s6;
+
+        s += 4 * src_stride;
+        d += 4 * dst_stride;
+        h -= 4;
+      } while (h != 0);
+      src += 8;
+      dst += 8;
+      width -= 8;
+    } while (width != 0);
+  }
+}
+
 void av1_highbd_convolve_2d_sr_sve2(const uint16_t *src, int src_stride,
                                     uint16_t *dst, int dst_stride, int w, int h,
                                     const InterpFilterParams *filter_params_x,
@@ -1499,16 +1737,19 @@ void av1_highbd_convolve_2d_sr_sve2(const uint16_t *src, int src_stride,
   const int x_filter_taps = get_filter_tap(filter_params_x, subpel_x_qn);
   const int y_filter_taps = get_filter_tap(filter_params_y, subpel_y_qn);
 
-  if (x_filter_taps <= 6 || y_filter_taps <= 6) {
+  if (x_filter_taps == 6 || y_filter_taps == 6) {
     av1_highbd_convolve_2d_sr_neon(src, src_stride, dst, dst_stride, w, h,
                                    filter_params_x, filter_params_y,
                                    subpel_x_qn, subpel_y_qn, conv_params, bd);
     return;
   }
 
+  const int clamped_x_taps = x_filter_taps < 4 ? 4 : x_filter_taps;
+  const int clamped_y_taps = y_filter_taps < 4 ? 4 : y_filter_taps;
+
   const int im_stride = MAX_SB_SIZE;
-  const int vert_offset = filter_params_y->taps / 2 - 1;
-  const int horiz_offset = filter_params_x->taps / 2 - 1;
+  const int vert_offset = clamped_y_taps / 2 - 1;
+  const int horiz_offset = clamped_x_taps / 2 - 1;
   const int x_offset = (1 << (bd + FILTER_BITS - 1));
   const int y_offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
   // The extra shim of (1 << (conv_params->round_1 - 1)) allows us to do a
@@ -1522,7 +1763,7 @@ void av1_highbd_convolve_2d_sr_sve2(const uint16_t *src, int src_stride,
       filter_params_x, subpel_x_qn & SUBPEL_MASK);
   const int16_t *y_filter_ptr = av1_get_interp_filter_subpel_kernel(
       filter_params_y, subpel_y_qn & SUBPEL_MASK);
-  const int im_h = h + y_filter_taps - 1;
+  const int im_h = h + clamped_y_taps - 1;
 
   if (x_filter_taps > 8) {
     highbd_convolve_2d_sr_horiz_12tap_sve2(src_ptr, src_stride, im_block,
@@ -1535,11 +1776,23 @@ void av1_highbd_convolve_2d_sr_sve2(const uint16_t *src, int src_stride,
     return;
   }
 
-  highbd_convolve_2d_sr_horiz_8tap_sve2(src_ptr, src_stride, im_block,
-                                        im_stride, w, im_h, x_filter_ptr,
-                                        conv_params, x_offset);
+  if (x_filter_taps <= 4) {
+    highbd_convolve_2d_sr_horiz_4tap_sve2(src_ptr, src_stride, im_block,
+                                          im_stride, w, im_h, x_filter_ptr,
+                                          conv_params, x_offset);
+  } else {
+    highbd_convolve_2d_sr_horiz_8tap_sve2(src_ptr, src_stride, im_block,
+                                          im_stride, w, im_h, x_filter_ptr,
+                                          conv_params, x_offset);
+  }
 
-  highbd_convolve_2d_sr_vert_8tap_sve2(im_block, im_stride, dst, dst_stride, w,
-                                       h, y_filter_ptr, conv_params, bd,
-                                       y_offset);
+  if (y_filter_taps <= 4) {
+    highbd_convolve_2d_sr_vert_4tap_sve2(im_block, im_stride, dst, dst_stride,
+                                         w, h, y_filter_ptr, conv_params, bd,
+                                         y_offset);
+  } else {
+    highbd_convolve_2d_sr_vert_8tap_sve2(im_block, im_stride, dst, dst_stride,
+                                         w, h, y_filter_ptr, conv_params, bd,
+                                         y_offset);
+  }
 }