only: getBitRangeMsb

This commit is contained in:
rusefi 2024-04-07 15:49:02 -04:00
parent 21af489f12
commit 5f897ec686
2 changed files with 13 additions and 3 deletions

View File

@ -211,14 +211,23 @@ bool isPhaseInRange(float test, float current, float next) {
}
}
// see also getBitRange in lua_lib.h
int getBitRangeLsb(const uint8_t data[], int bitIndex, int bitWidth) {
static int getBitRangeCommon(const uint8_t data[], int bitIndex, int bitWidth, int secondByteOffset) {
int byteIndex = bitIndex >> 3;
int shift = bitIndex - byteIndex * 8;
int value = data[byteIndex];
if (shift + bitWidth > 8) {
value = value + data[1 + byteIndex] * 256;
value = value + data[secondByteOffset + byteIndex] * 256;
}
int mask = (1 << bitWidth) - 1;
return (value >> shift) & mask;
}
// see also getBitRange in lua_lib.h
int getBitRangeLsb(const uint8_t data[], int bitIndex, int bitWidth) {
return getBitRangeCommon(data, bitIndex, bitWidth, 1);
}
// see also getBitRangeMsb in lua_lib.h
int getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth) {
return getBitRangeCommon(data, bitIndex, bitWidth, -1);
}

View File

@ -130,3 +130,4 @@ constexpr remove_reference_t<_Ty>&& move(_Ty&& _Arg) noexcept {
}
int getBitRangeLsb(const uint8_t data[], int bitIndex, int bitWidth);
int getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth);