Skip to content

Instantly share code, notes, and snippets.

@SwapnilGaikwad
Last active September 18, 2023 10:17
Show Gist options
  • Save SwapnilGaikwad/552b5f51c343248db178cf1274bfeb55 to your computer and use it in GitHub Desktop.
Save SwapnilGaikwad/552b5f51c343248db178cf1274bfeb55 to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using Xunit;
/*
* Step to execute this example:
* Create a console application named testSt2
* Replace Program.cs with this file
* Move testSt2 dir in src/tests/JIT/HardwareIntrinsics/Arm/
* Compile: dotnet build src/tests/JIT/HardwareIntrinsics/Arm/testSt2/testSt2.csproj -c Release
* Run: DOTNET_TieredCompilation=0 DOTNET_JitDisasm="RunTestScenario" $CORE_ROOT/corerun /home/ent-user/dotnet/runtime/artifacts/tests/coreclr/linux.arm64.Release/JIT/HardwareIntrinsics/Arm/testSt2/testSt2/testSt2.dll
*/
namespace JIT.HardwareIntrinsics.Arm
{
public static partial class Program
{
[Fact]
public static void StoreVector128x2_Int32()
{
var test = new StoreVector128x2Test();
test.RunTestScenario();
}
}
public sealed unsafe class StoreVector128x2Test
{
private struct DataTable
{
private byte[] inArray1;
private byte[] inArray2;
private byte[] outArray;
private GCHandle inHandle1;
private GCHandle inHandle2;
private GCHandle outHandle;
private ulong alignment;
public DataTable(Int32[] outArray, int alignment)
{
int sizeOfoutArray = outArray.Length * Unsafe.SizeOf<Int32>();
if ((alignment != 16 && alignment != 8) || (alignment * 2) < sizeOfoutArray)
{
throw new ArgumentException("Invalid value of alignment");
}
this.outArray = new byte[alignment * 4];
this.outHandle = GCHandle.Alloc(this.outArray, GCHandleType.Pinned);
this.alignment = (ulong)alignment;
}
public void* outArrayPtr => Align((byte*)(outHandle.AddrOfPinnedObject().ToPointer()), alignment);
public void Dispose()
{
outHandle.Free();
}
private static unsafe void* Align(byte* buffer, ulong expectedAlignment)
{
return (void*)(((ulong)buffer + expectedAlignment - 1) & ~(expectedAlignment - 1));
}
}
private static readonly int LargestVectorSize = 16;
private static readonly int OpElementCount = Unsafe.SizeOf<Vector128<Int32>>() / sizeof(Int32);
private static readonly int DestElementCount = OpElementCount * 2;
private static Int32[] _data1 = new Int32[OpElementCount];
private static Int32[] _data2 = new Int32[OpElementCount];
private Vector128<Int32> _fld1;
private Vector128<Int32> _fld2;
private DataTable _dataTable;
public StoreVector128x2Test()
{
for (var i = 0; i < OpElementCount; i++) { _data1[i] = TestLibrary.Generator.GetInt32(); _data2[i] = TestLibrary.Generator.GetInt32(); }
Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Int32>, byte>(ref _fld1), ref Unsafe.As<Int32, byte>(ref _data1[0]), (uint)Unsafe.SizeOf<Vector128<Int32>>());
Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Int32>, byte>(ref _fld2), ref Unsafe.As<Int32, byte>(ref _data2[0]), (uint)Unsafe.SizeOf<Vector128<Int32>>());
_dataTable = new DataTable(new Int32[DestElementCount], LargestVectorSize);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void RunTestScenario()
{
AdvSimd.Arm64.StoreVector128x2((Int32*)_dataTable.outArrayPtr, (_fld1, _fld2));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment