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
View crc32_expert_avx2.cs
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_expert_avx.cs
Last active September 26, 2022 16:30
Expert Checksum with AVX
View crc32_expert_avx.cs
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_expert.cs
Last active September 26, 2022 16:54
Expert Checksum
View crc32_expert.cs
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_bigendian.cs
Created September 26, 2022 15:26
Expert BigEndian Checksum
View crc32_bigendian.cs
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_hacker_expanded.cs
Created September 26, 2022 03:21
Hacker Checksum expanded
View crc32_hacker_expanded.cs
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;
View crc32_hacker.asm
G_M000_IG06: ;; offset=007BH
mov r15d, r11d
mov r12, qword ptr [rdx+r15]
mov r13, qword ptr [rdx+r15+08H]
mov rdi, qword ptr [rdx+r15+10H]
mov r15, qword ptr [rdx+r15+18H]
mov rcx, 0xFF00FF00FF00FF
and rcx, r12
add rsi, rcx
mov rcx, 0xFF00FF00FF00FF
@israellot
israellot / crc32_hacker.cs
Last active September 26, 2022 16:57
Hacker Checksum
View crc32_hacker.cs
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
Created September 26, 2022 01:38
Expert Checksum
View crc32_expert.cs
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 / example.asm
Last active October 1, 2022 21:06
Pro vs Senior asm output
View example.asm
//while (z < limit)
// {
// sum0 += (uint)(ptr[z + 0] + ptr[z + 4] + ptr[z + 8] + ptr[z + 12]
//PRO
G_M000_IG06:
cmp r11d, ecx
jae G_M000_IG17
mov ebx, r11d
movzx rbx, byte ptr [rdx+rbx]
@israellot
israellot / pointer_access_example.cs
Last active September 26, 2022 00:22
Example of pointer access with int vs uint
View pointer_access_example.cs
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];
}