_mm_crc32_u8给出的结果与参考代码不同

时间:2015-03-20 19:23:44

标签: c crc

我一直在与内在因素斗争。特别是我没有使用标准CRC计算和所谓等效的intel内在函数得到相同的结果。我想转而使用_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;
}

2 个答案:

答案 0 :(得分:3)

该表用于与Intel指令使用的CRC多项式不同的CRC多项式。该表适用于以太网/ ZIP /等。 CRC,通常称为CRC-32。英特尔指令使用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;
}

您可以使用此代码通过简单计算每个单字节消息0,1,2,...,255的CRC-32C来为代码生成替换表。

答案 1 :(得分:0)

FWIW,我已经获得了可以证明与Intel crc32c指令相匹配的SW代码,但是它使用了一个不同的多项式:0x82f63b78该函数绝对与此处的任何iSCSI测试示例都不匹配:https://tools.ietf.org/html/rfc3720#appendix-B.4

令人沮丧的是,我为CRC-32C尝试过的每个实现都带有与其他所有方法不同的哈希值。那里有真正的参考代码吗?

相关问题