Skip to content

Commit 836e2af

Browse files
Joakim Tjernlundtorvalds
Joakim Tjernlund
authored andcommitted
crc32: major optimization
Precompute more crc32 values(0xcc00, 0xcc0000 and 0xcc000000) into tables. This increases the table size from 1KB to 4KB but the performance benfit makes it worth it: 28% faster on MPC8321, 266 MHz 2x faster on Core 2 Duo, 3.1GHz [[email protected]: coding-style fixes] Signed-off-by: Joakim Tjernlund <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent d4977c7 commit 836e2af

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

lib/crc32.c

+15-9
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,20 @@ MODULE_LICENSE("GPL");
4848
#if CRC_LE_BITS == 8 || CRC_BE_BITS == 8
4949

5050
static inline u32
51-
crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 *tab)
51+
crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
5252
{
5353
# ifdef __LITTLE_ENDIAN
54-
# define DO_CRC(x) crc = tab[(crc ^ (x)) & 255 ] ^ (crc >> 8)
54+
# define DO_CRC(x) crc = tab[0][(crc ^ (x)) & 255] ^ (crc >> 8)
55+
# define DO_CRC4 crc = tab[3][(crc) & 255] ^ \
56+
tab[2][(crc >> 8) & 255] ^ \
57+
tab[1][(crc >> 16) & 255] ^ \
58+
tab[0][(crc >> 24) & 255]
5559
# else
56-
# define DO_CRC(x) crc = tab[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
60+
# define DO_CRC(x) crc = tab[0][((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
61+
# define DO_CRC4 crc = tab[0][(crc) & 255] ^ \
62+
tab[1][(crc >> 8) & 255] ^ \
63+
tab[2][(crc >> 16) & 255] ^ \
64+
tab[3][(crc >> 24) & 255]
5765
# endif
5866
const u32 *b;
5967
size_t rem_len;
@@ -70,10 +78,7 @@ crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 *tab)
7078
b = (const u32 *)buf;
7179
for (--b; len; --len) {
7280
crc ^= *++b; /* use pre increment for speed */
73-
DO_CRC(0);
74-
DO_CRC(0);
75-
DO_CRC(0);
76-
DO_CRC(0);
81+
DO_CRC4;
7782
}
7883
len = rem_len;
7984
/* And the last few bytes */
@@ -85,6 +90,7 @@ crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 *tab)
8590
}
8691
return crc;
8792
#undef DO_CRC
93+
#undef DO_CRC4
8894
}
8995
#endif
9096
/**
@@ -117,7 +123,7 @@ u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
117123
u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
118124
{
119125
# if CRC_LE_BITS == 8
120-
const u32 *tab = crc32table_le;
126+
const u32 (*tab)[] = crc32table_le;
121127

122128
crc = __cpu_to_le32(crc);
123129
crc = crc32_body(crc, p, len, tab);
@@ -174,7 +180,7 @@ u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
174180
u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
175181
{
176182
# if CRC_BE_BITS == 8
177-
const u32 *tab = crc32table_be;
183+
const u32 (*tab)[] = crc32table_be;
178184

179185
crc = __cpu_to_be32(crc);
180186
crc = crc32_body(crc, p, len, tab);

lib/gen_crc32table.c

+32-15
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#define LE_TABLE_SIZE (1 << CRC_LE_BITS)
88
#define BE_TABLE_SIZE (1 << CRC_BE_BITS)
99

10-
static uint32_t crc32table_le[LE_TABLE_SIZE];
11-
static uint32_t crc32table_be[BE_TABLE_SIZE];
10+
static uint32_t crc32table_le[4][LE_TABLE_SIZE];
11+
static uint32_t crc32table_be[4][BE_TABLE_SIZE];
1212

1313
/**
1414
* crc32init_le() - allocate and initialize LE table data
@@ -22,12 +22,19 @@ static void crc32init_le(void)
2222
unsigned i, j;
2323
uint32_t crc = 1;
2424

25-
crc32table_le[0] = 0;
25+
crc32table_le[0][0] = 0;
2626

2727
for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) {
2828
crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
2929
for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
30-
crc32table_le[i + j] = crc ^ crc32table_le[j];
30+
crc32table_le[0][i + j] = crc ^ crc32table_le[0][j];
31+
}
32+
for (i = 0; i < LE_TABLE_SIZE; i++) {
33+
crc = crc32table_le[0][i];
34+
for (j = 1; j < 4; j++) {
35+
crc = crc32table_le[0][crc & 0xff] ^ (crc >> 8);
36+
crc32table_le[j][i] = crc;
37+
}
3138
}
3239
}
3340

@@ -39,25 +46,35 @@ static void crc32init_be(void)
3946
unsigned i, j;
4047
uint32_t crc = 0x80000000;
4148

42-
crc32table_be[0] = 0;
49+
crc32table_be[0][0] = 0;
4350

4451
for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
4552
crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
4653
for (j = 0; j < i; j++)
47-
crc32table_be[i + j] = crc ^ crc32table_be[j];
54+
crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
55+
}
56+
for (i = 0; i < BE_TABLE_SIZE; i++) {
57+
crc = crc32table_be[0][i];
58+
for (j = 1; j < 4; j++) {
59+
crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
60+
crc32table_be[j][i] = crc;
61+
}
4862
}
4963
}
5064

51-
static void output_table(uint32_t table[], int len, char *trans)
65+
static void output_table(uint32_t table[4][256], int len, char *trans)
5266
{
53-
int i;
67+
int i, j;
5468

55-
for (i = 0; i < len - 1; i++) {
56-
if (i % ENTRIES_PER_LINE == 0)
57-
printf("\n");
58-
printf("%s(0x%8.8xL), ", trans, table[i]);
69+
for (j = 0 ; j < 4; j++) {
70+
printf("{");
71+
for (i = 0; i < len - 1; i++) {
72+
if (i % ENTRIES_PER_LINE == 0)
73+
printf("\n");
74+
printf("%s(0x%8.8xL), ", trans, table[j][i]);
75+
}
76+
printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
5977
}
60-
printf("%s(0x%8.8xL)\n", trans, table[len - 1]);
6178
}
6279

6380
int main(int argc, char** argv)
@@ -66,14 +83,14 @@ int main(int argc, char** argv)
6683

6784
if (CRC_LE_BITS > 1) {
6885
crc32init_le();
69-
printf("static const u32 crc32table_le[] = {");
86+
printf("static const u32 crc32table_le[4][256] = {");
7087
output_table(crc32table_le, LE_TABLE_SIZE, "tole");
7188
printf("};\n");
7289
}
7390

7491
if (CRC_BE_BITS > 1) {
7592
crc32init_be();
76-
printf("static const u32 crc32table_be[] = {");
93+
printf("static const u32 crc32table_be[4][256] = {");
7794
output_table(crc32table_be, BE_TABLE_SIZE, "tobe");
7895
printf("};\n");
7996
}

0 commit comments

Comments
 (0)