• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2019, Alliance for Open Media. 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 <algorithm>
12 #include <ostream>
13 
14 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
15 
16 #include "config/aom_dsp_rtcd.h"
17 
18 #include "test/acm_random.h"
19 #include "test/register_state_check.h"
20 #include "test/util.h"
21 
22 namespace {
23 
24 using libaom_test::ACMRandom;
25 
26 typedef void (*HadamardFunc)(const int16_t *a, ptrdiff_t a_stride,
27                              tran_low_t *b);
28 
HadamardLoop(const tran_low_t * a,tran_low_t * out)29 void HadamardLoop(const tran_low_t *a, tran_low_t *out) {
30   tran_low_t b[8];
31   for (int i = 0; i < 8; i += 2) {
32     b[i + 0] = a[i * 8] + a[(i + 1) * 8];
33     b[i + 1] = a[i * 8] - a[(i + 1) * 8];
34   }
35   tran_low_t c[8];
36   for (int i = 0; i < 8; i += 4) {
37     c[i + 0] = b[i + 0] + b[i + 2];
38     c[i + 1] = b[i + 1] + b[i + 3];
39     c[i + 2] = b[i + 0] - b[i + 2];
40     c[i + 3] = b[i + 1] - b[i + 3];
41   }
42   out[0] = c[0] + c[4];
43   out[7] = c[1] + c[5];
44   out[3] = c[2] + c[6];
45   out[4] = c[3] + c[7];
46   out[2] = c[0] - c[4];
47   out[6] = c[1] - c[5];
48   out[1] = c[2] - c[6];
49   out[5] = c[3] - c[7];
50 }
51 
ReferenceHadamard8x8(const int16_t * a,int a_stride,tran_low_t * b)52 void ReferenceHadamard8x8(const int16_t *a, int a_stride, tran_low_t *b) {
53   tran_low_t input[64];
54   tran_low_t buf[64];
55   for (int i = 0; i < 8; ++i) {
56     for (int j = 0; j < 8; ++j) {
57       input[i * 8 + j] = static_cast<tran_low_t>(a[i * a_stride + j]);
58     }
59   }
60   for (int i = 0; i < 8; ++i) HadamardLoop(input + i, buf + i * 8);
61   for (int i = 0; i < 8; ++i) HadamardLoop(buf + i, b + i * 8);
62 }
63 
ReferenceHadamard16x16(const int16_t * a,int a_stride,tran_low_t * b)64 void ReferenceHadamard16x16(const int16_t *a, int a_stride, tran_low_t *b) {
65   /* The source is a 16x16 block. The destination is rearranged to 8x32.
66    * Input is 9 bit. */
67   ReferenceHadamard8x8(a + 0 + 0 * a_stride, a_stride, b + 0);
68   ReferenceHadamard8x8(a + 8 + 0 * a_stride, a_stride, b + 64);
69   ReferenceHadamard8x8(a + 0 + 8 * a_stride, a_stride, b + 128);
70   ReferenceHadamard8x8(a + 8 + 8 * a_stride, a_stride, b + 192);
71 
72   /* Overlay the 8x8 blocks and combine. */
73   for (int i = 0; i < 64; ++i) {
74     /* 8x8 steps the range up to 15 bits. */
75     const tran_low_t a0 = b[0];
76     const tran_low_t a1 = b[64];
77     const tran_low_t a2 = b[128];
78     const tran_low_t a3 = b[192];
79 
80     /* Prevent the result from escaping int16_t. */
81     const tran_low_t b0 = (a0 + a1) >> 1;
82     const tran_low_t b1 = (a0 - a1) >> 1;
83     const tran_low_t b2 = (a2 + a3) >> 1;
84     const tran_low_t b3 = (a2 - a3) >> 1;
85 
86     /* Store a 16 bit value. */
87     b[0] = b0 + b2;
88     b[64] = b1 + b3;
89     b[128] = b0 - b2;
90     b[192] = b1 - b3;
91 
92     ++b;
93   }
94 }
95 
ReferenceHadamard32x32(const int16_t * a,int a_stride,tran_low_t * b)96 void ReferenceHadamard32x32(const int16_t *a, int a_stride, tran_low_t *b) {
97   ReferenceHadamard16x16(a + 0 + 0 * a_stride, a_stride, b + 0);
98   ReferenceHadamard16x16(a + 16 + 0 * a_stride, a_stride, b + 256);
99   ReferenceHadamard16x16(a + 0 + 16 * a_stride, a_stride, b + 512);
100   ReferenceHadamard16x16(a + 16 + 16 * a_stride, a_stride, b + 768);
101 
102   for (int i = 0; i < 256; ++i) {
103     const tran_low_t a0 = b[0];
104     const tran_low_t a1 = b[256];
105     const tran_low_t a2 = b[512];
106     const tran_low_t a3 = b[768];
107 
108     const tran_low_t b0 = (a0 + a1) >> 2;
109     const tran_low_t b1 = (a0 - a1) >> 2;
110     const tran_low_t b2 = (a2 + a3) >> 2;
111     const tran_low_t b3 = (a2 - a3) >> 2;
112 
113     b[0] = b0 + b2;
114     b[256] = b1 + b3;
115     b[512] = b0 - b2;
116     b[768] = b1 - b3;
117 
118     ++b;
119   }
120 }
121 
122 struct HadamardFuncWithSize {
HadamardFuncWithSize__anon00dd3a570111::HadamardFuncWithSize123   HadamardFuncWithSize(HadamardFunc f, int s) : func(f), block_size(s) {}
124   HadamardFunc func;
125   int block_size;
126 };
127 
operator <<(std::ostream & os,const HadamardFuncWithSize & hfs)128 std::ostream &operator<<(std::ostream &os, const HadamardFuncWithSize &hfs) {
129   return os << "block size: " << hfs.block_size;
130 }
131 
132 class HadamardTestBase : public ::testing::TestWithParam<HadamardFuncWithSize> {
133  public:
SetUp()134   virtual void SetUp() {
135     h_func_ = GetParam().func;
136     bwh_ = GetParam().block_size;
137     block_size_ = bwh_ * bwh_;
138     rnd_.Reset(ACMRandom::DeterministicSeed());
139   }
140 
141   virtual int16_t Rand() = 0;
142 
ReferenceHadamard(const int16_t * a,int a_stride,tran_low_t * b,int bwh)143   void ReferenceHadamard(const int16_t *a, int a_stride, tran_low_t *b,
144                          int bwh) {
145     if (bwh == 32)
146       ReferenceHadamard32x32(a, a_stride, b);
147     else if (bwh == 16)
148       ReferenceHadamard16x16(a, a_stride, b);
149     else
150       ReferenceHadamard8x8(a, a_stride, b);
151   }
152 
CompareReferenceRandom()153   void CompareReferenceRandom() {
154     const int kMaxBlockSize = 32 * 32;
155     DECLARE_ALIGNED(16, int16_t, a[kMaxBlockSize]);
156     DECLARE_ALIGNED(16, tran_low_t, b[kMaxBlockSize]);
157     memset(a, 0, sizeof(a));
158     memset(b, 0, sizeof(b));
159 
160     tran_low_t b_ref[kMaxBlockSize];
161     memset(b_ref, 0, sizeof(b_ref));
162 
163     for (int i = 0; i < block_size_; ++i) a[i] = Rand();
164 
165     ReferenceHadamard(a, bwh_, b_ref, bwh_);
166     API_REGISTER_STATE_CHECK(h_func_(a, bwh_, b));
167 
168     // The order of the output is not important. Sort before checking.
169     std::sort(b, b + block_size_);
170     std::sort(b_ref, b_ref + block_size_);
171     EXPECT_EQ(memcmp(b, b_ref, sizeof(b)), 0);
172   }
173 
VaryStride()174   void VaryStride() {
175     const int kMaxBlockSize = 32 * 32;
176     DECLARE_ALIGNED(16, int16_t, a[kMaxBlockSize * 8]);
177     DECLARE_ALIGNED(16, tran_low_t, b[kMaxBlockSize]);
178     memset(a, 0, sizeof(a));
179     for (int i = 0; i < block_size_ * 8; ++i) a[i] = Rand();
180 
181     tran_low_t b_ref[kMaxBlockSize];
182     for (int i = 8; i < 64; i += 8) {
183       memset(b, 0, sizeof(b));
184       memset(b_ref, 0, sizeof(b_ref));
185 
186       ReferenceHadamard(a, i, b_ref, bwh_);
187       API_REGISTER_STATE_CHECK(h_func_(a, i, b));
188 
189       // The order of the output is not important. Sort before checking.
190       std::sort(b, b + block_size_);
191       std::sort(b_ref, b_ref + block_size_);
192       EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
193     }
194   }
195 
SpeedTest(int times)196   void SpeedTest(int times) {
197     const int kMaxBlockSize = 32 * 32;
198     DECLARE_ALIGNED(16, int16_t, input[kMaxBlockSize]);
199     DECLARE_ALIGNED(16, tran_low_t, output[kMaxBlockSize]);
200     memset(input, 1, sizeof(input));
201     memset(output, 0, sizeof(output));
202 
203     aom_usec_timer timer;
204     aom_usec_timer_start(&timer);
205     for (int i = 0; i < times; ++i) {
206       h_func_(input, bwh_, output);
207     }
208     aom_usec_timer_mark(&timer);
209 
210     const int elapsed_time = static_cast<int>(aom_usec_timer_elapsed(&timer));
211     printf("Hadamard%dx%d[%12d runs]: %d us\n", bwh_, bwh_, times,
212            elapsed_time);
213   }
214 
215   ACMRandom rnd_;
216 
217  private:
218   int bwh_;
219   int block_size_;
220   HadamardFunc h_func_;
221 };
222 
223 class HadamardLowbdTest : public HadamardTestBase {
224  public:
Rand()225   virtual int16_t Rand() { return rnd_.Rand9Signed(); }
226 };
227 
TEST_P(HadamardLowbdTest,CompareReferenceRandom)228 TEST_P(HadamardLowbdTest, CompareReferenceRandom) { CompareReferenceRandom(); }
229 
TEST_P(HadamardLowbdTest,VaryStride)230 TEST_P(HadamardLowbdTest, VaryStride) { VaryStride(); }
231 
232 INSTANTIATE_TEST_SUITE_P(
233     C, HadamardLowbdTest,
234     ::testing::Values(HadamardFuncWithSize(&aom_hadamard_8x8_c, 8),
235                       HadamardFuncWithSize(&aom_hadamard_16x16_c, 16),
236                       HadamardFuncWithSize(&aom_hadamard_32x32_c, 32)));
237 
238 #if HAVE_SSE2
239 INSTANTIATE_TEST_SUITE_P(
240     SSE2, HadamardLowbdTest,
241     ::testing::Values(HadamardFuncWithSize(&aom_hadamard_8x8_sse2, 8),
242                       HadamardFuncWithSize(&aom_hadamard_16x16_sse2, 16),
243                       HadamardFuncWithSize(&aom_hadamard_32x32_sse2, 32)));
244 #endif  // HAVE_SSE2
245 
246 #if HAVE_AVX2
247 INSTANTIATE_TEST_SUITE_P(
248     AVX2, HadamardLowbdTest,
249     ::testing::Values(HadamardFuncWithSize(&aom_hadamard_16x16_avx2, 16),
250                       HadamardFuncWithSize(&aom_hadamard_32x32_avx2, 32)));
251 #endif  // HAVE_AVX2
252 
253 #if HAVE_NEON
254 INSTANTIATE_TEST_SUITE_P(
255     NEON, HadamardLowbdTest,
256     ::testing::Values(HadamardFuncWithSize(&aom_hadamard_8x8_neon, 8),
257                       HadamardFuncWithSize(&aom_hadamard_16x16_neon, 16)));
258 #endif  // HAVE_NEON
259 
260 }  // namespace
261