diff options
Diffstat (limited to 'libs/bitfield-c/src/bitfield')
-rw-r--r-- | libs/bitfield-c/src/bitfield/8byte.c | 18 | ||||
-rw-r--r-- | libs/bitfield-c/src/bitfield/8byte.h | 14 | ||||
-rw-r--r-- | libs/bitfield-c/src/bitfield/bitarray.c | 30 | ||||
-rw-r--r-- | libs/bitfield-c/src/bitfield/bitfield.c | 28 | ||||
-rw-r--r-- | libs/bitfield-c/src/bitfield/bitfield.h | 58 |
5 files changed, 74 insertions, 74 deletions
diff --git a/libs/bitfield-c/src/bitfield/8byte.c b/libs/bitfield-c/src/bitfield/8byte.c index 9325ed1b..be46e825 100644 --- a/libs/bitfield-c/src/bitfield/8byte.c +++ b/libs/bitfield-c/src/bitfield/8byte.c @@ -6,13 +6,13 @@ #define EIGHTBYTE_BIT (8 * sizeof(uint64_t)) -uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index, +unsigned int eightbyte_get_nibble(const uint64_t source, const unsigned int nibble_index, const bool data_is_big_endian) { - return (uint8_t) eightbyte_get_bitfield(source, NIBBLE_SIZE * nibble_index, + return (unsigned int) eightbyte_get_bitfield(source, NIBBLE_SIZE * nibble_index, NIBBLE_SIZE, data_is_big_endian); } -uint8_t eightbyte_get_byte(uint64_t source, const uint8_t byte_index, +unsigned int eightbyte_get_byte(uint64_t source, const unsigned int byte_index, const bool data_is_big_endian) { if(data_is_big_endian) { source = __builtin_bswap64(source); @@ -23,8 +23,8 @@ uint8_t eightbyte_get_byte(uint64_t source, const uint8_t byte_index, // TODO is this funciton necessary anymore? is it any faster for uint64_t than // get_bitfield(data[], ...)? is the performance better on a 32 bit platform // like the PIC32? -uint64_t eightbyte_get_bitfield(uint64_t source, const uint16_t offset, - const uint16_t bit_count, const bool data_is_big_endian) { +uint64_t eightbyte_get_bitfield(uint64_t source, const unsigned int offset, + const unsigned int bit_count, const bool data_is_big_endian) { int startByte = offset / CHAR_BIT; int endByte = (offset + bit_count - 1) / CHAR_BIT; @@ -32,11 +32,11 @@ uint64_t eightbyte_get_bitfield(uint64_t source, const uint16_t offset, source = __builtin_bswap64(source); } - uint8_t* bytes = (uint8_t*)&source; + unsigned int* bytes = (unsigned int*)&source; uint64_t ret = bytes[startByte]; if(startByte != endByte) { // The lowest byte address contains the most significant bit. - uint8_t i; + unsigned int i; for(i = startByte + 1; i <= endByte; i++) { ret = ret << 8; ret = ret | bytes[i]; @@ -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 value, const uint16_t offset, - const uint16_t bit_count, uint64_t* destination) { +bool eightbyte_set_bitfield(uint64_t value, const unsigned int offset, + const unsigned int bit_count, uint64_t* destination) { if(value > bitmask(bit_count)) { return false; } diff --git a/libs/bitfield-c/src/bitfield/8byte.h b/libs/bitfield-c/src/bitfield/8byte.h index 04512690..97c200bb 100644 --- a/libs/bitfield-c/src/bitfield/8byte.h +++ b/libs/bitfield-c/src/bitfield/8byte.h @@ -36,8 +36,8 @@ extern "C" { * * Returns the value of the requested bit field, right aligned in a uint64_t. */ -uint64_t eightbyte_get_bitfield(uint64_t source, const uint16_t offset, - const uint16_t bit_count, const bool data_is_big_endian); +uint64_t eightbyte_get_bitfield(uint64_t source, const unsigned int offset, + const unsigned int bit_count, const bool data_is_big_endian); /* Public: Return a single nibble from the payload, with range checking. * @@ -47,9 +47,9 @@ uint64_t eightbyte_get_bitfield(uint64_t source, const uint16_t offset, * data_is_big_endian - if the data passed in is little endian, set this to false and it * will be flipped before grabbing the bit field. * - * Returns the retreived nibble, right aligned in a uint8_t. + * Returns the retreived nibble, right aligned in a unsigned int. */ -uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index, +unsigned int eightbyte_get_nibble(const uint64_t source, const unsigned int nibble_index, const bool data_is_big_endian); /* Public: Return a single byte from the payload, with range checking. @@ -61,7 +61,7 @@ uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index, * * Returns the retreived byte. */ -uint8_t eightbyte_get_byte(const uint64_t source, const uint8_t byte_index, +unsigned int eightbyte_get_byte(const uint64_t source, const unsigned int byte_index, const bool data_is_big_endian); /* Public: Set the bit field in the given data array to the new value. @@ -75,11 +75,11 @@ uint8_t eightbyte_get_byte(const uint64_t source, const uint8_t byte_index, * false if it will not fit. */ bool eightbyte_set_bitfield(uint64_t value, - const uint16_t offset, const uint16_t bit_count, uint64_t* destination); + const unsigned int offset, const unsigned int bit_count, uint64_t* destination); /* Private: Determine the index of the last bit used. */ -uint8_t find_end_bit(const uint16_t num_bits); +unsigned int find_end_bit(const unsigned int num_bits); #ifdef __cplusplus } diff --git a/libs/bitfield-c/src/bitfield/bitarray.c b/libs/bitfield-c/src/bitfield/bitarray.c index dcb9a08f..ec786a11 100644 --- a/libs/bitfield-c/src/bitfield/bitarray.c +++ b/libs/bitfield-c/src/bitfield/bitarray.c @@ -15,15 +15,15 @@ bit_count = 0; \ } } while (0) -static const uint8_t reverse_mask[] = +static const unsigned int reverse_mask[] = { 0x55, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; -static const uint8_t reverse_mask_xor[] = +static const unsigned int reverse_mask_xor[] = { 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00 }; -bool copy_bits(const uint8_t* source_origin, const uint16_t source_length, - const uint16_t source_offset, uint16_t bit_count, - uint8_t* destination_origin, const uint16_t destination_length, - const uint16_t destination_offset) { +bool copy_bits(const uint8_t* source_origin, const unsigned int source_length, + const unsigned int source_offset, unsigned int bit_count, + uint8_t* destination_origin, const unsigned int destination_length, + const unsigned int destination_offset) { if(bit_count < 1) { return false; } @@ -108,8 +108,8 @@ bool copy_bits(const uint8_t* source_origin, const uint16_t source_length, return true; } -uint16_t bits_to_bytes(uint32_t bits) { - uint8_t byte_count = bits / CHAR_BIT; +unsigned int bits_to_bytes(uint32_t bits) { + unsigned int byte_count = bits / CHAR_BIT; if(bits % CHAR_BIT != 0) { ++byte_count; } @@ -121,14 +121,14 @@ uint16_t bits_to_bytes(uint32_t bits) { * * Returns: a bit position from 0 to 7. */ -uint8_t find_end_bit(const uint16_t numBits) { +unsigned int find_end_bit(const unsigned int numBits) { int endBit = numBits % CHAR_BIT; return endBit == 0 ? CHAR_BIT : endBit; } -bool copy_bits_right_aligned(const uint8_t source[], const uint16_t source_length, - const uint16_t offset, const uint16_t bit_count, - uint8_t* destination, const uint16_t destination_length) { +bool copy_bits_right_aligned(const uint8_t source[], const unsigned int source_length, + const unsigned int offset, const unsigned int bit_count, + uint8_t* destination, const unsigned int destination_length) { return copy_bits(source, source_length, offset, bit_count, destination, destination_length, // provide a proper destination offset so the result is right @@ -137,9 +137,9 @@ bool copy_bits_right_aligned(const uint8_t source[], const uint16_t source_lengt CHAR_BIT - find_end_bit(bit_count)); } -bool copy_bytes_right_aligned(const uint8_t source[], const uint16_t source_length, - const uint16_t offset, const uint16_t byte_count, - uint8_t* destination, const uint16_t destination_length) { +bool copy_bytes_right_aligned(const uint8_t source[], const unsigned int source_length, + const unsigned int offset, const unsigned int byte_count, + uint8_t* destination, const unsigned int destination_length) { return copy_bits_right_aligned(source, source_length, offset * CHAR_BIT, byte_count * CHAR_BIT, destination, destination_length); } diff --git a/libs/bitfield-c/src/bitfield/bitfield.c b/libs/bitfield-c/src/bitfield/bitfield.c index 795f0208..e9077e1c 100644 --- a/libs/bitfield-c/src/bitfield/bitfield.c +++ b/libs/bitfield-c/src/bitfield/bitfield.c @@ -4,14 +4,14 @@ #include <stddef.h> #include <sys/param.h> -uint64_t bitmask(const uint8_t bit_count) { +uint64_t bitmask(const unsigned int bit_count) { return (((uint64_t)0x1) << bit_count) - 1; } -uint8_t get_nibble(const uint8_t source[], const uint8_t source_length, - const uint8_t nibble_index) { - uint8_t byte_index = nibble_index / 2; - uint8_t result = get_byte(source, source_length, byte_index); +unsigned int get_nibble(const uint8_t source[], const unsigned int source_length, + const unsigned int nibble_index) { + unsigned int byte_index = nibble_index / 2; + unsigned int result = get_byte(source, source_length, byte_index); if(nibble_index % 2 == 0) { result >>= NIBBLE_SIZE; } @@ -19,16 +19,16 @@ uint8_t get_nibble(const uint8_t source[], const uint8_t source_length, return result; } -uint8_t get_byte(const uint8_t source[], const uint8_t source_length, - const uint8_t byte_index) { +unsigned int get_byte(const uint8_t source[], const unsigned int source_length, + const unsigned int byte_index) { if(byte_index < source_length) { return source[byte_index]; } return 0; } -uint64_t get_bitfield(const uint8_t source[], const uint8_t source_length, - const uint16_t offset, const uint16_t bit_count) { +uint64_t get_bitfield(const uint8_t source[], const unsigned int source_length, + const unsigned int offset, const unsigned int bit_count) { if(bit_count > 64 || bit_count < 1) { // TODO error reporting? return 0; @@ -47,15 +47,15 @@ uint64_t get_bitfield(const uint8_t source[], const uint8_t source_length, return combined.whole; } -bool set_nibble(const uint16_t nibble_index, const uint8_t value, - uint8_t* destination, const uint16_t destination_length) { +bool set_nibble(const unsigned int nibble_index, const unsigned int value, + uint8_t* destination, const unsigned int destination_length) { return copy_bits(&value, CHAR_BIT, NIBBLE_SIZE, NIBBLE_SIZE, destination, 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) { +bool set_bitfield(const uint64_t value, const unsigned int offset, + const unsigned int bit_count, uint8_t destination[], + unsigned int destination_length) { if(value > bitmask(bit_count)) { return false; } diff --git a/libs/bitfield-c/src/bitfield/bitfield.h b/libs/bitfield-c/src/bitfield/bitfield.h index df92639a..8dd7499f 100644 --- a/libs/bitfield-c/src/bitfield/bitfield.h +++ b/libs/bitfield-c/src/bitfield/bitfield.h @@ -37,8 +37,8 @@ extern "C" { * * Returns the value of the requested bit field, right aligned in a uint64_t. */ -uint64_t get_bitfield(const uint8_t source[], const uint8_t source_length, - const uint16_t offset, const uint16_t bit_count); +uint64_t get_bitfield(const uint8_t source[], const unsigned int source_length, + const unsigned int offset, const unsigned int bit_count); /* Public: Return a single nibble from the byte array, with range checking. * @@ -47,10 +47,10 @@ uint64_t get_bitfield(const uint8_t source[], const uint8_t source_length, * nibble_index - the index of the nibble to retreive. The leftmost nibble is * index 0. * - * Returns the retreived nibble, right aligned in a uint8_t. + * Returns the retreived nibble, right aligned in a unsigned int. */ -uint8_t get_nibble(const uint8_t source[], const uint8_t source_length, - const uint8_t nibble_index); +unsigned int get_nibble(const uint8_t source[], const unsigned int source_length, + const unsigned int nibble_index); /* Public: Return a single byte from the byte array, with range checking. * @@ -60,8 +60,8 @@ uint8_t get_nibble(const uint8_t source[], const uint8_t source_length, * * Returns the retreived byte. */ -uint8_t get_byte(const uint8_t source[], const uint8_t source_length, - const uint8_t byte_index); +unsigned int get_byte(const uint8_t source[], const unsigned int source_length, + const unsigned int byte_index); /* Public: Copy a range of bits from one bit array to another. * @@ -74,8 +74,8 @@ uint8_t get_byte(const uint8_t source[], const uint8_t source_length, * * For example: * - * uint8_t source[4] = {0x11, 0x22, 0x33, 0x44}; - * uint8_t destination[4] = {0}; + * unsigned int source[4] = {0x11, 0x22, 0x33, 0x44}; + * unsigned int destination[4] = {0}; * copy_bits(source, sizeof(source), 8, 8, destination, * sizeof(destination), 0); * // destination[0] == 0x22 @@ -105,10 +105,10 @@ uint8_t get_byte(const uint8_t source[], const uint8_t source_length, * Returns true if the copy was successful and false if the range exceeded the * size of the source or destination, or if the range size negative or 0. */ -bool copy_bits(const uint8_t* source_origin, const uint16_t source_length, - const uint16_t source_offset, uint16_t bit_count, - uint8_t* destination_origin, const uint16_t destination_length, - const uint16_t destination_offset); +bool copy_bits(const uint8_t* source_origin, const unsigned int source_length, + const unsigned int source_offset, unsigned int bit_count, + uint8_t* destination_origin, const unsigned int destination_length, + const unsigned int destination_offset); /* Public: Copy a range of bits from one array to another, right aligning the * result. @@ -118,8 +118,8 @@ bool copy_bits(const uint8_t* source_origin, const uint16_t source_length, * * For example: * - * uint8_t source[4] = {0x11, 0x22, 0x33, 0x44}; - * uint8_t destination[4] = {0}; + * unsigned int source[4] = {0x11, 0x22, 0x33, 0x44}; + * unsigned int destination[4] = {0}; * copy_bits_right_aligned(source, sizeof(source), 8, 8, destination, * sizeof(destination)); * // destination[0] == 0x0 @@ -136,9 +136,9 @@ bool copy_bits(const uint8_t* source_origin, const uint16_t source_length, * Returns true if the copy was successful and false if the range exceeded the * size of the source or destination, or if the range size negative or 0. */ -bool copy_bits_right_aligned(const uint8_t source[], const uint16_t source_length, - const uint16_t offset, const uint16_t bit_count, - uint8_t* destination, const uint16_t destination_length); +bool copy_bits_right_aligned(const uint8_t source[], const unsigned int source_length, + const unsigned int offset, const unsigned int bit_count, + uint8_t* destination, const unsigned int destination_length); /* Public: Copy a range of bytes from one byte array to another. * @@ -161,9 +161,9 @@ bool copy_bits_right_aligned(const uint8_t source[], const uint16_t source_lengt * Returns true if the copy was successful and false if the range exceeded the * size of the source or destination, or if the range size negative or 0. */ -bool copy_bytes_right_aligned(const uint8_t source[], const uint16_t source_length, - const uint16_t offset, const uint16_t byte_count, - uint8_t* destination, const uint16_t destination_length); +bool copy_bytes_right_aligned(const uint8_t source[], const unsigned int source_length, + const unsigned int offset, const unsigned int byte_count, + uint8_t* destination, const unsigned int destination_length); /* Public: Set the a nibble in the given data array to the new value. * @@ -177,8 +177,8 @@ bool copy_bytes_right_aligned(const uint8_t source[], const uint16_t source_leng * Returns true if the bit_count is enough to fully represent the value, and * false if it will not fit. */ -bool set_nibble(const uint16_t nibble_index, const uint8_t value, - uint8_t* destination, const uint16_t destination_length); +bool set_nibble(const unsigned int nibble_index, const unsigned int value, + uint8_t* destination, const unsigned int destination_length); /* Public: Set the bit field in the given data array to the new value. * @@ -192,21 +192,21 @@ bool set_nibble(const uint16_t nibble_index, const uint8_t value, * 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); +bool set_bitfield(const uint64_t value, const unsigned int offset, + const unsigned int bit_count, uint8_t destination[], + unsigned int destination_length); /* Public: Return a right aligned bitmask for a uint64_t. * * bit_count - the number of bits to mask, right aligned. */ -uint64_t bitmask(const uint8_t bit_count); +uint64_t bitmask(const unsigned int bit_count); /* Private: */ -uint16_t bits_to_bytes(uint32_t bits); +unsigned int bits_to_bytes(uint32_t bits); -/* Private: A union to assist swapping between uint64_t and a uint8_t array. +/* Private: A union to assist swapping between uint64_t and a unsigned int array. */ typedef union { uint64_t whole; |