Skip to content

Commit

Permalink
Minor shift changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bkille committed Jun 5, 2024
1 parent 2e47baa commit 69dc3e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
16 changes: 8 additions & 8 deletions benchmark/src/shift_bench.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ auto BM_BitShiftLeft = [](benchmark::State& state, auto input) {
container_type bitcont = make_random_container<container_type>(container_size);
auto first = bit::bit_iterator<decltype(std::begin(bitcont))>(std::begin(bitcont));
auto last = bit::bit_iterator<decltype(std::end(bitcont))>(std::end(bitcont));
auto n = bit::distance(first, last) / 2 - 1;
auto n = total_bits / 2 - 1;
for (auto _ : state) {
benchmark::DoNotOptimize(bit::shift_left(first, last, n));
benchmark::ClobberMemory();
Expand All @@ -34,7 +34,7 @@ auto BM_BitShiftLeft_UU = [](benchmark::State& state, auto input) {
container_type bitcont = make_random_container<container_type>(container_size);
bit::bit_iterator<iterator_type> first = bit::bit_iterator<iterator_type>(bitcont.begin()) + 1;
bit::bit_iterator<iterator_type> last = bit::bit_iterator<iterator_type>(bitcont.end()) - 1;
auto n = bit::distance(first, last) / 2 + 6;
auto n = total_bits / 2 + 3;
for (auto _ : state) {
benchmark::DoNotOptimize(bit::shift_left(first, last, n));
benchmark::ClobberMemory();
Expand Down Expand Up @@ -75,7 +75,7 @@ auto BM_BoolShiftLeft = [](benchmark::State& state, auto input) {
container_type cont = make_random_container<container_type>(container_size);
auto first = cont.begin();
auto last = cont.end();
auto n = std::distance(first, last) / 2 + 6;
auto n = std::distance(first, last) / 2 - 1;
for (auto _ : state) {
benchmark::DoNotOptimize(bit::word_shift_left(first, last, n));
benchmark::ClobberMemory();
Expand All @@ -91,7 +91,7 @@ auto BM_BitShiftRight = [](benchmark::State& state, auto input) {
container_type bitcont = make_random_container<container_type>(container_size);
auto first = bit::bit_iterator<decltype(std::begin(bitcont))>(std::begin(bitcont));
auto last = bit::bit_iterator<decltype(std::end(bitcont))>(std::end(bitcont));
auto n = bit::distance(first, last) / 2 - 1;
auto n = total_bits / 2 - 1;
for (auto _ : state) {
benchmark::DoNotOptimize(bit::shift_right(first, last, n));
benchmark::ClobberMemory();
Expand All @@ -105,9 +105,9 @@ auto BM_BitShiftRight_UU = [](benchmark::State& state, auto input) {
auto digits = bit::binary_digits<word_type>::value;
auto container_size = ceil(float(total_bits) / digits);
container_type bitcont = make_random_container<container_type>(container_size);
auto first = bit::bit_iterator<decltype(std::begin(bitcont))>(std::begin(bitcont)) + 2;
auto last = bit::bit_iterator<decltype(std::end(bitcont))>(std::end(bitcont)) - 3;
auto n = bit::distance(first, last) / 2 + 6;
auto first = bit::bit_iterator<decltype(std::begin(bitcont))>(std::begin(bitcont)) + 1;
auto last = bit::bit_iterator<decltype(std::end(bitcont))>(std::end(bitcont)) - 1;
auto n = total_bits / 2 + 3;
for (auto _ : state) {
benchmark::DoNotOptimize(bit::shift_right(first, last, n));
benchmark::ClobberMemory();
Expand Down Expand Up @@ -147,7 +147,7 @@ auto BM_BoolShiftRight = [](benchmark::State& state, auto input) {
container_type cont = make_random_container<container_type>(container_size);
auto first = cont.begin();
auto last = cont.end();
auto n = std::distance(first, last) / 2 + 6;
auto n = std::distance(first, last) / 2 - 1;
for (auto _ : state) {
benchmark::DoNotOptimize(bit::word_shift_right(first, last, n));
benchmark::ClobberMemory();
Expand Down
17 changes: 10 additions & 7 deletions include/bitlib/bit-algorithms/shift.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,15 @@ bit_iterator<RandomAccessIt> shift_left(
first.position(),
(is_last_aligned ? digits : last.position()) - first.position()
);
return bit_iterator<RandomAccessIt>(
first.base(),
first.position() + d - n
);
return first + d - n;
}

// Triggered if all remaining bits can fit in a word
if (d - n <= digits)
{
word_type new_word = get_word<word_type, RandomAccessIt>(middle, d - n);
write_word<word_type, RandomAccessIt>(new_word, first, d - n);
first += d - n;
return first;
return first + d - n;
}
// Multiple word case
word_type first_value = *first.base();
Expand Down Expand Up @@ -349,7 +345,14 @@ bit_iterator<RandomAccessIt> shift_right(
auto last_base_prev = std::prev(last.base());
auto middle_base_prev = std::prev(middle.base());

while (middle_base_prev + (first.position() <= middle.position()) > first.base()) {
while (middle_base_prev > first.base()) {
*last_base_prev = _shrd<word_type>(*middle_base_prev, *std::next(middle_base_prev), offset);
last_base_prev--;
middle_base_prev--;
}

if (first.position() <= middle.position())
{
*last_base_prev = _shrd<word_type>(*middle_base_prev, *std::next(middle_base_prev), offset);
last_base_prev--;
middle_base_prev--;
Expand Down

0 comments on commit 69dc3e9

Please sign in to comment.