Skip to content

Instantly share code, notes, and snippets.

@Drawaes
Created April 5, 2020 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drawaes/114eed38b52c5c8cac963886b8f30ca0 to your computer and use it in GitHub Desktop.
Save Drawaes/114eed38b52c5c8cac963886b8f30ca0 to your computer and use it in GitHub Desktop.
CalcCRC
var remainder = outputSpan.Length;
ref var pointer = ref MemoryMarshal.GetReference(span);
var currentCrc = (uint)0;
while(remainder >= 4)
{
var uintValue = Unsafe.As<byte, uint>(ref pointer);
currentCrc = System.Runtime.Intrinsics.X86.Sse42.Crc32(currentCrc, uintValue);
pointer = Unsafe.Add(ref pointer, 4);
remainder -= 4;
}
while(remainder > 0)
{
currentCrc = System.Runtime.Intrinsics.X86.Sse42.Crc32(currentCrc, pointer);
pointer = Unsafe.Add(ref pointer, 1);
remainder -= 1;
}
@wiz0u
Copy link

wiz0u commented Aug 24, 2021

If i'm correct, to compute a CRC32C, currentCrc should be initialized with ~0U and set to ~currentCrc at the end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment