Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <stdio.h>
0003 #include "../include/linux/crc32poly.h"
0004 #include "../include/generated/autoconf.h"
0005 #include "crc32defs.h"
0006 #include <inttypes.h>
0007 
0008 #define ENTRIES_PER_LINE 4
0009 
0010 #if CRC_LE_BITS > 8
0011 # define LE_TABLE_ROWS (CRC_LE_BITS/8)
0012 # define LE_TABLE_SIZE 256
0013 #else
0014 # define LE_TABLE_ROWS 1
0015 # define LE_TABLE_SIZE (1 << CRC_LE_BITS)
0016 #endif
0017 
0018 #if CRC_BE_BITS > 8
0019 # define BE_TABLE_ROWS (CRC_BE_BITS/8)
0020 # define BE_TABLE_SIZE 256
0021 #else
0022 # define BE_TABLE_ROWS 1
0023 # define BE_TABLE_SIZE (1 << CRC_BE_BITS)
0024 #endif
0025 
0026 static uint32_t crc32table_le[LE_TABLE_ROWS][256];
0027 static uint32_t crc32table_be[BE_TABLE_ROWS][256];
0028 static uint32_t crc32ctable_le[LE_TABLE_ROWS][256];
0029 
0030 /**
0031  * crc32init_le() - allocate and initialize LE table data
0032  *
0033  * crc is the crc of the byte i; other entries are filled in based on the
0034  * fact that crctable[i^j] = crctable[i] ^ crctable[j].
0035  *
0036  */
0037 static void crc32init_le_generic(const uint32_t polynomial,
0038                  uint32_t (*tab)[256])
0039 {
0040     unsigned i, j;
0041     uint32_t crc = 1;
0042 
0043     tab[0][0] = 0;
0044 
0045     for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
0046         crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
0047         for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
0048             tab[0][i + j] = crc ^ tab[0][j];
0049     }
0050     for (i = 0; i < LE_TABLE_SIZE; i++) {
0051         crc = tab[0][i];
0052         for (j = 1; j < LE_TABLE_ROWS; j++) {
0053             crc = tab[0][crc & 0xff] ^ (crc >> 8);
0054             tab[j][i] = crc;
0055         }
0056     }
0057 }
0058 
0059 static void crc32init_le(void)
0060 {
0061     crc32init_le_generic(CRC32_POLY_LE, crc32table_le);
0062 }
0063 
0064 static void crc32cinit_le(void)
0065 {
0066     crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
0067 }
0068 
0069 /**
0070  * crc32init_be() - allocate and initialize BE table data
0071  */
0072 static void crc32init_be(void)
0073 {
0074     unsigned i, j;
0075     uint32_t crc = 0x80000000;
0076 
0077     crc32table_be[0][0] = 0;
0078 
0079     for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
0080         crc = (crc << 1) ^ ((crc & 0x80000000) ? CRC32_POLY_BE : 0);
0081         for (j = 0; j < i; j++)
0082             crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
0083     }
0084     for (i = 0; i < BE_TABLE_SIZE; i++) {
0085         crc = crc32table_be[0][i];
0086         for (j = 1; j < BE_TABLE_ROWS; j++) {
0087             crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
0088             crc32table_be[j][i] = crc;
0089         }
0090     }
0091 }
0092 
0093 static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
0094 {
0095     int i, j;
0096 
0097     for (j = 0 ; j < rows; j++) {
0098         printf("{");
0099         for (i = 0; i < len - 1; i++) {
0100             if (i % ENTRIES_PER_LINE == 0)
0101                 printf("\n");
0102             printf("%s(0x%8.8xL), ", trans, table[j][i]);
0103         }
0104         printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
0105     }
0106 }
0107 
0108 int main(int argc, char** argv)
0109 {
0110     printf("/* this file is generated - do not edit */\n\n");
0111 
0112     if (CRC_LE_BITS > 1) {
0113         crc32init_le();
0114         printf("static const u32 ____cacheline_aligned "
0115                "crc32table_le[%d][%d] = {",
0116                LE_TABLE_ROWS, LE_TABLE_SIZE);
0117         output_table(crc32table_le, LE_TABLE_ROWS,
0118                  LE_TABLE_SIZE, "tole");
0119         printf("};\n");
0120     }
0121 
0122     if (CRC_BE_BITS > 1) {
0123         crc32init_be();
0124         printf("static const u32 ____cacheline_aligned "
0125                "crc32table_be[%d][%d] = {",
0126                BE_TABLE_ROWS, BE_TABLE_SIZE);
0127         output_table(crc32table_be, LE_TABLE_ROWS,
0128                  BE_TABLE_SIZE, "tobe");
0129         printf("};\n");
0130     }
0131     if (CRC_LE_BITS > 1) {
0132         crc32cinit_le();
0133         printf("static const u32 ____cacheline_aligned "
0134                "crc32ctable_le[%d][%d] = {",
0135                LE_TABLE_ROWS, LE_TABLE_SIZE);
0136         output_table(crc32ctable_le, LE_TABLE_ROWS,
0137                  LE_TABLE_SIZE, "tole");
0138         printf("};\n");
0139     }
0140 
0141     return 0;
0142 }