aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/gen_crc32c_table.c
blob: 1c5af8858dbd7e0132d14a0a7b5a0bb7242951f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: GPL-2.0+
/*
 * Generate a table for CRC-32C calculation.
 *
 * Copyright (C) 2018 Google LLC
 *
 * Written by Eric Biggers.
 */

#include <stdint.h>
#include <stdio.h>

/*
 * This is the CRC-32C (Castagnoli) polynomial: x^32+x^28+x^27+x^26+x^25+x^23+
 * x^22+x^20+x^19+x^18+x^14+x^13+x^11+x^10+x^9+x^8+x^6+x^0, with the polynomial
 * coefficients mapped to bits using the "little endian" convention.
 */
#define CRC32C_POLY_LE	0x82F63B78

static uint32_t crc32c_update_bit(uint32_t remainder, uint8_t bit)
{
	return (remainder >> 1) ^
		(((remainder ^ bit) & 1) ? CRC32C_POLY_LE : 0);
}

static uint32_t crc32c_update_byte(uint32_t remainder, uint8_t byte)
{
	int bit;

	for (bit = 0; bit < 8; bit++, byte >>= 1)
		remainder = crc32c_update_bit(remainder, byte & 1);
	return remainder;
}

static uint32_t crc32c_table[256];

int main(void)
{
	int i, j;

	for (i = 0; i < 256; i++)
		crc32c_table[i] = crc32c_update_byte(0, i);

	printf("/*\n");
	printf(" * crc32c_table.h - data table to accelerate CRC-32C computation\n");
	printf(" *\n");
	printf(" * This file was automatically generated by scripts/gen_crc32c_table.c\n");
	printf(" */\n");
	printf("\n");
	printf("static const u32 crc32c_table[] = {\n");
	for (i = 0; i < 64; i++) {
		printf("\t");
		for (j = 0; j < 4; j++) {
			printf("0x%08x,", crc32c_table[i * 4 + j]);
			if (j != 3)
				printf(" ");
		}
		printf("\n");
	}
	printf("};\n");
	return 0;
}