Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.0 KiB

  1. /* XzCrc64.c -- CRC64 calculation
  2. 2011-06-28 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include "XzCrc64.h"
  5. #include "CpuArch.h"
  6. #define kCrc64Poly UINT64_CONST(0xC96C5795D7870F42)
  7. #ifdef MY_CPU_LE
  8. #define CRC_NUM_TABLES 4
  9. #else
  10. #define CRC_NUM_TABLES 5
  11. #define CRC_UINT64_SWAP(v) \
  12. ((v >> 56) | \
  13. ((v >> 40) & ((UInt64)0xFF << 8)) | \
  14. ((v >> 24) & ((UInt64)0xFF << 16)) | \
  15. ((v >> 8) & ((UInt64)0xFF << 24)) | \
  16. ((v << 8) & ((UInt64)0xFF << 32)) | \
  17. ((v << 24) & ((UInt64)0xFF << 40)) | \
  18. ((v << 40) & ((UInt64)0xFF << 48)) | \
  19. (v << 56))
  20. UInt64 MY_FAST_CALL XzCrc64UpdateT1_BeT4(UInt64 v, const void *data, size_t size, const UInt64 *table);
  21. #endif
  22. #ifndef MY_CPU_BE
  23. UInt64 MY_FAST_CALL XzCrc64UpdateT4(UInt64 v, const void *data, size_t size, const UInt64 *table);
  24. #endif
  25. typedef UInt64 (MY_FAST_CALL *CRC_FUNC)(UInt64 v, const void *data, size_t size, const UInt64 *table);
  26. static CRC_FUNC g_Crc64Update;
  27. UInt64 g_Crc64Table[256 * CRC_NUM_TABLES];
  28. UInt64 MY_FAST_CALL Crc64Update(UInt64 v, const void *data, size_t size)
  29. {
  30. return g_Crc64Update(v, data, size, g_Crc64Table);
  31. }
  32. UInt64 MY_FAST_CALL Crc64Calc(const void *data, size_t size)
  33. {
  34. return g_Crc64Update(CRC64_INIT_VAL, data, size, g_Crc64Table) ^ CRC64_INIT_VAL;
  35. }
  36. void MY_FAST_CALL Crc64GenerateTable()
  37. {
  38. UInt32 i;
  39. for (i = 0; i < 256; i++)
  40. {
  41. UInt64 r = i;
  42. unsigned j;
  43. for (j = 0; j < 8; j++)
  44. r = (r >> 1) ^ (kCrc64Poly & ~((r & 1) - 1));
  45. g_Crc64Table[i] = r;
  46. }
  47. for (; i < 256 * CRC_NUM_TABLES; i++)
  48. {
  49. UInt64 r = g_Crc64Table[i - 256];
  50. g_Crc64Table[i] = g_Crc64Table[r & 0xFF] ^ (r >> 8);
  51. }
  52. #ifdef MY_CPU_LE
  53. g_Crc64Update = XzCrc64UpdateT4;
  54. #else
  55. {
  56. #ifndef MY_CPU_BE
  57. UInt32 k = 1;
  58. if (*(const Byte *)&k == 1)
  59. g_Crc64Update = XzCrc64UpdateT4;
  60. else
  61. #endif
  62. {
  63. for (i = 256 * CRC_NUM_TABLES - 1; i >= 256; i--)
  64. {
  65. UInt64 x = g_Crc64Table[i - 256];
  66. g_Crc64Table[i] = CRC_UINT64_SWAP(x);
  67. }
  68. g_Crc64Update = XzCrc64UpdateT1_BeT4;
  69. }
  70. }
  71. #endif
  72. }