vp9[loongarch]: Optimize fdct4x4/8x8_lsx
[platform/upstream/libvpx.git] / test / sum_squares_test.cc
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 <cstdlib>
13 #include <string>
14 #include <tuple>
15
16 #include "third_party/googletest/src/include/gtest/gtest.h"
17
18 #include "./vpx_config.h"
19 #include "./vpx_dsp_rtcd.h"
20 #include "test/acm_random.h"
21 #include "test/clear_system_state.h"
22 #include "test/register_state_check.h"
23 #include "test/util.h"
24 #include "vpx_ports/mem.h"
25
26 using libvpx_test::ACMRandom;
27
28 namespace {
29 const int kNumIterations = 10000;
30
31 typedef uint64_t (*SSI16Func)(const int16_t *src, int stride, int size);
32 typedef std::tuple<SSI16Func, SSI16Func> SumSquaresParam;
33
34 class SumSquaresTest : public ::testing::TestWithParam<SumSquaresParam> {
35  public:
36   virtual ~SumSquaresTest() {}
37   virtual void SetUp() {
38     ref_func_ = GET_PARAM(0);
39     tst_func_ = GET_PARAM(1);
40   }
41
42   virtual void TearDown() { libvpx_test::ClearSystemState(); }
43
44  protected:
45   SSI16Func ref_func_;
46   SSI16Func tst_func_;
47 };
48 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SumSquaresTest);
49
50 TEST_P(SumSquaresTest, OperationCheck) {
51   ACMRandom rnd(ACMRandom::DeterministicSeed());
52   DECLARE_ALIGNED(16, int16_t, src[256 * 256]);
53   const int msb = 11;  // Up to 12 bit input
54   const int limit = 1 << (msb + 1);
55
56   for (int k = 0; k < kNumIterations; k++) {
57     const int size = 4 << rnd(6);  // Up to 128x128
58     int stride = 4 << rnd(7);      // Up to 256 stride
59     while (stride < size) {        // Make sure it's valid
60       stride = 4 << rnd(7);
61     }
62
63     for (int i = 0; i < size; ++i) {
64       for (int j = 0; j < size; ++j) {
65         src[i * stride + j] = rnd(2) ? rnd(limit) : -rnd(limit);
66       }
67     }
68
69     const uint64_t res_ref = ref_func_(src, stride, size);
70     uint64_t res_tst;
71     ASM_REGISTER_STATE_CHECK(res_tst = tst_func_(src, stride, size));
72
73     ASSERT_EQ(res_ref, res_tst) << "Error: Sum Squares Test"
74                                 << " C output does not match optimized output.";
75   }
76 }
77
78 TEST_P(SumSquaresTest, ExtremeValues) {
79   ACMRandom rnd(ACMRandom::DeterministicSeed());
80   DECLARE_ALIGNED(16, int16_t, src[256 * 256]);
81   const int msb = 11;  // Up to 12 bit input
82   const int limit = 1 << (msb + 1);
83
84   for (int k = 0; k < kNumIterations; k++) {
85     const int size = 4 << rnd(6);  // Up to 128x128
86     int stride = 4 << rnd(7);      // Up to 256 stride
87     while (stride < size) {        // Make sure it's valid
88       stride = 4 << rnd(7);
89     }
90
91     const int val = rnd(2) ? limit - 1 : -(limit - 1);
92     for (int i = 0; i < size; ++i) {
93       for (int j = 0; j < size; ++j) {
94         src[i * stride + j] = val;
95       }
96     }
97
98     const uint64_t res_ref = ref_func_(src, stride, size);
99     uint64_t res_tst;
100     ASM_REGISTER_STATE_CHECK(res_tst = tst_func_(src, stride, size));
101
102     ASSERT_EQ(res_ref, res_tst) << "Error: Sum Squares Test"
103                                 << " C output does not match optimized output.";
104   }
105 }
106
107 using std::make_tuple;
108
109 #if HAVE_NEON
110 INSTANTIATE_TEST_SUITE_P(
111     NEON, SumSquaresTest,
112     ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
113                                  &vpx_sum_squares_2d_i16_neon)));
114 #endif  // HAVE_NEON
115
116 #if HAVE_SSE2
117 INSTANTIATE_TEST_SUITE_P(
118     SSE2, SumSquaresTest,
119     ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
120                                  &vpx_sum_squares_2d_i16_sse2)));
121 #endif  // HAVE_SSE2
122
123 #if HAVE_MSA
124 INSTANTIATE_TEST_SUITE_P(
125     MSA, SumSquaresTest,
126     ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
127                                  &vpx_sum_squares_2d_i16_msa)));
128 #endif  // HAVE_MSA
129 }  // namespace