forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CRC.h
35 lines (27 loc) · 882 Bytes
/
CRC.h
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
// Copyright (c) 2011-12, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#ifndef _CRC_h_
#define _CRC_h_
#include <climits>
template <typename T> uint32_t crc(uint32_t checksum, T data, uint32_t poly)
{
for (unsigned i = 0; i < sizeof(T) * CHAR_BIT; i++) {
int xorBit = (checksum & 1);
checksum = (checksum >> 1) | ((data & 1) << 31);
data = data >> 1;
if (xorBit)
checksum = checksum ^ poly;
}
return checksum;
}
inline uint32_t crc32(uint32_t checksum, uint32_t data, uint32_t poly)
{
return crc<uint32_t>(checksum, data, poly);
}
inline uint32_t crc8(uint32_t checksum, uint8_t data, uint32_t poly)
{
return crc<uint8_t>(checksum, data, poly);
}
#endif //_CRC_h_