1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Generate a table for CRC-32C calculation.
4 *
5 * Copyright (C) 2018 Google LLC
6 *
7 * Written by Eric Biggers.
8 */
9
10 #include <stdint.h>
11 #include <stdio.h>
12
13 /*
14 * This is the CRC-32C (Castagnoli) polynomial: x^32+x^28+x^27+x^26+x^25+x^23+
15 * 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
16 * coefficients mapped to bits using the "little endian" convention.
17 */
18 #define CRC32C_POLY_LE 0x82F63B78
19
crc32c_update_bit(uint32_t remainder,uint8_t bit)20 static uint32_t crc32c_update_bit(uint32_t remainder, uint8_t bit)
21 {
22 return (remainder >> 1) ^
23 (((remainder ^ bit) & 1) ? CRC32C_POLY_LE : 0);
24 }
25
crc32c_update_byte(uint32_t remainder,uint8_t byte)26 static uint32_t crc32c_update_byte(uint32_t remainder, uint8_t byte)
27 {
28 int bit;
29
30 for (bit = 0; bit < 8; bit++, byte >>= 1)
31 remainder = crc32c_update_bit(remainder, byte & 1);
32 return remainder;
33 }
34
35 static uint32_t crc32c_table[256];
36
main(void)37 int main(void)
38 {
39 int i, j;
40
41 for (i = 0; i < 256; i++)
42 crc32c_table[i] = crc32c_update_byte(0, i);
43
44 printf("/*\n");
45 printf(" * crc32c_table.h - data table to accelerate CRC-32C computation\n");
46 printf(" *\n");
47 printf(" * This file was automatically generated by scripts/gen_crc32c_table.c\n");
48 printf(" */\n");
49 printf("\n");
50 printf("#include <stdint.h>\n");
51 printf("\n");
52 printf("static const uint32_t crc32c_table[] = {\n");
53 for (i = 0; i < 64; i++) {
54 printf("\t");
55 for (j = 0; j < 4; j++) {
56 printf("0x%08x,", crc32c_table[i * 4 + j]);
57 if (j != 3)
58 printf(" ");
59 }
60 printf("\n");
61 }
62 printf("};\n");
63 return 0;
64 }
65