| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Numerics; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | #if !NETSTANDARD2_0 |
| | | 8 | | using System.Runtime.Intrinsics; |
| | | 9 | | using System.Runtime.Intrinsics.Arm; |
| | | 10 | | #endif |
| | | 11 | | using System.Text; |
| | | 12 | | using System.Threading.Tasks; |
| | | 13 | | |
| | | 14 | | namespace DeltaSharp.Util; |
| | | 15 | | internal class SpanHelper |
| | | 16 | | { |
| | | 17 | | #if NETSTANDARD2_0 |
| | | 18 | | public static int MatchForward(ReadOnlySpan<byte> x, ReadOnlySpan<byte> y) |
| | | 19 | | { |
| | 51438030 | 20 | | int length = Math.Min(x.Length, y.Length); |
| | 51438030 | 21 | | int matched = 0; |
| | 634299866 | 22 | | while (matched < length && x[matched] == y[matched]) |
| | 582861836 | 23 | | matched++; |
| | 51438030 | 24 | | return matched; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public static int MatchBackward(ReadOnlySpan<byte> x, ReadOnlySpan<byte> y) |
| | | 28 | | { |
| | 87416 | 29 | | int length = Math.Min(x.Length, y.Length); |
| | 87416 | 30 | | int matched = 0; |
| | 1646038 | 31 | | while (matched < length && x[x.Length - 1 - matched] == y[y.Length - 1 - matched]) |
| | 1558622 | 32 | | matched++; |
| | 87416 | 33 | | return matched; |
| | | 34 | | } |
| | | 35 | | #else |
| | | 36 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 37 | | public static int MatchForward(ReadOnlySpan<byte> x, ReadOnlySpan<byte> y) |
| | | 38 | | { |
| | 205752120 | 39 | | int length = Math.Min(x.Length, y.Length); |
| | 205752120 | 40 | | ref byte first = ref MemoryMarshal.GetReference(x); |
| | 205752120 | 41 | | ref byte second = ref MemoryMarshal.GetReference(y); |
| | 205752120 | 42 | | if (!Vector128.IsHardwareAccelerated || length < Vector128<byte>.Count) |
| | 397480 | 43 | | return MatchForwardScalar(ref first, ref second, length); |
| | | 44 | | |
| | | 45 | | // Most candidates differ in the first vector. Keep that comparison in the caller. |
| | 205354640 | 46 | | ulong mask = EqualByteMask(ref first, ref second, 0); |
| | 205354640 | 47 | | if (mask != AllEqualMask) |
| | 203597488 | 48 | | return MatchingPrefixBytes(mask); |
| | | 49 | | |
| | 1757152 | 50 | | return length == Vector128<byte>.Count ? length : MatchForwardTail(ref first, ref second, length); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | private static int MatchForwardTail(ref byte first, ref byte second, int length) |
| | | 54 | | { |
| | 1756216 | 55 | | nuint last = (nuint)length - (nuint)Vector128<byte>.Count; |
| | 85723856 | 56 | | for (nuint offset = (nuint)Vector128<byte>.Count; offset < last; offset += (nuint)Vector128<byte>.Count) |
| | | 57 | | { |
| | 42294448 | 58 | | ulong mask = EqualByteMask(ref first, ref second, offset); |
| | 42294448 | 59 | | if (mask != AllEqualMask) |
| | 1188736 | 60 | | return (int)offset + MatchingPrefixBytes(mask); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | // The final vector ends at the shorter span's boundary and can overlap the previous one. |
| | 567480 | 64 | | ulong finalMask = EqualByteMask(ref first, ref second, last); |
| | 567480 | 65 | | return finalMask == AllEqualMask ? length : (int)last + MatchingPrefixBytes(finalMask); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | private static int MatchForwardScalar(ref byte first, ref byte second, int length) |
| | | 69 | | { |
| | 397480 | 70 | | int i = 0; |
| | 1083352 | 71 | | for (; i < length - 8; i += 8) |
| | | 72 | | { |
| | 346544 | 73 | | ulong difference = Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref first, i)) |
| | 346544 | 74 | | ^ Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref second, i)); |
| | 346544 | 75 | | if (difference != 0) |
| | 3608 | 76 | | return i + BitOperations.TrailingZeroCount(difference) / 8; |
| | | 77 | | } |
| | | 78 | | |
| | 809472 | 79 | | while (i < length && Unsafe.Add(ref first, i) == Unsafe.Add(ref second, i)) |
| | 415600 | 80 | | i++; |
| | 393872 | 81 | | return i; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public static int MatchBackward(ReadOnlySpan<byte> x, ReadOnlySpan<byte> y) |
| | | 85 | | { |
| | 349664 | 86 | | var length = Math.Min(x.Length, y.Length); |
| | | 87 | | |
| | 349664 | 88 | | if (!Vector128.IsHardwareAccelerated || length < 16) |
| | | 89 | | { |
| | 140744 | 90 | | ref byte xPtr = ref Unsafe.Add(ref MemoryMarshal.GetReference(x), x.Length - 1); |
| | 140744 | 91 | | ref byte yPtr = ref Unsafe.Add(ref MemoryMarshal.GetReference(y), y.Length - 1); |
| | | 92 | | |
| | 140744 | 93 | | int i = 0; |
| | 140744 | 94 | | ulong difference = 0; |
| | | 95 | | |
| | 145224 | 96 | | for (; i < length - 8; i = i + 8) |
| | | 97 | | { |
| | 7792 | 98 | | var xlong = Unsafe.ReadUnaligned<ulong>(ref Unsafe.Subtract(ref xPtr, (i + 7))); |
| | 7792 | 99 | | var ylong = Unsafe.ReadUnaligned<ulong>(ref Unsafe.Subtract(ref yPtr, (i + 7))); |
| | 7792 | 100 | | difference = (xlong ^ ylong); |
| | | 101 | | |
| | 7792 | 102 | | if (difference != 0) |
| | | 103 | | { |
| | 5552 | 104 | | return (int)(i + (int)ulong.LeadingZeroCount(difference) / 8); |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | 1222584 | 108 | | for (; i < length; i++) |
| | | 109 | | { |
| | 669072 | 110 | | if (Unsafe.Add(ref xPtr, -i) != Unsafe.Add(ref yPtr, -i)) |
| | | 111 | | break; |
| | | 112 | | } |
| | | 113 | | |
| | 135192 | 114 | | return i; |
| | | 115 | | } |
| | | 116 | | |
| | 208920 | 117 | | int offset = length - Vector128<byte>.Count; |
| | 208920 | 118 | | ref byte first = ref Unsafe.Add(ref MemoryMarshal.GetReference(x), x.Length - length); |
| | 208920 | 119 | | ref byte second = ref Unsafe.Add(ref MemoryMarshal.GetReference(y), y.Length - length); |
| | | 120 | | |
| | | 121 | | ulong mask; |
| | 731832 | 122 | | for (; offset > 0; offset -= Vector128<byte>.Count) |
| | | 123 | | { |
| | 429896 | 124 | | mask = EqualByteMask(ref first, ref second, (nuint)offset); |
| | 429896 | 125 | | if (mask != AllEqualMask) |
| | | 126 | | goto Found; |
| | | 127 | | } |
| | | 128 | | |
| | 40480 | 129 | | offset = 0; |
| | 40480 | 130 | | mask = EqualByteMask(ref first, ref second, 0); |
| | 40480 | 131 | | if (mask == AllEqualMask) |
| | 4168 | 132 | | return length; |
| | | 133 | | |
| | | 134 | | Found: |
| | 204752 | 135 | | int matchingSuffix = AdvSimd.Arm64.IsSupported |
| | 204752 | 136 | | ? BitOperations.LeadingZeroCount(~mask) / 4 |
| | 204752 | 137 | | : ushort.LeadingZeroCount((ushort)~mask); |
| | 204752 | 138 | | return length - offset - Vector128<byte>.Count + matchingSuffix; |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | private static ulong AllEqualMask |
| | | 142 | | { |
| | | 143 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 248686944 | 144 | | get => AdvSimd.Arm64.IsSupported ? ulong.MaxValue : ushort.MaxValue; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 148 | | private static ulong EqualByteMask(ref byte first, ref byte second, nuint offset) |
| | | 149 | | { |
| | 248686944 | 150 | | var equal = Vector128.Equals(Vector128.LoadUnsafe(ref first, offset), Vector128.LoadUnsafe(ref second, offset)); |
| | 248686944 | 151 | | if (AdvSimd.Arm64.IsSupported) |
| | | 152 | | { |
| | | 153 | | // Each equality byte is 00 or FF. Narrow each pair to two four-bit results. |
| | 0 | 154 | | return AdvSimd.ShiftRightLogicalNarrowingLower(equal.AsUInt16(), 4).AsUInt64().ToScalar(); |
| | | 155 | | } |
| | 248686944 | 156 | | return equal.ExtractMostSignificantBits(); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 204825808 | 160 | | private static int MatchingPrefixBytes(ulong mask) => AdvSimd.Arm64.IsSupported |
| | 204825808 | 161 | | ? BitOperations.TrailingZeroCount(~mask) / 4 |
| | 204825808 | 162 | | : BitOperations.TrailingZeroCount((uint)~mask); |
| | | 163 | | #endif |
| | | 164 | | } |