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