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