1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
7 using System.Runtime.CompilerServices;
8 using System.Runtime.InteropServices;
9 using System.Runtime.Intrinsics.X86;
10 using System.Runtime.Intrinsics;
12 namespace IntelHardwareIntrinsicTest
19 static unsafe int Main(string[] args)
21 int testResult = Pass;
25 using (TestTable<double> doubleTable = new TestTable<double>(new double[2] { 1, -5 }, new double[2]))
27 var vf = Sse2.LoadAlignedVector128((double*)(doubleTable.inArrayPtr));
28 Unsafe.Write(doubleTable.outArrayPtr, vf);
30 if (!doubleTable.CheckResult((x, y) => BitConverter.DoubleToInt64Bits(x) == BitConverter.DoubleToInt64Bits(y)))
32 Console.WriteLine("Sse2 LoadAlignedVector128 failed on double:");
33 foreach (var item in doubleTable.outArray)
35 Console.Write(item + ", ");
42 using (TestTable<int> intTable = new TestTable<int>(new int[4] { 1, -5, 100, 0 }, new int[4]))
44 var vf = Sse2.LoadAlignedVector128((int*)(intTable.inArrayPtr));
45 Unsafe.Write(intTable.outArrayPtr, vf);
47 if (!intTable.CheckResult((x, y) => x == y))
49 Console.WriteLine("Sse2 LoadAlignedVector128 failed on int:");
50 foreach (var item in intTable.outArray)
52 Console.Write(item + ", ");
59 using (TestTable<long> longTable = new TestTable<long>(new long[2] { 1, -5 }, new long[2]))
61 var vf = Sse2.LoadAlignedVector128((long*)(longTable.inArrayPtr));
62 Unsafe.Write(longTable.outArrayPtr, vf);
64 if (!longTable.CheckResult((x, y) => x == y))
66 Console.WriteLine("Sse2 LoadAlignedVector128 failed on long:");
67 foreach (var item in longTable.outArray)
69 Console.Write(item + ", ");
76 using (TestTable<uint> uintTable = new TestTable<uint>(new uint[4] { 1, 5, 100, 0 }, new uint[4]))
78 var vf = Sse2.LoadAlignedVector128((uint*)(uintTable.inArrayPtr));
79 Unsafe.Write(uintTable.outArrayPtr, vf);
81 if (!uintTable.CheckResult((x, y) => x == y))
83 Console.WriteLine("Sse2 LoadAlignedVector128 failed on uint:");
84 foreach (var item in uintTable.outArray)
86 Console.Write(item + ", ");
93 using (TestTable<ulong> ulongTable = new TestTable<ulong>(new ulong[2] { 1, 5 }, new ulong[2]))
95 var vf = Sse2.LoadAlignedVector128((ulong*)(ulongTable.inArrayPtr));
96 Unsafe.Write(ulongTable.outArrayPtr, vf);
98 if (!ulongTable.CheckResult((x, y) => x == y))
100 Console.WriteLine("Sse2 LoadAlignedVector128 failed on ulong:");
101 foreach (var item in ulongTable.outArray)
103 Console.Write(item + ", ");
110 using (TestTable<short> shortTable = new TestTable<short>(new short[8] { 1, -5, 100, 0, 1, -5, 100, 0 }, new short[8]))
112 var vf = Sse2.LoadAlignedVector128((short*)(shortTable.inArrayPtr));
113 Unsafe.Write(shortTable.outArrayPtr, vf);
115 if (!shortTable.CheckResult((x, y) => x == y))
117 Console.WriteLine("Sse2 LoadAlignedVector128 failed on short:");
118 foreach (var item in shortTable.outArray)
120 Console.Write(item + ", ");
127 using (TestTable<ushort> ushortTable = new TestTable<ushort>(new ushort[8] { 1, 5, 100, 0, 1, 5, 100, 0 }, new ushort[8]))
129 var vf = Sse2.LoadAlignedVector128((ushort*)(ushortTable.inArrayPtr));
130 Unsafe.Write(ushortTable.outArrayPtr, vf);
132 if (!ushortTable.CheckResult((x, y) => x == y))
134 Console.WriteLine("Sse2 LoadAlignedVector128 failed on ushort:");
135 foreach (var item in ushortTable.outArray)
137 Console.Write(item + ", ");
144 using (TestTable<sbyte> sbyteTable = new TestTable<sbyte>(new sbyte[16] { 1, -5, 100, 0, 1, -5, 100, 0, 1, -5, 100, 0, 1, -5, 100, 0 }, new sbyte[16]))
146 var vf = Sse2.LoadAlignedVector128((sbyte*)(sbyteTable.inArrayPtr));
147 Unsafe.Write(sbyteTable.outArrayPtr, vf);
149 if (!sbyteTable.CheckResult((x, y) => x == y))
151 Console.WriteLine("Sse2 LoadAlignedVector128 failed on sbyte:");
152 foreach (var item in sbyteTable.outArray)
154 Console.Write(item + ", ");
161 using (TestTable<byte> byteTable = new TestTable<byte>(new byte[16] { 1, 5, 100, 0, 1, 5, 100, 0, 1, 5, 100, 0, 1, 5, 100, 0 }, new byte[16]))
163 var vf = Sse2.LoadAlignedVector128((byte*)(byteTable.inArrayPtr));
164 Unsafe.Write(byteTable.outArrayPtr, vf);
166 if (!byteTable.CheckResult((x, y) => x == y))
168 Console.WriteLine("Sse2 LoadAlignedVector128 failed on byte:");
169 foreach (var item in byteTable.outArray)
171 Console.Write(item + ", ");
182 public unsafe struct TestTable<T> : IDisposable where T : struct
184 private byte[] inArray;
187 private GCHandle inHandle;
188 private GCHandle outHandle;
190 private byte simdSize;
192 public TestTable(T[] a, T[] b)
194 this.inArray = new byte[32];
197 this.inHandle = GCHandle.Alloc(this.inArray, GCHandleType.Pinned);
198 this.outHandle = GCHandle.Alloc(this.outArray, GCHandleType.Pinned);
202 Unsafe.CopyBlockUnaligned(ref Unsafe.AsRef<byte>(inArrayPtr), ref Unsafe.As<T, byte>(ref a[0]), this.simdSize);
205 public void* inArrayPtr => Align((byte*)(inHandle.AddrOfPinnedObject().ToPointer()), simdSize);
206 public void* outArrayPtr => outHandle.AddrOfPinnedObject().ToPointer();
208 public bool CheckResult(Func<T, T, bool> check)
210 for (int i = 0; i < outArray.Length; i++)
212 if (!check(Unsafe.Add<T>(ref Unsafe.AsRef<T>(inArrayPtr), i), outArray[i]))
220 public void Dispose()
226 private static unsafe void* Align(byte* buffer, byte expectedAlignment)
228 // Compute how bad the misalignment is, which is at most (expectedAlignment - 1).
229 // Then subtract that from the expectedAlignment and add it to the original address
230 // to compute the aligned address.
232 var misalignment = expectedAlignment - ((ulong)(buffer) % expectedAlignment);
233 return (void*)(buffer + misalignment);