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
|
/*
* Copyright (c) 2018, 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 <immintrin.h>
#include <smmintrin.h>
#include "aom_dsp/x86/synonyms.h"
#include "aom_dsp/x86/synonyms_avx2.h"
#include "aom_dsp/x86/sum_squares_sse2.h"
#include "config/aom_dsp_rtcd.h"
static uint64_t aom_sum_squares_2d_i16_nxn_avx2(const int16_t *src, int stride,
int width, int height) {
uint64_t result;
__m256i v_acc_q = _mm256_setzero_si256();
const __m256i v_zext_mask_q = yy_set1_64_from_32i(~0);
for (int col = 0; col < height; col += 4) {
__m256i v_acc_d = _mm256_setzero_si256();
for (int row = 0; row < width; row += 16) {
const int16_t *tempsrc = src + row;
const __m256i v_val_0_w =
_mm256_loadu_si256((const __m256i *)(tempsrc + 0 * stride));
const __m256i v_val_1_w =
_mm256_loadu_si256((const __m256i *)(tempsrc + 1 * stride));
const __m256i v_val_2_w =
_mm256_loadu_si256((const __m256i *)(tempsrc + 2 * stride));
const __m256i v_val_3_w =
_mm256_loadu_si256((const __m256i *)(tempsrc + 3 * stride));
const __m256i v_sq_0_d = _mm256_madd_epi16(v_val_0_w, v_val_0_w);
const __m256i v_sq_1_d = _mm256_madd_epi16(v_val_1_w, v_val_1_w);
const __m256i v_sq_2_d = _mm256_madd_epi16(v_val_2_w, v_val_2_w);
const __m256i v_sq_3_d = _mm256_madd_epi16(v_val_3_w, v_val_3_w);
const __m256i v_sum_01_d = _mm256_add_epi32(v_sq_0_d, v_sq_1_d);
const __m256i v_sum_23_d = _mm256_add_epi32(v_sq_2_d, v_sq_3_d);
const __m256i v_sum_0123_d = _mm256_add_epi32(v_sum_01_d, v_sum_23_d);
v_acc_d = _mm256_add_epi32(v_acc_d, v_sum_0123_d);
}
v_acc_q =
_mm256_add_epi64(v_acc_q, _mm256_and_si256(v_acc_d, v_zext_mask_q));
v_acc_q = _mm256_add_epi64(v_acc_q, _mm256_srli_epi64(v_acc_d, 32));
src += 4 * stride;
}
__m128i lower_64_2_Value = _mm256_castsi256_si128(v_acc_q);
__m128i higher_64_2_Value = _mm256_extracti128_si256(v_acc_q, 1);
__m128i result_64_2_int = _mm_add_epi64(lower_64_2_Value, higher_64_2_Value);
result_64_2_int = _mm_add_epi64(
result_64_2_int, _mm_unpackhi_epi64(result_64_2_int, result_64_2_int));
xx_storel_64(&result, result_64_2_int);
return result;
}
uint64_t aom_sum_squares_2d_i16_avx2(const int16_t *src, int stride, int width,
int height) {
if (LIKELY(width == 4 && height == 4)) {
return aom_sum_squares_2d_i16_4x4_sse2(src, stride);
} else if (LIKELY(width == 4 && (height & 3) == 0)) {
return aom_sum_squares_2d_i16_4xn_sse2(src, stride, height);
} else if (LIKELY(width == 8 && (height & 3) == 0)) {
return aom_sum_squares_2d_i16_nxn_sse2(src, stride, width, height);
} else if (LIKELY(((width & 15) == 0) && ((height & 3) == 0))) {
return aom_sum_squares_2d_i16_nxn_avx2(src, stride, width, height);
} else {
return aom_sum_squares_2d_i16_c(src, stride, width, height);
}
}
static uint64_t aom_sum_sse_2d_i16_nxn_avx2(const int16_t *src, int stride,
int width, int height, int *sum) {
uint64_t result;
const __m256i zero_reg = _mm256_setzero_si256();
const __m256i one_reg = _mm256_set1_epi16(1);
__m256i v_sse_total = zero_reg;
__m256i v_sum_total = zero_reg;
for (int col = 0; col < height; col += 4) {
__m256i v_sse_row = zero_reg;
for (int row = 0; row < width; row += 16) {
const int16_t *tempsrc = src + row;
const __m256i v_val_0_w =
_mm256_loadu_si256((const __m256i *)(tempsrc + 0 * stride));
const __m256i v_val_1_w =
_mm256_loadu_si256((const __m256i *)(tempsrc + 1 * stride));
const __m256i v_val_2_w =
_mm256_loadu_si256((const __m256i *)(tempsrc + 2 * stride));
const __m256i v_val_3_w =
_mm256_loadu_si256((const __m256i *)(tempsrc + 3 * stride));
const __m256i v_sum_01 = _mm256_add_epi16(v_val_0_w, v_val_1_w);
const __m256i v_sum_23 = _mm256_add_epi16(v_val_2_w, v_val_3_w);
__m256i v_sum_0123 = _mm256_add_epi16(v_sum_01, v_sum_23);
v_sum_0123 = _mm256_madd_epi16(v_sum_0123, one_reg);
v_sum_total = _mm256_add_epi32(v_sum_total, v_sum_0123);
const __m256i v_sq_0_d = _mm256_madd_epi16(v_val_0_w, v_val_0_w);
const __m256i v_sq_1_d = _mm256_madd_epi16(v_val_1_w, v_val_1_w);
const __m256i v_sq_2_d = _mm256_madd_epi16(v_val_2_w, v_val_2_w);
const __m256i v_sq_3_d = _mm256_madd_epi16(v_val_3_w, v_val_3_w);
const __m256i v_sq_01_d = _mm256_add_epi32(v_sq_0_d, v_sq_1_d);
const __m256i v_sq_23_d = _mm256_add_epi32(v_sq_2_d, v_sq_3_d);
const __m256i v_sq_0123_d = _mm256_add_epi32(v_sq_01_d, v_sq_23_d);
v_sse_row = _mm256_add_epi32(v_sse_row, v_sq_0123_d);
}
const __m256i v_sse_row_low = _mm256_unpacklo_epi32(v_sse_row, zero_reg);
const __m256i v_sse_row_hi = _mm256_unpackhi_epi32(v_sse_row, zero_reg);
v_sse_row = _mm256_add_epi64(v_sse_row_low, v_sse_row_hi);
v_sse_total = _mm256_add_epi64(v_sse_total, v_sse_row);
src += 4 * stride;
}
const __m128i v_sum_total_low = _mm256_castsi256_si128(v_sum_total);
const __m128i v_sum_total_hi = _mm256_extracti128_si256(v_sum_total, 1);
__m128i sum_128bit = _mm_add_epi32(v_sum_total_hi, v_sum_total_low);
sum_128bit = _mm_add_epi32(sum_128bit, _mm_srli_si128(sum_128bit, 8));
sum_128bit = _mm_add_epi32(sum_128bit, _mm_srli_si128(sum_128bit, 4));
*sum += _mm_cvtsi128_si32(sum_128bit);
__m128i v_sse_total_lo = _mm256_castsi256_si128(v_sse_total);
__m128i v_sse_total_hi = _mm256_extracti128_si256(v_sse_total, 1);
__m128i sse_128bit = _mm_add_epi64(v_sse_total_lo, v_sse_total_hi);
sse_128bit =
_mm_add_epi64(sse_128bit, _mm_unpackhi_epi64(sse_128bit, sse_128bit));
xx_storel_64(&result, sse_128bit);
return result;
}
uint64_t aom_sum_sse_2d_i16_avx2(const int16_t *src, int src_stride, int width,
int height, int *sum) {
if (LIKELY(width == 4 && height == 4)) {
return aom_sum_sse_2d_i16_4x4_sse2(src, src_stride, sum);
} else if (LIKELY(width == 4 && (height & 3) == 0)) {
return aom_sum_sse_2d_i16_4xn_sse2(src, src_stride, height, sum);
} else if (LIKELY(width == 8 && (height & 3) == 0)) {
return aom_sum_sse_2d_i16_nxn_sse2(src, src_stride, width, height, sum);
} else if (LIKELY(((width & 15) == 0) && ((height & 3) == 0))) {
return aom_sum_sse_2d_i16_nxn_avx2(src, src_stride, width, height, sum);
} else {
return aom_sum_sse_2d_i16_c(src, src_stride, width, height, sum);
}
}
// Accumulate sum of 16-bit elements in the vector
static AOM_INLINE int32_t mm256_accumulate_epi16(__m256i vec_a) {
__m128i vtmp1 = _mm256_extracti128_si256(vec_a, 1);
__m128i vtmp2 = _mm256_castsi256_si128(vec_a);
vtmp1 = _mm_add_epi16(vtmp1, vtmp2);
vtmp2 = _mm_srli_si128(vtmp1, 8);
vtmp1 = _mm_add_epi16(vtmp1, vtmp2);
vtmp2 = _mm_srli_si128(vtmp1, 4);
vtmp1 = _mm_add_epi16(vtmp1, vtmp2);
vtmp2 = _mm_srli_si128(vtmp1, 2);
vtmp1 = _mm_add_epi16(vtmp1, vtmp2);
return _mm_extract_epi16(vtmp1, 0);
}
// Accumulate sum of 32-bit elements in the vector
static AOM_INLINE int32_t mm256_accumulate_epi32(__m256i vec_a) {
__m128i vtmp1 = _mm256_extracti128_si256(vec_a, 1);
__m128i vtmp2 = _mm256_castsi256_si128(vec_a);
vtmp1 = _mm_add_epi32(vtmp1, vtmp2);
vtmp2 = _mm_srli_si128(vtmp1, 8);
vtmp1 = _mm_add_epi32(vtmp1, vtmp2);
vtmp2 = _mm_srli_si128(vtmp1, 4);
vtmp1 = _mm_add_epi32(vtmp1, vtmp2);
return _mm_cvtsi128_si32(vtmp1);
}
uint64_t aom_var_2d_u8_avx2(uint8_t *src, int src_stride, int width,
int height) {
uint8_t *srcp;
uint64_t s = 0, ss = 0;
__m256i vzero = _mm256_setzero_si256();
__m256i v_acc_sum = vzero;
__m256i v_acc_sqs = vzero;
int i, j;
// Process 32 elements in a row
for (i = 0; i < width - 31; i += 32) {
srcp = src + i;
// Process 8 columns at a time
for (j = 0; j < height - 7; j += 8) {
__m256i vsrc[8];
for (int k = 0; k < 8; k++) {
vsrc[k] = _mm256_loadu_si256((__m256i *)srcp);
srcp += src_stride;
}
for (int k = 0; k < 8; k++) {
__m256i vsrc0 = _mm256_unpacklo_epi8(vsrc[k], vzero);
__m256i vsrc1 = _mm256_unpackhi_epi8(vsrc[k], vzero);
v_acc_sum = _mm256_add_epi16(v_acc_sum, vsrc0);
v_acc_sum = _mm256_add_epi16(v_acc_sum, vsrc1);
__m256i vsqs0 = _mm256_madd_epi16(vsrc0, vsrc0);
__m256i vsqs1 = _mm256_madd_epi16(vsrc1, vsrc1);
v_acc_sqs = _mm256_add_epi32(v_acc_sqs, vsqs0);
v_acc_sqs = _mm256_add_epi32(v_acc_sqs, vsqs1);
}
// Update total sum and clear the vectors
s += mm256_accumulate_epi16(v_acc_sum);
ss += mm256_accumulate_epi32(v_acc_sqs);
v_acc_sum = vzero;
v_acc_sqs = vzero;
}
// Process remaining rows (height not a multiple of 8)
for (; j < height; j++) {
__m256i vsrc = _mm256_loadu_si256((__m256i *)srcp);
__m256i vsrc0 = _mm256_unpacklo_epi8(vsrc, vzero);
__m256i vsrc1 = _mm256_unpackhi_epi8(vsrc, vzero);
v_acc_sum = _mm256_add_epi16(v_acc_sum, vsrc0);
v_acc_sum = _mm256_add_epi16(v_acc_sum, vsrc1);
__m256i vsqs0 = _mm256_madd_epi16(vsrc0, vsrc0);
__m256i vsqs1 = _mm256_madd_epi16(vsrc1, vsrc1);
v_acc_sqs = _mm256_add_epi32(v_acc_sqs, vsqs0);
v_acc_sqs = _mm256_add_epi32(v_acc_sqs, vsqs1);
srcp += src_stride;
}
// Update total sum and clear the vectors
s += mm256_accumulate_epi16(v_acc_sum);
ss += mm256_accumulate_epi32(v_acc_sqs);
v_acc_sum = vzero;
v_acc_sqs = vzero;
}
// Process the remaining area using C
srcp = src;
for (int k = 0; k < height; k++) {
for (int m = i; m < width; m++) {
uint8_t val = srcp[m];
s += val;
ss += val * val;
}
srcp += src_stride;
}
return (ss - s * s / (width * height));
}
uint64_t aom_var_2d_u16_avx2(uint8_t *src, int src_stride, int width,
int height) {
uint16_t *srcp1 = CONVERT_TO_SHORTPTR(src), *srcp;
uint64_t s = 0, ss = 0;
__m256i vzero = _mm256_setzero_si256();
__m256i v_acc_sum = vzero;
__m256i v_acc_sqs = vzero;
int i, j;
// Process 16 elements in a row
for (i = 0; i < width - 15; i += 16) {
srcp = srcp1 + i;
// Process 8 columns at a time
for (j = 0; j < height - 8; j += 8) {
__m256i vsrc[8];
for (int k = 0; k < 8; k++) {
vsrc[k] = _mm256_loadu_si256((__m256i *)srcp);
srcp += src_stride;
}
for (int k = 0; k < 8; k++) {
__m256i vsrc0 = _mm256_unpacklo_epi16(vsrc[k], vzero);
__m256i vsrc1 = _mm256_unpackhi_epi16(vsrc[k], vzero);
v_acc_sum = _mm256_add_epi32(vsrc0, v_acc_sum);
v_acc_sum = _mm256_add_epi32(vsrc1, v_acc_sum);
__m256i vsqs0 = _mm256_madd_epi16(vsrc[k], vsrc[k]);
v_acc_sqs = _mm256_add_epi32(v_acc_sqs, vsqs0);
}
// Update total sum and clear the vectors
s += mm256_accumulate_epi32(v_acc_sum);
ss += mm256_accumulate_epi32(v_acc_sqs);
v_acc_sum = vzero;
v_acc_sqs = vzero;
}
// Process remaining rows (height not a multiple of 8)
for (; j < height; j++) {
__m256i vsrc = _mm256_loadu_si256((__m256i *)srcp);
__m256i vsrc0 = _mm256_unpacklo_epi16(vsrc, vzero);
__m256i vsrc1 = _mm256_unpackhi_epi16(vsrc, vzero);
v_acc_sum = _mm256_add_epi32(vsrc0, v_acc_sum);
v_acc_sum = _mm256_add_epi32(vsrc1, v_acc_sum);
__m256i vsqs0 = _mm256_madd_epi16(vsrc, vsrc);
v_acc_sqs = _mm256_add_epi32(v_acc_sqs, vsqs0);
srcp += src_stride;
}
// Update total sum and clear the vectors
s += mm256_accumulate_epi32(v_acc_sum);
ss += mm256_accumulate_epi32(v_acc_sqs);
v_acc_sum = vzero;
v_acc_sqs = vzero;
}
// Process the remaining area using C
srcp = srcp1;
for (int k = 0; k < height; k++) {
for (int m = i; m < width; m++) {
uint16_t val = srcp[m];
s += val;
ss += val * val;
}
srcp += src_stride;
}
return (ss - s * s / (width * height));
}
|