Back to home page

LXR

 
 

    


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