Hacker Checksum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
int limit2 = 64; | |
int limit = arr.Length - 32; | |
while (z < limit) | |
{ | |
ulong l1 = *(ulong*)(ptr + z); | |
ulong l2 = *(ulong*)(ptr + z + 8); | |
ulong l3 = *(ulong*)(ptr + z + 16); | |
ulong l4 = *(ulong*)(ptr + z + 24); | |
tmp0 += (l1 & 0x00ff00ff00ff00ff) + (l2 & 0x00ff00ff00ff00ff) | |
+ (l3 & 0x00ff00ff00ff00ff) + (l4 & 0x00ff00ff00ff00ff); | |
tmp1 += ((l1 & 0xff00ff00ff00ff00) >> 8) + ((l2 & 0xff00ff00ff00ff00) >> 8) | |
+ ((l3 & 0xff00ff00ff00ff00) >> 8) + ((l4 & 0xff00ff00ff00ff00) >> 8); | |
limit2--; | |
z += 32; | |
if (limit2 == 0) | |
{ | |
sum0 += (uint)(tmp0 & 0xffff) + (uint)((tmp0 & 0x0000ffff00000000) >> 32); | |
sum1 += (uint)(tmp1 & 0xffff) + (uint)((tmp1 & 0x0000ffff00000000) >> 32); | |
sum2 += (uint)((tmp0 & 0xffff0000) >> 16) + (uint)(tmp0 >> 48); | |
sum3 += (uint)((tmp1 & 0xffff0000) >> 16) + (uint)(tmp1 >> 48); | |
tmp0 = 0; tmp1 = 0; limit2 = 64; | |
} | |
} | |
if (limit2 != 64) | |
{ | |
sum0 += (uint)(tmp0 & 0xffff) + (uint)((tmp0 & 0x0000ffff00000000) >> 32); | |
sum1 += (uint)(tmp1 & 0xffff) + (uint)((tmp1 & 0x0000ffff00000000) >> 32); | |
sum2 += (uint)((tmp0 & 0xffff0000) >> 16) + (uint)(tmp0 >> 48); | |
sum3 += (uint)((tmp1 & 0xffff0000) >> 16) + (uint)(tmp1 >> 48); | |
} | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment