xref: /aosp_15_r20/external/libvpx/vpx_dsp/arm/avg_neon.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2015 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <arm_neon.h>
12 #include <assert.h>
13 
14 #include "./vpx_dsp_rtcd.h"
15 #include "./vpx_config.h"
16 
17 #include "vpx/vpx_integer.h"
18 #include "vpx_dsp/arm/idct_neon.h"
19 #include "vpx_dsp/arm/mem_neon.h"
20 #include "vpx_dsp/arm/sum_neon.h"
21 
vpx_avg_4x4_neon(const uint8_t * a,int a_stride)22 uint32_t vpx_avg_4x4_neon(const uint8_t *a, int a_stride) {
23   const uint8x16_t b = load_unaligned_u8q(a, a_stride);
24   const uint16x8_t c = vaddl_u8(vget_low_u8(b), vget_high_u8(b));
25   return (horizontal_add_uint16x8(c) + (1 << 3)) >> 4;
26 }
27 
vpx_avg_8x8_neon(const uint8_t * a,int a_stride)28 uint32_t vpx_avg_8x8_neon(const uint8_t *a, int a_stride) {
29   int i;
30   uint8x8_t b, c;
31   uint16x8_t sum;
32   b = vld1_u8(a);
33   a += a_stride;
34   c = vld1_u8(a);
35   a += a_stride;
36   sum = vaddl_u8(b, c);
37 
38   for (i = 0; i < 6; ++i) {
39     const uint8x8_t d = vld1_u8(a);
40     a += a_stride;
41     sum = vaddw_u8(sum, d);
42   }
43 
44   return (horizontal_add_uint16x8(sum) + (1 << 5)) >> 6;
45 }
46 
47 // coeff: 16 bits, dynamic range [-32640, 32640].
48 // length: value range {16, 64, 256, 1024}.
49 // satd: 26 bits, dynamic range [-32640 * 1024, 32640 * 1024]
vpx_satd_neon(const tran_low_t * coeff,int length)50 int vpx_satd_neon(const tran_low_t *coeff, int length) {
51   int32x4_t sum_s32[2] = { vdupq_n_s32(0), vdupq_n_s32(0) };
52 
53   do {
54     int16x8_t abs0, abs1;
55     const int16x8_t s0 = load_tran_low_to_s16q(coeff);
56     const int16x8_t s1 = load_tran_low_to_s16q(coeff + 8);
57 
58     abs0 = vabsq_s16(s0);
59     sum_s32[0] = vpadalq_s16(sum_s32[0], abs0);
60     abs1 = vabsq_s16(s1);
61     sum_s32[1] = vpadalq_s16(sum_s32[1], abs1);
62 
63     length -= 16;
64     coeff += 16;
65   } while (length != 0);
66 
67   return horizontal_add_int32x4(vaddq_s32(sum_s32[0], sum_s32[1]));
68 }
69 
vpx_int_pro_row_neon(int16_t hbuf[16],uint8_t const * ref,const int ref_stride,const int height)70 void vpx_int_pro_row_neon(int16_t hbuf[16], uint8_t const *ref,
71                           const int ref_stride, const int height) {
72   int i;
73   uint8x16_t r0, r1, r2, r3;
74   uint16x8_t sum_lo[2], sum_hi[2];
75   uint16x8_t tmp_lo[2], tmp_hi[2];
76   int16x8_t avg_lo, avg_hi;
77 
78   const int norm_factor = (height >> 5) + 3;
79   const int16x8_t neg_norm_factor = vdupq_n_s16(-norm_factor);
80 
81   assert(height >= 4 && height % 4 == 0);
82 
83   r0 = vld1q_u8(ref + 0 * ref_stride);
84   r1 = vld1q_u8(ref + 1 * ref_stride);
85   r2 = vld1q_u8(ref + 2 * ref_stride);
86   r3 = vld1q_u8(ref + 3 * ref_stride);
87 
88   sum_lo[0] = vaddl_u8(vget_low_u8(r0), vget_low_u8(r1));
89   sum_hi[0] = vaddl_u8(vget_high_u8(r0), vget_high_u8(r1));
90   sum_lo[1] = vaddl_u8(vget_low_u8(r2), vget_low_u8(r3));
91   sum_hi[1] = vaddl_u8(vget_high_u8(r2), vget_high_u8(r3));
92 
93   ref += 4 * ref_stride;
94 
95   for (i = 4; i < height; i += 4) {
96     r0 = vld1q_u8(ref + 0 * ref_stride);
97     r1 = vld1q_u8(ref + 1 * ref_stride);
98     r2 = vld1q_u8(ref + 2 * ref_stride);
99     r3 = vld1q_u8(ref + 3 * ref_stride);
100 
101     tmp_lo[0] = vaddl_u8(vget_low_u8(r0), vget_low_u8(r1));
102     tmp_hi[0] = vaddl_u8(vget_high_u8(r0), vget_high_u8(r1));
103     tmp_lo[1] = vaddl_u8(vget_low_u8(r2), vget_low_u8(r3));
104     tmp_hi[1] = vaddl_u8(vget_high_u8(r2), vget_high_u8(r3));
105 
106     sum_lo[0] = vaddq_u16(sum_lo[0], tmp_lo[0]);
107     sum_hi[0] = vaddq_u16(sum_hi[0], tmp_hi[0]);
108     sum_lo[1] = vaddq_u16(sum_lo[1], tmp_lo[1]);
109     sum_hi[1] = vaddq_u16(sum_hi[1], tmp_hi[1]);
110 
111     ref += 4 * ref_stride;
112   }
113 
114   sum_lo[0] = vaddq_u16(sum_lo[0], sum_lo[1]);
115   sum_hi[0] = vaddq_u16(sum_hi[0], sum_hi[1]);
116 
117   avg_lo = vshlq_s16(vreinterpretq_s16_u16(sum_lo[0]), neg_norm_factor);
118   avg_hi = vshlq_s16(vreinterpretq_s16_u16(sum_hi[0]), neg_norm_factor);
119 
120   vst1q_s16(hbuf, avg_lo);
121   vst1q_s16(hbuf + 8, avg_hi);
122 }
123 
vpx_int_pro_col_neon(uint8_t const * ref,const int width)124 int16_t vpx_int_pro_col_neon(uint8_t const *ref, const int width) {
125   uint16x8_t sum;
126   int i;
127 
128   assert(width >= 16 && width % 16 == 0);
129 
130   sum = vpaddlq_u8(vld1q_u8(ref));
131   for (i = 16; i < width; i += 16) {
132     sum = vpadalq_u8(sum, vld1q_u8(ref + i));
133   }
134 
135   return (int16_t)horizontal_add_uint16x8(sum);
136 }
137 
138 // ref, src = [0, 510] - max diff = 16-bits
139 // bwl = {2, 3, 4}, width = {16, 32, 64}
vpx_vector_var_neon(int16_t const * ref,int16_t const * src,const int bwl)140 int vpx_vector_var_neon(int16_t const *ref, int16_t const *src, const int bwl) {
141   int width = 4 << bwl;
142   int32x4_t sse = vdupq_n_s32(0);
143   int16x8_t total = vdupq_n_s16(0);
144 
145   assert(width >= 8);
146   assert((width % 8) == 0);
147 
148   do {
149     const int16x8_t r = vld1q_s16(ref);
150     const int16x8_t s = vld1q_s16(src);
151     const int16x8_t diff = vsubq_s16(r, s);  // [-510, 510], 10 bits.
152     const int16x4_t diff_lo = vget_low_s16(diff);
153     const int16x4_t diff_hi = vget_high_s16(diff);
154     sse = vmlal_s16(sse, diff_lo, diff_lo);  // dynamic range 26 bits.
155     sse = vmlal_s16(sse, diff_hi, diff_hi);
156     total = vaddq_s16(total, diff);  // dynamic range 16 bits.
157 
158     ref += 8;
159     src += 8;
160     width -= 8;
161   } while (width != 0);
162 
163   {
164     // Note: 'total''s pairwise addition could be implemented similarly to
165     // horizontal_add_uint16x8(), but one less vpaddl with 'total' when paired
166     // with the summation of 'sse' performed better on a Cortex-A15.
167     const int32x4_t t0 = vpaddlq_s16(total);  // cascading summation of 'total'
168     const int32x2_t t1 = vadd_s32(vget_low_s32(t0), vget_high_s32(t0));
169     const int32x2_t t2 = vpadd_s32(t1, t1);
170     const int t = vget_lane_s32(t2, 0);
171     const int64x2_t s0 = vpaddlq_s32(sse);  // cascading summation of 'sse'.
172     const int32x2_t s1 = vadd_s32(vreinterpret_s32_s64(vget_low_s64(s0)),
173                                   vreinterpret_s32_s64(vget_high_s64(s0)));
174     const int s = vget_lane_s32(s1, 0);
175     const int shift_factor = bwl + 2;
176     return s - ((t * t) >> shift_factor);
177   }
178 }
179 
vpx_minmax_8x8_neon(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int * min,int * max)180 void vpx_minmax_8x8_neon(const uint8_t *a, int a_stride, const uint8_t *b,
181                          int b_stride, int *min, int *max) {
182   // Load and concatenate.
183   const uint8x16_t a01 = vcombine_u8(vld1_u8(a), vld1_u8(a + a_stride));
184   const uint8x16_t a23 =
185       vcombine_u8(vld1_u8(a + 2 * a_stride), vld1_u8(a + 3 * a_stride));
186   const uint8x16_t a45 =
187       vcombine_u8(vld1_u8(a + 4 * a_stride), vld1_u8(a + 5 * a_stride));
188   const uint8x16_t a67 =
189       vcombine_u8(vld1_u8(a + 6 * a_stride), vld1_u8(a + 7 * a_stride));
190 
191   const uint8x16_t b01 = vcombine_u8(vld1_u8(b), vld1_u8(b + b_stride));
192   const uint8x16_t b23 =
193       vcombine_u8(vld1_u8(b + 2 * b_stride), vld1_u8(b + 3 * b_stride));
194   const uint8x16_t b45 =
195       vcombine_u8(vld1_u8(b + 4 * b_stride), vld1_u8(b + 5 * b_stride));
196   const uint8x16_t b67 =
197       vcombine_u8(vld1_u8(b + 6 * b_stride), vld1_u8(b + 7 * b_stride));
198 
199   // Absolute difference.
200   const uint8x16_t ab01_diff = vabdq_u8(a01, b01);
201   const uint8x16_t ab23_diff = vabdq_u8(a23, b23);
202   const uint8x16_t ab45_diff = vabdq_u8(a45, b45);
203   const uint8x16_t ab67_diff = vabdq_u8(a67, b67);
204 
205   // Max values between the Q vectors.
206   const uint8x16_t ab0123_max = vmaxq_u8(ab01_diff, ab23_diff);
207   const uint8x16_t ab4567_max = vmaxq_u8(ab45_diff, ab67_diff);
208   const uint8x16_t ab0123_min = vminq_u8(ab01_diff, ab23_diff);
209   const uint8x16_t ab4567_min = vminq_u8(ab45_diff, ab67_diff);
210 
211   const uint8x16_t ab07_max = vmaxq_u8(ab0123_max, ab4567_max);
212   const uint8x16_t ab07_min = vminq_u8(ab0123_min, ab4567_min);
213 
214 #if VPX_ARCH_AARCH64
215   *min = *max = 0;  // Clear high bits
216   *((uint8_t *)max) = vmaxvq_u8(ab07_max);
217   *((uint8_t *)min) = vminvq_u8(ab07_min);
218 #else
219   // Split into 64-bit vectors and execute pairwise min/max.
220   uint8x8_t ab_max = vmax_u8(vget_high_u8(ab07_max), vget_low_u8(ab07_max));
221   uint8x8_t ab_min = vmin_u8(vget_high_u8(ab07_min), vget_low_u8(ab07_min));
222 
223   // Enough runs of vpmax/min propagate the max/min values to every position.
224   ab_max = vpmax_u8(ab_max, ab_max);
225   ab_min = vpmin_u8(ab_min, ab_min);
226 
227   ab_max = vpmax_u8(ab_max, ab_max);
228   ab_min = vpmin_u8(ab_min, ab_min);
229 
230   ab_max = vpmax_u8(ab_max, ab_max);
231   ab_min = vpmin_u8(ab_min, ab_min);
232 
233   *min = *max = 0;  // Clear high bits
234   // Store directly to avoid costly neon->gpr transfer.
235   vst1_lane_u8((uint8_t *)max, ab_max, 0);
236   vst1_lane_u8((uint8_t *)min, ab_min, 0);
237 #endif
238 }
239