7zCrcOpt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* 7zCrcOpt.c -- CRC32 calculation
  2. 2017-04-03 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include "CpuArch.h"
  5. #ifndef MY_CPU_BE
  6. #define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
  7. UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table)
  8. {
  9. const Byte *p = (const Byte *)data;
  10. for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
  11. v = CRC_UPDATE_BYTE_2(v, *p);
  12. for (; size >= 4; size -= 4, p += 4)
  13. {
  14. v ^= *(const UInt32 *)p;
  15. v =
  16. (table + 0x300)[((v ) & 0xFF)]
  17. ^ (table + 0x200)[((v >> 8) & 0xFF)]
  18. ^ (table + 0x100)[((v >> 16) & 0xFF)]
  19. ^ (table + 0x000)[((v >> 24))];
  20. }
  21. for (; size > 0; size--, p++)
  22. v = CRC_UPDATE_BYTE_2(v, *p);
  23. return v;
  24. }
  25. UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table)
  26. {
  27. const Byte *p = (const Byte *)data;
  28. for (; size > 0 && ((unsigned)(ptrdiff_t)p & 7) != 0; size--, p++)
  29. v = CRC_UPDATE_BYTE_2(v, *p);
  30. for (; size >= 8; size -= 8, p += 8)
  31. {
  32. UInt32 d;
  33. v ^= *(const UInt32 *)p;
  34. v =
  35. (table + 0x700)[((v ) & 0xFF)]
  36. ^ (table + 0x600)[((v >> 8) & 0xFF)]
  37. ^ (table + 0x500)[((v >> 16) & 0xFF)]
  38. ^ (table + 0x400)[((v >> 24))];
  39. d = *((const UInt32 *)p + 1);
  40. v ^=
  41. (table + 0x300)[((d ) & 0xFF)]
  42. ^ (table + 0x200)[((d >> 8) & 0xFF)]
  43. ^ (table + 0x100)[((d >> 16) & 0xFF)]
  44. ^ (table + 0x000)[((d >> 24))];
  45. }
  46. for (; size > 0; size--, p++)
  47. v = CRC_UPDATE_BYTE_2(v, *p);
  48. return v;
  49. }
  50. #endif
  51. #ifndef MY_CPU_LE
  52. #define CRC_UINT32_SWAP(v) ((v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | (v << 24))
  53. #define CRC_UPDATE_BYTE_2_BE(crc, b) (table[(((crc) >> 24) ^ (b))] ^ ((crc) << 8))
  54. UInt32 MY_FAST_CALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size, const UInt32 *table)
  55. {
  56. const Byte *p = (const Byte *)data;
  57. table += 0x100;
  58. v = CRC_UINT32_SWAP(v);
  59. for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
  60. v = CRC_UPDATE_BYTE_2_BE(v, *p);
  61. for (; size >= 4; size -= 4, p += 4)
  62. {
  63. v ^= *(const UInt32 *)p;
  64. v =
  65. (table + 0x000)[((v ) & 0xFF)]
  66. ^ (table + 0x100)[((v >> 8) & 0xFF)]
  67. ^ (table + 0x200)[((v >> 16) & 0xFF)]
  68. ^ (table + 0x300)[((v >> 24))];
  69. }
  70. for (; size > 0; size--, p++)
  71. v = CRC_UPDATE_BYTE_2_BE(v, *p);
  72. return CRC_UINT32_SWAP(v);
  73. }
  74. UInt32 MY_FAST_CALL CrcUpdateT1_BeT8(UInt32 v, const void *data, size_t size, const UInt32 *table)
  75. {
  76. const Byte *p = (const Byte *)data;
  77. table += 0x100;
  78. v = CRC_UINT32_SWAP(v);
  79. for (; size > 0 && ((unsigned)(ptrdiff_t)p & 7) != 0; size--, p++)
  80. v = CRC_UPDATE_BYTE_2_BE(v, *p);
  81. for (; size >= 8; size -= 8, p += 8)
  82. {
  83. UInt32 d;
  84. v ^= *(const UInt32 *)p;
  85. v =
  86. (table + 0x400)[((v ) & 0xFF)]
  87. ^ (table + 0x500)[((v >> 8) & 0xFF)]
  88. ^ (table + 0x600)[((v >> 16) & 0xFF)]
  89. ^ (table + 0x700)[((v >> 24))];
  90. d = *((const UInt32 *)p + 1);
  91. v ^=
  92. (table + 0x000)[((d ) & 0xFF)]
  93. ^ (table + 0x100)[((d >> 8) & 0xFF)]
  94. ^ (table + 0x200)[((d >> 16) & 0xFF)]
  95. ^ (table + 0x300)[((d >> 24))];
  96. }
  97. for (; size > 0; size--, p++)
  98. v = CRC_UPDATE_BYTE_2_BE(v, *p);
  99. return CRC_UINT32_SWAP(v);
  100. }
  101. #endif