gen_crc64table.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generate lookup table for the table-driven CRC64 calculation.
  4. *
  5. * gen_crc64table is executed in kernel build time and generates
  6. * lib/crc64table.h. This header is included by lib/crc64.c for
  7. * the table-driven CRC64 calculation.
  8. *
  9. * See lib/crc64.c for more information about which specification
  10. * and polynomial arithmetic that gen_crc64table.c follows to
  11. * generate the lookup table.
  12. *
  13. * Copyright 2018 SUSE Linux.
  14. * Author: Coly Li <[email protected]>
  15. */
  16. #include <inttypes.h>
  17. #include <stdio.h>
  18. #define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
  19. #define CRC64_ROCKSOFT_POLY 0x9A6C9329AC4BC9B5ULL
  20. static uint64_t crc64_table[256] = {0};
  21. static uint64_t crc64_rocksoft_table[256] = {0};
  22. static void generate_reflected_crc64_table(uint64_t table[256], uint64_t poly)
  23. {
  24. uint64_t i, j, c, crc;
  25. for (i = 0; i < 256; i++) {
  26. crc = 0ULL;
  27. c = i;
  28. for (j = 0; j < 8; j++) {
  29. if ((crc ^ (c >> j)) & 1)
  30. crc = (crc >> 1) ^ poly;
  31. else
  32. crc >>= 1;
  33. }
  34. table[i] = crc;
  35. }
  36. }
  37. static void generate_crc64_table(uint64_t table[256], uint64_t poly)
  38. {
  39. uint64_t i, j, c, crc;
  40. for (i = 0; i < 256; i++) {
  41. crc = 0;
  42. c = i << 56;
  43. for (j = 0; j < 8; j++) {
  44. if ((crc ^ c) & 0x8000000000000000ULL)
  45. crc = (crc << 1) ^ poly;
  46. else
  47. crc <<= 1;
  48. c <<= 1;
  49. }
  50. table[i] = crc;
  51. }
  52. }
  53. static void output_table(uint64_t table[256])
  54. {
  55. int i;
  56. for (i = 0; i < 256; i++) {
  57. printf("\t0x%016" PRIx64 "ULL", table[i]);
  58. if (i & 0x1)
  59. printf(",\n");
  60. else
  61. printf(", ");
  62. }
  63. printf("};\n");
  64. }
  65. static void print_crc64_tables(void)
  66. {
  67. printf("/* this file is generated - do not edit */\n\n");
  68. printf("#include <linux/types.h>\n");
  69. printf("#include <linux/cache.h>\n\n");
  70. printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
  71. output_table(crc64_table);
  72. printf("\nstatic const u64 ____cacheline_aligned crc64rocksofttable[256] = {\n");
  73. output_table(crc64_rocksoft_table);
  74. }
  75. int main(int argc, char *argv[])
  76. {
  77. generate_crc64_table(crc64_table, CRC64_ECMA182_POLY);
  78. generate_reflected_crc64_table(crc64_rocksoft_table, CRC64_ROCKSOFT_POLY);
  79. print_crc64_tables();
  80. return 0;
  81. }