1feba04fdSColy Li // SPDX-License-Identifier: GPL-2.0
2feba04fdSColy Li /*
3feba04fdSColy Li * Generate lookup table for the table-driven CRC64 calculation.
4feba04fdSColy Li *
5feba04fdSColy Li * gen_crc64table is executed in kernel build time and generates
6feba04fdSColy Li * lib/crc64table.h. This header is included by lib/crc64.c for
7feba04fdSColy Li * the table-driven CRC64 calculation.
8feba04fdSColy Li *
9feba04fdSColy Li * See lib/crc64.c for more information about which specification
10feba04fdSColy Li * and polynomial arithmetic that gen_crc64table.c follows to
11feba04fdSColy Li * generate the lookup table.
12feba04fdSColy Li *
13feba04fdSColy Li * Copyright 2018 SUSE Linux.
14feba04fdSColy Li * Author: Coly Li <colyli@suse.de>
15feba04fdSColy Li */
16feba04fdSColy Li #include <inttypes.h>
17feba04fdSColy Li #include <stdio.h>
18feba04fdSColy Li
19feba04fdSColy Li #define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
20*cbc0a40eSKeith Busch #define CRC64_ROCKSOFT_POLY 0x9A6C9329AC4BC9B5ULL
21feba04fdSColy Li
22feba04fdSColy Li static uint64_t crc64_table[256] = {0};
23*cbc0a40eSKeith Busch static uint64_t crc64_rocksoft_table[256] = {0};
24feba04fdSColy Li
generate_reflected_crc64_table(uint64_t table[256],uint64_t poly)25*cbc0a40eSKeith Busch static void generate_reflected_crc64_table(uint64_t table[256], uint64_t poly)
26*cbc0a40eSKeith Busch {
27*cbc0a40eSKeith Busch uint64_t i, j, c, crc;
28*cbc0a40eSKeith Busch
29*cbc0a40eSKeith Busch for (i = 0; i < 256; i++) {
30*cbc0a40eSKeith Busch crc = 0ULL;
31*cbc0a40eSKeith Busch c = i;
32*cbc0a40eSKeith Busch
33*cbc0a40eSKeith Busch for (j = 0; j < 8; j++) {
34*cbc0a40eSKeith Busch if ((crc ^ (c >> j)) & 1)
35*cbc0a40eSKeith Busch crc = (crc >> 1) ^ poly;
36*cbc0a40eSKeith Busch else
37*cbc0a40eSKeith Busch crc >>= 1;
38*cbc0a40eSKeith Busch }
39*cbc0a40eSKeith Busch table[i] = crc;
40*cbc0a40eSKeith Busch }
41*cbc0a40eSKeith Busch }
42*cbc0a40eSKeith Busch
generate_crc64_table(uint64_t table[256],uint64_t poly)43*cbc0a40eSKeith Busch static void generate_crc64_table(uint64_t table[256], uint64_t poly)
44feba04fdSColy Li {
45feba04fdSColy Li uint64_t i, j, c, crc;
46feba04fdSColy Li
47feba04fdSColy Li for (i = 0; i < 256; i++) {
48feba04fdSColy Li crc = 0;
49feba04fdSColy Li c = i << 56;
50feba04fdSColy Li
51feba04fdSColy Li for (j = 0; j < 8; j++) {
52feba04fdSColy Li if ((crc ^ c) & 0x8000000000000000ULL)
53*cbc0a40eSKeith Busch crc = (crc << 1) ^ poly;
54feba04fdSColy Li else
55feba04fdSColy Li crc <<= 1;
56feba04fdSColy Li c <<= 1;
57feba04fdSColy Li }
58feba04fdSColy Li
59*cbc0a40eSKeith Busch table[i] = crc;
60feba04fdSColy Li }
61feba04fdSColy Li }
62feba04fdSColy Li
output_table(uint64_t table[256])63*cbc0a40eSKeith Busch static void output_table(uint64_t table[256])
64feba04fdSColy Li {
65feba04fdSColy Li int i;
66feba04fdSColy Li
67feba04fdSColy Li for (i = 0; i < 256; i++) {
68*cbc0a40eSKeith Busch printf("\t0x%016" PRIx64 "ULL", table[i]);
69feba04fdSColy Li if (i & 0x1)
70feba04fdSColy Li printf(",\n");
71feba04fdSColy Li else
72feba04fdSColy Li printf(", ");
73feba04fdSColy Li }
74feba04fdSColy Li printf("};\n");
75feba04fdSColy Li }
76feba04fdSColy Li
print_crc64_tables(void)77*cbc0a40eSKeith Busch static void print_crc64_tables(void)
78*cbc0a40eSKeith Busch {
79*cbc0a40eSKeith Busch printf("/* this file is generated - do not edit */\n\n");
80*cbc0a40eSKeith Busch printf("#include <linux/types.h>\n");
81*cbc0a40eSKeith Busch printf("#include <linux/cache.h>\n\n");
82*cbc0a40eSKeith Busch printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
83*cbc0a40eSKeith Busch output_table(crc64_table);
84*cbc0a40eSKeith Busch
85*cbc0a40eSKeith Busch printf("\nstatic const u64 ____cacheline_aligned crc64rocksofttable[256] = {\n");
86*cbc0a40eSKeith Busch output_table(crc64_rocksoft_table);
87*cbc0a40eSKeith Busch }
88*cbc0a40eSKeith Busch
main(int argc,char * argv[])89feba04fdSColy Li int main(int argc, char *argv[])
90feba04fdSColy Li {
91*cbc0a40eSKeith Busch generate_crc64_table(crc64_table, CRC64_ECMA182_POLY);
92*cbc0a40eSKeith Busch generate_reflected_crc64_table(crc64_rocksoft_table, CRC64_ROCKSOFT_POLY);
93*cbc0a40eSKeith Busch print_crc64_tables();
94feba04fdSColy Li return 0;
95feba04fdSColy Li }
96