Skip to content

Commit c6af788

Browse files
committed
scalar ASCII function itself (untested as of now)
1 parent 362cba1 commit c6af788

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/Ascii.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,49 @@ public static unsafe bool SIMDIsAscii(this ReadOnlySpan<char> s)
131131
}
132132
return true;
133133
}
134+
135+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
136+
internal static unsafe nuint GetIndexOfFirstNonAsciiByte(byte* pBuffer, nuint bufferLength)
137+
{
138+
byte* pBufferEnd = pBuffer + bufferLength;
139+
byte* pCurrent = pBuffer;
140+
141+
// Process in blocks of 16 bytes when possible
142+
while (pCurrent + 16 <= pBufferEnd)
143+
{
144+
ulong v1 = *(ulong*)pCurrent;
145+
ulong v2 = *(ulong*)(pCurrent + 8);
146+
ulong v = v1 | v2;
147+
148+
if ((v & 0x8080808080808080) != 0)
149+
{
150+
for (; pCurrent < pBufferEnd; pCurrent++)
151+
{
152+
if (*pCurrent >= 0b10000000)
153+
{
154+
return (nuint)(pCurrent - pBuffer);
155+
}
156+
}
157+
}
158+
159+
pCurrent += 16;
160+
}
161+
162+
// Process the tail byte-by-byte
163+
for (; pCurrent < pBufferEnd; pCurrent++)
164+
{
165+
if (*pCurrent >= 0b10000000)
166+
{
167+
return (nuint)(pCurrent - pBuffer);
168+
}
169+
}
170+
171+
return bufferLength;
172+
}
173+
134174
}
175+
176+
135177
}
136178
// Further reading:
137179
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Encodings.Web/src/System/Text/Unicode/UnicodeHelpers.cs

0 commit comments

Comments
 (0)