Files
bitserial/README.cn.md
T
DawnMagnet 8bd5e0c2b3 Add .clang-format configuration and Makefile for build management
- Introduced a .clang-format file to standardize C++ code formatting across the project.
- Created a Makefile to facilitate building, testing, and benchmarking with various configurations.
- Added targets for configuring and building with different compilers (cl, g++, clang++).
- Included formatting targets to check and apply clang-format to specified source files.
2026-05-05 13:10:28 +08:00

5.8 KiB
Raw Blame History

bitserial

适用于 C++20 的大端、无填充、零分配位域序列化框架

特性

  • 单次声明生成 Schema:同时定义结构体与序列化模式(X-Macro 风格)
  • 全编译期展开:无运行时反射开销
  • 大端 MSB-first 位流:字段间无任何填充,逐位精确打包
  • BAQ 批量路径:提供 pack_bits<Bits> / unpack_bits<Bits> 固定位宽字节数组接口
    • 可用时对 3/4/5-bit 数组读取启用 AVX512BW 和 AVX2 intrinsic 后端
  • 丰富类型支持:bool、有符号/无符号整数、float32、float64
    • 读取时自动对有符号整数进行符号位扩展
  • 可组合:嵌套结构体、定长数组
  • 多种 IO 格式:字节数组、span、十六进制字符串
  • 零分配:直接写入用户提供的 std::span

编译要求

  • C++20 编译器
    • GCC 10+、Clang 11+、MSVC 19.29+(需加 /Zc:preprocessor)

快速开始

#include <bitserial/bitserial.hpp>

// 1. 定义数据包 Schema
#define MY_PACKET_FIELDS(BSF)                    \
  BSF##_F  (std::uint8_t,  版本,    4)           \
  BSF##_F  (std::uint8_t,  标志位,  4)           \
  BSF##_F  (std::int16_t,  温度)                 \
  BSF##_F  (bool,          正常)                 \
  BSF##_F  (float,         电压)                 \
  BSF##_ARR(std::uint8_t,  MAC地址,  6)

BITSERIAL_DEFINE(我的数据包, MY_PACKET_FIELDS);

// 2. 序列化
我的数据包 pkt{
  .版本 = 0xA,
  .标志位 = 0x5,
  .温度 = -42,
  .正常 = true,
  .电压 = 3.7f,
  .MAC地址 = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
};

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

// 3. 反序列化
auto pkt2 = bitserial::from_bytes<我的数据包>(bytes);
auto pkt3 = bitserial::from_hex<我的数据包>(hex);

兼容头 #include "bitserial.hpp" 仍然保留,会转发到新的公开头。

字段类型

宏 说明 示例
BSF_F(T, 名称) 使用自然位宽的标量字段 BSF_F(std::uint16_t, x) → 16 位
BSF_F(T, 名称, 位宽) 使用自定义位宽的标量字段 BSF_F(std::uint8_t, x, 4) → 4 位
BSF_NEST(T, 名称) 嵌套结构体 BSF_NEST(头部, hdr)
BSF_ARR(T, 名称, 长度) 自然位宽元素的定长数组 BSF_ARR(std::uint8_t, mac, 6)
BSF_ARR(T, 名称, 长度, 元素位宽) 自定义位宽元素的定长数组 BSF_ARR(std::int16_t, d, 4, 10)

编译期尺寸计算

static_assert(bitserial::bit_size_v<我的数据包> == 124);
static_assert(bitserial::byte_size_v<我的数据包> == 16);  // (124 + 7) / 8

用户提供缓冲区(零分配)

std::vector<std::uint8_t> buf(bitserial::byte_size_v<我的数据包>);
bitserial::to_bytes(pkt, std::span<std::uint8_t>(buf));

BAQ 批量 API

连续的 std::uint8_t 固定位宽数组可以直接使用批量 API;在 schema 中使用 BSF_ARR(std::uint8_t, data, N, Bits) 时也会自动走这条内部路径。

std::vector<std::uint8_t> values = {/* 3-bit 时取值 0..7 */};
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);

当前重点优化 Bits == 3、4、5;支持的 x86 CPU 会优先使用 AVX512BW, 其次使用 AVX2;所有 1..8 位宽以及非字节对齐偏移都有标量兜底。

工程结构

include/bitserial/bitserial.hpp        公开 schema 与序列化 API
include/bitserial/bit_io.hpp           底层 MSB-first 位流读写
include/bitserial/bulk.hpp             批量 pack/unpack 后端
include/bitserial/detail/config.hpp    编译器与 ISA 特性检测
tests/bitserial_tests.cpp              Catch2 正确性测试
benchmarks/baq_bulk_benchmarks.cpp     Catch2 BAQ 批量 benchmark

构建、测试、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

普通 Ninja / MinGW Makefiles 这类单配置生成器默认使用 Release;Visual Studio / Ninja Multi-Config 这类多配置生成器需要在 build 和 ctest 时额外传 --config Release 或 -C Release。

测试和 benchmark 都由 Catch2 组织。benchmark 使用 [!benchmark] 标签,因此普通 CTest 不会误跑性能测试。

错误处理

// 缓冲区过小抛出 std::out_of_range
// 非法十六进制字符抛出 std::invalid_argument

真实协议示例:IPv4 头部

#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);

拆分定义(追加成员方法)

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

BITSERIAL_BEGIN(传感器数据, SENSOR_FIELDS)
  bool 是否有效() const { return value != 0; }
BITSERIAL_END(传感器数据, SENSOR_FIELDS);

许可证

MIT