Browse Source

feat: add benchmark of range reverse

master
Dnomd343 4 months ago
parent
commit
1a5fc07687
  1. 4
      src/core/CMakeLists.txt
  2. 39
      src/core/benchmark/utility.cc
  3. 18
      src/core/utils/utility.h

4
src/core/CMakeLists.txt

@ -57,3 +57,7 @@ target_link_libraries(bm_all_cases PRIVATE klotski::core benchmark::benchmark_ma
add_executable(bm_ranges benchmark/ranges.cc)
target_compile_options(bm_ranges PRIVATE -fno-rtti -fno-exceptions)
target_link_libraries(bm_ranges PRIVATE klotski::core benchmark::benchmark_main)
add_executable(bm_utility benchmark/utility.cc)
target_compile_options(bm_utility PRIVATE -fno-rtti -fno-exceptions)
target_link_libraries(bm_utility PRIVATE klotski::core benchmark::benchmark_main)

39
src/core/benchmark/utility.cc

@ -0,0 +1,39 @@
#include <benchmark/benchmark.h>
#include <iostream>
#include "utils/utility.h"
#include "all_cases/all_cases.h"
using klotski::cases::Ranges;
using klotski::cases::BasicRanges;
static Ranges range_samples() {
auto ranges = BasicRanges::instance().fetch();
Ranges samples;
for (uint64_t i = 0; i < ranges.size(); ++i) {
if (i % 73500 == 0) {
samples.emplace_back(ranges[i]);
}
}
return samples;
}
static void range_reverse(benchmark::State &state) {
auto samples = range_samples();
for (auto _ : state) {
for (const auto range : samples) {
volatile auto kk = klotski::range_reverse(range);
}
}
}
BENCHMARK(range_reverse);
BENCHMARK_MAIN();

18
src/core/utils/utility.h

@ -5,6 +5,8 @@
#include <numeric>
#include <functional>
// ----------------------------------------------------------------------------------------- //
/// Mark target class as a singleton.
#define KLSK_INSTANCE(T) \
private: \
@ -28,6 +30,8 @@
/// Prevent reordering for both compiler and processor.
#define KLSK_MEM_BARRIER std::atomic_thread_fence(std::memory_order_seq_cst)
// ----------------------------------------------------------------------------------------- //
namespace klotski {
/// Calculate the sum of an array of integers.
@ -54,20 +58,14 @@ consteval std::array<T, N> to_offset(const std::array<T, N> &arr) {
}
/// Flips the input u32 every two bits in low-high symmetry.
inline uint32_t range_reverse(uint32_t bin) {
constexpr uint32_t range_reverse(uint32_t bin) {
bin = std::byteswap(bin);
// #if defined(__GNUC__) || defined(__clang__)
// bin = __builtin_bswap32(bin);
// #else
// // FIXME: `_byteswap_ulong` under MSVC
// // TODO: using `std::byteswap` (c++23)
// bin = ((bin << 16) & 0xFFFF0000) | ((bin >> 16) & 0x0000FFFF);
// bin = ((bin << 8) & 0xFF00FF00) | ((bin >> 8) & 0x00FF00FF);
// #endif
bin = ((bin << 4) & 0xF0F0F0F0) | ((bin >> 4) & 0x0F0F0F0F);
return ((bin << 2) & 0xCCCCCCCC) | ((bin >> 2) & 0x33333333);
}
// ----------------------------------------------------------------------------------------- //
/// Empty function calls that generally used for callbacks.
typedef std::function<void()> Notifier;
@ -97,6 +95,8 @@ private:
std::list<Task> tasks_;
};
// ----------------------------------------------------------------------------------------- //
} // namespace klotski
#include "worker.inl"

Loading…
Cancel
Save