mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
LUT only for lower byte needed, remove nibble code
This commit is contained in:
parent
f410522d4a
commit
afc3c0ae61
1 changed files with 9 additions and 23 deletions
32
crc.hh
32
crc.hh
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue