在c#中使用SSE 4.2 crc32算法?可能吗?

时间:2011-08-11 14:18:26

标签: c# cryptography sse crc32

我必须在很多文件上计算crc32,还有大文件(几GB)。我在网上找到了几个algo,例如Damiengthis one,但它很有效,但速度很慢(超过1分钟)。 我在各种crc32算法上找到了this benchmark,发现sse 4.2有硬件加速的crc32方法。

我没有找到任何使用SSE crc32代码的c#代码示例。

1 - 有可能吗?

2 - 如何检测当前的cpu是否支持SSE4.2? (切换crc32方法)

(请尽可能编码样本)

2 个答案:

答案 0 :(得分:2)

如今,我们已被.NET Core 3.0中可用的System.Runtime.Intrinsics.X86名称空间所困扰。这是使用SSE 4.2的CRC32-C算法的完整实现:​​

using System;
using System.Runtime.Intrinsics.X86;
using System.Security.Cryptography;

/// <summary>
/// The hardware implementation of the CRC32-C polynomial 
/// implemented on Intel CPUs supporting SSE4.2.
/// </summary>
public class Crc32HardwareAlgorithm : HashAlgorithm
{
    /// <summary>
    /// the current CRC value, bit-flipped
    /// </summary>
    private uint _crc;

    /// <summary>
    /// We can further optimize the algorithm when X64 is available.
    /// </summary>
    private bool _x64Available;

    /// <summary>
    /// Default constructor
    /// </summary>
    public Crc32HardwareAlgorithm()
    {
        if (!Sse42.IsSupported)
        {
            throw new NotSupportedException("SSE4.2 is not supported");
        }

        _x64Available = Sse42.X64.IsSupported;

        // The size, in bits, of the computed hash code.
        this.HashSizeValue = 32;
        this.Reset();
    }

    /// <summary>When overridden in a derived class, routes data written to the object into the hash algorithm for computing the hash.</summary>
    /// <param name="array">The input to compute the hash code for.</param>
    /// <param name="ibStart">The offset into the byte array from which to begin using data.</param>
    /// <param name="cbSize">The number of bytes in the byte array to use as data.</param>
    protected override void HashCore(byte[] array, int ibStart, int cbSize)
    {
        if (_x64Available)
        {
            while (cbSize >= 8)
            {
                _crc = (uint)Sse42.X64.Crc32(_crc, BitConverter.ToUInt64(array, ibStart));
                ibStart += 8;
                cbSize -= 8;
            }
        }

        while (cbSize > 0)
        {
            _crc = Sse42.Crc32(_crc, array[ibStart]);
            ibStart++;
            cbSize--;
        }
    }

    /// <summary>When overridden in a derived class, finalizes the hash computation after the last data is processed by the cryptographic stream object.</summary>
    /// <returns>The computed hash code.</returns>
    protected override byte[] HashFinal()
    {
        uint outputCrcValue = ~_crc;

        return BitConverter.GetBytes(outputCrcValue);
    }

    /// <summary>Initializes an implementation of the <see cref="T:System.Security.Cryptography.HashAlgorithm"></see> class.</summary>
    public override void Initialize()
    {
        this.Reset();
    }

    private void Reset()
    {
        _crc = uint.MaxValue;
    }
}

答案 1 :(得分:1)

我相信Mono允许通过Mono.Simd命名空间访问CPU指令:

http://tirania.org/blog/archive/2008/Nov-03.html

Stackoverflow相关问题:

Using SSE in c# is it possible?

Mono代码是开源的。看起来你不能只是将它添加到.NET项目中以获得好处,因为它似乎需要Mono运行时:

Calling mono c# code from Microsoft .net?

尽管如此,它会起作用,但它将是软件模拟的。