Skip to content

Commit c8f1f2c

Browse files
committed
Simplified check if there are any non-equal bytes
Hm, I remembered that movemask isn't the fastest, and ptest (TestZ in .NET-terms) is faster but current benchmarks didn't prove this, also Intel's instruction table didn't show any benefit in terms of latency or throughput. Thus simplified that check.
1 parent 5416edb commit c8f1f2c

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/ImageSharp/Formats/Gif/GifEncoderCore.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -435,11 +435,11 @@ private static Buffer2DRegion<byte> TrimTransparentPixels(Buffer2D<byte> buffer,
435435
{
436436
Vector256<byte> vec = Vector256.LoadUnsafe(ref rowPtr, (nuint)x);
437437
Vector256<byte> notEquals = ~Vector256.Equals(vec, trimmableVec256);
438+
uint mask = notEquals.ExtractMostSignificantBits();
438439

439-
if (notEquals != Vector256<byte>.Zero)
440+
if (mask != 0)
440441
{
441442
isTransparentRow = false;
442-
uint mask = notEquals.ExtractMostSignificantBits();
443443
nint start = x + (nint)uint.TrailingZeroCount(mask);
444444
nint end = (nint)uint.LeadingZeroCount(mask);
445445

@@ -463,11 +463,11 @@ private static Buffer2DRegion<byte> TrimTransparentPixels(Buffer2D<byte> buffer,
463463
{
464464
Vector128<byte> vec = Vector128.LoadUnsafe(ref rowPtr, (nuint)x);
465465
Vector128<byte> notEquals = ~Vector128.Equals(vec, trimmableVec);
466+
uint mask = notEquals.ExtractMostSignificantBits();
466467

467-
if (notEquals != Vector128<byte>.Zero)
468+
if (mask != 0)
468469
{
469470
isTransparentRow = false;
470-
uint mask = notEquals.ExtractMostSignificantBits();
471471
nint start = x + (nint)uint.TrailingZeroCount(mask);
472472
nint end = (nint)uint.LeadingZeroCount(mask) - Vector128<byte>.Count;
473473

@@ -493,11 +493,11 @@ private static Buffer2DRegion<byte> TrimTransparentPixels(Buffer2D<byte> buffer,
493493
Vector256<byte> vec = Unsafe.ReadUnaligned<Vector256<byte>>(ref Unsafe.Add(ref rowPtr, x));
494494
Vector256<byte> notEquals = Avx2.CompareEqual(vec, trimmableVec256);
495495
notEquals = Avx2.Xor(notEquals, Vector256<byte>.AllBitsSet);
496+
int mask = Avx2.MoveMask(notEquals);
496497

497-
if (!Avx.TestZ(notEquals, notEquals))
498+
if (mask != 0)
498499
{
499500
isTransparentRow = false;
500-
int mask = Avx2.MoveMask(notEquals);
501501
nint start = x + (nint)(uint)BitOperations.TrailingZeroCount(mask);
502502
nint end = (nint)(uint)BitOperations.LeadingZeroCount((uint)mask);
503503

@@ -522,11 +522,11 @@ private static Buffer2DRegion<byte> TrimTransparentPixels(Buffer2D<byte> buffer,
522522
Vector128<byte> vec = Unsafe.ReadUnaligned<Vector128<byte>>(ref Unsafe.Add(ref rowPtr, x));
523523
Vector128<byte> notEquals = Sse2.CompareEqual(vec, trimmableVec);
524524
notEquals = Sse2.Xor(notEquals, Vector128<byte>.AllBitsSet);
525+
int mask = Sse2.MoveMask(notEquals);
525526

526-
if (!Sse41.TestZ(notEquals, notEquals))
527+
if (mask != 0)
527528
{
528529
isTransparentRow = false;
529-
int mask = Sse2.MoveMask(notEquals);
530530
nint start = x + (nint)(uint)BitOperations.TrailingZeroCount(mask);
531531
nint end = (nint)(uint)BitOperations.LeadingZeroCount((uint)mask) - Vector128<byte>.Count;
532532

0 commit comments

Comments
 (0)