1 /* 7zCrcOpt.c -- CRC32 calculation
2 2013-11-12 : Igor Pavlov : Public domain */
3 
4 #include "Precomp.h"
5 
6 #include "CpuArch.h"
7 
8 #define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
9 
10 #ifndef MY_CPU_BE
11 
CrcUpdateT4(UInt32 v,const void * data,size_t size,const UInt32 * table)12 UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table)
13 {
14   const Byte *p = (const Byte *)data;
15   for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
16     v = CRC_UPDATE_BYTE_2(v, *p);
17   for (; size >= 4; size -= 4, p += 4)
18   {
19     v ^= *(const UInt32 *)p;
20     v =
21       table[0x300 + (v & 0xFF)] ^
22       table[0x200 + ((v >> 8) & 0xFF)] ^
23       table[0x100 + ((v >> 16) & 0xFF)] ^
24       table[0x000 + ((v >> 24))];
25   }
26   for (; size > 0; size--, p++)
27     v = CRC_UPDATE_BYTE_2(v, *p);
28   return v;
29 }
30 
CrcUpdateT8(UInt32 v,const void * data,size_t size,const UInt32 * table)31 UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table)
32 {
33   return CrcUpdateT4(v, data, size, table);
34 }
35 
36 #endif
37 
38 
39 #ifndef MY_CPU_LE
40 
41 #define CRC_UINT32_SWAP(v) ((v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | (v << 24))
42 
CrcUpdateT1_BeT4(UInt32 v,const void * data,size_t size,const UInt32 * table)43 UInt32 MY_FAST_CALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size, const UInt32 *table)
44 {
45   const Byte *p = (const Byte *)data;
46   for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
47     v = CRC_UPDATE_BYTE_2(v, *p);
48   v = CRC_UINT32_SWAP(v);
49   table += 0x100;
50   for (; size >= 4; size -= 4, p += 4)
51   {
52     v ^= *(const UInt32 *)p;
53     v =
54       table[0x000 + (v & 0xFF)] ^
55       table[0x100 + ((v >> 8) & 0xFF)] ^
56       table[0x200 + ((v >> 16) & 0xFF)] ^
57       table[0x300 + ((v >> 24))];
58   }
59   table -= 0x100;
60   v = CRC_UINT32_SWAP(v);
61   for (; size > 0; size--, p++)
62     v = CRC_UPDATE_BYTE_2(v, *p);
63   return v;
64 }
65 
66 #endif
67