|
3 | 3 |
|
4 | 4 | using System.Runtime.CompilerServices; |
5 | 5 | using System.Runtime.InteropServices; |
6 | | -using System.Runtime.Intrinsics; |
| 6 | +using System.Runtime.Intrinsics.Arm; |
| 7 | +using System.Runtime.Intrinsics.Wasm; |
| 8 | +using System.Runtime.Intrinsics.X86; |
7 | 9 |
|
8 | 10 | namespace System.Buffers |
9 | 11 | { |
10 | 12 | internal sealed class AsciiCharSearchValues<TOptimizations> : SearchValues<char> |
11 | 13 | where TOptimizations : struct, IndexOfAnyAsciiSearcher.IOptimizations |
12 | 14 | { |
13 | | - private Vector256<byte> _bitmap; |
14 | | - private readonly BitVector256 _lookup; |
| 15 | + private IndexOfAnyAsciiSearcher.AsciiState _state; |
15 | 16 |
|
16 | | - public AsciiCharSearchValues(Vector256<byte> bitmap, BitVector256 lookup) |
17 | | - { |
18 | | - _bitmap = bitmap; |
19 | | - _lookup = lookup; |
20 | | - } |
| 17 | + public AsciiCharSearchValues(ReadOnlySpan<char> values) => |
| 18 | + IndexOfAnyAsciiSearcher.ComputeAsciiState(values, out _state); |
21 | 19 |
|
22 | | - internal override char[] GetValues() => _lookup.GetCharValues(); |
| 20 | + internal override char[] GetValues() => |
| 21 | + _state.Lookup.GetCharValues(); |
23 | 22 |
|
24 | 23 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
25 | 24 | internal override bool ContainsCore(char value) => |
26 | | - _lookup.Contains128(value); |
| 25 | + _state.Lookup.Contains128(value); |
27 | 26 |
|
| 27 | + [CompExactlyDependsOn(typeof(Ssse3))] |
| 28 | + [CompExactlyDependsOn(typeof(AdvSimd))] |
| 29 | + [CompExactlyDependsOn(typeof(PackedSimd))] |
28 | 30 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
29 | 31 | internal override int IndexOfAny(ReadOnlySpan<char> span) => |
30 | | - IndexOfAny<IndexOfAnyAsciiSearcher.DontNegate>(ref MemoryMarshal.GetReference(span), span.Length); |
| 32 | + IndexOfAnyAsciiSearcher.IndexOfAnyVectorized<IndexOfAnyAsciiSearcher.DontNegate, TOptimizations>( |
| 33 | + ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), span.Length, ref _state); |
31 | 34 |
|
| 35 | + [CompExactlyDependsOn(typeof(Ssse3))] |
| 36 | + [CompExactlyDependsOn(typeof(AdvSimd))] |
| 37 | + [CompExactlyDependsOn(typeof(PackedSimd))] |
32 | 38 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
33 | 39 | internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => |
34 | | - IndexOfAny<IndexOfAnyAsciiSearcher.Negate>(ref MemoryMarshal.GetReference(span), span.Length); |
| 40 | + IndexOfAnyAsciiSearcher.IndexOfAnyVectorized<IndexOfAnyAsciiSearcher.Negate, TOptimizations>( |
| 41 | + ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), span.Length, ref _state); |
35 | 42 |
|
| 43 | + [CompExactlyDependsOn(typeof(Ssse3))] |
| 44 | + [CompExactlyDependsOn(typeof(AdvSimd))] |
| 45 | + [CompExactlyDependsOn(typeof(PackedSimd))] |
36 | 46 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
37 | 47 | internal override int LastIndexOfAny(ReadOnlySpan<char> span) => |
38 | | - LastIndexOfAny<IndexOfAnyAsciiSearcher.DontNegate>(ref MemoryMarshal.GetReference(span), span.Length); |
| 48 | + IndexOfAnyAsciiSearcher.LastIndexOfAnyVectorized<IndexOfAnyAsciiSearcher.DontNegate, TOptimizations>( |
| 49 | + ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), span.Length, ref _state); |
39 | 50 |
|
| 51 | + [CompExactlyDependsOn(typeof(Ssse3))] |
| 52 | + [CompExactlyDependsOn(typeof(AdvSimd))] |
| 53 | + [CompExactlyDependsOn(typeof(PackedSimd))] |
40 | 54 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
41 | 55 | internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => |
42 | | - LastIndexOfAny<IndexOfAnyAsciiSearcher.Negate>(ref MemoryMarshal.GetReference(span), span.Length); |
43 | | - |
44 | | - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
45 | | - private int IndexOfAny<TNegator>(ref char searchSpace, int searchSpaceLength) |
46 | | - where TNegator : struct, IndexOfAnyAsciiSearcher.INegator |
47 | | - { |
48 | | - return IndexOfAnyAsciiSearcher.IsVectorizationSupported && searchSpaceLength >= Vector128<short>.Count |
49 | | - ? IndexOfAnyAsciiSearcher.IndexOfAnyVectorized<TNegator, TOptimizations>(ref Unsafe.As<char, short>(ref searchSpace), searchSpaceLength, ref _bitmap) |
50 | | - : IndexOfAnyScalar<TNegator>(ref searchSpace, searchSpaceLength); |
51 | | - } |
52 | | - |
53 | | - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
54 | | - private int LastIndexOfAny<TNegator>(ref char searchSpace, int searchSpaceLength) |
55 | | - where TNegator : struct, IndexOfAnyAsciiSearcher.INegator |
56 | | - { |
57 | | - return IndexOfAnyAsciiSearcher.IsVectorizationSupported && searchSpaceLength >= Vector128<short>.Count |
58 | | - ? IndexOfAnyAsciiSearcher.LastIndexOfAnyVectorized<TNegator, TOptimizations>(ref Unsafe.As<char, short>(ref searchSpace), searchSpaceLength, ref _bitmap) |
59 | | - : LastIndexOfAnyScalar<TNegator>(ref searchSpace, searchSpaceLength); |
60 | | - } |
61 | | - |
62 | | - private int IndexOfAnyScalar<TNegator>(ref char searchSpace, int searchSpaceLength) |
63 | | - where TNegator : struct, IndexOfAnyAsciiSearcher.INegator |
64 | | - { |
65 | | - ref char searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength); |
66 | | - ref char cur = ref searchSpace; |
67 | | - |
68 | | - while (!Unsafe.AreSame(ref cur, ref searchSpaceEnd)) |
69 | | - { |
70 | | - char c = cur; |
71 | | - if (TNegator.NegateIfNeeded(_lookup.Contains128(c))) |
72 | | - { |
73 | | - return (int)((nuint)Unsafe.ByteOffset(ref searchSpace, ref cur) / sizeof(char)); |
74 | | - } |
75 | | - |
76 | | - cur = ref Unsafe.Add(ref cur, 1); |
77 | | - } |
78 | | - |
79 | | - return -1; |
80 | | - } |
81 | | - |
82 | | - private int LastIndexOfAnyScalar<TNegator>(ref char searchSpace, int searchSpaceLength) |
83 | | - where TNegator : struct, IndexOfAnyAsciiSearcher.INegator |
84 | | - { |
85 | | - for (int i = searchSpaceLength - 1; i >= 0; i--) |
86 | | - { |
87 | | - char c = Unsafe.Add(ref searchSpace, i); |
88 | | - if (TNegator.NegateIfNeeded(_lookup.Contains128(c))) |
89 | | - { |
90 | | - return i; |
91 | | - } |
92 | | - } |
93 | | - |
94 | | - return -1; |
95 | | - } |
| 56 | + IndexOfAnyAsciiSearcher.LastIndexOfAnyVectorized<IndexOfAnyAsciiSearcher.Negate, TOptimizations>( |
| 57 | + ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), span.Length, ref _state); |
96 | 58 | } |
97 | 59 | } |
0 commit comments