Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions vespalib/src/tests/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ vespa_add_executable(vespalib_util_gtest_runner_test_app TEST
SOURCES
gtest_runner.cpp
bfloat16_test.cpp
bit_packer_test.cpp
bit_span_test.cpp
bits_test.cpp
casts_test.cpp
Expand Down
63 changes: 63 additions & 0 deletions vespalib/src/tests/util/bit_packer_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/bit_packer.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vector>

using vespalib::BitPacker;
using vespalib::BitSpan;

namespace {

std::vector<bool> my_bits = {1, 1, 0, 0, 0, 1, 1, 1,
0, 0, 1, 1, 1, 0, 0, 0,
1, 1, 1, 1, 0, 0, 0, 0};

}

TEST(BitPackerTest, bits_can_be_packed)
{
BitPacker packer;
EXPECT_TRUE(packer.empty());
EXPECT_EQ(packer.size(), 0);
EXPECT_EQ(packer.storage().size(), 0);
for (int i = 0; i < 24; ++i) {
packer.push_back(my_bits[i]);
int bitcnt = i + 1;
EXPECT_FALSE(packer.empty());
EXPECT_EQ(packer.size(), bitcnt);
EXPECT_EQ(packer.storage().size(), (bitcnt + 7) / 8);
}
EXPECT_EQ(packer.size(), 24);
ASSERT_EQ(packer.storage().size(), 3);
EXPECT_EQ(packer.storage()[0], std::byte{0b11100011});
EXPECT_EQ(packer.storage()[1], std::byte{0b00011100});
EXPECT_EQ(packer.storage()[2], std::byte{0b00001111});
}

TEST(BitPackerTest, bit_span_can_be_created) {
BitPacker packer;
for (int i = 0; i < 24; ++i) {
packer.push_back(my_bits[i]);
}
auto span = packer.bit_span(10, 9);
ASSERT_EQ(span.size(), 9);
for (int i = 0; i < 9; ++ i) {
int idx = i + 10;
EXPECT_EQ(bool(my_bits[idx]), span[i]);
}
}

TEST(BitPackerTest, bit_spans_are_clamped) {
BitPacker packer;
for (int i = 0; i < 24; ++i) {
packer.push_back(my_bits[i]);
}
auto span = packer.bit_span(16, 100);
ASSERT_EQ(span.size(), 8);
for (int i = 0; i < 8; ++ i) {
int idx = i + 16;
EXPECT_EQ(bool(my_bits[idx]), span[i]);
}
EXPECT_EQ(packer.bit_span(100, 16).size(), 0);
}
37 changes: 13 additions & 24 deletions vespalib/src/tests/util/bit_span_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,7 @@

using vespalib::BitSpan;

std::vector<int> list(std::vector<int> bits) { return bits; }
std::vector<uint8_t> pack(std::vector<int> bits) {
size_t cnt = 0;
uint8_t byte = 0;
std::vector<uint8_t> result;
for (bool bit: bits) {
byte |= (uint8_t(bit) << (cnt % 8));
if ((++cnt % 8) == 0) {
result.push_back(byte);
byte = 0;
}
}
if ((cnt % 8) != 0) {
result.push_back(byte);
}
return result;
}
namespace {

std::vector<int> extract_with_range(BitSpan span) {
std::vector<int> result;
Expand All @@ -40,6 +24,18 @@ std::vector<int> extract_with_loop(BitSpan span) {
return result;
}

// shared test data, one int per bit
std::vector<int> my_bits = {1, 1, 0, 0, 0, 1, 1, 1,
0, 0, 1, 1, 1, 0, 0, 0,
1, 1, 1, 1, 0, 0, 0, 0};

// shared test data, bits packed into 3 bytes (NOTE: LSB first)
std::vector<std::byte> packed = {std::byte{0b11100011},
std::byte{0b00011100},
std::byte{0b00001111}};

} // namespace

TEST(BitSpanTest, empty_span)
{
BitSpan span;
Expand All @@ -56,13 +52,6 @@ TEST(BitSpanTest, empty_span_with_offset)
EXPECT_FALSE(span.begin() != span.end());
}

// shared test data, one int per bit
auto my_bits = list({1, 1, 0, 0, 0, 1, 1, 1,
0, 0, 1, 1, 1, 0, 0, 0,
1, 1, 1, 1, 0, 0, 0, 0});
// shared test data, bits packed into 3 bytes
auto packed = pack(my_bits);

TEST(BitSpanTest, span_with_all_the_bits)
{
BitSpan span(packed.data(), 3 * 8);
Expand Down
40 changes: 40 additions & 0 deletions vespalib/src/vespa/vespalib/util/bit_packer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "bit_span.h"
#include <algorithm>
#include <vector>

namespace vespalib {

/**
* Packs bool values into a vector of bytes (LSB-first), matching
* the layout expected by BitSpan.
*/
class BitPacker {
std::vector<std::byte> _data;
uint64_t _count;
public:
BitPacker() noexcept : _data(), _count(0) {}
void reserve(uint64_t bits) { _data.reserve((bits + 7) / 8); }
void push_back(bool bit) {
auto bit_idx = _count++ % 8;
if (bit_idx == 0) {
_data.push_back(std::byte{0});
}
if (bit) {
_data.back() |= (std::byte{1} << bit_idx);
}
}
BitSpan bit_span(uint64_t offset, uint64_t length) const noexcept {
uint64_t clamped_offset = std::min(offset, _count);
uint64_t clamped_length = std::min(length, _count - clamped_offset);
return BitSpan(_data.data() + (clamped_offset / 8), clamped_offset % 8, clamped_length);
}
uint64_t size() const noexcept { return _count; }
bool empty() const noexcept { return _count == 0; }
const std::vector<std::byte> &storage() const noexcept { return _data; }
};

}
21 changes: 11 additions & 10 deletions vespalib/src/vespa/vespalib/util/bit_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <cstddef>
#include <cstdint>

namespace vespalib {
Expand All @@ -12,9 +13,9 @@ namespace vespalib {
* An optional bit offset allows the span to start at any bit position.
*/
class BitSpan {
const uint8_t* _data;
uint32_t _offset;
uint32_t _count;
const std::byte* _data;
uint32_t _offset;
uint32_t _count;
public:
class Sentinel {
const uint32_t _end;
Expand All @@ -23,19 +24,19 @@ class BitSpan {
bool valid(uint32_t pos) const noexcept { return pos < _end; }
};
class Iterator {
const uint8_t* _data;
uint32_t _pos;
const std::byte* _data;
uint32_t _pos;
public:
Iterator(const uint8_t* data, uint32_t pos) noexcept : _data(data), _pos(pos) {}
bool operator*() const noexcept { return (_data[_pos / 8] >> (_pos % 8)) & 1; }
Iterator(const std::byte* data, uint32_t pos) noexcept : _data(data), _pos(pos) {}
bool operator*() const noexcept { return ((_data[_pos / 8] >> (_pos % 8)) & std::byte{1}) != std::byte{0}; }
Iterator& operator++() noexcept { ++_pos; return *this; }
bool operator!=(Sentinel s) const noexcept { return s.valid(_pos); }
};

BitSpan() noexcept : _data(nullptr), _offset(0), _count(0) {}
BitSpan(const void* data, uint32_t count) noexcept : _data(static_cast<const uint8_t*>(data)), _offset(0), _count(count) {}
BitSpan(const void* data, uint32_t offset, uint32_t count) noexcept : _data(static_cast<const uint8_t*>(data)), _offset(offset), _count(count) {}
bool operator[](uint32_t i) const noexcept { return (_data[(_offset + i) / 8] >> ((_offset + i) % 8)) & 1; }
BitSpan(const void* data, uint32_t count) noexcept : _data(static_cast<const std::byte*>(data)), _offset(0), _count(count) {}
BitSpan(const void* data, uint32_t offset, uint32_t count) noexcept : _data(static_cast<const std::byte*>(data)), _offset(offset), _count(count) {}
bool operator[](uint32_t i) const noexcept { return ((_data[(_offset + i) / 8] >> ((_offset + i) % 8)) & std::byte{1}) != std::byte{0}; }
uint32_t size() const noexcept { return _count; }
bool empty() const noexcept { return _count == 0; }
Iterator begin() const noexcept { return Iterator(_data, _offset); }
Expand Down