X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fbitfield%2F8byte.c;h=f08f227decdd7bf27181478a74f3cfc1d26b641e;hb=0ba19fae04ee48392872a9647a3b711b9115f147;hp=3b6b5466d76bdbba32bfa5829df66761acd06797;hpb=f29f8a4cefbdc798c4a9aba495da7d2c0a81774c;p=apps%2Fagl-service-can-low-level.git diff --git a/src/bitfield/8byte.c b/src/bitfield/8byte.c index 3b6b5466..f08f227d 100644 --- a/src/bitfield/8byte.c +++ b/src/bitfield/8byte.c @@ -18,26 +18,41 @@ static uint16_t bits_to_bytes(uint32_t bits) { return byte_count; } -uint64_t get_bit_field(uint64_t source, const uint16_t startBit, - const uint16_t bit_count, bool big_endian) { - uint8_t result[8] = {0}; +uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index, + const bool big_endian) { + return get_bit_field(source, NIBBLE_SIZE * nibble_index, NIBBLE_SIZE, + big_endian); +} + +uint8_t eightbyte_get_byte(const uint64_t source, const uint8_t byte_index, + const bool big_endian) { + // TODO we're not handling swapped endianness - we could use get_bit_field + // but this might be more efficient + return (source >> (EIGHTBYTE_BIT - ((byte_index + 1) * CHAR_BIT))) & 0xFF; +} + +uint64_t get_bit_field(uint64_t source, const uint16_t offset, + const uint16_t bit_count, const bool big_endian) { + 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, @@ -52,8 +67,3 @@ bool set_bit_field(uint64_t* destination, uint64_t value, const uint16_t offset, *destination |= value; return true; } - -uint8_t nth_byte(const uint64_t source, const uint16_t byte_index) { - return (source >> (EIGHTBYTE_BIT - ((byte_index + 1) * CHAR_BIT))) & 0xFF; -} -