gen_crc64table.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <colyli@suse.de>
  15. */
  16. #include <inttypes.h>
  17. #include <stdio.h>
  18. #define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
  19. static uint64_t crc64_table[256] = {0};
  20. static void generate_crc64_table(void)
  21. {
  22. uint64_t i, j, c, crc;
  23. for (i = 0; i < 256; i++) {
  24. crc = 0;
  25. c = i << 56;
  26. for (j = 0; j < 8; j++) {
  27. if ((crc ^ c) & 0x8000000000000000ULL)
  28. crc = (crc << 1) ^ CRC64_ECMA182_POLY;
  29. else
  30. crc <<= 1;
  31. c <<= 1;
  32. }
  33. crc64_table[i] = crc;
  34. }
  35. }
  36. static void print_crc64_table(void)
  37. {
  38. int i;
  39. printf("/* this file is generated - do not edit */\n\n");
  40. printf("#include <linux/types.h>\n");
  41. printf("#include <linux/cache.h>\n\n");
  42. printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
  43. for (i = 0; i < 256; i++) {
  44. printf("\t0x%016" PRIx64 "ULL", crc64_table[i]);
  45. if (i & 0x1)
  46. printf(",\n");
  47. else
  48. printf(", ");
  49. }
  50. printf("};\n");
  51. }
  52. int main(int argc, char *argv[])
  53. {
  54. generate_crc64_table();
  55. print_crc64_table();
  56. return 0;
  57. }