OLD | NEW |
1 /* 7zCrc.c -- CRC32 calculation | 1 /* 7zCrc.c -- CRC32 init |
2 2009-11-23 : Igor Pavlov : Public domain */ | 2 2015-03-10 : Igor Pavlov : Public domain */ |
3 | 3 |
| 4 #include "Precomp.h" |
| 5 |
4 #include "7zCrc.h" | 6 #include "7zCrc.h" |
5 #include "CpuArch.h" | 7 #include "CpuArch.h" |
6 | 8 |
7 #define kCrcPoly 0xEDB88320 | 9 #define kCrcPoly 0xEDB88320 |
8 | 10 |
9 #ifdef MY_CPU_LE | 11 #ifdef MY_CPU_LE |
10 #define CRC_NUM_TABLES 8 | 12 #define CRC_NUM_TABLES 8 |
11 #else | 13 #else |
12 #define CRC_NUM_TABLES 1 | 14 #define CRC_NUM_TABLES 9 |
| 15 |
| 16 #define CRC_UINT32_SWAP(v) ((v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF
0000) | (v << 24)) |
| 17 |
| 18 UInt32 MY_FAST_CALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size,
const UInt32 *table); |
| 19 UInt32 MY_FAST_CALL CrcUpdateT1_BeT8(UInt32 v, const void *data, size_t size,
const UInt32 *table); |
| 20 #endif |
| 21 |
| 22 #ifndef MY_CPU_BE |
| 23 UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const
UInt32 *table); |
| 24 UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const
UInt32 *table); |
13 #endif | 25 #endif |
14 | 26 |
15 typedef UInt32 (MY_FAST_CALL *CRC_FUNC)(UInt32 v, const void *data, size_t size,
const UInt32 *table); | 27 typedef UInt32 (MY_FAST_CALL *CRC_FUNC)(UInt32 v, const void *data, size_t size,
const UInt32 *table); |
16 | 28 |
17 static CRC_FUNC g_CrcUpdate; | 29 CRC_FUNC g_CrcUpdateT4; |
| 30 CRC_FUNC g_CrcUpdateT8; |
| 31 CRC_FUNC g_CrcUpdate; |
| 32 |
18 UInt32 g_CrcTable[256 * CRC_NUM_TABLES]; | 33 UInt32 g_CrcTable[256 * CRC_NUM_TABLES]; |
19 | 34 |
20 #if CRC_NUM_TABLES == 1 | |
21 | |
22 #define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) | |
23 | |
24 static UInt32 MY_FAST_CALL CrcUpdateT1(UInt32 v, const void *data, size_t size,
const UInt32 *table) | |
25 { | |
26 const Byte *p = (const Byte *)data; | |
27 for (; size > 0; size--, p++) | |
28 v = CRC_UPDATE_BYTE_2(v, *p); | |
29 return v; | |
30 } | |
31 | |
32 #else | |
33 | |
34 UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const U
Int32 *table); | |
35 UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const U
Int32 *table); | |
36 | |
37 #endif | |
38 | |
39 UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void *data, size_t size) | 35 UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void *data, size_t size) |
40 { | 36 { |
41 return g_CrcUpdate(v, data, size, g_CrcTable); | 37 return g_CrcUpdate(v, data, size, g_CrcTable); |
42 } | 38 } |
43 | 39 |
44 UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size) | 40 UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size) |
45 { | 41 { |
46 return g_CrcUpdate(CRC_INIT_VAL, data, size, g_CrcTable) ^ CRC_INIT_VAL; | 42 return g_CrcUpdate(CRC_INIT_VAL, data, size, g_CrcTable) ^ CRC_INIT_VAL; |
47 } | 43 } |
48 | 44 |
| 45 #define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) |
| 46 |
| 47 UInt32 MY_FAST_CALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const U
Int32 *table) |
| 48 { |
| 49 const Byte *p = (const Byte *)data; |
| 50 const Byte *pEnd = p + size; |
| 51 for (; p != pEnd; p++) |
| 52 v = CRC_UPDATE_BYTE_2(v, *p); |
| 53 return v; |
| 54 } |
| 55 |
49 void MY_FAST_CALL CrcGenerateTable() | 56 void MY_FAST_CALL CrcGenerateTable() |
50 { | 57 { |
51 UInt32 i; | 58 UInt32 i; |
52 for (i = 0; i < 256; i++) | 59 for (i = 0; i < 256; i++) |
53 { | 60 { |
54 UInt32 r = i; | 61 UInt32 r = i; |
55 unsigned j; | 62 unsigned j; |
56 for (j = 0; j < 8; j++) | 63 for (j = 0; j < 8; j++) |
57 r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1)); | 64 r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1)); |
58 g_CrcTable[i] = r; | 65 g_CrcTable[i] = r; |
59 } | 66 } |
60 #if CRC_NUM_TABLES == 1 | |
61 g_CrcUpdate = CrcUpdateT1; | |
62 #else | |
63 for (; i < 256 * CRC_NUM_TABLES; i++) | 67 for (; i < 256 * CRC_NUM_TABLES; i++) |
64 { | 68 { |
65 UInt32 r = g_CrcTable[i - 256]; | 69 UInt32 r = g_CrcTable[i - 256]; |
66 g_CrcTable[i] = g_CrcTable[r & 0xFF] ^ (r >> 8); | 70 g_CrcTable[i] = g_CrcTable[r & 0xFF] ^ (r >> 8); |
67 } | 71 } |
68 g_CrcUpdate = CrcUpdateT4; | 72 |
69 #ifdef MY_CPU_X86_OR_AMD64 | 73 #if CRC_NUM_TABLES < 4 |
70 if (!CPU_Is_InOrder()) | 74 |
71 g_CrcUpdate = CrcUpdateT8; | 75 g_CrcUpdate = CrcUpdateT1; |
| 76 |
| 77 #else |
| 78 |
| 79 #ifdef MY_CPU_LE |
| 80 |
| 81 g_CrcUpdateT4 = CrcUpdateT4; |
| 82 g_CrcUpdate = CrcUpdateT4; |
| 83 |
| 84 #if CRC_NUM_TABLES >= 8 |
| 85 g_CrcUpdateT8 = CrcUpdateT8; |
| 86 |
| 87 #ifdef MY_CPU_X86_OR_AMD64 |
| 88 if (!CPU_Is_InOrder()) |
| 89 g_CrcUpdate = CrcUpdateT8; |
| 90 #endif |
| 91 #endif |
| 92 |
| 93 #else |
| 94 { |
| 95 #ifndef MY_CPU_BE |
| 96 UInt32 k = 0x01020304; |
| 97 const Byte *p = (const Byte *)&k; |
| 98 if (p[0] == 4 && p[1] == 3) |
| 99 { |
| 100 g_CrcUpdateT4 = CrcUpdateT4; |
| 101 g_CrcUpdate = CrcUpdateT4; |
| 102 #if CRC_NUM_TABLES >= 8 |
| 103 g_CrcUpdateT8 = CrcUpdateT8; |
| 104 // g_CrcUpdate = CrcUpdateT8; |
| 105 #endif |
| 106 } |
| 107 else if (p[0] != 1 || p[1] != 2) |
| 108 g_CrcUpdate = CrcUpdateT1; |
| 109 else |
| 110 #endif |
| 111 { |
| 112 for (i = 256 * CRC_NUM_TABLES - 1; i >= 256; i--) |
| 113 { |
| 114 UInt32 x = g_CrcTable[i - 256]; |
| 115 g_CrcTable[i] = CRC_UINT32_SWAP(x); |
| 116 } |
| 117 g_CrcUpdateT4 = CrcUpdateT1_BeT4; |
| 118 g_CrcUpdate = CrcUpdateT1_BeT4; |
| 119 #if CRC_NUM_TABLES >= 8 |
| 120 g_CrcUpdateT8 = CrcUpdateT1_BeT8; |
| 121 // g_CrcUpdate = CrcUpdateT1_BeT8; |
| 122 #endif |
| 123 } |
| 124 } |
72 #endif | 125 #endif |
| 126 |
73 #endif | 127 #endif |
74 } | 128 } |
OLD | NEW |