Skip to content

Commit f6f706d

Browse files
committed
Use bmi2 target for kth_set_bit, improve compress_coords and radix sort
1 parent bee5f9d commit f6f706d

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

cp-algo/util/bit.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace cp_algo {
1111
size_t order_of_bit(auto x, size_t k) {
1212
return k ? std::popcount(x << (bit_width<decltype(x)> - k)) : 0;
1313
}
14-
// Requires GCC target("popcnt,bmi2")
14+
[[gnu::target("bmi2")]]
1515
size_t kth_set_bit(uint64_t x, size_t k) {
1616
return std::countr_zero(_pdep_u64(1ULL << k, x));
1717
}

cp-algo/util/compress_coords.hpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
namespace cp_algo {
66
// coords is a range of reference_wrapper<T>
77
auto compress_coords(auto &coords) {
8-
std::vector<int> original;
8+
using T = std::decay_t<std::unwrap_reference_t<
9+
std::ranges::range_value_t<decltype(coords)>
10+
>>;
11+
std::vector<T> original;
12+
if(empty(coords)) {
13+
return original;
14+
}
915
original.reserve(size(coords));
1016
radix_sort(coords);
11-
int idx = -1, prev = -1;
17+
size_t idx = -1;
18+
T prev = ~coords.front();
1219
for(auto &x: coords) {
1320
if(x != prev) {
1421
idx++;

cp-algo/util/sort.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ namespace cp_algo {
2424
if(empty(a)) {
2525
return;
2626
}
27-
auto mx = std::ranges::max(a);
28-
with_bit_floor(size(a), [&]<size_t floor>() {
27+
auto [mn, mx] = std::ranges::minmax(a);
28+
with_bit_floor<1>(size(a), [&]<size_t floor>() {
2929
constexpr int base = std::min<size_t>(floor, 1 << 16);
30-
for(int64_t i = 1; i <= mx; i *= base) {
30+
for(int64_t i = 1; i <= mx - mn; i *= base) {
3131
count_sort<base>(a, [&](auto x) {
32-
return x / i % base;
32+
return (x - mn) / i % base;
3333
});
3434
}
3535
});

0 commit comments

Comments
 (0)