1 /*
2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3 * Copyright (c) 2023, Alliance for Open Media. All rights reserved
4 *
5 * This source code is subject to the terms of the BSD 2 Clause License and
6 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7 * was not distributed with this source code in the LICENSE file, you can
8 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
9 * Media Patent License 1.0 was not distributed with this source code in the
10 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
11 */
12
13 #include <arm_neon.h>
14 #include <assert.h>
15 #include <string.h>
16
17 #include "config/aom_config.h"
18
19 #include "aom/aom_integer.h"
20 #include "aom_dsp/aom_dsp_common.h"
21 #include "aom_dsp/aom_filter.h"
22 #include "aom_dsp/arm/mem_neon.h"
23 #include "aom_dsp/arm/transpose_neon.h"
24 #include "aom_ports/mem.h"
25
26 DECLARE_ALIGNED(16, static const uint8_t, dot_prod_permute_tbl[48]) = {
27 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6,
28 4, 5, 6, 7, 5, 6, 7, 8, 6, 7, 8, 9, 7, 8, 9, 10,
29 8, 9, 10, 11, 9, 10, 11, 12, 10, 11, 12, 13, 11, 12, 13, 14
30 };
31
32 DECLARE_ALIGNED(16, static const uint8_t, dot_prod_tran_concat_tbl[32]) = {
33 0, 8, 16, 24, 1, 9, 17, 25, 2, 10, 18, 26, 3, 11, 19, 27,
34 4, 12, 20, 28, 5, 13, 21, 29, 6, 14, 22, 30, 7, 15, 23, 31
35 };
36
37 DECLARE_ALIGNED(16, static const uint8_t, dot_prod_merge_block_tbl[48]) = {
38 /* Shift left and insert new last column in transposed 4x4 block. */
39 1, 2, 3, 16, 5, 6, 7, 20, 9, 10, 11, 24, 13, 14, 15, 28,
40 /* Shift left and insert two new columns in transposed 4x4 block. */
41 2, 3, 16, 17, 6, 7, 20, 21, 10, 11, 24, 25, 14, 15, 28, 29,
42 /* Shift left and insert three new columns in transposed 4x4 block. */
43 3, 16, 17, 18, 7, 20, 21, 22, 11, 24, 25, 26, 15, 28, 29, 30
44 };
45
convolve8_4_usdot(const uint8x16_t samples,const int8x8_t filter,const uint8x16x2_t permute_tbl)46 static INLINE int16x4_t convolve8_4_usdot(const uint8x16_t samples,
47 const int8x8_t filter,
48 const uint8x16x2_t permute_tbl) {
49 uint8x16_t permuted_samples[2];
50 int32x4_t sum;
51
52 /* Permute samples ready for dot product. */
53 /* { 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6 } */
54 permuted_samples[0] = vqtbl1q_u8(samples, permute_tbl.val[0]);
55 /* { 4, 5, 6, 7, 5, 6, 7, 8, 6, 7, 8, 9, 7, 8, 9, 10 } */
56 permuted_samples[1] = vqtbl1q_u8(samples, permute_tbl.val[1]);
57
58 sum = vusdotq_lane_s32(vdupq_n_s32(0), permuted_samples[0], filter, 0);
59 sum = vusdotq_lane_s32(sum, permuted_samples[1], filter, 1);
60
61 /* Further narrowing and packing is performed by the caller. */
62 return vqmovn_s32(sum);
63 }
64
convolve8_8_usdot(const uint8x16_t samples,const int8x8_t filter,const uint8x16x3_t permute_tbl)65 static INLINE uint8x8_t convolve8_8_usdot(const uint8x16_t samples,
66 const int8x8_t filter,
67 const uint8x16x3_t permute_tbl) {
68 uint8x16_t permuted_samples[3];
69 int32x4_t sum0, sum1;
70 int16x8_t sum;
71
72 /* Permute samples ready for dot product. */
73 /* { 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6 } */
74 permuted_samples[0] = vqtbl1q_u8(samples, permute_tbl.val[0]);
75 /* { 4, 5, 6, 7, 5, 6, 7, 8, 6, 7, 8, 9, 7, 8, 9, 10 } */
76 permuted_samples[1] = vqtbl1q_u8(samples, permute_tbl.val[1]);
77 /* { 8, 9, 10, 11, 9, 10, 11, 12, 10, 11, 12, 13, 11, 12, 13, 14 } */
78 permuted_samples[2] = vqtbl1q_u8(samples, permute_tbl.val[2]);
79
80 /* First 4 output values. */
81 sum0 = vusdotq_lane_s32(vdupq_n_s32(0), permuted_samples[0], filter, 0);
82 sum0 = vusdotq_lane_s32(sum0, permuted_samples[1], filter, 1);
83 /* Second 4 output values. */
84 sum1 = vusdotq_lane_s32(vdupq_n_s32(0), permuted_samples[1], filter, 0);
85 sum1 = vusdotq_lane_s32(sum1, permuted_samples[2], filter, 1);
86
87 /* Narrow and re-pack. */
88 sum = vcombine_s16(vqmovn_s32(sum0), vqmovn_s32(sum1));
89 return vqrshrun_n_s16(sum, FILTER_BITS);
90 }
91
aom_convolve8_horiz_neon_i8mm(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,const int16_t * filter_x,int x_step_q4,const int16_t * filter_y,int y_step_q4,int w,int h)92 void aom_convolve8_horiz_neon_i8mm(const uint8_t *src, ptrdiff_t src_stride,
93 uint8_t *dst, ptrdiff_t dst_stride,
94 const int16_t *filter_x, int x_step_q4,
95 const int16_t *filter_y, int y_step_q4,
96 int w, int h) {
97 const int8x8_t filter = vmovn_s16(vld1q_s16(filter_x));
98 uint8x16_t s0, s1, s2, s3;
99
100 assert((intptr_t)dst % 4 == 0);
101 assert(dst_stride % 4 == 0);
102
103 (void)x_step_q4;
104 (void)filter_y;
105 (void)y_step_q4;
106
107 src -= ((SUBPEL_TAPS / 2) - 1);
108
109 if (w == 4) {
110 const uint8x16x2_t perm_tbl = vld1q_u8_x2(dot_prod_permute_tbl);
111 do {
112 int16x4_t t0, t1, t2, t3;
113 uint8x8_t d01, d23;
114
115 load_u8_16x4(src, src_stride, &s0, &s1, &s2, &s3);
116
117 t0 = convolve8_4_usdot(s0, filter, perm_tbl);
118 t1 = convolve8_4_usdot(s1, filter, perm_tbl);
119 t2 = convolve8_4_usdot(s2, filter, perm_tbl);
120 t3 = convolve8_4_usdot(s3, filter, perm_tbl);
121 d01 = vqrshrun_n_s16(vcombine_s16(t0, t1), FILTER_BITS);
122 d23 = vqrshrun_n_s16(vcombine_s16(t2, t3), FILTER_BITS);
123
124 store_u8x4_strided_x2(dst + 0 * dst_stride, dst_stride, d01);
125 store_u8x4_strided_x2(dst + 2 * dst_stride, dst_stride, d23);
126
127 src += 4 * src_stride;
128 dst += 4 * dst_stride;
129 h -= 4;
130 } while (h > 0);
131 } else {
132 const uint8x16x3_t perm_tbl = vld1q_u8_x3(dot_prod_permute_tbl);
133 const uint8_t *s;
134 uint8_t *d;
135 int width;
136 uint8x8_t d0, d1, d2, d3;
137
138 do {
139 width = w;
140 s = src;
141 d = dst;
142 do {
143 load_u8_16x4(s, src_stride, &s0, &s1, &s2, &s3);
144
145 d0 = convolve8_8_usdot(s0, filter, perm_tbl);
146 d1 = convolve8_8_usdot(s1, filter, perm_tbl);
147 d2 = convolve8_8_usdot(s2, filter, perm_tbl);
148 d3 = convolve8_8_usdot(s3, filter, perm_tbl);
149
150 store_u8_8x4(d, dst_stride, d0, d1, d2, d3);
151
152 s += 8;
153 d += 8;
154 width -= 8;
155 } while (width != 0);
156 src += 4 * src_stride;
157 dst += 4 * dst_stride;
158 h -= 4;
159 } while (h > 0);
160 }
161 }
162
transpose_concat_4x4(uint8x8_t a0,uint8x8_t a1,uint8x8_t a2,uint8x8_t a3,uint8x16_t * b,const uint8x16_t permute_tbl)163 static INLINE void transpose_concat_4x4(uint8x8_t a0, uint8x8_t a1,
164 uint8x8_t a2, uint8x8_t a3,
165 uint8x16_t *b,
166 const uint8x16_t permute_tbl) {
167 /* Transpose 8-bit elements and concatenate result rows as follows:
168 * a0: 00, 01, 02, 03, XX, XX, XX, XX
169 * a1: 10, 11, 12, 13, XX, XX, XX, XX
170 * a2: 20, 21, 22, 23, XX, XX, XX, XX
171 * a3: 30, 31, 32, 33, XX, XX, XX, XX
172 *
173 * b: 00, 10, 20, 30, 01, 11, 21, 31, 02, 12, 22, 32, 03, 13, 23, 33
174 *
175 * The 'permute_tbl' is always 'dot_prod_tran_concat_tbl' above. Passing it
176 * as an argument is preferable to loading it directly from memory as this
177 * inline helper is called many times from the same parent function.
178 */
179
180 uint8x16x2_t samples = { { vcombine_u8(a0, a1), vcombine_u8(a2, a3) } };
181 *b = vqtbl2q_u8(samples, permute_tbl);
182 }
183
transpose_concat_8x4(uint8x8_t a0,uint8x8_t a1,uint8x8_t a2,uint8x8_t a3,uint8x16_t * b0,uint8x16_t * b1,const uint8x16x2_t permute_tbl)184 static INLINE void transpose_concat_8x4(uint8x8_t a0, uint8x8_t a1,
185 uint8x8_t a2, uint8x8_t a3,
186 uint8x16_t *b0, uint8x16_t *b1,
187 const uint8x16x2_t permute_tbl) {
188 /* Transpose 8-bit elements and concatenate result rows as follows:
189 * a0: 00, 01, 02, 03, 04, 05, 06, 07
190 * a1: 10, 11, 12, 13, 14, 15, 16, 17
191 * a2: 20, 21, 22, 23, 24, 25, 26, 27
192 * a3: 30, 31, 32, 33, 34, 35, 36, 37
193 *
194 * b0: 00, 10, 20, 30, 01, 11, 21, 31, 02, 12, 22, 32, 03, 13, 23, 33
195 * b1: 04, 14, 24, 34, 05, 15, 25, 35, 06, 16, 26, 36, 07, 17, 27, 37
196 *
197 * The 'permute_tbl' is always 'dot_prod_tran_concat_tbl' above. Passing it
198 * as an argument is preferable to loading it directly from memory as this
199 * inline helper is called many times from the same parent function.
200 */
201
202 uint8x16x2_t samples = { { vcombine_u8(a0, a1), vcombine_u8(a2, a3) } };
203 *b0 = vqtbl2q_u8(samples, permute_tbl.val[0]);
204 *b1 = vqtbl2q_u8(samples, permute_tbl.val[1]);
205 }
206
convolve8_4_usdot_partial(const uint8x16_t samples_lo,const uint8x16_t samples_hi,const int8x8_t filter)207 static INLINE int16x4_t convolve8_4_usdot_partial(const uint8x16_t samples_lo,
208 const uint8x16_t samples_hi,
209 const int8x8_t filter) {
210 /* Sample permutation is performed by the caller. */
211 int32x4_t sum;
212
213 sum = vusdotq_lane_s32(vdupq_n_s32(0), samples_lo, filter, 0);
214 sum = vusdotq_lane_s32(sum, samples_hi, filter, 1);
215
216 /* Further narrowing and packing is performed by the caller. */
217 return vqmovn_s32(sum);
218 }
219
convolve8_8_usdot_partial(const uint8x16_t samples0_lo,const uint8x16_t samples0_hi,const uint8x16_t samples1_lo,const uint8x16_t samples1_hi,const int8x8_t filter)220 static INLINE uint8x8_t convolve8_8_usdot_partial(const uint8x16_t samples0_lo,
221 const uint8x16_t samples0_hi,
222 const uint8x16_t samples1_lo,
223 const uint8x16_t samples1_hi,
224 const int8x8_t filter) {
225 /* Sample permutation is performed by the caller. */
226 int32x4_t sum0, sum1;
227 int16x8_t sum;
228
229 /* First 4 output values. */
230 sum0 = vusdotq_lane_s32(vdupq_n_s32(0), samples0_lo, filter, 0);
231 sum0 = vusdotq_lane_s32(sum0, samples0_hi, filter, 1);
232 /* Second 4 output values. */
233 sum1 = vusdotq_lane_s32(vdupq_n_s32(0), samples1_lo, filter, 0);
234 sum1 = vusdotq_lane_s32(sum1, samples1_hi, filter, 1);
235
236 /* Narrow and re-pack. */
237 sum = vcombine_s16(vqmovn_s32(sum0), vqmovn_s32(sum1));
238 return vqrshrun_n_s16(sum, FILTER_BITS);
239 }
240
aom_convolve8_vert_neon_i8mm(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,const int16_t * filter_x,int x_step_q4,const int16_t * filter_y,int y_step_q4,int w,int h)241 void aom_convolve8_vert_neon_i8mm(const uint8_t *src, ptrdiff_t src_stride,
242 uint8_t *dst, ptrdiff_t dst_stride,
243 const int16_t *filter_x, int x_step_q4,
244 const int16_t *filter_y, int y_step_q4, int w,
245 int h) {
246 const int8x8_t filter = vmovn_s16(vld1q_s16(filter_y));
247 const uint8x16x3_t merge_block_tbl = vld1q_u8_x3(dot_prod_merge_block_tbl);
248 uint8x16x2_t samples_LUT;
249
250 assert((intptr_t)dst % 4 == 0);
251 assert(dst_stride % 4 == 0);
252
253 (void)filter_x;
254 (void)x_step_q4;
255 (void)y_step_q4;
256
257 src -= ((SUBPEL_TAPS / 2) - 1) * src_stride;
258
259 if (w == 4) {
260 const uint8x16_t tran_concat_tbl = vld1q_u8(dot_prod_tran_concat_tbl);
261
262 uint8x8_t s0, s1, s2, s3, s4, s5, s6;
263 load_u8_8x7(src, src_stride, &s0, &s1, &s2, &s3, &s4, &s5, &s6);
264 src += 7 * src_stride;
265
266 /* This operation combines a conventional transpose and the sample permute
267 * (see horizontal case) required before computing the dot product.
268 */
269 uint8x16_t s0123, s1234, s2345, s3456;
270 transpose_concat_4x4(s0, s1, s2, s3, &s0123, tran_concat_tbl);
271 transpose_concat_4x4(s1, s2, s3, s4, &s1234, tran_concat_tbl);
272 transpose_concat_4x4(s2, s3, s4, s5, &s2345, tran_concat_tbl);
273 transpose_concat_4x4(s3, s4, s5, s6, &s3456, tran_concat_tbl);
274
275 do {
276 uint8x8_t s7, s8, s9, s10;
277 load_u8_8x4(src, src_stride, &s7, &s8, &s9, &s10);
278
279 uint8x16_t s4567, s5678, s6789, s78910;
280 transpose_concat_4x4(s7, s8, s9, s10, &s78910, tran_concat_tbl);
281
282 /* Merge new data into block from previous iteration. */
283 samples_LUT.val[0] = s3456;
284 samples_LUT.val[1] = s78910;
285 s4567 = vqtbl2q_u8(samples_LUT, merge_block_tbl.val[0]);
286 s5678 = vqtbl2q_u8(samples_LUT, merge_block_tbl.val[1]);
287 s6789 = vqtbl2q_u8(samples_LUT, merge_block_tbl.val[2]);
288
289 int16x4_t d0 = convolve8_4_usdot_partial(s0123, s4567, filter);
290 int16x4_t d1 = convolve8_4_usdot_partial(s1234, s5678, filter);
291 int16x4_t d2 = convolve8_4_usdot_partial(s2345, s6789, filter);
292 int16x4_t d3 = convolve8_4_usdot_partial(s3456, s78910, filter);
293 uint8x8_t d01 = vqrshrun_n_s16(vcombine_s16(d0, d1), FILTER_BITS);
294 uint8x8_t d23 = vqrshrun_n_s16(vcombine_s16(d2, d3), FILTER_BITS);
295
296 store_u8x4_strided_x2(dst + 0 * dst_stride, dst_stride, d01);
297 store_u8x4_strided_x2(dst + 2 * dst_stride, dst_stride, d23);
298
299 /* Prepare block for next iteration - re-using as much as possible. */
300 /* Shuffle everything up four rows. */
301 s0123 = s4567;
302 s1234 = s5678;
303 s2345 = s6789;
304 s3456 = s78910;
305
306 src += 4 * src_stride;
307 dst += 4 * dst_stride;
308 h -= 4;
309 } while (h != 0);
310 } else {
311 const uint8x16x2_t tran_concat_tbl = vld1q_u8_x2(dot_prod_tran_concat_tbl);
312
313 do {
314 int height = h;
315 const uint8_t *s = src;
316 uint8_t *d = dst;
317
318 uint8x8_t s0, s1, s2, s3, s4, s5, s6;
319 load_u8_8x7(s, src_stride, &s0, &s1, &s2, &s3, &s4, &s5, &s6);
320 s += 7 * src_stride;
321
322 /* This operation combines a conventional transpose and the sample permute
323 * (see horizontal case) required before computing the dot product.
324 */
325 uint8x16_t s0123_lo, s0123_hi, s1234_lo, s1234_hi, s2345_lo, s2345_hi,
326 s3456_lo, s3456_hi;
327 transpose_concat_8x4(s0, s1, s2, s3, &s0123_lo, &s0123_hi,
328 tran_concat_tbl);
329 transpose_concat_8x4(s1, s2, s3, s4, &s1234_lo, &s1234_hi,
330 tran_concat_tbl);
331 transpose_concat_8x4(s2, s3, s4, s5, &s2345_lo, &s2345_hi,
332 tran_concat_tbl);
333 transpose_concat_8x4(s3, s4, s5, s6, &s3456_lo, &s3456_hi,
334 tran_concat_tbl);
335
336 do {
337 uint8x8_t s7, s8, s9, s10;
338 load_u8_8x4(s, src_stride, &s7, &s8, &s9, &s10);
339
340 uint8x16_t s4567_lo, s4567_hi, s5678_lo, s5678_hi, s6789_lo, s6789_hi,
341 s78910_lo, s78910_hi;
342 transpose_concat_8x4(s7, s8, s9, s10, &s78910_lo, &s78910_hi,
343 tran_concat_tbl);
344
345 /* Merge new data into block from previous iteration. */
346 samples_LUT.val[0] = s3456_lo;
347 samples_LUT.val[1] = s78910_lo;
348 s4567_lo = vqtbl2q_u8(samples_LUT, merge_block_tbl.val[0]);
349 s5678_lo = vqtbl2q_u8(samples_LUT, merge_block_tbl.val[1]);
350 s6789_lo = vqtbl2q_u8(samples_LUT, merge_block_tbl.val[2]);
351
352 samples_LUT.val[0] = s3456_hi;
353 samples_LUT.val[1] = s78910_hi;
354 s4567_hi = vqtbl2q_u8(samples_LUT, merge_block_tbl.val[0]);
355 s5678_hi = vqtbl2q_u8(samples_LUT, merge_block_tbl.val[1]);
356 s6789_hi = vqtbl2q_u8(samples_LUT, merge_block_tbl.val[2]);
357
358 uint8x8_t d0 = convolve8_8_usdot_partial(s0123_lo, s4567_lo, s0123_hi,
359 s4567_hi, filter);
360 uint8x8_t d1 = convolve8_8_usdot_partial(s1234_lo, s5678_lo, s1234_hi,
361 s5678_hi, filter);
362 uint8x8_t d2 = convolve8_8_usdot_partial(s2345_lo, s6789_lo, s2345_hi,
363 s6789_hi, filter);
364 uint8x8_t d3 = convolve8_8_usdot_partial(s3456_lo, s78910_lo, s3456_hi,
365 s78910_hi, filter);
366
367 store_u8_8x4(d, dst_stride, d0, d1, d2, d3);
368
369 /* Prepare block for next iteration - re-using as much as possible. */
370 /* Shuffle everything up four rows. */
371 s0123_lo = s4567_lo;
372 s0123_hi = s4567_hi;
373 s1234_lo = s5678_lo;
374 s1234_hi = s5678_hi;
375 s2345_lo = s6789_lo;
376 s2345_hi = s6789_hi;
377 s3456_lo = s78910_lo;
378 s3456_hi = s78910_hi;
379
380 s += 4 * src_stride;
381 d += 4 * dst_stride;
382 height -= 4;
383 } while (height != 0);
384 src += 8;
385 dst += 8;
386 w -= 8;
387 } while (w != 0);
388 }
389 }
390