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 <immintrin.h>
13 #include <smmintrin.h>
14 
15 #include "aom_dsp/x86/synonyms.h"
16 #include "aom_dsp/x86/synonyms_avx2.h"
17 #include "aom_dsp/x86/sum_squares_sse2.h"
18 #include "config/aom_dsp_rtcd.h"
19 
aom_sum_squares_2d_i16_nxn_avx2(const int16_t * src,int stride,int width,int height)20 static uint64_t aom_sum_squares_2d_i16_nxn_avx2(const int16_t *src, int stride,
21                                                 int width, int height) {
22   uint64_t result;
23   __m256i v_acc_q = _mm256_setzero_si256();
24   const __m256i v_zext_mask_q = yy_set1_64_from_32i(0xffffffff);
25   for (int col = 0; col < height; col += 4) {
26     __m256i v_acc_d = _mm256_setzero_si256();
27     for (int row = 0; row < width; row += 16) {
28       const int16_t *tempsrc = src + row;
29       const __m256i v_val_0_w =
30           _mm256_loadu_si256((const __m256i *)(tempsrc + 0 * stride));
31       const __m256i v_val_1_w =
32           _mm256_loadu_si256((const __m256i *)(tempsrc + 1 * stride));
33       const __m256i v_val_2_w =
34           _mm256_loadu_si256((const __m256i *)(tempsrc + 2 * stride));
35       const __m256i v_val_3_w =
36           _mm256_loadu_si256((const __m256i *)(tempsrc + 3 * stride));
37 
38       const __m256i v_sq_0_d = _mm256_madd_epi16(v_val_0_w, v_val_0_w);
39       const __m256i v_sq_1_d = _mm256_madd_epi16(v_val_1_w, v_val_1_w);
40       const __m256i v_sq_2_d = _mm256_madd_epi16(v_val_2_w, v_val_2_w);
41       const __m256i v_sq_3_d = _mm256_madd_epi16(v_val_3_w, v_val_3_w);
42 
43       const __m256i v_sum_01_d = _mm256_add_epi32(v_sq_0_d, v_sq_1_d);
44       const __m256i v_sum_23_d = _mm256_add_epi32(v_sq_2_d, v_sq_3_d);
45       const __m256i v_sum_0123_d = _mm256_add_epi32(v_sum_01_d, v_sum_23_d);
46 
47       v_acc_d = _mm256_add_epi32(v_acc_d, v_sum_0123_d);
48     }
49     v_acc_q =
50         _mm256_add_epi64(v_acc_q, _mm256_and_si256(v_acc_d, v_zext_mask_q));
51     v_acc_q = _mm256_add_epi64(v_acc_q, _mm256_srli_epi64(v_acc_d, 32));
52     src += 4 * stride;
53   }
54   __m128i lower_64_2_Value = _mm256_castsi256_si128(v_acc_q);
55   __m128i higher_64_2_Value = _mm256_extracti128_si256(v_acc_q, 1);
56   __m128i result_64_2_int = _mm_add_epi64(lower_64_2_Value, higher_64_2_Value);
57 
58   result_64_2_int = _mm_add_epi64(
59       result_64_2_int, _mm_unpackhi_epi64(result_64_2_int, result_64_2_int));
60 
61   xx_storel_64(&result, result_64_2_int);
62 
63   return result;
64 }
65 
aom_sum_squares_2d_i16_avx2(const int16_t * src,int stride,int width,int height)66 uint64_t aom_sum_squares_2d_i16_avx2(const int16_t *src, int stride, int width,
67                                      int height) {
68   if (LIKELY(width == 4 && height == 4)) {
69     return aom_sum_squares_2d_i16_4x4_sse2(src, stride);
70   } else if (LIKELY(width == 4 && (height & 3) == 0)) {
71     return aom_sum_squares_2d_i16_4xn_sse2(src, stride, height);
72   } else if (LIKELY(width == 8 && (height & 3) == 0)) {
73     return aom_sum_squares_2d_i16_nxn_sse2(src, stride, width, height);
74   } else if (LIKELY(((width & 15) == 0) && ((height & 3) == 0))) {
75     return aom_sum_squares_2d_i16_nxn_avx2(src, stride, width, height);
76   } else {
77     return aom_sum_squares_2d_i16_c(src, stride, width, height);
78   }
79 }
80