|
BRL-CAD
|
Routines for managing efficient high-performance bit vectors of arbitrary length. More...
Files | |
| file | bitv.h |
Data Structures | |
| struct | bu_bitv |
Typedefs | |
| typedef uint64_t | bitv_t |
| typedef struct bu_bitv | bu_bitv_t |
Routines for managing efficient high-performance bit vectors of arbitrary length.
The basic type "bitv_t" is defined in include/bu.h; it is the widest integer datatype for which efficient hardware support exists. BU_BITV_SHIFT and BU_BITV_MASK are also defined in bu.h
These bit vectors are "little endian", bit 0 is in the right hand side of the [0] word.
| #define BU_BITV_SHIFT 6 |
Bit vector shift size
Should equal to: log2(sizeof(bitv_t)*8.0). Using bu_bitv_shift() will return a run-time computed shift size if the size of a bitv_t changes. Performance impact is rather minimal for most models but disabled for a handful of primitives that heavily rely on bit vectors.
(8-bit type: 3, 16-bit type: 4, 32-bit type: 5, 64-bit type: 6)
| #define BU_BITV_MASK ((1<<BU_BITV_SHIFT)-1) |
| #define BU_CK_BITV | ( | _bp | ) | BU_CKMAG(_bp, BU_BITV_MAGIC, "bu_bitv") |
Initializes a bu_bitv struct without allocating any memory. This macro is not suitable for initializing a head list node.
Because struct bu_bitv embeds one full bitv_t word (currently 64 bits) inline, a stack-allocated instance already provides 64 usable bits without any heap allocation. Callers that need at most 64 bits can avoid bu_bitv_new() entirely:
nbits is set to sizeof(bitv_t)*8 (64) to reflect the capacity actually available in the embedded bits[1] field.
| #define BU_BITV_INIT_ZERO { {BU_BITV_MAGIC, BU_LIST_NULL, BU_LIST_NULL}, sizeof(bitv_t)*8, {0} } |
Macro suitable for declaration-statement initialization of a bu_bitv struct on the stack or as a static/global. Does not allocate memory and is not suitable for a head node.
The initialized vector has 64 bits of usable capacity matching the embedded bits[1] storage. Example:
| #define BU_BITV_IS_INITIALIZED | ( | _bp | ) | (((struct bu_bitv *)(_bp) != BU_BITV_NULL) && LIKELY((_bp)->l.magic == BU_BITV_MAGIC)) |
| #define BU_BITS2WORDS | ( | _nb | ) | (((size_t)(_nb>0?_nb:0)+BU_BITV_MASK)>>BU_BITV_SHIFT) |
| #define BU_BITSET | ( | _bv, | |
| bit | |||
| ) | ((_bv)->bits[(bit)>>BU_BITV_SHIFT] |= (((bitv_t)1)<<((bit)&BU_BITV_MASK))) |
| #define BU_BITCLR | ( | _bv, | |
| bit | |||
| ) | ((_bv)->bits[(bit)>>BU_BITV_SHIFT] &= ~(((bitv_t)1)<<((bit)&BU_BITV_MASK))) |
| #define BU_BITTEST | ( | _bv, | |
| bit | |||
| ) | (((_bv)->bits[(bit)>>BU_BITV_SHIFT] & (((bitv_t)1)<<((bit)&BU_BITV_MASK)))!=0) |
| #define BU_BITV_ZEROALL | ( | _bv | ) | bu_bitv_clear(_bv) |
DEPRECATED: Macros to efficiently find all the ONE bits in a bit vector. Counts words down, counts bits in words going up, for speed & portability. It does not matter if the shift causes the sign bit to smear to the right.
Example:
| #define BU_BITV_LOOP_END |
DEPRECATED: Paired with BU_BITV_LOOP_START()
bitv_t should be a fast integer type for implementing bit vectors.
On many machines, this is a 32-bit "long", but on some machines a compiler/vendor-specific type such as "long long" or even 'char' can give access to faster integers.
THE SIZE OF bitv_t MUST MATCH BU_BITV_SHIFT.
returns floor(log2(sizeof(bitv_t)*8.0)), i.e. the number of bits required with base-2 encoding to index any bit in an array of length sizeof(bitv_t)*8.0 bits long. users should not call this directly, instead calling the BU_BITV_SHIFT macro instead.
|
extern |
Iterate over all set bits in the bit vector efficiently.
This function quickly skips over large blocks of zeroes using machine-word level operations, making it significantly faster than a bit-by-bit test loop when the bit vector is sparse. For each bit that is set to 1, the provided callback function is invoked with the index of the set bit and the user-provided data pointer.
| bv | The bit vector to iterate over. |
| callback | The function to call for each set bit. |
| data | User-provided context passed directly to the callback. |
Allocate storage for a new bit vector of at least 'nbits' in length. The bit vector itself is guaranteed to be initialized to all zero.
Because struct bu_bitv embeds one full machine word (bitv_t, currently 64 bits) inline, requests for fewer than 64 bits are silently rounded up to 64 and require no extra heap allocation beyond the struct itself. Requesting exactly 0 bits is valid and results in a usable 64-bit vector.
Release all internal storage for this bit vector.
It is the caller's responsibility to not use the pointer 'bv' any longer. It is the caller's responsibility to dequeue from any linked list first.
Set all the bits in the bit vector to zero.
Also available as a BU_BITV_ZEROALL macro if you don't desire the pointer checking.
Performs an in-place bitwise OR operation on a bit vector.
Result is stored in 'ov' (ov = ov | iv). If the vectors are of differing lengths, the operation will safely process up to the bounds of the overlapping arrays. Any excess bits in 'ov' that do not exist in 'iv' are preserved as-is.
| ov | Destination and first operand bit vector. |
| iv | Source bit vector operand. |
Performs an in-place bitwise AND operation on a bit vector.
Result is stored in 'ov' (ov = ov & iv). If the vectors are of differing lengths, the operation will safely process up to the bounds of the overlapping arrays. For an AND operation, any excess bits in 'ov' that do not exist in 'iv' are cleared to 0.
| ov | Destination and first operand bit vector. |
| iv | Source bit vector operand. |
Performs an in-place bitwise NOT operation on a bit vector.
Flips all bits in the vector (ov = ~ov). Safely preserves the unused trailing padding bits in the final machine word.
| ov | The bit vector to invert. |
Performs an in-place bitwise XOR (exclusive OR) operation on a bit vector.
Result is stored in 'ov' (ov = ov ^ iv). If the vectors are of differing lengths, the operation processes up to the bounds of the overlapping arrays. Excess bits in 'ov' remain unchanged.
| ov | Destination and first operand bit vector. |
| iv | Source bit vector operand. |
Shifts entire bit vector left or right across word boundaries.
A positive shift value shifts the bits left (towards higher indices), effectively moving bit N to N+shift. A negative shift value shifts the bits right (towards lower indices), moving bit N to N-|shift|. Bits shifted out of bounds are discarded. Vacated bits are zeroed.
| ov | The bit vector to shift. |
| shift | The number of bits to shift. |
Print the bits set in a bit vector.
Convert a bit vector to an ascii string of hex digits. The string is from MSB to LSB (bytes and bits).
Convert a string of HEX digits (as produced by bu_bitv_to_hex) into a bit vector.
Convert a bit vector to an ascii string of binary digits in the GCC format ("0bn..."). The string is from MSB to LSB (bytes and bits).
Convert a string of BINARY digits (as produced by bu_bitv_to_binary) into a bit vector.
Convert a string of BINARY digits (as produced by bu_bitv_to_binary) into a bit vector. The "nbytes" argument may be zero if the user has no minimum length preference.
Compare two bit vectors for equality. They are considered equal iff their lengths and each bit are equal. Returns 1 for true, zero for false.
Compare two bit vectors for equality. They are considered equal iff their non-zero bits are equal (leading zero bits are ignored so lengths are not considered explicitly). Returns 1 for true, 0 for false.
Convert a string of hex characters to an equivalent string of binary characters.
The input hex string may have an optional prefix of '0x' or '0X' in which case the resulting binary string will be prefixed with '0b'.
The input string is expected to represent an integral number of bytes but will have leading zeroes prepended as necessary to fulfill that requirement.
Returns BRLCAD_OK for success, BRLCAD_ERROR for errors.
Convert a string of binary characters to an equivalent string of hex characters.
The input binary string may have an optional prefix of '0b' or '0B' in which case the resulting hex string will be prefixed with '0x'.
The input string is expected to represent an integral number of bytes but will have leading zeroes prepended as necessary to fulfill that requirement.
Returns BRLCAD_OK for success, BRLCAD_ERROR for errors.
Bit field printing implementation.
Print a bit field according to a format specification via bu_log().
Line printed is of the form "String Label: x1234 <FOO,BAR,RAB,OOF>" and is commonly used by debugging code to print which debug bits are enabled.
| label | string label |
| bits | integer with the bits to print |
| format | format specification |
The 'format' begins with a desired printing base (8 or 16), i.e., \010 means print octal and \020 for hex. Remaining string is the little endian bit position (i.e., 1 to 32 encoded in octal format) followed by a label for that bit (e.g., "\010\2Bit_one\1BIT_zero")
Note octal counting is used for the bit position label: \01 -> ... \07 -> \10 -> \11 -> ... \17 -> \20 ... etc