19 #include <boost/crc.hpp> 29 const unsigned int BUFFER_SIZE = 512 * 1024 *
sizeof(
uint64_t);
32 struct ExpectedResult {
38 ExpectedResult expectedResults[] = {
69 {0, BUFFER_SIZE, 2096790750},
70 {1, BUFFER_SIZE / 2, 3854797577},
75 for (
auto expected : expectedResults) {
76 uint32_t result = impl(buffer + expected.offset, expected.length, ~0U);
81 void testCRC32CContinuation(
83 for (
auto expected : expectedResults) {
84 size_t partialLength = expected.length / 2;
86 impl(buffer + expected.offset, partialLength, ~0U);
88 buffer + expected.offset + partialLength,
89 expected.length - partialLength,
95 void testMatchesBoost32Type() {
96 for (
auto expected : expectedResults) {
97 boost::crc_32_type result;
98 result.process_bytes(buffer + expected.offset, expected.length);
99 const uint32_t boostResult = result.checksum();
108 TEST(Checksum, crc32c_software) {
112 TEST(Checksum, crc32c_continuation_software) {
116 TEST(Checksum, crc32c_hardware) {
120 LOG(WARNING) <<
"skipping hardware-accelerated CRC-32C tests" 121 <<
" (not supported on this CPU)";
125 TEST(Checksum, crc32c_hardware_eq) {
127 for (
int i = 0;
i < 1000;
i++) {
133 LOG(WARNING) <<
"skipping hardware-accelerated CRC-32C tests" 134 <<
" (not supported on this CPU)";
138 TEST(Checksum, crc32c_continuation_hardware) {
142 LOG(WARNING) <<
"skipping hardware-accelerated CRC-32C tests" 143 <<
" (not supported on this CPU)";
147 TEST(Checksum, crc32c_autodetect) {
151 TEST(Checksum, crc32c_continuation_autodetect) {
158 for (
auto expected : expectedResults) {
166 LOG(WARNING) <<
"skipping hardware-accelerated CRC-32 tests" 167 <<
" (not supported on this CPU)";
171 TEST(Checksum, crc32_continuation) {
174 for (
auto expected : expectedResults) {
175 auto halflen = expected.length / 2;
179 buffer + expected.offset + halflen, halflen, sw_res);
183 buffer + expected.offset + halflen, halflen, hw_res);
193 LOG(WARNING) <<
"skipping hardware-accelerated CRC-32 tests" 194 <<
" (not supported on this CPU)";
200 testMatchesBoost32Type();
204 for (
size_t totlen = 1024; totlen < BUFFER_SIZE; totlen += BUFFER_SIZE / 8) {
207 auto crc2 =
folly::crc32(&buffer[mid], totlen - mid, 0);
215 for (
size_t totlen = 1024; totlen < BUFFER_SIZE; totlen += BUFFER_SIZE / 8) {
228 for (
unsigned long i = 0;
i < iters;
i++) {
233 LOG(WARNING) <<
"skipping hardware-accelerated CRC-32C benchmarks" 234 <<
" (not supported on this CPU)";
240 for (
unsigned long i = 0;
i < iters;
i++) {
249 for (
unsigned long i = 0;
i < iters;
i++) {
254 LOG(WARNING) <<
"skipping hardware-accelerated CRC-32 benchmarks" 255 <<
" (not supported on this CPU)";
261 for (
unsigned long i = 0;
i < iters;
i++) {
272 for (
unsigned long i = 0;
i < iters;
i++) {
280 std::vector<uint8_t> zbuffer;
281 zbuffer.reserve(blockSize);
282 memset(zbuffer.data(), 0, blockSize);
286 for (
unsigned long i = 0;
i < iters;
i++) {
287 result =
folly::crc32c(zbuffer.data(), blockSize, checksum1);
298 for (
unsigned long i = 0;
i < iters;
i++) {
376 gflags::ParseCommandLineFlags(&argc, &argv,
true);
390 if (!ret && FLAGS_benchmark) {
uint32_t crc32_type(const uint8_t *data, size_t nbytes, uint32_t startingChecksum)
std::vector< uint8_t > buffer(kBufferSize+16)
uint64_t fnv64_buf(const void *buf, size_t n, uint64_t hash=FNV_64_HASH_START) noexcept
BENCHMARK(crc32c_hardware_1KB_block, iters)
void benchmarkSoftwareCRC32C(unsigned long iters, size_t blockSize)
void benchmarkCombineHardwareCrc32(unsigned long iters, size_t blockSize)
uint32_t crc32c(const uint8_t *data, size_t nbytes, uint32_t startingChecksum)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
#define EXPECT_EQ(val1, val2)
uint32_t crc32_sw(const uint8_t *data, size_t nbytes, uint32_t startingChecksum)
int main(int argc, char **argv)
void benchmarkSoftwareCRC32(unsigned long iters, size_t blockSize)
void benchmarkCombineHardwareCrc32c(unsigned long iters, size_t blockSize)
uint32_t crc32(const uint8_t *data, size_t nbytes, uint32_t startingChecksum)
uint32_t crc32c_hw(const uint8_t *data, size_t nbytes, uint32_t startingChecksum=~0U)
auto end(TestAdlIterable &instance)
bool crc32_hw_supported()
void benchmarkCombineSoftwareLinear(unsigned long iters, size_t blockSize)
TEST(Checksum, crc32c_software)
uint32_t crc32c_sw(const uint8_t *data, size_t nbytes, uint32_t startingChecksum)
uint32_t crc32c_combine(uint32_t crc1, uint32_t crc2, size_t crc2len)
uint32_t crc32_hw(const uint8_t *, size_t, uint32_t)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
uint32_t crc32_combine(uint32_t crc1, uint32_t crc2, size_t crc2len)
void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize)
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
bool crc32c_hw_supported()
void benchmarkHardwareCRC32(unsigned long iters, size_t blockSize)