/src/aom/aom_dsp/x86/sad_impl_avx2.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016, 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 | | |
14 | | #include "config/aom_dsp_rtcd.h" |
15 | | |
16 | | static unsigned int sad32x32(const uint8_t *src_ptr, int src_stride, |
17 | 0 | const uint8_t *ref_ptr, int ref_stride) { |
18 | 0 | __m256i s1, s2, r1, r2; |
19 | 0 | __m256i sum = _mm256_setzero_si256(); |
20 | 0 | __m128i sum_i128; |
21 | 0 | int i; |
22 | |
|
23 | 0 | for (i = 0; i < 16; ++i) { |
24 | 0 | r1 = _mm256_loadu_si256((__m256i const *)ref_ptr); |
25 | 0 | r2 = _mm256_loadu_si256((__m256i const *)(ref_ptr + ref_stride)); |
26 | 0 | s1 = _mm256_sad_epu8(r1, _mm256_loadu_si256((__m256i const *)src_ptr)); |
27 | 0 | s2 = _mm256_sad_epu8( |
28 | 0 | r2, _mm256_loadu_si256((__m256i const *)(src_ptr + src_stride))); |
29 | 0 | sum = _mm256_add_epi32(sum, _mm256_add_epi32(s1, s2)); |
30 | 0 | ref_ptr += ref_stride << 1; |
31 | 0 | src_ptr += src_stride << 1; |
32 | 0 | } |
33 | |
|
34 | 0 | sum = _mm256_add_epi32(sum, _mm256_srli_si256(sum, 8)); |
35 | 0 | sum_i128 = _mm_add_epi32(_mm256_extracti128_si256(sum, 1), |
36 | 0 | _mm256_castsi256_si128(sum)); |
37 | 0 | return (unsigned int)_mm_cvtsi128_si32(sum_i128); |
38 | 0 | } |
39 | | |
40 | | static unsigned int sad64x32(const uint8_t *src_ptr, int src_stride, |
41 | 0 | const uint8_t *ref_ptr, int ref_stride) { |
42 | 0 | unsigned int half_width = 32; |
43 | 0 | uint32_t sum = sad32x32(src_ptr, src_stride, ref_ptr, ref_stride); |
44 | 0 | src_ptr += half_width; |
45 | 0 | ref_ptr += half_width; |
46 | 0 | sum += sad32x32(src_ptr, src_stride, ref_ptr, ref_stride); |
47 | 0 | return sum; |
48 | 0 | } |
49 | | |
50 | | static unsigned int sad64x64(const uint8_t *src_ptr, int src_stride, |
51 | 0 | const uint8_t *ref_ptr, int ref_stride) { |
52 | 0 | uint32_t sum = sad64x32(src_ptr, src_stride, ref_ptr, ref_stride); |
53 | 0 | src_ptr += src_stride << 5; |
54 | 0 | ref_ptr += ref_stride << 5; |
55 | 0 | sum += sad64x32(src_ptr, src_stride, ref_ptr, ref_stride); |
56 | 0 | return sum; |
57 | 0 | } |
58 | | |
59 | | unsigned int aom_sad128x64_avx2(const uint8_t *src_ptr, int src_stride, |
60 | 0 | const uint8_t *ref_ptr, int ref_stride) { |
61 | 0 | unsigned int half_width = 64; |
62 | 0 | uint32_t sum = sad64x64(src_ptr, src_stride, ref_ptr, ref_stride); |
63 | 0 | src_ptr += half_width; |
64 | 0 | ref_ptr += half_width; |
65 | 0 | sum += sad64x64(src_ptr, src_stride, ref_ptr, ref_stride); |
66 | 0 | return sum; |
67 | 0 | } |
68 | | |
69 | | unsigned int aom_sad64x128_avx2(const uint8_t *src_ptr, int src_stride, |
70 | 0 | const uint8_t *ref_ptr, int ref_stride) { |
71 | 0 | uint32_t sum = sad64x64(src_ptr, src_stride, ref_ptr, ref_stride); |
72 | 0 | src_ptr += src_stride << 6; |
73 | 0 | ref_ptr += ref_stride << 6; |
74 | 0 | sum += sad64x64(src_ptr, src_stride, ref_ptr, ref_stride); |
75 | 0 | return sum; |
76 | 0 | } |
77 | | |
78 | | unsigned int aom_sad128x128_avx2(const uint8_t *src_ptr, int src_stride, |
79 | 0 | const uint8_t *ref_ptr, int ref_stride) { |
80 | 0 | uint32_t sum = aom_sad128x64_avx2(src_ptr, src_stride, ref_ptr, ref_stride); |
81 | 0 | src_ptr += src_stride << 6; |
82 | 0 | ref_ptr += ref_stride << 6; |
83 | 0 | sum += aom_sad128x64_avx2(src_ptr, src_stride, ref_ptr, ref_stride); |
84 | 0 | return sum; |
85 | 0 | } |
86 | | |
87 | | unsigned int aom_sad_skip_128x64_avx2(const uint8_t *src_ptr, int src_stride, |
88 | 0 | const uint8_t *ref_ptr, int ref_stride) { |
89 | 0 | const uint32_t half_width = 64; |
90 | 0 | uint32_t sum = sad64x32(src_ptr, src_stride * 2, ref_ptr, ref_stride * 2); |
91 | 0 | src_ptr += half_width; |
92 | 0 | ref_ptr += half_width; |
93 | 0 | sum += sad64x32(src_ptr, src_stride * 2, ref_ptr, ref_stride * 2); |
94 | 0 | return 2 * sum; |
95 | 0 | } |
96 | | |
97 | | unsigned int aom_sad_skip_64x128_avx2(const uint8_t *src_ptr, int src_stride, |
98 | 0 | const uint8_t *ref_ptr, int ref_stride) { |
99 | 0 | const uint32_t sum = |
100 | 0 | sad64x64(src_ptr, 2 * src_stride, ref_ptr, 2 * ref_stride); |
101 | 0 | return 2 * sum; |
102 | 0 | } |
103 | | |
104 | | unsigned int aom_sad_skip_128x128_avx2(const uint8_t *src_ptr, int src_stride, |
105 | 0 | const uint8_t *ref_ptr, int ref_stride) { |
106 | 0 | const uint32_t sum = |
107 | 0 | aom_sad128x64_avx2(src_ptr, 2 * src_stride, ref_ptr, 2 * ref_stride); |
108 | 0 | return 2 * sum; |
109 | 0 | } |
110 | | |
111 | | static unsigned int sad_w64_avg_avx2(const uint8_t *src_ptr, int src_stride, |
112 | | const uint8_t *ref_ptr, int ref_stride, |
113 | | const int h, const uint8_t *second_pred, |
114 | 0 | const int second_pred_stride) { |
115 | 0 | int i; |
116 | 0 | __m256i sad1_reg, sad2_reg, ref1_reg, ref2_reg; |
117 | 0 | __m256i sum_sad = _mm256_setzero_si256(); |
118 | 0 | __m256i sum_sad_h; |
119 | 0 | __m128i sum_sad128; |
120 | 0 | for (i = 0; i < h; i++) { |
121 | 0 | ref1_reg = _mm256_loadu_si256((__m256i const *)ref_ptr); |
122 | 0 | ref2_reg = _mm256_loadu_si256((__m256i const *)(ref_ptr + 32)); |
123 | 0 | ref1_reg = _mm256_avg_epu8( |
124 | 0 | ref1_reg, _mm256_loadu_si256((__m256i const *)second_pred)); |
125 | 0 | ref2_reg = _mm256_avg_epu8( |
126 | 0 | ref2_reg, _mm256_loadu_si256((__m256i const *)(second_pred + 32))); |
127 | 0 | sad1_reg = |
128 | 0 | _mm256_sad_epu8(ref1_reg, _mm256_loadu_si256((__m256i const *)src_ptr)); |
129 | 0 | sad2_reg = _mm256_sad_epu8( |
130 | 0 | ref2_reg, _mm256_loadu_si256((__m256i const *)(src_ptr + 32))); |
131 | 0 | sum_sad = _mm256_add_epi32(sum_sad, _mm256_add_epi32(sad1_reg, sad2_reg)); |
132 | 0 | ref_ptr += ref_stride; |
133 | 0 | src_ptr += src_stride; |
134 | 0 | second_pred += second_pred_stride; |
135 | 0 | } |
136 | 0 | sum_sad_h = _mm256_srli_si256(sum_sad, 8); |
137 | 0 | sum_sad = _mm256_add_epi32(sum_sad, sum_sad_h); |
138 | 0 | sum_sad128 = _mm256_extracti128_si256(sum_sad, 1); |
139 | 0 | sum_sad128 = _mm_add_epi32(_mm256_castsi256_si128(sum_sad), sum_sad128); |
140 | 0 | return (unsigned int)_mm_cvtsi128_si32(sum_sad128); |
141 | 0 | } |
142 | | |
143 | | unsigned int aom_sad64x128_avg_avx2(const uint8_t *src_ptr, int src_stride, |
144 | | const uint8_t *ref_ptr, int ref_stride, |
145 | 0 | const uint8_t *second_pred) { |
146 | 0 | uint32_t sum = sad_w64_avg_avx2(src_ptr, src_stride, ref_ptr, ref_stride, 64, |
147 | 0 | second_pred, 64); |
148 | 0 | src_ptr += src_stride << 6; |
149 | 0 | ref_ptr += ref_stride << 6; |
150 | 0 | second_pred += 64 << 6; |
151 | 0 | sum += sad_w64_avg_avx2(src_ptr, src_stride, ref_ptr, ref_stride, 64, |
152 | 0 | second_pred, 64); |
153 | 0 | return sum; |
154 | 0 | } |
155 | | |
156 | | unsigned int aom_sad128x64_avg_avx2(const uint8_t *src_ptr, int src_stride, |
157 | | const uint8_t *ref_ptr, int ref_stride, |
158 | 0 | const uint8_t *second_pred) { |
159 | 0 | unsigned int half_width = 64; |
160 | 0 | uint32_t sum = sad_w64_avg_avx2(src_ptr, src_stride, ref_ptr, ref_stride, 64, |
161 | 0 | second_pred, 128); |
162 | 0 | src_ptr += half_width; |
163 | 0 | ref_ptr += half_width; |
164 | 0 | second_pred += half_width; |
165 | 0 | sum += sad_w64_avg_avx2(src_ptr, src_stride, ref_ptr, ref_stride, 64, |
166 | 0 | second_pred, 128); |
167 | 0 | return sum; |
168 | 0 | } |
169 | | |
170 | | unsigned int aom_sad128x128_avg_avx2(const uint8_t *src_ptr, int src_stride, |
171 | | const uint8_t *ref_ptr, int ref_stride, |
172 | 0 | const uint8_t *second_pred) { |
173 | 0 | uint32_t sum = aom_sad128x64_avg_avx2(src_ptr, src_stride, ref_ptr, |
174 | 0 | ref_stride, second_pred); |
175 | 0 | src_ptr += src_stride << 6; |
176 | 0 | ref_ptr += ref_stride << 6; |
177 | 0 | second_pred += 128 << 6; |
178 | 0 | sum += aom_sad128x64_avg_avx2(src_ptr, src_stride, ref_ptr, ref_stride, |
179 | 0 | second_pred); |
180 | 0 | return sum; |
181 | 0 | } |