diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index f05c911..c357ade 100644 --- a/src/core/CMakeLists.txt +++ b/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) diff --git a/src/core/benchmark/utility.cc b/src/core/benchmark/utility.cc new file mode 100644 index 0000000..efb01c6 --- /dev/null +++ b/src/core/benchmark/utility.cc @@ -0,0 +1,39 @@ +#include + +#include + +#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(); diff --git a/src/core/utils/utility.h b/src/core/utils/utility.h index 67995b2..d5fe048 100644 --- a/src/core/utils/utility.h +++ b/src/core/utils/utility.h @@ -5,6 +5,8 @@ #include #include +// ----------------------------------------------------------------------------------------- // + /// 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 to_offset(const std::array &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 Notifier; @@ -97,6 +95,8 @@ private: std::list tasks_; }; +// ----------------------------------------------------------------------------------------- // + } // namespace klotski #include "worker.inl"