bitserial

Big-endian, zero-padding, zero-allocation bit-field serialization framework for C++20

Features

  • Single-declaration schema: Define struct and serialization schema simultaneously (X-Macro style)
  • Compile-time expansion: No runtime reflection overhead
  • Big-endian MSB-first: No padding between fields, bit-accurate packing
  • Bulk BAQ paths: pack_bits<Bits> / unpack_bits<Bits> for fixed-width byte arrays
    • Optimized AVX512BW and AVX2 unpack backends for 3/4/5-bit arrays when available
  • Rich type support: bool, signed/unsigned integers, float32, float64
    • Automatic sign-extension for signed integers when reading
  • Composable: Nested structs, fixed-length arrays
  • Multiple IO formats: Bytes, span, hex
  • Zero-allocation: Writes directly to user-provided std::span

Requirements

  • C++20 compiler
    • GCC 10+, Clang 11+, MSVC 19.29+ (with /Zc:preprocessor)

Quick Start

#include <bitserial/bitserial.hpp>

// 1. Define packet schema
#define MY_PACKET_FIELDS(BSF)                    \
  BSF##_F  (std::uint8_t,  version,  4)          \
  BSF##_F  (std::uint8_t,  flags,    4)          \
  BSF##_F  (std::int16_t,  temp)                 \
  BSF##_F  (bool,          ok)                   \
  BSF##_F  (float,         voltage)              \
  BSF##_ARR(std::uint8_t,  mac,      6)

BITSERIAL_DEFINE(MyPacket, MY_PACKET_FIELDS);

// 2. Serialize
MyPacket pkt{
  .version = 0xA,
  .flags = 0x5,
  .temp = -42,
  .ok = true,
  .voltage = 3.7f,
  .mac = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
};

auto bytes = bitserial::to_bytes(pkt);
auto hex = bitserial::to_hex(pkt);  // "A5..."

// 3. Deserialize
auto pkt2 = bitserial::from_bytes<MyPacket>(bytes);
auto pkt3 = bitserial::from_hex<MyPacket>(hex);

The legacy include #include "bitserial.hpp" is kept as a forwarding header.

Field Types

Macro Description Example
BSF_F(T, name) Scalar with natural bit width BSF_F(std::uint16_t, x) → 16 bits
BSF_F(T, name, bits) Scalar with custom bit width BSF_F(std::uint8_t, x, 4) → 4 bits
BSF_NEST(T, name) Nested struct BSF_NEST(Header, hdr)
BSF_ARR(T, name, len) Fixed array with natural element width BSF_ARR(std::uint8_t, mac, 6)
BSF_ARR(T, name, len, ebits) Fixed array with custom element bits BSF_ARR(std::int16_t, d, 4, 10)

Compile-time Size Computation

static_assert(bitserial::bit_size_v<MyPacket> == 124);
static_assert(bitserial::byte_size_v<MyPacket> == 16);  // (124 + 7) / 8

User-provided Buffer (Zero-allocation)

std::vector<std::uint8_t> buf(bitserial::byte_size_v<MyPacket>);
bitserial::to_bytes(pkt, std::span<std::uint8_t>(buf));

Bulk BAQ API

For continuous std::uint8_t arrays with fixed element widths, use the bulk API directly, or let BSF_ARR(std::uint8_t, data, N, Bits) call it internally.

std::vector<std::uint8_t> values = {/* 0..7 for 3-bit */};
std::vector<std::uint8_t> packed((values.size() * 3 + 7) / 8);
std::vector<std::uint8_t> decoded(values.size());

bitserial::pack_bits<3>(values, packed);
bitserial::unpack_bits<3>(packed, decoded);

Optimized paths currently target Bits == 3, 4, and 5, with AVX512BW preferred on capable x86 CPUs, AVX2 used next, and scalar fallback for all 1..8 bit widths and non-byte-aligned offsets.

Project Layout

include/bitserial/bitserial.hpp        Public schema and serialization API
include/bitserial/bit_io.hpp           Low-level MSB-first bit IO
include/bitserial/bulk.hpp             Bulk pack/unpack backends
include/bitserial/detail/config.hpp    Compiler and ISA feature detection
tests/bitserial_tests.cpp              Catch2 correctness tests
benchmarks/baq_bulk_benchmarks.cpp     Catch2 BAQ bulk benchmarks

Build, Test, Benchmark

cmake -S . -B build -DBITSERIAL_BUILD_TESTS=ON -DBITSERIAL_BUILD_BENCHMARKS=ON
cmake --build build
ctest --test-dir build --output-on-failure
cmake --build build --target run_bitserial_benchmarks

Single-config generators such as Ninja and MinGW Makefiles default to Release. Multi-config generators such as Visual Studio and Ninja Multi-Config need --config Release for build commands and -C Release for CTest.

Both tests and benchmarks are organized through Catch2. The benchmark executable uses the [!benchmark] tag, so benchmarks do not run during normal CTest runs.

Error Handling

// Throws std::out_of_range if buffer is too small
// Throws std::invalid_argument for invalid hex characters

Real-world Example: IPv4 Header

#define IPV4_FIELDS(BSF)                                       \
  BSF##_F(std::uint8_t,  version,    4)                       \
  BSF##_F(std::uint8_t,  ihl,        4)                       \
  BSF##_F(std::uint8_t,  tos)                                 \
  BSF##_F(std::uint16_t, total_len)                           \
  BSF##_F(std::uint16_t, id)                                  \
  BSF##_F(std::uint8_t,  flags,      3)                       \
  BSF##_F(std::uint16_t, frag_off,   13)                      \
  BSF##_F(std::uint8_t,  ttl)                                 \
  BSF##_F(std::uint8_t,  proto)                               \
  BSF##_F(std::uint16_t, checksum)

BITSERIAL_DEFINE(IPv4Head, IPV4_FIELDS);

static_assert(bitserial::byte_size_v<IPv4Head> == 12);

Split Definition (Add Methods)

#define SENSOR_FIELDS(BSF)  \
  BSF##_F(std::uint16_t, id) \
  BSF##_F(std::int32_t, value)

BITSERIAL_BEGIN(SensorData, SENSOR_FIELDS)
  bool is_valid() const { return value != 0; }
BITSERIAL_END(SensorData, SENSOR_FIELDS);

License

MIT

S
Description
No description provided
Readme
423 KiB
Languages
C++ 91.6%
CMake 3.4%
Makefile 2.7%
Lua 2.3%