Skip to content

Instantly share code, notes, and snippets.

@Frityet
Created May 4, 2022 02:28
Show Gist options
  • Save Frityet/243d0a49f5b95eb21591ac98ece54d10 to your computer and use it in GitHub Desktop.
Save Frityet/243d0a49f5b95eb21591ac98ece54d10 to your computer and use it in GitHub Desktop.
namespace CSharpSpeedTest
{
public static class MemoryCopy
{
public static unsafe void Fast(void* dst, void* src, ulong n)
{
var offset = 0;
while (n != 0)
{
if (n % sizeof(ulong) * 8 == 0)
{
//8 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//16 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//24 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//32 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//40 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//48 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//56 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//64 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
n -= 64;
} else if (n % sizeof(ulong) * 7 == 0)
{
//8 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//16 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//24 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//32 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//40 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//48 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//56 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
} else if (n % sizeof(ulong) * 6 == 0)
{
//8 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//16 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//24 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//32 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//40 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//48 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
} else if (n % sizeof(ulong) * 5 == 0)
{
//8 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//16 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//24 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//32 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//40 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
} else if (n % sizeof(ulong) * 4 == 0)
{
//8 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//16 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//24 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//32 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
} else if (n % sizeof(ulong) * 3 == 0)
{
//8 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//16 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//24 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
} else if (n % sizeof(ulong) * 2 == 0)
{
//8 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
//16 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
} else if (n % sizeof(ulong) == 0)
{
//8 bytes
*((ulong*) dst + offset) = *((ulong*) src + offset);
offset += sizeof(ulong);
} else if (n % sizeof(uint) + sizeof(ushort) == 0)
{
//4 bytes
*((uint*) dst + offset) = *((uint*) src + offset);
offset += sizeof(uint);
//2 bytes
*((ushort*) dst + offset) = *((ushort*) src + offset);
offset += sizeof(ushort);
} else if (n % sizeof(uint) == 0)
{
//4 bytes
*((uint*) dst + offset) = *((uint*) src + offset);
offset += sizeof(uint);
} else if (n % sizeof(ushort) == 0)
{
//2 bytes
*((ushort*) dst + offset) = *((ushort*) src + offset);
offset += sizeof(ushort);
}
else
{
//1 byte
*((byte*) dst + offset) = *((byte*) src + offset);
offset += sizeof(byte);
}
}
}
public static unsafe void Normal(void* dst, void* src, ulong n)
{
for (ulong i = 0; i < n; i++)
*((byte*) dst + i) = *((byte*) src + i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment