| 1 | /* benchmark_adler32_copy.cc -- benchmark adler32 (elided copy) variants |
| 2 | * Copyright (C) 2022 Nathan Moinvaziri, Adam Stylinski |
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <assert.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | #include <benchmark/benchmark.h> |
| 11 | |
| 12 | extern "C" { |
| 13 | # include "zbuild.h" |
| 14 | # include "zutil_p.h" |
| 15 | # include "cpu_features.h" |
| 16 | } |
| 17 | |
| 18 | #define MAX_RANDOM_INTS (1024 * 1024) |
| 19 | #define MAX_RANDOM_INTS_SIZE (MAX_RANDOM_INTS * sizeof(uint32_t)) |
| 20 | |
| 21 | typedef uint32_t (*adler32_cpy_func)(uint32_t adler, unsigned char *dst, const uint8_t *buf, uint64_t len); |
| 22 | |
| 23 | class adler32_copy: public benchmark::Fixture { |
| 24 | private: |
| 25 | uint32_t *random_ints_src; |
| 26 | uint32_t *random_ints_dst; |
| 27 | |
| 28 | public: |
| 29 | void SetUp(const ::benchmark::State& state) { |
| 30 | /* Control the alignment so that we have the best case scenario for loads. With |
| 31 | * AVX512, unaligned loads can mean we're crossing a cacheline boundary at every load. |
| 32 | * And while this is a realistic scenario, it makes it difficult to compare benchmark |
| 33 | * to benchmark because one allocation could have been aligned perfectly for the loads |
| 34 | * while the subsequent one happened to not be. This is not to be advantageous to AVX512 |
| 35 | * (indeed, all lesser SIMD implementations benefit from this aligned allocation), but to |
| 36 | * control the _consistency_ of the results */ |
| 37 | random_ints_src = (uint32_t *)zng_alloc(MAX_RANDOM_INTS_SIZE); |
| 38 | random_ints_dst = (uint32_t *)zng_alloc(MAX_RANDOM_INTS_SIZE); |
| 39 | assert(random_ints_src != NULL); |
| 40 | assert(random_ints_dst != NULL); |
| 41 | |
| 42 | for (int32_t i = 0; i < MAX_RANDOM_INTS; i++) { |
| 43 | random_ints_src[i] = rand(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void Bench(benchmark::State& state, adler32_cpy_func adler32_func) { |
| 48 | uint32_t hash = 0; |
| 49 | |
| 50 | for (auto _ : state) { |
| 51 | hash = adler32_func(hash, (unsigned char *)random_ints_dst, |
| 52 | (const unsigned char*)random_ints_src, state.range(0)); |
| 53 | } |
| 54 | |
| 55 | benchmark::DoNotOptimize(hash); |
| 56 | } |
| 57 | |
| 58 | void TearDown(const ::benchmark::State& state) { |
| 59 | zng_free(random_ints_src); |
| 60 | zng_free(random_ints_dst); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | #define BENCHMARK_ADLER32_COPY(name, fptr, support_flag) \ |
| 65 | BENCHMARK_DEFINE_F(adler32_copy, name)(benchmark::State& state) { \ |
| 66 | if (!support_flag) { \ |
| 67 | state.SkipWithError("CPU does not support " #name); \ |
| 68 | } \ |
| 69 | Bench(state, fptr); \ |
| 70 | } \ |
| 71 | BENCHMARK_REGISTER_F(adler32_copy, name)->Range(8192, MAX_RANDOM_INTS_SIZE); |
| 72 | |
| 73 | #define BENCHMARK_ADLER32_BASELINE_COPY(name, fptr, support_flag) \ |
| 74 | BENCHMARK_DEFINE_F(adler32_copy, name)(benchmark::State& state) { \ |
| 75 | if (!support_flag) { \ |
| 76 | state.SkipWithError("CPU does not support " #name); \ |
| 77 | } \ |
| 78 | Bench(state, [](uint32_t init_sum, unsigned char *dst, \ |
| 79 | const uint8_t *buf, uint64_t len) -> uint32_t { \ |
| 80 | memcpy(dst, buf, (size_t)len); \ |
| 81 | return fptr(init_sum, buf, len); \ |
| 82 | }); \ |
| 83 | } \ |
| 84 | BENCHMARK_REGISTER_F(adler32_copy, name)->Range(8192, MAX_RANDOM_INTS_SIZE); |
| 85 | |
| 86 | BENCHMARK_ADLER32_BASELINE_COPY(c, adler32_c, 1); |
| 87 | |
| 88 | #ifdef ARM_NEON_ADLER32 |
| 89 | /* If we inline this copy for neon, the function would go here */ |
| 90 | //BENCHMARK_ADLER32_COPY(neon, adler32_neon, arm_cpu_has_neon); |
| 91 | BENCHMARK_ADLER32_BASELINE_COPY(neon_copy_baseline, adler32_neon, arm_cpu_has_neon); |
| 92 | #endif |
| 93 | |
| 94 | #ifdef PPC_VMX_ADLER32 |
| 95 | //BENCHMARK_ADLER32_COPY(vmx_inline_copy, adler32_fold_copy_vmx, power_cpu_has_altivec); |
| 96 | BENCHMARK_ADLER32_BASELINE_COPY(vmx_copy_baseline, adler32_vmx, power_cpu_has_altivec); |
| 97 | #endif |
| 98 | #ifdef POWER8_VSX_ADLER32 |
| 99 | //BENCHMARK_ADLER32_COPY(power8_inline_copy, adler32_fold_copy_power8, power_cpu_has_arch_2_07); |
| 100 | BENCHMARK_ADLER32_BASELINE_COPY(power8, adler32_power8, power_cpu_has_arch_2_07); |
| 101 | #endif |
| 102 | |
| 103 | #ifdef X86_SSE42_ADLER32 |
| 104 | BENCHMARK_ADLER32_BASELINE_COPY(sse42_baseline, adler32_ssse3, x86_cpu_has_ssse3); |
| 105 | BENCHMARK_ADLER32_COPY(sse42, adler32_fold_copy_sse42, x86_cpu_has_sse42); |
| 106 | #endif |
| 107 | #ifdef X86_AVX2_ADLER32 |
| 108 | BENCHMARK_ADLER32_BASELINE_COPY(avx2_baseline, adler32_avx2, x86_cpu_has_avx2); |
| 109 | BENCHMARK_ADLER32_COPY(avx2, adler32_fold_copy_avx2, x86_cpu_has_avx2); |
| 110 | #endif |
| 111 | #ifdef X86_AVX512_ADLER32 |
| 112 | BENCHMARK_ADLER32_BASELINE_COPY(avx512_baseline, adler32_avx512, x86_cpu_has_avx512); |
| 113 | BENCHMARK_ADLER32_COPY(avx512, adler32_fold_copy_avx512, x86_cpu_has_avx512); |
| 114 | #endif |
| 115 | #ifdef X86_AVX512VNNI_ADLER32 |
| 116 | BENCHMARK_ADLER32_BASELINE_COPY(avx512_vnni_baseline, adler32_avx512_vnni, x86_cpu_has_avx512vnni); |
| 117 | BENCHMARK_ADLER32_COPY(avx512_vnni, adler32_fold_copy_avx512_vnni, x86_cpu_has_avx512vnni); |
| 118 | #endif |