Commit | Line | Data |
---|---|---|
3845591f JA |
1 | #include <inttypes.h> |
2 | ||
3 | /* | |
4 | * Based on a posting to lkml by Austin Zhang <austin.zhang@intel.com> | |
5 | * | |
6 | * Using hardware provided CRC32 instruction to accelerate the CRC32 disposal. | |
7 | * CRC32C polynomial:0x1EDC6F41(BE)/0x82F63B78(LE) | |
8 | * CRC32 is a new instruction in Intel SSE4.2, the reference can be found at: | |
9 | * http://www.intel.com/products/processor/manuals/ | |
10 | * Intel(R) 64 and IA-32 Architectures Software Developer's Manual | |
11 | * Volume 2A: Instruction Set Reference, A-M | |
12 | */ | |
13 | ||
14 | #if BITS_PER_LONG == 64 | |
15 | #define REX_PRE "0x48, " | |
16 | #define SCALE_F 8 | |
17 | #else | |
18 | #define REX_PRE | |
19 | #define SCALE_F 4 | |
20 | #endif | |
21 | ||
22 | uint32_t crc32c_intel_le_hw_byte(uint32_t crc, unsigned char const *data, | |
23 | unsigned long length) | |
24 | { | |
25 | while (length--) { | |
26 | __asm__ __volatile__( | |
27 | ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1" | |
28 | :"=S"(crc) | |
29 | :"0"(crc), "c"(*data) | |
30 | ); | |
31 | data++; | |
32 | } | |
33 | ||
34 | return crc; | |
35 | } | |
36 | ||
37 | /* | |
38 | * Steps through buffer one byte at at time, calculates reflected | |
39 | * crc using table. | |
40 | */ | |
41 | uint32_t crc32c_intel(unsigned char const *data, unsigned long length) | |
42 | { | |
43 | unsigned int iquotient = length / SCALE_F; | |
44 | unsigned int iremainder = length % SCALE_F; | |
45 | #if BITS_PER_LONG == 64 | |
46 | uint64_t *ptmp = (uint64_t *) data; | |
47 | #else | |
48 | uint32_t *ptmp = (uint32_t *) data; | |
49 | #endif | |
50 | uint32_t crc = ~0; | |
51 | ||
52 | while (iquotient--) { | |
53 | __asm__ __volatile__( | |
54 | ".byte 0xf2, " REX_PRE "0xf, 0x38, 0xf1, 0xf1;" | |
55 | :"=S"(crc) | |
56 | :"0"(crc), "c"(*ptmp) | |
57 | ); | |
58 | ptmp++; | |
59 | } | |
60 | ||
61 | if (iremainder) | |
62 | crc = crc32c_intel_le_hw_byte(crc, (unsigned char *)ptmp, | |
63 | iremainder); | |
64 | ||
65 | return crc; | |
66 | } |