C++程序  |  65行  |  1.46 KB

// 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("#include <stdint.h>\n");
	printf("\n");
	printf("static const uint32_t 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;
}