Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Generate lookup table for the table-driven CRC64 calculation.
0004  *
0005  * gen_crc64table is executed in kernel build time and generates
0006  * lib/crc64table.h. This header is included by lib/crc64.c for
0007  * the table-driven CRC64 calculation.
0008  *
0009  * See lib/crc64.c for more information about which specification
0010  * and polynomial arithmetic that gen_crc64table.c follows to
0011  * generate the lookup table.
0012  *
0013  * Copyright 2018 SUSE Linux.
0014  *   Author: Coly Li <colyli@suse.de>
0015  */
0016 #include <inttypes.h>
0017 #include <stdio.h>
0018 
0019 #define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
0020 #define CRC64_ROCKSOFT_POLY 0x9A6C9329AC4BC9B5ULL
0021 
0022 static uint64_t crc64_table[256] = {0};
0023 static uint64_t crc64_rocksoft_table[256] = {0};
0024 
0025 static void generate_reflected_crc64_table(uint64_t table[256], uint64_t poly)
0026 {
0027     uint64_t i, j, c, crc;
0028 
0029     for (i = 0; i < 256; i++) {
0030         crc = 0ULL;
0031         c = i;
0032 
0033         for (j = 0; j < 8; j++) {
0034             if ((crc ^ (c >> j)) & 1)
0035                 crc = (crc >> 1) ^ poly;
0036             else
0037                 crc >>= 1;
0038         }
0039         table[i] = crc;
0040     }
0041 }
0042 
0043 static void generate_crc64_table(uint64_t table[256], uint64_t poly)
0044 {
0045     uint64_t i, j, c, crc;
0046 
0047     for (i = 0; i < 256; i++) {
0048         crc = 0;
0049         c = i << 56;
0050 
0051         for (j = 0; j < 8; j++) {
0052             if ((crc ^ c) & 0x8000000000000000ULL)
0053                 crc = (crc << 1) ^ poly;
0054             else
0055                 crc <<= 1;
0056             c <<= 1;
0057         }
0058 
0059         table[i] = crc;
0060     }
0061 }
0062 
0063 static void output_table(uint64_t table[256])
0064 {
0065     int i;
0066 
0067     for (i = 0; i < 256; i++) {
0068         printf("\t0x%016" PRIx64 "ULL", table[i]);
0069         if (i & 0x1)
0070             printf(",\n");
0071         else
0072             printf(", ");
0073     }
0074     printf("};\n");
0075 }
0076 
0077 static void print_crc64_tables(void)
0078 {
0079     printf("/* this file is generated - do not edit */\n\n");
0080     printf("#include <linux/types.h>\n");
0081     printf("#include <linux/cache.h>\n\n");
0082     printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
0083     output_table(crc64_table);
0084 
0085     printf("\nstatic const u64 ____cacheline_aligned crc64rocksofttable[256] = {\n");
0086     output_table(crc64_rocksoft_table);
0087 }
0088 
0089 int main(int argc, char *argv[])
0090 {
0091     generate_crc64_table(crc64_table, CRC64_ECMA182_POLY);
0092     generate_reflected_crc64_table(crc64_rocksoft_table, CRC64_ROCKSOFT_POLY);
0093     print_crc64_tables();
0094     return 0;
0095 }