Skip to content

Instantly share code, notes, and snippets.

@israellot
israellot / crc32_expert_avx2.cs
Created September 26, 2022 17:04
Expert Checksum with AVX2
public static unsafe uint ChecksumExpertAvx2(ReadOnlySpan<byte> arr)
{
ref byte refSpan = ref MemoryMarshal.GetReference<byte>(arr);
var z = 0;
uint sum = 0;
var vectorSum = Avx2.Xor(Vector256<byte>.Zero, Vector256<byte>.Zero).AsUInt32();
var mask = Vector256.Create((byte)3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12, (byte)3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12);
@israellot
israellot / crc32_hacker.cs
Last active September 26, 2022 16:57
Hacker Checksum
public unsafe static uint ChecksumHacker(ReadOnlySpan<byte> arr)
{
if (arr.Length == 0) return 0;
fixed (byte* ptr = arr)
{
uint sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
uint z = 0;
ulong tmp0 = 0, tmp1 = 0;
@israellot
israellot / crc32_expert.cs
Last active September 26, 2022 16:54
Expert Checksum
public static unsafe uint ChecksumExpert(ReadOnlySpan<byte> arr)
{
if (arr.Length == 0) return 0;
fixed (byte* ptr = arr)
{
uint sum = 0;
int z = 0;
var limit = arr.Length - 32;
@israellot
israellot / crc32_expert_avx.cs
Last active September 26, 2022 16:30
Expert Checksum with AVX
public static unsafe uint ChecksumExpertAvx(ReadOnlySpan<byte> arr)
{
ref byte refSpan = ref MemoryMarshal.GetReference<byte>(arr);
uint z = 0;
uint sum = 0;
var vectorSum = Avx.Xor(Vector128<byte>.Zero, Vector128<byte>.Zero).AsUInt32();
var mask = Vector128.Create((byte)3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12);
@israellot
israellot / crc32_bigendian.cs
Created September 26, 2022 15:26
Expert BigEndian Checksum
public static unsafe uint Checksum(ReadOnlySpan<byte> arr)
{
if (arr.Length == 0) return 0;
fixed (byte* ptr = arr)
{
uint sum = 0;
int z = 0;
var limit = arr.Length - 4;
@israellot
israellot / crc32.cs
Last active September 26, 2022 15:10
Junior Checksum
// Compute a 32-bit big-endian checksum on arr
public static uint Checksum(ReadOnlySpan<byte> arr)
{
if (arr.Length == 0) return 0;
uint sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
for(var i = 0; i<arr.Length; i++)
{
switch (i % 4)
@israellot
israellot / crc32_hacker_expanded.cs
Created September 26, 2022 03:21
Hacker Checksum expanded
public unsafe static uint ChecksumHackerExpanded(ReadOnlySpan<byte> arr)
{
if (arr.Length == 0) return 0;
fixed (byte* ptr = arr)
{
uint sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
uint z = 0;
int rem = arr.Length % 32;
@israellot
israellot / crc32_expert.cs
Created September 26, 2022 01:38
Expert Checksum
public unsafe static uint ChecksumExpert(ReadOnlySpan<byte> arr)
{
if (arr.Length == 0) return 0;
fixed (byte* ptr = arr)
{
uint sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
uint z = 0;
int rem = arr.Length % 32;
@israellot
israellot / pointer_access_example.cs
Last active September 26, 2022 00:22
Example of pointer access with int vs uint
public static unsafe int PointerAccessInt(byte* ptr,int x,int y)
{
return ptr[x]& ptr[x+y];
}
public static unsafe int PointerAccessUInt(byte* ptr,uint x,uint y)
{
return ptr[x]& ptr[x+y];
}
@israellot
israellot / crc32_benchmark.cs
Last active September 25, 2022 20:27
BenchmarkDotNet for the Checksum function
public class CrcBenchmark
{
public byte[]? SourceBytes { get; set; }
[Params(1_000_000)]
public int Length { get; set; }
[GlobalSetup]
public void Setup()
{