aboutsummaryrefslogtreecommitdiff
path: root/src/crc32c/src/crc32c_benchmark.cc
blob: c464304b3f2879da5e352432b78c6ac507c78367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2017 The CRC32C Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include <cstddef>
#include <cstdint>

#ifdef CRC32C_HAVE_CONFIG_H
#include "crc32c/crc32c_config.h"
#endif

#include "benchmark/benchmark.h"

#if CRC32C_TESTS_BUILT_WITH_GLOG
#include "glog/logging.h"
#endif  // CRC32C_TESTS_BUILT_WITH_GLOG

#include "./crc32c_arm64.h"
#include "./crc32c_arm64_linux_check.h"
#include "./crc32c_internal.h"
#include "./crc32c_sse42.h"
#include "./crc32c_sse42_check.h"
#include "crc32c/crc32c.h"

class CRC32CBenchmark : public benchmark::Fixture {
 public:
  void SetUp(const benchmark::State& state) override {
    block_size_ = static_cast<size_t>(state.range(0));
    block_data_ = std::string(block_size_, 'x');
    block_buffer_ = reinterpret_cast<const uint8_t*>(block_data_.data());
  }

 protected:
  std::string block_data_;
  const uint8_t* block_buffer_;
  size_t block_size_;
};

BENCHMARK_DEFINE_F(CRC32CBenchmark, Public)(benchmark::State& state) {
  uint32_t crc = 0;
  for (auto _ : state)
    crc = crc32c::Extend(crc, block_buffer_, block_size_);
  state.SetBytesProcessed(state.iterations() * block_size_);
}
BENCHMARK_REGISTER_F(CRC32CBenchmark, Public)
    ->RangeMultiplier(16)
    ->Range(256, 16777216);  // Block size.

BENCHMARK_DEFINE_F(CRC32CBenchmark, Portable)(benchmark::State& state) {
  uint32_t crc = 0;
  for (auto _ : state)
    crc = crc32c::ExtendPortable(crc, block_buffer_, block_size_);
  state.SetBytesProcessed(state.iterations() * block_size_);
}
BENCHMARK_REGISTER_F(CRC32CBenchmark, Portable)
    ->RangeMultiplier(16)
    ->Range(256, 16777216);  // Block size.

#if HAVE_ARM64_CRC32C

BENCHMARK_DEFINE_F(CRC32CBenchmark, ArmLinux)(benchmark::State& state) {
  if (!crc32c::CanUseArm64Linux()) {
    state.SkipWithError("ARM CRC32C instructions not available or not enabled");
    return;
  }

  uint32_t crc = 0;
  for (auto _ : state)
    crc = crc32c::ExtendArm64(crc, block_buffer_, block_size_);
  state.SetBytesProcessed(state.iterations() * block_size_);
}
BENCHMARK_REGISTER_F(CRC32CBenchmark, ArmLinux)
    ->RangeMultiplier(16)
    ->Range(256, 16777216);  // Block size.

#endif  // HAVE_ARM64_CRC32C

#if HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__))

BENCHMARK_DEFINE_F(CRC32CBenchmark, Sse42)(benchmark::State& state) {
  if (!crc32c::CanUseSse42()) {
    state.SkipWithError("SSE4.2 instructions not available or not enabled");
    return;
  }

  uint32_t crc = 0;
  for (auto _ : state)
    crc = crc32c::ExtendSse42(crc, block_buffer_, block_size_);
  state.SetBytesProcessed(state.iterations() * block_size_);
}
BENCHMARK_REGISTER_F(CRC32CBenchmark, Sse42)
    ->RangeMultiplier(16)
    ->Range(256, 16777216);  // Block size.

#endif  // HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__))

int main(int argc, char** argv) {
#if CRC32C_TESTS_BUILT_WITH_GLOG
  google::InitGoogleLogging(argv[0]);
  google::InstallFailureSignalHandler();
#endif  // CRC32C_TESTS_BUILT_WITH_GLOG

  benchmark::Initialize(&argc, argv);
  benchmark::RunSpecifiedBenchmarks();
  return 0;
}