__ mm_crc32_u8提供的结果与参考代码不同

问题描述 投票:3回答:2

我一直在努力处理内在函数。特别是,使用标准CRC计算和supposedly等效int内在函数,我不会得到相同的结果。我想转而使用_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;
}
c crc
2个回答
3
投票

该表针对的是不同于Intel指令的CRC多项式。该表适用于Ethernet / ZIP / etc。 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,使用此代码为代码生成替换表。


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.