summaryrefslogtreecommitdiffstats
path: root/media/libvpx/libvpx/vpx_dsp/arm/mem_neon.h
blob: 1a20da70ef9f6da2e200245f1dc09e1557c92da4 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
 *  Copyright (c) 2017 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef VPX_VPX_DSP_ARM_MEM_NEON_H_
#define VPX_VPX_DSP_ARM_MEM_NEON_H_

#include <arm_neon.h>
#include <assert.h>
#include <string.h>

#include "./vpx_config.h"
#include "vpx/vpx_integer.h"
#include "vpx_dsp/vpx_dsp_common.h"

// Support for these xN intrinsics is lacking in older versions of GCC.
#if defined(__GNUC__) && !defined(__clang__)
#if __GNUC__ < 8 || defined(__arm__)
static INLINE uint8x16x2_t vld1q_u8_x2(uint8_t const *ptr) {
  uint8x16x2_t res = { { vld1q_u8(ptr + 0 * 16), vld1q_u8(ptr + 1 * 16) } };
  return res;
}
#endif

#if __GNUC__ < 9 || defined(__arm__)
static INLINE uint8x16x3_t vld1q_u8_x3(uint8_t const *ptr) {
  uint8x16x3_t res = { { vld1q_u8(ptr + 0 * 16), vld1q_u8(ptr + 1 * 16),
                         vld1q_u8(ptr + 2 * 16) } };
  return res;
}
#endif
#endif

static INLINE int16x4_t create_s16x4_neon(const int16_t c0, const int16_t c1,
                                          const int16_t c2, const int16_t c3) {
  return vcreate_s16((uint16_t)c0 | ((uint32_t)c1 << 16) |
                     ((int64_t)(uint16_t)c2 << 32) | ((int64_t)c3 << 48));
}

static INLINE int32x2_t create_s32x2_neon(const int32_t c0, const int32_t c1) {
  return vcreate_s32((uint32_t)c0 | ((int64_t)(uint32_t)c1 << 32));
}

static INLINE int32x4_t create_s32x4_neon(const int32_t c0, const int32_t c1,
                                          const int32_t c2, const int32_t c3) {
  return vcombine_s32(create_s32x2_neon(c0, c1), create_s32x2_neon(c2, c3));
}

// Helper functions used to load tran_low_t into int16, narrowing if necessary.
static INLINE int16x8x2_t load_tran_low_to_s16x2q(const tran_low_t *buf) {
#if CONFIG_VP9_HIGHBITDEPTH
  const int32x4x2_t v0 = vld2q_s32(buf);
  const int32x4x2_t v1 = vld2q_s32(buf + 8);
  const int16x4_t s0 = vmovn_s32(v0.val[0]);
  const int16x4_t s1 = vmovn_s32(v0.val[1]);
  const int16x4_t s2 = vmovn_s32(v1.val[0]);
  const int16x4_t s3 = vmovn_s32(v1.val[1]);
  int16x8x2_t res;
  res.val[0] = vcombine_s16(s0, s2);
  res.val[1] = vcombine_s16(s1, s3);
  return res;
#else
  return vld2q_s16(buf);
#endif
}

static INLINE int16x8_t load_tran_low_to_s16q(const tran_low_t *buf) {
#if CONFIG_VP9_HIGHBITDEPTH
  const int32x4_t v0 = vld1q_s32(buf);
  const int32x4_t v1 = vld1q_s32(buf + 4);
  const int16x4_t s0 = vmovn_s32(v0);
  const int16x4_t s1 = vmovn_s32(v1);
  return vcombine_s16(s0, s1);
#else
  return vld1q_s16(buf);
#endif
}

static INLINE int16x4_t load_tran_low_to_s16d(const tran_low_t *buf) {
#if CONFIG_VP9_HIGHBITDEPTH
  const int32x4_t v0 = vld1q_s32(buf);
  return vmovn_s32(v0);
#else
  return vld1_s16(buf);
#endif
}

static INLINE void store_s16q_to_tran_low(tran_low_t *buf, const int16x8_t a) {
#if CONFIG_VP9_HIGHBITDEPTH
  const int32x4_t v0 = vmovl_s16(vget_low_s16(a));
  const int32x4_t v1 = vmovl_s16(vget_high_s16(a));
  vst1q_s32(buf, v0);
  vst1q_s32(buf + 4, v1);
#else
  vst1q_s16(buf, a);
#endif
}

#if CONFIG_VP9_HIGHBITDEPTH
static INLINE void store_s32q_to_tran_low(tran_low_t *buf, const int32x4_t a) {
  vst1q_s32(buf, a);
}

static INLINE int32x4_t load_tran_low_to_s32q(const tran_low_t *buf) {
  return vld1q_s32(buf);
}
#endif

// Propagate type information to the compiler. Without this the compiler may
// assume the required alignment of uint32_t (4 bytes) and add alignment hints
// to the memory access.
//
// This is used for functions operating on uint8_t which wish to load or store 4
// values at a time but which may not be on 4 byte boundaries.
static INLINE void uint32_to_mem(uint8_t *buf, uint32_t a) {
  memcpy(buf, &a, 4);
}

// Load 4 contiguous bytes when alignment is not guaranteed.
static INLINE uint8x8_t load_unaligned_u8_4x1(const uint8_t *buf) {
  uint32_t a;
  uint32x2_t a_u32;
  memcpy(&a, buf, 4);
  a_u32 = vdup_n_u32(0);
  a_u32 = vset_lane_u32(a, a_u32, 0);
  return vreinterpret_u8_u32(a_u32);
}

// Load 4 contiguous bytes and replicate across a vector when alignment is not
// guaranteed.
static INLINE uint8x8_t load_replicate_u8_4x1(const uint8_t *buf) {
  uint32_t a;
  memcpy(&a, buf, 4);
  return vreinterpret_u8_u32(vdup_n_u32(a));
}

// Store 4 contiguous bytes from the low half of an 8x8 vector.
static INLINE void store_u8_4x1(uint8_t *buf, uint8x8_t a) {
  vst1_lane_u32((uint32_t *)buf, vreinterpret_u32_u8(a), 0);
}

// Store 4 contiguous bytes from the high half of an 8x8 vector.
static INLINE void store_u8_4x1_high(uint8_t *buf, uint8x8_t a) {
  vst1_lane_u32((uint32_t *)buf, vreinterpret_u32_u8(a), 1);
}

// Load 2 sets of 4 bytes when alignment is not guaranteed.
static INLINE uint8x8_t load_unaligned_u8(const uint8_t *buf,
                                          ptrdiff_t stride) {
  uint32_t a;
  uint32x2_t a_u32;
  if (stride == 4) return vld1_u8(buf);
  memcpy(&a, buf, 4);
  buf += stride;
  a_u32 = vdup_n_u32(a);
  memcpy(&a, buf, 4);
  a_u32 = vset_lane_u32(a, a_u32, 1);
  return vreinterpret_u8_u32(a_u32);
}

// Load 8 bytes when alignment is not guaranteed.
static INLINE uint16x4_t load_unaligned_u16(const uint16_t *buf) {
  uint64_t a;
  uint64x1_t a_u64 = vdup_n_u64(0);
  memcpy(&a, buf, 8);
  a_u64 = vset_lane_u64(a, a_u64, 0);
  return vreinterpret_u16_u64(a_u64);
}

// Load 2 sets of 8 bytes when alignment is not guaranteed.
static INLINE uint16x8_t load_unaligned_u16q(const uint16_t *buf,
                                             ptrdiff_t stride) {
  uint64_t a;
  uint64x2_t a_u64;
  if (stride == 4) return vld1q_u16(buf);
  memcpy(&a, buf, 8);
  buf += stride;
  a_u64 = vdupq_n_u64(a);
  memcpy(&a, buf, 8);
  a_u64 = vsetq_lane_u64(a, a_u64, 1);
  return vreinterpretq_u16_u64(a_u64);
}

// Store 2 sets of 4 bytes when alignment is not guaranteed.
static INLINE void store_unaligned_u8(uint8_t *buf, ptrdiff_t stride,
                                      const uint8x8_t a) {
  const uint32x2_t a_u32 = vreinterpret_u32_u8(a);
  if (stride == 4) {
    vst1_u8(buf, a);
    return;
  }
  uint32_to_mem(buf, vget_lane_u32(a_u32, 0));
  buf += stride;
  uint32_to_mem(buf, vget_lane_u32(a_u32, 1));
}

// Load 4 sets of 4 bytes when alignment is not guaranteed.
static INLINE uint8x16_t load_unaligned_u8q(const uint8_t *buf,
                                            ptrdiff_t stride) {
  uint32_t a;
  uint32x4_t a_u32;
  if (stride == 4) return vld1q_u8(buf);
  memcpy(&a, buf, 4);
  buf += stride;
  a_u32 = vdupq_n_u32(a);
  memcpy(&a, buf, 4);
  buf += stride;
  a_u32 = vsetq_lane_u32(a, a_u32, 1);
  memcpy(&a, buf, 4);
  buf += stride;
  a_u32 = vsetq_lane_u32(a, a_u32, 2);
  memcpy(&a, buf, 4);
  buf += stride;
  a_u32 = vsetq_lane_u32(a, a_u32, 3);
  return vreinterpretq_u8_u32(a_u32);
}

// Store 4 sets of 4 bytes when alignment is not guaranteed.
static INLINE void store_unaligned_u8q(uint8_t *buf, ptrdiff_t stride,
                                       const uint8x16_t a) {
  const uint32x4_t a_u32 = vreinterpretq_u32_u8(a);
  if (stride == 4) {
    vst1q_u8(buf, a);
    return;
  }
  uint32_to_mem(buf, vgetq_lane_u32(a_u32, 0));
  buf += stride;
  uint32_to_mem(buf, vgetq_lane_u32(a_u32, 1));
  buf += stride;
  uint32_to_mem(buf, vgetq_lane_u32(a_u32, 2));
  buf += stride;
  uint32_to_mem(buf, vgetq_lane_u32(a_u32, 3));
}

// Load 2 sets of 4 bytes when alignment is guaranteed.
static INLINE uint8x8_t load_u8(const uint8_t *buf, ptrdiff_t stride) {
  uint32x2_t a = vdup_n_u32(0);

  assert(!((intptr_t)buf % sizeof(uint32_t)));
  assert(!(stride % sizeof(uint32_t)));

  a = vld1_lane_u32((const uint32_t *)buf, a, 0);
  buf += stride;
  a = vld1_lane_u32((const uint32_t *)buf, a, 1);
  return vreinterpret_u8_u32(a);
}

// Store 2 sets of 4 bytes when alignment is guaranteed.
static INLINE void store_u8(uint8_t *buf, ptrdiff_t stride, const uint8x8_t a) {
  uint32x2_t a_u32 = vreinterpret_u32_u8(a);

  assert(!((intptr_t)buf % sizeof(uint32_t)));
  assert(!(stride % sizeof(uint32_t)));

  vst1_lane_u32((uint32_t *)buf, a_u32, 0);
  buf += stride;
  vst1_lane_u32((uint32_t *)buf, a_u32, 1);
}

static INLINE void load_u8_8x4(const uint8_t *s, const ptrdiff_t p,
                               uint8x8_t *const s0, uint8x8_t *const s1,
                               uint8x8_t *const s2, uint8x8_t *const s3) {
  *s0 = vld1_u8(s);
  s += p;
  *s1 = vld1_u8(s);
  s += p;
  *s2 = vld1_u8(s);
  s += p;
  *s3 = vld1_u8(s);
}

static INLINE void store_u8_8x4(uint8_t *s, const ptrdiff_t p,
                                const uint8x8_t s0, const uint8x8_t s1,
                                const uint8x8_t s2, const uint8x8_t s3) {
  vst1_u8(s, s0);
  s += p;
  vst1_u8(s, s1);
  s += p;
  vst1_u8(s, s2);
  s += p;
  vst1_u8(s, s3);
}

static INLINE void load_u8_16x4(const uint8_t *s, const ptrdiff_t p,
                                uint8x16_t *const s0, uint8x16_t *const s1,
                                uint8x16_t *const s2, uint8x16_t *const s3) {
  *s0 = vld1q_u8(s);
  s += p;
  *s1 = vld1q_u8(s);
  s += p;
  *s2 = vld1q_u8(s);
  s += p;
  *s3 = vld1q_u8(s);
}

static INLINE void store_u8_16x4(uint8_t *s, const ptrdiff_t p,
                                 const uint8x16_t s0, const uint8x16_t s1,
                                 const uint8x16_t s2, const uint8x16_t s3) {
  vst1q_u8(s, s0);
  s += p;
  vst1q_u8(s, s1);
  s += p;
  vst1q_u8(s, s2);
  s += p;
  vst1q_u8(s, s3);
}

static INLINE void load_u8_8x7(const uint8_t *s, const ptrdiff_t p,
                               uint8x8_t *const s0, uint8x8_t *const s1,
                               uint8x8_t *const s2, uint8x8_t *const s3,
                               uint8x8_t *const s4, uint8x8_t *const s5,
                               uint8x8_t *const s6) {
  *s0 = vld1_u8(s);
  s += p;
  *s1 = vld1_u8(s);
  s += p;
  *s2 = vld1_u8(s);
  s += p;
  *s3 = vld1_u8(s);
  s += p;
  *s4 = vld1_u8(s);
  s += p;
  *s5 = vld1_u8(s);
  s += p;
  *s6 = vld1_u8(s);
}

static INLINE void load_u8_8x8(const uint8_t *s, const ptrdiff_t p,
                               uint8x8_t *const s0, uint8x8_t *const s1,
                               uint8x8_t *const s2, uint8x8_t *const s3,
                               uint8x8_t *const s4, uint8x8_t *const s5,
                               uint8x8_t *const s6, uint8x8_t *const s7) {
  *s0 = vld1_u8(s);
  s += p;
  *s1 = vld1_u8(s);
  s += p;
  *s2 = vld1_u8(s);
  s += p;
  *s3 = vld1_u8(s);
  s += p;
  *s4 = vld1_u8(s);
  s += p;
  *s5 = vld1_u8(s);
  s += p;
  *s6 = vld1_u8(s);
  s += p;
  *s7 = vld1_u8(s);
}

static INLINE void store_u8_8x8(uint8_t *s, const ptrdiff_t p,
                                const uint8x8_t s0, const uint8x8_t s1,
                                const uint8x8_t s2, const uint8x8_t s3,
                                const uint8x8_t s4, const uint8x8_t s5,
                                const uint8x8_t s6, const uint8x8_t s7) {
  vst1_u8(s, s0);
  s += p;
  vst1_u8(s, s1);
  s += p;
  vst1_u8(s, s2);
  s += p;
  vst1_u8(s, s3);
  s += p;
  vst1_u8(s, s4);
  s += p;
  vst1_u8(s, s5);
  s += p;
  vst1_u8(s, s6);
  s += p;
  vst1_u8(s, s7);
}

static INLINE void load_u8_16x8(const uint8_t *s, const ptrdiff_t p,
                                uint8x16_t *const s0, uint8x16_t *const s1,
                                uint8x16_t *const s2, uint8x16_t *const s3,
                                uint8x16_t *const s4, uint8x16_t *const s5,
                                uint8x16_t *const s6, uint8x16_t *const s7) {
  *s0 = vld1q_u8(s);
  s += p;
  *s1 = vld1q_u8(s);
  s += p;
  *s2 = vld1q_u8(s);
  s += p;
  *s3 = vld1q_u8(s);
  s += p;
  *s4 = vld1q_u8(s);
  s += p;
  *s5 = vld1q_u8(s);
  s += p;
  *s6 = vld1q_u8(s);
  s += p;
  *s7 = vld1q_u8(s);
}

static INLINE void store_u8_16x8(uint8_t *s, const ptrdiff_t p,
                                 const uint8x16_t s0, const uint8x16_t s1,
                                 const uint8x16_t s2, const uint8x16_t s3,
                                 const uint8x16_t s4, const uint8x16_t s5,
                                 const uint8x16_t s6, const uint8x16_t s7) {
  vst1q_u8(s, s0);
  s += p;
  vst1q_u8(s, s1);
  s += p;
  vst1q_u8(s, s2);
  s += p;
  vst1q_u8(s, s3);
  s += p;
  vst1q_u8(s, s4);
  s += p;
  vst1q_u8(s, s5);
  s += p;
  vst1q_u8(s, s6);
  s += p;
  vst1q_u8(s, s7);
}

static INLINE void load_u16_8x8(const uint16_t *s, const ptrdiff_t p,
                                uint16x8_t *s0, uint16x8_t *s1, uint16x8_t *s2,
                                uint16x8_t *s3, uint16x8_t *s4, uint16x8_t *s5,
                                uint16x8_t *s6, uint16x8_t *s7) {
  *s0 = vld1q_u16(s);
  s += p;
  *s1 = vld1q_u16(s);
  s += p;
  *s2 = vld1q_u16(s);
  s += p;
  *s3 = vld1q_u16(s);
  s += p;
  *s4 = vld1q_u16(s);
  s += p;
  *s5 = vld1q_u16(s);
  s += p;
  *s6 = vld1q_u16(s);
  s += p;
  *s7 = vld1q_u16(s);
}

#endif  // VPX_VPX_DSP_ARM_MEM_NEON_H_