From 0c0812de82648be40a35dc63a9b6c914bdcbbbf7 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 8 Nov 2021 16:58:40 +0100 Subject: [PATCH] Avoid pinning input and output data --- src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs b/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs index b300b7b5c..6e25dc003 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs @@ -600,12 +600,10 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy input0 = Sse2.MultiplyLow(out0, q0.AsInt16()); input8 = Sse2.MultiplyLow(out8, q8.AsInt16()); - fixed (short* inputPtr = input) - { - // in = out * Q - Sse2.Store(inputPtr, input0); - Sse2.Store(inputPtr + 8, input8); - } + // in = out * Q + ref short inputRef = ref MemoryMarshal.GetReference(input); + Unsafe.As>(ref inputRef) = input0; + Unsafe.As>(ref Unsafe.Add(ref inputRef, 8)) = input8; // zigzag the output before storing it. The re-ordering is: // 0 1 2 3 4 5 6 7 | 8 9 10 11 12 13 14 15 @@ -620,11 +618,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy Vector128 outZ0 = Sse2.Or(tmpLo, tmp8); Vector128 outZ8 = Sse2.Or(tmpHi, tmp7); - fixed (short* outputPtr = output) - { - Sse2.Store(outputPtr, outZ0.AsInt16()); - Sse2.Store(outputPtr + 8, outZ8.AsInt16()); - } + ref short outputRef = ref MemoryMarshal.GetReference(output); + Unsafe.As>(ref outputRef) = outZ0.AsInt16(); + Unsafe.As>(ref Unsafe.Add(ref outputRef, 8)) = outZ8.AsInt16(); Vector128 packedOutput = Sse2.PackSignedSaturate(outZ0.AsInt16(), outZ8.AsInt16());