blob: 85d0e412a04f966a187fac0a5f8c7ff16c9a49c8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <stdio.h>
2#include "crc32defs.h"
3#include <inttypes.h>
4
5#define ENTRIES_PER_LINE 4
6
7#define LE_TABLE_SIZE (1 << CRC_LE_BITS)
8#define BE_TABLE_SIZE (1 << CRC_BE_BITS)
9
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070010static uint32_t crc32table_le[4][LE_TABLE_SIZE];
11static uint32_t crc32table_be[4][BE_TABLE_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13/**
14 * crc32init_le() - allocate and initialize LE table data
15 *
16 * crc is the crc of the byte i; other entries are filled in based on the
17 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
18 *
19 */
20static void crc32init_le(void)
21{
22 unsigned i, j;
23 uint32_t crc = 1;
24
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070025 crc32table_le[0][0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) {
28 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
29 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070030 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
39}
40
41/**
42 * crc32init_be() - allocate and initialize BE table data
43 */
44static void crc32init_be(void)
45{
46 unsigned i, j;
47 uint32_t crc = 0x80000000;
48
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070049 crc32table_be[0][0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
52 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
53 for (j = 0; j < i; j++)
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070054 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63}
64
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070065static void output_table(uint32_t table[4][256], int len, char *trans)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070067 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070069 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80int main(int argc, char** argv)
81{
82 printf("/* this file is generated - do not edit */\n\n");
83
84 if (CRC_LE_BITS > 1) {
85 crc32init_le();
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070086 printf("static const u32 crc32table_le[4][256] = {");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 output_table(crc32table_le, LE_TABLE_SIZE, "tole");
88 printf("};\n");
89 }
90
91 if (CRC_BE_BITS > 1) {
92 crc32init_be();
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070093 printf("static const u32 crc32table_be[4][256] = {");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 output_table(crc32table_be, BE_TABLE_SIZE, "tobe");
95 printf("};\n");
96 }
97
98 return 0;
99}