gen_crc32table.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <stdio.h>
  2. #include "crc32defs.h"
  3. #include <inttypes.h>
  4. #define ENTRIES_PER_LINE 4
  5. #define LE_TABLE_SIZE (1 << CRC_LE_BITS)
  6. #define BE_TABLE_SIZE (1 << CRC_BE_BITS)
  7. static uint32_t crc32table_le[4][LE_TABLE_SIZE];
  8. static uint32_t crc32table_be[4][BE_TABLE_SIZE];
  9. /**
  10. * crc32init_le() - allocate and initialize LE table data
  11. *
  12. * crc is the crc of the byte i; other entries are filled in based on the
  13. * fact that crctable[i^j] = crctable[i] ^ crctable[j].
  14. *
  15. */
  16. static void crc32init_le(void)
  17. {
  18. unsigned i, j;
  19. uint32_t crc = 1;
  20. crc32table_le[0][0] = 0;
  21. for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) {
  22. crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  23. for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
  24. crc32table_le[0][i + j] = crc ^ crc32table_le[0][j];
  25. }
  26. for (i = 0; i < LE_TABLE_SIZE; i++) {
  27. crc = crc32table_le[0][i];
  28. for (j = 1; j < 4; j++) {
  29. crc = crc32table_le[0][crc & 0xff] ^ (crc >> 8);
  30. crc32table_le[j][i] = crc;
  31. }
  32. }
  33. }
  34. /**
  35. * crc32init_be() - allocate and initialize BE table data
  36. */
  37. static void crc32init_be(void)
  38. {
  39. unsigned i, j;
  40. uint32_t crc = 0x80000000;
  41. crc32table_be[0][0] = 0;
  42. for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
  43. crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
  44. for (j = 0; j < i; j++)
  45. crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
  46. }
  47. for (i = 0; i < BE_TABLE_SIZE; i++) {
  48. crc = crc32table_be[0][i];
  49. for (j = 1; j < 4; j++) {
  50. crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
  51. crc32table_be[j][i] = crc;
  52. }
  53. }
  54. }
  55. static void output_table(uint32_t table[4][256], int len, char *trans)
  56. {
  57. int i, j;
  58. for (j = 0 ; j < 4; j++) {
  59. printf("{");
  60. for (i = 0; i < len - 1; i++) {
  61. if (i % ENTRIES_PER_LINE == 0)
  62. printf("\n");
  63. printf("%s(0x%8.8xL), ", trans, table[j][i]);
  64. }
  65. printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
  66. }
  67. }
  68. int main(int argc, char** argv)
  69. {
  70. printf("/* this file is generated - do not edit */\n\n");
  71. if (CRC_LE_BITS > 1) {
  72. crc32init_le();
  73. printf("static const u32 crc32table_le[4][256] = {");
  74. output_table(crc32table_le, LE_TABLE_SIZE, "tole");
  75. printf("};\n");
  76. }
  77. if (CRC_BE_BITS > 1) {
  78. crc32init_be();
  79. printf("static const u32 crc32table_be[4][256] = {");
  80. output_table(crc32table_be, BE_TABLE_SIZE, "tobe");
  81. printf("};\n");
  82. }
  83. return 0;
  84. }