gen_crc64table.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This host program runs at kernel build time and generates the lookup tables
  4. * used by the generic CRC64 code.
  5. *
  6. * Copyright 2018 SUSE Linux.
  7. * Author: Coly Li <colyli@suse.de>
  8. */
  9. #include <inttypes.h>
  10. #include <stdio.h>
  11. #define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
  12. #define CRC64_NVME_POLY 0x9A6C9329AC4BC9B5ULL
  13. static uint64_t crc64_table[256] = {0};
  14. static uint64_t crc64_nvme_table[256] = {0};
  15. static void generate_reflected_crc64_table(uint64_t table[256], uint64_t poly)
  16. {
  17. uint64_t i, j, c, crc;
  18. for (i = 0; i < 256; i++) {
  19. crc = 0ULL;
  20. c = i;
  21. for (j = 0; j < 8; j++) {
  22. if ((crc ^ (c >> j)) & 1)
  23. crc = (crc >> 1) ^ poly;
  24. else
  25. crc >>= 1;
  26. }
  27. table[i] = crc;
  28. }
  29. }
  30. static void generate_crc64_table(uint64_t table[256], uint64_t poly)
  31. {
  32. uint64_t i, j, c, crc;
  33. for (i = 0; i < 256; i++) {
  34. crc = 0;
  35. c = i << 56;
  36. for (j = 0; j < 8; j++) {
  37. if ((crc ^ c) & 0x8000000000000000ULL)
  38. crc = (crc << 1) ^ poly;
  39. else
  40. crc <<= 1;
  41. c <<= 1;
  42. }
  43. table[i] = crc;
  44. }
  45. }
  46. static void output_table(uint64_t table[256])
  47. {
  48. int i;
  49. for (i = 0; i < 256; i++) {
  50. printf("\t0x%016" PRIx64 "ULL", table[i]);
  51. if (i & 0x1)
  52. printf(",\n");
  53. else
  54. printf(", ");
  55. }
  56. printf("};\n");
  57. }
  58. static void print_crc64_tables(void)
  59. {
  60. printf("/* this file is generated - do not edit */\n\n");
  61. printf("#include <linux/types.h>\n");
  62. printf("#include <linux/cache.h>\n\n");
  63. printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
  64. output_table(crc64_table);
  65. printf("\nstatic const u64 ____cacheline_aligned crc64nvmetable[256] = {\n");
  66. output_table(crc64_nvme_table);
  67. }
  68. int main(int argc, char *argv[])
  69. {
  70. generate_crc64_table(crc64_table, CRC64_ECMA182_POLY);
  71. generate_reflected_crc64_table(crc64_nvme_table, CRC64_NVME_POLY);
  72. print_crc64_tables();
  73. return 0;
  74. }