|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | + |
| 8 | +#if !netstandard |
| 9 | +using Internal.Runtime.CompilerServices; |
| 10 | +#endif |
| 11 | + |
| 12 | +#if !netstandard11 |
| 13 | +using System.Numerics; |
| 14 | +#endif |
| 15 | + |
| 16 | +namespace System |
| 17 | +{ |
| 18 | + internal static partial class SpanHelpers |
| 19 | + { |
| 20 | + public static unsafe int SequenceCompareTo(ref char first, int firstLength, ref char second, int secondLength) |
| 21 | + { |
| 22 | + Debug.Assert(firstLength >= 0); |
| 23 | + Debug.Assert(secondLength >= 0); |
| 24 | + |
| 25 | + int lengthDelta = firstLength - secondLength; |
| 26 | + |
| 27 | + if (Unsafe.AreSame(ref first, ref second)) |
| 28 | + goto Equal; |
| 29 | + |
| 30 | + IntPtr minLength = (IntPtr)((firstLength < secondLength) ? firstLength : secondLength); |
| 31 | + IntPtr i = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations |
| 32 | + |
| 33 | + if ((byte*)minLength >= (byte*)(sizeof(UIntPtr) / sizeof(char))) |
| 34 | + { |
| 35 | +#if !netstandard11 |
| 36 | + if (Vector.IsHardwareAccelerated && (byte*)minLength >= (byte*)Vector<ushort>.Count) |
| 37 | + { |
| 38 | + IntPtr nLength = minLength - Vector<ushort>.Count; |
| 39 | + do |
| 40 | + { |
| 41 | + if (Unsafe.ReadUnaligned<Vector<ushort>>(ref Unsafe.As<char, byte>(ref Unsafe.Add(ref first, i))) != |
| 42 | + Unsafe.ReadUnaligned<Vector<ushort>>(ref Unsafe.As<char, byte>(ref Unsafe.Add(ref second, i)))) |
| 43 | + { |
| 44 | + break; |
| 45 | + } |
| 46 | + i += Vector<ushort>.Count; |
| 47 | + } |
| 48 | + while ((byte*)nLength >= (byte*)i); |
| 49 | + } |
| 50 | +#endif |
| 51 | + |
| 52 | + while ((byte*)minLength >= (byte*)(i + sizeof(UIntPtr) / sizeof(char))) |
| 53 | + { |
| 54 | + if (Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.As<char, byte>(ref Unsafe.Add(ref first, i))) != |
| 55 | + Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.As<char, byte>(ref Unsafe.Add(ref second, i)))) |
| 56 | + { |
| 57 | + break; |
| 58 | + } |
| 59 | + i += sizeof(UIntPtr) / sizeof(char); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (sizeof(UIntPtr) > sizeof(int) && (byte*)minLength >= (byte*)(i + sizeof(int) / sizeof(char))) |
| 64 | + { |
| 65 | + if (Unsafe.ReadUnaligned<int>(ref Unsafe.As<char, byte>(ref Unsafe.Add(ref first, i))) == |
| 66 | + Unsafe.ReadUnaligned<int>(ref Unsafe.As<char, byte>(ref Unsafe.Add(ref second, i)))) |
| 67 | + { |
| 68 | + i += sizeof(int) / sizeof(char); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + while ((byte*)i < (byte*)minLength) |
| 73 | + { |
| 74 | + int result = Unsafe.Add(ref first, i).CompareTo(Unsafe.Add(ref second, i)); |
| 75 | + if (result != 0) return result; |
| 76 | + i += 1; |
| 77 | + } |
| 78 | + |
| 79 | + Equal: |
| 80 | + return lengthDelta; |
| 81 | + } |
| 82 | + |
| 83 | + public static unsafe int IndexOf(ref char searchSpace, char value, int length) |
| 84 | + { |
| 85 | + Debug.Assert(length >= 0); |
| 86 | + |
| 87 | + uint uValue = value; // Use uint for comparisons to avoid unnecessary 8->32 extensions |
| 88 | + IntPtr index = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations |
| 89 | + IntPtr nLength = (IntPtr)length; |
| 90 | +#if !netstandard11 |
| 91 | + if (Vector.IsHardwareAccelerated && length >= Vector<ushort>.Count * 2) |
| 92 | + { |
| 93 | + const int elementsPerByte = sizeof(ushort) / sizeof(byte); |
| 94 | + int unaligned = ((int)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1)) / elementsPerByte; |
| 95 | + nLength = (IntPtr)((Vector<ushort>.Count - unaligned) & (Vector<ushort>.Count - 1)); |
| 96 | + } |
| 97 | + SequentialScan: |
| 98 | +#endif |
| 99 | + while ((byte*)nLength >= (byte*)4) |
| 100 | + { |
| 101 | + nLength -= 4; |
| 102 | + |
| 103 | + if (uValue == Unsafe.Add(ref searchSpace, index)) |
| 104 | + goto Found; |
| 105 | + if (uValue == Unsafe.Add(ref searchSpace, index + 1)) |
| 106 | + goto Found1; |
| 107 | + if (uValue == Unsafe.Add(ref searchSpace, index + 2)) |
| 108 | + goto Found2; |
| 109 | + if (uValue == Unsafe.Add(ref searchSpace, index + 3)) |
| 110 | + goto Found3; |
| 111 | + |
| 112 | + index += 4; |
| 113 | + } |
| 114 | + |
| 115 | + while ((byte*)nLength > (byte*)0) |
| 116 | + { |
| 117 | + nLength -= 1; |
| 118 | + |
| 119 | + if (uValue == Unsafe.Add(ref searchSpace, index)) |
| 120 | + goto Found; |
| 121 | + |
| 122 | + index += 1; |
| 123 | + } |
| 124 | +#if !netstandard11 |
| 125 | + if (Vector.IsHardwareAccelerated && ((int)(byte*)index < length)) |
| 126 | + { |
| 127 | + nLength = (IntPtr)((length - (int)(byte*)index) & ~(Vector<ushort>.Count - 1)); |
| 128 | + |
| 129 | + // Get comparison Vector |
| 130 | + Vector<ushort> vComparison = new Vector<ushort>(value); |
| 131 | + |
| 132 | + while ((byte*)nLength > (byte*)index) |
| 133 | + { |
| 134 | + var vMatches = Vector.Equals(vComparison, Unsafe.ReadUnaligned<Vector<ushort>>(ref Unsafe.As<char, byte>(ref Unsafe.Add(ref searchSpace, index)))); |
| 135 | + if (Vector<ushort>.Zero.Equals(vMatches)) |
| 136 | + { |
| 137 | + index += Vector<ushort>.Count; |
| 138 | + continue; |
| 139 | + } |
| 140 | + // Find offset of first match |
| 141 | + return (int)(byte*)index + LocateFirstFoundChar(vMatches); |
| 142 | + } |
| 143 | + |
| 144 | + if ((int)(byte*)index < length) |
| 145 | + { |
| 146 | + nLength = (IntPtr)(length - (int)(byte*)index); |
| 147 | + goto SequentialScan; |
| 148 | + } |
| 149 | + } |
| 150 | +#endif |
| 151 | + return -1; |
| 152 | + Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549 |
| 153 | + return (int)(byte*)index; |
| 154 | + Found1: |
| 155 | + return (int)(byte*)(index + 1); |
| 156 | + Found2: |
| 157 | + return (int)(byte*)(index + 2); |
| 158 | + Found3: |
| 159 | + return (int)(byte*)(index + 3); |
| 160 | + } |
| 161 | + |
| 162 | +#if !netstandard11 |
| 163 | + // Vector sub-search adapted from https://github.com/aspnet/KestrelHttpServer/pull/1138 |
| 164 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 165 | + private static int LocateFirstFoundChar(Vector<ushort> match) |
| 166 | + { |
| 167 | + var vector64 = Vector.AsVectorUInt64(match); |
| 168 | + ulong candidate = 0; |
| 169 | + int i = 0; |
| 170 | + // Pattern unrolled by jit https://github.com/dotnet/coreclr/pull/8001 |
| 171 | + for (; i < Vector<ulong>.Count; i++) |
| 172 | + { |
| 173 | + candidate = vector64[i]; |
| 174 | + if (candidate != 0) |
| 175 | + { |
| 176 | + break; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // Single LEA instruction with jitted const (using function result) |
| 181 | + return i * 4 + LocateFirstFoundChar(candidate); |
| 182 | + } |
| 183 | + |
| 184 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 185 | + private static int LocateFirstFoundChar(ulong match) |
| 186 | + { |
| 187 | + unchecked |
| 188 | + { |
| 189 | + // Flag least significant power of two bit |
| 190 | + var powerOfTwoFlag = match ^ (match - 1); |
| 191 | + // Shift all powers of two into the high byte and extract |
| 192 | + return (int)((powerOfTwoFlag * XorPowerOfTwoToHighChar) >> 49); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private const ulong XorPowerOfTwoToHighChar = (0x03ul | |
| 197 | + 0x02ul << 16 | |
| 198 | + 0x01ul << 32) + 1; |
| 199 | +#endif |
| 200 | + } |
| 201 | +} |
0 commit comments