~daniel-mehrmann/e2fsprogs/master

« back to all changes in this revision

Viewing changes to lib/ext2fs/gen_crc32ctable.c

  • Committer: Daniel Mehrmann
  • Date: 2014-12-16 09:16:59 UTC
  • mfrom: (1.2.25)
  • Revision ID: daniel.mehrmann@gmx.de-20141216091659-ymhbl4ualba43vuc
Tags: 1.43-SN-2014-12-16-0ubuntu1
* Merge in snapshot from the maint branch 

Show diffs side-by-side

added added

removed removed

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