/src/aom/av1/encoder/x86/rdopt_avx2.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2018, Alliance for Open Media. All rights reserved. |
3 | | * |
4 | | * This source code is subject to the terms of the BSD 2 Clause License and |
5 | | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
6 | | * was not distributed with this source code in the LICENSE file, you can |
7 | | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
8 | | * Media Patent License 1.0 was not distributed with this source code in the |
9 | | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
10 | | */ |
11 | | |
12 | | #include <assert.h> |
13 | | #include <immintrin.h> |
14 | | #include "aom_dsp/x86/mem_sse2.h" |
15 | | #include "aom_dsp/x86/synonyms_avx2.h" |
16 | | |
17 | | #include "config/av1_rtcd.h" |
18 | | #include "av1/encoder/rdopt.h" |
19 | | |
20 | | // Process horizontal and vertical correlations in a 4x4 block of pixels. |
21 | | // We actually use the 4x4 pixels to calculate correlations corresponding to |
22 | | // the top-left 3x3 pixels, so this function must be called with 1x1 overlap, |
23 | | // moving the window along/down by 3 pixels at a time. |
24 | | static inline void horver_correlation_4x4(const int16_t *diff, int stride, |
25 | | __m256i *xy_sum_32, |
26 | | __m256i *xz_sum_32, __m256i *x_sum_32, |
27 | 0 | __m256i *x2_sum_32) { |
28 | | // Pixels in this 4x4 [ a b c d ] |
29 | | // are referred to as: [ e f g h ] |
30 | | // [ i j k l ] |
31 | | // [ m n o p ] |
32 | |
|
33 | 0 | const __m256i pixels = _mm256_set_epi64x( |
34 | 0 | loadu_int64(&diff[0 * stride]), loadu_int64(&diff[1 * stride]), |
35 | 0 | loadu_int64(&diff[2 * stride]), loadu_int64(&diff[3 * stride])); |
36 | | // pixels = [d c b a h g f e] [l k j i p o n m] as i16 |
37 | |
|
38 | 0 | const __m256i slli = _mm256_slli_epi64(pixels, 16); |
39 | | // slli = [c b a 0 g f e 0] [k j i 0 o n m 0] as i16 |
40 | |
|
41 | 0 | const __m256i madd_xy = _mm256_madd_epi16(pixels, slli); |
42 | | // madd_xy = [bc+cd ab fg+gh ef] [jk+kl ij no+op mn] as i32 |
43 | 0 | *xy_sum_32 = _mm256_add_epi32(*xy_sum_32, madd_xy); |
44 | | |
45 | | // Permute control [3 2] [1 0] => [2 1] [0 0], 0b10010000 = 0x90 |
46 | 0 | const __m256i perm = _mm256_permute4x64_epi64(slli, 0x90); |
47 | | // perm = [g f e 0 k j i 0] [o n m 0 o n m 0] as i16 |
48 | |
|
49 | 0 | const __m256i madd_xz = _mm256_madd_epi16(slli, perm); |
50 | | // madd_xz = [cg+bf ae gk+fj ei] [ko+jn im oo+nn mm] as i32 |
51 | 0 | *xz_sum_32 = _mm256_add_epi32(*xz_sum_32, madd_xz); |
52 | | |
53 | | // Sum every element in slli (and then also their squares) |
54 | 0 | const __m256i madd1_slli = _mm256_madd_epi16(slli, _mm256_set1_epi16(1)); |
55 | | // madd1_slli = [c+b a g+f e] [k+j i o+n m] as i32 |
56 | 0 | *x_sum_32 = _mm256_add_epi32(*x_sum_32, madd1_slli); |
57 | |
|
58 | 0 | const __m256i madd_slli = _mm256_madd_epi16(slli, slli); |
59 | | // madd_slli = [cc+bb aa gg+ff ee] [kk+jj ii oo+nn mm] as i32 |
60 | 0 | *x2_sum_32 = _mm256_add_epi32(*x2_sum_32, madd_slli); |
61 | 0 | } |
62 | | |
63 | | void av1_get_horver_correlation_full_avx2(const int16_t *diff, int stride, |
64 | | int width, int height, float *hcorr, |
65 | 0 | float *vcorr) { |
66 | | // The following notation is used: |
67 | | // x - current pixel |
68 | | // y - right neighbour pixel |
69 | | // z - below neighbour pixel |
70 | | // w - down-right neighbour pixel |
71 | 0 | int64_t xy_sum = 0, xz_sum = 0; |
72 | 0 | int64_t x_sum = 0, x2_sum = 0; |
73 | | |
74 | | // Process horizontal and vertical correlations through the body in 4x4 |
75 | | // blocks. This excludes the final row and column and possibly one extra |
76 | | // column depending how 3 divides into width and height |
77 | 0 | int32_t xy_xz_tmp[8] = { 0 }, x_x2_tmp[8] = { 0 }; |
78 | 0 | __m256i xy_sum_32 = _mm256_setzero_si256(); |
79 | 0 | __m256i xz_sum_32 = _mm256_setzero_si256(); |
80 | 0 | __m256i x_sum_32 = _mm256_setzero_si256(); |
81 | 0 | __m256i x2_sum_32 = _mm256_setzero_si256(); |
82 | 0 | for (int i = 0; i <= height - 4; i += 3) { |
83 | 0 | for (int j = 0; j <= width - 4; j += 3) { |
84 | 0 | horver_correlation_4x4(&diff[i * stride + j], stride, &xy_sum_32, |
85 | 0 | &xz_sum_32, &x_sum_32, &x2_sum_32); |
86 | 0 | } |
87 | 0 | const __m256i hadd_xy_xz = _mm256_hadd_epi32(xy_sum_32, xz_sum_32); |
88 | | // hadd_xy_xz = [ae+bf+cg ei+fj+gk ab+bc+cd ef+fg+gh] |
89 | | // [im+jn+ko mm+nn+oo ij+jk+kl mn+no+op] as i32 |
90 | 0 | yy_storeu_256(xy_xz_tmp, hadd_xy_xz); |
91 | 0 | xy_sum += (int64_t)xy_xz_tmp[5] + xy_xz_tmp[4] + xy_xz_tmp[1]; |
92 | 0 | xz_sum += (int64_t)xy_xz_tmp[7] + xy_xz_tmp[6] + xy_xz_tmp[3]; |
93 | |
|
94 | 0 | const __m256i hadd_x_x2 = _mm256_hadd_epi32(x_sum_32, x2_sum_32); |
95 | | // hadd_x_x2 = [aa+bb+cc ee+ff+gg a+b+c e+f+g] |
96 | | // [ii+jj+kk mm+nn+oo i+j+k m+n+o] as i32 |
97 | 0 | yy_storeu_256(x_x2_tmp, hadd_x_x2); |
98 | 0 | x_sum += (int64_t)x_x2_tmp[5] + x_x2_tmp[4] + x_x2_tmp[1]; |
99 | 0 | x2_sum += (int64_t)x_x2_tmp[7] + x_x2_tmp[6] + x_x2_tmp[3]; |
100 | |
|
101 | 0 | xy_sum_32 = _mm256_setzero_si256(); |
102 | 0 | xz_sum_32 = _mm256_setzero_si256(); |
103 | 0 | x_sum_32 = _mm256_setzero_si256(); |
104 | 0 | x2_sum_32 = _mm256_setzero_si256(); |
105 | 0 | } |
106 | | |
107 | | // x_sum now covers every pixel except the final 1-2 rows and 1-2 cols |
108 | 0 | int64_t x_finalrow = 0, x_finalcol = 0, x2_finalrow = 0, x2_finalcol = 0; |
109 | | |
110 | | // Do we have 2 rows remaining or just the one? Note that width and height |
111 | | // are powers of 2, so each modulo 3 must be 1 or 2. |
112 | 0 | if (height % 3 == 1) { // Just horiz corrs on the final row |
113 | 0 | const int16_t x0 = diff[(height - 1) * stride]; |
114 | 0 | x_sum += x0; |
115 | 0 | x_finalrow += x0; |
116 | 0 | x2_sum += x0 * x0; |
117 | 0 | x2_finalrow += x0 * x0; |
118 | 0 | for (int j = 0; j < width - 1; ++j) { |
119 | 0 | const int16_t x = diff[(height - 1) * stride + j]; |
120 | 0 | const int16_t y = diff[(height - 1) * stride + j + 1]; |
121 | 0 | xy_sum += x * y; |
122 | 0 | x_sum += y; |
123 | 0 | x2_sum += y * y; |
124 | 0 | x_finalrow += y; |
125 | 0 | x2_finalrow += y * y; |
126 | 0 | } |
127 | 0 | } else { // Two rows remaining to do |
128 | 0 | const int16_t x0 = diff[(height - 2) * stride]; |
129 | 0 | const int16_t z0 = diff[(height - 1) * stride]; |
130 | 0 | x_sum += x0 + z0; |
131 | 0 | x2_sum += x0 * x0 + z0 * z0; |
132 | 0 | x_finalrow += z0; |
133 | 0 | x2_finalrow += z0 * z0; |
134 | 0 | for (int j = 0; j < width - 1; ++j) { |
135 | 0 | const int16_t x = diff[(height - 2) * stride + j]; |
136 | 0 | const int16_t y = diff[(height - 2) * stride + j + 1]; |
137 | 0 | const int16_t z = diff[(height - 1) * stride + j]; |
138 | 0 | const int16_t w = diff[(height - 1) * stride + j + 1]; |
139 | | |
140 | | // Horizontal and vertical correlations for the penultimate row: |
141 | 0 | xy_sum += x * y; |
142 | 0 | xz_sum += x * z; |
143 | | |
144 | | // Now just horizontal correlations for the final row: |
145 | 0 | xy_sum += z * w; |
146 | |
|
147 | 0 | x_sum += y + w; |
148 | 0 | x2_sum += y * y + w * w; |
149 | 0 | x_finalrow += w; |
150 | 0 | x2_finalrow += w * w; |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | | // Do we have 2 columns remaining or just the one? |
155 | 0 | if (width % 3 == 1) { // Just vert corrs on the final col |
156 | 0 | const int16_t x0 = diff[width - 1]; |
157 | 0 | x_sum += x0; |
158 | 0 | x_finalcol += x0; |
159 | 0 | x2_sum += x0 * x0; |
160 | 0 | x2_finalcol += x0 * x0; |
161 | 0 | for (int i = 0; i < height - 1; ++i) { |
162 | 0 | const int16_t x = diff[i * stride + width - 1]; |
163 | 0 | const int16_t z = diff[(i + 1) * stride + width - 1]; |
164 | 0 | xz_sum += x * z; |
165 | 0 | x_finalcol += z; |
166 | 0 | x2_finalcol += z * z; |
167 | | // So the bottom-right elements don't get counted twice: |
168 | 0 | if (i < height - (height % 3 == 1 ? 2 : 3)) { |
169 | 0 | x_sum += z; |
170 | 0 | x2_sum += z * z; |
171 | 0 | } |
172 | 0 | } |
173 | 0 | } else { // Two cols remaining |
174 | 0 | const int16_t x0 = diff[width - 2]; |
175 | 0 | const int16_t y0 = diff[width - 1]; |
176 | 0 | x_sum += x0 + y0; |
177 | 0 | x2_sum += x0 * x0 + y0 * y0; |
178 | 0 | x_finalcol += y0; |
179 | 0 | x2_finalcol += y0 * y0; |
180 | 0 | for (int i = 0; i < height - 1; ++i) { |
181 | 0 | const int16_t x = diff[i * stride + width - 2]; |
182 | 0 | const int16_t y = diff[i * stride + width - 1]; |
183 | 0 | const int16_t z = diff[(i + 1) * stride + width - 2]; |
184 | 0 | const int16_t w = diff[(i + 1) * stride + width - 1]; |
185 | | |
186 | | // Horizontal and vertical correlations for the penultimate col: |
187 | | // Skip these on the last iteration of this loop if we also had two |
188 | | // rows remaining, otherwise the final horizontal and vertical correlation |
189 | | // get erroneously processed twice |
190 | 0 | if (i < height - 2 || height % 3 == 1) { |
191 | 0 | xy_sum += x * y; |
192 | 0 | xz_sum += x * z; |
193 | 0 | } |
194 | |
|
195 | 0 | x_finalcol += w; |
196 | 0 | x2_finalcol += w * w; |
197 | | // So the bottom-right elements don't get counted twice: |
198 | 0 | if (i < height - (height % 3 == 1 ? 2 : 3)) { |
199 | 0 | x_sum += z + w; |
200 | 0 | x2_sum += z * z + w * w; |
201 | 0 | } |
202 | | |
203 | | // Now just vertical correlations for the final column: |
204 | 0 | xz_sum += y * w; |
205 | 0 | } |
206 | 0 | } |
207 | | |
208 | | // Calculate the simple sums and squared-sums |
209 | 0 | int64_t x_firstrow = 0, x_firstcol = 0; |
210 | 0 | int64_t x2_firstrow = 0, x2_firstcol = 0; |
211 | |
|
212 | 0 | for (int j = 0; j < width; ++j) { |
213 | 0 | x_firstrow += diff[j]; |
214 | 0 | x2_firstrow += diff[j] * diff[j]; |
215 | 0 | } |
216 | 0 | for (int i = 0; i < height; ++i) { |
217 | 0 | x_firstcol += diff[i * stride]; |
218 | 0 | x2_firstcol += diff[i * stride] * diff[i * stride]; |
219 | 0 | } |
220 | |
|
221 | 0 | int64_t xhor_sum = x_sum - x_finalcol; |
222 | 0 | int64_t xver_sum = x_sum - x_finalrow; |
223 | 0 | int64_t y_sum = x_sum - x_firstcol; |
224 | 0 | int64_t z_sum = x_sum - x_firstrow; |
225 | 0 | int64_t x2hor_sum = x2_sum - x2_finalcol; |
226 | 0 | int64_t x2ver_sum = x2_sum - x2_finalrow; |
227 | 0 | int64_t y2_sum = x2_sum - x2_firstcol; |
228 | 0 | int64_t z2_sum = x2_sum - x2_firstrow; |
229 | |
|
230 | 0 | const float num_hor = (float)(height * (width - 1)); |
231 | 0 | const float num_ver = (float)((height - 1) * width); |
232 | |
|
233 | 0 | const float xhor_var_n = x2hor_sum - (xhor_sum * xhor_sum) / num_hor; |
234 | 0 | const float xver_var_n = x2ver_sum - (xver_sum * xver_sum) / num_ver; |
235 | |
|
236 | 0 | const float y_var_n = y2_sum - (y_sum * y_sum) / num_hor; |
237 | 0 | const float z_var_n = z2_sum - (z_sum * z_sum) / num_ver; |
238 | |
|
239 | 0 | const float xy_var_n = xy_sum - (xhor_sum * y_sum) / num_hor; |
240 | 0 | const float xz_var_n = xz_sum - (xver_sum * z_sum) / num_ver; |
241 | |
|
242 | 0 | if (xhor_var_n > 0 && y_var_n > 0) { |
243 | 0 | *hcorr = xy_var_n / sqrtf(xhor_var_n * y_var_n); |
244 | 0 | *hcorr = *hcorr < 0 ? 0 : *hcorr; |
245 | 0 | } else { |
246 | 0 | *hcorr = 1.0; |
247 | 0 | } |
248 | 0 | if (xver_var_n > 0 && z_var_n > 0) { |
249 | 0 | *vcorr = xz_var_n / sqrtf(xver_var_n * z_var_n); |
250 | 0 | *vcorr = *vcorr < 0 ? 0 : *vcorr; |
251 | 0 | } else { |
252 | 0 | *vcorr = 1.0; |
253 | 0 | } |
254 | 0 | } |
255 | | |
256 | 0 | static inline uint64_t xx_cvtsi128_si64(__m128i a) { |
257 | 0 | #if AOM_ARCH_X86_64 |
258 | 0 | return (uint64_t)_mm_cvtsi128_si64(a); |
259 | | #else |
260 | | uint64_t tmp; |
261 | | _mm_storel_epi64((__m128i *)&tmp, a); |
262 | | return tmp; |
263 | | #endif |
264 | 0 | } |
265 | | |
266 | 0 | static inline uint64_t sum_u32x4_to_u64(__m128i v) { |
267 | 0 | const __m256i v64 = _mm256_cvtepu32_epi64(v); |
268 | 0 | const __m128i sum2 = _mm_add_epi64(_mm256_castsi256_si128(v64), |
269 | 0 | _mm256_extracti128_si256(v64, 1)); |
270 | 0 | const __m128i sum1 = _mm_add_epi64(sum2, _mm_srli_si128(sum2, 8)); |
271 | 0 | return xx_cvtsi128_si64(sum1); |
272 | 0 | } |
273 | | |
274 | 0 | static inline uint64_t sum_u32x8_to_u64(__m256i v) { |
275 | 0 | const __m256i lo64 = _mm256_cvtepu32_epi64(_mm256_castsi256_si128(v)); |
276 | 0 | const __m256i hi64 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(v, 1)); |
277 | 0 | const __m256i sum4 = _mm256_add_epi64(lo64, hi64); |
278 | 0 | const __m128i sum2 = _mm_add_epi64(_mm256_castsi256_si128(sum4), |
279 | 0 | _mm256_extracti128_si256(sum4, 1)); |
280 | 0 | const __m128i sum1 = _mm_add_epi64(sum2, _mm_srli_si128(sum2, 8)); |
281 | 0 | return xx_cvtsi128_si64(sum1); |
282 | 0 | } |
283 | | |
284 | | void av1_get_energy_distribution_finer_avx2(const int16_t *diff, int stride, |
285 | | int bw, int bh, float *hordist, |
286 | 0 | float *verdist) { |
287 | 0 | const int w_shift = bw <= 8 ? 0 : 1; |
288 | 0 | const int h_shift = bh <= 8 ? 0 : 1; |
289 | 0 | const int esq_w = bw >> w_shift; |
290 | 0 | const int esq_h = bh >> h_shift; |
291 | 0 | assert(esq_w <= 16); |
292 | 0 | assert(esq_h <= 16); |
293 | 0 | uint32_t col_sum[16] = { 0 }; |
294 | 0 | uint32_t row_sum[16] = { 0 }; |
295 | 0 | uint64_t total = 0; |
296 | |
|
297 | 0 | if (bw == 4) { |
298 | 0 | __m128i v_col = _mm_setzero_si128(); |
299 | 0 | if (h_shift == 0) { |
300 | 0 | for (int i = 0; i < esq_h; ++i) { |
301 | 0 | const __m128i d = _mm_cvtepi16_epi32( |
302 | 0 | _mm_loadl_epi64((const __m128i *)(diff + i * stride))); |
303 | 0 | const __m128i sq = _mm_mullo_epi32(d, d); |
304 | 0 | v_col = _mm_add_epi32(v_col, sq); |
305 | 0 | const __m128i s1 = _mm_hadd_epi32(sq, sq); |
306 | 0 | const __m128i s2 = _mm_hadd_epi32(s1, s1); |
307 | 0 | row_sum[i] = (uint32_t)_mm_cvtsi128_si32(s2); |
308 | 0 | } |
309 | 0 | } else { |
310 | 0 | for (int i = 0; i < esq_h; ++i) { |
311 | 0 | const __m128i d0 = |
312 | 0 | _mm_loadl_epi64((const __m128i *)(diff + (2 * i) * stride)); |
313 | 0 | const __m128i d1 = |
314 | 0 | _mm_loadl_epi64((const __m128i *)(diff + (2 * i + 1) * stride)); |
315 | 0 | const __m128i d01 = _mm_unpacklo_epi16(d0, d1); |
316 | 0 | const __m128i sq = _mm_madd_epi16(d01, d01); |
317 | 0 | v_col = _mm_add_epi32(v_col, sq); |
318 | 0 | const __m128i s1 = _mm_hadd_epi32(sq, sq); |
319 | 0 | const __m128i s2 = _mm_hadd_epi32(s1, s1); |
320 | 0 | row_sum[i] = (uint32_t)_mm_cvtsi128_si32(s2); |
321 | 0 | } |
322 | 0 | } |
323 | 0 | _mm_storeu_si128((__m128i *)col_sum, v_col); |
324 | 0 | total = sum_u32x4_to_u64(v_col); |
325 | 0 | } else if (bw == 8) { |
326 | 0 | __m256i v_col = _mm256_setzero_si256(); |
327 | 0 | if (h_shift == 0) { |
328 | 0 | for (int i = 0; i < esq_h; ++i) { |
329 | 0 | const __m256i d = _mm256_cvtepi16_epi32( |
330 | 0 | _mm_loadu_si128((const __m128i *)(diff + i * stride))); |
331 | 0 | const __m256i sq = _mm256_mullo_epi32(d, d); |
332 | 0 | v_col = _mm256_add_epi32(v_col, sq); |
333 | 0 | const __m128i sq128 = _mm_add_epi32(_mm256_castsi256_si128(sq), |
334 | 0 | _mm256_extracti128_si256(sq, 1)); |
335 | 0 | const __m128i s1 = _mm_hadd_epi32(sq128, sq128); |
336 | 0 | const __m128i s2 = _mm_hadd_epi32(s1, s1); |
337 | 0 | row_sum[i] = (uint32_t)_mm_cvtsi128_si32(s2); |
338 | 0 | } |
339 | 0 | } else { |
340 | 0 | for (int i = 0; i < esq_h; ++i) { |
341 | 0 | const __m128i d0 = |
342 | 0 | _mm_loadu_si128((const __m128i *)(diff + (2 * i) * stride)); |
343 | 0 | const __m128i d1 = |
344 | 0 | _mm_loadu_si128((const __m128i *)(diff + (2 * i + 1) * stride)); |
345 | 0 | const __m256i d0_256 = _mm256_cvtepi16_epi32(d0); |
346 | 0 | const __m256i d1_256 = _mm256_cvtepi16_epi32(d1); |
347 | 0 | const __m256i sq = _mm256_add_epi32(_mm256_mullo_epi32(d0_256, d0_256), |
348 | 0 | _mm256_mullo_epi32(d1_256, d1_256)); |
349 | 0 | v_col = _mm256_add_epi32(v_col, sq); |
350 | 0 | const __m128i sq128 = _mm_add_epi32(_mm256_castsi256_si128(sq), |
351 | 0 | _mm256_extracti128_si256(sq, 1)); |
352 | 0 | const __m128i s1 = _mm_hadd_epi32(sq128, sq128); |
353 | 0 | const __m128i s2 = _mm_hadd_epi32(s1, s1); |
354 | 0 | row_sum[i] = (uint32_t)_mm_cvtsi128_si32(s2); |
355 | 0 | } |
356 | 0 | } |
357 | 0 | _mm256_storeu_si256((__m256i *)col_sum, v_col); |
358 | 0 | total = sum_u32x8_to_u64(v_col); |
359 | 0 | } else if (bw == 16) { |
360 | 0 | __m256i v_col = _mm256_setzero_si256(); |
361 | 0 | if (h_shift == 0) { |
362 | 0 | for (int i = 0; i < esq_h; ++i) { |
363 | 0 | const __m256i d = |
364 | 0 | _mm256_loadu_si256((const __m256i *)(diff + i * stride)); |
365 | 0 | const __m256i sq = _mm256_madd_epi16(d, d); |
366 | 0 | v_col = _mm256_add_epi32(v_col, sq); |
367 | 0 | const __m128i sq128 = _mm_add_epi32(_mm256_castsi256_si128(sq), |
368 | 0 | _mm256_extracti128_si256(sq, 1)); |
369 | 0 | const __m128i s1 = _mm_hadd_epi32(sq128, sq128); |
370 | 0 | const __m128i s2 = _mm_hadd_epi32(s1, s1); |
371 | 0 | row_sum[i] = (uint32_t)_mm_cvtsi128_si32(s2); |
372 | 0 | } |
373 | 0 | } else { |
374 | 0 | for (int i = 0; i < esq_h; ++i) { |
375 | 0 | const __m256i d0 = |
376 | 0 | _mm256_loadu_si256((const __m256i *)(diff + (2 * i) * stride)); |
377 | 0 | const __m256i d1 = |
378 | 0 | _mm256_loadu_si256((const __m256i *)(diff + (2 * i + 1) * stride)); |
379 | 0 | const __m256i sq = _mm256_add_epi32(_mm256_madd_epi16(d0, d0), |
380 | 0 | _mm256_madd_epi16(d1, d1)); |
381 | 0 | v_col = _mm256_add_epi32(v_col, sq); |
382 | 0 | const __m128i sq128 = _mm_add_epi32(_mm256_castsi256_si128(sq), |
383 | 0 | _mm256_extracti128_si256(sq, 1)); |
384 | 0 | const __m128i s1 = _mm_hadd_epi32(sq128, sq128); |
385 | 0 | const __m128i s2 = _mm_hadd_epi32(s1, s1); |
386 | 0 | row_sum[i] = (uint32_t)_mm_cvtsi128_si32(s2); |
387 | 0 | } |
388 | 0 | } |
389 | 0 | _mm256_storeu_si256((__m256i *)col_sum, v_col); |
390 | 0 | total = sum_u32x8_to_u64(v_col); |
391 | 0 | } else { |
392 | 0 | assert(bw == 32); |
393 | 0 | __m256i v_col_lo = _mm256_setzero_si256(); |
394 | 0 | __m256i v_col_hi = _mm256_setzero_si256(); |
395 | 0 | if (h_shift == 0) { |
396 | 0 | for (int i = 0; i < esq_h; ++i) { |
397 | 0 | const int16_t *r0 = diff + i * stride; |
398 | 0 | const __m256i d0_lo = _mm256_loadu_si256((const __m256i *)r0); |
399 | 0 | const __m256i d0_hi = _mm256_loadu_si256((const __m256i *)(r0 + 16)); |
400 | 0 | const __m256i sq_lo = _mm256_madd_epi16(d0_lo, d0_lo); |
401 | 0 | const __m256i sq_hi = _mm256_madd_epi16(d0_hi, d0_hi); |
402 | 0 | v_col_lo = _mm256_add_epi32(v_col_lo, sq_lo); |
403 | 0 | v_col_hi = _mm256_add_epi32(v_col_hi, sq_hi); |
404 | 0 | const __m256i sq_sum = _mm256_add_epi32(sq_lo, sq_hi); |
405 | 0 | const __m128i sq128 = |
406 | 0 | _mm_add_epi32(_mm256_castsi256_si128(sq_sum), |
407 | 0 | _mm256_extracti128_si256(sq_sum, 1)); |
408 | 0 | const __m128i s1 = _mm_hadd_epi32(sq128, sq128); |
409 | 0 | const __m128i s2 = _mm_hadd_epi32(s1, s1); |
410 | 0 | row_sum[i] = (uint32_t)_mm_cvtsi128_si32(s2); |
411 | 0 | } |
412 | 0 | } else { |
413 | 0 | for (int i = 0; i < esq_h; ++i) { |
414 | 0 | const int16_t *r0 = diff + (2 * i) * stride; |
415 | 0 | const int16_t *r1 = diff + (2 * i + 1) * stride; |
416 | 0 | const __m256i d0_lo = _mm256_loadu_si256((const __m256i *)r0); |
417 | 0 | const __m256i d0_hi = _mm256_loadu_si256((const __m256i *)(r0 + 16)); |
418 | 0 | const __m256i d1_lo = _mm256_loadu_si256((const __m256i *)r1); |
419 | 0 | const __m256i d1_hi = _mm256_loadu_si256((const __m256i *)(r1 + 16)); |
420 | 0 | const __m256i sq_lo = _mm256_add_epi32(_mm256_madd_epi16(d0_lo, d0_lo), |
421 | 0 | _mm256_madd_epi16(d1_lo, d1_lo)); |
422 | 0 | const __m256i sq_hi = _mm256_add_epi32(_mm256_madd_epi16(d0_hi, d0_hi), |
423 | 0 | _mm256_madd_epi16(d1_hi, d1_hi)); |
424 | 0 | v_col_lo = _mm256_add_epi32(v_col_lo, sq_lo); |
425 | 0 | v_col_hi = _mm256_add_epi32(v_col_hi, sq_hi); |
426 | 0 | const __m256i sq_sum = _mm256_add_epi32(sq_lo, sq_hi); |
427 | 0 | const __m128i sq128 = |
428 | 0 | _mm_add_epi32(_mm256_castsi256_si128(sq_sum), |
429 | 0 | _mm256_extracti128_si256(sq_sum, 1)); |
430 | 0 | const __m128i s1 = _mm_hadd_epi32(sq128, sq128); |
431 | 0 | const __m128i s2 = _mm_hadd_epi32(s1, s1); |
432 | 0 | row_sum[i] = (uint32_t)_mm_cvtsi128_si32(s2); |
433 | 0 | } |
434 | 0 | } |
435 | 0 | _mm256_storeu_si256((__m256i *)col_sum, v_col_lo); |
436 | 0 | _mm256_storeu_si256((__m256i *)(col_sum + 8), v_col_hi); |
437 | 0 | total = sum_u32x8_to_u64(v_col_lo) + sum_u32x8_to_u64(v_col_hi); |
438 | 0 | } |
439 | | |
440 | | // Output hordist and verdist arrays are normalized 1D projections of esq |
441 | 0 | if (total == 0) { |
442 | 0 | const float hor_val = 1.0f / esq_w; |
443 | 0 | for (int j = 0; j < esq_w - 1; ++j) hordist[j] = hor_val; |
444 | 0 | const float ver_val = 1.0f / esq_h; |
445 | 0 | for (int i = 0; i < esq_h - 1; ++i) verdist[i] = ver_val; |
446 | 0 | return; |
447 | 0 | } |
448 | | |
449 | 0 | const float e_recip = 1.0f / (float)total; |
450 | 0 | for (int j = 0; j < esq_w - 1; ++j) hordist[j] = (float)col_sum[j] * e_recip; |
451 | 0 | for (int i = 0; i < esq_h - 1; ++i) verdist[i] = (float)row_sum[i] * e_recip; |
452 | 0 | } |