LUT only for lower byte needed, remove nibble code

This commit is contained in:
Ahmet Inan 2018-07-29 10:26:40 +02:00
commit afc3c0ae61

32
crc.hh
View file

@ -12,21 +12,17 @@ namespace DSP {
template <typename TYPE>
class CRC
{
static const int BYTES = sizeof(TYPE);
TYPE lut[BYTES * 256];
TYPE lut[256];
TYPE poly;
TYPE crc;
public:
CRC(TYPE poly, TYPE crc = 0) : poly(poly), crc(crc)
{
// exploit linearity of CRC only bytewise
for (int k = 0; k < BYTES; ++k) {
for (int j = 0; j < 256; ++j) {
TYPE tmp = j << (k << 3);
for (int i = 8; i; --i)
tmp = (tmp >> 1) ^ ((tmp & 1) * poly);
lut[(k << 8) + j] = tmp;
}
for (int j = 0; j < 256; ++j) {
TYPE tmp = j;
for (int i = 8; i; --i)
tmp = (tmp >> 1) ^ ((tmp & 1) * poly);
lut[j] = tmp;
}
}
void reset(TYPE v = 0)
@ -40,17 +36,14 @@ public:
TYPE operator()(bool data)
{
TYPE tmp = crc ^ data;
tmp = (tmp >> 1) ^ ((tmp & 1) * poly);
return crc = tmp;
return crc = (crc >> 1) ^ ((tmp & 1) * poly);
}
TYPE operator()(uint8_t data)
{
TYPE tmp = crc ^ data;
crc = lut[tmp & 255];
for (int k = 1; k < BYTES; ++k)
crc ^= lut[(k << 8) + ((tmp >>= 8) & 255)];
return crc;
return crc = (crc >> 8) ^ lut[tmp & 255];
}
TYPE operator()(uint16_t data)
{
(*this)(uint8_t(data & 255));
@ -79,13 +72,6 @@ public:
}
};
template<>
uint16_t CRC<uint16_t>::operator()(uint8_t data)
{
uint16_t tmp = crc ^ data;
return crc = lut[256 + (tmp >> 8)] ^ lut[tmp & 255];
}
template<>
uint8_t CRC<uint8_t>::operator()(uint8_t data)
{