crc64_tablegen.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file crc64_tablegen.c
  4. /// \brief Generate crc64_table_le.h and crc64_table_be.h
  5. ///
  6. /// Compiling: gcc -std=c99 -o crc64_tablegen crc64_tablegen.c
  7. /// Add -DWORDS_BIGENDIAN to generate big endian table.
  8. //
  9. // Author: Lasse Collin
  10. //
  11. // This file has been put into the public domain.
  12. // You can do whatever you want with this file.
  13. //
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #include <stdio.h>
  16. #include "tuklib_integer.h"
  17. static uint64_t crc64_table[4][256];
  18. extern void
  19. init_crc64_table(void)
  20. {
  21. static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
  22. for (size_t s = 0; s < 4; ++s) {
  23. for (size_t b = 0; b < 256; ++b) {
  24. uint64_t r = s == 0 ? b : crc64_table[s - 1][b];
  25. for (size_t i = 0; i < 8; ++i) {
  26. if (r & 1)
  27. r = (r >> 1) ^ poly64;
  28. else
  29. r >>= 1;
  30. }
  31. crc64_table[s][b] = r;
  32. }
  33. }
  34. #ifdef WORDS_BIGENDIAN
  35. for (size_t s = 0; s < 4; ++s)
  36. for (size_t b = 0; b < 256; ++b)
  37. crc64_table[s][b] = bswap64(crc64_table[s][b]);
  38. #endif
  39. return;
  40. }
  41. static void
  42. print_crc64_table(void)
  43. {
  44. printf("/* This file has been automatically generated by "
  45. "crc64_tablegen.c. */\n\n"
  46. "const uint64_t lzma_crc64_table[4][256] = {\n\t{");
  47. for (size_t s = 0; s < 4; ++s) {
  48. for (size_t b = 0; b < 256; ++b) {
  49. if ((b % 2) == 0)
  50. printf("\n\t\t");
  51. printf("UINT64_C(0x%016" PRIX64 ")",
  52. crc64_table[s][b]);
  53. if (b != 255)
  54. printf(",%s", (b+1) % 2 == 0 ? "" : " ");
  55. }
  56. if (s == 3)
  57. printf("\n\t}\n};\n");
  58. else
  59. printf("\n\t}, {");
  60. }
  61. return;
  62. }
  63. int
  64. main(void)
  65. {
  66. init_crc64_table();
  67. print_crc64_table();
  68. return 0;
  69. }