1 /* XzCrc64Opt.c -- CRC64 calculation
2 2011-06-28 : 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 
XzCrc64UpdateT4(UInt64 v,const void * data,size_t size,const UInt64 * table)12 UInt64 MY_FAST_CALL XzCrc64UpdateT4(UInt64 v, const void *data, size_t size, const UInt64 *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     UInt32 d = (UInt32)v ^ *(const UInt32 *)p;
20     v = (v >> 32) ^
21       table[0x300 + ((d      ) & 0xFF)] ^
22       table[0x200 + ((d >>  8) & 0xFF)] ^
23       table[0x100 + ((d >> 16) & 0xFF)] ^
24       table[0x000 + ((d >> 24))];
25   }
26   for (; size > 0; size--, p++)
27     v = CRC_UPDATE_BYTE_2(v, *p);
28   return v;
29 }
30 
31 #endif
32 
33 
34 #ifndef MY_CPU_LE
35 
36 #define CRC_UINT64_SWAP(v) \
37   ((v >> 56) | \
38   ((v >> 40) & ((UInt64)0xFF <<  8)) | \
39   ((v >> 24) & ((UInt64)0xFF << 16)) | \
40   ((v >>  8) & ((UInt64)0xFF << 24)) | \
41   ((v <<  8) & ((UInt64)0xFF << 32)) | \
42   ((v << 24) & ((UInt64)0xFF << 40)) | \
43   ((v << 40) & ((UInt64)0xFF << 48)) | \
44    (v << 56))
45 
XzCrc64UpdateT1_BeT4(UInt64 v,const void * data,size_t size,const UInt64 * table)46 UInt64 MY_FAST_CALL XzCrc64UpdateT1_BeT4(UInt64 v, const void *data, size_t size, const UInt64 *table)
47 {
48   const Byte *p = (const Byte *)data;
49   for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
50     v = CRC_UPDATE_BYTE_2(v, *p);
51   v = CRC_UINT64_SWAP(v);
52   table += 0x100;
53   for (; size >= 4; size -= 4, p += 4)
54   {
55     UInt32 d = (UInt32)(v >> 32) ^ *(const UInt32 *)p;
56     v = (v << 32) ^
57       table[0x000 + ((d      ) & 0xFF)] ^
58       table[0x100 + ((d >>  8) & 0xFF)] ^
59       table[0x200 + ((d >> 16) & 0xFF)] ^
60       table[0x300 + ((d >> 24))];
61   }
62   table -= 0x100;
63   v = CRC_UINT64_SWAP(v);
64   for (; size > 0; size--, p++)
65     v = CRC_UPDATE_BYTE_2(v, *p);
66   return v;
67 }
68 
69 #endif
70