-
Notifications
You must be signed in to change notification settings - Fork 4
/
Crc64.cpp
62 lines (50 loc) · 1.6 KB
/
Crc64.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "Crc64.h"
#include "CrcParameters.h"
#include <iostream>
#include <iomanip>
#include <cstdint>
#include <array>
//*****************************************************************************
void Crc64::DisplayTable() const
{
int index = 0;
for (int i = 0; i < 16; ++i)
{
for (int j = 0; j < 16; ++j)
{
std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(16) << crcTable[index++] << ", ";
}
std::cout << "\n";
}
}
//*****************************************************************************
void Crc64::GenerateTable(uint64_t polynomial, bool reflectIn, bool reflectOut)
{
for (int byte = 0; byte < 256; ++byte)
{
uint64_t crc = (reflectIn ? (Reverse(uint64_t(byte)) >> 56) : byte);
for (int bit = 0; bit < 64; ++bit)
{
if (crc & 0x8000000000000000)
{
crc = (crc << 1) ^ polynomial;
}
else
{
crc <<= 1;
}
}
crcTable[byte] = (reflectOut ? Reverse(crc) : crc);
}
}
//*****************************************************************************
uint64_t Crc64::Reverse(uint64_t value) const
{
value = ((value & 0xAAAAAAAAAAAAAAAA) >> 1) | ((value & 0x5555555555555555) << 1);
value = ((value & 0xCCCCCCCCCCCCCCCC) >> 2) | ((value & 0x3333333333333333) << 2);
value = ((value & 0xF0F0F0F0F0F0F0F0) >> 4) | ((value & 0x0F0F0F0F0F0F0F0F) << 4);
value = ((value & 0xFF00FF00FF00FF00) >> 8) | ((value & 0x00FF00FF00FF00FF) << 8);
value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16);
value = (value >> 32) | (value << 32);
return value;
}