Whamcloud - gitweb
LU-1373 ptlrpc: add flow control extension to ptlrpc req set
[fs/lustre-release.git] / libcfs / libcfs / posix / posix-crc32.c
1 /*
2  *      This file contains part of linux kernel implementation of crc32
3  *      kernel version 2.6.32
4  */
5 #include <libcfs/libcfs.h>
6 #define CRCPOLY_LE      0xedb88320
7 #define CRC_LE_BITS     8
8 #define LE_TABLE_SIZE   (1 << CRC_LE_BITS)
9
10 static unsigned int crc32table_le[LE_TABLE_SIZE];
11 /**
12  * crc32init_le() - allocate and initialize LE table data
13  *
14  * crc is the crc of the byte i; other entries are filled in based on the
15  * fact that crctable[i^j] = crctable[i] ^ crctable[j].
16  *
17  */
18 void crc32init_le(void)
19 {
20         unsigned i, j;
21         unsigned int crc = 1;
22
23         crc32table_le[0] = 0;
24
25         for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) {
26                 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
27                 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
28                         crc32table_le[i + j] = crc ^ crc32table_le[j];
29         }
30 }
31
32 unsigned int crc32_le(unsigned int crc, unsigned char const *p, size_t len)
33 {
34         const unsigned int      *b = (unsigned int *)p;
35         const unsigned int      *tab = crc32table_le;
36
37 # ifdef __LITTLE_ENDIAN
38 #  define DO_CRC(x) crc = tab[(crc ^ (x)) & 255] ^ (crc>>8)
39 # else
40 #  define DO_CRC(x) crc = tab[((crc >> 24) ^ (x)) & 255] ^ (crc<<8)
41 # endif
42
43         crc = cpu_to_le32(crc);
44         /* Align it */
45         if (unlikely(((long)b) & 3 && len)) {
46                 do {
47                         unsigned char *p = (unsigned char *)b;
48                         DO_CRC(*p++);
49                         b = (void *)p;
50                 } while ((--len) && ((long)b) & 3);
51         }
52         if (likely(len >= 4)) {
53                 /* load data 32 bits wide, xor data 32 bits wide. */
54                 size_t save_len = len & 3;
55                 len = len >> 2;
56                 --b; /* use pre increment below(*++b) for speed */
57                 do {
58                         crc ^= *++b;
59                         DO_CRC(0);
60                         DO_CRC(0);
61                         DO_CRC(0);
62                         DO_CRC(0);
63                 } while (--len);
64                 b++; /* point to next byte(s) */
65                 len = save_len;
66         }
67         /* And the last few bytes */
68         if (len) {
69                 do {
70                         unsigned char *p = (unsigned char *)b;
71                         DO_CRC(*p++);
72                         b = (void *)p;
73                 } while (--len);
74         }
75
76         return le32_to_cpu(crc);
77 #undef DO_CRC
78 }