blob: 792e0abc4144ff6374a7362a2620b2df987d343b [file] [log] [blame]
Johann8c02a362016-04-15 11:35:56 -07001/*
Yaowu Xubde4ac82016-11-28 15:26:06 -08002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Johann8c02a362016-04-15 11:35:56 -07003 *
Yaowu Xubde4ac82016-11-28 15:26:06 -08004 * 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.
Johann8c02a362016-04-15 11:35:56 -070010 */
11
12#include <algorithm>
13
14#include "third_party/googletest/src/include/gtest/gtest.h"
15
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "./aom_dsp_rtcd.h"
Johann8c02a362016-04-15 11:35:56 -070017
18#include "test/acm_random.h"
19#include "test/register_state_check.h"
20
21namespace {
22
Yaowu Xuc27fc142016-08-22 16:08:15 -070023using ::libaom_test::ACMRandom;
Johann8c02a362016-04-15 11:35:56 -070024
Johann32ff4902016-05-12 13:02:26 -070025typedef void (*HadamardFunc)(const int16_t *a, int a_stride, int16_t *b);
Johann8c02a362016-04-15 11:35:56 -070026
27void hadamard_loop(const int16_t *a, int a_stride, int16_t *out) {
28 int16_t b[8];
29 for (int i = 0; i < 8; i += 2) {
30 b[i + 0] = a[i * a_stride] + a[(i + 1) * a_stride];
31 b[i + 1] = a[i * a_stride] - a[(i + 1) * a_stride];
32 }
33 int16_t c[8];
34 for (int i = 0; i < 8; i += 4) {
35 c[i + 0] = b[i + 0] + b[i + 2];
36 c[i + 1] = b[i + 1] + b[i + 3];
37 c[i + 2] = b[i + 0] - b[i + 2];
38 c[i + 3] = b[i + 1] - b[i + 3];
39 }
40 out[0] = c[0] + c[4];
41 out[7] = c[1] + c[5];
42 out[3] = c[2] + c[6];
43 out[4] = c[3] + c[7];
44 out[2] = c[0] - c[4];
45 out[6] = c[1] - c[5];
46 out[1] = c[2] - c[6];
47 out[5] = c[3] - c[7];
48}
49
Johann32ff4902016-05-12 13:02:26 -070050void reference_hadamard8x8(const int16_t *a, int a_stride, int16_t *b) {
Johann8c02a362016-04-15 11:35:56 -070051 int16_t buf[64];
Johann32ff4902016-05-12 13:02:26 -070052 for (int i = 0; i < 8; ++i) {
Johann8c02a362016-04-15 11:35:56 -070053 hadamard_loop(a + i, a_stride, buf + i * 8);
54 }
55
Johann32ff4902016-05-12 13:02:26 -070056 for (int i = 0; i < 8; ++i) {
Johann8c02a362016-04-15 11:35:56 -070057 hadamard_loop(buf + i, 8, b + i * 8);
58 }
59}
60
Johann32ff4902016-05-12 13:02:26 -070061void reference_hadamard16x16(const int16_t *a, int a_stride, int16_t *b) {
62 /* The source is a 16x16 block. The destination is rearranged to 8x32.
63 * Input is 9 bit. */
64 reference_hadamard8x8(a + 0 + 0 * a_stride, a_stride, b + 0);
65 reference_hadamard8x8(a + 8 + 0 * a_stride, a_stride, b + 64);
66 reference_hadamard8x8(a + 0 + 8 * a_stride, a_stride, b + 128);
67 reference_hadamard8x8(a + 8 + 8 * a_stride, a_stride, b + 192);
68
69 /* Overlay the 8x8 blocks and combine. */
70 for (int i = 0; i < 64; ++i) {
71 /* 8x8 steps the range up to 15 bits. */
72 const int16_t a0 = b[0];
73 const int16_t a1 = b[64];
74 const int16_t a2 = b[128];
75 const int16_t a3 = b[192];
76
77 /* Prevent the result from escaping int16_t. */
78 const int16_t b0 = (a0 + a1) >> 1;
79 const int16_t b1 = (a0 - a1) >> 1;
80 const int16_t b2 = (a2 + a3) >> 1;
81 const int16_t b3 = (a2 - a3) >> 1;
82
83 /* Store a 16 bit value. */
clang-format3a826f12016-08-11 17:46:05 -070084 b[0] = b0 + b2;
85 b[64] = b1 + b3;
Johann32ff4902016-05-12 13:02:26 -070086 b[128] = b0 - b2;
87 b[192] = b1 - b3;
88
89 ++b;
90 }
91}
92
93class HadamardTestBase : public ::testing::TestWithParam<HadamardFunc> {
94 public:
95 virtual void SetUp() {
96 h_func_ = GetParam();
97 rnd_.Reset(ACMRandom::DeterministicSeed());
98 }
99
100 protected:
101 HadamardFunc h_func_;
102 ACMRandom rnd_;
103};
104
105class Hadamard8x8Test : public HadamardTestBase {};
106
107TEST_P(Hadamard8x8Test, CompareReferenceRandom) {
James Zern9222d462016-04-22 00:06:49 -0700108 DECLARE_ALIGNED(16, int16_t, a[64]);
109 DECLARE_ALIGNED(16, int16_t, b[64]);
110 int16_t b_ref[64];
Johann32ff4902016-05-12 13:02:26 -0700111 for (int i = 0; i < 64; ++i) {
Johann8c02a362016-04-15 11:35:56 -0700112 a[i] = rnd_.Rand9Signed();
113 }
114 memset(b, 0, sizeof(b));
115 memset(b_ref, 0, sizeof(b_ref));
116
Johann32ff4902016-05-12 13:02:26 -0700117 reference_hadamard8x8(a, 8, b_ref);
Johann8c02a362016-04-15 11:35:56 -0700118 ASM_REGISTER_STATE_CHECK(h_func_(a, 8, b));
119
120 // The order of the output is not important. Sort before checking.
121 std::sort(b, b + 64);
122 std::sort(b_ref, b_ref + 64);
123 EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
124}
125
Johann32ff4902016-05-12 13:02:26 -0700126TEST_P(Hadamard8x8Test, VaryStride) {
James Zern9222d462016-04-22 00:06:49 -0700127 DECLARE_ALIGNED(16, int16_t, a[64 * 8]);
128 DECLARE_ALIGNED(16, int16_t, b[64]);
129 int16_t b_ref[64];
Johann32ff4902016-05-12 13:02:26 -0700130 for (int i = 0; i < 64 * 8; ++i) {
Johann8c02a362016-04-15 11:35:56 -0700131 a[i] = rnd_.Rand9Signed();
132 }
133
134 for (int i = 8; i < 64; i += 8) {
135 memset(b, 0, sizeof(b));
136 memset(b_ref, 0, sizeof(b_ref));
137
Johann32ff4902016-05-12 13:02:26 -0700138 reference_hadamard8x8(a, i, b_ref);
Johann8c02a362016-04-15 11:35:56 -0700139 ASM_REGISTER_STATE_CHECK(h_func_(a, i, b));
140
141 // The order of the output is not important. Sort before checking.
142 std::sort(b, b + 64);
143 std::sort(b_ref, b_ref + 64);
144 EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
145 }
146}
147
Johann32ff4902016-05-12 13:02:26 -0700148INSTANTIATE_TEST_CASE_P(C, Hadamard8x8Test,
Yaowu Xuf883b422016-08-30 14:01:10 -0700149 ::testing::Values(&aom_hadamard_8x8_c));
Johann8c02a362016-04-15 11:35:56 -0700150
151#if HAVE_SSE2
Johann32ff4902016-05-12 13:02:26 -0700152INSTANTIATE_TEST_CASE_P(SSE2, Hadamard8x8Test,
Yaowu Xuf883b422016-08-30 14:01:10 -0700153 ::testing::Values(&aom_hadamard_8x8_sse2));
Johann8c02a362016-04-15 11:35:56 -0700154#endif // HAVE_SSE2
155
Johann2967bf32016-06-22 16:08:10 -0700156#if HAVE_SSSE3 && ARCH_X86_64
Johann32ff4902016-05-12 13:02:26 -0700157INSTANTIATE_TEST_CASE_P(SSSE3, Hadamard8x8Test,
Yaowu Xuf883b422016-08-30 14:01:10 -0700158 ::testing::Values(&aom_hadamard_8x8_ssse3));
Johann2967bf32016-06-22 16:08:10 -0700159#endif // HAVE_SSSE3 && ARCH_X86_64
Johann9b54e812016-05-11 13:26:19 -0700160
161#if HAVE_NEON
Johann32ff4902016-05-12 13:02:26 -0700162INSTANTIATE_TEST_CASE_P(NEON, Hadamard8x8Test,
Yaowu Xuf883b422016-08-30 14:01:10 -0700163 ::testing::Values(&aom_hadamard_8x8_neon));
Johann9b54e812016-05-11 13:26:19 -0700164#endif // HAVE_NEON
Johann32ff4902016-05-12 13:02:26 -0700165
166class Hadamard16x16Test : public HadamardTestBase {};
167
168TEST_P(Hadamard16x16Test, CompareReferenceRandom) {
169 DECLARE_ALIGNED(16, int16_t, a[16 * 16]);
170 DECLARE_ALIGNED(16, int16_t, b[16 * 16]);
171 int16_t b_ref[16 * 16];
172 for (int i = 0; i < 16 * 16; ++i) {
173 a[i] = rnd_.Rand9Signed();
174 }
175 memset(b, 0, sizeof(b));
176 memset(b_ref, 0, sizeof(b_ref));
177
178 reference_hadamard16x16(a, 16, b_ref);
179 ASM_REGISTER_STATE_CHECK(h_func_(a, 16, b));
180
181 // The order of the output is not important. Sort before checking.
182 std::sort(b, b + 16 * 16);
183 std::sort(b_ref, b_ref + 16 * 16);
184 EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
185}
186
187TEST_P(Hadamard16x16Test, VaryStride) {
188 DECLARE_ALIGNED(16, int16_t, a[16 * 16 * 8]);
189 DECLARE_ALIGNED(16, int16_t, b[16 * 16]);
190 int16_t b_ref[16 * 16];
191 for (int i = 0; i < 16 * 16 * 8; ++i) {
192 a[i] = rnd_.Rand9Signed();
193 }
194
195 for (int i = 8; i < 64; i += 8) {
196 memset(b, 0, sizeof(b));
197 memset(b_ref, 0, sizeof(b_ref));
198
199 reference_hadamard16x16(a, i, b_ref);
200 ASM_REGISTER_STATE_CHECK(h_func_(a, i, b));
201
202 // The order of the output is not important. Sort before checking.
203 std::sort(b, b + 16 * 16);
204 std::sort(b_ref, b_ref + 16 * 16);
205 EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
206 }
207}
208
209INSTANTIATE_TEST_CASE_P(C, Hadamard16x16Test,
Yaowu Xuf883b422016-08-30 14:01:10 -0700210 ::testing::Values(&aom_hadamard_16x16_c));
Johann32ff4902016-05-12 13:02:26 -0700211
212#if HAVE_SSE2
213INSTANTIATE_TEST_CASE_P(SSE2, Hadamard16x16Test,
Yaowu Xuf883b422016-08-30 14:01:10 -0700214 ::testing::Values(&aom_hadamard_16x16_sse2));
Johann32ff4902016-05-12 13:02:26 -0700215#endif // HAVE_SSE2
Johannc516dd62016-06-10 15:49:44 -0700216
217#if HAVE_NEON
218INSTANTIATE_TEST_CASE_P(NEON, Hadamard16x16Test,
Yaowu Xuf883b422016-08-30 14:01:10 -0700219 ::testing::Values(&aom_hadamard_16x16_neon));
Johannc516dd62016-06-10 15:49:44 -0700220#endif // HAVE_NEON
Johann8c02a362016-04-15 11:35:56 -0700221} // namespace