1 // SPDX-License-Identifier: GPL-2.0
3 * Generate lookup table for the table-driven CRC64 calculation.
5 * gen_crc64table is executed in kernel build time and generates
6 * lib/crc64table.h. This header is included by lib/crc64.c for
7 * the table-driven CRC64 calculation.
9 * See lib/crc64.c for more information about which specification
10 * and polynomial arithmetic that gen_crc64table.c follows to
11 * generate the lookup table.
13 * Copyright 2018 SUSE Linux.
14 * Author: Coly Li <colyli@suse.de>
19 #define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
20 #define CRC64_ROCKSOFT_POLY 0x9A6C9329AC4BC9B5ULL
22 static uint64_t crc64_table
[256] = {0};
23 static uint64_t crc64_rocksoft_table
[256] = {0};
25 static void generate_reflected_crc64_table(uint64_t table
[256], uint64_t poly
)
27 uint64_t i
, j
, c
, crc
;
29 for (i
= 0; i
< 256; i
++) {
33 for (j
= 0; j
< 8; j
++) {
34 if ((crc
^ (c
>> j
)) & 1)
35 crc
= (crc
>> 1) ^ poly
;
43 static void generate_crc64_table(uint64_t table
[256], uint64_t poly
)
45 uint64_t i
, j
, c
, crc
;
47 for (i
= 0; i
< 256; i
++) {
51 for (j
= 0; j
< 8; j
++) {
52 if ((crc
^ c
) & 0x8000000000000000ULL
)
53 crc
= (crc
<< 1) ^ poly
;
63 static void output_table(uint64_t table
[256])
67 for (i
= 0; i
< 256; i
++) {
68 printf("\t0x%016" PRIx64
"ULL", table
[i
]);
77 static void print_crc64_tables(void)
79 printf("/* this file is generated - do not edit */\n\n");
80 printf("#include <linux/types.h>\n");
81 printf("#include <linux/cache.h>\n\n");
82 printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
83 output_table(crc64_table
);
85 printf("\nstatic const u64 ____cacheline_aligned crc64rocksofttable[256] = {\n");
86 output_table(crc64_rocksoft_table
);
89 int main(int argc
, char *argv
[])
91 generate_crc64_table(crc64_table
, CRC64_ECMA182_POLY
);
92 generate_reflected_crc64_table(crc64_rocksoft_table
, CRC64_ROCKSOFT_POLY
);