|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// UNSUPPORTED: c++03 |
| 10 | + |
| 11 | +#include <cstdint> |
| 12 | +#include <cstddef> |
| 13 | +#include <functional> |
| 14 | + |
| 15 | +#include "benchmark/benchmark.h" |
| 16 | + |
| 17 | +#include "GenerateInput.h" |
| 18 | +#include "test_macros.h" |
| 19 | + |
| 20 | +constexpr std::size_t TestNumInputs = 1024; |
| 21 | + |
| 22 | +template <class _Size> |
| 23 | +inline TEST_ALWAYS_INLINE _Size loadword(const void* __p) { |
| 24 | + _Size __r; |
| 25 | + std::memcpy(&__r, __p, sizeof(__r)); |
| 26 | + return __r; |
| 27 | +} |
| 28 | + |
| 29 | +inline TEST_ALWAYS_INLINE std::size_t hash_len_16(std::size_t __u, std::size_t __v) { |
| 30 | + const std::size_t __mul = 0x9ddfea08eb382d69ULL; |
| 31 | + std::size_t __a = (__u ^ __v) * __mul; |
| 32 | + __a ^= (__a >> 47); |
| 33 | + std::size_t __b = (__v ^ __a) * __mul; |
| 34 | + __b ^= (__b >> 47); |
| 35 | + __b *= __mul; |
| 36 | + return __b; |
| 37 | +} |
| 38 | + |
| 39 | +template <std::size_t _Len> |
| 40 | +inline TEST_ALWAYS_INLINE std::size_t hash_len_0_to_8(const char* __s) { |
| 41 | + static_assert(_Len == 4 || _Len == 8, ""); |
| 42 | + const uint64_t __a = loadword<uint32_t>(__s); |
| 43 | + const uint64_t __b = loadword<uint32_t>(__s + _Len - 4); |
| 44 | + return hash_len_16(_Len + (__a << 3), __b); |
| 45 | +} |
| 46 | + |
| 47 | +struct UInt32Hash { |
| 48 | + UInt32Hash() = default; |
| 49 | + inline TEST_ALWAYS_INLINE std::size_t operator()(uint32_t data) const { |
| 50 | + return hash_len_0_to_8<4>(reinterpret_cast<const char*>(&data)); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +template <class HashFn, class GenInputs> |
| 55 | +void BM_Hash(benchmark::State& st, HashFn fn, GenInputs gen) { |
| 56 | + auto in = gen(st.range(0)); |
| 57 | + const auto end = in.data() + in.size(); |
| 58 | + std::size_t last_hash = 0; |
| 59 | + benchmark::DoNotOptimize(&last_hash); |
| 60 | + while (st.KeepRunning()) { |
| 61 | + for (auto it = in.data(); it != end; ++it) { |
| 62 | + benchmark::DoNotOptimize(last_hash += fn(*it)); |
| 63 | + } |
| 64 | + benchmark::ClobberMemory(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +BENCHMARK_CAPTURE(BM_Hash, uint32_random_std_hash, std::hash<uint32_t>{}, getRandomIntegerInputs<uint32_t>) |
| 69 | + ->Arg(TestNumInputs); |
| 70 | + |
| 71 | +BENCHMARK_CAPTURE(BM_Hash, uint32_random_custom_hash, UInt32Hash{}, getRandomIntegerInputs<uint32_t>) |
| 72 | + ->Arg(TestNumInputs); |
| 73 | + |
| 74 | +BENCHMARK_CAPTURE(BM_Hash, uint32_top_std_hash, std::hash<uint32_t>{}, getSortedTopBitsIntegerInputs<uint32_t>) |
| 75 | + ->Arg(TestNumInputs); |
| 76 | + |
| 77 | +BENCHMARK_CAPTURE(BM_Hash, uint32_top_custom_hash, UInt32Hash{}, getSortedTopBitsIntegerInputs<uint32_t>) |
| 78 | + ->Arg(TestNumInputs); |
| 79 | + |
| 80 | +BENCHMARK_MAIN(); |
0 commit comments