gen_crc32table.c 3.3 KB

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