Bug 1882465 - Update .hg-annotate-ignore-revs and .git-blame-ignore-revs to reflect...
[gecko.git] / third_party / aom / test / hash_test.cc
bloba1de9323dbf65dc430c2f2c06cc855866e64c71a
1 /*
2 * Copyright (c) 2018, Alliance for Open Media. All rights reserved
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.
12 #include <cstdlib>
13 #include <new>
14 #include <tuple>
16 #include "config/aom_config.h"
17 #include "config/av1_rtcd.h"
19 #include "aom_ports/aom_timer.h"
20 #include "av1/encoder/hash.h"
21 #include "test/acm_random.h"
22 #include "test/util.h"
23 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
25 namespace {
27 typedef uint32_t (*get_crc32c_value_func)(void *calculator, uint8_t *p,
28 size_t length);
30 typedef std::tuple<get_crc32c_value_func, int> HashParam;
32 class AV1Crc32cHashTest : public ::testing::TestWithParam<HashParam> {
33 public:
34 ~AV1Crc32cHashTest() override;
35 void SetUp() override;
37 void TearDown() override;
39 protected:
40 void RunCheckOutput(get_crc32c_value_func test_impl);
41 void RunSpeedTest(get_crc32c_value_func test_impl);
43 void RunZeroTest(get_crc32c_value_func test_impl);
45 libaom_test::ACMRandom rnd_;
46 CRC32C calc_;
47 uint8_t *buffer_;
48 int bsize_;
49 size_t length_;
52 AV1Crc32cHashTest::~AV1Crc32cHashTest() = default;
54 void AV1Crc32cHashTest::SetUp() {
55 rnd_.Reset(libaom_test::ACMRandom::DeterministicSeed());
56 av1_crc32c_calculator_init(&calc_);
58 bsize_ = GET_PARAM(1);
59 length_ = bsize_ * bsize_ * sizeof(uint16_t);
60 buffer_ = new uint8_t[length_];
61 ASSERT_NE(buffer_, nullptr);
62 for (size_t i = 0; i < length_; ++i) {
63 buffer_[i] = rnd_.Rand8();
67 void AV1Crc32cHashTest::TearDown() { delete[] buffer_; }
69 void AV1Crc32cHashTest::RunCheckOutput(get_crc32c_value_func test_impl) {
70 get_crc32c_value_func ref_impl = av1_get_crc32c_value_c;
71 // for the same buffer crc should be the same
72 uint32_t crc0 = test_impl(&calc_, buffer_, length_);
73 uint32_t crc1 = test_impl(&calc_, buffer_, length_);
74 uint32_t crc2 = ref_impl(&calc_, buffer_, length_);
75 ASSERT_EQ(crc0, crc1);
76 ASSERT_EQ(crc0, crc2); // should equal to software version
77 // modify buffer
78 buffer_[0] += 1;
79 uint32_t crc3 = test_impl(&calc_, buffer_, length_);
80 uint32_t crc4 = ref_impl(&calc_, buffer_, length_);
81 ASSERT_NE(crc0, crc3); // crc shoud not equal to previous one
82 ASSERT_EQ(crc3, crc4);
85 void AV1Crc32cHashTest::RunSpeedTest(get_crc32c_value_func test_impl) {
86 get_crc32c_value_func impls[] = { av1_get_crc32c_value_c, test_impl };
87 const int repeat = 10000000 / (bsize_ + bsize_);
89 aom_usec_timer timer;
90 double time[2];
91 for (int i = 0; i < 2; ++i) {
92 aom_usec_timer_start(&timer);
93 for (int j = 0; j < repeat; ++j) {
94 impls[i](&calc_, buffer_, length_);
96 aom_usec_timer_mark(&timer);
97 time[i] = static_cast<double>(aom_usec_timer_elapsed(&timer));
99 printf("hash %3dx%-3d:%7.2f/%7.2fus", bsize_, bsize_, time[0], time[1]);
100 printf("(%3.2f)\n", time[0] / time[1]);
103 void AV1Crc32cHashTest::RunZeroTest(get_crc32c_value_func test_impl) {
104 uint8_t buffer0[1024] = { 0 };
105 // for buffer with different size the crc should not be the same
106 const uint32_t crc0 = test_impl(&calc_, buffer0, 32);
107 const uint32_t crc1 = test_impl(&calc_, buffer0, 128);
108 const uint32_t crc2 = test_impl(&calc_, buffer0, 1024);
109 ASSERT_NE(crc0, crc1);
110 ASSERT_NE(crc0, crc2);
111 ASSERT_NE(crc1, crc2);
114 TEST_P(AV1Crc32cHashTest, CheckOutput) { RunCheckOutput(GET_PARAM(0)); }
116 TEST_P(AV1Crc32cHashTest, CheckZero) { RunZeroTest(GET_PARAM(0)); }
118 TEST_P(AV1Crc32cHashTest, DISABLED_Speed) { RunSpeedTest(GET_PARAM(0)); }
120 const int kValidBlockSize[] = { 64, 32, 8, 4 };
122 INSTANTIATE_TEST_SUITE_P(
123 C, AV1Crc32cHashTest,
124 ::testing::Combine(::testing::Values(&av1_get_crc32c_value_c),
125 ::testing::ValuesIn(kValidBlockSize)));
127 #if HAVE_SSE4_2
128 INSTANTIATE_TEST_SUITE_P(
129 SSE4_2, AV1Crc32cHashTest,
130 ::testing::Combine(::testing::Values(&av1_get_crc32c_value_sse4_2),
131 ::testing::ValuesIn(kValidBlockSize)));
132 #endif
134 #if HAVE_ARM_CRC32
135 INSTANTIATE_TEST_SUITE_P(
136 ARM_CRC32, AV1Crc32cHashTest,
137 ::testing::Combine(::testing::Values(&av1_get_crc32c_value_arm_crc32),
138 ::testing::ValuesIn(kValidBlockSize)));
139 #endif
141 } // namespace