From a62c1833e0e0683069b1924f4e2efc11c94aad0f Mon Sep 17 00:00:00 2001
From: Jonathan Wright <[EMAIL REDACTED]>
Date: Fri, 2 Aug 2024 09:06:28 +0100
Subject: [PATCH] Add Arm Neon USMMLA impl. for horiz. 12-tap convolve_2d_sr
Split the 12-tap filter into two 6-tap filters to enable a
convolution kernel implementation using the Armv8.6 USMMLA matrix
multiply instructions. These 2x8 by 8x2 matrix multiply instructions
do twice the work of a USDOT dot product instruction.
Change-Id: Ibdc612f2543b8c3ca93c1fd2c23cb84285e233e8
---
av1/common/arm/convolve_neon_i8mm.c | 12 +--
av1/common/arm/convolve_neon_i8mm.h | 136 +++++++++++++++-------------
av1/common/arm/convolve_sve2.c | 5 +-
3 files changed, 76 insertions(+), 77 deletions(-)
diff --git a/av1/common/arm/convolve_neon_i8mm.c b/av1/common/arm/convolve_neon_i8mm.c
index 464c190780..796d3f709d 100644
--- a/av1/common/arm/convolve_neon_i8mm.c
+++ b/av1/common/arm/convolve_neon_i8mm.c
@@ -31,13 +31,6 @@ DECLARE_ALIGNED(16, static const uint8_t, kDotProdMergeBlockTbl[48]) = {
3, 16, 17, 18, 7, 20, 21, 22, 11, 24, 25, 26, 15, 28, 29, 30
};
-DECLARE_ALIGNED(16, static const uint8_t, kMatMulPermuteTbl[32]) = {
- // clang-format off
- 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
- // clang-format on
-};
-
static INLINE int16x4_t convolve12_4_x(uint8x16_t samples[2],
const int8x16_t filter[2],
const uint8x16_t permute_tbl,
@@ -1289,14 +1282,11 @@ void av1_convolve_2d_sr_neon_i8mm(const uint8_t *src, int src_stride,
DECLARE_ALIGNED(16, int16_t,
im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE]);
- const int16x8_t x_filter_0_7 = vld1q_s16(x_filter_ptr);
- const int16x4_t x_filter_8_11 = vld1_s16(x_filter_ptr + 8);
const int16x8_t y_filter_0_7 = vld1q_s16(y_filter_ptr);
const int16x4_t y_filter_8_11 = vld1_s16(y_filter_ptr + 8);
convolve_2d_sr_horiz_12tap_neon_i8mm(src_ptr, src_stride, im_block,
- im_stride, w, im_h, x_filter_0_7,
- x_filter_8_11);
+ im_stride, w, im_h, x_filter_ptr);
convolve_2d_sr_vert_12tap_neon(im_block, im_stride, dst, dst_stride, w, h,
y_filter_0_7, y_filter_8_11);
diff --git a/av1/common/arm/convolve_neon_i8mm.h b/av1/common/arm/convolve_neon_i8mm.h
index ddd8364ea5..fcbdd2bf5c 100644
--- a/av1/common/arm/convolve_neon_i8mm.h
+++ b/av1/common/arm/convolve_neon_i8mm.h
@@ -29,49 +29,52 @@ DECLARE_ALIGNED(16, static const uint8_t, kDotProdPermuteTbl[48]) = {
8, 9, 10, 11, 9, 10, 11, 12, 10, 11, 12, 13, 11, 12, 13, 14
};
-static INLINE int16x4_t convolve12_4_2d_h(uint8x16_t samples,
- const int8x16_t filters,
- const uint8x16x3_t permute_tbl,
+DECLARE_ALIGNED(16, static const uint8_t, kMatMulPermuteTbl[32]) = {
+ // clang-format off
+ 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
+ // clang-format on
+};
+
+static INLINE int16x4_t convolve12_4_2d_h(uint8x16_t samples[2],
+ const int8x16_t filter[2],
+ const uint8x16_t permute_tbl,
int32x4_t horiz_const) {
- // 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 }
- // { 8, 9, 10, 11, 9, 10, 11, 12, 10, 11, 12, 13, 11, 12, 13, 14 }
- uint8x16_t perm_samples[3] = { vqtbl1q_u8(samples, permute_tbl.val[0]),
- vqtbl1q_u8(samples, permute_tbl.val[1]),
- vqtbl1q_u8(samples, permute_tbl.val[2]) };
-
- int32x4_t sum = vusdotq_laneq_s32(horiz_const, perm_samples[0], filters, 0);
- sum = vusdotq_laneq_s32(sum, perm_samples[1], filters, 1);
- sum = vusdotq_laneq_s32(sum, perm_samples[2], filters, 2);
+ // 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 perm_samples[2] = { vqtbl1q_u8(samples[0], permute_tbl),
+ vqtbl1q_u8(samples[1], permute_tbl) };
+
+ // These instructions multiply a 2x8 matrix (samples) by an 8x2 matrix
+ // (filter), destructively accumulating into the destination register.
+ int32x4_t sum = vusmmlaq_s32(horiz_const, perm_samples[0], filter[0]);
+ sum = vusmmlaq_s32(sum, perm_samples[1], filter[1]);
// Narrow and re-pack.
return vshrn_n_s32(sum, ROUND0_BITS);
}
static INLINE int16x8_t convolve12_8_2d_h(uint8x16_t samples[2],
- const int8x16_t filters,
- const uint8x16x3_t permute_tbl,
+ const int8x16_t filter[2],
+ const uint8x16x2_t permute_tbl,
const int32x4_t horiz_const) {
- // 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 }
- // { 8, 9, 10, 11, 9, 10, 11, 12, 10, 11, 12, 13, 11, 12, 13, 14 }
- // {12, 13, 14, 15, 13, 14, 15, 16, 14, 15, 16, 17, 15, 16, 17, 18 }
+ /// 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 }
+ // { 6, 7, 8, 9, 10, 11, 12, 13, 8, 9, 10, 11, 12, 13, 14, 15 }
+ // { 10, 11, 12, 13, 14, 15, 16, 17, 12, 13, 14, 15, 16, 17, 18, 19 }
uint8x16_t perm_samples[4] = { vqtbl1q_u8(samples[0], permute_tbl.val[0]),
vqtbl1q_u8(samples[0], permute_tbl.val[1]),
- vqtbl1q_u8(samples[0], permute_tbl.val[2]),
- vqtbl1q_u8(samples[1], permute_tbl.val[2]) };
-
- int32x4_t sum0123 =
- vusdotq_laneq_s32(horiz_const, perm_samples[0], filters, 0);
- sum0123 = vusdotq_laneq_s32(sum0123, perm_samples[1], filters, 1);
- sum0123 = vusdotq_laneq_s32(sum0123, perm_samples[2], filters, 2);
+ vqtbl1q_u8(samples[1], permute_tbl.val[0]),
+ vqtbl1q_u8(samples[1], permute_tbl.val[1]) };
- int32x4_t sum4567 =
- vusdotq_laneq_s32(horiz_const, perm_samples[1], filters, 0);
- sum4567 = vusdotq_laneq_s32(sum4567, perm_samples[2], filters, 1);
- sum4567 = vusdotq_laneq_s32(sum4567, perm_samples[3], filters, 2);
+ // These instructions multiply a 2x8 matrix (samples) by an 8x2 matrix
+ // (filter), destructively accumulating into the destination register.
+ int32x4_t sum0123 = vusmmlaq_s32(horiz_const, perm_samples[0], filter[0]);
+ int32x4_t sum4567 = vusmmlaq_s32(horiz_const, perm_samples[1], filter[0]);
+ sum0123 = vusmmlaq_s32(sum0123, perm_samples[2], filter[1]);
+ sum4567 = vusmmlaq_s32(sum4567, perm_samples[3], filter[1]);
// Narrow and re-pack.
return vcombine_s16(vshrn_n_s32(sum0123, ROUND0_BITS),
@@ -80,34 +83,44 @@ static INLINE int16x8_t convolve12_8_2d_h(uint8x16_t samples[2],
static INLINE void convolve_2d_sr_horiz_12tap_neon_i8mm(
const uint8_t *src_ptr, int src_stride, int16_t *dst_ptr,
- const int dst_stride, int w, int h, const int16x8_t x_filter_0_7,
- const int16x4_t x_filter_8_11) {
+ const int dst_stride, int w, int h, const int16_t *x_filter_ptr) {
// The no-op filter should never be used here.
- assert(vgetq_lane_s16(x_filter_0_7, 5) != 128);
+ assert(x_filter_ptr[5] != 128);
const int bd = 8;
- // Narrow filter values to 8-bit.
- const int16x8x2_t x_filter_s16 = {
- { x_filter_0_7, vcombine_s16(x_filter_8_11, vdup_n_s16(0)) }
+ // Split 12-tap filter into two 6-tap filters, masking the top two elements.
+ // { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0 }
+ const int8x8_t mask = vcreate_s8(0x0000ffffffffffff);
+ const int8x8_t filter_0 = vand_s8(vmovn_s16(vld1q_s16(x_filter_ptr)), mask);
+ const int8x8_t filter_1 =
+ vext_s8(vmovn_s16(vld1q_s16(x_filter_ptr + 4)), vdup_n_s8(0), 2);
+
+ // Stagger each 6-tap filter to enable use of matrix multiply instructions.
+ // { f0, f1, f2, f3, f4, f5, 0, 0, 0, f0, f1, f2, f3, f4, f5, 0 }
+ const int8x16_t filter[2] = {
+ vcombine_s8(filter_0, vext_s8(filter_0, filter_0, 7)),
+ vcombine_s8(filter_1, vext_s8(filter_1, filter_1, 7))
};
- const int8x16_t x_filter = vcombine_s8(vmovn_s16(x_filter_s16.val[0]),
- vmovn_s16(x_filter_s16.val[1]));
+
// This shim of 1 << (ROUND0_BITS - 1) enables us to use non-rounding shifts
- // - which are generally faster than rounding shifts on modern CPUs.
+ // in convolution kernels - which are generally faster than rounding shifts on
+ // modern CPUs.
const int32x4_t horiz_const =
vdupq_n_s32((1 << (bd + FILTER_BITS - 1)) + (1 << (ROUND0_BITS - 1)));
- const uint8x16x3_t permute_tbl = vld1q_u8_x3(kDotProdPermuteTbl);
if (w <= 4) {
+ 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);
+ uint8x16_t s0[2], s1[2], s2[2], s3[2];
+ load_u8_16x4(src_ptr, src_stride, &s0[0], &s1[0], &s2[0], &s3[0]);
+ load_u8_16x4(src_ptr + 6, src_stride, &s0[1], &s1[1], &s2[1], &s3[1]);
- int16x4_t d0 = convolve12_4_2d_h(s0, x_filter, permute_tbl, horiz_const);
- int16x4_t d1 = convolve12_4_2d_h(s1, x_filter, permute_tbl, horiz_const);
- int16x4_t d2 = convolve12_4_2d_h(s2, x_filter, permute_tbl, horiz_const);
- int16x4_t d3 = convolve12_4_2d_h(s3, x_filter, permute_tbl, horiz_const);
+ int16x4_t d0 = convolve12_4_2d_h(s0, filter, permute_tbl, horiz_const);
+ int16x4_t d1 = convolve12_4_2d_h(s1, filter, permute_tbl, horiz_const);
+ int16x4_t d2 = convolve12_4_2d_h(s2, filter, permute_tbl, horiz_const);
+ int16x4_t d3 = convolve12_4_2d_h(s3, filter, permute_tbl, horiz_const);
store_s16_4x4(dst_ptr, dst_stride, d0, d1, d2, d3);
@@ -117,8 +130,10 @@ static INLINE void convolve_2d_sr_horiz_12tap_neon_i8mm(
} while (h > 4);
do {
- uint8x16_t s0 = vld1q_u8(src_ptr);
- int16x4_t d0 = convolve12_4_2d_h(s0, x_filter, permute_tbl, horiz_const);
+ uint8x16_t s0[2];
+ s0[0] = vld1q_u8(src_ptr);
+ s0[1] = vld1q_u8(src_ptr + 6);
+ int16x4_t d0 = convolve12_4_2d_h(s0, filter, permute_tbl, horiz_const);
vst1_s16(dst_ptr, d0);
src_ptr += src_stride;
@@ -126,6 +141,8 @@ static INLINE void convolve_2d_sr_horiz_12tap_neon_i8mm(
} while (--h != 0);
} else {
+ const uint8x16x2_t permute_tbl = vld1q_u8_x2(kMatMulPermuteTbl);
+
do {
const uint8_t *s = src_ptr;
int16_t *d = dst_ptr;
@@ -134,16 +151,12 @@ static INLINE void convolve_2d_sr_horiz_12tap_neon_i8mm(
do {
uint8x16_t s0[2], s1[2], s2[2], s3[2];
load_u8_16x4(s, src_stride, &s0[0], &s1[0], &s2[0], &s3[0]);
- load_u8_16x4(s + 4, src_stride, &s0[1], &s1[1], &s2[1], &s3[1]);
+ load_u8_16x4(s + 6, src_stride, &s0[1], &s1[1], &s2[1], &s3[1]);
- int16x8_t d0 =
- convolve12_8_2d_h(s0, x_filter, permute_tbl, horiz_const);
- int16x8_t d1 =
- convolve12_8_2d_h(s1, x_filter, permute_tbl, horiz_const);
- int16x8_t d2 =
- convolve12_8_2d_h(s2, x_filter, permute_tbl, horiz_const);
- int16x8_t d3 =
- convolve12_8_2d_h(s3, x_filter, permute_tbl, horiz_const);
+ int16x8_t d0 = convolve12_8_2d_h(s0, filter, permute_tbl, horiz_const);
+ int16x8_t d1 = convolve12_8_2d_h(s1, filter, permute_tbl, horiz_const);
+ int16x8_t d2 = convolve12_8_2d_h(s2, filter, permute_tbl, horiz_const);
+ int16x8_t d3 = convolve12_8_2d_h(s3, filter, permute_tbl, horiz_const);
store_s16_8x4(d, dst_stride, d0, d1, d2, d3);
@@ -165,9 +178,8 @@ static INLINE void convolve_2d_sr_horiz_12tap_neon_i8mm(
do {
uint8x16_t s0[2];
s0[0] = vld1q_u8(s);
- s0[1] = vld1q_u8(s + 4);
- int16x8_t d0 =
- convolve12_8_2d_h(s0, x_filter, permute_tbl, horiz_const);
+ s0[1] = vld1q_u8(s + 6);
+ int16x8_t d0 = convolve12_8_2d_h(s0, filter, permute_tbl, horiz_const);
vst1q_s16(d, d0);
s += 8;
diff --git a/av1/common/arm/convolve_sve2.c b/av1/common/arm/convolve_sve2.c
index 832a3b4e5e..8875f86581 100644
--- a/av1/common/arm/convolve_sve2.c
+++ b/av1/common/arm/convolve_sve2.c
@@ -184,14 +184,11 @@ void av1_convolve_2d_sr_sve2(const uint8_t *src, int src_stride, uint8_t *dst,
DECLARE_ALIGNED(16, int16_t,
im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE]);
- const int16x8_t x_filter_0_7 = vld1q_s16(x_filter_ptr);
- const int16x4_t x_filter_8_11 = vld1_s16(x_filter_ptr + 8);
const int16x8_t y_filter_0_7 = vld1q_s16(y_filter_ptr);
const int16x8_t y_filter_4_11 = vld1q_s16(y_filter_ptr + 4);
convolve_2d_sr_horiz_12tap_neon_i8mm(src_ptr, src_stride, im_block,
- im_stride, w, im_h, x_filter_0_7,
- x_filter_8_11);
+ im_stride, w, im_h, x_filter_ptr);
convolve_2d_sr_vert_12tap_sve2(im_block, im_stride, dst, dst_stride, w, h,
y_filter_0_7, y_filter_4_11);