diff options
author | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2013-12-29 14:18:50 -0500 |
---|---|---|
committer | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2013-12-29 14:18:50 -0500 |
commit | e5a2a6b9550319cf05c3a3ae93080b17d322078e (patch) | |
tree | 067e980d8140983b842dc7a886160ce0e83700cd /src | |
parent | e3bb578a0b11a25b0ae4c805fb7b98b724103b6f (diff) |
Document all bitfield functions.
Diffstat (limited to 'src')
-rw-r--r-- | src/bitfield/8byte.c | 28 | ||||
-rw-r--r-- | src/bitfield/8byte.h | 8 | ||||
-rw-r--r-- | src/bitfield/bitarray.c | 4 |
3 files changed, 23 insertions, 17 deletions
diff --git a/src/bitfield/8byte.c b/src/bitfield/8byte.c index 3b6b5466..845be8c0 100644 --- a/src/bitfield/8byte.c +++ b/src/bitfield/8byte.c @@ -18,26 +18,28 @@ static uint16_t bits_to_bytes(uint32_t bits) { return byte_count; } -uint64_t get_bit_field(uint64_t source, const uint16_t startBit, +uint64_t get_bit_field(uint64_t source, const uint16_t offset, const uint16_t bit_count, bool big_endian) { - uint8_t result[8] = {0}; + int startByte = offset / CHAR_BIT; + int endByte = (offset + bit_count - 1) / CHAR_BIT; + if(!big_endian) { source = __builtin_bswap64(source); } - copyBitsRightAligned((const uint8_t*)&source, sizeof(source), startBit, - bit_count, result, sizeof(result)); - uint64_t int_result = 0; - if(!big_endian) { - // we need to swap the byte order of the array to get it into a - // uint64_t, but it's been right aligned so we have to be more careful - for(int i = 0; i < bits_to_bytes(bit_count); i++) { - int_result |= result[bits_to_bytes(bit_count) - i - 1] << (CHAR_BIT * i); + uint8_t* bytes = (uint8_t*)&source; + uint64_t ret = bytes[startByte]; + if(startByte != endByte) { + // The lowest byte address contains the most significant bit. + int i; + for(i = startByte + 1; i <= endByte; i++) { + ret = ret << 8; + ret = ret | bytes[i]; } - } else { - int_result = *(uint64_t*)result; } - return int_result; + + ret >>= 8 - find_end_bit(offset + bit_count); + return ret & bitmask(bit_count); } bool set_bit_field(uint64_t* destination, uint64_t value, const uint16_t offset, diff --git a/src/bitfield/8byte.h b/src/bitfield/8byte.h index 36b5fe60..1ee9c0ec 100644 --- a/src/bitfield/8byte.h +++ b/src/bitfield/8byte.h @@ -18,7 +18,7 @@ extern "C" { * * source - the bytes in question. * offset - the starting index of the bit field (beginning from 0). - * numBits - the width of the bit field to extract. + * bit_count - the width of the bit field to extract. * big_endian - if the data passed in is little endian, set this to false and it * will be flipped before grabbing the bit field. * @@ -44,7 +44,7 @@ extern "C" { * * uint64_t value = get_bit_field(data, 2, 4); * - * Returns the value of the requested bit field. + * Returns the value of the requested bit field, right aligned in a uint64_t. */ uint64_t get_bit_field(uint64_t source, const uint16_t offset, const uint16_t bit_count, bool big_endian); @@ -71,6 +71,10 @@ bool set_bit_field(uint64_t* destination, uint64_t value, const uint16_t offset, */ uint8_t nth_byte(const uint64_t source, const uint16_t byte_index); +/* Private: Determine the index of the last bit used. + */ +uint8_t find_end_bit(const uint16_t num_bits); + #ifdef __cplusplus } #endif diff --git a/src/bitfield/bitarray.c b/src/bitfield/bitarray.c index 8fbc941d..f8c23527 100644 --- a/src/bitfield/bitarray.c +++ b/src/bitfield/bitarray.c @@ -113,7 +113,7 @@ bool copyBits(const uint8_t* source_origin, const uint16_t source_length, * * Returns: a bit position from 0 to 7. */ -static uint8_t findEndBit(const uint16_t startBit, const uint16_t numBits) { +uint8_t find_end_bit(const uint16_t numBits) { int endBit = numBits % CHAR_BIT; return endBit == 0 ? CHAR_BIT : endBit; } @@ -125,5 +125,5 @@ bool copyBitsRightAligned(const uint8_t source[], const uint16_t source_length, destination_length, // provide a proper destination offset so the result is right // aligned - CHAR_BIT - findEndBit(offset, bit_count)); + CHAR_BIT - find_end_bit(bit_count)); } |