Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 1 | /* |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 3 | * |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 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. |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 12 | #include <cmath> |
| 13 | #include <cstdlib> |
| 14 | #include <string> |
| 15 | |
Tom Finegan | 7a07ece | 2017-02-07 17:14:05 -0800 | [diff] [blame] | 16 | #include "third_party/googletest/src/googletest/include/gtest/gtest.h" |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 17 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 18 | #include "./aom_config.h" |
| 19 | #include "./aom_dsp_rtcd.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 20 | #include "aom_ports/mem.h" |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 21 | #include "test/acm_random.h" |
| 22 | #include "test/clear_system_state.h" |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 23 | #include "test/register_state_check.h" |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 24 | #include "test/util.h" |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 25 | #include "test/function_equivalence_test.h" |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 26 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 27 | using libaom_test::ACMRandom; |
| 28 | using libaom_test::FunctionEquivalenceTest; |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 29 | |
| 30 | namespace { |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 31 | const int kNumIterations = 10000; |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 32 | |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 33 | static const int16_t kInt13Max = (1 << 12) - 1; |
| 34 | |
Alex Converse | 4c5b020 | 2017-03-29 15:48:40 -0700 | [diff] [blame] | 35 | typedef uint64_t (*SSI16Func)(const int16_t *src, int stride, int width, |
| 36 | int height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 37 | typedef libaom_test::FuncParam<SSI16Func> TestFuncs; |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 38 | |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 39 | class SumSquaresTest : public ::testing::TestWithParam<TestFuncs> { |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 40 | public: |
| 41 | virtual ~SumSquaresTest() {} |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 42 | virtual void SetUp() { params_ = this->GetParam(); } |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 43 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 44 | virtual void TearDown() { libaom_test::ClearSystemState(); } |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 45 | |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 46 | protected: |
Geza Lore | a3f7ddc | 2016-07-12 15:26:36 +0100 | [diff] [blame] | 47 | TestFuncs params_; |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 50 | TEST_P(SumSquaresTest, OperationCheck) { |
| 51 | ACMRandom rnd(ACMRandom::DeterministicSeed()); |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 52 | DECLARE_ALIGNED(16, int16_t, src[256 * 256]); |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 53 | |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 54 | int failed = 0; |
| 55 | |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 56 | const int msb = 11; // Up to 12 bit input |
| 57 | const int limit = 1 << (msb + 1); |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 58 | |
| 59 | for (int k = 0; k < kNumIterations; k++) { |
Alex Converse | 4c5b020 | 2017-03-29 15:48:40 -0700 | [diff] [blame] | 60 | int width = 4 * rnd(32); // Up to 128x128 |
| 61 | int height = 4 * rnd(32); // Up to 128x128 |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 62 | int stride = 4 << rnd(7); // Up to 256 stride |
Alex Converse | 4c5b020 | 2017-03-29 15:48:40 -0700 | [diff] [blame] | 63 | while (stride < width) { // Make sure it's valid |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 64 | stride = 4 << rnd(7); |
| 65 | } |
| 66 | |
Alex Converse | 4c5b020 | 2017-03-29 15:48:40 -0700 | [diff] [blame] | 67 | for (int ii = 0; ii < height; ii++) { |
| 68 | for (int jj = 0; jj < width; jj++) { |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 69 | src[ii * stride + jj] = rnd(2) ? rnd(limit) : -rnd(limit); |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Alex Converse | 4c5b020 | 2017-03-29 15:48:40 -0700 | [diff] [blame] | 73 | const uint64_t res_ref = params_.ref_func(src, stride, width, height); |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 74 | uint64_t res_tst; |
Alex Converse | 4c5b020 | 2017-03-29 15:48:40 -0700 | [diff] [blame] | 75 | ASM_REGISTER_STATE_CHECK(res_tst = |
| 76 | params_.tst_func(src, stride, width, height)); |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 77 | |
| 78 | if (!failed) { |
| 79 | failed = res_ref != res_tst; |
| 80 | EXPECT_EQ(res_ref, res_tst) |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 81 | << "Error: Sum Squares Test" |
| 82 | << " C output does not match optimized output."; |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 83 | } |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 87 | TEST_P(SumSquaresTest, ExtremeValues) { |
| 88 | ACMRandom rnd(ACMRandom::DeterministicSeed()); |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 89 | DECLARE_ALIGNED(16, int16_t, src[256 * 256]); |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 90 | |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 91 | int failed = 0; |
| 92 | |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 93 | const int msb = 11; // Up to 12 bit input |
| 94 | const int limit = 1 << (msb + 1); |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 95 | |
| 96 | for (int k = 0; k < kNumIterations; k++) { |
Alex Converse | 4c5b020 | 2017-03-29 15:48:40 -0700 | [diff] [blame] | 97 | int width = 4 * rnd(32); // Up to 128x128 |
| 98 | int height = 4 * rnd(32); // Up to 128x128 |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 99 | int stride = 4 << rnd(7); // Up to 256 stride |
Alex Converse | 4c5b020 | 2017-03-29 15:48:40 -0700 | [diff] [blame] | 100 | while (stride < width) { // Make sure it's valid |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 101 | stride = 4 << rnd(7); |
| 102 | } |
| 103 | |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 104 | int val = rnd(2) ? limit - 1 : -(limit - 1); |
Alex Converse | 4c5b020 | 2017-03-29 15:48:40 -0700 | [diff] [blame] | 105 | for (int ii = 0; ii < height; ii++) { |
| 106 | for (int jj = 0; jj < width; jj++) { |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 107 | src[ii * stride + jj] = val; |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Alex Converse | 4c5b020 | 2017-03-29 15:48:40 -0700 | [diff] [blame] | 111 | const uint64_t res_ref = params_.ref_func(src, stride, width, height); |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 112 | uint64_t res_tst; |
Alex Converse | 4c5b020 | 2017-03-29 15:48:40 -0700 | [diff] [blame] | 113 | ASM_REGISTER_STATE_CHECK(res_tst = |
| 114 | params_.tst_func(src, stride, width, height)); |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 115 | |
| 116 | if (!failed) { |
| 117 | failed = res_ref != res_tst; |
| 118 | EXPECT_EQ(res_ref, res_tst) |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 119 | << "Error: Sum Squares Test" |
| 120 | << " C output does not match optimized output."; |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 121 | } |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 124 | |
| 125 | #if HAVE_SSE2 |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 126 | |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 127 | INSTANTIATE_TEST_CASE_P( |
James Zern | 5e831c5 | 2016-06-09 23:38:31 -0700 | [diff] [blame] | 128 | SSE2, SumSquaresTest, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 129 | ::testing::Values(TestFuncs(&aom_sum_squares_2d_i16_c, |
| 130 | &aom_sum_squares_2d_i16_sse2))); |
Geza Lore | a3f7ddc | 2016-07-12 15:26:36 +0100 | [diff] [blame] | 131 | |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 132 | #endif // HAVE_SSE2 |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 133 | |
| 134 | ////////////////////////////////////////////////////////////////////////////// |
| 135 | // 1D version |
| 136 | ////////////////////////////////////////////////////////////////////////////// |
| 137 | |
| 138 | typedef uint64_t (*F1D)(const int16_t *src, uint32_t N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 139 | typedef libaom_test::FuncParam<F1D> TestFuncs1D; |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 140 | |
| 141 | class SumSquares1DTest : public FunctionEquivalenceTest<F1D> { |
| 142 | protected: |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 143 | static const int kIterations = 1000; |
| 144 | static const int kMaxSize = 256; |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | TEST_P(SumSquares1DTest, RandomValues) { |
| 148 | DECLARE_ALIGNED(16, int16_t, src[kMaxSize * kMaxSize]); |
| 149 | |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 150 | for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) { |
| 151 | for (int i = 0; i < kMaxSize * kMaxSize; ++i) |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 152 | src[i] = rng_(kInt13Max * 2 + 1) - kInt13Max; |
| 153 | |
| 154 | const int N = rng_(2) ? rng_(kMaxSize * kMaxSize + 1 - kMaxSize) + kMaxSize |
| 155 | : rng_(kMaxSize) + 1; |
| 156 | |
Geza Lore | a3f7ddc | 2016-07-12 15:26:36 +0100 | [diff] [blame] | 157 | const uint64_t ref_res = params_.ref_func(src, N); |
| 158 | uint64_t tst_res; |
| 159 | ASM_REGISTER_STATE_CHECK(tst_res = params_.tst_func(src, N)); |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 160 | |
| 161 | ASSERT_EQ(ref_res, tst_res); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | TEST_P(SumSquares1DTest, ExtremeValues) { |
| 166 | DECLARE_ALIGNED(16, int16_t, src[kMaxSize * kMaxSize]); |
| 167 | |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 168 | for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) { |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 169 | if (rng_(2)) { |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 170 | for (int i = 0; i < kMaxSize * kMaxSize; ++i) src[i] = kInt13Max; |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 171 | } else { |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 172 | for (int i = 0; i < kMaxSize * kMaxSize; ++i) src[i] = -kInt13Max; |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | const int N = rng_(2) ? rng_(kMaxSize * kMaxSize + 1 - kMaxSize) + kMaxSize |
| 176 | : rng_(kMaxSize) + 1; |
| 177 | |
Geza Lore | a3f7ddc | 2016-07-12 15:26:36 +0100 | [diff] [blame] | 178 | const uint64_t ref_res = params_.ref_func(src, N); |
| 179 | uint64_t tst_res; |
| 180 | ASM_REGISTER_STATE_CHECK(tst_res = params_.tst_func(src, N)); |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 181 | |
| 182 | ASSERT_EQ(ref_res, tst_res); |
| 183 | } |
| 184 | } |
| 185 | |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 186 | #if HAVE_SSE2 |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 187 | INSTANTIATE_TEST_CASE_P(SSE2, SumSquares1DTest, |
| 188 | ::testing::Values(TestFuncs1D( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 189 | aom_sum_squares_i16_c, aom_sum_squares_i16_sse2))); |
Geza Lore | a3f7ddc | 2016-07-12 15:26:36 +0100 | [diff] [blame] | 190 | |
Geza Lore | 471362f | 2016-06-21 20:23:13 +0100 | [diff] [blame] | 191 | #endif // HAVE_SSE2 |
Geza Lore | abd0050 | 2016-02-12 16:04:35 +0000 | [diff] [blame] | 192 | } // namespace |