Add bitpacks.hh, a proxy-based wrapper for packed bit-fields #558
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduce a
Bitpackclass, representing a packed bit-field within an unsigned integral type.Bitpacks are intended for use instead of C++ bit-fields or manual bit twiddling and should...not have any undefined or implementation-defined behavior or layout (besides endianness);
Fielddefinitions are interpreted as one might expect using<<,|,&, and~(seeBitpack<>::Field<>::raw_viewandraw_withfor the core of all this C++ syntax);readily serve as superclasses for classes fronting particular hardware control registers or other bit-packed types;
work in
constexprcontext and involatile-qualified forms;be reasonably ergonomic to use in as many contexts as possible.
The fundamental perspective is one of
Field::Viewproxies to contiguous spans of bits within aBitpack<T>'s underlying storageT. Said spans are defined usingFieldInfostructures. Field proxies to fields of typeFand withFieldInfo Imay be explicitly constructed with.view<F, I>(), but there is also some convenience machinery for definingBitpacks as products of differently-typed fields, such that a proxy to a given field with typeFcan be accessed uniformly as.view<F>()(where, effectively, theFieldInfoIis computed fromF).There are also some gross C-preprocessor macros that aim to de-clutter use of
Bitpacks, especially in combination withenum classes.The raw implementation is in
sdk/include/bitpacks.hhand a small smattering of tests (mostly checking operation in combination withconstexprandvolatile) and demonstration of use is available intests/bitpacks-test.cc. It is probably sensible to review the two in parallel, as the implementation is probably confusing without examples of use, and the use is probably confusing without some understanding of the implementation.This is still a little bit of an experiment and a request for comments / opinions, so please, fire away!