1 /* XzCrc64.c -- CRC64 calculation
2 2017-05-23 : Igor Pavlov : Public domain */
9 #define kCrc64Poly UINT64_CONST(0xC96C5795D7870F42)
12 #define CRC64_NUM_TABLES 4
14 #define CRC64_NUM_TABLES 5
15 #define CRC_UINT64_SWAP(v) \
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)) \
25 UInt64 MY_FAST_CALL
XzCrc64UpdateT1_BeT4(UInt64 v
, const void *data
, size_t size
, const UInt64
*table
);
29 UInt64 MY_FAST_CALL
XzCrc64UpdateT4(UInt64 v
, const void *data
, size_t size
, const UInt64
*table
);
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()
50 for (i
= 0; i
< 256; i
++)
54 for (j
= 0; j
< 8; j
++)
55 r
= (r
>> 1) ^ (kCrc64Poly
& ((UInt64
)0 - (r
& 1)));
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);
66 g_Crc64Update
= XzCrc64UpdateT4
;
72 if (*(const Byte
*)&k
== 1)
73 g_Crc64Update
= XzCrc64UpdateT4
;
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
;