Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 4 | * 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. |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <algorithm> |
| 13 | |
| 14 | #include "third_party/googletest/src/include/gtest/gtest.h" |
| 15 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 16 | #include "./aom_dsp_rtcd.h" |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 17 | |
| 18 | #include "test/acm_random.h" |
| 19 | #include "test/register_state_check.h" |
| 20 | |
| 21 | namespace { |
| 22 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 23 | using ::libaom_test::ACMRandom; |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 24 | |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 25 | typedef void (*HadamardFunc)(const int16_t *a, int a_stride, int16_t *b); |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 26 | |
| 27 | void 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 | |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 50 | void reference_hadamard8x8(const int16_t *a, int a_stride, int16_t *b) { |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 51 | int16_t buf[64]; |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 52 | for (int i = 0; i < 8; ++i) { |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 53 | hadamard_loop(a + i, a_stride, buf + i * 8); |
| 54 | } |
| 55 | |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 56 | for (int i = 0; i < 8; ++i) { |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 57 | hadamard_loop(buf + i, 8, b + i * 8); |
| 58 | } |
| 59 | } |
| 60 | |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 61 | void 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-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 84 | b[0] = b0 + b2; |
| 85 | b[64] = b1 + b3; |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 86 | b[128] = b0 - b2; |
| 87 | b[192] = b1 - b3; |
| 88 | |
| 89 | ++b; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | class 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 | |
| 105 | class Hadamard8x8Test : public HadamardTestBase {}; |
| 106 | |
| 107 | TEST_P(Hadamard8x8Test, CompareReferenceRandom) { |
James Zern | 9222d46 | 2016-04-22 00:06:49 -0700 | [diff] [blame] | 108 | DECLARE_ALIGNED(16, int16_t, a[64]); |
| 109 | DECLARE_ALIGNED(16, int16_t, b[64]); |
| 110 | int16_t b_ref[64]; |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 111 | for (int i = 0; i < 64; ++i) { |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 112 | a[i] = rnd_.Rand9Signed(); |
| 113 | } |
| 114 | memset(b, 0, sizeof(b)); |
| 115 | memset(b_ref, 0, sizeof(b_ref)); |
| 116 | |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 117 | reference_hadamard8x8(a, 8, b_ref); |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 118 | 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 | |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 126 | TEST_P(Hadamard8x8Test, VaryStride) { |
James Zern | 9222d46 | 2016-04-22 00:06:49 -0700 | [diff] [blame] | 127 | DECLARE_ALIGNED(16, int16_t, a[64 * 8]); |
| 128 | DECLARE_ALIGNED(16, int16_t, b[64]); |
| 129 | int16_t b_ref[64]; |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 130 | for (int i = 0; i < 64 * 8; ++i) { |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 131 | 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 | |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 138 | reference_hadamard8x8(a, i, b_ref); |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 139 | 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 | |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 148 | INSTANTIATE_TEST_CASE_P(C, Hadamard8x8Test, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 149 | ::testing::Values(&aom_hadamard_8x8_c)); |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 150 | |
| 151 | #if HAVE_SSE2 |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 152 | INSTANTIATE_TEST_CASE_P(SSE2, Hadamard8x8Test, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 153 | ::testing::Values(&aom_hadamard_8x8_sse2)); |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 154 | #endif // HAVE_SSE2 |
| 155 | |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 156 | #if HAVE_SSSE3 && ARCH_X86_64 |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 157 | INSTANTIATE_TEST_CASE_P(SSSE3, Hadamard8x8Test, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 158 | ::testing::Values(&aom_hadamard_8x8_ssse3)); |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 159 | #endif // HAVE_SSSE3 && ARCH_X86_64 |
Johann | 9b54e81 | 2016-05-11 13:26:19 -0700 | [diff] [blame] | 160 | |
| 161 | #if HAVE_NEON |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 162 | INSTANTIATE_TEST_CASE_P(NEON, Hadamard8x8Test, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 163 | ::testing::Values(&aom_hadamard_8x8_neon)); |
Johann | 9b54e81 | 2016-05-11 13:26:19 -0700 | [diff] [blame] | 164 | #endif // HAVE_NEON |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 165 | |
| 166 | class Hadamard16x16Test : public HadamardTestBase {}; |
| 167 | |
| 168 | TEST_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 | |
| 187 | TEST_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 | |
| 209 | INSTANTIATE_TEST_CASE_P(C, Hadamard16x16Test, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 210 | ::testing::Values(&aom_hadamard_16x16_c)); |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 211 | |
| 212 | #if HAVE_SSE2 |
| 213 | INSTANTIATE_TEST_CASE_P(SSE2, Hadamard16x16Test, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 214 | ::testing::Values(&aom_hadamard_16x16_sse2)); |
Johann | 32ff490 | 2016-05-12 13:02:26 -0700 | [diff] [blame] | 215 | #endif // HAVE_SSE2 |
Johann | c516dd6 | 2016-06-10 15:49:44 -0700 | [diff] [blame] | 216 | |
| 217 | #if HAVE_NEON |
| 218 | INSTANTIATE_TEST_CASE_P(NEON, Hadamard16x16Test, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 219 | ::testing::Values(&aom_hadamard_16x16_neon)); |
Johann | c516dd6 | 2016-06-10 15:49:44 -0700 | [diff] [blame] | 220 | #endif // HAVE_NEON |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 221 | } // namespace |