PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 1 | /* |
James Zern | b7c05bd | 2024-06-11 19:15:10 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2018, Alliance for Open Media. All rights reserved. |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 3 | * |
| 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. |
| 10 | */ |
| 11 | |
| 12 | #include <cstdlib> |
| 13 | #include <new> |
sarahparker | a543df5 | 2018-11-02 16:02:05 -0700 | [diff] [blame] | 14 | #include <tuple> |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 15 | |
Tom Finegan | 60e653d | 2018-05-22 11:34:58 -0700 | [diff] [blame] | 16 | #include "config/aom_config.h" |
Tom Finegan | 44702c8 | 2018-05-22 13:00:39 -0700 | [diff] [blame] | 17 | #include "config/av1_rtcd.h" |
Tom Finegan | 60e653d | 2018-05-22 11:34:58 -0700 | [diff] [blame] | 18 | |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 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" |
| 24 | |
Peng Bin | fdfdb19 | 2018-03-01 13:24:42 +0800 | [diff] [blame] | 25 | namespace { |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 26 | |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 27 | typedef uint32_t (*get_crc32c_value_func)(void *calculator, uint8_t *p, |
Hien Ho | 5fa56f1 | 2019-04-26 13:29:26 -0700 | [diff] [blame] | 28 | size_t length); |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 29 | |
sarahparker | a543df5 | 2018-11-02 16:02:05 -0700 | [diff] [blame] | 30 | typedef std::tuple<get_crc32c_value_func, int> HashParam; |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 31 | |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 32 | class AV1Crc32cHashTest : public ::testing::TestWithParam<HashParam> { |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 33 | public: |
James Zern | faa2dcf | 2023-07-24 18:29:51 -0700 | [diff] [blame] | 34 | ~AV1Crc32cHashTest() override; |
| 35 | void SetUp() override; |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 36 | |
James Zern | faa2dcf | 2023-07-24 18:29:51 -0700 | [diff] [blame] | 37 | void TearDown() override; |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 38 | |
| 39 | protected: |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 40 | void RunCheckOutput(get_crc32c_value_func test_impl); |
| 41 | void RunSpeedTest(get_crc32c_value_func test_impl); |
Peng Bin | 3fdd255 | 2018-04-02 16:38:34 +0800 | [diff] [blame] | 42 | |
| 43 | void RunZeroTest(get_crc32c_value_func test_impl); |
| 44 | |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 45 | libaom_test::ACMRandom rnd_; |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 46 | CRC32C calc_; |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 47 | uint8_t *buffer_; |
| 48 | int bsize_; |
Hien Ho | 5fa56f1 | 2019-04-26 13:29:26 -0700 | [diff] [blame] | 49 | size_t length_; |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 50 | }; |
| 51 | |
James Zern | f1fa1eb | 2023-07-25 15:34:13 -0700 | [diff] [blame] | 52 | AV1Crc32cHashTest::~AV1Crc32cHashTest() = default; |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 53 | |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 54 | void AV1Crc32cHashTest::SetUp() { |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 55 | rnd_.Reset(libaom_test::ACMRandom::DeterministicSeed()); |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 56 | av1_crc32c_calculator_init(&calc_); |
| 57 | |
| 58 | bsize_ = GET_PARAM(1); |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 59 | length_ = bsize_ * bsize_ * sizeof(uint16_t); |
Peng Bin | fdfdb19 | 2018-03-01 13:24:42 +0800 | [diff] [blame] | 60 | buffer_ = new uint8_t[length_]; |
James Zern | 9dea04e | 2022-04-28 13:18:36 -0700 | [diff] [blame] | 61 | ASSERT_NE(buffer_, nullptr); |
Hien Ho | 5fa56f1 | 2019-04-26 13:29:26 -0700 | [diff] [blame] | 62 | for (size_t i = 0; i < length_; ++i) { |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 63 | buffer_[i] = rnd_.Rand8(); |
| 64 | } |
| 65 | } |
| 66 | |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 67 | void AV1Crc32cHashTest::TearDown() { delete[] buffer_; } |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 68 | |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 69 | void AV1Crc32cHashTest::RunCheckOutput(get_crc32c_value_func test_impl) { |
| 70 | get_crc32c_value_func ref_impl = av1_get_crc32c_value_c; |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 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_); |
Peng Bin | fdfdb19 | 2018-03-01 13:24:42 +0800 | [diff] [blame] | 81 | ASSERT_NE(crc0, crc3); // crc shoud not equal to previous one |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 82 | ASSERT_EQ(crc3, crc4); |
| 83 | } |
| 84 | |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 85 | void AV1Crc32cHashTest::RunSpeedTest(get_crc32c_value_func test_impl) { |
| 86 | get_crc32c_value_func impls[] = { av1_get_crc32c_value_c, test_impl }; |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 87 | const int repeat = 10000000 / (bsize_ + bsize_); |
| 88 | |
| 89 | aom_usec_timer timer; |
Peng Bin | fdfdb19 | 2018-03-01 13:24:42 +0800 | [diff] [blame] | 90 | double time[2]; |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 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_); |
| 95 | } |
| 96 | aom_usec_timer_mark(&timer); |
| 97 | time[i] = static_cast<double>(aom_usec_timer_elapsed(&timer)); |
| 98 | } |
| 99 | printf("hash %3dx%-3d:%7.2f/%7.2fus", bsize_, bsize_, time[0], time[1]); |
| 100 | printf("(%3.2f)\n", time[0] / time[1]); |
| 101 | } |
| 102 | |
Peng Bin | 3fdd255 | 2018-04-02 16:38:34 +0800 | [diff] [blame] | 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); |
| 112 | } |
| 113 | |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 114 | TEST_P(AV1Crc32cHashTest, CheckOutput) { RunCheckOutput(GET_PARAM(0)); } |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 115 | |
Peng Bin | 3fdd255 | 2018-04-02 16:38:34 +0800 | [diff] [blame] | 116 | TEST_P(AV1Crc32cHashTest, CheckZero) { RunZeroTest(GET_PARAM(0)); } |
| 117 | |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 118 | TEST_P(AV1Crc32cHashTest, DISABLED_Speed) { RunSpeedTest(GET_PARAM(0)); } |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 119 | |
| 120 | const int kValidBlockSize[] = { 64, 32, 8, 4 }; |
| 121 | |
Cheng Chen | 96786fe | 2020-02-14 17:28:25 -0800 | [diff] [blame] | 122 | INSTANTIATE_TEST_SUITE_P( |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 123 | C, AV1Crc32cHashTest, |
| 124 | ::testing::Combine(::testing::Values(&av1_get_crc32c_value_c), |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 125 | ::testing::ValuesIn(kValidBlockSize))); |
| 126 | |
| 127 | #if HAVE_SSE4_2 |
Cheng Chen | 96786fe | 2020-02-14 17:28:25 -0800 | [diff] [blame] | 128 | INSTANTIATE_TEST_SUITE_P( |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 129 | SSE4_2, AV1Crc32cHashTest, |
| 130 | ::testing::Combine(::testing::Values(&av1_get_crc32c_value_sse4_2), |
PENGBIN | ffda377 | 2018-02-26 17:36:37 +0800 | [diff] [blame] | 131 | ::testing::ValuesIn(kValidBlockSize))); |
| 132 | #endif |
| 133 | |
Jonathan Wright | 6f22393 | 2022-05-31 23:07:50 +0100 | [diff] [blame] | 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 |
| 140 | |
Peng Bin | fdfdb19 | 2018-03-01 13:24:42 +0800 | [diff] [blame] | 141 | } // namespace |