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