Skip to content

Instantly share code, notes, and snippets.

@george-polevoy
Created September 7, 2022 17:00
Show Gist options
  • Save george-polevoy/9b4aaf8f4651789959ee0b693bd1345b to your computer and use it in GitHub Desktop.
Save george-polevoy/9b4aaf8f4651789959ee0b693bd1345b to your computer and use it in GitHub Desktop.
SpanResultingFromStructRef
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Explicit)]
struct FieldList
{
[FieldOffset(0)] private int _item0;
[FieldOffset(4)] private int _item1;
[FieldOffset(8)] private int _item2;
[FieldOffset(12)] private int _item3;
[FieldOffset(16)] private int _item4;
[FieldOffset(20)] private int _item5;
[FieldOffset(24)] private int _item6;
[FieldOffset(28)] private int _item7;
private void ShiftLocalsLeft_Switch(int startingAt)
{
switch (startingAt)
{
case 0: goto CASE0;
case 1: goto CASE1;
case 2: goto CASE2;
case 3: goto CASE3;
case 4: goto CASE4;
case 5: goto CASE5;
case 6: goto CASE6;
case 7: goto CASE7;
}
goto END;
CASE0:_item0 = _item1;;
CASE1:_item1 = _item2;
CASE2:_item2 = _item3;
CASE3:_item3 = _item4;
CASE4:_item4 = _item5;
CASE5:_item5 = _item6;
CASE6:_item6 = _item7;
CASE7:_item7 = default;
END:;
}
private void ShiftLocalsLeft_SpanFor(int startingAt)
{
Span<int> valSpan = MemoryMarshal.CreateSpan(ref _item0, 8);
for (var i = startingAt; i < 7; i++)
{
valSpan[i] = valSpan[i+1];
}
}
private void ShiftLocalsLeft_SpanOperations(int startingAt)
{
Span<int> valSpan = MemoryMarshal.CreateSpan(ref _item0, 8);
valSpan.Slice(startingAt).CopyTo(valSpan.Slice(startingAt - 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment