diff --git a/src/core/benchmark/ranges.cc b/src/core/benchmark/ranges.cc index fe8f276..01bb5f3 100644 --- a/src/core/benchmark/ranges.cc +++ b/src/core/benchmark/ranges.cc @@ -5,6 +5,7 @@ #include "all_cases/all_cases.h" using klotski::cases::AllCases; +using klotski::cases::BasicRanges; static void SpawnRanges(benchmark::State &state) { // constexpr auto nums = target_nums(); @@ -23,6 +24,13 @@ static void SpawnRanges(benchmark::State &state) { } +static void RangesReverse(benchmark::State &state) { + auto ranges = BasicRanges::instance().fetch(); + for (auto _ : state) { + ranges.reverse(); + } +} + static void RangesUnionExport(benchmark::State &state) { auto &all_cases = AllCases::instance().fetch(); for (auto _ : state) { @@ -71,6 +79,8 @@ static void RangesAt(benchmark::State &state) { // BENCHMARK(SpawnRanges)->Unit(benchmark::kMillisecond); +// BENCHMARK(RangesReverse)->Unit(benchmark::kMillisecond); + // BENCHMARK(RangesUnionExport)->Unit(benchmark::kMillisecond); // BENCHMARK(RangesSize); diff --git a/src/core/ranges/internal/ranges.cc b/src/core/ranges/internal/ranges.cc index 6abfab8..16ee782 100644 --- a/src/core/ranges/internal/ranges.cc +++ b/src/core/ranges/internal/ranges.cc @@ -1,3 +1,5 @@ +#include + #include "ranges/ranges.h" #include "common_code/common_code.h" @@ -5,18 +7,10 @@ using klotski::cases::Ranges; using klotski::codec::CommonCode; using klotski::cases::RangesUnion; -RangesUnion& RangesUnion::operator+=(const RangesUnion &ranges_union) { - for (const auto head : Heads) { - ranges(head) += ranges_union.ranges(head); - } - return *this; -} - std::vector RangesUnion::codes() const { std::vector codes; codes.reserve(size()); for (const auto head : Heads) { - // for (const auto range : (*this)[head]) { for (const auto range : ranges(head)) { codes.emplace_back(CommonCode::unsafe_create(head << 32 | range)); } @@ -26,26 +20,19 @@ std::vector RangesUnion::codes() const { } KLSK_INLINE CommonCode RangesUnion::operator[](size_type n) const { - // uint64_t head = 0; - // while (n >= ranges(head).size()) { - // n -= ranges(head).size(); - // ++head; - // } - // KLSK_ASSUME(head < 16); - // return CommonCode::unsafe_create(head << 32 | ranges(head)[n]); - if (n < ranges(0).size()) { return CommonCode::unsafe_create(ranges(0)[n]); } n -= ranges(0).size(); - KLSK_UNROLL(sizeof(Heads) - 2) - for (const uint64_t head : std::to_array({0x1, 0x2, 0x4, 0x5, 0x6, 0x8, 0x9, 0xA, 0xC, 0xD})) { + KLSK_UNROLL(Heads.size() - 2) + for (const uint64_t head : std::span {Heads.data() + 1, Heads.size() - 2}) { if (n < ranges(head).size()) { return CommonCode::unsafe_create(head << 32 | ranges(head)[n]); } n -= ranges(head).size(); } - return CommonCode::unsafe_create((uint64_t)0xE << 32 | ranges(0xE)[n]); + constexpr auto head = static_cast(0xE); + return CommonCode::unsafe_create(head << 32 | ranges(0xE)[n]); } diff --git a/src/core/ranges/internal/ranges.inl b/src/core/ranges/internal/ranges.inl index 73f5304..8aafb86 100644 --- a/src/core/ranges/internal/ranges.inl +++ b/src/core/ranges/internal/ranges.inl @@ -3,14 +3,39 @@ namespace klotski::cases { KLSK_INLINE_H void Ranges::reverse() { + #pragma clang loop vectorize(enable) for (auto &x : *this) { x = range_reverse(x); } } +inline Ranges& RangesUnion::ranges(const size_t head) { + return std::array::operator[](head); +} + +inline const Ranges& RangesUnion::ranges(const size_t head) const { + return std::array::operator[](head); +} + inline Ranges& Ranges::operator+=(const Ranges &ranges) { this->insert(this->end(), ranges.begin(), ranges.end()); return *this; } +inline RangesUnion& RangesUnion::operator+=(const RangesUnion &ranges_union) { + for (const auto head : Heads) { + ranges(head) += ranges_union.ranges(head); + } + return *this; +} + +KLSK_INLINE_H size_t RangesUnion::size() const { + size_type size = 0; + KLSK_UNROLL(sizeof(Heads)) + for (const auto head : Heads) { + size += ranges(head).size(); + } + return size; +} + } // namespace klotski::cases diff --git a/src/core/ranges/internal/ranges_union.inl b/src/core/ranges/internal/ranges_union.inl deleted file mode 100644 index 9e24961..0000000 --- a/src/core/ranges/internal/ranges_union.inl +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -namespace klotski::cases { - -inline Ranges& RangesUnion::ranges(const size_t head) { - return std::array::operator[](head); -} - -inline const Ranges& RangesUnion::ranges(const size_t head) const { - return std::array::operator[](head); -} - -KLSK_INLINE_H size_t RangesUnion::size() const { - size_type size = 0; - KLSK_UNROLL(sizeof(Heads)) - for (const auto head : Heads) { - size += ranges(head).size(); - } - return size; -} - -// KLSK_INLINE_H uint32_t RangesUnion::operator[](size_type n) const { -// size_t head = 0; -// for (;;) { -// if (n >= ranges(head).size()) { -// n -= ranges(head).size(); -// ++head; -// } else { -// break; -// } -// } -// return ranges(head)[n]; -// } - -} // namespace klotski::cases diff --git a/src/core/ranges/ranges.h b/src/core/ranges/ranges.h index 344283b..a4df2b8 100644 --- a/src/core/ranges/ranges.h +++ b/src/core/ranges/ranges.h @@ -73,4 +73,3 @@ private: } // namespace klotski::cases #include "internal/ranges.inl" -#include "internal/ranges_union.inl"