gen_crc32ctable.c revision db8bbf27c9eeaa9c1803fb52a6cc7a60c5250c7e
1#include <stdio.h> 2#include "crc32c_defs.h" 3#include <inttypes.h> 4 5#define ENTRIES_PER_LINE 4 6 7#if CRC_LE_BITS <= 8 8#define LE_TABLE_SIZE (1 << CRC_LE_BITS) 9#else 10#define LE_TABLE_SIZE 256 11#endif 12 13#if CRC_BE_BITS <= 8 14#define BE_TABLE_SIZE (1 << CRC_BE_BITS) 15#else 16#define BE_TABLE_SIZE 256 17#endif 18 19static uint32_t crc32ctable_le[8][256]; 20static uint32_t crc32ctable_be[8][256]; 21 22/** 23 * crc32cinit_le() - allocate and initialize LE table data 24 * 25 * crc is the crc of the byte i; other entries are filled in based on the 26 * fact that crctable[i^j] = crctable[i] ^ crctable[j]. 27 * 28 */ 29static void crc32cinit_le(void) 30{ 31 unsigned i, j; 32 uint32_t crc = 1; 33 34 crc32ctable_le[0][0] = 0; 35 36 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) { 37 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); 38 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i) 39 crc32ctable_le[0][i + j] = crc ^ crc32ctable_le[0][j]; 40 } 41 for (i = 0; i < LE_TABLE_SIZE; i++) { 42 crc = crc32ctable_le[0][i]; 43 for (j = 1; j < 8; j++) { 44 crc = crc32ctable_le[0][crc & 0xff] ^ (crc >> 8); 45 crc32ctable_le[j][i] = crc; 46 } 47 } 48} 49 50/** 51 * crc32cinit_be() - allocate and initialize BE table data 52 */ 53static void crc32cinit_be(void) 54{ 55 unsigned i, j; 56 uint32_t crc = 0x80000000; 57 58 crc32ctable_be[0][0] = 0; 59 60 for (i = 1; i < BE_TABLE_SIZE; i <<= 1) { 61 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0); 62 for (j = 0; j < i; j++) 63 crc32ctable_be[0][i + j] = crc ^ crc32ctable_be[0][j]; 64 } 65 for (i = 0; i < BE_TABLE_SIZE; i++) { 66 crc = crc32ctable_be[0][i]; 67 for (j = 1; j < 8; j++) { 68 crc = crc32ctable_be[0][(crc >> 24) & 0xff] ^ 69 (crc << 8); 70 crc32ctable_be[j][i] = crc; 71 } 72 } 73} 74 75static void output_table(uint32_t table[8][256], int len, char trans) 76{ 77 int i, j; 78 79 for (j = 0 ; j < 8; j++) { 80 printf("static const uint32_t t%d_%ce[] = {", j, trans); 81 for (i = 0; i < len - 1; i++) { 82 if ((i % ENTRIES_PER_LINE) == 0) 83 printf("\n"); 84 printf("to%ce(0x%8.8xL),", trans, table[j][i]); 85 if ((i % ENTRIES_PER_LINE) != (ENTRIES_PER_LINE - 1)) 86 printf(" "); 87 } 88 printf("to%ce(0x%8.8xL)};\n\n", trans, table[j][len - 1]); 89 90 if (trans == 'l') { 91 if ((j+1)*8 >= CRC_LE_BITS) 92 break; 93 } else { 94 if ((j+1)*8 >= CRC_BE_BITS) 95 break; 96 } 97 } 98} 99 100int main(int argc, char **argv) 101{ 102 printf("/*\n"); 103 printf(" * crc32ctable.h - CRC32c tables\n"); 104 printf(" * this file is generated - do not edit\n"); 105 printf(" * # gen_crc32ctable > crc32c_table.h\n"); 106 printf(" * with\n"); 107 printf(" * CRC_LE_BITS = %d\n", CRC_LE_BITS); 108 printf(" * CRC_BE_BITS = %d\n", CRC_BE_BITS); 109 printf(" */\n"); 110 printf("#include <stdint.h>\n"); 111 112 if (CRC_LE_BITS > 1) { 113 crc32cinit_le(); 114 output_table(crc32ctable_le, LE_TABLE_SIZE, 'l'); 115 } 116 117 if (CRC_BE_BITS > 1) { 118 crc32cinit_be(); 119 output_table(crc32ctable_be, BE_TABLE_SIZE, 'b'); 120 } 121 122 return 0; 123} 124