Browse Source

Avoid length-check in pinning spans

Only where it seems profitable. E.g. not when a UnmanagedMemoryStream is constructed of that pointer.
pull/2418/head
Günther Foidl 3 years ago
parent
commit
a4ad7b09fc
  1. 4
      src/ImageSharp/ColorSpaces/Companding/SRgbCompanding.cs
  2. 2
      src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs
  3. 2
      src/ImageSharp/Compression/Zlib/Adler32.cs
  4. 6
      src/ImageSharp/Compression/Zlib/Crc32.cs
  5. 9
      src/ImageSharp/Formats/Webp/Lossless/LosslessUtils.cs
  6. 10
      src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs
  7. 4
      src/ImageSharp/Formats/Webp/WebpCommonUtils.cs
  8. 2
      src/ImageSharp/Memory/Buffer2DExtensions.cs

4
src/ImageSharp/ColorSpaces/Companding/SRgbCompanding.cs

@ -167,7 +167,7 @@ public static class SRgbCompanding
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void CompandAvx2(Span<Vector4> vectors, float[] table) private static unsafe void CompandAvx2(Span<Vector4> vectors, float[] table)
{ {
fixed (float* tablePointer = &table[0]) fixed (float* tablePointer = &MemoryMarshal.GetArrayDataReference(table))
{ {
var scale = Vector256.Create((float)Scale); var scale = Vector256.Create((float)Scale);
Vector256<float> zero = Vector256<float>.Zero; Vector256<float> zero = Vector256<float>.Zero;
@ -199,7 +199,7 @@ public static class SRgbCompanding
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void CompandScalar(Span<Vector4> vectors, float[] table) private static unsafe void CompandScalar(Span<Vector4> vectors, float[] table)
{ {
fixed (float* tablePointer = &table[0]) fixed (float* tablePointer = &MemoryMarshal.GetArrayDataReference(table))
{ {
Vector4 zero = Vector4.Zero; Vector4 zero = Vector4.Zero;
var scale = new Vector4(Scale); var scale = new Vector4(Scale);

2
src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs

@ -644,7 +644,7 @@ internal static partial class SimdUtils
ReadOnlySpan<byte> source, ReadOnlySpan<byte> source,
Span<float> dest) Span<float> dest)
{ {
fixed (byte* sourceBase = source) fixed (byte* sourceBase = &MemoryMarshal.GetReference(source))
{ {
if (Avx2.IsSupported) if (Avx2.IsSupported)
{ {

2
src/ImageSharp/Compression/Zlib/Adler32.cs

@ -387,7 +387,7 @@ internal static class Adler32
uint s1 = adler & 0xFFFF; uint s1 = adler & 0xFFFF;
uint s2 = (adler >> 16) & 0xFFFF; uint s2 = (adler >> 16) & 0xFFFF;
fixed (byte* bufferPtr = buffer) fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer))
{ {
byte* localBufferPtr = bufferPtr; byte* localBufferPtr = bufferPtr;
uint length = (uint)buffer.Length; uint length = (uint)buffer.Length;

6
src/ImageSharp/Compression/Zlib/Crc32.cs

@ -81,7 +81,7 @@ internal static partial class Crc32
int chunksize = buffer.Length & ~ChunksizeMask; int chunksize = buffer.Length & ~ChunksizeMask;
int length = chunksize; int length = chunksize;
fixed (byte* bufferPtr = buffer) fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer))
{ {
fixed (ulong* k05PolyPtr = K05Poly) fixed (ulong* k05PolyPtr = K05Poly)
{ {
@ -201,7 +201,7 @@ internal static partial class Crc32
[MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)]
private static unsafe uint CalculateArm(uint crc, ReadOnlySpan<byte> buffer) private static unsafe uint CalculateArm(uint crc, ReadOnlySpan<byte> buffer)
{ {
fixed (byte* bufferPtr = buffer) fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer))
{ {
byte* localBufferPtr = bufferPtr; byte* localBufferPtr = bufferPtr;
int len = buffer.Length; int len = buffer.Length;
@ -248,7 +248,7 @@ internal static partial class Crc32
[MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)]
private static unsafe uint CalculateArm64(uint crc, ReadOnlySpan<byte> buffer) private static unsafe uint CalculateArm64(uint crc, ReadOnlySpan<byte> buffer)
{ {
fixed (byte* bufferPtr = buffer) fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer))
{ {
byte* localBufferPtr = bufferPtr; byte* localBufferPtr = bufferPtr;
int len = buffer.Length; int len = buffer.Length;

9
src/ImageSharp/Formats/Webp/Lossless/LosslessUtils.cs

@ -569,7 +569,7 @@ internal static unsafe class LosslessUtils
Span<uint> pixelData, Span<uint> pixelData,
Span<uint> outputSpan) Span<uint> outputSpan)
{ {
fixed (uint* inputFixed = pixelData) fixed (uint* inputFixed = &MemoryMarshal.GetReference(pixelData))
{ {
fixed (uint* outputFixed = outputSpan) fixed (uint* outputFixed = outputSpan)
{ {
@ -1474,8 +1474,7 @@ internal static unsafe class LosslessUtils
{ {
if (Sse2.IsSupported) if (Sse2.IsSupported)
{ {
Span<short> output = scratch; fixed (short* ptr = &MemoryMarshal.GetReference(scratch))
fixed (short* p = output)
{ {
Vector128<byte> a0 = Sse2.ConvertScalarToVector128UInt32(a).AsByte(); Vector128<byte> a0 = Sse2.ConvertScalarToVector128UInt32(a).AsByte();
Vector128<byte> b0 = Sse2.ConvertScalarToVector128UInt32(b).AsByte(); Vector128<byte> b0 = Sse2.ConvertScalarToVector128UInt32(b).AsByte();
@ -1489,8 +1488,8 @@ internal static unsafe class LosslessUtils
Vector128<byte> pa = Sse2.UnpackLow(ac, Vector128<byte>.Zero); // |a - c| Vector128<byte> pa = Sse2.UnpackLow(ac, Vector128<byte>.Zero); // |a - c|
Vector128<byte> pb = Sse2.UnpackLow(bc, Vector128<byte>.Zero); // |b - c| Vector128<byte> pb = Sse2.UnpackLow(bc, Vector128<byte>.Zero); // |b - c|
Vector128<ushort> diff = Sse2.Subtract(pb.AsUInt16(), pa.AsUInt16()); Vector128<ushort> diff = Sse2.Subtract(pb.AsUInt16(), pa.AsUInt16());
Sse2.Store((ushort*)p, diff); Sse2.Store((ushort*)ptr, diff);
int paMinusPb = output[3] + output[2] + output[1] + output[0]; int paMinusPb = ptr[3] + ptr[2] + ptr[1] + ptr[0];
return (paMinusPb <= 0) ? a : b; return (paMinusPb <= 0) ? a : b;
} }
} }

10
src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs

@ -353,8 +353,8 @@ internal static unsafe class PredictorEncoder
else else
{ {
#pragma warning disable SA1503 // Braces should not be omitted #pragma warning disable SA1503 // Braces should not be omitted
fixed (uint* currentRow = currentRowSpan) fixed (uint* currentRow = &MemoryMarshal.GetReference(currentRowSpan))
fixed (uint* upperRow = upperRowSpan) fixed (uint* upperRow = &MemoryMarshal.GetReference(upperRowSpan))
{ {
for (int x = xStart; x < xEnd; x++) for (int x = xStart; x < xEnd; x++)
{ {
@ -664,9 +664,9 @@ internal static unsafe class PredictorEncoder
Span<short> scratch) Span<short> scratch)
{ {
#pragma warning disable SA1503 // Braces should not be omitted #pragma warning disable SA1503 // Braces should not be omitted
fixed (uint* current = currentSpan) fixed (uint* current = &MemoryMarshal.GetReference(currentSpan))
fixed (uint* upper = upperSpan) fixed (uint* upper = &MemoryMarshal.GetReference(upperSpan))
fixed (uint* outputFixed = outputSpan) fixed (uint* outputFixed = &MemoryMarshal.GetReference(outputSpan))
{ {
uint* output = outputFixed; uint* output = outputFixed;
if (xStart == 0) if (xStart == 0)

4
src/ImageSharp/Formats/Webp/WebpCommonUtils.cs

@ -25,7 +25,7 @@ internal static class WebpCommonUtils
ReadOnlySpan<byte> rowBytes = MemoryMarshal.AsBytes(row); ReadOnlySpan<byte> rowBytes = MemoryMarshal.AsBytes(row);
int i = 0; int i = 0;
int length = (row.Length * 4) - 3; int length = (row.Length * 4) - 3;
fixed (byte* src = rowBytes) fixed (byte* src = &MemoryMarshal.GetReference(rowBytes))
{ {
var alphaMaskVector256 = Vector256.Create(0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255); var alphaMaskVector256 = Vector256.Create(0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255);
Vector256<byte> all0x80Vector256 = Vector256.Create((byte)0x80).AsByte(); Vector256<byte> all0x80Vector256 = Vector256.Create((byte)0x80).AsByte();
@ -81,7 +81,7 @@ internal static class WebpCommonUtils
ReadOnlySpan<byte> rowBytes = MemoryMarshal.AsBytes(row); ReadOnlySpan<byte> rowBytes = MemoryMarshal.AsBytes(row);
int i = 0; int i = 0;
int length = (row.Length * 4) - 3; int length = (row.Length * 4) - 3;
fixed (byte* src = rowBytes) fixed (byte* src = &MemoryMarshal.GetReference(rowBytes))
{ {
for (; i + 64 <= length; i += 64) for (; i + 64 <= length; i += 64)
{ {

2
src/ImageSharp/Memory/Buffer2DExtensions.cs

@ -50,7 +50,7 @@ public static class Buffer2DExtensions
Span<byte> span = MemoryMarshal.AsBytes(buffer.DangerousGetSingleMemory().Span); Span<byte> span = MemoryMarshal.AsBytes(buffer.DangerousGetSingleMemory().Span);
fixed (byte* ptr = span) fixed (byte* ptr = &MemoryMarshal.GetReference(span))
{ {
byte* basePtr = ptr; byte* basePtr = ptr;
for (int y = 0; y < buffer.Height; y++) for (int y = 0; y < buffer.Height; y++)

Loading…
Cancel
Save