1 // SPDX-License-Identifier: GPL-2.0
3 #include "../include/linux/crc32poly.h"
4 #include "../include/generated/autoconf.h"
8 #define ENTRIES_PER_LINE 4
11 # define LE_TABLE_ROWS (CRC_LE_BITS/8)
12 # define LE_TABLE_SIZE 256
14 # define LE_TABLE_ROWS 1
15 # define LE_TABLE_SIZE (1 << CRC_LE_BITS)
19 # define BE_TABLE_ROWS (CRC_BE_BITS/8)
20 # define BE_TABLE_SIZE 256
22 # define BE_TABLE_ROWS 1
23 # define BE_TABLE_SIZE (1 << CRC_BE_BITS)
26 static uint32_t crc32table_le[LE_TABLE_ROWS][256];
27 static uint32_t crc32table_be[BE_TABLE_ROWS][256];
28 static uint32_t crc32ctable_le[LE_TABLE_ROWS][256];
31 * crc32init_le() - allocate and initialize LE table data
33 * crc is the crc of the byte i; other entries are filled in based on the
34 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
37 static void crc32init_le_generic(const uint32_t polynomial,
45 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
46 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
47 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
48 tab[0][i + j] = crc ^ tab[0][j];
50 for (i = 0; i < LE_TABLE_SIZE; i++) {
52 for (j = 1; j < LE_TABLE_ROWS; j++) {
53 crc = tab[0][crc & 0xff] ^ (crc >> 8);
59 static void crc32init_le(void)
61 crc32init_le_generic(CRC32_POLY_LE, crc32table_le);
64 static void crc32cinit_le(void)
66 crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
70 * crc32init_be() - allocate and initialize BE table data
72 static void crc32init_be(void)
75 uint32_t crc = 0x80000000;
77 crc32table_be[0][0] = 0;
79 for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
80 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRC32_POLY_BE : 0);
81 for (j = 0; j < i; j++)
82 crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
84 for (i = 0; i < BE_TABLE_SIZE; i++) {
85 crc = crc32table_be[0][i];
86 for (j = 1; j < BE_TABLE_ROWS; j++) {
87 crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
88 crc32table_be[j][i] = crc;
93 static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
97 for (j = 0 ; j < rows; j++) {
99 for (i = 0; i < len - 1; i++) {
100 if (i % ENTRIES_PER_LINE == 0)
102 printf("%s(0x%8.8xL), ", trans, table[j][i]);
104 printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
108 int main(int argc, char** argv)
110 printf("/* this file is generated - do not edit */\n\n");
112 if (CRC_LE_BITS > 1) {
114 printf("static const u32 ____cacheline_aligned "
115 "crc32table_le[%d][%d] = {",
116 LE_TABLE_ROWS, LE_TABLE_SIZE);
117 output_table(crc32table_le, LE_TABLE_ROWS,
118 LE_TABLE_SIZE, "tole");
122 if (CRC_BE_BITS > 1) {
124 printf("static const u32 ____cacheline_aligned "
125 "crc32table_be[%d][%d] = {",
126 BE_TABLE_ROWS, BE_TABLE_SIZE);
127 output_table(crc32table_be, LE_TABLE_ROWS,
128 BE_TABLE_SIZE, "tobe");
131 if (CRC_LE_BITS > 1) {
133 printf("static const u32 ____cacheline_aligned "
134 "crc32ctable_le[%d][%d] = {",
135 LE_TABLE_ROWS, LE_TABLE_SIZE);
136 output_table(crc32ctable_le, LE_TABLE_ROWS,
137 LE_TABLE_SIZE, "tole");