]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | #include <stdio.h> |
324eb0f1 | 3 | #include "../include/generated/autoconf.h" |
1da177e4 LT |
4 | #include "crc32defs.h" |
5 | #include <inttypes.h> | |
6 | ||
7 | #define ENTRIES_PER_LINE 4 | |
8 | ||
324eb0f1 BP |
9 | #if CRC_LE_BITS > 8 |
10 | # define LE_TABLE_ROWS (CRC_LE_BITS/8) | |
11 | # define LE_TABLE_SIZE 256 | |
9a1dbf6a | 12 | #else |
324eb0f1 BP |
13 | # define LE_TABLE_ROWS 1 |
14 | # define LE_TABLE_SIZE (1 << CRC_LE_BITS) | |
9a1dbf6a BP |
15 | #endif |
16 | ||
324eb0f1 BP |
17 | #if CRC_BE_BITS > 8 |
18 | # define BE_TABLE_ROWS (CRC_BE_BITS/8) | |
19 | # define BE_TABLE_SIZE 256 | |
9a1dbf6a | 20 | #else |
324eb0f1 BP |
21 | # define BE_TABLE_ROWS 1 |
22 | # define BE_TABLE_SIZE (1 << CRC_BE_BITS) | |
9a1dbf6a | 23 | #endif |
1da177e4 | 24 | |
324eb0f1 BP |
25 | static uint32_t crc32table_le[LE_TABLE_ROWS][256]; |
26 | static uint32_t crc32table_be[BE_TABLE_ROWS][256]; | |
46c5801e | 27 | static uint32_t crc32ctable_le[LE_TABLE_ROWS][256]; |
1da177e4 LT |
28 | |
29 | /** | |
30 | * crc32init_le() - allocate and initialize LE table data | |
31 | * | |
32 | * crc is the crc of the byte i; other entries are filled in based on the | |
33 | * fact that crctable[i^j] = crctable[i] ^ crctable[j]. | |
34 | * | |
35 | */ | |
46c5801e DW |
36 | static void crc32init_le_generic(const uint32_t polynomial, |
37 | uint32_t (*tab)[256]) | |
1da177e4 LT |
38 | { |
39 | unsigned i, j; | |
40 | uint32_t crc = 1; | |
41 | ||
46c5801e | 42 | tab[0][0] = 0; |
1da177e4 | 43 | |
9a1dbf6a | 44 | for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) { |
46c5801e | 45 | crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0); |
1da177e4 | 46 | for (j = 0; j < LE_TABLE_SIZE; j += 2 * i) |
46c5801e | 47 | tab[0][i + j] = crc ^ tab[0][j]; |
836e2af9 JT |
48 | } |
49 | for (i = 0; i < LE_TABLE_SIZE; i++) { | |
46c5801e | 50 | crc = tab[0][i]; |
324eb0f1 | 51 | for (j = 1; j < LE_TABLE_ROWS; j++) { |
46c5801e DW |
52 | crc = tab[0][crc & 0xff] ^ (crc >> 8); |
53 | tab[j][i] = crc; | |
836e2af9 | 54 | } |
1da177e4 LT |
55 | } |
56 | } | |
57 | ||
46c5801e DW |
58 | static void crc32init_le(void) |
59 | { | |
60 | crc32init_le_generic(CRCPOLY_LE, crc32table_le); | |
61 | } | |
62 | ||
63 | static void crc32cinit_le(void) | |
64 | { | |
65 | crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le); | |
66 | } | |
67 | ||
1da177e4 LT |
68 | /** |
69 | * crc32init_be() - allocate and initialize BE table data | |
70 | */ | |
71 | static void crc32init_be(void) | |
72 | { | |
73 | unsigned i, j; | |
74 | uint32_t crc = 0x80000000; | |
75 | ||
836e2af9 | 76 | crc32table_be[0][0] = 0; |
1da177e4 LT |
77 | |
78 | for (i = 1; i < BE_TABLE_SIZE; i <<= 1) { | |
79 | crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0); | |
80 | for (j = 0; j < i; j++) | |
836e2af9 JT |
81 | crc32table_be[0][i + j] = crc ^ crc32table_be[0][j]; |
82 | } | |
83 | for (i = 0; i < BE_TABLE_SIZE; i++) { | |
84 | crc = crc32table_be[0][i]; | |
324eb0f1 | 85 | for (j = 1; j < BE_TABLE_ROWS; j++) { |
836e2af9 JT |
86 | crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8); |
87 | crc32table_be[j][i] = crc; | |
88 | } | |
1da177e4 LT |
89 | } |
90 | } | |
91 | ||
324eb0f1 | 92 | static void output_table(uint32_t (*table)[256], int rows, int len, char *trans) |
1da177e4 | 93 | { |
836e2af9 | 94 | int i, j; |
1da177e4 | 95 | |
324eb0f1 | 96 | for (j = 0 ; j < rows; j++) { |
836e2af9 JT |
97 | printf("{"); |
98 | for (i = 0; i < len - 1; i++) { | |
99 | if (i % ENTRIES_PER_LINE == 0) | |
100 | printf("\n"); | |
101 | printf("%s(0x%8.8xL), ", trans, table[j][i]); | |
102 | } | |
103 | printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]); | |
1da177e4 | 104 | } |
1da177e4 LT |
105 | } |
106 | ||
107 | int main(int argc, char** argv) | |
108 | { | |
109 | printf("/* this file is generated - do not edit */\n\n"); | |
110 | ||
111 | if (CRC_LE_BITS > 1) { | |
112 | crc32init_le(); | |
f5e38b92 | 113 | printf("static const u32 ____cacheline_aligned " |
324eb0f1 BP |
114 | "crc32table_le[%d][%d] = {", |
115 | LE_TABLE_ROWS, LE_TABLE_SIZE); | |
116 | output_table(crc32table_le, LE_TABLE_ROWS, | |
117 | LE_TABLE_SIZE, "tole"); | |
1da177e4 LT |
118 | printf("};\n"); |
119 | } | |
120 | ||
121 | if (CRC_BE_BITS > 1) { | |
122 | crc32init_be(); | |
f5e38b92 | 123 | printf("static const u32 ____cacheline_aligned " |
324eb0f1 BP |
124 | "crc32table_be[%d][%d] = {", |
125 | BE_TABLE_ROWS, BE_TABLE_SIZE); | |
126 | output_table(crc32table_be, LE_TABLE_ROWS, | |
127 | BE_TABLE_SIZE, "tobe"); | |
1da177e4 LT |
128 | printf("};\n"); |
129 | } | |
46c5801e DW |
130 | if (CRC_LE_BITS > 1) { |
131 | crc32cinit_le(); | |
f5e38b92 | 132 | printf("static const u32 ____cacheline_aligned " |
46c5801e DW |
133 | "crc32ctable_le[%d][%d] = {", |
134 | LE_TABLE_ROWS, LE_TABLE_SIZE); | |
135 | output_table(crc32ctable_le, LE_TABLE_ROWS, | |
136 | LE_TABLE_SIZE, "tole"); | |
137 | printf("};\n"); | |
138 | } | |
1da177e4 LT |
139 | |
140 | return 0; | |
141 | } |