From fad143d5f8ff7d9ed2e4fda48924cf0be5fe71f3 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 3 Apr 2023 19:39:32 +0200 Subject: [PATCH] Switch back to Sse2 version of residual costs, since avx version is not the same as scalar version --- .../Formats/Webp/Lossy/Vp8Residual.cs | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs index 177041506..68bf09f94 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs @@ -156,7 +156,7 @@ internal class Vp8Residual return LossyUtils.Vp8BitCost(0, (byte)p0); } - if (Avx2.IsSupported) + if (Sse2.IsSupported) { Span scratch = stackalloc byte[32]; Span ctxs = scratch.Slice(0, 16); @@ -165,19 +165,23 @@ internal class Vp8Residual // Precompute clamped levels and contexts, packed to 8b. ref short outputRef = ref MemoryMarshal.GetReference(this.Coeffs); - Vector256 c0 = Unsafe.As>(ref outputRef).AsInt16(); - Vector256 d0 = Avx2.Subtract(Vector256.Zero, c0); - Vector256 e0 = Avx2.Max(c0, d0); // abs(v), 16b - Vector256 f = Avx2.PackSignedSaturate(e0, e0); - Vector256 g = Avx2.Min(f.AsByte(), Vector256.Create((byte)2)); - Vector256 h = Avx2.Min(f.AsByte(), Vector256.Create((byte)67)); // clampLevel in [0..67] + Vector128 c0 = Unsafe.As>(ref outputRef).AsInt16(); + Vector128 c1 = Unsafe.As>(ref Unsafe.Add(ref outputRef, 8)).AsInt16(); + Vector128 d0 = Sse2.Subtract(Vector128.Zero, c0); + Vector128 d1 = Sse2.Subtract(Vector128.Zero, c1); + Vector128 e0 = Sse2.Max(c0, d0); // abs(v), 16b + Vector128 e1 = Sse2.Max(c1, d1); + Vector128 f = Sse2.PackSignedSaturate(e0, e1); + Vector128 g = Sse2.Min(f.AsByte(), Vector128.Create((byte)2)); // context = 0, 1, 2 + Vector128 h = Sse2.Min(f.AsByte(), Vector128.Create((byte)67)); // clampLevel in [0..67] ref byte ctxsRef = ref MemoryMarshal.GetReference(ctxs); ref byte levelsRef = ref MemoryMarshal.GetReference(levels); ref ushort absLevelsRef = ref MemoryMarshal.GetReference(absLevels); - Unsafe.As>(ref ctxsRef) = g.GetLower(); - Unsafe.As>(ref levelsRef) = h.GetLower(); - Unsafe.As>(ref absLevelsRef) = e0.AsUInt16(); + Unsafe.As>(ref ctxsRef) = g; + Unsafe.As>(ref levelsRef) = h; + Unsafe.As>(ref absLevelsRef) = e0.AsUInt16(); + Unsafe.As>(ref Unsafe.Add(ref absLevelsRef, 8)) = e1.AsUInt16(); int level; int flevel;