gen_crc32table.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. static uint32_t crc32ctable_le[LE_TABLE_ROWS][256];
  23. /**
  24. * crc32init_le() - allocate and initialize LE table data
  25. *
  26. * crc is the crc of the byte i; other entries are filled in based on the
  27. * fact that crctable[i^j] = crctable[i] ^ crctable[j].
  28. *
  29. */
  30. static void crc32init_le_generic(const uint32_t polynomial,
  31. uint32_t (*tab)[256])
  32. {
  33. unsigned i, j;
  34. uint32_t crc = 1;
  35. tab[0][0] = 0;
  36. for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
  37. crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
  38. for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
  39. tab[0][i + j] = crc ^ tab[0][j];
  40. }
  41. for (i = 0; i < LE_TABLE_SIZE; i++) {
  42. crc = tab[0][i];
  43. for (j = 1; j < LE_TABLE_ROWS; j++) {
  44. crc = tab[0][crc & 0xff] ^ (crc >> 8);
  45. tab[j][i] = crc;
  46. }
  47. }
  48. }
  49. static void crc32init_le(void)
  50. {
  51. crc32init_le_generic(CRCPOLY_LE, crc32table_le);
  52. }
  53. static void crc32cinit_le(void)
  54. {
  55. crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
  56. }
  57. /**
  58. * crc32init_be() - allocate and initialize BE table data
  59. */
  60. static void crc32init_be(void)
  61. {
  62. unsigned i, j;
  63. uint32_t crc = 0x80000000;
  64. crc32table_be[0][0] = 0;
  65. for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
  66. crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
  67. for (j = 0; j < i; j++)
  68. crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
  69. }
  70. for (i = 0; i < BE_TABLE_SIZE; i++) {
  71. crc = crc32table_be[0][i];
  72. for (j = 1; j < BE_TABLE_ROWS; j++) {
  73. crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
  74. crc32table_be[j][i] = crc;
  75. }
  76. }
  77. }
  78. static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
  79. {
  80. int i, j;
  81. for (j = 0 ; j < rows; j++) {
  82. printf("{");
  83. for (i = 0; i < len - 1; i++) {
  84. if (i % ENTRIES_PER_LINE == 0)
  85. printf("\n");
  86. printf("%s(0x%8.8xL), ", trans, table[j][i]);
  87. }
  88. printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
  89. }
  90. }
  91. int main(int argc, char** argv)
  92. {
  93. printf("/* this file is generated - do not edit */\n\n");
  94. if (CRC_LE_BITS > 1) {
  95. crc32init_le();
  96. printf("static const u32 ____cacheline_aligned "
  97. "crc32table_le[%d][%d] = {",
  98. LE_TABLE_ROWS, LE_TABLE_SIZE);
  99. output_table(crc32table_le, LE_TABLE_ROWS,
  100. LE_TABLE_SIZE, "tole");
  101. printf("};\n");
  102. }
  103. if (CRC_BE_BITS > 1) {
  104. crc32init_be();
  105. printf("static const u32 ____cacheline_aligned "
  106. "crc32table_be[%d][%d] = {",
  107. BE_TABLE_ROWS, BE_TABLE_SIZE);
  108. output_table(crc32table_be, LE_TABLE_ROWS,
  109. BE_TABLE_SIZE, "tobe");
  110. printf("};\n");
  111. }
  112. if (CRC_LE_BITS > 1) {
  113. crc32cinit_le();
  114. printf("static const u32 ____cacheline_aligned "
  115. "crc32ctable_le[%d][%d] = {",
  116. LE_TABLE_ROWS, LE_TABLE_SIZE);
  117. output_table(crc32ctable_le, LE_TABLE_ROWS,
  118. LE_TABLE_SIZE, "tole");
  119. printf("};\n");
  120. }
  121. return 0;
  122. }