18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci#include <stdio.h> 38c2ecf20Sopenharmony_ci#include "../include/linux/crc32poly.h" 48c2ecf20Sopenharmony_ci#include "../include/generated/autoconf.h" 58c2ecf20Sopenharmony_ci#include "crc32defs.h" 68c2ecf20Sopenharmony_ci#include <inttypes.h> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#define ENTRIES_PER_LINE 4 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#if CRC_LE_BITS > 8 118c2ecf20Sopenharmony_ci# define LE_TABLE_ROWS (CRC_LE_BITS/8) 128c2ecf20Sopenharmony_ci# define LE_TABLE_SIZE 256 138c2ecf20Sopenharmony_ci#else 148c2ecf20Sopenharmony_ci# define LE_TABLE_ROWS 1 158c2ecf20Sopenharmony_ci# define LE_TABLE_SIZE (1 << CRC_LE_BITS) 168c2ecf20Sopenharmony_ci#endif 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#if CRC_BE_BITS > 8 198c2ecf20Sopenharmony_ci# define BE_TABLE_ROWS (CRC_BE_BITS/8) 208c2ecf20Sopenharmony_ci# define BE_TABLE_SIZE 256 218c2ecf20Sopenharmony_ci#else 228c2ecf20Sopenharmony_ci# define BE_TABLE_ROWS 1 238c2ecf20Sopenharmony_ci# define BE_TABLE_SIZE (1 << CRC_BE_BITS) 248c2ecf20Sopenharmony_ci#endif 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic uint32_t crc32table_le[LE_TABLE_ROWS][256]; 278c2ecf20Sopenharmony_cistatic uint32_t crc32table_be[BE_TABLE_ROWS][256]; 288c2ecf20Sopenharmony_cistatic uint32_t crc32ctable_le[LE_TABLE_ROWS][256]; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/** 318c2ecf20Sopenharmony_ci * crc32init_le() - allocate and initialize LE table data 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci * crc is the crc of the byte i; other entries are filled in based on the 348c2ecf20Sopenharmony_ci * fact that crctable[i^j] = crctable[i] ^ crctable[j]. 358c2ecf20Sopenharmony_ci * 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_cistatic void crc32init_le_generic(const uint32_t polynomial, 388c2ecf20Sopenharmony_ci uint32_t (*tab)[256]) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci unsigned i, j; 418c2ecf20Sopenharmony_ci uint32_t crc = 1; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci tab[0][0] = 0; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) { 468c2ecf20Sopenharmony_ci crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0); 478c2ecf20Sopenharmony_ci for (j = 0; j < LE_TABLE_SIZE; j += 2 * i) 488c2ecf20Sopenharmony_ci tab[0][i + j] = crc ^ tab[0][j]; 498c2ecf20Sopenharmony_ci } 508c2ecf20Sopenharmony_ci for (i = 0; i < LE_TABLE_SIZE; i++) { 518c2ecf20Sopenharmony_ci crc = tab[0][i]; 528c2ecf20Sopenharmony_ci for (j = 1; j < LE_TABLE_ROWS; j++) { 538c2ecf20Sopenharmony_ci crc = tab[0][crc & 0xff] ^ (crc >> 8); 548c2ecf20Sopenharmony_ci tab[j][i] = crc; 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci } 578c2ecf20Sopenharmony_ci} 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cistatic void crc32init_le(void) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci crc32init_le_generic(CRC32_POLY_LE, crc32table_le); 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic void crc32cinit_le(void) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le); 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci/** 708c2ecf20Sopenharmony_ci * crc32init_be() - allocate and initialize BE table data 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_cistatic void crc32init_be(void) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci unsigned i, j; 758c2ecf20Sopenharmony_ci uint32_t crc = 0x80000000; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci crc32table_be[0][0] = 0; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci for (i = 1; i < BE_TABLE_SIZE; i <<= 1) { 808c2ecf20Sopenharmony_ci crc = (crc << 1) ^ ((crc & 0x80000000) ? CRC32_POLY_BE : 0); 818c2ecf20Sopenharmony_ci for (j = 0; j < i; j++) 828c2ecf20Sopenharmony_ci crc32table_be[0][i + j] = crc ^ crc32table_be[0][j]; 838c2ecf20Sopenharmony_ci } 848c2ecf20Sopenharmony_ci for (i = 0; i < BE_TABLE_SIZE; i++) { 858c2ecf20Sopenharmony_ci crc = crc32table_be[0][i]; 868c2ecf20Sopenharmony_ci for (j = 1; j < BE_TABLE_ROWS; j++) { 878c2ecf20Sopenharmony_ci crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8); 888c2ecf20Sopenharmony_ci crc32table_be[j][i] = crc; 898c2ecf20Sopenharmony_ci } 908c2ecf20Sopenharmony_ci } 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic void output_table(uint32_t (*table)[256], int rows, int len, char *trans) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci int i, j; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci for (j = 0 ; j < rows; j++) { 988c2ecf20Sopenharmony_ci printf("{"); 998c2ecf20Sopenharmony_ci for (i = 0; i < len - 1; i++) { 1008c2ecf20Sopenharmony_ci if (i % ENTRIES_PER_LINE == 0) 1018c2ecf20Sopenharmony_ci printf("\n"); 1028c2ecf20Sopenharmony_ci printf("%s(0x%8.8xL), ", trans, table[j][i]); 1038c2ecf20Sopenharmony_ci } 1048c2ecf20Sopenharmony_ci printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]); 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ciint main(int argc, char** argv) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci printf("/* this file is generated - do not edit */\n\n"); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci if (CRC_LE_BITS > 1) { 1138c2ecf20Sopenharmony_ci crc32init_le(); 1148c2ecf20Sopenharmony_ci printf("static const u32 ____cacheline_aligned " 1158c2ecf20Sopenharmony_ci "crc32table_le[%d][%d] = {", 1168c2ecf20Sopenharmony_ci LE_TABLE_ROWS, LE_TABLE_SIZE); 1178c2ecf20Sopenharmony_ci output_table(crc32table_le, LE_TABLE_ROWS, 1188c2ecf20Sopenharmony_ci LE_TABLE_SIZE, "tole"); 1198c2ecf20Sopenharmony_ci printf("};\n"); 1208c2ecf20Sopenharmony_ci } 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci if (CRC_BE_BITS > 1) { 1238c2ecf20Sopenharmony_ci crc32init_be(); 1248c2ecf20Sopenharmony_ci printf("static const u32 ____cacheline_aligned " 1258c2ecf20Sopenharmony_ci "crc32table_be[%d][%d] = {", 1268c2ecf20Sopenharmony_ci BE_TABLE_ROWS, BE_TABLE_SIZE); 1278c2ecf20Sopenharmony_ci output_table(crc32table_be, LE_TABLE_ROWS, 1288c2ecf20Sopenharmony_ci BE_TABLE_SIZE, "tobe"); 1298c2ecf20Sopenharmony_ci printf("};\n"); 1308c2ecf20Sopenharmony_ci } 1318c2ecf20Sopenharmony_ci if (CRC_LE_BITS > 1) { 1328c2ecf20Sopenharmony_ci crc32cinit_le(); 1338c2ecf20Sopenharmony_ci printf("static const u32 ____cacheline_aligned " 1348c2ecf20Sopenharmony_ci "crc32ctable_le[%d][%d] = {", 1358c2ecf20Sopenharmony_ci LE_TABLE_ROWS, LE_TABLE_SIZE); 1368c2ecf20Sopenharmony_ci output_table(crc32ctable_le, LE_TABLE_ROWS, 1378c2ecf20Sopenharmony_ci LE_TABLE_SIZE, "tole"); 1388c2ecf20Sopenharmony_ci printf("};\n"); 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci return 0; 1428c2ecf20Sopenharmony_ci} 143