From ad659fb73166dede231a1d3d44e27f6755ad4e09 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Mon, 15 Oct 2018 01:30:35 +0200 Subject: [PATCH] move tests --- src/ImageSharp/Common/Extensions/SimdUtils.cs | 2 +- .../Color/Bulk/ToVector4.cs | 2 +- .../ImageSharp.Tests/Common/SimdUtilsTests.cs | 40 ++++++++++++++++ .../PixelFormats/PixelOperationsTests.cs | 48 ------------------- .../TestUtilities/TestDataGenerator.cs | 7 +++ 5 files changed, 49 insertions(+), 50 deletions(-) diff --git a/src/ImageSharp/Common/Extensions/SimdUtils.cs b/src/ImageSharp/Common/Extensions/SimdUtils.cs index 56118a764..481e0726d 100644 --- a/src/ImageSharp/Common/Extensions/SimdUtils.cs +++ b/src/ImageSharp/Common/Extensions/SimdUtils.cs @@ -111,7 +111,7 @@ namespace SixLabors.ImageSharp /// https://github.com/dotnet/coreclr/pull/10662 /// /// - internal static void BulkConvertByteToNormalizedFloatFast(ReadOnlySpan source, Span dest) + internal static void BulkConvertByteToNormalizedFloatWithExtendedIntrinsics(ReadOnlySpan source, Span dest) { Guard.IsTrue( source.Length % Vector.Count == 0, diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs index 0e5e9d94f..3ea256e85 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs @@ -94,7 +94,7 @@ namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk Span sBytes = MemoryMarshal.Cast(this.source.GetSpan()); Span dFloats = MemoryMarshal.Cast(this.destination.GetSpan()); - SimdUtils.BulkConvertByteToNormalizedFloatFast(sBytes, dFloats); + SimdUtils.BulkConvertByteToNormalizedFloatWithExtendedIntrinsics(sBytes, dFloats); } } diff --git a/tests/ImageSharp.Tests/Common/SimdUtilsTests.cs b/tests/ImageSharp.Tests/Common/SimdUtilsTests.cs index c6c3b68f3..4e39af70f 100644 --- a/tests/ImageSharp.Tests/Common/SimdUtilsTests.cs +++ b/tests/ImageSharp.Tests/Common/SimdUtilsTests.cs @@ -186,6 +186,46 @@ namespace SixLabors.ImageSharp.Tests.Common Assert.Equal(expected, dest); } + [Theory] + [InlineData(1, 0)] + [InlineData(2, 32)] + [InlineData(3, 128)] + public void BulkConvertByteToNormalizedFloat(int seed, int count) + { + if (this.SkipOnNonAvx2()) + { + return; + } + + byte[] source = new Random(seed).GenerateRandomByteArray(count); + float[] result = new float[count]; + float[] expected = source.Select(b => (float)b / 255f).ToArray(); + + SimdUtils.BulkConvertByteToNormalizedFloat(source, result); + + Assert.Equal(expected, result, new ApproximateFloatComparer(1e-5f)); + } + + [Theory] + [InlineData(1, 0)] + [InlineData(2, 32)] + [InlineData(3, 128)] + public void BulkConvertByteToNormalizedFloatWithExtendedIntrinsics(int seed, int count) + { + if (!Vector.IsHardwareAccelerated) + { + return; + } + + byte[] source = new Random(seed).GenerateRandomByteArray(count); + float[] result = new float[count]; + float[] expected = source.Select(b => (float)b / 255f).ToArray(); + + SimdUtils.BulkConvertByteToNormalizedFloatWithExtendedIntrinsics(source, result); + + Assert.Equal(expected, result, new ApproximateFloatComparer(1e-5f)); + } + [Theory] [InlineData(0)] [InlineData(7)] diff --git a/tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs b/tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs index 2e84886c0..4d7ec71e7 100644 --- a/tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs +++ b/tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs @@ -49,54 +49,6 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats ); } - [Fact] - public void BulkConvertByteToNormalizedFloat() - { - if (!Vector.IsHardwareAccelerated) - { - return; - } - - ImageSharp.PixelFormats.Rgba32[] source = CreatePixelTestData(64); - Vector4[] expected = CreateExpectedVector4Data(source); - - TestOperation( - source, - expected, - (s, d) => - { - ReadOnlySpan sBytes = MemoryMarshal.Cast(s); - Span dFloats = MemoryMarshal.Cast(d.Memory.Span); - - SimdUtils.BulkConvertByteToNormalizedFloat(sBytes, dFloats); - } - ); - } - - [Fact] - public void BulkConvertByteToNormalizedFloatFast() - { - if (!Vector.IsHardwareAccelerated) - { - return; - } - - ImageSharp.PixelFormats.Rgba32[] source = CreatePixelTestData(128); - Vector4[] expected = CreateExpectedVector4Data(source); - - TestOperation( - source, - expected, - (s, d) => - { - ReadOnlySpan sBytes = MemoryMarshal.Cast(s); - Span dFloats = MemoryMarshal.Cast(d.Memory.Span); - - SimdUtils.BulkConvertByteToNormalizedFloatFast(sBytes, dFloats); - } - ); - } - // [Fact] // Profiling benchmark - enable manually! #pragma warning disable xUnit1013 // Public method should be marked as test diff --git a/tests/ImageSharp.Tests/TestUtilities/TestDataGenerator.cs b/tests/ImageSharp.Tests/TestUtilities/TestDataGenerator.cs index 0b1b89cc0..6f3b18e1f 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestDataGenerator.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestDataGenerator.cs @@ -46,6 +46,13 @@ namespace SixLabors.ImageSharp.Tests return values; } + public static byte[] GenerateRandomByteArray(this Random rnd, int length) + { + byte[] values = new byte[length]; + rnd.NextBytes(values); + return values; + } + private static float GetRandomFloat(Random rnd, float minVal, float maxVal) { return (float)rnd.NextDouble() * (maxVal - minVal) + minVal;