From: Fei Peng Date: Thu, 8 Feb 2018 03:30:16 +0000 (-0800) Subject: Add AVX.Compare/CompareScalar tests X-Git-Tag: accepted/tizen/unified/20190422.045933~2860 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3f843fe5b1c28487f1e44f02edf50c025b53b63a;p=platform%2Fupstream%2Fcoreclr.git Add AVX.Compare/CompareScalar tests --- diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Avx/Compare.cs b/tests/src/JIT/HardwareIntrinsics/X86/Avx/Compare.cs new file mode 100644 index 0000000..bae45cc --- /dev/null +++ b/tests/src/JIT/HardwareIntrinsics/X86/Avx/Compare.cs @@ -0,0 +1,219 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +// + +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics.X86; +using System.Runtime.Intrinsics; + +namespace IntelHardwareIntrinsicTest +{ + class Program + { + const int Pass = 100; + const int Fail = 0; + + static unsafe int Main(string[] args) + { + int testResult = Pass; + + if (Avx.IsSupported) + { + using (TestTable floatTable = new TestTable(new float[8] { 1, -5, 100, 0, 1, -5, 100, 0 }, new float[8] { 22, -5, -50, 0, 22, -1, -50, 0 }, new float[8])) + using (TestTable doubleTable = new TestTable(new double[4] { 1, -5, 100, 0 }, new double[4] { 1, 1, 50, 0 }, new double[4])) + { + + var vf1 = Unsafe.Read>(floatTable.inArray1Ptr); + var vf2 = Unsafe.Read>(floatTable.inArray2Ptr); + var vf3 = Avx.Compare(vf1, vf2, FloatComparisonMode.EqualOrderedNonSignaling); + Unsafe.Write(floatTable.outArrayPtr, vf3); + + var vd1 = Unsafe.Read>(doubleTable.inArray1Ptr); + var vd2 = Unsafe.Read>(doubleTable.inArray2Ptr); + var vd3 = Avx.Compare(vd1, vd2, FloatComparisonMode.EqualOrderedNonSignaling); + Unsafe.Write(doubleTable.outArrayPtr, vd3); + + for (int i = 0; i < floatTable.outArray.Length; i++) + { + if (BitConverter.SingleToInt32Bits(floatTable.outArray[i]) != (floatTable.inArray1[i] == floatTable.inArray2[i] ? -1 : 0)) + { + Console.WriteLine("Avx Compare failed on float:"); + foreach (var item in floatTable.outArray) + { + Console.Write(item + ", "); + } + Console.WriteLine(); + return Fail; + } + } + + for (int i = 0; i < doubleTable.outArray.Length; i++) + { + if (BitConverter.DoubleToInt64Bits(doubleTable.outArray[i]) != (doubleTable.inArray1[i] == doubleTable.inArray2[i] ? -1 : 0)) + { + Console.WriteLine("Avx Compare failed on double:"); + foreach (var item in doubleTable.outArray) + { + Console.Write(item + ", "); + } + Console.WriteLine(); + return Fail; + } + } + + var svf1 = Unsafe.Read>(floatTable.inArray1Ptr); + var svf2 = Unsafe.Read>(floatTable.inArray2Ptr); + var svf3 = Avx.Compare(svf1, svf2, FloatComparisonMode.EqualOrderedNonSignaling); + Unsafe.Write(floatTable.outArrayPtr, svf3); + + var svd1 = Unsafe.Read>(doubleTable.inArray1Ptr); + var svd2 = Unsafe.Read>(doubleTable.inArray2Ptr); + var svd3 = Avx.Compare(svd1, svd2, FloatComparisonMode.EqualOrderedNonSignaling); + Unsafe.Write(doubleTable.outArrayPtr, svd3); + + for (int i = 0; i < floatTable.outArray.Length/2; i++) + { + if (BitConverter.SingleToInt32Bits(floatTable.outArray[i]) != (floatTable.inArray1[i] == floatTable.inArray2[i] ? -1 : 0)) + { + Console.WriteLine("Avx Compare Vector128 failed on float:"); + foreach (var item in floatTable.outArray) + { + Console.Write(item + ", "); + } + Console.WriteLine(); + return Fail; + } + } + + + for (int i = 0; i < doubleTable.outArray.Length/2; i++) + { + if (BitConverter.DoubleToInt64Bits(doubleTable.outArray[i]) != (doubleTable.inArray1[i] == doubleTable.inArray2[i] ? -1 : 0)) + { + Console.WriteLine("Avx Compare Vector128 failed on double:"); + foreach (var item in doubleTable.outArray) + { + Console.Write(item + ", "); + } + Console.WriteLine(); + return Fail; + } + } + + try + { + var ve = Avx.Compare(vf1, vf2, (FloatComparisonMode)32); + Unsafe.Write(floatTable.outArrayPtr, ve); + Console.WriteLine("Avx Compare failed on float with out-of-range argument:"); + return Fail; + } + catch (ArgumentOutOfRangeException e) + { + testResult = Pass; + } + + try + { + var ve = Avx.Compare(vd1, vd2, (FloatComparisonMode)32); + Unsafe.Write(floatTable.outArrayPtr, ve); + Console.WriteLine("Avx Compare failed on double with out-of-range argument:"); + return Fail; + } + catch (ArgumentOutOfRangeException e) + { + testResult = Pass; + } + + try + { + var ve = typeof(Avx).GetMethod(nameof(Avx.Compare), new Type[] { typeof(Vector256), typeof(Vector256), typeof(FloatComparisonMode) }) + .Invoke(null, new object[] {vf1, vf2, (FloatComparisonMode)32}); + Console.WriteLine("Indirect-calling Avx Compare failed on float with out-of-range argument:"); + return Fail; + } + catch (System.Reflection.TargetInvocationException e) + { + if (e.InnerException is ArgumentOutOfRangeException) + { + testResult = Pass; + } + else + { + Console.WriteLine("Indirect-calling Avx Compare failed on float with out-of-range argument:"); + return Fail; + } + } + + try + { + var ve = typeof(Avx).GetMethod(nameof(Avx.Compare), new Type[] { typeof(Vector256), typeof(Vector256), typeof(FloatComparisonMode) }) + .Invoke(null, new object[] {vd1, vd2, (FloatComparisonMode)32}); + Console.WriteLine("Indirect-calling Avx Compare failed on double with out-of-range argument:"); + return Fail; + } + catch (System.Reflection.TargetInvocationException e) + { + if (e.InnerException is ArgumentOutOfRangeException) + { + testResult = Pass; + } + else + { + Console.WriteLine("Indirect-calling Avx Compare failed on double with out-of-range argument:"); + return Fail; + } + } + } + } + + return testResult; + } + + public unsafe struct TestTable : IDisposable where T : struct + { + public T[] inArray1; + public T[] inArray2; + public T[] outArray; + + public void* inArray1Ptr => inHandle1.AddrOfPinnedObject().ToPointer(); + public void* inArray2Ptr => inHandle2.AddrOfPinnedObject().ToPointer(); + public void* outArrayPtr => outHandle.AddrOfPinnedObject().ToPointer(); + + GCHandle inHandle1; + GCHandle inHandle2; + GCHandle outHandle; + public TestTable(T[] a, T[] b, T[] c) + { + this.inArray1 = a; + this.inArray2 = b; + this.outArray = c; + + inHandle1 = GCHandle.Alloc(inArray1, GCHandleType.Pinned); + inHandle2 = GCHandle.Alloc(inArray2, GCHandleType.Pinned); + outHandle = GCHandle.Alloc(outArray, GCHandleType.Pinned); + } + public bool CheckResult(Func check) + { + for (int i = 0; i < inArray1.Length; i++) + { + if (!check(inArray1[i], inArray2[i], outArray[i])) + { + return false; + } + } + return true; + } + + public void Dispose() + { + inHandle1.Free(); + inHandle2.Free(); + outHandle.Free(); + } + } + + } +} \ No newline at end of file diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Avx/CompareScalar.cs b/tests/src/JIT/HardwareIntrinsics/X86/Avx/CompareScalar.cs new file mode 100644 index 0000000..255b3e6 --- /dev/null +++ b/tests/src/JIT/HardwareIntrinsics/X86/Avx/CompareScalar.cs @@ -0,0 +1,202 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +// + +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics.X86; +using System.Runtime.Intrinsics; + +namespace IntelHardwareIntrinsicTest +{ + class Program + { + const int Pass = 100; + const int Fail = 0; + + static unsafe int Main(string[] args) + { + int testResult = Pass; + + if (Avx.IsSupported) + { + using (TestTable floatTable = new TestTable(new float[8] { 1, -5, 100, 0, 1, -5, 100, 0 }, new float[8] { 22, -5, -50, 0, 22, -1, -50, 0 }, new float[8])) + using (TestTable doubleTable = new TestTable(new double[4] { 1, -5, 100, 0 }, new double[4] { 1, 1, 50, 0 }, new double[4])) + { + + var vf1 = Unsafe.Read>(floatTable.inArray1Ptr); + var vf2 = Unsafe.Read>(floatTable.inArray2Ptr); + var vf3 = Avx.CompareScalar(vf1, vf2, FloatComparisonMode.EqualOrderedNonSignaling); + Unsafe.Write(floatTable.outArrayPtr, vf3); + + var vd1 = Unsafe.Read>(doubleTable.inArray1Ptr); + var vd2 = Unsafe.Read>(doubleTable.inArray2Ptr); + var vd3 = Avx.CompareScalar(vd1, vd2, FloatComparisonMode.EqualOrderedNonSignaling); + Unsafe.Write(doubleTable.outArrayPtr, vd3); + + + if (BitConverter.SingleToInt32Bits(floatTable.outArray[0]) != (floatTable.inArray1[0] == floatTable.inArray2[0] ? -1 : 0)) + { + Console.WriteLine("Avx CompareScalar failed on float:"); + foreach (var item in floatTable.outArray) + { + Console.Write(item + ", "); + } + Console.WriteLine(); + return Fail; + } + for (int i = 1; i < 4; i++) + { + if (floatTable.outArray[i] != floatTable.inArray1[i]) + { + Console.WriteLine("Avx CompareScalar failed on float:"); + foreach (var item in floatTable.outArray) + { + Console.Write(item + ", "); + } + Console.WriteLine(); + return Fail; + } + } + + + if (BitConverter.DoubleToInt64Bits(doubleTable.outArray[0]) != (doubleTable.inArray1[0] == doubleTable.inArray2[0] ? -1 : 0)) + { + Console.WriteLine("Avx CompareScalar failed on double:"); + foreach (var item in doubleTable.outArray) + { + Console.Write(item + ", "); + } + Console.WriteLine(); + return Fail; + } + for (int i = 1; i < 2; i++) + { + if (doubleTable.outArray[i] != doubleTable.inArray1[i]) + { + Console.WriteLine("Avx CompareScalar failed on double:"); + foreach (var item in doubleTable.outArray) + { + Console.Write(item + ", "); + } + Console.WriteLine(); + return Fail; + } + } + + try + { + var ve = Avx.CompareScalar(vf1, vf2, (FloatComparisonMode)32); + Unsafe.Write(floatTable.outArrayPtr, ve); + Console.WriteLine("Avx CompareScalar failed on float with out-of-range argument:"); + return Fail; + } + catch (ArgumentOutOfRangeException e) + { + testResult = Pass; + } + + try + { + var ve = Avx.CompareScalar(vd1, vd2, (FloatComparisonMode)32); + Unsafe.Write(floatTable.outArrayPtr, ve); + Console.WriteLine("Avx CompareScalar failed on double with out-of-range argument:"); + return Fail; + } + catch (ArgumentOutOfRangeException e) + { + testResult = Pass; + } + + try + { + var ve = typeof(Avx).GetMethod(nameof(Avx.CompareScalar), new Type[] { typeof(Vector128), typeof(Vector128), typeof(FloatComparisonMode) }) + .Invoke(null, new object[] {vf1, vf2, (FloatComparisonMode)32}); + Console.WriteLine("Indirect-calling Avx CompareScalar failed on float with out-of-range argument:"); + return Fail; + } + catch (System.Reflection.TargetInvocationException e) + { + if (e.InnerException is ArgumentOutOfRangeException) + { + testResult = Pass; + } + else + { + Console.WriteLine("Indirect-calling Avx CompareScalar failed on float with out-of-range argument:"); + return Fail; + } + } + + try + { + var ve = typeof(Avx).GetMethod(nameof(Avx.CompareScalar), new Type[] { typeof(Vector128), typeof(Vector128), typeof(FloatComparisonMode) }) + .Invoke(null, new object[] {vd1, vd2, (FloatComparisonMode)32}); + Console.WriteLine("Indirect-calling Avx CompareScalar failed on double with out-of-range argument:"); + return Fail; + } + catch (System.Reflection.TargetInvocationException e) + { + if (e.InnerException is ArgumentOutOfRangeException) + { + testResult = Pass; + } + else + { + Console.WriteLine("Indirect-calling Avx CompareScalar failed on double with out-of-range argument:"); + return Fail; + } + } + } + } + + return testResult; + } + + public unsafe struct TestTable : IDisposable where T : struct + { + public T[] inArray1; + public T[] inArray2; + public T[] outArray; + + public void* inArray1Ptr => inHandle1.AddrOfPinnedObject().ToPointer(); + public void* inArray2Ptr => inHandle2.AddrOfPinnedObject().ToPointer(); + public void* outArrayPtr => outHandle.AddrOfPinnedObject().ToPointer(); + + GCHandle inHandle1; + GCHandle inHandle2; + GCHandle outHandle; + public TestTable(T[] a, T[] b, T[] c) + { + this.inArray1 = a; + this.inArray2 = b; + this.outArray = c; + + inHandle1 = GCHandle.Alloc(inArray1, GCHandleType.Pinned); + inHandle2 = GCHandle.Alloc(inArray2, GCHandleType.Pinned); + outHandle = GCHandle.Alloc(outArray, GCHandleType.Pinned); + } + public bool CheckResult(Func check) + { + for (int i = 0; i < inArray1.Length; i++) + { + if (!check(inArray1[i], inArray2[i], outArray[i])) + { + return false; + } + } + return true; + } + + public void Dispose() + { + inHandle1.Free(); + inHandle2.Free(); + outHandle.Free(); + } + } + + } +} \ No newline at end of file diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Avx/CompareScalar_r.csproj b/tests/src/JIT/HardwareIntrinsics/X86/Avx/CompareScalar_r.csproj new file mode 100644 index 0000000..1404d28 --- /dev/null +++ b/tests/src/JIT/HardwareIntrinsics/X86/Avx/CompareScalar_r.csproj @@ -0,0 +1,34 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + true + + + + + + + False + + + + None + + + + + + + + + + + diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Avx/CompareScalar_ro.csproj b/tests/src/JIT/HardwareIntrinsics/X86/Avx/CompareScalar_ro.csproj new file mode 100644 index 0000000..dbefd49 --- /dev/null +++ b/tests/src/JIT/HardwareIntrinsics/X86/Avx/CompareScalar_ro.csproj @@ -0,0 +1,34 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + true + + + + + + + False + + + + None + True + + + + + + + + + + diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Avx/Compare_r.csproj b/tests/src/JIT/HardwareIntrinsics/X86/Avx/Compare_r.csproj new file mode 100644 index 0000000..cf47b4a --- /dev/null +++ b/tests/src/JIT/HardwareIntrinsics/X86/Avx/Compare_r.csproj @@ -0,0 +1,34 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + true + + + + + + + False + + + + None + + + + + + + + + + + diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Avx/Compare_ro.csproj b/tests/src/JIT/HardwareIntrinsics/X86/Avx/Compare_ro.csproj new file mode 100644 index 0000000..962807a --- /dev/null +++ b/tests/src/JIT/HardwareIntrinsics/X86/Avx/Compare_ro.csproj @@ -0,0 +1,34 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + true + + + + + + + False + + + + None + True + + + + + + + + + +