Skip to content

Instantly share code, notes, and snippets.

@MiloszKrajewski
Created January 30, 2023 10:50
Show Gist options
  • Save MiloszKrajewski/fc4a1f43ecebf74128fe59876b37d28a to your computer and use it in GitHub Desktop.
Save MiloszKrajewski/fc4a1f43ecebf74128fe59876b37d28a to your computer and use it in GitHub Desktop.
class UnpackTest
{
byte[] _source;
byte[] _target;
public UnpackTest()
{
int size = 1000 * Vector128<byte>.Count;
var source = new byte[size];
var target = new byte[size * 2];
new Random(0).NextBytes(source);
_source = source;
_target = target;
}
unsafe void TestA()
{
var length = _source.Length;
fixed (byte* s0 = _source)
fixed (byte* t0 = _target)
{
var s = s0;
var t = t0;
var l = s + length;
while (s < l)
{
UnpackA(s, t);
s += 16;
t += 32;
}
}
}
unsafe void TestB()
{
var length = _source.Length;
fixed (byte* s0 = _source)
fixed (byte* t0 = _target)
{
var s = s0;
var t = t0;
var l = s + length;
var z = Vector128<byte>.Zero;
while (s < l)
{
UnpackB(s, t, z);
s += 16;
t += 32;
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static unsafe void UnpackA(byte* source, byte* target)
{
var vector = Sse2.LoadVector128(source);
Sse2.Store(target + 0x00, Sse2.UnpackLow(vector, Vector128<byte>.Zero));
Sse2.Store(target + 0x10, Sse2.UnpackHigh(vector, Vector128<byte>.Zero));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static unsafe void UnpackB(byte* source, byte* target, Vector128<byte> zero)
{
var vector = Sse2.LoadVector128(source);
Sse2.Store(target + 0x00, Sse2.UnpackLow(vector, zero));
Sse2.Store(target + 0x10, Sse2.UnpackHigh(vector, zero));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment