Files
bitserial/benchmarks/baq_bulk_benchmarks.cpp
T
DawnMagnet a8b5e91e81 Add bitserial bulk packing and unpacking functionality
- Introduced bulk.hpp for efficient bit packing and unpacking of various data types.
- Implemented scalar and AVX2 optimized functions for packing and unpacking bits.
- Added configuration checks for C++20 and AVX2 support in config.hpp.
- Created comprehensive tests in bitserial_tests.cpp to validate functionality across unsigned, signed, boolean, float, double, nested structures, and arrays.
- Included edge case tests for bulk packing and unpacking to ensure correctness.
2026-05-04 22:37:23 +08:00

120 lines
3.1 KiB
C++

#include "catch_amalgamated.hpp"
#include <bitserial/bitserial.hpp>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <random>
#include <span>
#include <vector>
namespace {
constexpr std::size_t kElementCount = 1024 * 1024;
template <std::size_t Bits>
struct BaqFixture {
static constexpr std::uint8_t max_value =
static_cast<std::uint8_t>((1u << Bits) - 1u);
std::vector<std::uint8_t> src;
std::vector<std::uint8_t> packed_ref;
std::vector<std::uint8_t> packed_bulk;
std::vector<std::uint8_t> unpacked_ref;
std::vector<std::uint8_t> unpacked_bulk;
explicit BaqFixture(std::size_t n = kElementCount) :
src(n),
packed_ref((n * Bits + 7) / 8, 0),
packed_bulk((n * Bits + 7) / 8, 0),
unpacked_ref(n),
unpacked_bulk(n) {
std::mt19937 gen(0xB17u + static_cast<unsigned>(Bits));
std::uniform_int_distribution<std::uint16_t> dist(0, max_value);
for (std::uint8_t &x : src) {
x = static_cast<std::uint8_t>(dist(gen));
}
pack_reference(src, packed_ref);
bitserial::pack_bits<Bits>(src, packed_bulk);
REQUIRE(packed_bulk == packed_ref);
unpack_reference(packed_ref, unpacked_ref);
bitserial::unpack_bits<Bits>(packed_ref, unpacked_bulk);
REQUIRE(unpacked_bulk == unpacked_ref);
}
static void pack_reference(
std::span<const std::uint8_t> in,
std::span<std::uint8_t> out
) noexcept {
std::size_t bit_pos = 0;
for (const std::uint8_t x : in) {
bitserial::write_bits(out, bit_pos, Bits, x & max_value);
bit_pos += Bits;
}
}
static void unpack_reference(
std::span<const std::uint8_t> in,
std::span<std::uint8_t> out
) noexcept {
std::size_t bit_pos = 0;
for (std::uint8_t &x : out) {
x = static_cast<std::uint8_t>(bitserial::read_bits(in, bit_pos, Bits));
bit_pos += Bits;
}
}
};
template <std::size_t Bits>
void check_edges() {
for (std::size_t n = 0; n <= 257; ++n) {
BaqFixture<Bits> f(n);
CAPTURE(Bits, n);
CHECK(f.packed_bulk == f.packed_ref);
CHECK(f.unpacked_bulk == f.unpacked_ref);
}
}
template <std::size_t Bits>
void run_benchmarks(char const *label) {
BaqFixture<Bits> f;
CAPTURE(label, Bits, f.src.size(), f.packed_ref.size());
BENCHMARK(std::string(label) + " pack read/write_bits loop") {
BaqFixture<Bits>::pack_reference(f.src, f.packed_ref);
return f.packed_ref.front();
};
BENCHMARK(std::string(label) + " pack bitserial bulk") {
bitserial::pack_bits<Bits>(f.src, f.packed_bulk);
return f.packed_bulk.front();
};
BENCHMARK(std::string(label) + " unpack read_bits loop") {
BaqFixture<Bits>::unpack_reference(f.packed_ref, f.unpacked_ref);
return f.unpacked_ref.front();
};
BENCHMARK(std::string(label) + " unpack bitserial bulk") {
bitserial::unpack_bits<Bits>(f.packed_ref, f.unpacked_bulk);
return f.unpacked_bulk.front();
};
}
} // namespace
TEST_CASE("BAQ bulk pack/unpack edge cases", "[baq][bulk]") {
check_edges<3>();
check_edges<4>();
check_edges<5>();
}
TEST_CASE("BAQ bulk benchmarks", "[!benchmark][baq][bulk]") {
run_benchmarks<3>("3:8");
run_benchmarks<4>("4:8");
run_benchmarks<5>("5:8");
}