]> git.karo-electronics.de Git - karo-tx-linux.git/blob - lib/gen_crc32table.c
crc32: add slice-by-8 algorithm to existing code
[karo-tx-linux.git] / lib / gen_crc32table.c
1 #include <stdio.h>
2 #include "../include/generated/autoconf.h"
3 #include "crc32defs.h"
4 #include <inttypes.h>
5
6 #define ENTRIES_PER_LINE 4
7
8 #if CRC_LE_BITS > 8
9 # define LE_TABLE_ROWS (CRC_LE_BITS/8)
10 # define LE_TABLE_SIZE 256
11 #else
12 # define LE_TABLE_ROWS 1
13 # define LE_TABLE_SIZE (1 << CRC_LE_BITS)
14 #endif
15
16 #if CRC_BE_BITS > 8
17 # define BE_TABLE_ROWS (CRC_BE_BITS/8)
18 # define BE_TABLE_SIZE 256
19 #else
20 # define BE_TABLE_ROWS 1
21 # define BE_TABLE_SIZE (1 << CRC_BE_BITS)
22 #endif
23
24 static uint32_t crc32table_le[LE_TABLE_ROWS][256];
25 static uint32_t crc32table_be[BE_TABLE_ROWS][256];
26
27 /**
28  * crc32init_le() - allocate and initialize LE table data
29  *
30  * crc is the crc of the byte i; other entries are filled in based on the
31  * fact that crctable[i^j] = crctable[i] ^ crctable[j].
32  *
33  */
34 static void crc32init_le(void)
35 {
36         unsigned i, j;
37         uint32_t crc = 1;
38
39         crc32table_le[0][0] = 0;
40
41         for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
42                 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
43                 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
44                         crc32table_le[0][i + j] = crc ^ crc32table_le[0][j];
45         }
46         for (i = 0; i < LE_TABLE_SIZE; i++) {
47                 crc = crc32table_le[0][i];
48                 for (j = 1; j < LE_TABLE_ROWS; j++) {
49                         crc = crc32table_le[0][crc & 0xff] ^ (crc >> 8);
50                         crc32table_le[j][i] = crc;
51                 }
52         }
53 }
54
55 /**
56  * crc32init_be() - allocate and initialize BE table data
57  */
58 static void crc32init_be(void)
59 {
60         unsigned i, j;
61         uint32_t crc = 0x80000000;
62
63         crc32table_be[0][0] = 0;
64
65         for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
66                 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
67                 for (j = 0; j < i; j++)
68                         crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
69         }
70         for (i = 0; i < BE_TABLE_SIZE; i++) {
71                 crc = crc32table_be[0][i];
72                 for (j = 1; j < BE_TABLE_ROWS; j++) {
73                         crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
74                         crc32table_be[j][i] = crc;
75                 }
76         }
77 }
78
79 static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
80 {
81         int i, j;
82
83         for (j = 0 ; j < rows; j++) {
84                 printf("{");
85                 for (i = 0; i < len - 1; i++) {
86                         if (i % ENTRIES_PER_LINE == 0)
87                                 printf("\n");
88                         printf("%s(0x%8.8xL), ", trans, table[j][i]);
89                 }
90                 printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
91         }
92 }
93
94 int main(int argc, char** argv)
95 {
96         printf("/* this file is generated - do not edit */\n\n");
97
98         if (CRC_LE_BITS > 1) {
99                 crc32init_le();
100                 printf("static const u32 __cacheline_aligned "
101                        "crc32table_le[%d][%d] = {",
102                        LE_TABLE_ROWS, LE_TABLE_SIZE);
103                 output_table(crc32table_le, LE_TABLE_ROWS,
104                              LE_TABLE_SIZE, "tole");
105                 printf("};\n");
106         }
107
108         if (CRC_BE_BITS > 1) {
109                 crc32init_be();
110                 printf("static const u32 __cacheline_aligned "
111                        "crc32table_be[%d][%d] = {",
112                        BE_TABLE_ROWS, BE_TABLE_SIZE);
113                 output_table(crc32table_be, LE_TABLE_ROWS,
114                              BE_TABLE_SIZE, "tobe");
115                 printf("};\n");
116         }
117
118         return 0;
119 }