Skip to content

Add packed bit array support to GenericArrayView#115

Draft
Copilot wants to merge 7 commits intomasterfrom
copilot/extend-emboss-bitpacking-support
Draft

Add packed bit array support to GenericArrayView#115
Copilot wants to merge 7 commits intomasterfrom
copilot/extend-emboss-bitpacking-support

Conversation

Copy link

Copilot AI commented Dec 17, 2025

Extends byte arrays (UInt:8[]) to support packing/unpacking bit-packed data (1-64 bits per element) to/from standard C++ integer widths (8/16/32/64 bits). Enables efficient handling of devices that stream packed formats (e.g., cameras with 12-bit pixels).

API

Three new template methods on GenericArrayView (SFINAE-enabled for byte arrays only):

// Calculate output buffer size
std::size_t size = array.UnpackedSizeInBytes<16>();

// Unpack 12-bit packed data to 16-bit elements
std::uint16_t output[4];
bool ok = array.UnpackTo<16, 12>(
    reinterpret_cast<uint8_t*>(output), sizeof(output));

// Pack 16-bit elements to 12-bit packed format
std::uint16_t input[3] = {0x234, 0x678, 0xABC};
bool ok = array.PackFrom<16, 12>(
    reinterpret_cast<const uint8_t*>(input), 3);

Implementation

  • Naive bit-by-bit extraction/insertion in little-endian bit order
  • Full buffer bounds validation
  • Static assertions enforce valid bit widths (TargetBits ∈ {8,16,32,64}, SourceBits ∈ [1,64])
  • Safe from undefined behavior (64-bit shift edge cases handled)

Tests

Added 8 test cases covering:

  • 12-bit ↔ 16-bit (primary use case)
  • 8-bit, 10-bit variants
  • Round-trip validation
  • Buffer overflow protection
  • Size calculation

Example Use Case

// Camera streams 12-bit packed pixels
uint8_t camera_data[6];  // 4 pixels * 12 bits = 48 bits
auto packed = ArrayView<...>{ReadWriteContiguousBuffer{camera_data, 6}};

// Unpack to native 16-bit for processing
uint16_t pixels[4];
packed.UnpackTo<16, 12>(reinterpret_cast<uint8_t*>(pixels), sizeof(pixels));

Optimization with CPU intrinsics deferred to future work.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.googleapis.com
    • Triggering command: /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 version (dns block)
    • Triggering command: /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 /usr/local/lib/node_modules/@bazel/bazelisk/bazelisk-linux_amd64 test //runtime/cpp/test:emboss_array_view_test --test_output=errors (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

extend emboss byte arrays e.g. UInt:8[] to support packed bit arrays up to 64 bits for a single element. The array view class in this case should have an UnpackTo() method that can be used to unpack to an external buffer and a PackFrom() method that can be used to pack elements from an external buffer. these are both templated so a user can choose the bit size they want to pack from or to. there is also an UnpackedSizeInBytes() method that helps a user to determine the size of the output buffer. there could be better names for these. I'm sure that are more consistent with the way emboss does things. if you have a strong feeling about that, please choose different names.

internally the bitpacking and unpacking uses highly efficient bitpacking and unpacking methods that take advantage e.g. cpu intrinsics. we will optimize those at a later time so it is fine to use a naive implementation.

One of the use cases would be for example a lot of cameras have a streaming mode where they stream 12-bits per pixel packed, and a user might want to unpack them to a 16-bit container that is supported natively by C++.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 6 commits December 17, 2025 02:09
…eInBytes methods

Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
…ected values

Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
…ounds check

Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
…ndition check

Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
Copilot AI changed the title [WIP] Extend emboss byte arrays to support packed bit arrays Add packed bit array support to GenericArrayView Dec 17, 2025
Copilot AI requested a review from AaronWebster December 17, 2025 02:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants