From 8355353985776f06d8199b05102cc28b00f4a72f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 10 Jun 2025 10:48:04 +1000 Subject: [PATCH] Respond to additional feedback --- .../Common/Helpers/Vector128Utilities.cs | 14 ++++---------- src/ImageSharp/Formats/Webp/Lossy/Vp8Encoding.cs | 16 ++++++++-------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/Vector128Utilities.cs b/src/ImageSharp/Common/Helpers/Vector128Utilities.cs index 7eac4f58c4..a5d377eb90 100644 --- a/src/ImageSharp/Common/Helpers/Vector128Utilities.cs +++ b/src/ImageSharp/Common/Helpers/Vector128Utilities.cs @@ -86,11 +86,6 @@ internal static class Vector128_ [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector128 ShuffleNative(Vector128 vector, [ConstantExpected] byte control) { - if (Sse2.IsSupported) - { - return Sse2.Shuffle(vector, control); - } - // Don't use InverseMMShuffle here as we want to avoid the cast. Vector128 indices = Vector128.Create( control & 0x3, @@ -529,17 +524,16 @@ internal static class Vector128_ if (AdvSimd.IsSupported) { Vector128 prodLo = AdvSimd.MultiplyWideningLower(left.GetLower(), right.GetLower()); - Vector128 prodHi = AdvSimd.MultiplyWideningLower(left.GetUpper(), right.GetUpper()); + Vector128 prodHi = AdvSimd.MultiplyWideningUpper(left, right); if (AdvSimd.Arm64.IsSupported) { return AdvSimd.Arm64.AddPairwise(prodLo, prodHi); } - Vector128 v0 = AdvSimd.AddPairwiseWidening(prodLo); - Vector128 v1 = AdvSimd.AddPairwiseWidening(prodHi); - - return Vector128.Narrow(v0, v1); + Vector64 v0 = AdvSimd.AddPairwise(prodLo.GetLower(), prodLo.GetUpper()); + Vector64 v1 = AdvSimd.AddPairwise(prodHi.GetLower(), prodHi.GetUpper()); + return Vector128.Create(v0, v1); } { diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoding.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoding.cs index 72420a0947..7fe71588c4 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoding.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoding.cs @@ -221,10 +221,10 @@ internal static unsafe class Vp8Encoding ref byte referenceRef = ref MemoryMarshal.GetReference(reference); // Load four bytes/pixels per line. - Vector128 ref0 = Vector128.CreateScalar(Unsafe.As(ref referenceRef)).AsByte(); - Vector128 ref1 = Vector128.CreateScalar(Unsafe.As(ref Unsafe.Add(ref referenceRef, WebpConstants.Bps))).AsByte(); - Vector128 ref2 = Vector128.CreateScalar(Unsafe.As(ref Unsafe.Add(ref referenceRef, WebpConstants.Bps * 2))).AsByte(); - Vector128 ref3 = Vector128.CreateScalar(Unsafe.As(ref Unsafe.Add(ref referenceRef, WebpConstants.Bps * 3))).AsByte(); + Vector128 ref0 = Vector128.CreateScalar(Unsafe.ReadUnaligned(ref referenceRef)).AsByte(); + Vector128 ref1 = Vector128.CreateScalar(Unsafe.ReadUnaligned(ref Unsafe.Add(ref referenceRef, WebpConstants.Bps))).AsByte(); + Vector128 ref2 = Vector128.CreateScalar(Unsafe.ReadUnaligned(ref Unsafe.Add(ref referenceRef, WebpConstants.Bps * 2))).AsByte(); + Vector128 ref3 = Vector128.CreateScalar(Unsafe.ReadUnaligned(ref Unsafe.Add(ref referenceRef, WebpConstants.Bps * 3))).AsByte(); // Convert to 16b. ref0 = Vector128_.UnpackLow(ref0, Vector128.Zero); @@ -253,10 +253,10 @@ internal static unsafe class Vp8Encoding int output2 = ref2.AsInt32().ToScalar(); int output3 = ref3.AsInt32().ToScalar(); - Unsafe.As(ref outputRef) = output0; - Unsafe.As(ref Unsafe.Add(ref outputRef, WebpConstants.Bps)) = output1; - Unsafe.As(ref Unsafe.Add(ref outputRef, WebpConstants.Bps * 2)) = output2; - Unsafe.As(ref Unsafe.Add(ref outputRef, WebpConstants.Bps * 3)) = output3; + Unsafe.WriteUnaligned(ref outputRef, output0); + Unsafe.WriteUnaligned(ref Unsafe.Add(ref outputRef, WebpConstants.Bps), output1); + Unsafe.WriteUnaligned(ref Unsafe.Add(ref outputRef, WebpConstants.Bps * 2), output2); + Unsafe.WriteUnaligned(ref Unsafe.Add(ref outputRef, WebpConstants.Bps * 3), output3); } else {