From 69dc3e9889261761289e58e27016e34d37884be0 Mon Sep 17 00:00:00 2001 From: Bryce Lorenz Kille Date: Wed, 5 Jun 2024 18:53:14 -0500 Subject: [PATCH] Minor shift changes --- benchmark/src/shift_bench.hpp | 16 ++++++++-------- include/bitlib/bit-algorithms/shift.hpp | 17 ++++++++++------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/benchmark/src/shift_bench.hpp b/benchmark/src/shift_bench.hpp index 51b91bd..8f38383 100644 --- a/benchmark/src/shift_bench.hpp +++ b/benchmark/src/shift_bench.hpp @@ -16,7 +16,7 @@ auto BM_BitShiftLeft = [](benchmark::State& state, auto input) { container_type bitcont = make_random_container(container_size); auto first = bit::bit_iterator(std::begin(bitcont)); auto last = bit::bit_iterator(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(); @@ -34,7 +34,7 @@ auto BM_BitShiftLeft_UU = [](benchmark::State& state, auto input) { container_type bitcont = make_random_container(container_size); bit::bit_iterator first = bit::bit_iterator(bitcont.begin()) + 1; bit::bit_iterator last = bit::bit_iterator(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(); @@ -75,7 +75,7 @@ auto BM_BoolShiftLeft = [](benchmark::State& state, auto input) { container_type cont = make_random_container(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(); @@ -91,7 +91,7 @@ auto BM_BitShiftRight = [](benchmark::State& state, auto input) { container_type bitcont = make_random_container(container_size); auto first = bit::bit_iterator(std::begin(bitcont)); auto last = bit::bit_iterator(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(); @@ -105,9 +105,9 @@ auto BM_BitShiftRight_UU = [](benchmark::State& state, auto input) { auto digits = bit::binary_digits::value; auto container_size = ceil(float(total_bits) / digits); container_type bitcont = make_random_container(container_size); - auto first = bit::bit_iterator(std::begin(bitcont)) + 2; - auto last = bit::bit_iterator(std::end(bitcont)) - 3; - auto n = bit::distance(first, last) / 2 + 6; + auto first = bit::bit_iterator(std::begin(bitcont)) + 1; + auto last = bit::bit_iterator(std::end(bitcont)) - 1; + auto n = total_bits / 2 + 3; for (auto _ : state) { benchmark::DoNotOptimize(bit::shift_right(first, last, n)); benchmark::ClobberMemory(); @@ -147,7 +147,7 @@ auto BM_BoolShiftRight = [](benchmark::State& state, auto input) { container_type cont = make_random_container(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(); diff --git a/include/bitlib/bit-algorithms/shift.hpp b/include/bitlib/bit-algorithms/shift.hpp index af87187..081188f 100644 --- a/include/bitlib/bit-algorithms/shift.hpp +++ b/include/bitlib/bit-algorithms/shift.hpp @@ -88,10 +88,7 @@ bit_iterator shift_left( first.position(), (is_last_aligned ? digits : last.position()) - first.position() ); - return bit_iterator( - first.base(), - first.position() + d - n - ); + return first + d - n; } // Triggered if all remaining bits can fit in a word @@ -99,8 +96,7 @@ bit_iterator shift_left( { word_type new_word = get_word(middle, d - n); write_word(new_word, first, d - n); - first += d - n; - return first; + return first + d - n; } // Multiple word case word_type first_value = *first.base(); @@ -349,7 +345,14 @@ bit_iterator 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(*middle_base_prev, *std::next(middle_base_prev), offset); + last_base_prev--; + middle_base_prev--; + } + + if (first.position() <= middle.position()) + { *last_base_prev = _shrd(*middle_base_prev, *std::next(middle_base_prev), offset); last_base_prev--; middle_base_prev--;