diff --git a/src/benchmarks/micro/MicroBenchmarks.csproj b/src/benchmarks/micro/MicroBenchmarks.csproj index b6a2f3afcbf..3b84b71f332 100644 --- a/src/benchmarks/micro/MicroBenchmarks.csproj +++ b/src/benchmarks/micro/MicroBenchmarks.csproj @@ -10,6 +10,8 @@ $(NoWarn);CS8002 $(NoWarn);SYSLIB0011 + + $(NoWarn);SYSLIB5003 Exe AnyCPU portable diff --git a/src/benchmarks/micro/sve/Partition.cs b/src/benchmarks/micro/sve/Partition.cs index 71444c0e52e..162a9499717 100644 --- a/src/benchmarks/micro/sve/Partition.cs +++ b/src/benchmarks/micro/sve/Partition.cs @@ -1,5 +1,3 @@ -#pragma warning disable SYSLIB5003 - using System; using System.Numerics; using System.Runtime.Intrinsics; @@ -74,131 +72,120 @@ public unsafe ulong Scalar() [Benchmark] public unsafe ulong SvePartition() { - if (Sve.IsSupported) + fixed (uint* input = _input, left = _left, right = _right) { - fixed (uint* input = _input, left = _left, right = _right) - { - long i = 0; + long i = 0; - ulong indexLeft = 0; - ulong indexRight = 0; + ulong indexLeft = 0; + ulong indexRight = 0; - Vector ones = Vector.One; + Vector ones = Vector.One; - Vector firstElemVec = Sve.DuplicateSelectedScalarToVector( - Sve.LoadVector(Sve.CreateTrueMaskUInt32(), input), 0 - ); + Vector firstElemVec = Sve.DuplicateSelectedScalarToVector( + Sve.LoadVector(Sve.CreateTrueMaskUInt32(), input), 0 + ); - // Create a predicate for the loop. - Vector pLoop = Sve.CreateWhileLessThanMask32Bit(i, Size); + // Create a predicate for the loop. + Vector pLoop = Sve.CreateWhileLessThanMask32Bit(i, Size); - while (Sve.TestAnyTrue(Sve.CreateTrueMaskUInt32(), pLoop)) - { - // Load from the input array based on the loop predicate. - Vector data = Sve.LoadVector(pLoop, input + i); - - // Predicate for elements in input array less than the first element. - Vector pCompare = Sve.CompareLessThan(data, firstElemVec); + while (Sve.TestAnyTrue(Sve.CreateTrueMaskUInt32(), pLoop)) + { + // Load from the input array based on the loop predicate. + Vector data = Sve.LoadVector(pLoop, input + i); - // Apply the pLoop mask. - Vector pInner = Sve.ConditionalSelect(pLoop, pCompare, Vector.Zero); + // Predicate for elements in input array less than the first element. + Vector pCompare = Sve.CompareLessThan(data, firstElemVec); - // Squash all found elements to the lower lanes of the vector. - Vector compacted = Sve.Compact(pInner, data); + // Apply the pLoop mask. + Vector pInner = Sve.ConditionalSelect(pLoop, pCompare, Vector.Zero); - // Store the squashed elements to the first output array. - // (This uses the loop predicate, so some additional zeros may be stored). - Sve.StoreAndZip(pLoop, left + indexLeft, compacted); + // Squash all found elements to the lower lanes of the vector. + Vector compacted = Sve.Compact(pInner, data); - // Increment the position in the first output array by the number of elements found. - indexLeft = Sve.SaturatingIncrementByActiveElementCount(indexLeft, pInner); + // Store the squashed elements to the first output array. + // (This uses the loop predicate, so some additional zeros may be stored). + Sve.StoreAndZip(pLoop, left + indexLeft, compacted); - // Find all elements in input array NOT less than the first element. - // (Flip the pCompare predicate by XORing with ones) - pInner = Sve.ConditionalSelect(pLoop, Sve.Xor(pCompare, ones), Vector.Zero); + // Increment the position in the first output array by the number of elements found. + indexLeft = Sve.SaturatingIncrementByActiveElementCount(indexLeft, pInner); - // Repeat for the right array. - compacted = Sve.Compact(pInner, data); - Sve.StoreAndZip(pLoop, right + indexRight, compacted); - indexRight = Sve.SaturatingIncrementByActiveElementCount(indexRight, pInner); + // Find all elements in input array NOT less than the first element. + // (Flip the pCompare predicate by XORing with ones) + pInner = Sve.ConditionalSelect(pLoop, Sve.Xor(pCompare, ones), Vector.Zero); - i = Sve.SaturatingIncrementBy32BitElementCount(i, 1); - pLoop = Sve.CreateWhileLessThanMask32Bit(i, Size); - } + // Repeat for the right array. + compacted = Sve.Compact(pInner, data); + Sve.StoreAndZip(pLoop, right + indexRight, compacted); + indexRight = Sve.SaturatingIncrementByActiveElementCount(indexRight, pInner); - return indexRight; + i = Sve.SaturatingIncrementBy32BitElementCount(i, 1); + pLoop = Sve.CreateWhileLessThanMask32Bit(i, Size); } + return indexRight; } - return 0; } [Benchmark] public unsafe ulong SveTail() { - if (Sve.IsSupported) + fixed (uint* input = _input, left = _left, right = _right) { - fixed (uint* input = _input, left = _left, right = _right) - { - long i = 0; + long i = 0; - ulong indexLeft = 0; - ulong indexRight = 0; + ulong indexLeft = 0; + ulong indexRight = 0; - Vector firstElemVec = Sve.DuplicateSelectedScalarToVector( - Sve.LoadVector(Sve.CreateTrueMaskUInt32(), input), 0 - ); + Vector firstElemVec = Sve.DuplicateSelectedScalarToVector( + Sve.LoadVector(Sve.CreateTrueMaskUInt32(), input), 0 + ); - Vector pTrue = Sve.CreateTrueMaskUInt32(); + Vector pTrue = Sve.CreateTrueMaskUInt32(); - while (i < (Size - (int)Sve.Count32BitElements())) - { - Vector data = Sve.LoadVector(pTrue, input + i); + while (i < (Size - (int)Sve.Count32BitElements())) + { + Vector data = Sve.LoadVector(pTrue, input + i); - // Predicate for elements in input array less than the first element. - Vector pInner = Sve.CompareLessThan(data, firstElemVec); + // Predicate for elements in input array less than the first element. + Vector pInner = Sve.CompareLessThan(data, firstElemVec); - // Squash all found elements to the lower lanes of the vector. - Vector compacted = Sve.Compact(pInner, data); + // Squash all found elements to the lower lanes of the vector. + Vector compacted = Sve.Compact(pInner, data); - // Store the squashed elements to the first output array. - Sve.StoreAndZip(pTrue, left + indexLeft, compacted); + // Store the squashed elements to the first output array. + Sve.StoreAndZip(pTrue, left + indexLeft, compacted); - // Increment the position in the first output array by the number of elements found. - indexLeft = Sve.SaturatingIncrementByActiveElementCount(indexLeft, pInner); + // Increment the position in the first output array by the number of elements found. + indexLeft = Sve.SaturatingIncrementByActiveElementCount(indexLeft, pInner); - // Find elements greater than or equal to the first element. - pInner = Sve.CompareGreaterThanOrEqual(data, firstElemVec); + // Find elements greater than or equal to the first element. + pInner = Sve.CompareGreaterThanOrEqual(data, firstElemVec); - // Repeat for the right array. - compacted = Sve.Compact(pInner, data); - Sve.StoreAndZip(pTrue, right + indexRight, compacted); - indexRight = Sve.SaturatingIncrementByActiveElementCount(indexRight, pInner); + // Repeat for the right array. + compacted = Sve.Compact(pInner, data); + Sve.StoreAndZip(pTrue, right + indexRight, compacted); + indexRight = Sve.SaturatingIncrementByActiveElementCount(indexRight, pInner); - i = Sve.SaturatingIncrementBy32BitElementCount(i, 1); - } + i = Sve.SaturatingIncrementBy32BitElementCount(i, 1); + } - // Handler remaining elements. - for (; i < Size; i++) + // Handler remaining elements. + for (; i < Size; i++) + { + if (input[i] < input[0]) { - if (input[i] < input[0]) - { - left[indexLeft] = input[i]; - indexLeft++; - } - else - { - right[indexRight] = input[i]; - indexRight++; - } + left[indexLeft] = input[i]; + indexLeft++; + } + else + { + right[indexRight] = input[i]; + indexRight++; } - - return indexRight; } + + return indexRight; } - return 0; } } } - -#pragma warning restore SYSLIB5003 diff --git a/src/benchmarks/micro/sve/StrCmp.cs b/src/benchmarks/micro/sve/StrCmp.cs index 3f884e5a178..95f46760cc0 100644 --- a/src/benchmarks/micro/sve/StrCmp.cs +++ b/src/benchmarks/micro/sve/StrCmp.cs @@ -1,5 +1,3 @@ -#pragma warning disable SYSLIB5003 - using System; using System.Numerics; using System.Linq; @@ -118,106 +116,95 @@ public int Vector128StrCmp() [Benchmark] public unsafe long SveStrCmp() { - if (Sve.IsSupported) - { - int i = 0; - int elemsInVector = (int)Sve.Count8BitElements(); + int i = 0; + int elemsInVector = (int)Sve.Count8BitElements(); - Vector ptrue = Sve.CreateTrueMaskByte(); - Vector pLoop = (Vector)Sve.CreateWhileLessThanMask8Bit(i, Size); - Vector cmp = Vector.Zero; - Vector arr1_data, arr2_data; + Vector ptrue = Sve.CreateTrueMaskByte(); + Vector pLoop = (Vector)Sve.CreateWhileLessThanMask8Bit(i, Size); + Vector cmp = Vector.Zero; + Vector arr1_data, arr2_data; - if (_arr1.Length == _arr2.Length) + if (_arr1.Length == _arr2.Length) + { + fixed (byte* arr1_ptr = _arr1, arr2_ptr = _arr2) { - fixed (byte* arr1_ptr = _arr1, arr2_ptr = _arr2) + while (Sve.TestFirstTrue(ptrue, pLoop)) { - while (Sve.TestFirstTrue(ptrue, pLoop)) - { - arr1_data = Sve.LoadVector(pLoop, arr1_ptr + i); - arr2_data = Sve.LoadVector(pLoop, arr2_ptr + i); + arr1_data = Sve.LoadVector(pLoop, arr1_ptr + i); + arr2_data = Sve.LoadVector(pLoop, arr2_ptr + i); - // stop if any values arent equal - cmp = Sve.CompareNotEqualTo(arr1_data, arr2_data); + // stop if any values arent equal + cmp = Sve.CompareNotEqualTo(arr1_data, arr2_data); - if (Sve.TestAnyTrue(ptrue, cmp)) - break; + if (Sve.TestAnyTrue(ptrue, cmp)) + break; - i += elemsInVector; + i += elemsInVector; - pLoop = (Vector)Sve.CreateWhileLessThanMask8Bit(i, Size); - } + pLoop = (Vector)Sve.CreateWhileLessThanMask8Bit(i, Size); + } - // create a bitmask to find position of changed value - int mask = 0; - for (int j = 0; j < elemsInVector; j++) - { - // set bits in lanes with non zero elements - if (cmp.GetElement(j) != 0) - mask |= (1 << j); - } + // create a bitmask to find position of changed value + int mask = 0; + for (int j = 0; j < elemsInVector; j++) + { + // set bits in lanes with non zero elements + if (cmp.GetElement(j) != 0) + mask |= (1 << j); + } - int zeroCount = BitOperations.TrailingZeroCount(mask); + int zeroCount = BitOperations.TrailingZeroCount(mask); - if (zeroCount < elemsInVector) - return _arr1[i + zeroCount] - _arr2[i + zeroCount]; + if (zeroCount < elemsInVector) + return _arr1[i + zeroCount] - _arr2[i + zeroCount]; - return 0; - } + return 0; } - - Debug.Assert(false, "Different array lengths are not expected"); - return 0; } + + Debug.Assert(false, "Different array lengths are not expected"); return 0; } [Benchmark] public unsafe long SveTail() { - if (Sve.IsSupported) - { - Vector ptrue = Sve.CreateTrueMaskByte(); - Vector cmp; - Vector arr1_data, arr2_data; + Vector ptrue = Sve.CreateTrueMaskByte(); + Vector cmp; + Vector arr1_data, arr2_data; - int i = 0; - int elemsInVector = (int)Sve.Count8BitElements(); + int i = 0; + int elemsInVector = (int)Sve.Count8BitElements(); - if (_arr1.Length == _arr2.Length) + if (_arr1.Length == _arr2.Length) + { + fixed (byte* arr1_ptr = _arr1, arr2_ptr = _arr2) { - fixed (byte* arr1_ptr = _arr1, arr2_ptr = _arr2) + for (; i <= Size - elemsInVector; i += elemsInVector) { - for (; i <= Size - elemsInVector; i += elemsInVector) - { - arr1_data = Sve.LoadVector(ptrue, arr1_ptr + i); - arr2_data = Sve.LoadVector(ptrue, arr2_ptr + i); + arr1_data = Sve.LoadVector(ptrue, arr1_ptr + i); + arr2_data = Sve.LoadVector(ptrue, arr2_ptr + i); - cmp = Sve.CompareNotEqualTo(arr1_data, arr2_data); - - if (Sve.TestAnyTrue(ptrue, cmp)) - { - break; - } - } + cmp = Sve.CompareNotEqualTo(arr1_data, arr2_data); - for (; i < Size; i++) + if (Sve.TestAnyTrue(ptrue, cmp)) { - if (_arr1[i] != _arr2[i]) - return _arr1[i] - _arr2[i]; + break; } + } - return 0; + for (; i < Size; i++) + { + if (_arr1[i] != _arr2[i]) + return _arr1[i] - _arr2[i]; } - } - Debug.Assert(false, "Different array lengths are not expected"); - return 0; + return 0; + } } + Debug.Assert(false, "Different array lengths are not expected"); return 0; } } } - -#pragma warning restore SYSLIB5003 diff --git a/src/benchmarks/micro/sve/StrIndexOf.cs b/src/benchmarks/micro/sve/StrIndexOf.cs index f08efc79824..04e9e71a967 100644 --- a/src/benchmarks/micro/sve/StrIndexOf.cs +++ b/src/benchmarks/micro/sve/StrIndexOf.cs @@ -1,5 +1,3 @@ -#pragma warning disable SYSLIB5003 - using System; using System.Numerics; using System.Linq; @@ -106,81 +104,71 @@ public unsafe int Vector128IndexOf() [Benchmark] public unsafe int SveIndexOf() { - if (Sve.IsSupported) + int i = 0; + + fixed (char* arr_ptr = _array) { - int i = 0; + Vector target = new Vector((ushort)_searchValue); + var pLoop = (Vector)Sve.CreateWhileLessThanMask16Bit(i, Size); - fixed (char* arr_ptr = _array) + while (Sve.TestFirstTrue(Sve.CreateTrueMaskUInt16(), pLoop)) { - Vector target = new Vector((ushort)_searchValue); - var pLoop = (Vector)Sve.CreateWhileLessThanMask16Bit(i, Size); + Vector vals = Sve.LoadVector(pLoop, ((ushort*)arr_ptr) + i); + Vector cmpVec = Sve.CompareEqual(vals, target); - while (Sve.TestFirstTrue(Sve.CreateTrueMaskUInt16(), pLoop)) + // Test if the character is found in the current values. + if (Sve.TestAnyTrue(Sve.CreateTrueMaskUInt16(), cmpVec)) { - Vector vals = Sve.LoadVector(pLoop, ((ushort*)arr_ptr) + i); - Vector cmpVec = Sve.CompareEqual(vals, target); - - // Test if the character is found in the current values. - if (Sve.TestAnyTrue(Sve.CreateTrueMaskUInt16(), cmpVec)) - { - // Set elements up to and including the first active element to 1 and the rest to 0. - Vector brkVec = Sve.CreateBreakAfterMask(Sve.CreateTrueMaskUInt16(), cmpVec); - // The offset is the number of active elements minus 1. - return (int)Sve.SaturatingIncrementByActiveElementCount(i - 1, brkVec); - } - - i += (int)Sve.Count16BitElements(); - pLoop = (Vector)Sve.CreateWhileLessThanMask16Bit(i, Size); + // Set elements up to and including the first active element to 1 and the rest to 0. + Vector brkVec = Sve.CreateBreakAfterMask(Sve.CreateTrueMaskUInt16(), cmpVec); + // The offset is the number of active elements minus 1. + return (int)Sve.SaturatingIncrementByActiveElementCount(i - 1, brkVec); } + + i += (int)Sve.Count16BitElements(); + pLoop = (Vector)Sve.CreateWhileLessThanMask16Bit(i, Size); } - } - return -1; + return -1; + } } [Benchmark] public unsafe int SveTail() { - if (Sve.IsSupported) + int i = 0; + + fixed (char* arr_ptr = _array) { - int i = 0; + Vector target = new Vector((ushort)_searchValue); + var pLoop = (Vector)Sve.CreateTrueMaskInt16(); - fixed (char* arr_ptr = _array) + while (i < (Size - (int)Sve.Count16BitElements())) { - Vector target = new Vector((ushort)_searchValue); - var pLoop = (Vector)Sve.CreateTrueMaskInt16(); + Vector vals = Sve.LoadVector(pLoop, ((ushort*)arr_ptr) + i); + Vector cmpVec = Sve.CompareEqual(vals, target); - while (i < (Size - (int)Sve.Count16BitElements())) + // Test if the character is found in the current values. + if (Sve.TestAnyTrue(Sve.CreateTrueMaskUInt16(), cmpVec)) { - Vector vals = Sve.LoadVector(pLoop, ((ushort*)arr_ptr) + i); - Vector cmpVec = Sve.CompareEqual(vals, target); - - // Test if the character is found in the current values. - if (Sve.TestAnyTrue(Sve.CreateTrueMaskUInt16(), cmpVec)) - { - // Set elements up to and including the first active element to 1 and the rest to 0. - Vector brkVec = Sve.CreateBreakAfterMask(Sve.CreateTrueMaskUInt16(), cmpVec); - // The offset is the number of active elements minus 1. - return (int)Sve.SaturatingIncrementByActiveElementCount(i - 1, brkVec); - } - - i += (int)Sve.Count16BitElements(); + // Set elements up to and including the first active element to 1 and the rest to 0. + Vector brkVec = Sve.CreateBreakAfterMask(Sve.CreateTrueMaskUInt16(), cmpVec); + // The offset is the number of active elements minus 1. + return (int)Sve.SaturatingIncrementByActiveElementCount(i - 1, brkVec); } - for (; i < Size; i++) - { - if (arr_ptr[i] == _searchValue) - return i; - } + i += (int)Sve.Count16BitElements(); + } - return -1; + for (; i < Size; i++) + { + if (arr_ptr[i] == _searchValue) + return i; } - } - return -1; + return -1; + } } } } - -#pragma warning restore SYSLIB5003 diff --git a/src/benchmarks/micro/sve/StrLen.cs b/src/benchmarks/micro/sve/StrLen.cs index c932a8b921c..c60d1344606 100644 --- a/src/benchmarks/micro/sve/StrLen.cs +++ b/src/benchmarks/micro/sve/StrLen.cs @@ -1,5 +1,3 @@ -#pragma warning disable SYSLIB5003 - using System; using System.Numerics; using System.Runtime.Intrinsics; @@ -116,39 +114,33 @@ public unsafe ulong Vector128StrLen() [Benchmark] public unsafe ulong SveStrLen() { - if (Sve.IsSupported) - { - Vector ptrue = Sve.CreateTrueMaskByte(); - Vector cmp, data; + Vector ptrue = Sve.CreateTrueMaskByte(); + Vector cmp, data; - ulong i = 0; - ulong elemsInVector = Sve.Count8BitElements(); + ulong i = 0; + ulong elemsInVector = Sve.Count8BitElements(); - Vector pLoop = (Vector)Sve.CreateWhileLessThanMask8Bit((int)i, Size); + Vector pLoop = (Vector)Sve.CreateWhileLessThanMask8Bit((int)i, Size); - fixed (byte* arr_ptr = _array) + fixed (byte* arr_ptr = _array) + { + while (true) { - while (true) + data = Sve.LoadVector(pLoop, arr_ptr + i); + cmp = Sve.CompareEqual(data, Vector.Zero); + + if (Sve.TestAnyTrue(ptrue, cmp)) + break; + else { - data = Sve.LoadVector(pLoop, arr_ptr + i); - cmp = Sve.CompareEqual(data, Vector.Zero); - - if (Sve.TestAnyTrue(ptrue, cmp)) - break; - else - { - i += elemsInVector; - pLoop = (Vector)Sve.CreateWhileLessThanMask8Bit((int)i, Size); - } + i += elemsInVector; + pLoop = (Vector)Sve.CreateWhileLessThanMask8Bit((int)i, Size); } - - i += Sve.GetActiveElementCount(pLoop, data); - return i; } + + i += Sve.GetActiveElementCount(pLoop, data); + return i; } - return 0; } } } - -#pragma warning restore SYSLIB5003