X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fbitfield%2F8byte.c;h=e3eddf200b1511302a1af2e35ce2994e861b7bbe;hb=4af52c415f1668fbd168da74d0aca903c592463f;hp=0f249d9306722153a6164e6a230a3c3eaa9c2b30;hpb=6ce03a4f1b229e605da08b073fad6f7c0fe8bf10;p=apps%2Fagl-service-can-low-level.git diff --git a/src/bitfield/8byte.c b/src/bitfield/8byte.c index 0f249d93..e3eddf20 100644 --- a/src/bitfield/8byte.c +++ b/src/bitfield/8byte.c @@ -4,53 +4,58 @@ #include #include -uint64_t bitmask(const uint8_t numBits) { - return (((uint64_t)0x1) << numBits) - 1; +#define EIGHTBYTE_BIT (8 * sizeof(uint64_t)) + +uint64_t bitmask(const uint8_t bit_count) { + return (((uint64_t)0x1) << bit_count) - 1; } -static uint16_t bitsToBytes(uint32_t bits) { - uint8_t byte_count = bits / CHAR_BIT; - if(bits % CHAR_BIT != 0) { - ++byte_count; - } - return byte_count; +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 getBitField(uint64_t data, const uint16_t startBit, - const uint16_t numBits, bool bigEndian) { - uint8_t result[8] = {0}; - if(!bigEndian) { - data = __builtin_bswap64(data); +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*)&data, sizeof(data), startBit, numBits, - result, sizeof(result)); - uint64_t int_result = 0; - - if(!bigEndian) { - // 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 < bitsToBytes(numBits); i++) { - int_result |= result[bitsToBytes(numBits) - 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; -} -/** - * TODO it would be nice to have a warning if you call with this a value that - * won't fit in the number of bits you've specified it should use. - */ -void setBitField(uint64_t* data, uint64_t value, const uint16_t startPos, - const uint16_t numBits) { - int shiftDistance = 64 - startPos - numBits; - value <<= shiftDistance; - *data &= ~(bitmask(numBits) << shiftDistance); - *data |= value; + ret >>= 8 - find_end_bit(offset + bit_count); + return ret & bitmask(bit_count); } -uint8_t nthByte(const uint64_t source, const uint16_t byteNum) { - return (source >> (64 - ((byteNum + 1) * CHAR_BIT))) & 0xFF; -} +bool set_bit_field(uint64_t* destination, uint64_t value, const uint16_t offset, + const uint16_t bit_count) { + if(value > bitmask(bit_count)) { + return false; + } + int shiftDistance = EIGHTBYTE_BIT - offset - bit_count; + value <<= shiftDistance; + *destination &= ~(bitmask(bit_count) << shiftDistance); + *destination |= value; + return true; +}