_mm_crc32_u8提供的结果与参考代码不同 [英] _mm_crc32_u8 gives different result than reference code

查看:351
本文介绍了_mm_crc32_u8提供的结果与参考代码不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力处理内在函数。特别是,使用标准CRC计算和据说等价的intel内在函数,我不会得到相同的结果。我想转到使用 _mm_crc32_u16 _mm_crc32_u32 的方法,但是如果我无法使用8位操作工作没有任何意义。

I've been struggling with the intrinsics. In particular I don't get the same results using the standard CRC calculation and the supposedly equivalent intel intrinsics. I'd like to move to using _mm_crc32_u16, and _mm_crc32_u32 but if I can't get the 8 bit operation to work there's no point.

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;
}


推荐答案

该表用于其他CRC多项式比Intel指令使用的多项式大。该表适用于Ethernet / ZIP / etc。 CRC,通常称为CRC-32。英特尔指令对CRC(通常称为CRC-32C)使用iSCSI(Castagnoli)多项式。

That table is for a different CRC polynomial than the one used by the Intel instruction. The table is for the Ethernet/ZIP/etc. CRC, often referred to as CRC-32. The Intel instruction uses the iSCSI (Castagnoli) polynomial, for the CRC often referred to as CRC-32C.

这个简短的示例代码可以通过不注释所需的多项式来计算任一个:

This short example code can calculate either, by uncommenting the desired polynomial:

#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。

You can use this code to generate a replacement table for your code by simply computing the CRC-32C of each of the one-byte messages 0, 1, 2, ..., 255.

这篇关于_mm_crc32_u8提供的结果与参考代码不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆