^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Generate lookup table for the table-driven CRC64 calculation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * gen_crc64table is executed in kernel build time and generates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * lib/crc64table.h. This header is included by lib/crc64.c for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * the table-driven CRC64 calculation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * See lib/crc64.c for more information about which specification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * and polynomial arithmetic that gen_crc64table.c follows to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * generate the lookup table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright 2018 SUSE Linux.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Author: Coly Li <colyli@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static uint64_t crc64_table[256] = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static void generate_crc64_table(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) uint64_t i, j, c, crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) for (i = 0; i < 256; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) crc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) c = i << 56;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) for (j = 0; j < 8; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if ((crc ^ c) & 0x8000000000000000ULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) crc = (crc << 1) ^ CRC64_ECMA182_POLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) crc <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) c <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) crc64_table[i] = crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void print_crc64_table(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) printf("/* this file is generated - do not edit */\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) printf("#include <linux/types.h>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) printf("#include <linux/cache.h>\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) for (i = 0; i < 256; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) printf("\t0x%016" PRIx64 "ULL", crc64_table[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (i & 0x1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) printf(",\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) printf(", ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) printf("};\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) generate_crc64_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) print_crc64_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }