Skip to content

Commit

Permalink
Division (#236)
Browse files Browse the repository at this point in the history
* minor on Bitstream

* Minor changes on Bitmask

* minor

---------

Co-authored-by: Samuel Li <sam@cisl-m121a>
  • Loading branch information
shaomeng and Samuel Li authored Apr 16, 2024
1 parent d87bdd0 commit f6ddfa7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
18 changes: 10 additions & 8 deletions src/Bitmask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sperr::Bitmask::Bitmask(size_t nbits)
{
if (nbits > 0) {
auto num_longs = nbits / 64;
if (nbits % 64 != 0)
if (nbits - num_longs * 64 != 0)
num_longs++;
m_buf.assign(num_longs, 0);
m_num_bits = nbits;
Expand All @@ -23,7 +23,7 @@ auto sperr::Bitmask::size() const -> size_t
void sperr::Bitmask::resize(size_t nbits)
{
auto num_longs = nbits / 64;
if (nbits % 64 != 0)
if (nbits - num_longs * 64 != 0)
num_longs++;
m_buf.resize(num_longs, 0);
m_num_bits = nbits;
Expand All @@ -36,8 +36,10 @@ auto sperr::Bitmask::rlong(size_t idx) const -> uint64_t

auto sperr::Bitmask::rbit(size_t idx) const -> bool
{
auto word = m_buf[idx / 64];
word &= uint64_t{1} << (idx % 64);
auto div = idx / 64;
auto rem = idx - div * 64;
auto word = m_buf[div];
word &= uint64_t{1} << rem;
return (word != 0);
}

Expand All @@ -50,7 +52,7 @@ auto sperr::Bitmask::has_true(size_t start, size_t len) const -> int64_t
// Collect the remaining bits from the start long.
auto word = m_buf[long_idx];
auto answer = uint64_t{0};
for (auto i = start % 64; i < 64 && processed_bits < len; i++) {
for (auto i = start - long_idx * 64; i < 64 && processed_bits < len; i++) {
answer |= word & (uint64_t{1} << i);
if constexpr (Position) {
if (answer != 0)
Expand Down Expand Up @@ -133,7 +135,7 @@ void sperr::Bitmask::wlong(size_t idx, uint64_t value)
void sperr::Bitmask::wbit(size_t idx, bool bit)
{
const auto wstart = idx / 64;
const auto mask = uint64_t{1} << (idx % 64);
const auto mask = uint64_t{1} << (idx - wstart * 64);

auto word = m_buf[wstart];
if (bit)
Expand All @@ -146,7 +148,7 @@ void sperr::Bitmask::wbit(size_t idx, bool bit)
void sperr::Bitmask::wtrue(size_t idx)
{
const auto wstart = idx / 64;
const auto mask = uint64_t{1} << (idx % 64);
const auto mask = uint64_t{1} << (idx - wstart * 64);

auto word = m_buf[wstart];
word |= mask;
Expand All @@ -156,7 +158,7 @@ void sperr::Bitmask::wtrue(size_t idx)
void sperr::Bitmask::wfalse(size_t idx)
{
const auto wstart = idx / 64;
const auto mask = uint64_t{1} << (idx % 64);
const auto mask = uint64_t{1} << (idx - wstart * 64);

auto word = m_buf[wstart];
word &= ~mask;
Expand Down
20 changes: 12 additions & 8 deletions src/Bitstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void sperr::Bitstream::reserve(size_t nbits)
if (nbits > m_buf.size() * 64) {
// Number of longs that's absolutely needed.
auto num_longs = nbits / 64;
if (nbits % 64 != 0)
if (num_longs * 64 < nbits)
num_longs++;

const auto dist = std::distance(m_buf.begin(), m_itr);
Expand All @@ -54,8 +54,9 @@ auto sperr::Bitstream::rtell() const -> size_t

void sperr::Bitstream::rseek(size_t offset)
{
m_itr = m_buf.begin() + offset / 64;
const auto rem = offset % 64;
size_t div = offset / 64;
size_t rem = offset - div * 64;
m_itr = m_buf.begin() + div;
if (rem) {
m_buffer = *m_itr >> rem;
++m_itr;
Expand Down Expand Up @@ -90,8 +91,9 @@ auto sperr::Bitstream::wtell() const -> size_t

void sperr::Bitstream::wseek(size_t offset)
{
m_itr = m_buf.begin() + offset / 64;
const auto rem = offset % 64;
size_t div = offset / 64;
size_t rem = offset - div * 64;
m_itr = m_buf.begin() + div;
if (rem) {
m_buffer = *m_itr;
m_buffer &= (uint64_t{1} << rem) - 1;
Expand All @@ -114,7 +116,7 @@ void sperr::Bitstream::wbit(bool bit)
#endif
{
if (m_itr == m_buf.end()) { // allocate memory if necessary.
const auto dist = m_buf.size();
auto dist = m_buf.size();
m_buf.resize(std::max(size_t{1}, dist) * 2 - dist / 2); // use a growth factor of 1.5
m_itr = m_buf.begin() + dist;
}
Expand All @@ -129,7 +131,7 @@ void sperr::Bitstream::flush()
{
if (m_bits) { // only really flush when there are remaining bits.
if (m_itr == m_buf.end()) {
const auto dist = m_buf.size();
auto dist = m_buf.size();
m_buf.resize(std::max(size_t{1}, dist) * 2 - dist / 2); // use a growth factor of 1.5
m_itr = m_buf.begin() + dist;
}
Expand All @@ -144,6 +146,7 @@ void sperr::Bitstream::flush()
void sperr::Bitstream::write_bitstream(void* p, size_t num_bits) const
{
assert(num_bits <= m_buf.size() * 64);

const auto num_longs = num_bits / 64;
auto rem_bytes = num_bits / 8 - num_longs * sizeof(uint64_t);
if (num_bits % 8 != 0)
Expand All @@ -162,8 +165,9 @@ void sperr::Bitstream::write_bitstream(void* p, size_t num_bits) const
auto sperr::Bitstream::get_bitstream(size_t num_bits) const -> std::vector<std::byte>
{
assert(num_bits <= m_buf.size() * 64);

auto num_bytes = num_bits / 8;
if (num_bits % 8 != 0)
if (num_bits - num_bytes * 8 != 0)
num_bytes++;

auto tmp = std::vector<std::byte>(num_bytes);
Expand Down

0 comments on commit f6ddfa7

Please sign in to comment.