gen_crc32table.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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[LE_TABLE_SIZE];
  8. static uint32_t crc32table_be[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;
  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[i + j] = crc ^ crc32table_le[j];
  25. }
  26. }
  27. /**
  28. * crc32init_be() - allocate and initialize BE table data
  29. */
  30. static void crc32init_be(void)
  31. {
  32. unsigned i, j;
  33. uint32_t crc = 0x80000000;
  34. crc32table_be[0] = 0;
  35. for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
  36. crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
  37. for (j = 0; j < i; j++)
  38. crc32table_be[i + j] = crc ^ crc32table_be[j];
  39. }
  40. }
  41. static void output_table(uint32_t table[], int len, char *trans)
  42. {
  43. int i;
  44. for (i = 0; i < len - 1; i++) {
  45. if (i % ENTRIES_PER_LINE == 0)
  46. printf("\n");
  47. printf("%s(0x%8.8xL), ", trans, table[i]);
  48. }
  49. printf("%s(0x%8.8xL)\n", trans, table[len - 1]);
  50. }
  51. int main(int argc, char** argv)
  52. {
  53. printf("/* this file is generated - do not edit */\n\n");
  54. if (CRC_LE_BITS > 1) {
  55. crc32init_le();
  56. printf("static const u32 crc32table_le[] = {");
  57. output_table(crc32table_le, LE_TABLE_SIZE, "tole");
  58. printf("};\n");
  59. }
  60. if (CRC_BE_BITS > 1) {
  61. crc32init_be();
  62. printf("static const u32 crc32table_be[] = {");
  63. output_table(crc32table_be, BE_TABLE_SIZE, "tobe");
  64. printf("};\n");
  65. }
  66. return 0;
  67. }