| | | 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 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Runtime.Intrinsics; |
| | | 8 | | using System.Runtime.Intrinsics.Wasm; |
| | | 9 | | using System.Runtime.Intrinsics.X86; |
| | | 10 | | |
| | | 11 | | namespace System.Buffers |
| | | 12 | | { |
| | | 13 | | internal sealed class ProbabilisticWithAsciiCharSearchValues<TOptimizations> : SearchValues<char> |
| | | 14 | | where TOptimizations : struct, IndexOfAnyAsciiSearcher.IOptimizations |
| | | 15 | | { |
| | | 16 | | private IndexOfAnyAsciiSearcher.AsciiState _asciiState; |
| | | 17 | | private IndexOfAnyAsciiSearcher.AsciiState _inverseAsciiState; |
| | | 18 | | private ProbabilisticMapState _map; |
| | | 19 | | |
| | 1395 | 20 | | public ProbabilisticWithAsciiCharSearchValues(ReadOnlySpan<char> values, int maxInclusive) |
| | | 21 | | { |
| | 1395 | 22 | | Debug.Assert(IndexOfAnyAsciiSearcher.IsVectorizationSupported); |
| | 1395 | 23 | | Debug.Assert(values.ContainsAnyInRange((char)0, (char)127)); |
| | | 24 | | |
| | 1395 | 25 | | IndexOfAnyAsciiSearcher.ComputeAsciiState(values, out _asciiState); |
| | 1395 | 26 | | _inverseAsciiState = _asciiState.CreateInverse(); |
| | | 27 | | |
| | 1395 | 28 | | _map = new ProbabilisticMapState(values, maxInclusive); |
| | 1395 | 29 | | } |
| | | 30 | | |
| | | 31 | | internal override char[] GetValues() => |
| | 0 | 32 | | _map.GetValues(); |
| | | 33 | | |
| | | 34 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 35 | | internal override bool ContainsCore(char value) => |
| | 133785 | 36 | | _map.FastContains(value); |
| | | 37 | | |
| | | 38 | | internal override int IndexOfAny(ReadOnlySpan<char> span) |
| | | 39 | | { |
| | 5580 | 40 | | int offset = 0; |
| | | 41 | | |
| | | 42 | | // We check whether the first character is ASCII before calling into IndexOfAnyAsciiSearcher |
| | | 43 | | // in order to minimize the overhead this fast-path has on non-ASCII texts. |
| | 5580 | 44 | | if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count && char.IsAsci |
| | | 45 | | { |
| | | 46 | | // We are using IndexOfAnyAsciiSearcher to search for the first ASCII character in the set, or any non-A |
| | | 47 | | // We do this by inverting the bitmap and using the opposite search function (Negate instead of DontNega |
| | | 48 | | |
| | | 49 | | // If the bitmap we're using contains a 0, we have to use 'Ssse3AndWasmHandleZeroInNeedle' when running |
| | | 50 | | // Everything else should use 'Default'. 'TOptimizations' specifies whether '_asciiState' contains a 0. |
| | | 51 | | // Since we're using the inverse bitmap in this case, we have to use 'Ssse3AndWasmHandleZeroInNeedle' if |
| | | 52 | | // running on X86/WASM and 'TOptimizations' is 'Default' (as that means that the inverse bitmap definite |
| | 1056 | 53 | | Debug.Assert(_asciiState.Lookup.Contains(0) != _inverseAsciiState.Lookup.Contains(0)); |
| | | 54 | | |
| | 1056 | 55 | | if ((Ssse3.IsSupported || PackedSimd.IsSupported) && typeof(TOptimizations) == typeof(IndexOfAnyAsciiSea |
| | | 56 | | { |
| | 271 | 57 | | Debug.Assert(_inverseAsciiState.Lookup.Contains(0), "The inverse bitmap did not contain a 0."); |
| | | 58 | | |
| | 271 | 59 | | offset = IndexOfAnyAsciiSearcher.IndexOfAny<IndexOfAnyAsciiSearcher.Negate, IndexOfAnyAsciiSearcher. |
| | 271 | 60 | | ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), |
| | 271 | 61 | | span.Length, |
| | 271 | 62 | | ref _inverseAsciiState); |
| | | 63 | | } |
| | | 64 | | else |
| | | 65 | | { |
| | 785 | 66 | | Debug.Assert(!(Ssse3.IsSupported || PackedSimd.IsSupported) || !_inverseAsciiState.Lookup.Contains(0 |
| | 785 | 67 | | "The inverse bitmap contained a 0, but we're not using Ssse3AndWasmHandleZeroInNeedle."); |
| | | 68 | | |
| | 785 | 69 | | offset = IndexOfAnyAsciiSearcher.IndexOfAny<IndexOfAnyAsciiSearcher.Negate, IndexOfAnyAsciiSearcher. |
| | 785 | 70 | | ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), |
| | 785 | 71 | | span.Length, |
| | 785 | 72 | | ref _inverseAsciiState); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | // If we've reached the end of the span or stopped at an ASCII character, we've found the result. |
| | 1056 | 76 | | if ((uint)offset >= (uint)span.Length || char.IsAscii(span[offset])) |
| | | 77 | | { |
| | 867 | 78 | | return offset; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | // Fall back to using the ProbabilisticMap. |
| | 189 | 82 | | span = span.Slice(offset); |
| | | 83 | | } |
| | | 84 | | |
| | 4713 | 85 | | int index = ProbabilisticMap.IndexOfAny<SearchValues.TrueConst>( |
| | 4713 | 86 | | ref MemoryMarshal.GetReference(span), |
| | 4713 | 87 | | span.Length, |
| | 4713 | 88 | | ref _map); |
| | | 89 | | |
| | 4713 | 90 | | if (index >= 0) |
| | | 91 | | { |
| | | 92 | | // We found a match. Account for the number of ASCII characters we've skipped previously. |
| | 2280 | 93 | | index += offset; |
| | | 94 | | } |
| | | 95 | | |
| | 4713 | 96 | | return index; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) |
| | | 100 | | { |
| | 5580 | 101 | | int offset = 0; |
| | | 102 | | |
| | | 103 | | // We check whether the first character is ASCII before calling into IndexOfAnyAsciiSearcher |
| | | 104 | | // in order to minimize the overhead this fast-path has on non-ASCII texts. |
| | 5580 | 105 | | if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count && char.IsAsci |
| | | 106 | | { |
| | | 107 | | // Do a regular IndexOfAnyExcept for the ASCII characters. The search will stop if we encounter a non-AS |
| | 1056 | 108 | | offset = IndexOfAnyAsciiSearcher.IndexOfAny<IndexOfAnyAsciiSearcher.Negate, TOptimizations, SearchValues |
| | 1056 | 109 | | ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), |
| | 1056 | 110 | | span.Length, |
| | 1056 | 111 | | ref _asciiState); |
| | | 112 | | |
| | | 113 | | // If we've reached the end of the span or stopped at an ASCII character, we've found the result. |
| | 1056 | 114 | | if ((uint)offset >= (uint)span.Length || char.IsAscii(span[offset])) |
| | | 115 | | { |
| | 477 | 116 | | return offset; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | // Fall back to a simple char-by-char search. |
| | 579 | 120 | | span = span.Slice(offset); |
| | | 121 | | } |
| | | 122 | | |
| | 5103 | 123 | | int index = ProbabilisticMapState.IndexOfAnySimpleLoop<SearchValues.TrueConst, IndexOfAnyAsciiSearcher.Negat |
| | 5103 | 124 | | ref MemoryMarshal.GetReference(span), |
| | 5103 | 125 | | span.Length, |
| | 5103 | 126 | | ref _map); |
| | | 127 | | |
| | 5103 | 128 | | if (index >= 0) |
| | | 129 | | { |
| | | 130 | | // We found a match. Account for the number of ASCII characters we've skipped previously. |
| | 2346 | 131 | | index += offset; |
| | | 132 | | } |
| | | 133 | | |
| | 5103 | 134 | | return index; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | internal override int LastIndexOfAny(ReadOnlySpan<char> span) |
| | | 138 | | { |
| | | 139 | | // We check whether the last character is ASCII before calling into IndexOfAnyAsciiSearcher |
| | | 140 | | // in order to minimize the overhead this fast-path has on non-ASCII texts. |
| | 2790 | 141 | | if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count && char.IsAsci |
| | | 142 | | { |
| | | 143 | | // We are using IndexOfAnyAsciiSearcher to search for the last ASCII character in the set, or any non-AS |
| | | 144 | | // We do this by inverting the bitmap and using the opposite search function (Negate instead of DontNega |
| | | 145 | | |
| | | 146 | | // If the bitmap we're using contains a 0, we have to use 'Ssse3AndWasmHandleZeroInNeedle' when running |
| | | 147 | | // Everything else should use 'Default'. 'TOptimizations' specifies whether '_asciiState' contains a 0. |
| | | 148 | | // Since we're using the inverse bitmap in this case, we have to use 'Ssse3AndWasmHandleZeroInNeedle' if |
| | | 149 | | // running on X86/WASM and 'TOptimizations' is 'Default' (as that means that the inverse bitmap definite |
| | 398 | 150 | | Debug.Assert(_asciiState.Lookup.Contains(0) != _inverseAsciiState.Lookup.Contains(0)); |
| | | 151 | | |
| | | 152 | | int offset; |
| | | 153 | | |
| | 398 | 154 | | if ((Ssse3.IsSupported || PackedSimd.IsSupported) && typeof(TOptimizations) == typeof(IndexOfAnyAsciiSea |
| | | 155 | | { |
| | 144 | 156 | | Debug.Assert(_inverseAsciiState.Lookup.Contains(0), "The inverse bitmap did not contain a 0."); |
| | | 157 | | |
| | 144 | 158 | | offset = IndexOfAnyAsciiSearcher.LastIndexOfAny<IndexOfAnyAsciiSearcher.Negate, IndexOfAnyAsciiSearc |
| | 144 | 159 | | ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), |
| | 144 | 160 | | span.Length, |
| | 144 | 161 | | ref _inverseAsciiState); |
| | | 162 | | } |
| | | 163 | | else |
| | | 164 | | { |
| | 254 | 165 | | Debug.Assert(!(Ssse3.IsSupported || PackedSimd.IsSupported) || !_inverseAsciiState.Lookup.Contains(0 |
| | 254 | 166 | | "The inverse bitmap contained a 0, but we're not using Ssse3AndWasmHandleZeroInNeedle."); |
| | | 167 | | |
| | 254 | 168 | | offset = IndexOfAnyAsciiSearcher.LastIndexOfAny<IndexOfAnyAsciiSearcher.Negate, IndexOfAnyAsciiSearc |
| | 254 | 169 | | ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), |
| | 254 | 170 | | span.Length, |
| | 254 | 171 | | ref _inverseAsciiState); |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | // If we've reached the end of the span or stopped at an ASCII character, we've found the result. |
| | 398 | 175 | | if ((uint)offset >= (uint)span.Length || char.IsAscii(span[offset])) |
| | | 176 | | { |
| | 264 | 177 | | return offset; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | // Fall back to using the ProbabilisticMap. |
| | 134 | 181 | | span = span.Slice(0, offset + 1); |
| | | 182 | | } |
| | | 183 | | |
| | 2526 | 184 | | return ProbabilisticMap.LastIndexOfAny<SearchValues.TrueConst>( |
| | 2526 | 185 | | ref MemoryMarshal.GetReference(span), |
| | 2526 | 186 | | span.Length, |
| | 2526 | 187 | | ref _map); |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) |
| | | 191 | | { |
| | | 192 | | // We check whether the last character is ASCII before calling into IndexOfAnyAsciiSearcher |
| | | 193 | | // in order to minimize the overhead this fast-path has on non-ASCII texts. |
| | 2790 | 194 | | if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count && char.IsAsci |
| | | 195 | | { |
| | | 196 | | // Do a regular LastIndexOfAnyExcept for the ASCII characters. The search will stop if we encounter a no |
| | 398 | 197 | | int offset = IndexOfAnyAsciiSearcher.LastIndexOfAny<IndexOfAnyAsciiSearcher.Negate, TOptimizations, Sear |
| | 398 | 198 | | ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), |
| | 398 | 199 | | span.Length, |
| | 398 | 200 | | ref _asciiState); |
| | | 201 | | |
| | | 202 | | // If we've reached the end of the span or stopped at an ASCII character, we've found the result. |
| | 398 | 203 | | if ((uint)offset >= (uint)span.Length || char.IsAscii(span[offset])) |
| | | 204 | | { |
| | 298 | 205 | | return offset; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | // Fall back to a simple char-by-char search. |
| | 100 | 209 | | span = span.Slice(0, offset + 1); |
| | | 210 | | } |
| | | 211 | | |
| | 2492 | 212 | | return ProbabilisticMapState.LastIndexOfAnySimpleLoop<SearchValues.TrueConst, IndexOfAnyAsciiSearcher.Negate |
| | 2492 | 213 | | ref MemoryMarshal.GetReference(span), |
| | 2492 | 214 | | span.Length, |
| | 2492 | 215 | | ref _map); |
| | | 216 | | } |
| | | 217 | | } |
| | | 218 | | } |
| | | 219 | | |