1 /* benchmark_crc32.cc -- benchmark crc32 variants 2 * Copyright (C) 2022 Nathan Moinvaziri 3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 5 6 #include <stdio.h> 7 #include <assert.h> 8 9 #include <benchmark/benchmark.h> 10 11 extern "C" { 12 # include "zbuild.h" 13 # include "zutil_p.h" 14 # include "cpu_features.h" 15 } 16 17 #define MAX_RANDOM_INTS (1024 * 1024) 18 #define MAX_RANDOM_INTS_SIZE (MAX_RANDOM_INTS * sizeof(uint32_t)) 19 20 class crc32: public benchmark::Fixture { 21 private: 22 uint32_t *random_ints; 23 24 public: SetUp(const::benchmark::State & state)25 void SetUp(const ::benchmark::State& state) { 26 random_ints = (uint32_t *)zng_alloc(MAX_RANDOM_INTS_SIZE); 27 assert(random_ints != NULL); 28 29 for (int32_t i = 0; i < MAX_RANDOM_INTS; i++) { 30 random_ints[i] = rand(); 31 } 32 } 33 Bench(benchmark::State & state,crc32_func crc32)34 void Bench(benchmark::State& state, crc32_func crc32) { 35 uint32_t hash = 0; 36 37 for (auto _ : state) { 38 hash = crc32(hash, (const unsigned char *)random_ints, state.range(0)); 39 } 40 41 benchmark::DoNotOptimize(hash); 42 } 43 TearDown(const::benchmark::State & state)44 void TearDown(const ::benchmark::State& state) { 45 zng_free(random_ints); 46 } 47 }; 48 49 #define BENCHMARK_CRC32(name, fptr, support_flag) \ 50 BENCHMARK_DEFINE_F(crc32, name)(benchmark::State& state) { \ 51 if (!support_flag) { \ 52 state.SkipWithError("CPU does not support " #name); \ 53 } \ 54 Bench(state, fptr); \ 55 } \ 56 BENCHMARK_REGISTER_F(crc32, name)->Range(1, MAX_RANDOM_INTS_SIZE); 57 58 BENCHMARK_CRC32(braid, crc32_braid, 1); 59 60 #ifdef ARM_ACLE_CRC_HASH 61 BENCHMARK_CRC32(acle, crc32_acle, arm_cpu_has_crc32); 62 #elif defined(POWER8_VSX_CRC32) 63 BENCHMARK_CRC32(power8, crc32_power8, power_cpu_has_arch_2_07); 64 #elif defined(S390_CRC32_VX) 65 BENCHMARK_CRC32(vx, PREFIX(s390_crc32_vx), PREFIX(s390_cpu_has_vx)); 66 #elif defined(X86_PCLMULQDQ_CRC) 67 /* CRC32 fold does a memory copy while hashing */ 68 BENCHMARK_CRC32(pclmulqdq, crc32_pclmulqdq, x86_cpu_has_pclmulqdq); 69 #endif 70