Skip to content

Commit fd15881

Browse files
authored
Merge pull request #631 from intel/lukevalenty/pseudo_pext_lookup_indirect_search_fix
Fix two bugs related to pseudo pext indirect lookup with search
2 parents 8fe5d1c + e0a87d9 commit fd15881

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

include/lookup/lookup.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
namespace lookup {
1111
[[nodiscard]] CONSTEVAL static auto make(compile_time auto input) {
1212
return strategies<linear_search_lookup<4>,
13-
pseudo_pext_lookup<true, 1>>::make(input);
13+
pseudo_pext_lookup<true, 2>>::make(input);
1414
}
1515
} // namespace lookup

include/lookup/pseudo_pext_lookup.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ constexpr auto count_duplicates(std::array<T, S> keys) -> std::size_t {
122122
return dups;
123123
}
124124

125-
/// count the length of the longest run of identical values (n log n)
125+
/// count the length of the longest run of identical values (n)
126126
template <typename T, std::size_t S>
127127
constexpr auto count_longest_run(std::array<T, S> keys) -> std::size_t {
128128
std::sort(keys.begin(), keys.end());
@@ -138,8 +138,11 @@ constexpr auto count_longest_run(std::array<T, S> keys) -> std::size_t {
138138

139139
if (curr_value == prev_value) {
140140
current_run++;
141-
} else {
142-
longest_run = std::max(longest_run, current_run);
141+
}
142+
143+
longest_run = std::max(longest_run, current_run);
144+
145+
if (curr_value != prev_value) {
143146
current_run = 0;
144147
}
145148

@@ -241,15 +244,15 @@ constexpr auto calc_pseudo_pext_mask(std::array<entry<T, V>, S> const &pairs,
241244
while (max_search_len > 1 && std::popcount(mask) > 4) {
242245
auto try_mask = remove_cheapest_bit(mask, keys);
243246
auto current_longest_run = count_longest_run(with_mask(try_mask, keys));
244-
if (current_longest_run < max_search_len) {
247+
if (current_longest_run <= max_search_len) {
245248
mask = try_mask;
246249
prev_longest_run = current_longest_run;
247250
} else {
248251
return std::make_tuple(mask, prev_longest_run);
249252
}
250253
}
251254

252-
return std::make_tuple(mask, std::size_t{});
255+
return std::make_tuple(mask, prev_longest_run);
253256
}
254257

255258
} // namespace detail

test/lookup/pseudo_pext_lookup.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,39 @@ TEMPLATE_TEST_CASE("lookup with scoped enum entries", "[pseudo pext lookup]",
9898
CHECK(lookup[some_key_t::KAPPA] == 87);
9999
CHECK(lookup[some_key_t::GAMMA] == 4);
100100
}
101+
102+
TEST_CASE("pbt regression 0", "[pseudo pext lookup]") {
103+
constexpr auto lookup = lookup::pseudo_pext_lookup<true, 2>::make(
104+
CX_VALUE(lookup::input<std::uint16_t, std::uint16_t, 5>{
105+
0, std::array<lookup::entry<std::uint16_t, std::uint16_t>, 5>{
106+
lookup::entry<std::uint16_t, std::uint16_t>{1, 0},
107+
lookup::entry<std::uint16_t, std::uint16_t>{3, 0},
108+
lookup::entry<std::uint16_t, std::uint16_t>{11, 0},
109+
lookup::entry<std::uint16_t, std::uint16_t>{16, 0},
110+
lookup::entry<std::uint16_t, std::uint16_t>{0, 1}}}));
111+
112+
CHECK(lookup[1] == 0);
113+
CHECK(lookup[3] == 0);
114+
CHECK(lookup[11] == 0);
115+
CHECK(lookup[16] == 0);
116+
CHECK(lookup[0] == 1);
117+
}
118+
119+
TEST_CASE("pbt regression 1", "[pseudo pext lookup]") {
120+
constexpr auto lookup = lookup::pseudo_pext_lookup<true, 2>::make(
121+
CX_VALUE(lookup::input<std::uint16_t, std::uint16_t, 6>{
122+
0, std::array<lookup::entry<std::uint16_t, std::uint16_t>, 6>{
123+
lookup::entry<std::uint16_t, std::uint16_t>{1, 0},
124+
lookup::entry<std::uint16_t, std::uint16_t>{3, 0},
125+
lookup::entry<std::uint16_t, std::uint16_t>{4, 0},
126+
lookup::entry<std::uint16_t, std::uint16_t>{15, 0},
127+
lookup::entry<std::uint16_t, std::uint16_t>{30, 0},
128+
lookup::entry<std::uint16_t, std::uint16_t>{31, 1}}}));
129+
130+
CHECK(lookup[1] == 0);
131+
CHECK(lookup[3] == 0);
132+
CHECK(lookup[4] == 0);
133+
CHECK(lookup[15] == 0);
134+
CHECK(lookup[30] == 0);
135+
CHECK(lookup[31] == 1);
136+
}

0 commit comments

Comments
 (0)