hadamard_test.cc 6.32 KB
Newer Older
Johann's avatar
Johann committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Johann's avatar
Johann committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Johann's avatar
Johann committed
10 11 12 13
 */

#include <algorithm>

14
#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
Johann's avatar
Johann committed
15

16
#include "./aom_dsp_rtcd.h"
Johann's avatar
Johann committed
17 18 19 20 21 22

#include "test/acm_random.h"
#include "test/register_state_check.h"

namespace {

23
using ::libaom_test::ACMRandom;
Johann's avatar
Johann committed
24

Johann's avatar
Johann committed
25
typedef void (*HadamardFunc)(const int16_t *a, int a_stride, int16_t *b);
Johann's avatar
Johann committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

void hadamard_loop(const int16_t *a, int a_stride, int16_t *out) {
  int16_t b[8];
  for (int i = 0; i < 8; i += 2) {
    b[i + 0] = a[i * a_stride] + a[(i + 1) * a_stride];
    b[i + 1] = a[i * a_stride] - a[(i + 1) * a_stride];
  }
  int16_t c[8];
  for (int i = 0; i < 8; i += 4) {
    c[i + 0] = b[i + 0] + b[i + 2];
    c[i + 1] = b[i + 1] + b[i + 3];
    c[i + 2] = b[i + 0] - b[i + 2];
    c[i + 3] = b[i + 1] - b[i + 3];
  }
  out[0] = c[0] + c[4];
  out[7] = c[1] + c[5];
  out[3] = c[2] + c[6];
  out[4] = c[3] + c[7];
  out[2] = c[0] - c[4];
  out[6] = c[1] - c[5];
  out[1] = c[2] - c[6];
  out[5] = c[3] - c[7];
}

Johann's avatar
Johann committed
50
void reference_hadamard8x8(const int16_t *a, int a_stride, int16_t *b) {
Johann's avatar
Johann committed
51
  int16_t buf[64];
Johann's avatar
Johann committed
52
  for (int i = 0; i < 8; ++i) {
Johann's avatar
Johann committed
53 54 55
    hadamard_loop(a + i, a_stride, buf + i * 8);
  }

Johann's avatar
Johann committed
56
  for (int i = 0; i < 8; ++i) {
Johann's avatar
Johann committed
57 58 59 60
    hadamard_loop(buf + i, 8, b + i * 8);
  }
}

Johann's avatar
Johann committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
void reference_hadamard16x16(const int16_t *a, int a_stride, int16_t *b) {
  /* The source is a 16x16 block. The destination is rearranged to 8x32.
   * Input is 9 bit. */
  reference_hadamard8x8(a + 0 + 0 * a_stride, a_stride, b + 0);
  reference_hadamard8x8(a + 8 + 0 * a_stride, a_stride, b + 64);
  reference_hadamard8x8(a + 0 + 8 * a_stride, a_stride, b + 128);
  reference_hadamard8x8(a + 8 + 8 * a_stride, a_stride, b + 192);

  /* Overlay the 8x8 blocks and combine. */
  for (int i = 0; i < 64; ++i) {
    /* 8x8 steps the range up to 15 bits. */
    const int16_t a0 = b[0];
    const int16_t a1 = b[64];
    const int16_t a2 = b[128];
    const int16_t a3 = b[192];

    /* Prevent the result from escaping int16_t. */
    const int16_t b0 = (a0 + a1) >> 1;
    const int16_t b1 = (a0 - a1) >> 1;
    const int16_t b2 = (a2 + a3) >> 1;
    const int16_t b3 = (a2 - a3) >> 1;

    /* Store a 16 bit value. */
clang-format's avatar
clang-format committed
84 85
    b[0] = b0 + b2;
    b[64] = b1 + b3;
Johann's avatar
Johann committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    b[128] = b0 - b2;
    b[192] = b1 - b3;

    ++b;
  }
}

class HadamardTestBase : public ::testing::TestWithParam<HadamardFunc> {
 public:
  virtual void SetUp() {
    h_func_ = GetParam();
    rnd_.Reset(ACMRandom::DeterministicSeed());
  }

 protected:
  HadamardFunc h_func_;
  ACMRandom rnd_;
};

class Hadamard8x8Test : public HadamardTestBase {};

TEST_P(Hadamard8x8Test, CompareReferenceRandom) {
108 109 110
  DECLARE_ALIGNED(16, int16_t, a[64]);
  DECLARE_ALIGNED(16, int16_t, b[64]);
  int16_t b_ref[64];
Johann's avatar
Johann committed
111
  for (int i = 0; i < 64; ++i) {
Johann's avatar
Johann committed
112 113 114 115 116
    a[i] = rnd_.Rand9Signed();
  }
  memset(b, 0, sizeof(b));
  memset(b_ref, 0, sizeof(b_ref));

Johann's avatar
Johann committed
117
  reference_hadamard8x8(a, 8, b_ref);
Johann's avatar
Johann committed
118 119 120 121 122 123 124 125
  ASM_REGISTER_STATE_CHECK(h_func_(a, 8, b));

  // The order of the output is not important. Sort before checking.
  std::sort(b, b + 64);
  std::sort(b_ref, b_ref + 64);
  EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
}

Johann's avatar
Johann committed
126
TEST_P(Hadamard8x8Test, VaryStride) {
127 128 129
  DECLARE_ALIGNED(16, int16_t, a[64 * 8]);
  DECLARE_ALIGNED(16, int16_t, b[64]);
  int16_t b_ref[64];
Johann's avatar
Johann committed
130
  for (int i = 0; i < 64 * 8; ++i) {
Johann's avatar
Johann committed
131 132 133 134 135 136 137
    a[i] = rnd_.Rand9Signed();
  }

  for (int i = 8; i < 64; i += 8) {
    memset(b, 0, sizeof(b));
    memset(b_ref, 0, sizeof(b_ref));

Johann's avatar
Johann committed
138
    reference_hadamard8x8(a, i, b_ref);
Johann's avatar
Johann committed
139 140 141 142 143 144 145 146 147
    ASM_REGISTER_STATE_CHECK(h_func_(a, i, b));

    // The order of the output is not important. Sort before checking.
    std::sort(b, b + 64);
    std::sort(b_ref, b_ref + 64);
    EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
  }
}

Johann's avatar
Johann committed
148
INSTANTIATE_TEST_CASE_P(C, Hadamard8x8Test,
149
                        ::testing::Values(&aom_hadamard_8x8_c));
Johann's avatar
Johann committed
150 151

#if HAVE_SSE2
Johann's avatar
Johann committed
152
INSTANTIATE_TEST_CASE_P(SSE2, Hadamard8x8Test,
153
                        ::testing::Values(&aom_hadamard_8x8_sse2));
Johann's avatar
Johann committed
154 155
#endif  // HAVE_SSE2

156
#if HAVE_SSSE3 && ARCH_X86_64
Johann's avatar
Johann committed
157
INSTANTIATE_TEST_CASE_P(SSSE3, Hadamard8x8Test,
158
                        ::testing::Values(&aom_hadamard_8x8_ssse3));
159
#endif  // HAVE_SSSE3 && ARCH_X86_64
Johann's avatar
Johann committed
160 161

#if HAVE_NEON
Johann's avatar
Johann committed
162
INSTANTIATE_TEST_CASE_P(NEON, Hadamard8x8Test,
163
                        ::testing::Values(&aom_hadamard_8x8_neon));
Johann's avatar
Johann committed
164
#endif  // HAVE_NEON
Johann's avatar
Johann committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

class Hadamard16x16Test : public HadamardTestBase {};

TEST_P(Hadamard16x16Test, CompareReferenceRandom) {
  DECLARE_ALIGNED(16, int16_t, a[16 * 16]);
  DECLARE_ALIGNED(16, int16_t, b[16 * 16]);
  int16_t b_ref[16 * 16];
  for (int i = 0; i < 16 * 16; ++i) {
    a[i] = rnd_.Rand9Signed();
  }
  memset(b, 0, sizeof(b));
  memset(b_ref, 0, sizeof(b_ref));

  reference_hadamard16x16(a, 16, b_ref);
  ASM_REGISTER_STATE_CHECK(h_func_(a, 16, b));

  // The order of the output is not important. Sort before checking.
  std::sort(b, b + 16 * 16);
  std::sort(b_ref, b_ref + 16 * 16);
  EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
}

TEST_P(Hadamard16x16Test, VaryStride) {
  DECLARE_ALIGNED(16, int16_t, a[16 * 16 * 8]);
  DECLARE_ALIGNED(16, int16_t, b[16 * 16]);
  int16_t b_ref[16 * 16];
  for (int i = 0; i < 16 * 16 * 8; ++i) {
    a[i] = rnd_.Rand9Signed();
  }

  for (int i = 8; i < 64; i += 8) {
    memset(b, 0, sizeof(b));
    memset(b_ref, 0, sizeof(b_ref));

    reference_hadamard16x16(a, i, b_ref);
    ASM_REGISTER_STATE_CHECK(h_func_(a, i, b));

    // The order of the output is not important. Sort before checking.
    std::sort(b, b + 16 * 16);
    std::sort(b_ref, b_ref + 16 * 16);
    EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
  }
}

INSTANTIATE_TEST_CASE_P(C, Hadamard16x16Test,
210
                        ::testing::Values(&aom_hadamard_16x16_c));
Johann's avatar
Johann committed
211 212 213

#if HAVE_SSE2
INSTANTIATE_TEST_CASE_P(SSE2, Hadamard16x16Test,
214
                        ::testing::Values(&aom_hadamard_16x16_sse2));
Johann's avatar
Johann committed
215
#endif  // HAVE_SSE2
Johann's avatar
Johann committed
216 217 218

#if HAVE_NEON
INSTANTIATE_TEST_CASE_P(NEON, Hadamard16x16Test,
219
                        ::testing::Values(&aom_hadamard_16x16_neon));
Johann's avatar
Johann committed
220
#endif  // HAVE_NEON
Johann's avatar
Johann committed
221
}  // namespace