18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Generate lookup table for the table-driven CRC64 calculation.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * gen_crc64table is executed in kernel build time and generates
68c2ecf20Sopenharmony_ci * lib/crc64table.h. This header is included by lib/crc64.c for
78c2ecf20Sopenharmony_ci * the table-driven CRC64 calculation.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * See lib/crc64.c for more information about which specification
108c2ecf20Sopenharmony_ci * and polynomial arithmetic that gen_crc64table.c follows to
118c2ecf20Sopenharmony_ci * generate the lookup table.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Copyright 2018 SUSE Linux.
148c2ecf20Sopenharmony_ci *   Author: Coly Li <colyli@suse.de>
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci#include <inttypes.h>
178c2ecf20Sopenharmony_ci#include <stdio.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic uint64_t crc64_table[256] = {0};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic void generate_crc64_table(void)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	uint64_t i, j, c, crc;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	for (i = 0; i < 256; i++) {
288c2ecf20Sopenharmony_ci		crc = 0;
298c2ecf20Sopenharmony_ci		c = i << 56;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci		for (j = 0; j < 8; j++) {
328c2ecf20Sopenharmony_ci			if ((crc ^ c) & 0x8000000000000000ULL)
338c2ecf20Sopenharmony_ci				crc = (crc << 1) ^ CRC64_ECMA182_POLY;
348c2ecf20Sopenharmony_ci			else
358c2ecf20Sopenharmony_ci				crc <<= 1;
368c2ecf20Sopenharmony_ci			c <<= 1;
378c2ecf20Sopenharmony_ci		}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci		crc64_table[i] = crc;
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void print_crc64_table(void)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	int i;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	printf("/* this file is generated - do not edit */\n\n");
488c2ecf20Sopenharmony_ci	printf("#include <linux/types.h>\n");
498c2ecf20Sopenharmony_ci	printf("#include <linux/cache.h>\n\n");
508c2ecf20Sopenharmony_ci	printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
518c2ecf20Sopenharmony_ci	for (i = 0; i < 256; i++) {
528c2ecf20Sopenharmony_ci		printf("\t0x%016" PRIx64 "ULL", crc64_table[i]);
538c2ecf20Sopenharmony_ci		if (i & 0x1)
548c2ecf20Sopenharmony_ci			printf(",\n");
558c2ecf20Sopenharmony_ci		else
568c2ecf20Sopenharmony_ci			printf(", ");
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci	printf("};\n");
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ciint main(int argc, char *argv[])
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	generate_crc64_table();
648c2ecf20Sopenharmony_ci	print_crc64_table();
658c2ecf20Sopenharmony_ci	return 0;
668c2ecf20Sopenharmony_ci}
67