From ebfd6ce89946d5b9948c8da0ea576e8f330989f0 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 8 Mar 2017 12:24:22 +0100 Subject: [PATCH] if (count < 256) { no SIMD } --- src/ImageSharp/Colors/Color.BulkOperations.cs | 7 ++++ .../Color/Bulk/ToVector4.cs | 2 +- .../Colors/BulkPixelOperationsTests.cs | 33 +++++++++++++++---- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Colors/Color.BulkOperations.cs b/src/ImageSharp/Colors/Color.BulkOperations.cs index aadca236a..cdbfdfcd7 100644 --- a/src/ImageSharp/Colors/Color.BulkOperations.cs +++ b/src/ImageSharp/Colors/Color.BulkOperations.cs @@ -88,6 +88,13 @@ namespace ImageSharp /// internal override void ToVector4(BufferPointer sourceColors, BufferPointer destVectors, int count) { + if (count < 256) + { + // Doesn't worth to bother with SIMD: + base.ToVector4(sourceColors, destVectors, count); + return; + } + int remainder = count % Vector.Count; int alignedCount = count - remainder; diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs index 130091386..b48eaa35a 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs @@ -12,7 +12,7 @@ namespace ImageSharp.Benchmarks.Color.Bulk private PinnedBuffer destination; - [Params(16, 128, 1024)] + [Params(64, 300, 1024)] public int Count { get; set; } [Setup] diff --git a/tests/ImageSharp.Tests/Colors/BulkPixelOperationsTests.cs b/tests/ImageSharp.Tests/Colors/BulkPixelOperationsTests.cs index 786c77246..6621292ec 100644 --- a/tests/ImageSharp.Tests/Colors/BulkPixelOperationsTests.cs +++ b/tests/ImageSharp.Tests/Colors/BulkPixelOperationsTests.cs @@ -1,15 +1,22 @@ // ReSharper disable InconsistentNaming +// ReSharper disable AccessToDisposedClosure namespace ImageSharp.Tests.Colors { using System; using System.Numerics; using Xunit; + using Xunit.Abstractions; public class BulkPixelOperationsTests { public class Color : BulkPixelOperationsTests { + public Color(ITestOutputHelper output) + : base(output) + { + } + // For 4.6 test runner MemberData does not work without redeclaring the public field in the derived test class: public static new TheoryData ArraySizesData => new TheoryData { 7, 16, 1111 }; @@ -32,19 +39,21 @@ namespace ImageSharp.Tests.Colors ); } - [Fact] + // [Fact] // Profiling benchmark - enable manually! public void Benchmark_ToVector4() { - int times = 150000; + int times = 200000; int count = 1024; using (PinnedBuffer source = new PinnedBuffer(count)) using (PinnedBuffer dest = new PinnedBuffer(count)) { - for (int i = 0; i < times; i++) - { - BulkPixelOperations.Instance.ToVector4(source, dest, count); - } + this.Measure( + times, + () => + { + BulkPixelOperations.Instance.ToVector4(source, dest, count); + }); } } } @@ -52,6 +61,11 @@ namespace ImageSharp.Tests.Colors public class Argb : BulkPixelOperationsTests { // For 4.6 test runner MemberData does not work without redeclaring the public field in the derived test class: + public Argb(ITestOutputHelper output) + : base(output) + { + } + public static new TheoryData ArraySizesData => new TheoryData { 7, 16, 1111 }; } @@ -64,9 +78,14 @@ namespace ImageSharp.Tests.Colors } } - public abstract class BulkPixelOperationsTests + public abstract class BulkPixelOperationsTests : MeasureFixture where TColor : struct, IPixel { + protected BulkPixelOperationsTests(ITestOutputHelper output) + : base(output) + { + } + public static TheoryData ArraySizesData => new TheoryData { 7, 16, 1111 }; private static BulkPixelOperations Operations => BulkPixelOperations.Instance;