Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 126 additions & 7 deletions src/ImageSharp/Formats/Gif/GifEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,23 +412,142 @@ private static Buffer2DRegion<byte> TrimTransparentPixels(Buffer2D<byte> buffer,
int bottom = int.MaxValue;
int left = int.MaxValue;
int right = int.MinValue;

// Run through th buffer in a single pass. Use variables to track the min/max values.
int minY = -1;
bool isTransparentRow = true;

// Run through the buffer in a single pass. Use variables to track the min/max values.
for (int y = 0; y < buffer.Height; y++)
{
isTransparentRow = true;
Span<byte> rowSpan = buffer.DangerousGetRowSpan(y);
ref byte rowPtr = ref MemoryMarshal.GetReference(rowSpan);
nint rowLength = (nint)(uint)rowSpan.Length;
nint x = 0;

#if NET7_0_OR_GREATER
if (Vector128.IsHardwareAccelerated && rowLength >= Vector128<byte>.Count)
{
Vector256<byte> trimmableVec256 = Vector256.Create(trimmableIndex);

if (Vector256.IsHardwareAccelerated && rowLength >= Vector256<byte>.Count)
{
do
{
Vector256<byte> vec = Vector256.LoadUnsafe(ref rowPtr, (nuint)x);
Vector256<byte> notEquals = ~Vector256.Equals(vec, trimmableVec256);

if (notEquals != Vector256<byte>.Zero)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment I don't have any idea on how to make this branchless.
isTransparentRow could be tracked in a vector, but left and right not, as there's a mismatch of vector-types, namely byte and int.

A quite complicated approach would be to use VectorXYZ<byte> and track the left and right -- but just before these can overflow merge it back to the scalar left, right and start over. But I guess the book-keeping is more work, so I'm not sure if this is actually faster. For sure the code gets painful.

{
isTransparentRow = false;
uint mask = notEquals.ExtractMostSignificantBits();
nint start = x + (nint)uint.TrailingZeroCount(mask);
nint end = (nint)uint.LeadingZeroCount(mask);

// end is from the end, but we need the index from the beginning
end = x + Vector256<byte>.Count - 1 - end;

left = Math.Min(left, (int)start);
right = Math.Max(right, (int)end);
}

x += Vector256<byte>.Count;
}
while (x <= rowLength - Vector256<byte>.Count);
}

Vector128<byte> trimmableVec = Vector256.IsHardwareAccelerated
? trimmableVec256.GetLower()
: Vector128.Create(trimmableIndex);

while (x <= rowLength - Vector128<byte>.Count)
{
Vector128<byte> vec = Vector128.LoadUnsafe(ref rowPtr, (nuint)x);
Vector128<byte> notEquals = ~Vector128.Equals(vec, trimmableVec);

if (notEquals != Vector128<byte>.Zero)
{
isTransparentRow = false;
uint mask = notEquals.ExtractMostSignificantBits();
nint start = x + (nint)uint.TrailingZeroCount(mask);
nint end = (nint)uint.LeadingZeroCount(mask) - Vector128<byte>.Count;

// end is from the end, but we need the index from the beginning
end = x + Vector128<byte>.Count - 1 - end;

left = Math.Min(left, (int)start);
right = Math.Max(right, (int)end);
}

x += Vector128<byte>.Count;
}
}
#else
if (Sse41.IsSupported && rowLength >= Vector128<byte>.Count)
{
Vector256<byte> trimmableVec256 = Vector256.Create(trimmableIndex);

if (Avx2.IsSupported && rowLength >= Vector256<byte>.Count)
{
do
{
Vector256<byte> vec = Unsafe.ReadUnaligned<Vector256<byte>>(ref Unsafe.Add(ref rowPtr, x));
Vector256<byte> notEquals = Avx2.CompareEqual(vec, trimmableVec256);
notEquals = Avx2.Xor(notEquals, Vector256<byte>.AllBitsSet);

if (!Avx.TestZ(notEquals, notEquals))
{
isTransparentRow = false;
int mask = Avx2.MoveMask(notEquals);
nint start = x + (nint)(uint)BitOperations.TrailingZeroCount(mask);
nint end = (nint)(uint)BitOperations.LeadingZeroCount((uint)mask);

// end is from the end, but we need the index from the beginning
end = x + Vector256<byte>.Count - 1 - end;

left = Math.Min(left, (int)start);
right = Math.Max(right, (int)end);
}

x += Vector256<byte>.Count;
}
while (x <= rowLength - Vector256<byte>.Count);
}

Vector128<byte> trimmableVec = Sse41.IsSupported
? trimmableVec256.GetLower()
: Vector128.Create(trimmableIndex);

while (x <= rowLength - Vector128<byte>.Count)
{
Vector128<byte> vec = Unsafe.ReadUnaligned<Vector128<byte>>(ref Unsafe.Add(ref rowPtr, x));
Vector128<byte> notEquals = Sse2.CompareEqual(vec, trimmableVec);
notEquals = Sse2.Xor(notEquals, Vector128<byte>.AllBitsSet);

if (!Sse41.TestZ(notEquals, notEquals))
{
isTransparentRow = false;
int mask = Sse2.MoveMask(notEquals);
nint start = x + (nint)(uint)BitOperations.TrailingZeroCount(mask);
nint end = (nint)(uint)BitOperations.LeadingZeroCount((uint)mask) - Vector128<byte>.Count;

// TODO: It may be possible to optimize this inner loop using SIMD.
for (int x = 0; x < rowSpan.Length; x++)
// end is from the end, but we need the index from the beginning
end = x + Vector128<byte>.Count - 1 - end;

left = Math.Min(left, (int)start);
right = Math.Max(right, (int)end);
}

x += Vector128<byte>.Count;
}
}
#endif
for (; x < rowLength; ++x)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The remainder could be handled vectorized too, by shifting the mask of the most significant bits around by the count of elements left in the final vector.
I tried this somewhere else, the cost for that book-keeping isn't negligible, so didn't do this here and now (maybe I'll try this later).

{
if (rowSpan[x] != trimmableIndex)
if (Unsafe.Add(ref rowPtr, x) != trimmableIndex)
{
isTransparentRow = false;
left = Math.Min(left, x);
right = Math.Max(right, x);
left = Math.Min(left, (int)x);
right = Math.Max(right, (int)x);
}
}

Expand Down