< Summary

Information
Class: DeltaSharp.Util.SpanHelper
Assembly: DeltaSharp
File(s): /home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Util/SpanHelper.cs
Line coverage
98%
Covered lines: 71
Uncovered lines: 1
Coverable lines: 72
Total lines: 164
Line coverage: 98.6%
Branch coverage
92%
Covered branches: 52
Total branches: 56
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MatchForward(...)100%44100%
MatchBackward(...)100%44100%
MatchForward(...)100%88100%
MatchForwardTail(...)100%66100%
MatchForwardScalar(...)100%88100%
MatchBackward(...)95%2020100%
get_AllEqualMask()50%22100%
EqualByteMask(...)50%2275%
MatchingPrefixBytes(...)50%22100%

File(s)

/home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Util/SpanHelper.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Numerics;
 5using System.Runtime.CompilerServices;
 6using System.Runtime.InteropServices;
 7#if !NETSTANDARD2_0
 8using System.Runtime.Intrinsics;
 9using System.Runtime.Intrinsics.Arm;
 10#endif
 11using System.Text;
 12using System.Threading.Tasks;
 13
 14namespace DeltaSharp.Util;
 15internal class SpanHelper
 16{
 17#if NETSTANDARD2_0
 18    public static int MatchForward(ReadOnlySpan<byte> x, ReadOnlySpan<byte> y)
 19    {
 5143803020        int length = Math.Min(x.Length, y.Length);
 5143803021        int matched = 0;
 63429986622        while (matched < length && x[matched] == y[matched])
 58286183623            matched++;
 5143803024        return matched;
 25    }
 26
 27    public static int MatchBackward(ReadOnlySpan<byte> x, ReadOnlySpan<byte> y)
 28    {
 8741629        int length = Math.Min(x.Length, y.Length);
 8741630        int matched = 0;
 164603831        while (matched < length && x[x.Length - 1 - matched] == y[y.Length - 1 - matched])
 155862232            matched++;
 8741633        return matched;
 34    }
 35#else
 36    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 37    public static int MatchForward(ReadOnlySpan<byte> x, ReadOnlySpan<byte> y)
 38    {
 20575212039        int length = Math.Min(x.Length, y.Length);
 20575212040        ref byte first = ref MemoryMarshal.GetReference(x);
 20575212041        ref byte second = ref MemoryMarshal.GetReference(y);
 20575212042        if (!Vector128.IsHardwareAccelerated || length < Vector128<byte>.Count)
 39748043            return MatchForwardScalar(ref first, ref second, length);
 44
 45        // Most candidates differ in the first vector. Keep that comparison in the caller.
 20535464046        ulong mask = EqualByteMask(ref first, ref second, 0);
 20535464047        if (mask != AllEqualMask)
 20359748848            return MatchingPrefixBytes(mask);
 49
 175715250        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    {
 175621655        nuint last = (nuint)length - (nuint)Vector128<byte>.Count;
 8572385656        for (nuint offset = (nuint)Vector128<byte>.Count; offset < last; offset += (nuint)Vector128<byte>.Count)
 57        {
 4229444858            ulong mask = EqualByteMask(ref first, ref second, offset);
 4229444859            if (mask != AllEqualMask)
 118873660                return (int)offset + MatchingPrefixBytes(mask);
 61        }
 62
 63        // The final vector ends at the shorter span's boundary and can overlap the previous one.
 56748064        ulong finalMask = EqualByteMask(ref first, ref second, last);
 56748065        return finalMask == AllEqualMask ? length : (int)last + MatchingPrefixBytes(finalMask);
 66    }
 67
 68    private static int MatchForwardScalar(ref byte first, ref byte second, int length)
 69    {
 39748070        int i = 0;
 108335271        for (; i < length - 8; i += 8)
 72        {
 34654473            ulong difference = Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref first, i))
 34654474                             ^ Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref second, i));
 34654475            if (difference != 0)
 360876                return i + BitOperations.TrailingZeroCount(difference) / 8;
 77        }
 78
 80947279        while (i < length && Unsafe.Add(ref first, i) == Unsafe.Add(ref second, i))
 41560080            i++;
 39387281        return i;
 82    }
 83
 84    public static int MatchBackward(ReadOnlySpan<byte> x, ReadOnlySpan<byte> y)
 85    {
 34966486        var length = Math.Min(x.Length, y.Length);
 87
 34966488        if (!Vector128.IsHardwareAccelerated || length < 16)
 89        {
 14074490            ref byte xPtr = ref Unsafe.Add(ref MemoryMarshal.GetReference(x), x.Length - 1);
 14074491            ref byte yPtr = ref Unsafe.Add(ref MemoryMarshal.GetReference(y), y.Length - 1);
 92
 14074493            int i = 0;
 14074494            ulong difference = 0;
 95
 14522496            for (; i < length - 8; i = i + 8)
 97            {
 779298                var xlong = Unsafe.ReadUnaligned<ulong>(ref Unsafe.Subtract(ref xPtr, (i + 7)));
 779299                var ylong = Unsafe.ReadUnaligned<ulong>(ref Unsafe.Subtract(ref yPtr, (i + 7)));
 7792100                difference = (xlong ^ ylong);
 101
 7792102                if (difference != 0)
 103                {
 5552104                    return (int)(i + (int)ulong.LeadingZeroCount(difference) / 8);
 105                }
 106            }
 107
 1222584108            for (; i < length; i++)
 109            {
 669072110                if (Unsafe.Add(ref xPtr, -i) != Unsafe.Add(ref yPtr, -i))
 111                    break;
 112            }
 113
 135192114            return i;
 115        }
 116
 208920117        int offset = length - Vector128<byte>.Count;
 208920118        ref byte first = ref Unsafe.Add(ref MemoryMarshal.GetReference(x), x.Length - length);
 208920119        ref byte second = ref Unsafe.Add(ref MemoryMarshal.GetReference(y), y.Length - length);
 120
 121        ulong mask;
 731832122        for (; offset > 0; offset -= Vector128<byte>.Count)
 123        {
 429896124            mask = EqualByteMask(ref first, ref second, (nuint)offset);
 429896125            if (mask != AllEqualMask)
 126                goto Found;
 127        }
 128
 40480129        offset = 0;
 40480130        mask = EqualByteMask(ref first, ref second, 0);
 40480131        if (mask == AllEqualMask)
 4168132            return length;
 133
 134    Found:
 204752135        int matchingSuffix = AdvSimd.Arm64.IsSupported
 204752136            ? BitOperations.LeadingZeroCount(~mask) / 4
 204752137            : ushort.LeadingZeroCount((ushort)~mask);
 204752138        return length - offset - Vector128<byte>.Count + matchingSuffix;
 139    }
 140
 141    private static ulong AllEqualMask
 142    {
 143        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 248686944144        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    {
 248686944150        var equal = Vector128.Equals(Vector128.LoadUnsafe(ref first, offset), Vector128.LoadUnsafe(ref second, offset));
 248686944151        if (AdvSimd.Arm64.IsSupported)
 152        {
 153            // Each equality byte is 00 or FF. Narrow each pair to two four-bit results.
 0154            return AdvSimd.ShiftRightLogicalNarrowingLower(equal.AsUInt16(), 4).AsUInt64().ToScalar();
 155        }
 248686944156        return equal.ExtractMostSignificantBits();
 157    }
 158
 159    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 204825808160    private static int MatchingPrefixBytes(ulong mask) => AdvSimd.Arm64.IsSupported
 204825808161        ? BitOperations.TrailingZeroCount(~mask) / 4
 204825808162        : BitOperations.TrailingZeroCount((uint)~mask);
 163#endif
 164}