xref: /aosp_15_r20/external/libvpx/test/sum_squares_test.cc (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 "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_SVE
123 INSTANTIATE_TEST_SUITE_P(
124     SVE, SumSquaresTest,
125     ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
126                                  &vpx_sum_squares_2d_i16_sve)));
127 #endif  // HAVE_SVE
128 
129 #if HAVE_SSE2
130 INSTANTIATE_TEST_SUITE_P(
131     SSE2, SumSquaresTest,
132     ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
133                                  &vpx_sum_squares_2d_i16_sse2)));
134 #endif  // HAVE_SSE2
135 
136 #if HAVE_MSA
137 INSTANTIATE_TEST_SUITE_P(
138     MSA, SumSquaresTest,
139     ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
140                                  &vpx_sum_squares_2d_i16_msa)));
141 #endif  // HAVE_MSA
142 
143 typedef int64_t (*SSEFunc)(const uint8_t *a, int a_stride, const uint8_t *b,
144                            int b_stride, int width, int height);
145 
146 struct TestSSEFuncs {
TestSSEFuncs__anon7d1888850111::TestSSEFuncs147   TestSSEFuncs(SSEFunc ref = nullptr, SSEFunc tst = nullptr, int depth = 0)
148       : ref_func(ref), tst_func(tst), bit_depth(depth) {}
149   SSEFunc ref_func;  // Pointer to reference function
150   SSEFunc tst_func;  // Pointer to tested function
151   int bit_depth;
152 };
153 
154 typedef std::tuple<TestSSEFuncs, int> SSETestParam;
155 
156 class SSETest : public ::testing::TestWithParam<SSETestParam> {
157  public:
158   ~SSETest() override = default;
SetUp()159   void SetUp() override {
160     params_ = GET_PARAM(0);
161     width_ = GET_PARAM(1);
162     is_hbd_ =
163 #if CONFIG_VP9_HIGHBITDEPTH
164         params_.ref_func == vpx_highbd_sse_c;
165 #else
166         false;
167 #endif
168     rnd_.Reset(ACMRandom::DeterministicSeed());
169     src_ = reinterpret_cast<uint8_t *>(vpx_memalign(32, 256 * 256 * 2));
170     ref_ = reinterpret_cast<uint8_t *>(vpx_memalign(32, 256 * 256 * 2));
171     ASSERT_NE(src_, nullptr);
172     ASSERT_NE(ref_, nullptr);
173   }
174 
TearDown()175   void TearDown() override {
176     vpx_free(src_);
177     vpx_free(ref_);
178   }
179   void RunTest(bool is_random, int width, int height, int run_times);
180 
GenRandomData(int width,int height,int stride)181   void GenRandomData(int width, int height, int stride) {
182     uint16_t *src16 = reinterpret_cast<uint16_t *>(src_);
183     uint16_t *ref16 = reinterpret_cast<uint16_t *>(ref_);
184     const int msb = 11;  // Up to 12 bit input
185     const int limit = 1 << (msb + 1);
186     for (int ii = 0; ii < height; ii++) {
187       for (int jj = 0; jj < width; jj++) {
188         if (!is_hbd_) {
189           src_[ii * stride + jj] = rnd_.Rand8();
190           ref_[ii * stride + jj] = rnd_.Rand8();
191         } else {
192           src16[ii * stride + jj] = rnd_(limit);
193           ref16[ii * stride + jj] = rnd_(limit);
194         }
195       }
196     }
197   }
198 
GenExtremeData(int width,int height,int stride,uint8_t * data,int16_t val)199   void GenExtremeData(int width, int height, int stride, uint8_t *data,
200                       int16_t val) {
201     uint16_t *data16 = reinterpret_cast<uint16_t *>(data);
202     for (int ii = 0; ii < height; ii++) {
203       for (int jj = 0; jj < width; jj++) {
204         if (!is_hbd_) {
205           data[ii * stride + jj] = static_cast<uint8_t>(val);
206         } else {
207           data16[ii * stride + jj] = val;
208         }
209       }
210     }
211   }
212 
213  protected:
214   bool is_hbd_;
215   int width_;
216   TestSSEFuncs params_;
217   uint8_t *src_;
218   uint8_t *ref_;
219   ACMRandom rnd_;
220 };
221 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SSETest);
222 
RunTest(bool is_random,int width,int height,int run_times)223 void SSETest::RunTest(bool is_random, int width, int height, int run_times) {
224   int failed = 0;
225   vpx_usec_timer ref_timer, test_timer;
226   for (int k = 0; k < 3; k++) {
227     int stride = 4 << rnd_(7);  // Up to 256 stride
228     while (stride < width) {    // Make sure it's valid
229       stride = 4 << rnd_(7);
230     }
231     if (is_random) {
232       GenRandomData(width, height, stride);
233     } else {
234       const int msb = is_hbd_ ? 12 : 8;  // Up to 12 bit input
235       const int limit = (1 << msb) - 1;
236       if (k == 0) {
237         GenExtremeData(width, height, stride, src_, 0);
238         GenExtremeData(width, height, stride, ref_, limit);
239       } else {
240         GenExtremeData(width, height, stride, src_, limit);
241         GenExtremeData(width, height, stride, ref_, 0);
242       }
243     }
244     int64_t res_ref, res_tst;
245     uint8_t *src = src_;
246     uint8_t *ref = ref_;
247 #if CONFIG_VP9_HIGHBITDEPTH
248     if (is_hbd_) {
249       src = CONVERT_TO_BYTEPTR(src_);
250       ref = CONVERT_TO_BYTEPTR(ref_);
251     }
252 #endif
253     res_ref = params_.ref_func(src, stride, ref, stride, width, height);
254     res_tst = params_.tst_func(src, stride, ref, stride, width, height);
255     if (run_times > 1) {
256       vpx_usec_timer_start(&ref_timer);
257       for (int j = 0; j < run_times; j++) {
258         params_.ref_func(src, stride, ref, stride, width, height);
259       }
260       vpx_usec_timer_mark(&ref_timer);
261       const int elapsed_time_c =
262           static_cast<int>(vpx_usec_timer_elapsed(&ref_timer));
263 
264       vpx_usec_timer_start(&test_timer);
265       for (int j = 0; j < run_times; j++) {
266         params_.tst_func(src, stride, ref, stride, width, height);
267       }
268       vpx_usec_timer_mark(&test_timer);
269       const int elapsed_time_simd =
270           static_cast<int>(vpx_usec_timer_elapsed(&test_timer));
271 
272       printf(
273           "c_time=%d \t simd_time=%d \t "
274           "gain=%d\n",
275           elapsed_time_c, elapsed_time_simd,
276           (elapsed_time_c / elapsed_time_simd));
277     } else {
278       if (!failed) {
279         failed = res_ref != res_tst;
280         EXPECT_EQ(res_ref, res_tst)
281             << "Error:" << (is_hbd_ ? "hbd " : " ") << k << " SSE Test ["
282             << width << "x" << height
283             << "] C output does not match optimized output.";
284       }
285     }
286   }
287 }
288 
TEST_P(SSETest,OperationCheck)289 TEST_P(SSETest, OperationCheck) {
290   for (int height = 4; height <= 128; height += 4) {
291     RunTest(true, width_, height, 1);  // GenRandomData
292   }
293 }
294 
TEST_P(SSETest,ExtremeValues)295 TEST_P(SSETest, ExtremeValues) {
296   for (int height = 4; height <= 128; height += 4) {
297     RunTest(false, width_, height, 1);
298   }
299 }
300 
TEST_P(SSETest,DISABLED_Speed)301 TEST_P(SSETest, DISABLED_Speed) {
302   for (int height = 4; height <= 128; height += 4) {
303     RunTest(true, width_, height, 100);
304   }
305 }
306 
307 #if HAVE_NEON
308 TestSSEFuncs sse_neon[] = {
309   TestSSEFuncs(&vpx_sse_c, &vpx_sse_neon),
310 #if CONFIG_VP9_HIGHBITDEPTH
311   TestSSEFuncs(&vpx_highbd_sse_c, &vpx_highbd_sse_neon)
312 #endif
313 };
314 INSTANTIATE_TEST_SUITE_P(NEON, SSETest,
315                          Combine(ValuesIn(sse_neon), Range(4, 129, 4)));
316 #endif  // HAVE_NEON
317 
318 #if HAVE_NEON_DOTPROD
319 TestSSEFuncs sse_neon_dotprod[] = {
320   TestSSEFuncs(&vpx_sse_c, &vpx_sse_neon_dotprod),
321 };
322 INSTANTIATE_TEST_SUITE_P(NEON_DOTPROD, SSETest,
323                          Combine(ValuesIn(sse_neon_dotprod), Range(4, 129, 4)));
324 #endif  // HAVE_NEON_DOTPROD
325 
326 #if HAVE_SSE4_1
327 TestSSEFuncs sse_sse4[] = {
328   TestSSEFuncs(&vpx_sse_c, &vpx_sse_sse4_1),
329 #if CONFIG_VP9_HIGHBITDEPTH
330   TestSSEFuncs(&vpx_highbd_sse_c, &vpx_highbd_sse_sse4_1)
331 #endif
332 };
333 INSTANTIATE_TEST_SUITE_P(SSE4_1, SSETest,
334                          Combine(ValuesIn(sse_sse4), Range(4, 129, 4)));
335 #endif  // HAVE_SSE4_1
336 
337 #if HAVE_AVX2
338 
339 TestSSEFuncs sse_avx2[] = {
340   TestSSEFuncs(&vpx_sse_c, &vpx_sse_avx2),
341 #if CONFIG_VP9_HIGHBITDEPTH
342   TestSSEFuncs(&vpx_highbd_sse_c, &vpx_highbd_sse_avx2)
343 #endif
344 };
345 INSTANTIATE_TEST_SUITE_P(AVX2, SSETest,
346                          Combine(ValuesIn(sse_avx2), Range(4, 129, 4)));
347 #endif  // HAVE_AVX2
348 }  // namespace
349