Linux 6.16-rc4
[linux-2.6-block.git] / lib / gen_crc32table.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2#include <stdio.h>
1fb2e3f2 3#include "../include/linux/crc32poly.h"
324eb0f1 4#include "../include/generated/autoconf.h"
1da177e4
LT
5#include <inttypes.h>
6
5e3c1c48
EB
7static uint32_t crc32table_le[256];
8static uint32_t crc32table_be[256];
9static uint32_t crc32ctable_le[256];
1da177e4
LT
10
11/**
12 * crc32init_le() - allocate and initialize LE table data
13 *
14 * crc is the crc of the byte i; other entries are filled in based on the
15 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
16 *
17 */
5e3c1c48 18static void crc32init_le_generic(const uint32_t polynomial, uint32_t tab[256])
1da177e4
LT
19{
20 unsigned i, j;
21 uint32_t crc = 1;
22
5e3c1c48 23 tab[0] = 0;
1da177e4 24
5e3c1c48 25 for (i = 128; i; i >>= 1) {
46c5801e 26 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
5e3c1c48
EB
27 for (j = 0; j < 256; j += 2 * i)
28 tab[i + j] = crc ^ tab[j];
1da177e4
LT
29 }
30}
31
46c5801e
DW
32static void crc32init_le(void)
33{
e37f2f93 34 crc32init_le_generic(CRC32_POLY_LE, crc32table_le);
46c5801e
DW
35}
36
37static void crc32cinit_le(void)
38{
39 crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
40}
41
1da177e4
LT
42/**
43 * crc32init_be() - allocate and initialize BE table data
44 */
45static void crc32init_be(void)
46{
47 unsigned i, j;
48 uint32_t crc = 0x80000000;
49
5e3c1c48 50 crc32table_be[0] = 0;
1da177e4 51
5e3c1c48 52 for (i = 1; i < 256; i <<= 1) {
e37f2f93 53 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRC32_POLY_BE : 0);
1da177e4 54 for (j = 0; j < i; j++)
5e3c1c48 55 crc32table_be[i + j] = crc ^ crc32table_be[j];
1da177e4
LT
56 }
57}
58
5e3c1c48 59static void output_table(const uint32_t table[256])
1da177e4 60{
5e3c1c48
EB
61 int i;
62
63 for (i = 0; i < 256; i += 4) {
64 printf("\t0x%08x, 0x%08x, 0x%08x, 0x%08x,\n",
65 table[i], table[i + 1], table[i + 2], table[i + 3]);
1da177e4 66 }
1da177e4
LT
67}
68
69int main(int argc, char** argv)
70{
71 printf("/* this file is generated - do not edit */\n\n");
72
5e3c1c48
EB
73 crc32init_le();
74 printf("static const u32 ____cacheline_aligned crc32table_le[256] = {\n");
75 output_table(crc32table_le);
76 printf("};\n");
1da177e4 77
5e3c1c48
EB
78 crc32init_be();
79 printf("static const u32 ____cacheline_aligned crc32table_be[256] = {\n");
80 output_table(crc32table_be);
81 printf("};\n");
82
83 crc32cinit_le();
84 printf("static const u32 ____cacheline_aligned crc32ctable_le[256] = {\n");
85 output_table(crc32ctable_le);
86 printf("};\n");
1da177e4
LT
87
88 return 0;
89}