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 <algorithm>
12
13 #include "third_party/googletest/src/include/gtest/gtest.h"
14
15 #include "./vpx_dsp_rtcd.h"
16 #include "vpx_ports/vpx_timer.h"
17
18 #include "test/acm_random.h"
19 #include "test/register_state_check.h"
20
21 namespace {
22
23 using ::libvpx_test::ACMRandom;
24
25 typedef void (*HadamardFunc)(const int16_t *a, ptrdiff_t a_stride,
26 tran_low_t *b);
27
hadamard_loop(const tran_low_t * a,tran_low_t * out)28 void hadamard_loop(const tran_low_t *a, tran_low_t *out) {
29 tran_low_t b[8];
30 for (int i = 0; i < 8; i += 2) {
31 b[i + 0] = a[i * 8] + a[(i + 1) * 8];
32 b[i + 1] = a[i * 8] - a[(i + 1) * 8];
33 }
34 tran_low_t c[8];
35 for (int i = 0; i < 8; i += 4) {
36 c[i + 0] = b[i + 0] + b[i + 2];
37 c[i + 1] = b[i + 1] + b[i + 3];
38 c[i + 2] = b[i + 0] - b[i + 2];
39 c[i + 3] = b[i + 1] - b[i + 3];
40 }
41 out[0] = c[0] + c[4];
42 out[7] = c[1] + c[5];
43 out[3] = c[2] + c[6];
44 out[4] = c[3] + c[7];
45 out[2] = c[0] - c[4];
46 out[6] = c[1] - c[5];
47 out[1] = c[2] - c[6];
48 out[5] = c[3] - c[7];
49 }
50
reference_hadamard8x8(const int16_t * a,int a_stride,tran_low_t * b)51 void reference_hadamard8x8(const int16_t *a, int a_stride, tran_low_t *b) {
52 tran_low_t input[64];
53 tran_low_t buf[64];
54 for (int i = 0; i < 8; ++i) {
55 for (int j = 0; j < 8; ++j) {
56 input[i * 8 + j] = static_cast<tran_low_t>(a[i * a_stride + j]);
57 }
58 }
59 for (int i = 0; i < 8; ++i) hadamard_loop(input + i, buf + i * 8);
60 for (int i = 0; i < 8; ++i) hadamard_loop(buf + i, b + i * 8);
61 }
62
reference_hadamard16x16(const int16_t * a,int a_stride,tran_low_t * b)63 void reference_hadamard16x16(const int16_t *a, int a_stride, tran_low_t *b) {
64 /* The source is a 16x16 block. The destination is rearranged to 8x32.
65 * Input is 9 bit. */
66 reference_hadamard8x8(a + 0 + 0 * a_stride, a_stride, b + 0);
67 reference_hadamard8x8(a + 8 + 0 * a_stride, a_stride, b + 64);
68 reference_hadamard8x8(a + 0 + 8 * a_stride, a_stride, b + 128);
69 reference_hadamard8x8(a + 8 + 8 * a_stride, a_stride, b + 192);
70
71 /* Overlay the 8x8 blocks and combine. */
72 for (int i = 0; i < 64; ++i) {
73 /* 8x8 steps the range up to 15 bits. */
74 const tran_low_t a0 = b[0];
75 const tran_low_t a1 = b[64];
76 const tran_low_t a2 = b[128];
77 const tran_low_t a3 = b[192];
78
79 /* Prevent the result from escaping int16_t. */
80 const tran_low_t b0 = (a0 + a1) >> 1;
81 const tran_low_t b1 = (a0 - a1) >> 1;
82 const tran_low_t b2 = (a2 + a3) >> 1;
83 const tran_low_t b3 = (a2 - a3) >> 1;
84
85 /* Store a 16 bit value. */
86 b[0] = b0 + b2;
87 b[64] = b1 + b3;
88 b[128] = b0 - b2;
89 b[192] = b1 - b3;
90
91 ++b;
92 }
93 }
94
reference_hadamard32x32(const int16_t * a,int a_stride,tran_low_t * b)95 void reference_hadamard32x32(const int16_t *a, int a_stride, tran_low_t *b) {
96 reference_hadamard16x16(a + 0 + 0 * a_stride, a_stride, b + 0);
97 reference_hadamard16x16(a + 16 + 0 * a_stride, a_stride, b + 256);
98 reference_hadamard16x16(a + 0 + 16 * a_stride, a_stride, b + 512);
99 reference_hadamard16x16(a + 16 + 16 * a_stride, a_stride, b + 768);
100
101 for (int i = 0; i < 256; ++i) {
102 const tran_low_t a0 = b[0];
103 const tran_low_t a1 = b[256];
104 const tran_low_t a2 = b[512];
105 const tran_low_t a3 = b[768];
106
107 const tran_low_t b0 = (a0 + a1) >> 2;
108 const tran_low_t b1 = (a0 - a1) >> 2;
109 const tran_low_t b2 = (a2 + a3) >> 2;
110 const tran_low_t b3 = (a2 - a3) >> 2;
111
112 b[0] = b0 + b2;
113 b[256] = b1 + b3;
114 b[512] = b0 - b2;
115 b[768] = b1 - b3;
116
117 ++b;
118 }
119 }
120
121 struct HadamardFuncWithSize {
HadamardFuncWithSize__anon932007980111::HadamardFuncWithSize122 HadamardFuncWithSize(HadamardFunc f, int s) : func(f), block_size(s) {}
123 HadamardFunc func;
124 int block_size;
125 };
126
operator <<(std::ostream & os,const HadamardFuncWithSize & hfs)127 std::ostream &operator<<(std::ostream &os, const HadamardFuncWithSize &hfs) {
128 return os << "block size: " << hfs.block_size;
129 }
130
131 class HadamardTestBase : public ::testing::TestWithParam<HadamardFuncWithSize> {
132 public:
SetUp()133 virtual void SetUp() {
134 h_func_ = GetParam().func;
135 bwh_ = GetParam().block_size;
136 block_size_ = bwh_ * bwh_;
137 rnd_.Reset(ACMRandom::DeterministicSeed());
138 }
139
140 virtual int16_t Rand() = 0;
141
ReferenceHadamard(const int16_t * a,int a_stride,tran_low_t * b,int bwh)142 void ReferenceHadamard(const int16_t *a, int a_stride, tran_low_t *b,
143 int bwh) {
144 if (bwh == 32)
145 reference_hadamard32x32(a, a_stride, b);
146 else if (bwh == 16)
147 reference_hadamard16x16(a, a_stride, b);
148 else
149 reference_hadamard8x8(a, a_stride, b);
150 }
151
CompareReferenceRandom()152 void CompareReferenceRandom() {
153 const int kMaxBlockSize = 32 * 32;
154 DECLARE_ALIGNED(16, int16_t, a[kMaxBlockSize]);
155 DECLARE_ALIGNED(16, tran_low_t, b[kMaxBlockSize]);
156 memset(a, 0, sizeof(a));
157 memset(b, 0, sizeof(b));
158
159 tran_low_t b_ref[kMaxBlockSize];
160 memset(b_ref, 0, sizeof(b_ref));
161
162 for (int i = 0; i < block_size_; ++i) a[i] = Rand();
163
164 ReferenceHadamard(a, bwh_, b_ref, bwh_);
165 ASM_REGISTER_STATE_CHECK(h_func_(a, bwh_, b));
166
167 // The order of the output is not important. Sort before checking.
168 std::sort(b, b + block_size_);
169 std::sort(b_ref, b_ref + block_size_);
170 EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
171 }
172
VaryStride()173 void VaryStride() {
174 const int kMaxBlockSize = 32 * 32;
175 DECLARE_ALIGNED(16, int16_t, a[kMaxBlockSize * 8]);
176 DECLARE_ALIGNED(16, tran_low_t, b[kMaxBlockSize]);
177 memset(a, 0, sizeof(a));
178 for (int i = 0; i < block_size_ * 8; ++i) a[i] = Rand();
179
180 tran_low_t b_ref[kMaxBlockSize];
181 for (int i = 8; i < 64; i += 8) {
182 memset(b, 0, sizeof(b));
183 memset(b_ref, 0, sizeof(b_ref));
184
185 ReferenceHadamard(a, i, b_ref, bwh_);
186 ASM_REGISTER_STATE_CHECK(h_func_(a, i, b));
187
188 // The order of the output is not important. Sort before checking.
189 std::sort(b, b + block_size_);
190 std::sort(b_ref, b_ref + block_size_);
191 EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
192 }
193 }
194
SpeedTest(int times)195 void SpeedTest(int times) {
196 const int kMaxBlockSize = 32 * 32;
197 DECLARE_ALIGNED(16, int16_t, input[kMaxBlockSize]);
198 DECLARE_ALIGNED(16, tran_low_t, output[kMaxBlockSize]);
199 memset(input, 1, sizeof(input));
200 memset(output, 0, sizeof(output));
201
202 vpx_usec_timer timer;
203 vpx_usec_timer_start(&timer);
204 for (int i = 0; i < times; ++i) {
205 h_func_(input, bwh_, output);
206 }
207 vpx_usec_timer_mark(&timer);
208
209 const int elapsed_time = static_cast<int>(vpx_usec_timer_elapsed(&timer));
210 printf("Hadamard%dx%d[%12d runs]: %d us\n", bwh_, bwh_, times,
211 elapsed_time);
212 }
213
214 protected:
215 int bwh_;
216 int block_size_;
217 HadamardFunc h_func_;
218 ACMRandom rnd_;
219 };
220
221 class HadamardLowbdTest : public HadamardTestBase {
222 protected:
Rand()223 virtual int16_t Rand() { return rnd_.Rand9Signed(); }
224 };
225
TEST_P(HadamardLowbdTest,CompareReferenceRandom)226 TEST_P(HadamardLowbdTest, CompareReferenceRandom) { CompareReferenceRandom(); }
227
TEST_P(HadamardLowbdTest,VaryStride)228 TEST_P(HadamardLowbdTest, VaryStride) { VaryStride(); }
229
TEST_P(HadamardLowbdTest,DISABLED_Speed)230 TEST_P(HadamardLowbdTest, DISABLED_Speed) {
231 SpeedTest(10);
232 SpeedTest(10000);
233 SpeedTest(10000000);
234 }
235
236 INSTANTIATE_TEST_CASE_P(
237 C, HadamardLowbdTest,
238 ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_c, 8),
239 HadamardFuncWithSize(&vpx_hadamard_16x16_c, 16),
240 HadamardFuncWithSize(&vpx_hadamard_32x32_c, 32)));
241
242 #if HAVE_SSE2
243 INSTANTIATE_TEST_CASE_P(
244 SSE2, HadamardLowbdTest,
245 ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_sse2, 8),
246 HadamardFuncWithSize(&vpx_hadamard_16x16_sse2, 16),
247 HadamardFuncWithSize(&vpx_hadamard_32x32_sse2, 32)));
248 #endif // HAVE_SSE2
249
250 #if HAVE_AVX2
251 INSTANTIATE_TEST_CASE_P(
252 AVX2, HadamardLowbdTest,
253 ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_16x16_avx2, 16),
254 HadamardFuncWithSize(&vpx_hadamard_32x32_avx2, 32)));
255 #endif // HAVE_AVX2
256
257 #if HAVE_SSSE3 && VPX_ARCH_X86_64
258 INSTANTIATE_TEST_CASE_P(
259 SSSE3, HadamardLowbdTest,
260 ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_ssse3, 8)));
261 #endif // HAVE_SSSE3 && VPX_ARCH_X86_64
262
263 #if HAVE_NEON
264 INSTANTIATE_TEST_CASE_P(
265 NEON, HadamardLowbdTest,
266 ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_neon, 8),
267 HadamardFuncWithSize(&vpx_hadamard_16x16_neon, 16)));
268 #endif // HAVE_NEON
269
270 // TODO(jingning): Remove highbitdepth flag when the SIMD functions are
271 // in place and turn on the unit test.
272 #if !CONFIG_VP9_HIGHBITDEPTH
273 #if HAVE_MSA
274 INSTANTIATE_TEST_CASE_P(
275 MSA, HadamardLowbdTest,
276 ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_msa, 8),
277 HadamardFuncWithSize(&vpx_hadamard_16x16_msa, 16)));
278 #endif // HAVE_MSA
279 #endif // !CONFIG_VP9_HIGHBITDEPTH
280
281 #if HAVE_VSX
282 INSTANTIATE_TEST_CASE_P(
283 VSX, HadamardLowbdTest,
284 ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_vsx, 8),
285 HadamardFuncWithSize(&vpx_hadamard_16x16_vsx, 16)));
286 #endif // HAVE_VSX
287
288 #if CONFIG_VP9_HIGHBITDEPTH
289 class HadamardHighbdTest : public HadamardTestBase {
290 protected:
Rand()291 virtual int16_t Rand() { return rnd_.Rand13Signed(); }
292 };
293
TEST_P(HadamardHighbdTest,CompareReferenceRandom)294 TEST_P(HadamardHighbdTest, CompareReferenceRandom) { CompareReferenceRandom(); }
295
TEST_P(HadamardHighbdTest,VaryStride)296 TEST_P(HadamardHighbdTest, VaryStride) { VaryStride(); }
297
TEST_P(HadamardHighbdTest,DISABLED_Speed)298 TEST_P(HadamardHighbdTest, DISABLED_Speed) {
299 SpeedTest(10);
300 SpeedTest(10000);
301 SpeedTest(10000000);
302 }
303
304 INSTANTIATE_TEST_CASE_P(
305 C, HadamardHighbdTest,
306 ::testing::Values(HadamardFuncWithSize(&vpx_highbd_hadamard_8x8_c, 8),
307 HadamardFuncWithSize(&vpx_highbd_hadamard_16x16_c, 16),
308 HadamardFuncWithSize(&vpx_highbd_hadamard_32x32_c, 32)));
309
310 #if HAVE_AVX2
311 INSTANTIATE_TEST_CASE_P(
312 AVX2, HadamardHighbdTest,
313 ::testing::Values(HadamardFuncWithSize(&vpx_highbd_hadamard_8x8_avx2, 8),
314 HadamardFuncWithSize(&vpx_highbd_hadamard_16x16_avx2, 16),
315 HadamardFuncWithSize(&vpx_highbd_hadamard_32x32_avx2,
316 32)));
317 #endif // HAVE_AVX2
318
319 #endif // CONFIG_VP9_HIGHBITDEPTH
320 } // namespace
321