text
stringlengths
0
2.2M
// enabling optimization (at least -O2) automatically unrolls the inner for-loop
const size_t Unroll = 4;
const size_t BytesAtOnce = 16 * Unroll;
while (length >= BytesAtOnce)
{
for (size_t unrolling = 0; unrolling < Unroll; unrolling++)
{
#if __BYTE_ORDER == __BIG_ENDIAN
uint32_t one = *current++ ^ swap(crc);
uint32_t two = *current++;
uint32_t three = *current++;
uint32_t four = *current++;
crc = Crc32Lookup[ 0][ four & 0xFF] ^
Crc32Lookup[ 1][(four >> 8) & 0xFF] ^
Crc32Lookup[ 2][(four >> 16) & 0xFF] ^
Crc32Lookup[ 3][(four >> 24) & 0xFF] ^
Crc32Lookup[ 4][ three & 0xFF] ^
Crc32Lookup[ 5][(three >> 8) & 0xFF] ^
Crc32Lookup[ 6][(three >> 16) & 0xFF] ^
Crc32Lookup[ 7][(three >> 24) & 0xFF] ^
Crc32Lookup[ 8][ two & 0xFF] ^
Crc32Lookup[ 9][(two >> 8) & 0xFF] ^
Crc32Lookup[10][(two >> 16) & 0xFF] ^
Crc32Lookup[11][(two >> 24) & 0xFF] ^
Crc32Lookup[12][ one & 0xFF] ^
Crc32Lookup[13][(one >> 8) & 0xFF] ^
Crc32Lookup[14][(one >> 16) & 0xFF] ^
Crc32Lookup[15][(one >> 24) & 0xFF];
#else
uint32_t one = *current++ ^ crc;
uint32_t two = *current++;
uint32_t three = *current++;
uint32_t four = *current++;
crc = Crc32Lookup[ 0][(four >> 24) & 0xFF] ^
Crc32Lookup[ 1][(four >> 16) & 0xFF] ^
Crc32Lookup[ 2][(four >> 8) & 0xFF] ^
Crc32Lookup[ 3][ four & 0xFF] ^
Crc32Lookup[ 4][(three >> 24) & 0xFF] ^
Crc32Lookup[ 5][(three >> 16) & 0xFF] ^
Crc32Lookup[ 6][(three >> 8) & 0xFF] ^
Crc32Lookup[ 7][ three & 0xFF] ^
Crc32Lookup[ 8][(two >> 24) & 0xFF] ^
Crc32Lookup[ 9][(two >> 16) & 0xFF] ^
Crc32Lookup[10][(two >> 8) & 0xFF] ^
Crc32Lookup[11][ two & 0xFF] ^
Crc32Lookup[12][(one >> 24) & 0xFF] ^
Crc32Lookup[13][(one >> 16) & 0xFF] ^
Crc32Lookup[14][(one >> 8) & 0xFF] ^
Crc32Lookup[15][ one & 0xFF];
#endif
}
length -= BytesAtOnce;
}
const uint8_t* currentChar = (const uint8_t*) current;
// remaining 1 to 63 bytes (standard algorithm)
while (length-- != 0)
crc = (crc >> 8) ^ Crc32Lookup[0][(crc & 0xFF) ^ *currentChar++];
return ~crc; // same as crc ^ 0xFFFFFFFF
}
/// compute CRC32 (Slicing-by-16 algorithm, prefetch upcoming data blocks)
uint32_t crc32_16bytes_prefetch(const void* data, size_t length, uint32_t previousCrc32 = 0, size_t prefetchAhead = 256)
{
// CRC code is identical to crc32_16bytes (including unrolling), only added prefetching
// 256 bytes look-ahead seems to be the sweet spot on Core i7 CPUs
uint32_t crc = ~previousCrc32; // same as previousCrc32 ^ 0xFFFFFFFF
const uint32_t* current = (const uint32_t*) data;
// enabling optimization (at least -O2) automatically unrolls the for-loop
const size_t Unroll = 4;
const size_t BytesAtOnce = 16 * Unroll;
while (length >= BytesAtOnce + prefetchAhead)
{
PREFETCH(((const char*) current) + prefetchAhead);
for (size_t unrolling = 0; unrolling < Unroll; unrolling++)
{
#if __BYTE_ORDER == __BIG_ENDIAN
uint32_t one = *current++ ^ swap(crc);
uint32_t two = *current++;
uint32_t three = *current++;
uint32_t four = *current++;
crc = Crc32Lookup[ 0][ four & 0xFF] ^
Crc32Lookup[ 1][(four >> 8) & 0xFF] ^
Crc32Lookup[ 2][(four >> 16) & 0xFF] ^
Crc32Lookup[ 3][(four >> 24) & 0xFF] ^
Crc32Lookup[ 4][ three & 0xFF] ^
Crc32Lookup[ 5][(three >> 8) & 0xFF] ^
Crc32Lookup[ 6][(three >> 16) & 0xFF] ^
Crc32Lookup[ 7][(three >> 24) & 0xFF] ^
Crc32Lookup[ 8][ two & 0xFF] ^
Crc32Lookup[ 9][(two >> 8) & 0xFF] ^