summaryrefslogtreecommitdiffstats
path: root/third_party/aom/aom_dsp/arm/sadxd_neon_dotprod.c
blob: 3d11d1cb961e81f24e5034612242cd5128e78ddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright (c) 2023, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

#include <arm_neon.h>

#include "config/aom_config.h"
#include "config/aom_dsp_rtcd.h"

#include "aom/aom_integer.h"
#include "aom_dsp/arm/mem_neon.h"
#include "aom_dsp/arm/sum_neon.h"

static INLINE void sad16_neon(uint8x16_t src, uint8x16_t ref,
                              uint32x4_t *const sad_sum) {
  uint8x16_t abs_diff = vabdq_u8(src, ref);
  *sad_sum = vdotq_u32(*sad_sum, abs_diff, vdupq_n_u8(1));
}

static INLINE void sadwxhx3d_large_neon_dotprod(const uint8_t *src,
                                                int src_stride,
                                                const uint8_t *const ref[4],
                                                int ref_stride, uint32_t res[4],
                                                int w, int h) {
  uint32x4_t sum_lo[3] = { vdupq_n_u32(0), vdupq_n_u32(0), vdupq_n_u32(0) };
  uint32x4_t sum_hi[3] = { vdupq_n_u32(0), vdupq_n_u32(0), vdupq_n_u32(0) };

  int ref_offset = 0;
  int i = h;
  do {
    int j = 0;
    do {
      const uint8x16_t s0 = vld1q_u8(src + j);
      sad16_neon(s0, vld1q_u8(ref[0] + ref_offset + j), &sum_lo[0]);
      sad16_neon(s0, vld1q_u8(ref[1] + ref_offset + j), &sum_lo[1]);
      sad16_neon(s0, vld1q_u8(ref[2] + ref_offset + j), &sum_lo[2]);

      const uint8x16_t s1 = vld1q_u8(src + j + 16);
      sad16_neon(s1, vld1q_u8(ref[0] + ref_offset + j + 16), &sum_hi[0]);
      sad16_neon(s1, vld1q_u8(ref[1] + ref_offset + j + 16), &sum_hi[1]);
      sad16_neon(s1, vld1q_u8(ref[2] + ref_offset + j + 16), &sum_hi[2]);

      j += 32;
    } while (j < w);

    src += src_stride;
    ref_offset += ref_stride;
  } while (--i != 0);

  res[0] = horizontal_add_u32x4(vaddq_u32(sum_lo[0], sum_hi[0]));
  res[1] = horizontal_add_u32x4(vaddq_u32(sum_lo[1], sum_hi[1]));
  res[2] = horizontal_add_u32x4(vaddq_u32(sum_lo[2], sum_hi[2]));
}

static INLINE void sad128xhx3d_neon_dotprod(const uint8_t *src, int src_stride,
                                            const uint8_t *const ref[4],
                                            int ref_stride, uint32_t res[4],
                                            int h) {
  sadwxhx3d_large_neon_dotprod(src, src_stride, ref, ref_stride, res, 128, h);
}

static INLINE void sad64xhx3d_neon_dotprod(const uint8_t *src, int src_stride,
                                           const uint8_t *const ref[4],
                                           int ref_stride, uint32_t res[4],
                                           int h) {
  sadwxhx3d_large_neon_dotprod(src, src_stride, ref, ref_stride, res, 64, h);
}

static INLINE void sad32xhx3d_neon_dotprod(const uint8_t *src, int src_stride,
                                           const uint8_t *const ref[4],
                                           int ref_stride, uint32_t res[4],
                                           int h) {
  sadwxhx3d_large_neon_dotprod(src, src_stride, ref, ref_stride, res, 32, h);
}

static INLINE void sad16xhx3d_neon_dotprod(const uint8_t *src, int src_stride,
                                           const uint8_t *const ref[4],
                                           int ref_stride, uint32_t res[4],
                                           int h) {
  uint32x4_t sum[3] = { vdupq_n_u32(0), vdupq_n_u32(0), vdupq_n_u32(0) };

  int ref_offset = 0;
  int i = h;
  do {
    const uint8x16_t s = vld1q_u8(src);
    sad16_neon(s, vld1q_u8(ref[0] + ref_offset), &sum[0]);
    sad16_neon(s, vld1q_u8(ref[1] + ref_offset), &sum[1]);
    sad16_neon(s, vld1q_u8(ref[2] + ref_offset), &sum[2]);

    src += src_stride;
    ref_offset += ref_stride;
  } while (--i != 0);

  res[0] = horizontal_add_u32x4(sum[0]);
  res[1] = horizontal_add_u32x4(sum[1]);
  res[2] = horizontal_add_u32x4(sum[2]);
}

#define SAD_WXH_3D_NEON_DOTPROD(w, h)                                         \
  void aom_sad##w##x##h##x3d_neon_dotprod(const uint8_t *src, int src_stride, \
                                          const uint8_t *const ref[4],        \
                                          int ref_stride, uint32_t res[4]) {  \
    sad##w##xhx3d_neon_dotprod(src, src_stride, ref, ref_stride, res, (h));   \
  }

SAD_WXH_3D_NEON_DOTPROD(16, 8)
SAD_WXH_3D_NEON_DOTPROD(16, 16)
SAD_WXH_3D_NEON_DOTPROD(16, 32)

SAD_WXH_3D_NEON_DOTPROD(32, 16)
SAD_WXH_3D_NEON_DOTPROD(32, 32)
SAD_WXH_3D_NEON_DOTPROD(32, 64)

SAD_WXH_3D_NEON_DOTPROD(64, 32)
SAD_WXH_3D_NEON_DOTPROD(64, 64)
SAD_WXH_3D_NEON_DOTPROD(64, 128)

SAD_WXH_3D_NEON_DOTPROD(128, 64)
SAD_WXH_3D_NEON_DOTPROD(128, 128)

#if !CONFIG_REALTIME_ONLY
SAD_WXH_3D_NEON_DOTPROD(16, 4)
SAD_WXH_3D_NEON_DOTPROD(16, 64)
SAD_WXH_3D_NEON_DOTPROD(32, 8)
SAD_WXH_3D_NEON_DOTPROD(64, 16)
#endif  // !CONFIG_REALTIME_ONLY

#undef SAD_WXH_3D_NEON_DOTPROD

static INLINE void sadwxhx4d_large_neon_dotprod(const uint8_t *src,
                                                int src_stride,
                                                const uint8_t *const ref[4],
                                                int ref_stride, uint32_t res[4],
                                                int w, int h) {
  uint32x4_t sum_lo[4] = { vdupq_n_u32(0), vdupq_n_u32(0), vdupq_n_u32(0),
                           vdupq_n_u32(0) };
  uint32x4_t sum_hi[4] = { vdupq_n_u32(0), vdupq_n_u32(0), vdupq_n_u32(0),
                           vdupq_n_u32(0) };
  uint32x4_t sum[4];

  int ref_offset = 0;
  int i = h;
  do {
    int j = 0;
    do {
      const uint8x16_t s0 = vld1q_u8(src + j);
      sad16_neon(s0, vld1q_u8(ref[0] + ref_offset + j), &sum_lo[0]);
      sad16_neon(s0, vld1q_u8(ref[1] + ref_offset + j), &sum_lo[1]);
      sad16_neon(s0, vld1q_u8(ref[2] + ref_offset + j), &sum_lo[2]);
      sad16_neon(s0, vld1q_u8(ref[3] + ref_offset + j), &sum_lo[3]);

      const uint8x16_t s1 = vld1q_u8(src + j + 16);
      sad16_neon(s1, vld1q_u8(ref[0] + ref_offset + j + 16), &sum_hi[0]);
      sad16_neon(s1, vld1q_u8(ref[1] + ref_offset + j + 16), &sum_hi[1]);
      sad16_neon(s1, vld1q_u8(ref[2] + ref_offset + j + 16), &sum_hi[2]);
      sad16_neon(s1, vld1q_u8(ref[3] + ref_offset + j + 16), &sum_hi[3]);

      j += 32;
    } while (j < w);

    src += src_stride;
    ref_offset += ref_stride;
  } while (--i != 0);

  sum[0] = vaddq_u32(sum_lo[0], sum_hi[0]);
  sum[1] = vaddq_u32(sum_lo[1], sum_hi[1]);
  sum[2] = vaddq_u32(sum_lo[2], sum_hi[2]);
  sum[3] = vaddq_u32(sum_lo[3], sum_hi[3]);

  vst1q_u32(res, horizontal_add_4d_u32x4(sum));
}

static INLINE void sad128xhx4d_neon_dotprod(const uint8_t *src, int src_stride,
                                            const uint8_t *const ref[4],
                                            int ref_stride, uint32_t res[4],
                                            int h) {
  sadwxhx4d_large_neon_dotprod(src, src_stride, ref, ref_stride, res, 128, h);
}

static INLINE void sad64xhx4d_neon_dotprod(const uint8_t *src, int src_stride,
                                           const uint8_t *const ref[4],
                                           int ref_stride, uint32_t res[4],
                                           int h) {
  sadwxhx4d_large_neon_dotprod(src, src_stride, ref, ref_stride, res, 64, h);
}

static INLINE void sad32xhx4d_neon_dotprod(const uint8_t *src, int src_stride,
                                           const uint8_t *const ref[4],
                                           int ref_stride, uint32_t res[4],
                                           int h) {
  sadwxhx4d_large_neon_dotprod(src, src_stride, ref, ref_stride, res, 32, h);
}

static INLINE void sad16xhx4d_neon_dotprod(const uint8_t *src, int src_stride,
                                           const uint8_t *const ref[4],
                                           int ref_stride, uint32_t res[4],
                                           int h) {
  uint32x4_t sum[4] = { vdupq_n_u32(0), vdupq_n_u32(0), vdupq_n_u32(0),
                        vdupq_n_u32(0) };

  int ref_offset = 0;
  int i = h;
  do {
    const uint8x16_t s = vld1q_u8(src);
    sad16_neon(s, vld1q_u8(ref[0] + ref_offset), &sum[0]);
    sad16_neon(s, vld1q_u8(ref[1] + ref_offset), &sum[1]);
    sad16_neon(s, vld1q_u8(ref[2] + ref_offset), &sum[2]);
    sad16_neon(s, vld1q_u8(ref[3] + ref_offset), &sum[3]);

    src += src_stride;
    ref_offset += ref_stride;
  } while (--i != 0);

  vst1q_u32(res, horizontal_add_4d_u32x4(sum));
}

#define SAD_WXH_4D_NEON_DOTPROD(w, h)                                         \
  void aom_sad##w##x##h##x4d_neon_dotprod(const uint8_t *src, int src_stride, \
                                          const uint8_t *const ref[4],        \
                                          int ref_stride, uint32_t res[4]) {  \
    sad##w##xhx4d_neon_dotprod(src, src_stride, ref, ref_stride, res, (h));   \
  }

SAD_WXH_4D_NEON_DOTPROD(16, 8)
SAD_WXH_4D_NEON_DOTPROD(16, 16)
SAD_WXH_4D_NEON_DOTPROD(16, 32)

SAD_WXH_4D_NEON_DOTPROD(32, 16)
SAD_WXH_4D_NEON_DOTPROD(32, 32)
SAD_WXH_4D_NEON_DOTPROD(32, 64)

SAD_WXH_4D_NEON_DOTPROD(64, 32)
SAD_WXH_4D_NEON_DOTPROD(64, 64)
SAD_WXH_4D_NEON_DOTPROD(64, 128)

SAD_WXH_4D_NEON_DOTPROD(128, 64)
SAD_WXH_4D_NEON_DOTPROD(128, 128)

#if !CONFIG_REALTIME_ONLY
SAD_WXH_4D_NEON_DOTPROD(16, 4)
SAD_WXH_4D_NEON_DOTPROD(16, 64)
SAD_WXH_4D_NEON_DOTPROD(32, 8)
SAD_WXH_4D_NEON_DOTPROD(64, 16)
#endif  // !CONFIG_REALTIME_ONLY

#undef SAD_WXH_4D_NEON_DOTPROD

#define SAD_SKIP_WXH_4D_NEON_DOTPROD(w, h)                                    \
  void aom_sad_skip_##w##x##h##x4d_neon_dotprod(                              \
      const uint8_t *src, int src_stride, const uint8_t *const ref[4],        \
      int ref_stride, uint32_t res[4]) {                                      \
    sad##w##xhx4d_neon_dotprod(src, 2 * src_stride, ref, 2 * ref_stride, res, \
                               ((h) >> 1));                                   \
    res[0] <<= 1;                                                             \
    res[1] <<= 1;                                                             \
    res[2] <<= 1;                                                             \
    res[3] <<= 1;                                                             \
  }

SAD_SKIP_WXH_4D_NEON_DOTPROD(16, 8)
SAD_SKIP_WXH_4D_NEON_DOTPROD(16, 16)
SAD_SKIP_WXH_4D_NEON_DOTPROD(16, 32)

SAD_SKIP_WXH_4D_NEON_DOTPROD(32, 16)
SAD_SKIP_WXH_4D_NEON_DOTPROD(32, 32)
SAD_SKIP_WXH_4D_NEON_DOTPROD(32, 64)

SAD_SKIP_WXH_4D_NEON_DOTPROD(64, 32)
SAD_SKIP_WXH_4D_NEON_DOTPROD(64, 64)
SAD_SKIP_WXH_4D_NEON_DOTPROD(64, 128)

SAD_SKIP_WXH_4D_NEON_DOTPROD(128, 64)
SAD_SKIP_WXH_4D_NEON_DOTPROD(128, 128)

#if !CONFIG_REALTIME_ONLY
SAD_SKIP_WXH_4D_NEON_DOTPROD(16, 4)
SAD_SKIP_WXH_4D_NEON_DOTPROD(16, 64)
SAD_SKIP_WXH_4D_NEON_DOTPROD(32, 8)
SAD_SKIP_WXH_4D_NEON_DOTPROD(64, 16)
#endif  // !CONFIG_REALTIME_ONLY

#undef SAD_SKIP_WXH_4D_NEON_DOTPROD