| 1 |
// Copyright 2024 Marc A. Lehmann |
| 2 |
// Copyright 2008,2009,2010 Massachusetts Institute of Technology. |
| 3 |
// All rights reserved. Use of this source code is governed by a |
| 4 |
// BSD-style license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
#include "crc32ctables.c" |
| 7 |
|
| 8 |
// Implementations adapted from Intel's Slicing By 8 Sourceforge Project |
| 9 |
// http://sourceforge.net/projects/slicing-by-8/ |
| 10 |
/*++ |
| 11 |
* |
| 12 |
* Copyright (c) 2004-2006 Intel Corporation - All Rights Reserved |
| 13 |
* |
| 14 |
* This software program is licensed subject to the BSD License, |
| 15 |
* available at http://www.opensource.org/licenses/bsd-license.html |
| 16 |
* |
| 17 |
* Abstract: The main routine |
| 18 |
* |
| 19 |
--*/ |
| 20 |
|
| 21 |
static uint32_t crc32cSlicingBy8(uint32_t crc, const void* data, size_t length) { |
| 22 |
size_t li; |
| 23 |
const char* p_buf = (const char*) data; |
| 24 |
|
| 25 |
// Handle leading misaligned bytes |
| 26 |
size_t initial_bytes = (sizeof(int32_t) - (intptr_t)p_buf) & (sizeof(int32_t) - 1); |
| 27 |
if (length < initial_bytes) initial_bytes = length; |
| 28 |
for (li = 0; li < initial_bytes; li++) { |
| 29 |
crc = crc_tableil8_o32[(crc ^ *p_buf++) & 0x000000FF] ^ (crc >> 8); |
| 30 |
} |
| 31 |
|
| 32 |
length -= initial_bytes; |
| 33 |
size_t running_length = length & ~(sizeof(uint64_t) - 1); |
| 34 |
size_t end_bytes = length - running_length; |
| 35 |
|
| 36 |
for (li = 0; li < running_length/8; li++) { |
| 37 |
crc ^= *(uint32_t*) p_buf; |
| 38 |
p_buf += 4; |
| 39 |
uint32_t term1 = crc_tableil8_o88[crc & 0x000000FF] ^ |
| 40 |
crc_tableil8_o80[(crc >> 8) & 0x000000FF]; |
| 41 |
uint32_t term2 = crc >> 16; |
| 42 |
crc = term1 ^ |
| 43 |
crc_tableil8_o72[term2 & 0x000000FF] ^ |
| 44 |
crc_tableil8_o64[(term2 >> 8) & 0x000000FF]; |
| 45 |
term1 = crc_tableil8_o56[(*(uint32_t *)p_buf) & 0x000000FF] ^ |
| 46 |
crc_tableil8_o48[((*(uint32_t *)p_buf) >> 8) & 0x000000FF]; |
| 47 |
|
| 48 |
term2 = (*(uint32_t *)p_buf) >> 16; |
| 49 |
crc = crc ^ term1 ^ |
| 50 |
crc_tableil8_o40[term2 & 0x000000FF] ^ |
| 51 |
crc_tableil8_o32[(term2 >> 8) & 0x000000FF]; |
| 52 |
p_buf += 4; |
| 53 |
} |
| 54 |
|
| 55 |
for (li=0; li < end_bytes; li++) { |
| 56 |
crc = crc_tableil8_o32[(crc ^ *p_buf++) & 0x000000FF] ^ (crc >> 8); |
| 57 |
} |
| 58 |
|
| 59 |
return crc; |
| 60 |
} |
| 61 |
|