xref: /aosp_15_r20/external/libaom/aom_dsp/arm/avg_sve.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2023, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <arm_neon.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_dsp_rtcd.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/arm/aom_neon_sve_bridge.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/arm/mem_neon.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem.h"
21*77c1e3ccSAndroid Build Coastguard Worker 
aom_vector_var_sve(const int16_t * ref,const int16_t * src,int bwl)22*77c1e3ccSAndroid Build Coastguard Worker int aom_vector_var_sve(const int16_t *ref, const int16_t *src, int bwl) {
23*77c1e3ccSAndroid Build Coastguard Worker   assert(bwl >= 2 && bwl <= 5);
24*77c1e3ccSAndroid Build Coastguard Worker   int width = 4 << bwl;
25*77c1e3ccSAndroid Build Coastguard Worker 
26*77c1e3ccSAndroid Build Coastguard Worker   int64x2_t sse_s64[2] = { vdupq_n_s64(0), vdupq_n_s64(0) };
27*77c1e3ccSAndroid Build Coastguard Worker   int16x8_t v_mean[2] = { vdupq_n_s16(0), vdupq_n_s16(0) };
28*77c1e3ccSAndroid Build Coastguard Worker 
29*77c1e3ccSAndroid Build Coastguard Worker   do {
30*77c1e3ccSAndroid Build Coastguard Worker     int16x8_t r0 = vld1q_s16(ref);
31*77c1e3ccSAndroid Build Coastguard Worker     int16x8_t s0 = vld1q_s16(src);
32*77c1e3ccSAndroid Build Coastguard Worker 
33*77c1e3ccSAndroid Build Coastguard Worker     // diff: dynamic range [-510, 510] 10 (signed) bits.
34*77c1e3ccSAndroid Build Coastguard Worker     int16x8_t diff0 = vsubq_s16(r0, s0);
35*77c1e3ccSAndroid Build Coastguard Worker     // v_mean: dynamic range 16 * diff -> [-8160, 8160], 14 (signed) bits.
36*77c1e3ccSAndroid Build Coastguard Worker     v_mean[0] = vaddq_s16(v_mean[0], diff0);
37*77c1e3ccSAndroid Build Coastguard Worker 
38*77c1e3ccSAndroid Build Coastguard Worker     // v_sse: dynamic range 2 * 16 * diff^2 -> [0, 8,323,200], 24 (signed) bits.
39*77c1e3ccSAndroid Build Coastguard Worker     sse_s64[0] = aom_sdotq_s16(sse_s64[0], diff0, diff0);
40*77c1e3ccSAndroid Build Coastguard Worker 
41*77c1e3ccSAndroid Build Coastguard Worker     int16x8_t r1 = vld1q_s16(ref + 8);
42*77c1e3ccSAndroid Build Coastguard Worker     int16x8_t s1 = vld1q_s16(src + 8);
43*77c1e3ccSAndroid Build Coastguard Worker 
44*77c1e3ccSAndroid Build Coastguard Worker     // diff: dynamic range [-510, 510] 10 (signed) bits.
45*77c1e3ccSAndroid Build Coastguard Worker     int16x8_t diff1 = vsubq_s16(r1, s1);
46*77c1e3ccSAndroid Build Coastguard Worker     // v_mean: dynamic range 16 * diff -> [-8160, 8160], 14 (signed) bits.
47*77c1e3ccSAndroid Build Coastguard Worker     v_mean[1] = vaddq_s16(v_mean[1], diff1);
48*77c1e3ccSAndroid Build Coastguard Worker 
49*77c1e3ccSAndroid Build Coastguard Worker     // v_sse: dynamic range 2 * 16 * diff^2 -> [0, 8,323,200], 24 (signed) bits.
50*77c1e3ccSAndroid Build Coastguard Worker     sse_s64[1] = aom_sdotq_s16(sse_s64[1], diff1, diff1);
51*77c1e3ccSAndroid Build Coastguard Worker 
52*77c1e3ccSAndroid Build Coastguard Worker     ref += 16;
53*77c1e3ccSAndroid Build Coastguard Worker     src += 16;
54*77c1e3ccSAndroid Build Coastguard Worker     width -= 16;
55*77c1e3ccSAndroid Build Coastguard Worker   } while (width != 0);
56*77c1e3ccSAndroid Build Coastguard Worker 
57*77c1e3ccSAndroid Build Coastguard Worker   // Dynamic range [0, 65280], 16 (unsigned) bits.
58*77c1e3ccSAndroid Build Coastguard Worker   const uint32_t mean_abs = abs(vaddlvq_s16(vaddq_s16(v_mean[0], v_mean[1])));
59*77c1e3ccSAndroid Build Coastguard Worker   const int64_t sse = vaddvq_s64(vaddq_s64(sse_s64[0], sse_s64[1]));
60*77c1e3ccSAndroid Build Coastguard Worker 
61*77c1e3ccSAndroid Build Coastguard Worker   // (mean_abs * mean_abs): dynamic range 32 (unsigned) bits.
62*77c1e3ccSAndroid Build Coastguard Worker   return (int)(sse - ((mean_abs * mean_abs) >> (bwl + 2)));
63*77c1e3ccSAndroid Build Coastguard Worker }
64