1 #include <bitfield/bitfield.h>
6 #define NIBBLE_SIZE (CHAR_BIT / 2)
8 #define PREPARE_FIRST_COPY() \
10 if (source_length >= (CHAR_BIT - destination_offset_modulo)) { \
11 *destination &= reverse_mask[destination_offset_modulo]; \
12 source_length -= CHAR_BIT - destination_offset_modulo; \
14 *destination &= reverse_mask[destination_offset_modulo] \
15 | reverse_mask_xor[destination_offset_modulo + source_length + 1];\
16 c &= reverse_mask[destination_offset_modulo + source_length ];\
21 * Find the ending bit of a bitfield within the final byte.
23 * Returns: a bit position from 0 to 7.
25 static uint8_t findEndBit(const uint16_t startBit, const uint16_t numBits) {
26 int endBit = (startBit + numBits) % CHAR_BIT;
27 return endBit == 0 ? CHAR_BIT : endBit;
31 // TODO can probably remove this
32 static int byteForBit(const uint16_t startBit) {
33 return startBit / CHAR_BIT;
37 * http://stackoverflow.com/questions/3534535/whats-a-time-efficient-algorithm-to-copy-unaligned-bit-arrays
39 static void bitarray_copy(const uint8_t* source_origin, int source_offset,
40 int source_length, uint8_t* destination_origin, int destination_offset) {
41 static const uint8_t mask[] =
42 { 0x55, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
43 static const uint8_t reverse_mask[] =
44 { 0x55, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
45 static const uint8_t reverse_mask_xor[] =
46 { 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00 };
48 if(source_length < 1) {
52 int source_offset_modulo;
53 int destination_offset_modulo;
55 const uint8_t* source = source_origin + byteForBit(source_offset);
56 uint8_t* destination = destination_origin + byteForBit(destination_offset);
58 source_offset_modulo = source_offset % CHAR_BIT;
59 destination_offset_modulo = destination_offset % CHAR_BIT;
61 if(source_offset_modulo == destination_offset_modulo) {
62 if(source_offset_modulo) {
65 c = reverse_mask_xor[destination_offset_modulo] & *source++;
71 int byte_len = source_length / CHAR_BIT;
72 int source_length_modulo = source_length % CHAR_BIT;
75 memcpy(destination, source, byte_len);
77 destination += byte_len;
79 if(source_length_modulo) {
80 *destination &= reverse_mask_xor[source_length_modulo];
81 *destination |= reverse_mask[source_length_modulo] & *source;
88 * Begin: Line things up on destination.
90 if (source_offset_modulo > destination_offset_modulo) {
91 bit_diff_ls = source_offset_modulo - destination_offset_modulo;
92 bit_diff_rs = CHAR_BIT - bit_diff_ls;
94 c = *source++ << bit_diff_ls;
95 c |= *source >> bit_diff_rs;
96 c &= reverse_mask_xor[destination_offset_modulo];
98 bit_diff_rs = destination_offset_modulo - source_offset_modulo;
99 bit_diff_ls = CHAR_BIT - bit_diff_rs;
101 c = *source >> bit_diff_rs &
102 reverse_mask_xor[destination_offset_modulo];
104 PREPARE_FIRST_COPY();
108 * Middle: copy with only shifting the source.
110 int byte_len = source_length / CHAR_BIT;
112 while(--byte_len >= 0) {
113 c = *source++ << bit_diff_ls;
114 c |= *source >> bit_diff_rs;
119 * End: copy the remaing bits;
121 int source_length_modulo = source_length % CHAR_BIT;
122 if(source_length_modulo) {
123 c = *source++ << bit_diff_ls;
124 c |= *source >> bit_diff_rs;
125 c &= reverse_mask[source_length_modulo];
127 *destination &= reverse_mask_xor[source_length_modulo];
133 uint64_t bitmask(const uint8_t numBits) {
134 return (((uint64_t)0x1) << numBits) - 1;
137 uint64_t getBitField(uint64_t data, const uint16_t startBit,
138 const uint16_t numBits, bool bigEndian) {
140 getBits(startBit, numBits, (const uint8_t*)&data,
141 CHAR_BIT * sizeof(uint64_t),
142 bigEndian ? ENDIANNESS_BIG_ENDIAN : ENDIANNESS_LITTLE_ENDIAN,
148 * TODO it would be nice to have a warning if you call with this a value that
149 * won't fit in the number of bits you've specified it should use.
151 void setBitField(uint64_t* data, uint64_t value, int startBit, int numBits) {
152 int shiftDistance = 64 - startBit - numBits;
153 value <<= shiftDistance;
154 *data &= ~(bitmask(numBits) << shiftDistance);
158 uint8_t nthByte(uint64_t source, int byteNum) {
159 return (source >> (64 - ((byteNum + 1) * CHAR_BIT))) & 0xFF;
162 uint8_t getNibble(const uint8_t nibble_index, const uint8_t data[],
163 const uint8_t length, Endianness endianness) {
164 uint8_t byte_index = nibble_index / 2;
166 if(byte_index < length) {
167 result = data[byte_index];
168 if(nibble_index % 2 == 0) {
169 result >>= NIBBLE_SIZE;
172 result &= bitmask(NIBBLE_SIZE);
176 // TODO getBytes, return status and store in output parameter
177 uint8_t getByte(const uint8_t byte_index, const uint8_t data[],
178 const uint8_t length, Endianness endianness) {
179 if(byte_index < length) {
180 return data[byte_index];
185 void getBits(const uint16_t start_index, const uint16_t field_size,
186 const uint8_t data[], const uint8_t length, Endianness endianness,
188 bitarray_copy(data, start_index, field_size, result, 0);