Skip to content

Instantly share code, notes, and snippets.

public unsafe class UnsafeUIntArray
{
public int Length { get; init; }
private readonly uint[] _data;
private readonly uint* _dataPtr;
public UnsafeUIntArray(int length)
{
Length = length;
[DisassemblyDiagnoser(maxDepth: 100)]
public class RandomRead
{
[Params(64, 256, 512, 1024, 4096, 1024 * 16, 1024 * 1024)]
public int ArraySize { get; set; }
private uint[] _safeArray;
private UnsafeUIntArray _unsafeArray;
private int[] _sequence;
; Method Program:ForEachSumExample(int[]):int (FullOpts)
G_M000_IG03: ;; offset=0x000D
mov r10d, edx
add eax, dword ptr [rcx+4*r10+0x10]
inc edx
cmp r8d, edx
jg SHORT G_M000_IG03
G_M000_IG04: ;; offset=0x001C
; Method ArrayAccessExample:Get(int):int:this (FullOpts)
G_M000_IG01: ;; offset=0x0000
sub rsp, 40
G_M000_IG02: ;; offset=0x0004
mov rax, gword ptr [rcx+0x08]
cmp edx, dword ptr [rax+0x08]
jae SHORT G_M000_IG04
mov ecx, edx
mov eax, dword ptr [rax+4*rcx+0x10]
mov eax, ecx
imul rax, rax, 0x663D81
shr rax, 32
mov ecx, 641
div edx:eax, ecx
@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_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_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_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;