Fix
[ryzomcore.git] / nel / 3rdparty / seven_zip / XzCrc64.c
blobb6d02cbebd5151e840ec94975a4d76d5444dafcf
1 /* XzCrc64.c -- CRC64 calculation
2 2017-05-23 : Igor Pavlov : Public domain */
4 #include "Precomp.h"
6 #include "XzCrc64.h"
7 #include "CpuArch.h"
9 #define kCrc64Poly UINT64_CONST(0xC96C5795D7870F42)
11 #ifdef MY_CPU_LE
12 #define CRC64_NUM_TABLES 4
13 #else
14 #define CRC64_NUM_TABLES 5
15 #define CRC_UINT64_SWAP(v) \
16 ((v >> 56) \
17 | ((v >> 40) & ((UInt64)0xFF << 8)) \
18 | ((v >> 24) & ((UInt64)0xFF << 16)) \
19 | ((v >> 8) & ((UInt64)0xFF << 24)) \
20 | ((v << 8) & ((UInt64)0xFF << 32)) \
21 | ((v << 24) & ((UInt64)0xFF << 40)) \
22 | ((v << 40) & ((UInt64)0xFF << 48)) \
23 | ((v << 56)))
25 UInt64 MY_FAST_CALL XzCrc64UpdateT1_BeT4(UInt64 v, const void *data, size_t size, const UInt64 *table);
26 #endif
28 #ifndef MY_CPU_BE
29 UInt64 MY_FAST_CALL XzCrc64UpdateT4(UInt64 v, const void *data, size_t size, const UInt64 *table);
30 #endif
32 typedef UInt64 (MY_FAST_CALL *CRC64_FUNC)(UInt64 v, const void *data, size_t size, const UInt64 *table);
34 static CRC64_FUNC g_Crc64Update;
35 UInt64 g_Crc64Table[256 * CRC64_NUM_TABLES];
37 UInt64 MY_FAST_CALL Crc64Update(UInt64 v, const void *data, size_t size)
39 return g_Crc64Update(v, data, size, g_Crc64Table);
42 UInt64 MY_FAST_CALL Crc64Calc(const void *data, size_t size)
44 return g_Crc64Update(CRC64_INIT_VAL, data, size, g_Crc64Table) ^ CRC64_INIT_VAL;
47 void MY_FAST_CALL Crc64GenerateTable()
49 UInt32 i;
50 for (i = 0; i < 256; i++)
52 UInt64 r = i;
53 unsigned j;
54 for (j = 0; j < 8; j++)
55 r = (r >> 1) ^ (kCrc64Poly & ((UInt64)0 - (r & 1)));
56 g_Crc64Table[i] = r;
58 for (i = 256; i < 256 * CRC64_NUM_TABLES; i++)
60 UInt64 r = g_Crc64Table[(size_t)i - 256];
61 g_Crc64Table[i] = g_Crc64Table[r & 0xFF] ^ (r >> 8);
64 #ifdef MY_CPU_LE
66 g_Crc64Update = XzCrc64UpdateT4;
68 #else
70 #ifndef MY_CPU_BE
71 UInt32 k = 1;
72 if (*(const Byte *)&k == 1)
73 g_Crc64Update = XzCrc64UpdateT4;
74 else
75 #endif
77 for (i = 256 * CRC64_NUM_TABLES - 1; i >= 256; i--)
79 UInt64 x = g_Crc64Table[(size_t)i - 256];
80 g_Crc64Table[i] = CRC_UINT64_SWAP(x);
82 g_Crc64Update = XzCrc64UpdateT1_BeT4;
85 #endif