Skip to content

Commit 132541a

Browse files
committed
improve to_string
1 parent 7fbce6d commit 132541a

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

cp-algo/structures/bitpack.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,8 @@ namespace cp_algo::structures {
3737

3838
std::string to_string() const {
3939
std::string res(blocks * width, '0');
40-
for(size_t i = 0, pos = 0; i < blocks; i++, pos += width) {
41-
auto block = data[i];
42-
for(size_t j = 0; j < width; j++) {
43-
res[pos + j] = '0' + block % 2;
44-
block /= 2;
45-
}
40+
for(size_t i = 0; i < blocks; i++) {
41+
write_bits64(res.data() + i * width, data[i]);
4642
}
4743
res.resize(n);
4844
return res;

cp-algo/util/bit.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,30 @@ namespace cp_algo {
3232
[[gnu::always_inline]] inline uint64_t read_bits64(char const* p) {
3333
return read_bits(p) | (uint64_t(read_bits(p + 32)) << 32);
3434
}
35+
36+
[[gnu::target("avx2"), gnu::always_inline]] inline void write_bits(char *p, uint32_t bits) {
37+
auto bytes = u32x8() + bits;
38+
static constexpr u8x32 shuffler = {
39+
0, 0, 0, 0, 0, 0, 0, 0,
40+
1, 1, 1, 1, 1, 1, 1, 1,
41+
2, 2, 2, 2, 2, 2, 2, 2,
42+
3, 3, 3, 3, 3, 3, 3, 3
43+
};
44+
auto shuffled = u8x32(_mm256_shuffle_epi8(__m256i() + bits, __m256i(shuffler)));
45+
static constexpr u8x32 mask = {
46+
1, 2, 4, 8, 16, 32, 64, 128,
47+
1, 2, 4, 8, 16, 32, 64, 128,
48+
1, 2, 4, 8, 16, 32, 64, 128,
49+
1, 2, 4, 8, 16, 32, 64, 128
50+
};
51+
u8x32 to_save = (shuffled & mask) ? '1' : '0';
52+
for(int z = 0; z < 32; z++) {
53+
p[z] = to_save[z];
54+
}
55+
}
56+
[[gnu::target("avx2"), gnu::always_inline]] inline void write_bits64(char *p, uint64_t bits) {
57+
write_bits(p, uint32_t(bits));
58+
write_bits(p + 32, uint32_t(bits >> 32));
59+
}
3560
}
3661
#endif // CP_ALGO_UTIL_BIT_HPP

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy