|
6 | 6 | using System.Runtime.CompilerServices;
|
7 | 7 | using System.Runtime.InteropServices;
|
8 | 8 | using System.Runtime.Intrinsics;
|
| 9 | +using System.Runtime.Intrinsics.Arm; |
9 | 10 | using System.Runtime.Intrinsics.X86;
|
10 | 11 | using SixLabors.ImageSharp.Advanced;
|
11 | 12 | using SixLabors.ImageSharp.Memory;
|
@@ -175,7 +176,52 @@ public static (bool Difference, Rectangle Bounds) DeDuplicatePixels<TPixel>(
|
175 | 176 | }
|
176 | 177 | }
|
177 | 178 |
|
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 | + |
179 | 225 | for (i = remaining; i > 0; i--)
|
180 | 226 | {
|
181 | 227 | x = (uint)(length - i);
|
|
0 commit comments