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