gen_crc32table.c 3.3 KB

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