17 static_assert(
sizeof(T) == 1 ||
sizeof(T) == 2 ||
sizeof(T) == 4,
"Type must be 1,2 or 4 bytes long");
18 value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1);
19 value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2);
22 value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4);
25 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
27 value = (value >> (4*
sizeof(T))) | (value << (4*
sizeof(T)));
35template<
typename T,
size_t LEN>
36void makeCrcTable(std::array<T,LEN> &table,
const T crcpoly,
const uint8_t bits,
const bool refin =
false,
const bool refout =
false)
38 T mask = 1 << (bits-1);
39 for (uint16_t
byte = 0;
byte < LEN; ++byte)
41 uint8_t shiftin = std::max<int16_t>(bits-8,0);
42 T
crc = (refin ? reverseBits<T>(
byte) >> shiftin : byte);
43 for (uint8_t bit = 0; bit < bits; ++bit)
47 crc = (
crc << 1) ^ crcpoly;
54 table[byte] = (refout ? reverseBits<T>(
crc) :
crc);
59uint8_t
calculateCrc8(std::array<uint8_t,256> &crctable,uint8_t* buf,uint16_t len,uint8_t
crc=0);
60uint16_t
calculateCrc16_8(std::array<uint16_t,256> &crctable,uint8_t* buf,uint16_t len,uint16_t
crc=0);
void makeCrcTable(std::array< T, LEN > &table, const T crcpoly, const uint8_t bits, const bool refin=false, const bool refout=false)
uint16_t calculateCrc16_8_rev(std::array< uint16_t, 256 > &crctable, uint8_t *buf, uint16_t len, uint16_t crc=0)
uint8_t calculateCrc8(std::array< uint8_t, 256 > &crctable, uint8_t *buf, uint16_t len, uint8_t crc=0)
uint16_t calculateCrc16_8(std::array< uint16_t, 256 > &crctable, uint8_t *buf, uint16_t len, uint16_t crc=0)