gen_crc32table.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <stdio.h>
  2. #include "../include/generated/autoconf.h"
  3. #include "crc32defs.h"
  4. #include <inttypes.h>
  5. #define ENTRIES_PER_LINE 4
  6. #if CRC_LE_BITS > 8
  7. # define LE_TABLE_ROWS (CRC_LE_BITS/8)
  8. # define LE_TABLE_SIZE 256
  9. #else
  10. # define LE_TABLE_ROWS 1
  11. # define LE_TABLE_SIZE (1 << CRC_LE_BITS)
  12. #endif
  13. #if CRC_BE_BITS > 8
  14. # define BE_TABLE_ROWS (CRC_BE_BITS/8)
  15. # define BE_TABLE_SIZE 256
  16. #else
  17. # define BE_TABLE_ROWS 1
  18. # define BE_TABLE_SIZE (1 << CRC_BE_BITS)
  19. #endif
  20. static uint32_t crc32table_le[LE_TABLE_ROWS][256];
  21. static uint32_t crc32table_be[BE_TABLE_ROWS][256];
  22. /**
  23. * crc32init_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. */
  29. static void crc32init_le(void)
  30. {
  31. unsigned i, j;
  32. uint32_t crc = 1;
  33. crc32table_le[0][0] = 0;
  34. for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
  35. crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  36. for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
  37. crc32table_le[0][i + j] = crc ^ crc32table_le[0][j];
  38. }
  39. for (i = 0; i < LE_TABLE_SIZE; i++) {
  40. crc = crc32table_le[0][i];
  41. for (j = 1; j < LE_TABLE_ROWS; j++) {
  42. crc = crc32table_le[0][crc & 0xff] ^ (crc >> 8);
  43. crc32table_le[j][i] = crc;
  44. }
  45. }
  46. }
  47. /**
  48. * crc32init_be() - allocate and initialize BE table data
  49. */
  50. static void crc32init_be(void)
  51. {
  52. unsigned i, j;
  53. uint32_t crc = 0x80000000;
  54. crc32table_be[0][0] = 0;
  55. for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
  56. crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
  57. for (j = 0; j < i; j++)
  58. crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
  59. }
  60. for (i = 0; i < BE_TABLE_SIZE; i++) {
  61. crc = crc32table_be[0][i];
  62. for (j = 1; j < BE_TABLE_ROWS; j++) {
  63. crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
  64. crc32table_be[j][i] = crc;
  65. }
  66. }
  67. }
  68. static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
  69. {
  70. int i, j;
  71. for (j = 0 ; j < rows; j++) {
  72. printf("{");
  73. for (i = 0; i < len - 1; i++) {
  74. if (i % ENTRIES_PER_LINE == 0)
  75. printf("\n");
  76. printf("%s(0x%8.8xL), ", trans, table[j][i]);
  77. }
  78. printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
  79. }
  80. }
  81. int main(int argc, char** argv)
  82. {
  83. printf("/* this file is generated - do not edit */\n\n");
  84. if (CRC_LE_BITS > 1) {
  85. crc32init_le();
  86. printf("static const u32 __cacheline_aligned "
  87. "crc32table_le[%d][%d] = {",
  88. LE_TABLE_ROWS, LE_TABLE_SIZE);
  89. output_table(crc32table_le, LE_TABLE_ROWS,
  90. LE_TABLE_SIZE, "tole");
  91. printf("};\n");
  92. }
  93. if (CRC_BE_BITS > 1) {
  94. crc32init_be();
  95. printf("static const u32 __cacheline_aligned "
  96. "crc32table_be[%d][%d] = {",
  97. BE_TABLE_ROWS, BE_TABLE_SIZE);
  98. output_table(crc32table_be, LE_TABLE_ROWS,
  99. BE_TABLE_SIZE, "tobe");
  100. printf("};\n");
  101. }
  102. return 0;
  103. }