1 /*
2 * Copyright (c) 2016 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <cmath>
12 #include <cstdint>
13 #include <cstdlib>
14 #include <string>
15 #include <tuple>
16
17 #include "third_party/googletest/src/include/gtest/gtest.h"
18
19 #include "./vpx_config.h"
20 #include "./vpx_dsp_rtcd.h"
21 #include "test/acm_random.h"
22 #include "test/clear_system_state.h"
23 #include "test/register_state_check.h"
24 #include "test/util.h"
25 #include "vpx_mem/vpx_mem.h"
26 #include "vpx_ports/mem.h"
27 #include "vpx_ports/vpx_timer.h"
28
29 using libvpx_test::ACMRandom;
30 using ::testing::Combine;
31 using ::testing::Range;
32 using ::testing::ValuesIn;
33
34 namespace {
35 const int kNumIterations = 10000;
36
37 typedef uint64_t (*SSI16Func)(const int16_t *src, int stride, int size);
38 typedef std::tuple<SSI16Func, SSI16Func> SumSquaresParam;
39
40 class SumSquaresTest : public ::testing::TestWithParam<SumSquaresParam> {
41 public:
42 ~SumSquaresTest() override = default;
SetUp()43 void SetUp() override {
44 ref_func_ = GET_PARAM(0);
45 tst_func_ = GET_PARAM(1);
46 }
47
TearDown()48 void TearDown() override { libvpx_test::ClearSystemState(); }
49
50 protected:
51 SSI16Func ref_func_;
52 SSI16Func tst_func_;
53 };
54 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SumSquaresTest);
55
TEST_P(SumSquaresTest,OperationCheck)56 TEST_P(SumSquaresTest, OperationCheck) {
57 ACMRandom rnd(ACMRandom::DeterministicSeed());
58 DECLARE_ALIGNED(16, int16_t, src[256 * 256]);
59 const int msb = 11; // Up to 12 bit input
60 const int limit = 1 << (msb + 1);
61
62 for (int k = 0; k < kNumIterations; k++) {
63 const int size = 4 << rnd(6); // Up to 128x128
64 int stride = 4 << rnd(7); // Up to 256 stride
65 while (stride < size) { // Make sure it's valid
66 stride = 4 << rnd(7);
67 }
68
69 for (int i = 0; i < size; ++i) {
70 for (int j = 0; j < size; ++j) {
71 src[i * stride + j] = rnd(2) ? rnd(limit) : -rnd(limit);
72 }
73 }
74
75 const uint64_t res_ref = ref_func_(src, stride, size);
76 uint64_t res_tst;
77 ASM_REGISTER_STATE_CHECK(res_tst = tst_func_(src, stride, size));
78
79 ASSERT_EQ(res_ref, res_tst) << "Error: Sum Squares Test"
80 << " C output does not match optimized output.";
81 }
82 }
83
TEST_P(SumSquaresTest,ExtremeValues)84 TEST_P(SumSquaresTest, ExtremeValues) {
85 ACMRandom rnd(ACMRandom::DeterministicSeed());
86 DECLARE_ALIGNED(16, int16_t, src[256 * 256]);
87 const int msb = 11; // Up to 12 bit input
88 const int limit = 1 << (msb + 1);
89
90 for (int k = 0; k < kNumIterations; k++) {
91 const int size = 4 << rnd(6); // Up to 128x128
92 int stride = 4 << rnd(7); // Up to 256 stride
93 while (stride < size) { // Make sure it's valid
94 stride = 4 << rnd(7);
95 }
96
97 const int val = rnd(2) ? limit - 1 : -(limit - 1);
98 for (int i = 0; i < size; ++i) {
99 for (int j = 0; j < size; ++j) {
100 src[i * stride + j] = val;
101 }
102 }
103
104 const uint64_t res_ref = ref_func_(src, stride, size);
105 uint64_t res_tst;
106 ASM_REGISTER_STATE_CHECK(res_tst = tst_func_(src, stride, size));
107
108 ASSERT_EQ(res_ref, res_tst) << "Error: Sum Squares Test"
109 << " C output does not match optimized output.";
110 }
111 }
112
113 using std::make_tuple;
114
115 #if HAVE_NEON
116 INSTANTIATE_TEST_SUITE_P(
117 NEON, SumSquaresTest,
118 ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
119 &vpx_sum_squares_2d_i16_neon)));
120 #endif // HAVE_NEON
121
122 #if HAVE_SSE2
123 INSTANTIATE_TEST_SUITE_P(
124 SSE2, SumSquaresTest,
125 ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
126 &vpx_sum_squares_2d_i16_sse2)));
127 #endif // HAVE_SSE2
128
129 #if HAVE_MSA
130 INSTANTIATE_TEST_SUITE_P(
131 MSA, SumSquaresTest,
132 ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
133 &vpx_sum_squares_2d_i16_msa)));
134 #endif // HAVE_MSA
135
136 typedef int64_t (*SSEFunc)(const uint8_t *a, int a_stride, const uint8_t *b,
137 int b_stride, int width, int height);
138
139 struct TestSSEFuncs {
TestSSEFuncs__anon2dc7a7e30111::TestSSEFuncs140 TestSSEFuncs(SSEFunc ref = nullptr, SSEFunc tst = nullptr, int depth = 0)
141 : ref_func(ref), tst_func(tst), bit_depth(depth) {}
142 SSEFunc ref_func; // Pointer to reference function
143 SSEFunc tst_func; // Pointer to tested function
144 int bit_depth;
145 };
146
147 typedef std::tuple<TestSSEFuncs, int> SSETestParam;
148
149 class SSETest : public ::testing::TestWithParam<SSETestParam> {
150 public:
151 ~SSETest() override = default;
SetUp()152 void SetUp() override {
153 params_ = GET_PARAM(0);
154 width_ = GET_PARAM(1);
155 is_hbd_ =
156 #if CONFIG_VP9_HIGHBITDEPTH
157 params_.ref_func == vpx_highbd_sse_c;
158 #else
159 false;
160 #endif
161 rnd_.Reset(ACMRandom::DeterministicSeed());
162 src_ = reinterpret_cast<uint8_t *>(vpx_memalign(32, 256 * 256 * 2));
163 ref_ = reinterpret_cast<uint8_t *>(vpx_memalign(32, 256 * 256 * 2));
164 ASSERT_NE(src_, nullptr);
165 ASSERT_NE(ref_, nullptr);
166 }
167
TearDown()168 void TearDown() override {
169 vpx_free(src_);
170 vpx_free(ref_);
171 }
172 void RunTest(bool is_random, int width, int height, int run_times);
173
GenRandomData(int width,int height,int stride)174 void GenRandomData(int width, int height, int stride) {
175 uint16_t *src16 = reinterpret_cast<uint16_t *>(src_);
176 uint16_t *ref16 = reinterpret_cast<uint16_t *>(ref_);
177 const int msb = 11; // Up to 12 bit input
178 const int limit = 1 << (msb + 1);
179 for (int ii = 0; ii < height; ii++) {
180 for (int jj = 0; jj < width; jj++) {
181 if (!is_hbd_) {
182 src_[ii * stride + jj] = rnd_.Rand8();
183 ref_[ii * stride + jj] = rnd_.Rand8();
184 } else {
185 src16[ii * stride + jj] = rnd_(limit);
186 ref16[ii * stride + jj] = rnd_(limit);
187 }
188 }
189 }
190 }
191
GenExtremeData(int width,int height,int stride,uint8_t * data,int16_t val)192 void GenExtremeData(int width, int height, int stride, uint8_t *data,
193 int16_t val) {
194 uint16_t *data16 = reinterpret_cast<uint16_t *>(data);
195 for (int ii = 0; ii < height; ii++) {
196 for (int jj = 0; jj < width; jj++) {
197 if (!is_hbd_) {
198 data[ii * stride + jj] = static_cast<uint8_t>(val);
199 } else {
200 data16[ii * stride + jj] = val;
201 }
202 }
203 }
204 }
205
206 protected:
207 bool is_hbd_;
208 int width_;
209 TestSSEFuncs params_;
210 uint8_t *src_;
211 uint8_t *ref_;
212 ACMRandom rnd_;
213 };
214 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SSETest);
215
RunTest(bool is_random,int width,int height,int run_times)216 void SSETest::RunTest(bool is_random, int width, int height, int run_times) {
217 int failed = 0;
218 vpx_usec_timer ref_timer, test_timer;
219 for (int k = 0; k < 3; k++) {
220 int stride = 4 << rnd_(7); // Up to 256 stride
221 while (stride < width) { // Make sure it's valid
222 stride = 4 << rnd_(7);
223 }
224 if (is_random) {
225 GenRandomData(width, height, stride);
226 } else {
227 const int msb = is_hbd_ ? 12 : 8; // Up to 12 bit input
228 const int limit = (1 << msb) - 1;
229 if (k == 0) {
230 GenExtremeData(width, height, stride, src_, 0);
231 GenExtremeData(width, height, stride, ref_, limit);
232 } else {
233 GenExtremeData(width, height, stride, src_, limit);
234 GenExtremeData(width, height, stride, ref_, 0);
235 }
236 }
237 int64_t res_ref, res_tst;
238 uint8_t *src = src_;
239 uint8_t *ref = ref_;
240 #if CONFIG_VP9_HIGHBITDEPTH
241 if (is_hbd_) {
242 src = CONVERT_TO_BYTEPTR(src_);
243 ref = CONVERT_TO_BYTEPTR(ref_);
244 }
245 #endif
246 res_ref = params_.ref_func(src, stride, ref, stride, width, height);
247 res_tst = params_.tst_func(src, stride, ref, stride, width, height);
248 if (run_times > 1) {
249 vpx_usec_timer_start(&ref_timer);
250 for (int j = 0; j < run_times; j++) {
251 params_.ref_func(src, stride, ref, stride, width, height);
252 }
253 vpx_usec_timer_mark(&ref_timer);
254 const int elapsed_time_c =
255 static_cast<int>(vpx_usec_timer_elapsed(&ref_timer));
256
257 vpx_usec_timer_start(&test_timer);
258 for (int j = 0; j < run_times; j++) {
259 params_.tst_func(src, stride, ref, stride, width, height);
260 }
261 vpx_usec_timer_mark(&test_timer);
262 const int elapsed_time_simd =
263 static_cast<int>(vpx_usec_timer_elapsed(&test_timer));
264
265 printf(
266 "c_time=%d \t simd_time=%d \t "
267 "gain=%d\n",
268 elapsed_time_c, elapsed_time_simd,
269 (elapsed_time_c / elapsed_time_simd));
270 } else {
271 if (!failed) {
272 failed = res_ref != res_tst;
273 EXPECT_EQ(res_ref, res_tst)
274 << "Error:" << (is_hbd_ ? "hbd " : " ") << k << " SSE Test ["
275 << width << "x" << height
276 << "] C output does not match optimized output.";
277 }
278 }
279 }
280 }
281
TEST_P(SSETest,OperationCheck)282 TEST_P(SSETest, OperationCheck) {
283 for (int height = 4; height <= 128; height += 4) {
284 RunTest(true, width_, height, 1); // GenRandomData
285 }
286 }
287
TEST_P(SSETest,ExtremeValues)288 TEST_P(SSETest, ExtremeValues) {
289 for (int height = 4; height <= 128; height += 4) {
290 RunTest(false, width_, height, 1);
291 }
292 }
293
TEST_P(SSETest,DISABLED_Speed)294 TEST_P(SSETest, DISABLED_Speed) {
295 for (int height = 4; height <= 128; height += 4) {
296 RunTest(true, width_, height, 100);
297 }
298 }
299
300 #if HAVE_NEON
301 TestSSEFuncs sse_neon[] = {
302 TestSSEFuncs(&vpx_sse_c, &vpx_sse_neon),
303 #if CONFIG_VP9_HIGHBITDEPTH
304 TestSSEFuncs(&vpx_highbd_sse_c, &vpx_highbd_sse_neon)
305 #endif
306 };
307 INSTANTIATE_TEST_SUITE_P(NEON, SSETest,
308 Combine(ValuesIn(sse_neon), Range(4, 129, 4)));
309 #endif // HAVE_NEON
310
311 #if HAVE_NEON_DOTPROD
312 TestSSEFuncs sse_neon_dotprod[] = {
313 TestSSEFuncs(&vpx_sse_c, &vpx_sse_neon_dotprod),
314 };
315 INSTANTIATE_TEST_SUITE_P(NEON_DOTPROD, SSETest,
316 Combine(ValuesIn(sse_neon_dotprod), Range(4, 129, 4)));
317 #endif // HAVE_NEON_DOTPROD
318
319 #if HAVE_SSE4_1
320 TestSSEFuncs sse_sse4[] = {
321 TestSSEFuncs(&vpx_sse_c, &vpx_sse_sse4_1),
322 #if CONFIG_VP9_HIGHBITDEPTH
323 TestSSEFuncs(&vpx_highbd_sse_c, &vpx_highbd_sse_sse4_1)
324 #endif
325 };
326 INSTANTIATE_TEST_SUITE_P(SSE4_1, SSETest,
327 Combine(ValuesIn(sse_sse4), Range(4, 129, 4)));
328 #endif // HAVE_SSE4_1
329
330 #if HAVE_AVX2
331
332 TestSSEFuncs sse_avx2[] = {
333 TestSSEFuncs(&vpx_sse_c, &vpx_sse_avx2),
334 #if CONFIG_VP9_HIGHBITDEPTH
335 TestSSEFuncs(&vpx_highbd_sse_c, &vpx_highbd_sse_avx2)
336 #endif
337 };
338 INSTANTIATE_TEST_SUITE_P(AVX2, SSETest,
339 Combine(ValuesIn(sse_avx2), Range(4, 129, 4)));
340 #endif // HAVE_AVX2
341 } // namespace
342