1 #include <bitfield/bitfield.h>
2 #include <bitfield/8byte.h>
7 #define EIGHTBYTE_BIT (8 * sizeof(uint64_t))
9 uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index,
10 const bool data_is_big_endian) {
11 return eightbyte_get_bitfield(source, NIBBLE_SIZE * nibble_index, NIBBLE_SIZE,
15 uint8_t eightbyte_get_byte(uint64_t source, const uint8_t byte_index,
16 const bool data_is_big_endian) {
17 if(data_is_big_endian) {
18 source = __builtin_bswap64(source);
20 return (source >> (EIGHTBYTE_BIT - ((byte_index + 1) * CHAR_BIT))) & 0xFF;
23 // TODO is this funciton necessary anymore? is it any faster for uint64_t than
24 // get_bitfield(data[], ...)? is the performance better on a 32 bit platform
26 uint64_t eightbyte_get_bitfield(uint64_t source, const uint16_t offset,
27 const uint16_t bit_count, const bool data_is_big_endian) {
28 int startByte = offset / CHAR_BIT;
29 int endByte = (offset + bit_count - 1) / CHAR_BIT;
31 if(!data_is_big_endian) {
32 source = __builtin_bswap64(source);
35 uint8_t* bytes = (uint8_t*)&source;
36 uint64_t ret = bytes[startByte];
37 if(startByte != endByte) {
38 // The lowest byte address contains the most significant bit.
39 for(uint8_t i = startByte + 1; i <= endByte; i++) {
45 ret >>= 8 - find_end_bit(offset + bit_count);
46 return ret & bitmask(bit_count);
49 bool set_bit_field(uint64_t* destination, uint64_t value, const uint16_t offset,
50 const uint16_t bit_count) {
51 if(value > bitmask(bit_count)) {
55 int shiftDistance = EIGHTBYTE_BIT - offset - bit_count;
56 value <<= shiftDistance;
57 *destination &= ~(bitmask(bit_count) << shiftDistance);
58 *destination |= value;