Skip to content

Commit a5033e4

Browse files
Benchmarks, tests, and cleanup.
1 parent 6478865 commit a5033e4

File tree

5 files changed

+86
-114
lines changed

5 files changed

+86
-114
lines changed

src/ImageSharp/Common/Helpers/SimdUtils.Avx2Intrinsics.cs

Lines changed: 0 additions & 103 deletions
This file was deleted.

tests/ImageSharp.Benchmarks/Color/Bulk/FromVector4.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
#endif
1414

1515
using BenchmarkDotNet.Attributes;
16-
using BenchmarkDotNet.Environments;
17-
using BenchmarkDotNet.Jobs;
1816
using SixLabors.ImageSharp.Memory;
1917
using SixLabors.ImageSharp.PixelFormats;
2018

2119
// ReSharper disable InconsistentNaming
2220
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk
2321
{
24-
[Config(typeof(Config.ShortClr))]
22+
[Config(typeof(Config.ShortCore31))]
2523
public abstract class FromVector4<TPixel>
2624
where TPixel : unmanaged, IPixel<TPixel>
2725
{

tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4_Rgba32.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk
1515
{
16-
[Config(typeof(Config.ShortClr))]
16+
[Config(typeof(Config.ShortCore31))]
1717
public class ToVector4_Rgba32 : ToVector4<Rgba32>
1818
{
1919
[Benchmark]
@@ -52,6 +52,17 @@ public void ExtendedIntrinsics()
5252
SimdUtils.ExtendedIntrinsics.ByteToNormalizedFloat(sBytes, dFloats);
5353
}
5454

55+
#if SUPPORTS_RUNTIME_INTRINSICS
56+
[Benchmark]
57+
public void HwIntrinsics()
58+
{
59+
Span<byte> sBytes = MemoryMarshal.Cast<Rgba32, byte>(this.source.GetSpan());
60+
Span<float> dFloats = MemoryMarshal.Cast<Vector4, float>(this.destination.GetSpan());
61+
62+
SimdUtils.HwIntrinsics.ByteToNormalizedFloat(sBytes, dFloats);
63+
}
64+
#endif
65+
5566
// [Benchmark]
5667
public void ExtendedIntrinsics_BulkConvertByteToNormalizedFloat_2Loops()
5768
{

tests/ImageSharp.Tests/Common/SimdUtilsTests.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.Runtime.CompilerServices;
88
using System.Runtime.InteropServices;
99
using SixLabors.ImageSharp.Common.Tuples;
10-
10+
using SixLabors.ImageSharp.Tests.TestUtilities;
1111
using Xunit;
1212
using Xunit.Abstractions;
1313

@@ -209,9 +209,17 @@ public void ExtendedIntrinsics_BulkConvertByteToNormalizedFloat(int count)
209209
[MemberData(nameof(ArraySizesDivisibleBy32))]
210210
public void HwIntrinsics_BulkConvertByteToNormalizedFloat(int count)
211211
{
212-
TestImpl_BulkConvertByteToNormalizedFloat(
213-
count,
214-
(s, d) => SimdUtils.HwIntrinsics.ByteToNormalizedFloat(s.Span, d.Span));
212+
static void RunTest(string serialized)
213+
{
214+
TestImpl_BulkConvertByteToNormalizedFloat(
215+
FeatureTestRunner.Deserialize(serialized),
216+
(s, d) => SimdUtils.HwIntrinsics.ByteToNormalizedFloat(s.Span, d.Span));
217+
}
218+
219+
FeatureTestRunner.RunWithHwIntrinsicsFeature(
220+
RunTest,
221+
HwIntrinsics.AllowAll | HwIntrinsics.DisableAVX2 | HwIntrinsics.DisableSSE41,
222+
count);
215223
}
216224
#endif
217225

@@ -294,9 +302,17 @@ public void ExtendedIntrinsics_ConvertToSingle(short scale)
294302
[MemberData(nameof(ArraySizesDivisibleBy32))]
295303
public void HwIntrinsics_BulkConvertNormalizedFloatToByteClampOverflows(int count)
296304
{
297-
TestImpl_BulkConvertNormalizedFloatToByteClampOverflows(
298-
count,
299-
(s, d) => SimdUtils.HwIntrinsics.NormalizedFloatToByteSaturate(s.Span, d.Span));
305+
static void RunTest(string serialized)
306+
{
307+
TestImpl_BulkConvertNormalizedFloatToByteClampOverflows(
308+
FeatureTestRunner.Deserialize(serialized),
309+
(s, d) => SimdUtils.HwIntrinsics.NormalizedFloatToByteSaturate(s.Span, d.Span));
310+
}
311+
312+
FeatureTestRunner.RunWithHwIntrinsicsFeature(
313+
RunTest,
314+
HwIntrinsics.AllowAll | HwIntrinsics.DisableAVX2,
315+
count);
300316
}
301317

302318
#endif

tests/ImageSharp.Tests/TestUtilities/FeatureTesting/FeatureTestRunner.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public static T Deserialize<T>(string value)
3333
where T : IXunitSerializable
3434
=> BasicSerializer.Deserialize<T>(value);
3535

36+
/// <summary>
37+
/// Allows the deserialization of integers passed to the feature test.
38+
/// </summary>
39+
/// <param name="value">The string value to deserialize.</param>
40+
/// <returns>The <see cref="int"/> value.</returns>
41+
public static int Deserialize(string value)
42+
=> Convert.ToInt32(value);
43+
3644
/// <summary>
3745
/// Runs the given test <paramref name="action"/> within an environment
3846
/// where the given <paramref name="intrinsics"/> features.
@@ -201,6 +209,48 @@ public static void RunWithHwIntrinsicsFeature<T>(
201209
}
202210
}
203211

212+
/// <summary>
213+
/// Runs the given test <paramref name="action"/> within an environment
214+
/// where the given <paramref name="intrinsics"/> features.
215+
/// </summary>
216+
/// <param name="action">The test action to run.</param>
217+
/// <param name="intrinsics">The intrinsics features.</param>
218+
/// <param name="serializable">The value to pass as a parameter to the test action.</param>
219+
public static void RunWithHwIntrinsicsFeature(
220+
Action<string> action,
221+
HwIntrinsics intrinsics,
222+
int serializable)
223+
{
224+
if (!RemoteExecutor.IsSupported)
225+
{
226+
return;
227+
}
228+
229+
foreach (KeyValuePair<HwIntrinsics, string> intrinsic in intrinsics.ToFeatureKeyValueCollection())
230+
{
231+
var processStartInfo = new ProcessStartInfo();
232+
if (intrinsic.Key != HwIntrinsics.AllowAll)
233+
{
234+
processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0";
235+
236+
RemoteExecutor.Invoke(
237+
action,
238+
serializable.ToString(),
239+
new RemoteInvokeOptions
240+
{
241+
StartInfo = processStartInfo
242+
})
243+
.Dispose();
244+
}
245+
else
246+
{
247+
// Since we are running using the default architecture there is no
248+
// point creating the overhead of running the action in a separate process.
249+
action(serializable.ToString());
250+
}
251+
}
252+
}
253+
204254
internal static Dictionary<HwIntrinsics, string> ToFeatureKeyValueCollection(this HwIntrinsics intrinsics)
205255
{
206256
// Loop through and translate the given values into COMPlus equivaluents

0 commit comments

Comments
 (0)