1 /* benchmark_compare256.cc -- benchmark compare256 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 8 #include <benchmark/benchmark.h> 9 10 extern "C" { 11 # include "zbuild.h" 12 # include "zutil_p.h" 13 # include "cpu_features.h" 14 } 15 16 #define MAX_COMPARE_SIZE (256) 17 18 class compare256: public benchmark::Fixture { 19 private: 20 uint8_t *str1; 21 uint8_t *str2; 22 23 public: SetUp(const::benchmark::State & state)24 void SetUp(const ::benchmark::State& state) { 25 str1 = (uint8_t *)zng_alloc(MAX_COMPARE_SIZE); 26 assert(str1 != NULL); 27 memset(str1, 'a', MAX_COMPARE_SIZE); 28 29 str2 = (uint8_t *)zng_alloc(MAX_COMPARE_SIZE); 30 assert(str2 != NULL); 31 memset(str2, 'a', MAX_COMPARE_SIZE); 32 } 33 Bench(benchmark::State & state,compare256_func compare256)34 void Bench(benchmark::State& state, compare256_func compare256) { 35 int32_t match_len = (int32_t)state.range(0) - 1; 36 uint32_t len; 37 38 str2[match_len] = 0; 39 for (auto _ : state) { 40 len = compare256((const uint8_t *)str1, (const uint8_t *)str2); 41 } 42 str2[match_len] = 'a'; 43 44 benchmark::DoNotOptimize(len); 45 } 46 TearDown(const::benchmark::State & state)47 void TearDown(const ::benchmark::State& state) { 48 zng_free(str1); 49 zng_free(str2); 50 } 51 }; 52 53 #define BENCHMARK_COMPARE256(name, fptr, support_flag) \ 54 BENCHMARK_DEFINE_F(compare256, name)(benchmark::State& state) { \ 55 if (!support_flag) { \ 56 state.SkipWithError("CPU does not support " #name); \ 57 } \ 58 Bench(state, fptr); \ 59 } \ 60 BENCHMARK_REGISTER_F(compare256, name)->Range(1, MAX_COMPARE_SIZE); 61 62 BENCHMARK_COMPARE256(c, compare256_c, 1); 63 64 #ifdef UNALIGNED_OK 65 BENCHMARK_COMPARE256(unaligned_16, compare256_unaligned_16, 1); 66 #ifdef HAVE_BUILTIN_CTZ 67 BENCHMARK_COMPARE256(unaligned_32, compare256_unaligned_32, 1); 68 #endif 69 #if defined(UNALIGNED64_OK) && defined(HAVE_BUILTIN_CTZLL) 70 BENCHMARK_COMPARE256(unaligned_64, compare256_unaligned_64, 1); 71 #endif 72 #endif 73 #if defined(X86_SSE2) && defined(HAVE_BUILTIN_CTZ) 74 BENCHMARK_COMPARE256(sse2, compare256_sse2, x86_cpu_has_sse2); 75 #endif 76 #if defined(X86_AVX2) && defined(HAVE_BUILTIN_CTZ) 77 BENCHMARK_COMPARE256(avx2, compare256_avx2, x86_cpu_has_avx2); 78 #endif 79 #if defined(ARM_NEON) && defined(HAVE_BUILTIN_CTZLL) 80 BENCHMARK_COMPARE256(neon, compare256_neon, arm_cpu_has_neon); 81 #endif 82 #ifdef POWER9 83 BENCHMARK_COMPARE256(power9, compare256_power9, power_cpu_has_arch_3_00); 84 #endif 85