gen_crc32table.c 3.33 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2
#include <stdio.h>
3
#include "../include/linux/crc32poly.h"
4
#include "../include/generated/autoconf.h"
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9
#include "crc32defs.h"
#include <inttypes.h>

#define ENTRIES_PER_LINE 4

10 11 12
#if CRC_LE_BITS > 8
# define LE_TABLE_ROWS (CRC_LE_BITS/8)
# define LE_TABLE_SIZE 256
13
#else
14 15
# define LE_TABLE_ROWS 1
# define LE_TABLE_SIZE (1 << CRC_LE_BITS)
16 17
#endif

18 19 20
#if CRC_BE_BITS > 8
# define BE_TABLE_ROWS (CRC_BE_BITS/8)
# define BE_TABLE_SIZE 256
21
#else
22 23
# define BE_TABLE_ROWS 1
# define BE_TABLE_SIZE (1 << CRC_BE_BITS)
24
#endif
Linus Torvalds's avatar
Linus Torvalds committed
25

26 27
static uint32_t crc32table_le[LE_TABLE_ROWS][256];
static uint32_t crc32table_be[BE_TABLE_ROWS][256];
Darrick J. Wong's avatar
Darrick J. Wong committed
28
static uint32_t crc32ctable_le[LE_TABLE_ROWS][256];
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32 33 34 35 36

/**
 * crc32init_le() - allocate and initialize LE table data
 *
 * crc is the crc of the byte i; other entries are filled in based on the
 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
 *
 */
Darrick J. Wong's avatar
Darrick J. Wong committed
37 38
static void crc32init_le_generic(const uint32_t polynomial,
				 uint32_t (*tab)[256])
Linus Torvalds's avatar
Linus Torvalds committed
39 40 41 42
{
	unsigned i, j;
	uint32_t crc = 1;

Darrick J. Wong's avatar
Darrick J. Wong committed
43
	tab[0][0] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
44

45
	for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
Darrick J. Wong's avatar
Darrick J. Wong committed
46
		crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
Linus Torvalds's avatar
Linus Torvalds committed
47
		for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
Darrick J. Wong's avatar
Darrick J. Wong committed
48
			tab[0][i + j] = crc ^ tab[0][j];
Joakim Tjernlund's avatar
Joakim Tjernlund committed
49 50
	}
	for (i = 0; i < LE_TABLE_SIZE; i++) {
Darrick J. Wong's avatar
Darrick J. Wong committed
51
		crc = tab[0][i];
52
		for (j = 1; j < LE_TABLE_ROWS; j++) {
Darrick J. Wong's avatar
Darrick J. Wong committed
53 54
			crc = tab[0][crc & 0xff] ^ (crc >> 8);
			tab[j][i] = crc;
Joakim Tjernlund's avatar
Joakim Tjernlund committed
55
		}
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58
	}
}

Darrick J. Wong's avatar
Darrick J. Wong committed
59 60
static void crc32init_le(void)
{
61
	crc32init_le_generic(CRC32_POLY_LE, crc32table_le);
Darrick J. Wong's avatar
Darrick J. Wong committed
62 63 64 65 66 67 68
}

static void crc32cinit_le(void)
{
	crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
}

Linus Torvalds's avatar
Linus Torvalds committed
69 70 71 72 73 74 75 76
/**
 * crc32init_be() - allocate and initialize BE table data
 */
static void crc32init_be(void)
{
	unsigned i, j;
	uint32_t crc = 0x80000000;

Joakim Tjernlund's avatar
Joakim Tjernlund committed
77
	crc32table_be[0][0] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
78 79

	for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
80
		crc = (crc << 1) ^ ((crc & 0x80000000) ? CRC32_POLY_BE : 0);
Linus Torvalds's avatar
Linus Torvalds committed
81
		for (j = 0; j < i; j++)
Joakim Tjernlund's avatar
Joakim Tjernlund committed
82 83 84 85
			crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
	}
	for (i = 0; i < BE_TABLE_SIZE; i++) {
		crc = crc32table_be[0][i];
86
		for (j = 1; j < BE_TABLE_ROWS; j++) {
Joakim Tjernlund's avatar
Joakim Tjernlund committed
87 88 89
			crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
			crc32table_be[j][i] = crc;
		}
Linus Torvalds's avatar
Linus Torvalds committed
90 91 92
	}
}

93
static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
Linus Torvalds's avatar
Linus Torvalds committed
94
{
Joakim Tjernlund's avatar
Joakim Tjernlund committed
95
	int i, j;
Linus Torvalds's avatar
Linus Torvalds committed
96

97
	for (j = 0 ; j < rows; j++) {
Joakim Tjernlund's avatar
Joakim Tjernlund committed
98 99 100 101 102 103 104
		printf("{");
		for (i = 0; i < len - 1; i++) {
			if (i % ENTRIES_PER_LINE == 0)
				printf("\n");
			printf("%s(0x%8.8xL), ", trans, table[j][i]);
		}
		printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
Linus Torvalds's avatar
Linus Torvalds committed
105 106 107 108 109 110 111 112 113
	}
}

int main(int argc, char** argv)
{
	printf("/* this file is generated - do not edit */\n\n");

	if (CRC_LE_BITS > 1) {
		crc32init_le();
114
		printf("static const u32 ____cacheline_aligned "
115 116 117 118
		       "crc32table_le[%d][%d] = {",
		       LE_TABLE_ROWS, LE_TABLE_SIZE);
		output_table(crc32table_le, LE_TABLE_ROWS,
			     LE_TABLE_SIZE, "tole");
Linus Torvalds's avatar
Linus Torvalds committed
119 120 121 122 123
		printf("};\n");
	}

	if (CRC_BE_BITS > 1) {
		crc32init_be();
124
		printf("static const u32 ____cacheline_aligned "
125 126 127 128
		       "crc32table_be[%d][%d] = {",
		       BE_TABLE_ROWS, BE_TABLE_SIZE);
		output_table(crc32table_be, LE_TABLE_ROWS,
			     BE_TABLE_SIZE, "tobe");
Linus Torvalds's avatar
Linus Torvalds committed
129 130
		printf("};\n");
	}
Darrick J. Wong's avatar
Darrick J. Wong committed
131 132
	if (CRC_LE_BITS > 1) {
		crc32cinit_le();
133
		printf("static const u32 ____cacheline_aligned "
Darrick J. Wong's avatar
Darrick J. Wong committed
134 135 136 137 138 139
		       "crc32ctable_le[%d][%d] = {",
		       LE_TABLE_ROWS, LE_TABLE_SIZE);
		output_table(crc32ctable_le, LE_TABLE_ROWS,
			     LE_TABLE_SIZE, "tole");
		printf("};\n");
	}
Linus Torvalds's avatar
Linus Torvalds committed
140 141 142

	return 0;
}