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