我一直在与内在因素作斗争。特别是使用标准 CRC 计算和假定等效的英特尔内在函数时,我没有得到相同的结果。我想转而使用_mm_crc32_u16,_mm_crc32_u32但如果我不能让 8 位操作工作,那就没有意义了。
static UINT32 g_ui32CRC32Table[256] =
{
0x00000000L, 0x77073096L, 0xEE0E612CL, 0x990951BAL,
0x076DC419L, 0x706AF48FL, 0xE963A535L, 0x9E6495A3L,
0x0EDB8832L, 0x79DCB8A4L, 0xE0D5E91EL, 0x97D2D988L,
....
// Your basic 32-bit CRC calculator
// NOTE: this code cannot be changed
UINT32 CalcCRC32(unsigned char *pucBuff, int iLen)
{
UINT32 crc = 0xFFFFFFFF;
for (int x = 0; x < iLen; x++)
{
crc = g_ui32CRC32Table[(crc ^ *pucBuff++) & 0xFFL] ^ (crc >> 8);
}
return crc ^ 0xFFFFFFFF;
}
UINT32 CalcCRC32_Intrinsic(unsigned char *pucBuff, int iLen)
{
UINT32 crc = 0xFFFFFFFF;
for (int x = 0; x < iLen; x++)
{
crc = _mm_crc32_u8(crc, *pucBuff++);
}
return crc ^ 0xFFFFFFFF;
}
Run Code Online (Sandbox Code Playgroud)
该表用于与 Intel 指令使用的 CRC 多项式不同的 CRC 多项式。该表用于以太网/ZIP/等。CRC,通常称为CRC-32。Intel 指令使用 iSCSI (Castagnoli) 多项式,CRC 通常称为 CRC-32C。
这个简短的示例代码可以通过取消注释所需的多项式来计算:
#include <stddef.h>
#include <stdint.h>
/* CRC-32 (Ethernet, ZIP, etc.) polynomial in reversed bit order. */
#define POLY 0xedb88320
/* CRC-32C (iSCSI) polynomial in reversed bit order. */
/* #define POLY 0x82f63b78 */
/* Compute CRC of buf[0..len-1] with initial CRC crc. This permits the
computation of a CRC by feeding this routine a chunk of the input data at a
time. The value of crc for the first chunk should be zero. */
uint32_t crc32c(uint32_t crc, const unsigned char *buf, size_t len)
{
int k;
crc = ~crc;
while (len--) {
crc ^= *buf++;
for (k = 0; k < 8; k++)
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
}
return ~crc;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此代码通过简单地计算每个单字节消息 0、1、2、...、255 的 CRC-32C 来为您的代码生成替换表。
| 归档时间: |
|
| 查看次数: |
1261 次 |
| 最近记录: |