| 1 | /* benchmark_slidehash.cc -- benchmark slide_hash variants |
| 2 | * Copyright (C) 2022 Adam Stylinski, Nathan Moinvaziri |
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 | */ |
| 5 | |
| 6 | #include <limits.h> |
| 7 | |
| 8 | #include <benchmark/benchmark.h> |
| 9 | |
| 10 | extern "C" { |
| 11 | # include "zbuild.h" |
| 12 | # include "zutil_p.h" |
| 13 | # include "deflate.h" |
| 14 | # include "cpu_features.h" |
| 15 | } |
| 16 | |
| 17 | #define MAX_RANDOM_INTS 32768 |
| 18 | |
| 19 | class slide_hash: public benchmark::Fixture { |
| 20 | private: |
| 21 | uint16_t *l0; |
| 22 | uint16_t *l1; |
| 23 | deflate_state *s_g; |
| 24 | |
| 25 | public: |
| 26 | void SetUp(const ::benchmark::State& state) { |
| 27 | l0 = (uint16_t *)zng_alloc(HASH_SIZE * sizeof(uint16_t)); |
| 28 | |
| 29 | for (int32_t i = 0; i < HASH_SIZE; i++) { |
| 30 | l0[i] = rand(); |
| 31 | } |
| 32 | |
| 33 | l1 = (uint16_t *)zng_alloc(MAX_RANDOM_INTS * sizeof(uint16_t)); |
| 34 | |
| 35 | for (int32_t i = 0; i < MAX_RANDOM_INTS; i++) { |
| 36 | l1[i] = rand(); |
| 37 | } |
| 38 | |
| 39 | deflate_state *s = (deflate_state*)malloc(sizeof(deflate_state)); |
| 40 | s->head = l0; |
| 41 | s->prev = l1; |
| 42 | s_g = s; |
| 43 | } |
| 44 | |
| 45 | void Bench(benchmark::State& state, slide_hash_func slide_hash) { |
| 46 | s_g->w_size = (uint32_t)state.range(0); |
| 47 | |
| 48 | for (auto _ : state) { |
| 49 | slide_hash(s_g); |
| 50 | benchmark::DoNotOptimize(s_g); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void TearDown(const ::benchmark::State& state) { |
| 55 | zng_free(l0); |
| 56 | zng_free(l1); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | #define BENCHMARK_SLIDEHASH(name, fptr, support_flag) \ |
| 61 | BENCHMARK_DEFINE_F(slide_hash, name)(benchmark::State& state) { \ |
| 62 | if (!support_flag) { \ |
| 63 | state.SkipWithError("CPU does not support " #name); \ |
| 64 | } \ |
| 65 | Bench(state, fptr); \ |
| 66 | } \ |
| 67 | BENCHMARK_REGISTER_F(slide_hash, name)->RangeMultiplier(2)->Range(1024, MAX_RANDOM_INTS); |
| 68 | |
| 69 | BENCHMARK_SLIDEHASH(c, slide_hash_c, 1); |
| 70 | |
| 71 | #ifdef ARM_NEON_SLIDEHASH |
| 72 | BENCHMARK_SLIDEHASH(neon, slide_hash_neon, arm_cpu_has_neon); |
| 73 | #endif |
| 74 | #ifdef POWER8_VSX_SLIDEHASH |
| 75 | BENCHMARK_SLIDEHASH(power8, slide_hash_power8, power_cpu_has_arch_2_07); |
| 76 | #endif |
| 77 | #ifdef PPC_VMX_SLIDEHASH |
| 78 | BENCHMARK_SLIDEHASH(vmx, slide_hash_vmx, power_cpu_has_altivec); |
| 79 | #endif |
| 80 | |
| 81 | #ifdef X86_SSE2 |
| 82 | BENCHMARK_SLIDEHASH(sse2, slide_hash_sse2, x86_cpu_has_sse2); |
| 83 | #endif |
| 84 | #ifdef X86_AVX2 |
| 85 | BENCHMARK_SLIDEHASH(avx2, slide_hash_avx2, x86_cpu_has_avx2); |
| 86 | #endif |