Adding tests for the SSE2 LoadVector128, LoadAlignedVector128, and LoadScalarVector12...
[platform/upstream/coreclr.git] / tests / src / JIT / HardwareIntrinsics / X86 / Sse2 / LoadVector128.cs
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.
4 //
5
6 using System;
7 using System.Runtime.CompilerServices;
8 using System.Runtime.InteropServices;
9 using System.Runtime.Intrinsics.X86;
10 using System.Runtime.Intrinsics;
11
12 namespace IntelHardwareIntrinsicTest
13 {
14     class Program
15     {
16         const int Pass = 100;
17         const int Fail = 0;
18
19         static unsafe int Main(string[] args)
20         {
21             int testResult = Pass;
22
23             if (Sse2.IsSupported)
24             {
25                 using (TestTable<double> doubleTable = new TestTable<double>(new double[2] { 1, -5 }, new double[2]))
26                 {
27                     var vf = Sse2.LoadVector128((double*)(doubleTable.inArrayPtr));
28                     Unsafe.Write(doubleTable.outArrayPtr, vf);
29
30                     if (!doubleTable.CheckResult((x, y) => BitConverter.DoubleToInt64Bits(x) == BitConverter.DoubleToInt64Bits(y)))
31                     {
32                         Console.WriteLine("Sse2 LoadVector128 failed on double:");
33                         foreach (var item in doubleTable.outArray)
34                         {
35                             Console.Write(item + ", ");
36                         }
37                         Console.WriteLine();
38                         testResult = Fail;
39                     }
40                 }
41
42                 using (TestTable<int> intTable = new TestTable<int>(new int[4] { 1, -5, 100, 0 }, new int[4]))
43                 {
44                     var vf = Sse2.LoadVector128((int*)(intTable.inArrayPtr));
45                     Unsafe.Write(intTable.outArrayPtr, vf);
46
47                     if (!intTable.CheckResult((x, y) => x == y))
48                     {
49                         Console.WriteLine("Sse2 LoadVector128 failed on int:");
50                         foreach (var item in intTable.outArray)
51                         {
52                             Console.Write(item + ", ");
53                         }
54                         Console.WriteLine();
55                         testResult = Fail;
56                     }
57                 }
58
59                 using (TestTable<long> longTable = new TestTable<long>(new long[2] { 1, -5 }, new long[2]))
60                 {
61                     var vf = Sse2.LoadVector128((long*)(longTable.inArrayPtr));
62                     Unsafe.Write(longTable.outArrayPtr, vf);
63
64                     if (!longTable.CheckResult((x, y) => x == y))
65                     {
66                         Console.WriteLine("Sse2 LoadVector128 failed on long:");
67                         foreach (var item in longTable.outArray)
68                         {
69                             Console.Write(item + ", ");
70                         }
71                         Console.WriteLine();
72                         testResult = Fail;
73                     }
74                 }
75
76                 using (TestTable<uint> uintTable = new TestTable<uint>(new uint[4] { 1, 5, 100, 0 }, new uint[4]))
77                 {
78                     var vf = Sse2.LoadVector128((uint*)(uintTable.inArrayPtr));
79                     Unsafe.Write(uintTable.outArrayPtr, vf);
80
81                     if (!uintTable.CheckResult((x, y) => x == y))
82                     {
83                         Console.WriteLine("Sse2 LoadVector128 failed on uint:");
84                         foreach (var item in uintTable.outArray)
85                         {
86                             Console.Write(item + ", ");
87                         }
88                         Console.WriteLine();
89                         testResult = Fail;
90                     }
91                 }
92
93                 using (TestTable<ulong> ulongTable = new TestTable<ulong>(new ulong[2] { 1, 5 }, new ulong[2]))
94                 {
95                     var vf = Sse2.LoadVector128((ulong*)(ulongTable.inArrayPtr));
96                     Unsafe.Write(ulongTable.outArrayPtr, vf);
97
98                     if (!ulongTable.CheckResult((x, y) => x == y))
99                     {
100                         Console.WriteLine("Sse2 LoadVector128 failed on ulong:");
101                         foreach (var item in ulongTable.outArray)
102                         {
103                             Console.Write(item + ", ");
104                         }
105                         Console.WriteLine();
106                         testResult = Fail;
107                     }
108                 }
109
110                 using (TestTable<short> shortTable = new TestTable<short>(new short[8] { 1, -5, 100, 0, 1, -5, 100, 0 }, new short[8]))
111                 {
112                     var vf = Sse2.LoadVector128((short*)(shortTable.inArrayPtr));
113                     Unsafe.Write(shortTable.outArrayPtr, vf);
114
115                     if (!shortTable.CheckResult((x, y) => x == y))
116                     {
117                         Console.WriteLine("Sse2 LoadVector128 failed on short:");
118                         foreach (var item in shortTable.outArray)
119                         {
120                             Console.Write(item + ", ");
121                         }
122                         Console.WriteLine();
123                         testResult = Fail;
124                     }
125                 }
126
127                 using (TestTable<ushort> ushortTable = new TestTable<ushort>(new ushort[8] { 1, 5, 100, 0, 1, 5, 100, 0 }, new ushort[8]))
128                 {
129                     var vf = Sse2.LoadVector128((ushort*)(ushortTable.inArrayPtr));
130                     Unsafe.Write(ushortTable.outArrayPtr, vf);
131
132                     if (!ushortTable.CheckResult((x, y) => x == y))
133                     {
134                         Console.WriteLine("Sse2 LoadVector128 failed on ushort:");
135                         foreach (var item in ushortTable.outArray)
136                         {
137                             Console.Write(item + ", ");
138                         }
139                         Console.WriteLine();
140                         testResult = Fail;
141                     }
142                 }
143
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]))
145                 {
146                     var vf = Sse2.LoadVector128((sbyte*)(sbyteTable.inArrayPtr));
147                     Unsafe.Write(sbyteTable.outArrayPtr, vf);
148
149                     if (!sbyteTable.CheckResult((x, y) => x == y))
150                     {
151                         Console.WriteLine("Sse2 LoadVector128 failed on sbyte:");
152                         foreach (var item in sbyteTable.outArray)
153                         {
154                             Console.Write(item + ", ");
155                         }
156                         Console.WriteLine();
157                         testResult = Fail;
158                     }
159                 }
160
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]))
162                 {
163                     var vf = Sse2.LoadVector128((byte*)(byteTable.inArrayPtr));
164                     Unsafe.Write(byteTable.outArrayPtr, vf);
165
166                     if (!byteTable.CheckResult((x, y) => x == y))
167                     {
168                         Console.WriteLine("Sse2 LoadVector128 failed on byte:");
169                         foreach (var item in byteTable.outArray)
170                         {
171                             Console.Write(item + ", ");
172                         }
173                         Console.WriteLine();
174                         testResult = Fail;
175                     }
176                 }
177             }
178
179             return testResult;
180         }
181
182         public unsafe struct TestTable<T> : IDisposable where T : struct
183         {
184             public T[] inArray;
185             public T[] outArray;
186
187             public void* inArrayPtr => inHandle.AddrOfPinnedObject().ToPointer();
188             public void* outArrayPtr => outHandle.AddrOfPinnedObject().ToPointer();
189
190             GCHandle inHandle;
191             GCHandle outHandle;
192             public TestTable(T[] a, T[] b)
193             {
194                 this.inArray = a;
195                 this.outArray = b;
196
197                 inHandle = GCHandle.Alloc(inArray, GCHandleType.Pinned);
198                 outHandle = GCHandle.Alloc(outArray, GCHandleType.Pinned);
199             }
200             public bool CheckResult(Func<T, T, bool> check)
201             {
202                 for (int i = 0; i < inArray.Length; i++)
203                 {
204                     if (!check(inArray[i], outArray[i]))
205                     {
206                         return false;
207                     }
208                 }
209                 return true;
210             }
211
212             public void Dispose()
213             {
214                 inHandle.Free();
215                 outHandle.Free();
216             }
217         }
218     }
219 }