diff options
Diffstat (limited to 'src/bitfield')
-rw-r--r-- | src/bitfield/8byte.c | 4 | ||||
-rw-r--r-- | src/bitfield/8byte.h | 4 | ||||
-rw-r--r-- | src/bitfield/bitfield.c | 24 | ||||
-rw-r--r-- | src/bitfield/bitfield.h | 23 |
4 files changed, 47 insertions, 8 deletions
diff --git a/src/bitfield/8byte.c b/src/bitfield/8byte.c index 72f7e32b..0ae6894b 100644 --- a/src/bitfield/8byte.c +++ b/src/bitfield/8byte.c @@ -47,8 +47,8 @@ uint64_t eightbyte_get_bitfield(uint64_t source, const uint16_t offset, return ret & bitmask(bit_count); } -bool eightbyte_set_bitfield(uint64_t* destination, uint64_t value, const uint16_t offset, - const uint16_t bit_count) { +bool eightbyte_set_bitfield(uint64_t value, const uint16_t offset, + const uint16_t bit_count, uint64_t* destination) { if(value > bitmask(bit_count)) { return false; } diff --git a/src/bitfield/8byte.h b/src/bitfield/8byte.h index 2916c218..04512690 100644 --- a/src/bitfield/8byte.h +++ b/src/bitfield/8byte.h @@ -74,8 +74,8 @@ uint8_t eightbyte_get_byte(const uint64_t source, const uint8_t byte_index, * Returns true if the bit_count is enough to fully represent the value, and * false if it will not fit. */ -bool eightbyte_set_bitfield(uint64_t* destination, uint64_t value, - const uint16_t offset, const uint16_t bit_count); +bool eightbyte_set_bitfield(uint64_t value, + const uint16_t offset, const uint16_t bit_count, uint64_t* destination); /* Private: Determine the index of the last bit used. */ diff --git a/src/bitfield/bitfield.c b/src/bitfield/bitfield.c index e40ab1ab..b244c9b1 100644 --- a/src/bitfield/bitfield.c +++ b/src/bitfield/bitfield.c @@ -34,10 +34,7 @@ uint64_t get_bitfield(const uint8_t source[], const uint8_t source_length, return 0; } - union { - uint64_t whole; - uint8_t bytes[sizeof(uint64_t)]; - } combined; + ArrayOrBytes combined; memset(combined.bytes, 0, sizeof(combined.bytes)); copy_bits_right_aligned(source, source_length, offset, bit_count, combined.bytes, sizeof(combined.bytes)); @@ -53,3 +50,22 @@ bool set_nibble(const uint16_t nibble_index, const uint8_t value, destination_length, nibble_index * NIBBLE_SIZE); } +bool set_bitfield(const uint64_t value, const uint16_t offset, + const uint16_t bit_count, uint8_t destination[], + uint16_t destination_length) { + if(value > bitmask(bit_count)) { + return false; + } + + ArrayOrBytes combined = { + whole: value + }; + + if(BYTE_ORDER == LITTLE_ENDIAN) { + combined.whole = __builtin_bswap64(combined.whole); + } + + return copy_bits(combined.bytes, sizeof(combined.bytes), + sizeof(combined.bytes) * CHAR_BIT - bit_count, bit_count, + destination, destination_length, offset); +} diff --git a/src/bitfield/bitfield.h b/src/bitfield/bitfield.h index a080c866..b3c30f02 100644 --- a/src/bitfield/bitfield.h +++ b/src/bitfield/bitfield.h @@ -168,6 +168,22 @@ bool copy_bytes_right_aligned(const uint8_t source[], const uint16_t source_leng bool set_nibble(const uint16_t nibble_index, const uint8_t value, uint8_t* destination, const uint16_t destination_length); +/* Public: Set the bit field in the given data array to the new value. + * + * value - the value to set in the bit field. + * offset - the starting index of the bit field (beginning from 0). + * bit_count - the number of bits to set in the data. + * destination - the destination array. + * destination_length - the total length of the destination array in bytes, + * for range checking. + * + * Returns true if the bit_count is enough to fully represent the value, and + * false if it will not fit. + */ +bool set_bitfield(const uint64_t value, const uint16_t offset, + const uint16_t bit_count, uint8_t destination[], + uint16_t destination_length); + /* Private: */ uint16_t bits_to_bytes(uint32_t bits); @@ -178,6 +194,13 @@ uint16_t bits_to_bytes(uint32_t bits); */ uint64_t bitmask(const uint8_t bit_count); +/* Private: A union to assist swapping between uint64_t and a uint8_t array. + */ +typedef union { + uint64_t whole; + uint8_t bytes[sizeof(uint64_t)]; +} ArrayOrBytes; + #ifdef __cplusplus } #endif |