From 2a48c941afb70b8d33f186171129e8fa4beaf625 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Wed, 8 Dec 2021 20:01:50 +0100 Subject: [PATCH] Add AVX2 version of CollectHistogram --- .../Formats/Webp/Lossy/Vp8Histogram.cs | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Histogram.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Histogram.cs index d384302b94..f679fcb136 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Histogram.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Histogram.cs @@ -3,6 +3,11 @@ using System; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +#if SUPPORTS_RUNTIME_INTRINSICS +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; +#endif namespace SixLabors.ImageSharp.Formats.Webp.Lossy { @@ -19,6 +24,10 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy /// private const int MaxCoeffThresh = 31; +#if SUPPORTS_RUNTIME_INTRINSICS + private static readonly Vector256 MaxCoeffThreshVec = Vector256.Create((short)MaxCoeffThresh); +#endif + private int maxValue; private int lastNonZero; @@ -52,11 +61,38 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy Vp8Encoding.FTransform(reference.Slice(WebpLookupTables.Vp8DspScan[j]), pred.Slice(WebpLookupTables.Vp8DspScan[j]), this.output, this.scratch); // Convert coefficients to bin. - for (int k = 0; k < 16; ++k) +#if SUPPORTS_RUNTIME_INTRINSICS + if (Avx2.IsSupported) { - int v = Math.Abs(this.output[k]) >> 3; - int clippedValue = ClipMax(v, MaxCoeffThresh); - ++this.distribution[clippedValue]; + // Load. + ref short outputRef = ref MemoryMarshal.GetReference(this.output); + Vector256 out0 = Unsafe.As>(ref outputRef); + + // v = abs(out) >> 3 + Vector256 abs0 = Avx2.Abs(out0.AsInt16()); + Vector256 v0 = Avx2.ShiftRightArithmetic(abs0.AsInt16(), 3); + + // bin = min(v, MAX_COEFF_THRESH) + Vector256 min0 = Avx2.Min(v0, MaxCoeffThreshVec); + + // Store. + Unsafe.As>(ref outputRef) = min0; + + // Convert coefficients to bin. + for (int k = 0; k < 16; ++k) + { + ++this.distribution[this.output[k]]; + } + } + else +#endif + { + for (int k = 0; k < 16; ++k) + { + int v = Math.Abs(this.output[k]) >> 3; + int clippedValue = ClipMax(v, MaxCoeffThresh); + ++this.distribution[clippedValue]; + } } }