Blame view

lib/gen_crc32table.c 2.25 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
  #include <stdio.h>
  #include "crc32defs.h"
  #include <inttypes.h>
  
  #define ENTRIES_PER_LINE 4
  
  #define LE_TABLE_SIZE (1 << CRC_LE_BITS)
  #define BE_TABLE_SIZE (1 << CRC_BE_BITS)
836e2af92   Joakim Tjernlund   crc32: major opti...
9
10
  static uint32_t crc32table_le[4][LE_TABLE_SIZE];
  static uint32_t crc32table_be[4][BE_TABLE_SIZE];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
15
16
17
18
19
20
21
22
  
  /**
   * crc32init_le() - allocate and initialize LE table data
   *
   * crc is the crc of the byte i; other entries are filled in based on the
   * fact that crctable[i^j] = crctable[i] ^ crctable[j].
   *
   */
  static void crc32init_le(void)
  {
  	unsigned i, j;
  	uint32_t crc = 1;
836e2af92   Joakim Tjernlund   crc32: major opti...
23
  	crc32table_le[0][0] = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
26
27
  
  	for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) {
  		crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  		for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
836e2af92   Joakim Tjernlund   crc32: major opti...
28
29
30
31
32
33
34
35
  			crc32table_le[0][i + j] = crc ^ crc32table_le[0][j];
  	}
  	for (i = 0; i < LE_TABLE_SIZE; i++) {
  		crc = crc32table_le[0][i];
  		for (j = 1; j < 4; j++) {
  			crc = crc32table_le[0][crc & 0xff] ^ (crc >> 8);
  			crc32table_le[j][i] = crc;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
38
39
40
41
42
43
44
45
  	}
  }
  
  /**
   * crc32init_be() - allocate and initialize BE table data
   */
  static void crc32init_be(void)
  {
  	unsigned i, j;
  	uint32_t crc = 0x80000000;
836e2af92   Joakim Tjernlund   crc32: major opti...
46
  	crc32table_be[0][0] = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
48
49
50
  
  	for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
  		crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
  		for (j = 0; j < i; j++)
836e2af92   Joakim Tjernlund   crc32: major opti...
51
52
53
54
55
56
57
58
  			crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
  	}
  	for (i = 0; i < BE_TABLE_SIZE; i++) {
  		crc = crc32table_be[0][i];
  		for (j = 1; j < 4; j++) {
  			crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
  			crc32table_be[j][i] = crc;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
  	}
  }
836e2af92   Joakim Tjernlund   crc32: major opti...
61
  static void output_table(uint32_t table[4][256], int len, char *trans)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
  {
836e2af92   Joakim Tjernlund   crc32: major opti...
63
  	int i, j;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64

836e2af92   Joakim Tjernlund   crc32: major opti...
65
66
67
68
69
70
71
72
73
74
  	for (j = 0 ; j < 4; j++) {
  		printf("{");
  		for (i = 0; i < len - 1; i++) {
  			if (i % ENTRIES_PER_LINE == 0)
  				printf("
  ");
  			printf("%s(0x%8.8xL), ", trans, table[j][i]);
  		}
  		printf("%s(0x%8.8xL)},
  ", trans, table[j][len - 1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
77
78
79
80
81
82
83
84
85
  }
  
  int main(int argc, char** argv)
  {
  	printf("/* this file is generated - do not edit */
  
  ");
  
  	if (CRC_LE_BITS > 1) {
  		crc32init_le();
836e2af92   Joakim Tjernlund   crc32: major opti...
86
  		printf("static const u32 crc32table_le[4][256] = {");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
88
89
90
91
92
93
  		output_table(crc32table_le, LE_TABLE_SIZE, "tole");
  		printf("};
  ");
  	}
  
  	if (CRC_BE_BITS > 1) {
  		crc32init_be();
836e2af92   Joakim Tjernlund   crc32: major opti...
94
  		printf("static const u32 crc32table_be[4][256] = {");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
98
99
100
101
  		output_table(crc32table_be, BE_TABLE_SIZE, "tobe");
  		printf("};
  ");
  	}
  
  	return 0;
  }