forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ascii.cs
230 lines (196 loc) · 11.3 KB
/
Ascii.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
namespace System.Text
{
public static partial class Ascii
{
/// <summary>
/// Determines whether the provided value contains only ASCII bytes.
/// </summary>
/// <param name="value">The value to inspect.</param>
/// <returns>True if <paramref name="value"/> contains only ASCII bytes or is
/// empty; False otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsValid(ReadOnlySpan<byte> value) =>
IsValidCore(ref MemoryMarshal.GetReference(value), value.Length);
/// <summary>
/// Determines whether the provided value contains only ASCII chars.
/// </summary>
/// <param name="value">The value to inspect.</param>
/// <returns>True if <paramref name="value"/> contains only ASCII chars or is
/// empty; False otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsValid(ReadOnlySpan<char> value) =>
IsValidCore(ref Unsafe.As<char, ushort>(ref MemoryMarshal.GetReference(value)), value.Length);
/// <summary>
/// Determines whether the provided value is ASCII byte.
/// </summary>
/// <param name="value">The value to inspect.</param>
/// <returns>True if <paramref name="value"/> is ASCII, False otherwise.</returns>
public static bool IsValid(byte value) => value <= 127;
/// <summary>
/// Determines whether the provided value is ASCII char.
/// </summary>
/// <param name="value">The value to inspect.</param>
/// <returns>True if <paramref name="value"/> is ASCII, False otherwise.</returns>
public static bool IsValid(char value) => value <= 127;
private static unsafe bool IsValidCore<T>(ref T searchSpace, int length) where T : unmanaged
{
Debug.Assert(typeof(T) == typeof(byte) || typeof(T) == typeof(ushort));
ref T searchSpaceEnd = ref Unsafe.Add(ref searchSpace, length);
if (!Vector128.IsHardwareAccelerated || length < Vector128<T>.Count)
{
int elementsPerUlong = sizeof(ulong) / sizeof(T);
if (length < elementsPerUlong)
{
if (typeof(T) == typeof(byte) && length >= sizeof(uint))
{
// Process byte inputs with lengths [4, 7]
return AllBytesInUInt32AreAscii(
Unsafe.ReadUnaligned<uint>(ref Unsafe.As<T, byte>(ref searchSpace)) |
Unsafe.ReadUnaligned<uint>(ref Unsafe.As<T, byte>(ref Unsafe.Subtract(ref searchSpaceEnd, sizeof(uint)))));
}
// Process inputs with lengths [0, 3]
while (Unsafe.IsAddressLessThan(ref searchSpace, ref searchSpaceEnd))
{
if (typeof(T) == typeof(byte)
? (Unsafe.BitCast<T, byte>(searchSpace) > 127)
: (Unsafe.BitCast<T, char>(searchSpace) > 127))
{
return false;
}
searchSpace = ref Unsafe.Add(ref searchSpace, 1);
}
return true;
}
// If vectorization isn't supported, process 16 bytes at a time.
if (!Vector128.IsHardwareAccelerated && length > 2 * elementsPerUlong)
{
ref T finalStart = ref Unsafe.Subtract(ref searchSpaceEnd, 2 * elementsPerUlong);
do
{
if (!AllCharsInUInt64AreAscii<T>(
Unsafe.ReadUnaligned<ulong>(ref Unsafe.As<T, byte>(ref searchSpace)) |
Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref Unsafe.As<T, byte>(ref searchSpace), sizeof(ulong)))))
{
return false;
}
searchSpace = ref Unsafe.Add(ref searchSpace, 2 * elementsPerUlong);
}
while (Unsafe.IsAddressLessThan(ref searchSpace, ref finalStart));
searchSpace = ref finalStart;
}
// Process the last [8, 16] bytes.
return AllCharsInUInt64AreAscii<T>(
Unsafe.ReadUnaligned<ulong>(ref Unsafe.As<T, byte>(ref searchSpace)) |
Unsafe.ReadUnaligned<ulong>(ref Unsafe.Subtract(ref Unsafe.As<T, byte>(ref searchSpaceEnd), sizeof(ulong))));
}
// Process inputs with lengths [16, 32] bytes.
if (length <= 2 * Vector128<T>.Count)
{
return AllCharsInVectorAreAscii(
Vector128.LoadUnsafe(ref searchSpace) |
Vector128.LoadUnsafe(ref Unsafe.Subtract(ref searchSpaceEnd, (nuint)Vector128<T>.Count)));
}
if (Vector256.IsHardwareAccelerated)
{
// Process inputs with lengths [33, 64] bytes.
if (length <= 2 * Vector256<T>.Count)
{
return AllCharsInVectorAreAscii(
Vector256.LoadUnsafe(ref searchSpace) |
Vector256.LoadUnsafe(ref Unsafe.Subtract(ref searchSpaceEnd, (nuint)Vector256<T>.Count)));
}
// Process long inputs 128 bytes at a time.
if (length > 4 * Vector256<T>.Count)
{
// Process the first 128 bytes.
if (!AllCharsInVectorAreAscii(
Vector256.LoadUnsafe(ref searchSpace) |
Vector256.LoadUnsafe(ref searchSpace, (nuint)Vector256<T>.Count) |
Vector256.LoadUnsafe(ref searchSpace, 2 * (nuint)Vector256<T>.Count) |
Vector256.LoadUnsafe(ref searchSpace, 3 * (nuint)Vector256<T>.Count)))
{
return false;
}
searchSpace = ref Unsafe.Add(ref searchSpace, 4 * Vector256<T>.Count);
// Try to opportunistically align the reads below. The input isn't pinned, so the GC
// is free to move the references. We're therefore assuming that reads may still be unaligned.
// They may also be unaligned if the input chars aren't 2-byte aligned.
nuint misalignedElements = ((nuint)Unsafe.AsPointer(ref searchSpace) & (nuint)(Vector256<byte>.Count - 1)) / (nuint)sizeof(T);
searchSpace = ref Unsafe.Subtract(ref searchSpace, misalignedElements);
ref T finalStart = ref Unsafe.Subtract(ref searchSpaceEnd, 4 * Vector256<T>.Count);
while (Unsafe.IsAddressLessThan(ref searchSpace, ref finalStart))
{
if (!AllCharsInVectorAreAscii(
Vector256.LoadUnsafe(ref searchSpace) |
Vector256.LoadUnsafe(ref searchSpace, (nuint)Vector256<T>.Count) |
Vector256.LoadUnsafe(ref searchSpace, 2 * (nuint)Vector256<T>.Count) |
Vector256.LoadUnsafe(ref searchSpace, 3 * (nuint)Vector256<T>.Count)))
{
return false;
}
searchSpace = ref Unsafe.Add(ref searchSpace, 4 * Vector256<T>.Count);
}
searchSpace = ref finalStart;
}
// Process the last [1, 128] bytes.
// The search space has at least 2 * Vector256 bytes available to read.
// We process the first 2 and last 2 vectors, which may overlap.
return AllCharsInVectorAreAscii(
Vector256.LoadUnsafe(ref searchSpace) |
Vector256.LoadUnsafe(ref searchSpace, (nuint)Vector256<T>.Count) |
Vector256.LoadUnsafe(ref Unsafe.Subtract(ref searchSpaceEnd, 2 * (nuint)Vector256<T>.Count)) |
Vector256.LoadUnsafe(ref Unsafe.Subtract(ref searchSpaceEnd, (nuint)Vector256<T>.Count)));
}
else
{
// Process long inputs 64 bytes at a time.
if (length > 4 * Vector128<T>.Count)
{
// Process the first 64 bytes.
if (!AllCharsInVectorAreAscii(
Vector128.LoadUnsafe(ref searchSpace) |
Vector128.LoadUnsafe(ref searchSpace, (nuint)Vector128<T>.Count) |
Vector128.LoadUnsafe(ref searchSpace, 2 * (nuint)Vector128<T>.Count) |
Vector128.LoadUnsafe(ref searchSpace, 3 * (nuint)Vector128<T>.Count)))
{
return false;
}
searchSpace = ref Unsafe.Add(ref searchSpace, 4 * Vector128<T>.Count);
// Try to opportunistically align the reads below. The input isn't pinned, so the GC
// is free to move the references. We're therefore assuming that reads may still be unaligned.
// They may also be unaligned if the input chars aren't 2-byte aligned.
nuint misalignedElements = ((nuint)Unsafe.AsPointer(ref searchSpace) & (nuint)(Vector128<byte>.Count - 1)) / (nuint)sizeof(T);
searchSpace = ref Unsafe.Subtract(ref searchSpace, misalignedElements);
ref T finalStart = ref Unsafe.Subtract(ref searchSpaceEnd, 4 * Vector128<T>.Count);
while (Unsafe.IsAddressLessThan(ref searchSpace, ref finalStart))
{
if (!AllCharsInVectorAreAscii(
Vector128.LoadUnsafe(ref searchSpace) |
Vector128.LoadUnsafe(ref searchSpace, (nuint)Vector128<T>.Count) |
Vector128.LoadUnsafe(ref searchSpace, 2 * (nuint)Vector128<T>.Count) |
Vector128.LoadUnsafe(ref searchSpace, 3 * (nuint)Vector128<T>.Count)))
{
return false;
}
searchSpace = ref Unsafe.Add(ref searchSpace, 4 * Vector128<T>.Count);
}
searchSpace = ref finalStart;
}
// Process the last [1, 64] bytes.
// The search space has at least 2 * Vector128 bytes available to read.
// We process the first 2 and last 2 vectors, which may overlap.
return AllCharsInVectorAreAscii(
Vector128.LoadUnsafe(ref searchSpace) |
Vector128.LoadUnsafe(ref searchSpace, (nuint)Vector128<T>.Count) |
Vector128.LoadUnsafe(ref Unsafe.Subtract(ref searchSpaceEnd, 2 * (nuint)Vector128<T>.Count)) |
Vector128.LoadUnsafe(ref Unsafe.Subtract(ref searchSpaceEnd, (nuint)Vector128<T>.Count)));
}
}
}
}