crc64_fast.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file crc64.c
  4. /// \brief CRC64 calculation
  5. ///
  6. /// Calculate the CRC64 using the slice-by-four algorithm. This is the same
  7. /// idea that is used in crc32_fast.c, but for CRC64 we use only four tables
  8. /// instead of eight to avoid increasing CPU cache usage.
  9. //
  10. // Author: Lasse Collin
  11. //
  12. // This file has been put into the public domain.
  13. // You can do whatever you want with this file.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include "check.h"
  17. #include "crc_macros.h"
  18. #ifdef WORDS_BIGENDIAN
  19. # define A1(x) ((x) >> 56)
  20. #else
  21. # define A1 A
  22. #endif
  23. // See the comments in crc32_fast.c. They aren't duplicated here.
  24. extern LZMA_API(uint64_t)
  25. lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc)
  26. {
  27. crc = ~crc;
  28. #ifdef WORDS_BIGENDIAN
  29. crc = bswap64(crc);
  30. #endif
  31. if (size > 4) {
  32. const uint8_t *limit;
  33. while ((uintptr_t)(buf) & 3) {
  34. crc = lzma_crc64_table[0][*buf++ ^ A1(crc)] ^ S8(crc);
  35. --size;
  36. }
  37. limit = buf + (size & ~(size_t)(3));
  38. size &= (size_t)(3);
  39. while (buf < limit) {
  40. #ifdef WORDS_BIGENDIAN
  41. const uint32_t tmp = (crc >> 32)
  42. ^ *(const uint32_t *)(buf);
  43. #else
  44. const uint32_t tmp = crc ^ *(const uint32_t *)(buf);
  45. #endif
  46. buf += 4;
  47. crc = lzma_crc64_table[3][A(tmp)]
  48. ^ lzma_crc64_table[2][B(tmp)]
  49. ^ S32(crc)
  50. ^ lzma_crc64_table[1][C(tmp)]
  51. ^ lzma_crc64_table[0][D(tmp)];
  52. }
  53. }
  54. while (size-- != 0)
  55. crc = lzma_crc64_table[0][*buf++ ^ A1(crc)] ^ S8(crc);
  56. #ifdef WORDS_BIGENDIAN
  57. crc = bswap64(crc);
  58. #endif
  59. return ~crc;
  60. }