Skip to content

Commit c3dacde

Browse files
Add AdvSimd implementation
1 parent 83322e3 commit c3dacde

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/ImageSharp/Formats/AnimationUtilities.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Runtime.CompilerServices;
77
using System.Runtime.InteropServices;
88
using System.Runtime.Intrinsics;
9+
using System.Runtime.Intrinsics.Arm;
910
using System.Runtime.Intrinsics.X86;
1011
using SixLabors.ImageSharp.Advanced;
1112
using SixLabors.ImageSharp.Memory;
@@ -175,7 +176,52 @@ public static (bool Difference, Rectangle Bounds) DeDuplicatePixels<TPixel>(
175176
}
176177
}
177178

178-
// TODO: v4 AdvSimd when we can use .NET 8
179+
if (AdvSimd.IsSupported && remaining >= 4)
180+
{
181+
// Update offset since we may be operating on the remainder previously incremented by pixel steps of 8.
182+
x *= 2;
183+
Vector128<uint> r128 = previousFrame != null ? Vector128.Create(bg.PackedValue) : Vector128<uint>.Zero;
184+
Vector128<uint> vmb128 = Vector128<uint>.Zero;
185+
if (blend)
186+
{
187+
vmb128 = AdvSimd.CompareEqual(vmb128, vmb128);
188+
}
189+
190+
while (remaining >= 4)
191+
{
192+
Vector128<uint> p = Unsafe.Add(ref Unsafe.As<Vector256<byte>, Vector128<uint>>(ref previousBase256), x);
193+
Vector128<uint> c = Unsafe.Add(ref Unsafe.As<Vector256<byte>, Vector128<uint>>(ref currentBase256), x);
194+
195+
Vector128<uint> eq = AdvSimd.CompareEqual(p, c);
196+
Vector128<uint> r = SimdUtils.HwIntrinsics.BlendVariable(c, r128, AdvSimd.And(eq, vmb128));
197+
198+
if (nextFrame != null)
199+
{
200+
Vector128<int> n = AdvSimd.ShiftRightLogical(Unsafe.Add(ref Unsafe.As<Vector256<byte>, Vector128<uint>>(ref nextBase256), x), 24).AsInt32();
201+
eq = AdvSimd.BitwiseClear(eq, AdvSimd.CompareGreaterThan(AdvSimd.ShiftRightLogical(c, 24).AsInt32(), n).AsUInt32());
202+
}
203+
204+
Unsafe.Add(ref Unsafe.As<Vector256<byte>, Vector128<uint>>(ref resultBase256), x) = r;
205+
206+
ulong msk = ~AdvSimd.ExtractNarrowingLower(eq).AsUInt64().ToScalar();
207+
if (msk != 0)
208+
{
209+
// If is diff is found, the left side is marked by the min of previously found left side and the start position.
210+
// The right is the max of the previously found right side and the end position.
211+
int start = i + (BitOperations.TrailingZeroCount(msk) / 16);
212+
int end = i + (4 - (BitOperations.LeadingZeroCount(msk) / 16));
213+
left = Math.Min(left, start);
214+
right = Math.Max(right, end);
215+
hasRowDiff = true;
216+
hasDiff = true;
217+
}
218+
219+
x++;
220+
i += 4;
221+
remaining -= 4;
222+
}
223+
}
224+
179225
for (i = remaining; i > 0; i--)
180226
{
181227
x = (uint)(length - i);

0 commit comments

Comments
 (0)