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 <cmath>
13 #include <cstdlib>
14 #include <string>
15 #include <tuple>
16
17 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
18
19 #include "config/aom_config.h"
20 #include "config/aom_dsp_rtcd.h"
21
22 #include "aom_ports/mem.h"
23 #include "test/acm_random.h"
24 #include "test/register_state_check.h"
25 #include "test/util.h"
26 #include "test/function_equivalence_test.h"
27
28 using libaom_test::ACMRandom;
29 using libaom_test::FunctionEquivalenceTest;
30 using ::testing::Combine;
31 using ::testing::Range;
32 using ::testing::Values;
33 using ::testing::ValuesIn;
34
35 namespace {
36 const int kNumIterations = 10000;
37
38 static const int16_t kInt13Max = (1 << 12) - 1;
39
40 typedef uint64_t (*SSI16Func)(const int16_t *src, int stride, int width,
41 int height);
42 typedef libaom_test::FuncParam<SSI16Func> TestFuncs;
43
44 class SumSquaresTest : public ::testing::TestWithParam<TestFuncs> {
45 public:
~SumSquaresTest()46 virtual ~SumSquaresTest() {}
SetUp()47 virtual void SetUp() {
48 params_ = this->GetParam();
49 rnd_.Reset(ACMRandom::DeterministicSeed());
50 src_ = reinterpret_cast<int16_t *>(aom_memalign(16, 256 * 256 * 2));
51 ASSERT_TRUE(src_ != NULL);
52 }
53
TearDown()54 virtual void TearDown() { aom_free(src_); }
55 void RunTest(int isRandom);
56 void RunSpeedTest();
57
GenRandomData(int width,int height,int stride)58 void GenRandomData(int width, int height, int stride) {
59 const int msb = 11; // Up to 12 bit input
60 const int limit = 1 << (msb + 1);
61 for (int ii = 0; ii < height; ii++) {
62 for (int jj = 0; jj < width; jj++) {
63 src_[ii * stride + jj] = rnd_(2) ? rnd_(limit) : -rnd_(limit);
64 }
65 }
66 }
67
GenExtremeData(int width,int height,int stride)68 void GenExtremeData(int width, int height, int stride) {
69 const int msb = 11; // Up to 12 bit input
70 const int limit = 1 << (msb + 1);
71 const int val = rnd_(2) ? limit - 1 : -(limit - 1);
72 for (int ii = 0; ii < height; ii++) {
73 for (int jj = 0; jj < width; jj++) {
74 src_[ii * stride + jj] = val;
75 }
76 }
77 }
78
79 protected:
80 TestFuncs params_;
81 int16_t *src_;
82 ACMRandom rnd_;
83 };
84 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SumSquaresTest);
85
RunTest(int isRandom)86 void SumSquaresTest::RunTest(int isRandom) {
87 int failed = 0;
88 for (int k = 0; k < kNumIterations; k++) {
89 const int width = 4 * (rnd_(31) + 1); // Up to 128x128
90 const int height = 4 * (rnd_(31) + 1); // Up to 128x128
91 int stride = 4 << rnd_(7); // Up to 256 stride
92 while (stride < width) { // Make sure it's valid
93 stride = 4 << rnd_(7);
94 }
95 if (isRandom) {
96 GenRandomData(width, height, stride);
97 } else {
98 GenExtremeData(width, height, stride);
99 }
100 const uint64_t res_ref = params_.ref_func(src_, stride, width, height);
101 uint64_t res_tst;
102 API_REGISTER_STATE_CHECK(res_tst =
103 params_.tst_func(src_, stride, width, height));
104
105 if (!failed) {
106 failed = res_ref != res_tst;
107 EXPECT_EQ(res_ref, res_tst)
108 << "Error: Sum Squares Test [" << width << "x" << height
109 << "] C output does not match optimized output.";
110 }
111 }
112 }
113
RunSpeedTest()114 void SumSquaresTest::RunSpeedTest() {
115 for (int block = BLOCK_4X4; block < BLOCK_SIZES_ALL; block++) {
116 const int width = block_size_wide[block]; // Up to 128x128
117 const int height = block_size_high[block]; // Up to 128x128
118 int stride = 4 << rnd_(7); // Up to 256 stride
119 while (stride < width) { // Make sure it's valid
120 stride = 4 << rnd_(7);
121 }
122 GenExtremeData(width, height, stride);
123 const int num_loops = 1000000000 / (width + height);
124 aom_usec_timer timer;
125 aom_usec_timer_start(&timer);
126
127 for (int i = 0; i < num_loops; ++i)
128 params_.ref_func(src_, stride, width, height);
129
130 aom_usec_timer_mark(&timer);
131 const int elapsed_time = static_cast<int>(aom_usec_timer_elapsed(&timer));
132 printf("SumSquaresTest C %3dx%-3d: %7.2f ns\n", width, height,
133 1000.0 * elapsed_time / num_loops);
134
135 aom_usec_timer timer1;
136 aom_usec_timer_start(&timer1);
137 for (int i = 0; i < num_loops; ++i)
138 params_.tst_func(src_, stride, width, height);
139 aom_usec_timer_mark(&timer1);
140 const int elapsed_time1 = static_cast<int>(aom_usec_timer_elapsed(&timer1));
141 printf("SumSquaresTest Test %3dx%-3d: %7.2f ns\n", width, height,
142 1000.0 * elapsed_time1 / num_loops);
143 }
144 }
145
TEST_P(SumSquaresTest,OperationCheck)146 TEST_P(SumSquaresTest, OperationCheck) {
147 RunTest(1); // GenRandomData
148 }
149
TEST_P(SumSquaresTest,ExtremeValues)150 TEST_P(SumSquaresTest, ExtremeValues) {
151 RunTest(0); // GenExtremeData
152 }
153
TEST_P(SumSquaresTest,DISABLED_Speed)154 TEST_P(SumSquaresTest, DISABLED_Speed) { RunSpeedTest(); }
155
156 #if HAVE_SSE2
157
158 INSTANTIATE_TEST_SUITE_P(
159 SSE2, SumSquaresTest,
160 ::testing::Values(TestFuncs(&aom_sum_squares_2d_i16_c,
161 &aom_sum_squares_2d_i16_sse2)));
162
163 #endif // HAVE_SSE2
164
165 #if HAVE_NEON
166
167 INSTANTIATE_TEST_SUITE_P(
168 NEON, SumSquaresTest,
169 ::testing::Values(TestFuncs(&aom_sum_squares_2d_i16_c,
170 &aom_sum_squares_2d_i16_neon)));
171
172 #endif // HAVE_NEON
173
174 #if HAVE_AVX2
175 INSTANTIATE_TEST_SUITE_P(
176 AVX2, SumSquaresTest,
177 ::testing::Values(TestFuncs(&aom_sum_squares_2d_i16_c,
178 &aom_sum_squares_2d_i16_avx2)));
179 #endif // HAVE_AVX2
180
181 //////////////////////////////////////////////////////////////////////////////
182 // 1D version
183 //////////////////////////////////////////////////////////////////////////////
184
185 typedef uint64_t (*F1D)(const int16_t *src, uint32_t N);
186 typedef libaom_test::FuncParam<F1D> TestFuncs1D;
187
188 class SumSquares1DTest : public FunctionEquivalenceTest<F1D> {
189 protected:
190 static const int kIterations = 1000;
191 static const int kMaxSize = 256;
192 };
193 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SumSquares1DTest);
194
TEST_P(SumSquares1DTest,RandomValues)195 TEST_P(SumSquares1DTest, RandomValues) {
196 DECLARE_ALIGNED(16, int16_t, src[kMaxSize * kMaxSize]);
197
198 for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
199 for (int i = 0; i < kMaxSize * kMaxSize; ++i)
200 src[i] = rng_(kInt13Max * 2 + 1) - kInt13Max;
201
202 const int N = rng_(2) ? rng_(kMaxSize * kMaxSize + 1 - kMaxSize) + kMaxSize
203 : rng_(kMaxSize) + 1;
204
205 const uint64_t ref_res = params_.ref_func(src, N);
206 uint64_t tst_res;
207 API_REGISTER_STATE_CHECK(tst_res = params_.tst_func(src, N));
208
209 ASSERT_EQ(ref_res, tst_res);
210 }
211 }
212
TEST_P(SumSquares1DTest,ExtremeValues)213 TEST_P(SumSquares1DTest, ExtremeValues) {
214 DECLARE_ALIGNED(16, int16_t, src[kMaxSize * kMaxSize]);
215
216 for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
217 if (rng_(2)) {
218 for (int i = 0; i < kMaxSize * kMaxSize; ++i) src[i] = kInt13Max;
219 } else {
220 for (int i = 0; i < kMaxSize * kMaxSize; ++i) src[i] = -kInt13Max;
221 }
222
223 const int N = rng_(2) ? rng_(kMaxSize * kMaxSize + 1 - kMaxSize) + kMaxSize
224 : rng_(kMaxSize) + 1;
225
226 const uint64_t ref_res = params_.ref_func(src, N);
227 uint64_t tst_res;
228 API_REGISTER_STATE_CHECK(tst_res = params_.tst_func(src, N));
229
230 ASSERT_EQ(ref_res, tst_res);
231 }
232 }
233
234 #if HAVE_SSE2
235 INSTANTIATE_TEST_SUITE_P(SSE2, SumSquares1DTest,
236 ::testing::Values(TestFuncs1D(
237 aom_sum_squares_i16_c, aom_sum_squares_i16_sse2)));
238
239 #endif // HAVE_SSE2
240
241 typedef int64_t (*sse_func)(const uint8_t *a, int a_stride, const uint8_t *b,
242 int b_stride, int width, int height);
243 typedef libaom_test::FuncParam<sse_func> TestSSEFuncs;
244
245 typedef std::tuple<TestSSEFuncs, int> SSETestParam;
246
247 class SSETest : public ::testing::TestWithParam<SSETestParam> {
248 public:
~SSETest()249 virtual ~SSETest() {}
SetUp()250 virtual void SetUp() {
251 params_ = GET_PARAM(0);
252 width_ = GET_PARAM(1);
253 isHbd_ =
254 #if CONFIG_AV1_HIGHBITDEPTH
255 params_.ref_func == aom_highbd_sse_c;
256 #else
257 0;
258 #endif
259 rnd_.Reset(ACMRandom::DeterministicSeed());
260 src_ = reinterpret_cast<uint8_t *>(aom_memalign(32, 256 * 256 * 2));
261 ref_ = reinterpret_cast<uint8_t *>(aom_memalign(32, 256 * 256 * 2));
262 ASSERT_TRUE(src_ != NULL);
263 ASSERT_TRUE(ref_ != NULL);
264 }
265
TearDown()266 virtual void TearDown() {
267 aom_free(src_);
268 aom_free(ref_);
269 }
270 void RunTest(int isRandom, int width, int height, int run_times);
271
GenRandomData(int width,int height,int stride)272 void GenRandomData(int width, int height, int stride) {
273 uint16_t *pSrc = (uint16_t *)src_;
274 uint16_t *pRef = (uint16_t *)ref_;
275 const int msb = 11; // Up to 12 bit input
276 const int limit = 1 << (msb + 1);
277 for (int ii = 0; ii < height; ii++) {
278 for (int jj = 0; jj < width; jj++) {
279 if (!isHbd_) {
280 src_[ii * stride + jj] = rnd_.Rand8();
281 ref_[ii * stride + jj] = rnd_.Rand8();
282 } else {
283 pSrc[ii * stride + jj] = rnd_(limit);
284 pRef[ii * stride + jj] = rnd_(limit);
285 }
286 }
287 }
288 }
289
GenExtremeData(int width,int height,int stride,uint8_t * data,int16_t val)290 void GenExtremeData(int width, int height, int stride, uint8_t *data,
291 int16_t val) {
292 uint16_t *pData = (uint16_t *)data;
293 for (int ii = 0; ii < height; ii++) {
294 for (int jj = 0; jj < width; jj++) {
295 if (!isHbd_) {
296 data[ii * stride + jj] = (uint8_t)val;
297 } else {
298 pData[ii * stride + jj] = val;
299 }
300 }
301 }
302 }
303
304 protected:
305 int isHbd_;
306 int width_;
307 TestSSEFuncs params_;
308 uint8_t *src_;
309 uint8_t *ref_;
310 ACMRandom rnd_;
311 };
312 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SSETest);
313
RunTest(int isRandom,int width,int height,int run_times)314 void SSETest::RunTest(int isRandom, int width, int height, int run_times) {
315 int failed = 0;
316 aom_usec_timer ref_timer, test_timer;
317 for (int k = 0; k < 3; k++) {
318 int stride = 4 << rnd_(7); // Up to 256 stride
319 while (stride < width) { // Make sure it's valid
320 stride = 4 << rnd_(7);
321 }
322 if (isRandom) {
323 GenRandomData(width, height, stride);
324 } else {
325 const int msb = isHbd_ ? 12 : 8; // Up to 12 bit input
326 const int limit = (1 << msb) - 1;
327 if (k == 0) {
328 GenExtremeData(width, height, stride, src_, 0);
329 GenExtremeData(width, height, stride, ref_, limit);
330 } else {
331 GenExtremeData(width, height, stride, src_, limit);
332 GenExtremeData(width, height, stride, ref_, 0);
333 }
334 }
335 int64_t res_ref, res_tst;
336 uint8_t *pSrc = src_;
337 uint8_t *pRef = ref_;
338 if (isHbd_) {
339 pSrc = CONVERT_TO_BYTEPTR(src_);
340 pRef = CONVERT_TO_BYTEPTR(ref_);
341 }
342 res_ref = params_.ref_func(pSrc, stride, pRef, stride, width, height);
343 res_tst = params_.tst_func(pSrc, stride, pRef, stride, width, height);
344 if (run_times > 1) {
345 aom_usec_timer_start(&ref_timer);
346 for (int j = 0; j < run_times; j++) {
347 params_.ref_func(pSrc, stride, pRef, stride, width, height);
348 }
349 aom_usec_timer_mark(&ref_timer);
350 const int elapsed_time_c =
351 static_cast<int>(aom_usec_timer_elapsed(&ref_timer));
352
353 aom_usec_timer_start(&test_timer);
354 for (int j = 0; j < run_times; j++) {
355 params_.tst_func(pSrc, stride, pRef, stride, width, height);
356 }
357 aom_usec_timer_mark(&test_timer);
358 const int elapsed_time_simd =
359 static_cast<int>(aom_usec_timer_elapsed(&test_timer));
360
361 printf(
362 "c_time=%d \t simd_time=%d \t "
363 "gain=%d\n",
364 elapsed_time_c, elapsed_time_simd,
365 (elapsed_time_c / elapsed_time_simd));
366 } else {
367 if (!failed) {
368 failed = res_ref != res_tst;
369 EXPECT_EQ(res_ref, res_tst)
370 << "Error:" << (isHbd_ ? "hbd " : " ") << k << " SSE Test ["
371 << width << "x" << height
372 << "] C output does not match optimized output.";
373 }
374 }
375 }
376 }
377
TEST_P(SSETest,OperationCheck)378 TEST_P(SSETest, OperationCheck) {
379 for (int height = 4; height <= 128; height += 4) {
380 RunTest(1, width_, height, 1); // GenRandomData
381 }
382 }
383
TEST_P(SSETest,ExtremeValues)384 TEST_P(SSETest, ExtremeValues) {
385 for (int height = 4; height <= 128; height += 4) {
386 RunTest(0, width_, height, 1);
387 }
388 }
389
TEST_P(SSETest,DISABLED_Speed)390 TEST_P(SSETest, DISABLED_Speed) {
391 for (int height = 4; height <= 128; height += 4) {
392 RunTest(1, width_, height, 100);
393 }
394 }
395
396 #if HAVE_NEON
397 TestSSEFuncs sse_neon[] = {
398 TestSSEFuncs(&aom_sse_c, &aom_sse_neon),
399 #if CONFIG_AV1_HIGHBITDEPTH
400 TestSSEFuncs(&aom_highbd_sse_c, &aom_highbd_sse_neon)
401 #endif
402 };
403 INSTANTIATE_TEST_SUITE_P(NEON, SSETest,
404 Combine(ValuesIn(sse_neon), Range(4, 129, 4)));
405 #endif // HAVE_NEON
406
407 #if HAVE_SSE4_1
408 TestSSEFuncs sse_sse4[] = {
409 TestSSEFuncs(&aom_sse_c, &aom_sse_sse4_1),
410 #if CONFIG_AV1_HIGHBITDEPTH
411 TestSSEFuncs(&aom_highbd_sse_c, &aom_highbd_sse_sse4_1)
412 #endif
413 };
414 INSTANTIATE_TEST_SUITE_P(SSE4_1, SSETest,
415 Combine(ValuesIn(sse_sse4), Range(4, 129, 4)));
416 #endif // HAVE_SSE4_1
417
418 #if HAVE_AVX2
419
420 TestSSEFuncs sse_avx2[] = {
421 TestSSEFuncs(&aom_sse_c, &aom_sse_avx2),
422 #if CONFIG_AV1_HIGHBITDEPTH
423 TestSSEFuncs(&aom_highbd_sse_c, &aom_highbd_sse_avx2)
424 #endif
425 };
426 INSTANTIATE_TEST_SUITE_P(AVX2, SSETest,
427 Combine(ValuesIn(sse_avx2), Range(4, 129, 4)));
428 #endif // HAVE_AVX2
429
430 //////////////////////////////////////////////////////////////////////////////
431 // get_blk sum squares test functions
432 //////////////////////////////////////////////////////////////////////////////
433
434 typedef void (*sse_sum_func)(const int16_t *data, int stride, int bw, int bh,
435 int *x_sum, int64_t *x2_sum);
436 typedef libaom_test::FuncParam<sse_sum_func> TestSSE_SumFuncs;
437
438 typedef std::tuple<TestSSE_SumFuncs, int> SSE_SumTestParam;
439
440 class SSE_Sum_Test : public ::testing::TestWithParam<SSE_SumTestParam> {
441 public:
~SSE_Sum_Test()442 virtual ~SSE_Sum_Test() {}
SetUp()443 virtual void SetUp() {
444 params_ = GET_PARAM(0);
445 width_ = GET_PARAM(1);
446 rnd_.Reset(ACMRandom::DeterministicSeed());
447 src_ = reinterpret_cast<int16_t *>(aom_memalign(32, 256 * 256 * 2));
448 ASSERT_TRUE(src_ != NULL);
449 }
450
TearDown()451 virtual void TearDown() { aom_free(src_); }
452 void RunTest(int isRandom, int width, int height, int run_times);
453
GenRandomData(int width,int height,int stride)454 void GenRandomData(int width, int height, int stride) {
455 const int msb = 11; // Up to 12 bit input
456 const int limit = 1 << (msb + 1);
457 for (int ii = 0; ii < height; ii++) {
458 for (int jj = 0; jj < width; jj++) {
459 src_[ii * stride + jj] = rnd_(limit);
460 }
461 }
462 }
463
GenExtremeData(int width,int height,int stride,int16_t * data,int16_t val)464 void GenExtremeData(int width, int height, int stride, int16_t *data,
465 int16_t val) {
466 for (int ii = 0; ii < height; ii++) {
467 for (int jj = 0; jj < width; jj++) {
468 data[ii * stride + jj] = val;
469 }
470 }
471 }
472
473 protected:
474 int width_;
475 TestSSE_SumFuncs params_;
476 int16_t *src_;
477 ACMRandom rnd_;
478 };
479 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SSE_Sum_Test);
480
RunTest(int isRandom,int width,int height,int run_times)481 void SSE_Sum_Test::RunTest(int isRandom, int width, int height, int run_times) {
482 aom_usec_timer ref_timer, test_timer;
483 for (int k = 0; k < 3; k++) {
484 int stride = 4 << rnd_(7); // Up to 256 stride
485 while (stride < width) { // Make sure it's valid
486 stride = 4 << rnd_(7);
487 }
488 if (isRandom) {
489 GenRandomData(width, height, stride);
490 } else {
491 const int msb = 12; // Up to 12 bit input
492 const int limit = (1 << msb) - 1;
493 if (k == 0) {
494 GenExtremeData(width, height, stride, src_, limit);
495 } else {
496 GenExtremeData(width, height, stride, src_, -limit);
497 }
498 }
499 int sum_c = 0;
500 int64_t sse_intr = 0;
501 int sum_intr = 0;
502 int64_t sse_c = 0;
503
504 params_.ref_func(src_, stride, width, height, &sum_c, &sse_c);
505 params_.tst_func(src_, stride, width, height, &sum_intr, &sse_intr);
506
507 if (run_times > 1) {
508 aom_usec_timer_start(&ref_timer);
509 for (int j = 0; j < run_times; j++) {
510 params_.ref_func(src_, stride, width, height, &sum_c, &sse_c);
511 }
512 aom_usec_timer_mark(&ref_timer);
513 const int elapsed_time_c =
514 static_cast<int>(aom_usec_timer_elapsed(&ref_timer));
515
516 aom_usec_timer_start(&test_timer);
517 for (int j = 0; j < run_times; j++) {
518 params_.tst_func(src_, stride, width, height, &sum_intr, &sse_intr);
519 }
520 aom_usec_timer_mark(&test_timer);
521 const int elapsed_time_simd =
522 static_cast<int>(aom_usec_timer_elapsed(&test_timer));
523
524 printf(
525 "c_time=%d \t simd_time=%d \t "
526 "gain=%f\t width=%d\t height=%d \n",
527 elapsed_time_c, elapsed_time_simd,
528 (float)((float)elapsed_time_c / (float)elapsed_time_simd), width,
529 height);
530
531 } else {
532 EXPECT_EQ(sum_c, sum_intr)
533 << "Error:" << k << " SSE Sum Test [" << width << "x" << height
534 << "] C output does not match optimized output.";
535 EXPECT_EQ(sse_c, sse_intr)
536 << "Error:" << k << " SSE Sum Test [" << width << "x" << height
537 << "] C output does not match optimized output.";
538 }
539 }
540 }
541
TEST_P(SSE_Sum_Test,OperationCheck)542 TEST_P(SSE_Sum_Test, OperationCheck) {
543 for (int height = 4; height <= 64; height = height * 2) {
544 RunTest(1, width_, height, 1); // GenRandomData
545 }
546 }
547
TEST_P(SSE_Sum_Test,ExtremeValues)548 TEST_P(SSE_Sum_Test, ExtremeValues) {
549 for (int height = 4; height <= 64; height = height * 2) {
550 RunTest(0, width_, height, 1);
551 }
552 }
553
TEST_P(SSE_Sum_Test,DISABLED_Speed)554 TEST_P(SSE_Sum_Test, DISABLED_Speed) {
555 for (int height = 4; height <= 64; height = height * 2) {
556 RunTest(1, width_, height, 10000);
557 }
558 }
559
560 #if HAVE_SSE2
561 TestSSE_SumFuncs sse_sum_sse2[] = { TestSSE_SumFuncs(
562 &aom_get_blk_sse_sum_c, &aom_get_blk_sse_sum_sse2) };
563 INSTANTIATE_TEST_SUITE_P(SSE2, SSE_Sum_Test,
564 Combine(ValuesIn(sse_sum_sse2), Range(4, 65, 4)));
565 #endif // HAVE_SSE2
566
567 #if HAVE_AVX2
568 TestSSE_SumFuncs sse_sum_avx2[] = { TestSSE_SumFuncs(
569 &aom_get_blk_sse_sum_c, &aom_get_blk_sse_sum_avx2) };
570 INSTANTIATE_TEST_SUITE_P(AVX2, SSE_Sum_Test,
571 Combine(ValuesIn(sse_sum_avx2), Range(4, 65, 4)));
572 #endif // HAVE_AVX2
573
574 //////////////////////////////////////////////////////////////////////////////
575 // 2D Variance test functions
576 //////////////////////////////////////////////////////////////////////////////
577
578 typedef uint64_t (*Var2DFunc)(uint8_t *src, int stride, int width, int height);
579 typedef libaom_test::FuncParam<Var2DFunc> TestFuncVar2D;
580
581 const uint16_t test_block_size[2] = { 128, 256 };
582
583 class Lowbd2dVarTest : public ::testing::TestWithParam<TestFuncVar2D> {
584 public:
~Lowbd2dVarTest()585 virtual ~Lowbd2dVarTest() {}
SetUp()586 virtual void SetUp() {
587 params_ = this->GetParam();
588 rnd_.Reset(ACMRandom::DeterministicSeed());
589 src_ = reinterpret_cast<uint8_t *>(
590 aom_memalign(16, 512 * 512 * sizeof(uint8_t)));
591 ASSERT_TRUE(src_ != NULL);
592 }
593
TearDown()594 virtual void TearDown() { aom_free(src_); }
595 void RunTest(int isRandom);
596 void RunSpeedTest();
597
GenRandomData(int width,int height,int stride)598 void GenRandomData(int width, int height, int stride) {
599 const int msb = 7; // Up to 8 bit input
600 const int limit = 1 << (msb + 1);
601 for (int ii = 0; ii < height; ii++) {
602 for (int jj = 0; jj < width; jj++) {
603 src_[ii * stride + jj] = rnd_(limit);
604 }
605 }
606 }
607
GenExtremeData(int width,int height,int stride)608 void GenExtremeData(int width, int height, int stride) {
609 const int msb = 7; // Up to 8 bit input
610 const int limit = 1 << (msb + 1);
611 const int val = rnd_(2) ? limit - 1 : 0;
612 for (int ii = 0; ii < height; ii++) {
613 for (int jj = 0; jj < width; jj++) {
614 src_[ii * stride + jj] = val;
615 }
616 }
617 }
618
619 protected:
620 TestFuncVar2D params_;
621 uint8_t *src_;
622 ACMRandom rnd_;
623 };
624 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Lowbd2dVarTest);
625
RunTest(int isRandom)626 void Lowbd2dVarTest::RunTest(int isRandom) {
627 int failed = 0;
628 for (int k = 0; k < kNumIterations; k++) {
629 const int width = 4 * (rnd_(63) + 1); // Up to 256x256
630 const int height = 4 * (rnd_(63) + 1); // Up to 256x256
631 int stride = 4 << rnd_(8); // Up to 512 stride
632 while (stride < width) { // Make sure it's valid
633 stride = 4 << rnd_(8);
634 }
635 if (isRandom) {
636 GenRandomData(width, height, stride);
637 } else {
638 GenExtremeData(width, height, stride);
639 }
640
641 const uint64_t res_ref = params_.ref_func(src_, stride, width, height);
642 uint64_t res_tst;
643 API_REGISTER_STATE_CHECK(res_tst =
644 params_.tst_func(src_, stride, width, height));
645
646 if (!failed) {
647 failed = res_ref != res_tst;
648 EXPECT_EQ(res_ref, res_tst)
649 << "Error: Sum Squares Test [" << width << "x" << height
650 << "] C output does not match optimized output.";
651 }
652 }
653 }
654
RunSpeedTest()655 void Lowbd2dVarTest::RunSpeedTest() {
656 for (int block = 0; block < 2; block++) {
657 const int width = test_block_size[block];
658 const int height = test_block_size[block];
659 int stride = 4 << rnd_(8); // Up to 512 stride
660 while (stride < width) { // Make sure it's valid
661 stride = 4 << rnd_(8);
662 }
663 GenExtremeData(width, height, stride);
664 const int num_loops = 1000000000 / (width + height);
665 aom_usec_timer timer;
666 aom_usec_timer_start(&timer);
667
668 for (int i = 0; i < num_loops; ++i)
669 params_.ref_func(src_, stride, width, height);
670
671 aom_usec_timer_mark(&timer);
672 const int elapsed_time = static_cast<int>(aom_usec_timer_elapsed(&timer));
673
674 aom_usec_timer timer1;
675 aom_usec_timer_start(&timer1);
676 for (int i = 0; i < num_loops; ++i)
677 params_.tst_func(src_, stride, width, height);
678 aom_usec_timer_mark(&timer1);
679 const int elapsed_time1 = static_cast<int>(aom_usec_timer_elapsed(&timer1));
680 printf("%3dx%-3d: Scaling = %.2f\n", width, height,
681 (double)elapsed_time / elapsed_time1);
682 }
683 }
684
TEST_P(Lowbd2dVarTest,OperationCheck)685 TEST_P(Lowbd2dVarTest, OperationCheck) {
686 RunTest(1); // GenRandomData
687 }
688
TEST_P(Lowbd2dVarTest,ExtremeValues)689 TEST_P(Lowbd2dVarTest, ExtremeValues) {
690 RunTest(0); // GenExtremeData
691 }
692
TEST_P(Lowbd2dVarTest,DISABLED_Speed)693 TEST_P(Lowbd2dVarTest, DISABLED_Speed) { RunSpeedTest(); }
694
695 #if HAVE_SSE2
696
697 INSTANTIATE_TEST_SUITE_P(SSE2, Lowbd2dVarTest,
698 ::testing::Values(TestFuncVar2D(&aom_var_2d_u8_c,
699 &aom_var_2d_u8_sse2)));
700
701 #endif // HAVE_SSE2
702
703 #if HAVE_AVX2
704
705 INSTANTIATE_TEST_SUITE_P(AVX2, Lowbd2dVarTest,
706 ::testing::Values(TestFuncVar2D(&aom_var_2d_u8_c,
707 &aom_var_2d_u8_avx2)));
708
709 #endif // HAVE_SSE2
710
711 class Highbd2dVarTest : public ::testing::TestWithParam<TestFuncVar2D> {
712 public:
~Highbd2dVarTest()713 virtual ~Highbd2dVarTest() {}
SetUp()714 virtual void SetUp() {
715 params_ = this->GetParam();
716 rnd_.Reset(ACMRandom::DeterministicSeed());
717 src_ = reinterpret_cast<uint16_t *>(
718 aom_memalign(16, 512 * 512 * sizeof(uint16_t)));
719 ASSERT_TRUE(src_ != NULL);
720 }
721
TearDown()722 virtual void TearDown() { aom_free(src_); }
723 void RunTest(int isRandom);
724 void RunSpeedTest();
725
GenRandomData(int width,int height,int stride)726 void GenRandomData(int width, int height, int stride) {
727 const int msb = 11; // Up to 12 bit input
728 const int limit = 1 << (msb + 1);
729 for (int ii = 0; ii < height; ii++) {
730 for (int jj = 0; jj < width; jj++) {
731 src_[ii * stride + jj] = rnd_(limit);
732 }
733 }
734 }
735
GenExtremeData(int width,int height,int stride)736 void GenExtremeData(int width, int height, int stride) {
737 const int msb = 11; // Up to 12 bit input
738 const int limit = 1 << (msb + 1);
739 const int val = rnd_(2) ? limit - 1 : 0;
740 for (int ii = 0; ii < height; ii++) {
741 for (int jj = 0; jj < width; jj++) {
742 src_[ii * stride + jj] = val;
743 }
744 }
745 }
746
747 protected:
748 TestFuncVar2D params_;
749 uint16_t *src_;
750 ACMRandom rnd_;
751 };
752 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Highbd2dVarTest);
753
RunTest(int isRandom)754 void Highbd2dVarTest::RunTest(int isRandom) {
755 int failed = 0;
756 for (int k = 0; k < kNumIterations; k++) {
757 const int width = 4 * (rnd_(63) + 1); // Up to 256x256
758 const int height = 4 * (rnd_(63) + 1); // Up to 256x256
759 int stride = 4 << rnd_(8); // Up to 512 stride
760 while (stride < width) { // Make sure it's valid
761 stride = 4 << rnd_(8);
762 }
763 if (isRandom) {
764 GenRandomData(width, height, stride);
765 } else {
766 GenExtremeData(width, height, stride);
767 }
768
769 const uint64_t res_ref =
770 params_.ref_func(CONVERT_TO_BYTEPTR(src_), stride, width, height);
771 uint64_t res_tst;
772 API_REGISTER_STATE_CHECK(
773 res_tst =
774 params_.tst_func(CONVERT_TO_BYTEPTR(src_), stride, width, height));
775
776 if (!failed) {
777 failed = res_ref != res_tst;
778 EXPECT_EQ(res_ref, res_tst)
779 << "Error: Sum Squares Test [" << width << "x" << height
780 << "] C output does not match optimized output.";
781 }
782 }
783 }
784
RunSpeedTest()785 void Highbd2dVarTest::RunSpeedTest() {
786 for (int block = 0; block < 2; block++) {
787 const int width = test_block_size[block];
788 const int height = test_block_size[block];
789 int stride = 4 << rnd_(8); // Up to 512 stride
790 while (stride < width) { // Make sure it's valid
791 stride = 4 << rnd_(8);
792 }
793 GenExtremeData(width, height, stride);
794 const int num_loops = 1000000000 / (width + height);
795 aom_usec_timer timer;
796 aom_usec_timer_start(&timer);
797
798 for (int i = 0; i < num_loops; ++i)
799 params_.ref_func(CONVERT_TO_BYTEPTR(src_), stride, width, height);
800
801 aom_usec_timer_mark(&timer);
802 const int elapsed_time = static_cast<int>(aom_usec_timer_elapsed(&timer));
803
804 aom_usec_timer timer1;
805 aom_usec_timer_start(&timer1);
806 for (int i = 0; i < num_loops; ++i)
807 params_.tst_func(CONVERT_TO_BYTEPTR(src_), stride, width, height);
808 aom_usec_timer_mark(&timer1);
809 const int elapsed_time1 = static_cast<int>(aom_usec_timer_elapsed(&timer1));
810 printf("%3dx%-3d: Scaling = %.2f\n", width, height,
811 (double)elapsed_time / elapsed_time1);
812 }
813 }
814
TEST_P(Highbd2dVarTest,OperationCheck)815 TEST_P(Highbd2dVarTest, OperationCheck) {
816 RunTest(1); // GenRandomData
817 }
818
TEST_P(Highbd2dVarTest,ExtremeValues)819 TEST_P(Highbd2dVarTest, ExtremeValues) {
820 RunTest(0); // GenExtremeData
821 }
822
TEST_P(Highbd2dVarTest,DISABLED_Speed)823 TEST_P(Highbd2dVarTest, DISABLED_Speed) { RunSpeedTest(); }
824
825 #if HAVE_SSE2
826
827 INSTANTIATE_TEST_SUITE_P(
828 SSE2, Highbd2dVarTest,
829 ::testing::Values(TestFuncVar2D(&aom_var_2d_u16_c, &aom_var_2d_u16_sse2)));
830
831 #endif // HAVE_SSE2
832
833 #if HAVE_AVX2
834
835 INSTANTIATE_TEST_SUITE_P(
836 AVX2, Highbd2dVarTest,
837 ::testing::Values(TestFuncVar2D(&aom_var_2d_u16_c, &aom_var_2d_u16_avx2)));
838
839 #endif // HAVE_SSE2
840 } // namespace
841