| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- using System.Diagnostics;
- using System.Numerics;
- using System.Runtime.CompilerServices;
- using System.Runtime.Intrinsics;
- using System.Runtime.Intrinsics.X86;
- using Internal.Runtime.CompilerServices;
- #if BIT64
- using nuint = System.UInt64;
- #else
- using nuint = System.UInt32;
- #endif // BIT64
- namespace System
- {
- internal static partial class SpanHelpers // .Byte
- {
- public static int IndexOf(ref byte searchSpace, int searchSpaceLength, ref byte value, int valueLength)
- {
- Debug.Assert(searchSpaceLength >= 0);
- Debug.Assert(valueLength >= 0);
- if (valueLength == 0)
- return 0; // A zero-length sequence is always treated as "found" at the start of the search space.
- byte valueHead = value;
- ref byte valueTail = ref Unsafe.Add(ref value, 1);
- int valueTailLength = valueLength - 1;
- int remainingSearchSpaceLength = searchSpaceLength - valueTailLength;
- int offset = 0;
- while (remainingSearchSpaceLength > 0)
- {
- // Do a quick search for the first element of "value".
- int relativeIndex = IndexOf(ref Unsafe.Add(ref searchSpace, offset), valueHead, remainingSearchSpaceLength);
- if (relativeIndex == -1)
- break;
- remainingSearchSpaceLength -= relativeIndex;
- offset += relativeIndex;
- if (remainingSearchSpaceLength <= 0)
- break; // The unsearched portion is now shorter than the sequence we're looking for. So it can't be there.
- // Found the first element of "value". See if the tail matches.
- if (SequenceEqual(ref Unsafe.Add(ref searchSpace, offset + 1), ref valueTail, valueTailLength))
- return offset; // The tail matched. Return a successful find.
- remainingSearchSpaceLength--;
- offset++;
- }
- return -1;
- }
- public static int IndexOfAny(ref byte searchSpace, int searchSpaceLength, ref byte value, int valueLength)
- {
- Debug.Assert(searchSpaceLength >= 0);
- Debug.Assert(valueLength >= 0);
- if (valueLength == 0)
- return -1; // A zero-length set of values is always treated as "not found".
- int offset = -1;
- for (int i = 0; i < valueLength; i++)
- {
- var tempIndex = IndexOf(ref searchSpace, Unsafe.Add(ref value, i), searchSpaceLength);
- if ((uint)tempIndex < (uint)offset)
- {
- offset = tempIndex;
- // Reduce space for search, cause we don't care if we find the search value after the index of a previously found value
- searchSpaceLength = tempIndex;
- if (offset == 0)
- break;
- }
- }
- return offset;
- }
- public static int LastIndexOfAny(ref byte searchSpace, int searchSpaceLength, ref byte value, int valueLength)
- {
- Debug.Assert(searchSpaceLength >= 0);
- Debug.Assert(valueLength >= 0);
- if (valueLength == 0)
- return -1; // A zero-length set of values is always treated as "not found".
- int offset = -1;
- for (int i = 0; i < valueLength; i++)
- {
- var tempIndex = LastIndexOf(ref searchSpace, Unsafe.Add(ref value, i), searchSpaceLength);
- if (tempIndex > offset)
- offset = tempIndex;
- }
- return offset;
- }
- // Adapted from IndexOf(...)
- [MethodImpl(MethodImplOptions.AggressiveOptimization)]
- public static unsafe bool Contains(ref byte searchSpace, byte value, int length)
- {
- Debug.Assert(length >= 0);
- uint uValue = value; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- IntPtr offset = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr lengthToExamine = (IntPtr)length;
- if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
- {
- lengthToExamine = UnalignedCountVector(ref searchSpace);
- }
- SequentialScan:
- while ((byte*)lengthToExamine >= (byte*)8)
- {
- lengthToExamine -= 8;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 0) ||
- uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 1) ||
- uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 2) ||
- uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 3) ||
- uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 4) ||
- uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 5) ||
- uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 6) ||
- uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 7))
- {
- goto Found;
- }
- offset += 8;
- }
- if ((byte*)lengthToExamine >= (byte*)4)
- {
- lengthToExamine -= 4;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 0) ||
- uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 1) ||
- uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 2) ||
- uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 3))
- {
- goto Found;
- }
- offset += 4;
- }
- while ((byte*)lengthToExamine > (byte*)0)
- {
- lengthToExamine -= 1;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset))
- goto Found;
- offset += 1;
- }
- if (Vector.IsHardwareAccelerated && ((int)(byte*)offset < length))
- {
- lengthToExamine = (IntPtr)((length - (int)(byte*)offset) & ~(Vector<byte>.Count - 1));
- Vector<byte> values = new Vector<byte>(value);
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- var matches = Vector.Equals(values, LoadVector(ref searchSpace, offset));
- if (Vector<byte>.Zero.Equals(matches))
- {
- offset += Vector<byte>.Count;
- continue;
- }
- goto Found;
- }
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- return false;
- Found:
- return true;
- }
- [MethodImpl(MethodImplOptions.AggressiveOptimization)]
- public static unsafe int IndexOf(ref byte searchSpace, byte value, int length)
- {
- Debug.Assert(length >= 0);
- uint uValue = value; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- IntPtr offset = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr lengthToExamine = (IntPtr)length;
- if (Avx2.IsSupported || Sse2.IsSupported)
- {
- // Avx2 branch also operates on Sse2 sizes, so check is combined.
- if (length >= Vector128<byte>.Count * 2)
- {
- lengthToExamine = UnalignedCountVector128(ref searchSpace);
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if (length >= Vector<byte>.Count * 2)
- {
- lengthToExamine = UnalignedCountVector(ref searchSpace);
- }
- }
- SequentialScan:
- while ((byte*)lengthToExamine >= (byte*)8)
- {
- lengthToExamine -= 8;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset))
- goto Found;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 1))
- goto Found1;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 2))
- goto Found2;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 3))
- goto Found3;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 4))
- goto Found4;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 5))
- goto Found5;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 6))
- goto Found6;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 7))
- goto Found7;
- offset += 8;
- }
- if ((byte*)lengthToExamine >= (byte*)4)
- {
- lengthToExamine -= 4;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset))
- goto Found;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 1))
- goto Found1;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 2))
- goto Found2;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 3))
- goto Found3;
- offset += 4;
- }
- while ((byte*)lengthToExamine > (byte*)0)
- {
- lengthToExamine -= 1;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset))
- goto Found;
- offset += 1;
- }
- // We get past SequentialScan only if IsHardwareAccelerated or intrinsic .IsSupported is true; and remain length is greater than Vector length.
- // However, we still have the redundant check to allow the JIT to see that the code is unreachable and eliminate it when the platform does not
- // have hardware accelerated. After processing Vector lengths we return to SequentialScan to finish any remaining.
- if (Avx2.IsSupported)
- {
- if ((int)(byte*)offset < length)
- {
- if ((((nuint)Unsafe.AsPointer(ref searchSpace) + (nuint)offset) & (nuint)(Vector256<byte>.Count - 1)) != 0)
- {
- // Not currently aligned to Vector256 (is aligned to Vector128); this can cause a problem for searches
- // with no upper bound e.g. String.strlen.
- // Start with a check on Vector128 to align to Vector256, before moving to processing Vector256.
- // This ensures we do not fault across memory pages while searching for an end of string.
- Vector128<byte> values = Vector128.Create(value);
- Vector128<byte> search = LoadVector128(ref searchSpace, offset);
- // Same method as below
- int matches = Sse2.MoveMask(Sse2.CompareEqual(values, search));
- if (matches == 0)
- {
- // Zero flags set so no matches
- offset += Vector128<byte>.Count;
- }
- else
- {
- // Find bitflag offset of first match and add to current offset
- return ((int)(byte*)offset) + BitOperations.TrailingZeroCount(matches);
- }
- }
- lengthToExamine = GetByteVector256SpanLength(offset, length);
- if ((byte*)lengthToExamine > (byte*)offset)
- {
- Vector256<byte> values = Vector256.Create(value);
- do
- {
- Vector256<byte> search = LoadVector256(ref searchSpace, offset);
- int matches = Avx2.MoveMask(Avx2.CompareEqual(values, search));
- // Note that MoveMask has converted the equal vector elements into a set of bit flags,
- // So the bit position in 'matches' corresponds to the element offset.
- if (matches == 0)
- {
- // Zero flags set so no matches
- offset += Vector256<byte>.Count;
- continue;
- }
- // Find bitflag offset of first match and add to current offset
- return ((int)(byte*)offset) + BitOperations.TrailingZeroCount(matches);
- } while ((byte*)lengthToExamine > (byte*)offset);
- }
- lengthToExamine = GetByteVector128SpanLength(offset, length);
- if ((byte*)lengthToExamine > (byte*)offset)
- {
- Vector128<byte> values = Vector128.Create(value);
- Vector128<byte> search = LoadVector128(ref searchSpace, offset);
- // Same method as above
- int matches = Sse2.MoveMask(Sse2.CompareEqual(values, search));
- if (matches == 0)
- {
- // Zero flags set so no matches
- offset += Vector128<byte>.Count;
- }
- else
- {
- // Find bitflag offset of first match and add to current offset
- return ((int)(byte*)offset) + BitOperations.TrailingZeroCount(matches);
- }
- }
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Sse2.IsSupported)
- {
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = GetByteVector128SpanLength(offset, length);
- Vector128<byte> values = Vector128.Create(value);
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- Vector128<byte> search = LoadVector128(ref searchSpace, offset);
- // Same method as above
- int matches = Sse2.MoveMask(Sse2.CompareEqual(values, search));
- if (matches == 0)
- {
- // Zero flags set so no matches
- offset += Vector128<byte>.Count;
- continue;
- }
- // Find bitflag offset of first match and add to current offset
- return ((int)(byte*)offset) + BitOperations.TrailingZeroCount(matches);
- }
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = GetByteVectorSpanLength(offset, length);
- Vector<byte> values = new Vector<byte>(value);
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- var matches = Vector.Equals(values, LoadVector(ref searchSpace, offset));
- if (Vector<byte>.Zero.Equals(matches))
- {
- offset += Vector<byte>.Count;
- continue;
- }
- // Find offset of first match and add to current offset
- return (int)(byte*)offset + LocateFirstFoundByte(matches);
- }
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- return -1;
- Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
- return (int)(byte*)offset;
- Found1:
- return (int)(byte*)(offset + 1);
- Found2:
- return (int)(byte*)(offset + 2);
- Found3:
- return (int)(byte*)(offset + 3);
- Found4:
- return (int)(byte*)(offset + 4);
- Found5:
- return (int)(byte*)(offset + 5);
- Found6:
- return (int)(byte*)(offset + 6);
- Found7:
- return (int)(byte*)(offset + 7);
- }
- public static int LastIndexOf(ref byte searchSpace, int searchSpaceLength, ref byte value, int valueLength)
- {
- Debug.Assert(searchSpaceLength >= 0);
- Debug.Assert(valueLength >= 0);
- if (valueLength == 0)
- return 0; // A zero-length sequence is always treated as "found" at the start of the search space.
- byte valueHead = value;
- ref byte valueTail = ref Unsafe.Add(ref value, 1);
- int valueTailLength = valueLength - 1;
- int offset = 0;
- for (; ; )
- {
- Debug.Assert(0 <= offset && offset <= searchSpaceLength); // Ensures no deceptive underflows in the computation of "remainingSearchSpaceLength".
- int remainingSearchSpaceLength = searchSpaceLength - offset - valueTailLength;
- if (remainingSearchSpaceLength <= 0)
- break; // The unsearched portion is now shorter than the sequence we're looking for. So it can't be there.
- // Do a quick search for the first element of "value".
- int relativeIndex = LastIndexOf(ref searchSpace, valueHead, remainingSearchSpaceLength);
- if (relativeIndex == -1)
- break;
- // Found the first element of "value". See if the tail matches.
- if (SequenceEqual(ref Unsafe.Add(ref searchSpace, relativeIndex + 1), ref valueTail, valueTailLength))
- return relativeIndex; // The tail matched. Return a successful find.
- offset += remainingSearchSpaceLength - relativeIndex;
- }
- return -1;
- }
- [MethodImpl(MethodImplOptions.AggressiveOptimization)]
- public static unsafe int LastIndexOf(ref byte searchSpace, byte value, int length)
- {
- Debug.Assert(length >= 0);
- uint uValue = value; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- IntPtr offset = (IntPtr)length; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr lengthToExamine = (IntPtr)length;
- if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
- {
- lengthToExamine = UnalignedCountVectorFromEnd(ref searchSpace, length);
- }
- SequentialScan:
- while ((byte*)lengthToExamine >= (byte*)8)
- {
- lengthToExamine -= 8;
- offset -= 8;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 7))
- goto Found7;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 6))
- goto Found6;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 5))
- goto Found5;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 4))
- goto Found4;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 3))
- goto Found3;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 2))
- goto Found2;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 1))
- goto Found1;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset))
- goto Found;
- }
- if ((byte*)lengthToExamine >= (byte*)4)
- {
- lengthToExamine -= 4;
- offset -= 4;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 3))
- goto Found3;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 2))
- goto Found2;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 1))
- goto Found1;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset))
- goto Found;
- }
- while ((byte*)lengthToExamine > (byte*)0)
- {
- lengthToExamine -= 1;
- offset -= 1;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset))
- goto Found;
- }
- if (Vector.IsHardwareAccelerated && ((byte*)offset > (byte*)0))
- {
- lengthToExamine = (IntPtr)((int)(byte*)offset & ~(Vector<byte>.Count - 1));
- Vector<byte> values = new Vector<byte>(value);
- while ((byte*)lengthToExamine > (byte*)(Vector<byte>.Count - 1))
- {
- var matches = Vector.Equals(values, LoadVector(ref searchSpace, offset - Vector<byte>.Count));
- if (Vector<byte>.Zero.Equals(matches))
- {
- offset -= Vector<byte>.Count;
- lengthToExamine -= Vector<byte>.Count;
- continue;
- }
- // Find offset of first match and add to current offset
- return (int)(offset) - Vector<byte>.Count + LocateLastFoundByte(matches);
- }
- if ((byte*)offset > (byte*)0)
- {
- lengthToExamine = offset;
- goto SequentialScan;
- }
- }
- return -1;
- Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
- return (int)(byte*)offset;
- Found1:
- return (int)(byte*)(offset + 1);
- Found2:
- return (int)(byte*)(offset + 2);
- Found3:
- return (int)(byte*)(offset + 3);
- Found4:
- return (int)(byte*)(offset + 4);
- Found5:
- return (int)(byte*)(offset + 5);
- Found6:
- return (int)(byte*)(offset + 6);
- Found7:
- return (int)(byte*)(offset + 7);
- }
- [MethodImpl(MethodImplOptions.AggressiveOptimization)]
- public static unsafe int IndexOfAny(ref byte searchSpace, byte value0, byte value1, int length)
- {
- Debug.Assert(length >= 0);
- uint uValue0 = value0; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- uint uValue1 = value1; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- IntPtr offset = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr lengthToExamine = (IntPtr)length;
- if (Avx2.IsSupported || Sse2.IsSupported)
- {
- // Avx2 branch also operates on Sse2 sizes, so check is combined.
- if (length >= Vector128<byte>.Count * 2)
- {
- lengthToExamine = UnalignedCountVector128(ref searchSpace);
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if (length >= Vector<byte>.Count * 2)
- {
- lengthToExamine = UnalignedCountVector(ref searchSpace);
- }
- }
- SequentialScan:
- uint lookUp;
- while ((byte*)lengthToExamine >= (byte*)8)
- {
- lengthToExamine -= 8;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 1);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 2);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found2;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 3);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found3;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 4);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found4;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 5);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found5;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 6);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found6;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 7);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found7;
- offset += 8;
- }
- if ((byte*)lengthToExamine >= (byte*)4)
- {
- lengthToExamine -= 4;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 1);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 2);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found2;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 3);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found3;
- offset += 4;
- }
- while ((byte*)lengthToExamine > (byte*)0)
- {
- lengthToExamine -= 1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found;
- offset += 1;
- }
- // We get past SequentialScan only if IsHardwareAccelerated or intrinsic .IsSupported is true. However, we still have the redundant check to allow
- // the JIT to see that the code is unreachable and eliminate it when the platform does not have hardware accelerated.
- if (Avx2.IsSupported)
- {
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = GetByteVector256SpanLength(offset, length);
- if ((byte*)lengthToExamine > (byte*)offset)
- {
- Vector256<byte> values0 = Vector256.Create(value0);
- Vector256<byte> values1 = Vector256.Create(value1);
- do
- {
- Vector256<byte> search = LoadVector256(ref searchSpace, offset);
- // Bitwise Or to combine the matches and MoveMask to convert them to bitflags
- int matches = Avx2.MoveMask(
- Avx2.Or(
- Avx2.CompareEqual(values0, search),
- Avx2.CompareEqual(values1, search)));
- // Note that MoveMask has converted the equal vector elements into a set of bit flags,
- // So the bit position in 'matches' corresponds to the element offset.
- if (matches == 0)
- {
- // Zero flags set so no matches
- offset += Vector256<byte>.Count;
- continue;
- }
- // Find bitflag offset of first match and add to current offset
- return ((int)(byte*)offset) + BitOperations.TrailingZeroCount(matches);
- } while ((byte*)lengthToExamine > (byte*)offset);
- }
- lengthToExamine = GetByteVector128SpanLength(offset, length);
- if ((byte*)lengthToExamine > (byte*)offset)
- {
- Vector128<byte> values0 = Vector128.Create(value0);
- Vector128<byte> values1 = Vector128.Create(value1);
- Vector128<byte> search = LoadVector128(ref searchSpace, offset);
- // Same method as above
- int matches = Sse2.MoveMask(
- Sse2.Or(
- Sse2.CompareEqual(values0, search),
- Sse2.CompareEqual(values1, search)));
- if (matches == 0)
- {
- // Zero flags set so no matches
- offset += Vector128<byte>.Count;
- }
- else
- {
- // Find bitflag offset of first match and add to current offset
- return ((int)(byte*)offset) + BitOperations.TrailingZeroCount(matches);
- }
- }
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Sse2.IsSupported)
- {
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = GetByteVector128SpanLength(offset, length);
- Vector128<byte> values0 = Vector128.Create(value0);
- Vector128<byte> values1 = Vector128.Create(value1);
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- Vector128<byte> search = LoadVector128(ref searchSpace, offset);
- // Same method as above
- int matches = Sse2.MoveMask(
- Sse2.Or(
- Sse2.CompareEqual(values0, search),
- Sse2.CompareEqual(values1, search)));
- if (matches == 0)
- {
- // Zero flags set so no matches
- offset += Vector128<byte>.Count;
- continue;
- }
- // Find bitflag offset of first match and add to current offset
- return ((int)(byte*)offset) + BitOperations.TrailingZeroCount(matches);
- }
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = GetByteVectorSpanLength(offset, length);
- Vector<byte> values0 = new Vector<byte>(value0);
- Vector<byte> values1 = new Vector<byte>(value1);
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- Vector<byte> search = LoadVector(ref searchSpace, offset);
- var matches = Vector.BitwiseOr(
- Vector.Equals(search, values0),
- Vector.Equals(search, values1));
- if (Vector<byte>.Zero.Equals(matches))
- {
- offset += Vector<byte>.Count;
- continue;
- }
- // Find offset of first match and add to current offset
- return (int)(byte*)offset + LocateFirstFoundByte(matches);
- }
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- return -1;
- Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
- return (int)(byte*)offset;
- Found1:
- return (int)(byte*)(offset + 1);
- Found2:
- return (int)(byte*)(offset + 2);
- Found3:
- return (int)(byte*)(offset + 3);
- Found4:
- return (int)(byte*)(offset + 4);
- Found5:
- return (int)(byte*)(offset + 5);
- Found6:
- return (int)(byte*)(offset + 6);
- Found7:
- return (int)(byte*)(offset + 7);
- }
- [MethodImpl(MethodImplOptions.AggressiveOptimization)]
- public static unsafe int IndexOfAny(ref byte searchSpace, byte value0, byte value1, byte value2, int length)
- {
- Debug.Assert(length >= 0);
- uint uValue0 = value0; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- uint uValue1 = value1;
- uint uValue2 = value2;
- IntPtr offset = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr lengthToExamine = (IntPtr)length;
- if (Avx2.IsSupported || Sse2.IsSupported)
- {
- // Avx2 branch also operates on Sse2 sizes, so check is combined.
- if (length >= Vector128<byte>.Count * 2)
- {
- lengthToExamine = UnalignedCountVector128(ref searchSpace);
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if (length >= Vector<byte>.Count * 2)
- {
- lengthToExamine = UnalignedCountVector(ref searchSpace);
- }
- }
- SequentialScan:
- uint lookUp;
- while ((byte*)lengthToExamine >= (byte*)8)
- {
- lengthToExamine -= 8;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 1);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 2);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found2;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 3);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found3;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 4);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found4;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 5);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found5;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 6);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found6;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 7);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found7;
- offset += 8;
- }
- if ((byte*)lengthToExamine >= (byte*)4)
- {
- lengthToExamine -= 4;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 1);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 2);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found2;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 3);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found3;
- offset += 4;
- }
- while ((byte*)lengthToExamine > (byte*)0)
- {
- lengthToExamine -= 1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found;
- offset += 1;
- }
- if (Avx2.IsSupported)
- {
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = GetByteVector256SpanLength(offset, length);
- if ((byte*)lengthToExamine > (byte*)offset)
- {
- Vector256<byte> values0 = Vector256.Create(value0);
- Vector256<byte> values1 = Vector256.Create(value1);
- Vector256<byte> values2 = Vector256.Create(value2);
- do
- {
- Vector256<byte> search = LoadVector256(ref searchSpace, offset);
- Vector256<byte> matches0 = Avx2.CompareEqual(values0, search);
- Vector256<byte> matches1 = Avx2.CompareEqual(values1, search);
- Vector256<byte> matches2 = Avx2.CompareEqual(values2, search);
- // Bitwise Or to combine the matches and MoveMask to convert them to bitflags
- int matches = Avx2.MoveMask(Avx2.Or(Avx2.Or(matches0, matches1), matches2));
- // Note that MoveMask has converted the equal vector elements into a set of bit flags,
- // So the bit position in 'matches' corresponds to the element offset.
- if (matches == 0)
- {
- // Zero flags set so no matches
- offset += Vector256<byte>.Count;
- continue;
- }
- // Find bitflag offset of first match and add to current offset
- return ((int)(byte*)offset) + BitOperations.TrailingZeroCount(matches);
- } while ((byte*)lengthToExamine > (byte*)offset);
- }
- lengthToExamine = GetByteVector128SpanLength(offset, length);
- if ((byte*)lengthToExamine > (byte*)offset)
- {
- Vector128<byte> values0 = Vector128.Create(value0);
- Vector128<byte> values1 = Vector128.Create(value1);
- Vector128<byte> values2 = Vector128.Create(value2);
- Vector128<byte> search = LoadVector128(ref searchSpace, offset);
- Vector128<byte> matches0 = Sse2.CompareEqual(values0, search);
- Vector128<byte> matches1 = Sse2.CompareEqual(values1, search);
- Vector128<byte> matches2 = Sse2.CompareEqual(values2, search);
- // Same method as above
- int matches = Sse2.MoveMask(Sse2.Or(Sse2.Or(matches0, matches1), matches2));
- if (matches == 0)
- {
- // Zero flags set so no matches
- offset += Vector128<byte>.Count;
- }
- else
- {
- // Find bitflag offset of first match and add to current offset
- return ((int)(byte*)offset) + BitOperations.TrailingZeroCount(matches);
- }
- }
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Sse2.IsSupported)
- {
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = GetByteVector128SpanLength(offset, length);
- Vector128<byte> values0 = Vector128.Create(value0);
- Vector128<byte> values1 = Vector128.Create(value1);
- Vector128<byte> values2 = Vector128.Create(value2);
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- Vector128<byte> search = LoadVector128(ref searchSpace, offset);
- Vector128<byte> matches0 = Sse2.CompareEqual(values0, search);
- Vector128<byte> matches1 = Sse2.CompareEqual(values1, search);
- Vector128<byte> matches2 = Sse2.CompareEqual(values2, search);
- // Same method as above
- int matches = Sse2.MoveMask(Sse2.Or(Sse2.Or(matches0, matches1), matches2));
- if (matches == 0)
- {
- // Zero flags set so no matches
- offset += Vector128<byte>.Count;
- continue;
- }
- // Find bitflag offset of first match and add to current offset
- return ((int)(byte*)offset) + BitOperations.TrailingZeroCount(matches);
- }
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = GetByteVectorSpanLength(offset, length);
- Vector<byte> values0 = new Vector<byte>(value0);
- Vector<byte> values1 = new Vector<byte>(value1);
- Vector<byte> values2 = new Vector<byte>(value2);
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- Vector<byte> search = LoadVector(ref searchSpace, offset);
- var matches = Vector.BitwiseOr(
- Vector.BitwiseOr(
- Vector.Equals(search, values0),
- Vector.Equals(search, values1)),
- Vector.Equals(search, values2));
- if (Vector<byte>.Zero.Equals(matches))
- {
- offset += Vector<byte>.Count;
- continue;
- }
- // Find offset of first match and add to current offset
- return (int)(byte*)offset + LocateFirstFoundByte(matches);
- }
- if ((int)(byte*)offset < length)
- {
- lengthToExamine = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- return -1;
- Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
- return (int)(byte*)offset;
- Found1:
- return (int)(byte*)(offset + 1);
- Found2:
- return (int)(byte*)(offset + 2);
- Found3:
- return (int)(byte*)(offset + 3);
- Found4:
- return (int)(byte*)(offset + 4);
- Found5:
- return (int)(byte*)(offset + 5);
- Found6:
- return (int)(byte*)(offset + 6);
- Found7:
- return (int)(byte*)(offset + 7);
- }
- public static unsafe int LastIndexOfAny(ref byte searchSpace, byte value0, byte value1, int length)
- {
- Debug.Assert(length >= 0);
- uint uValue0 = value0; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- uint uValue1 = value1;
- IntPtr offset = (IntPtr)length; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr lengthToExamine = (IntPtr)length;
- if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
- {
- lengthToExamine = UnalignedCountVectorFromEnd(ref searchSpace, length);
- }
- SequentialScan:
- uint lookUp;
- while ((byte*)lengthToExamine >= (byte*)8)
- {
- lengthToExamine -= 8;
- offset -= 8;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 7);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found7;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 6);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found6;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 5);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found5;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 4);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found4;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 3);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found3;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 2);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found2;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 1);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found;
- }
- if ((byte*)lengthToExamine >= (byte*)4)
- {
- lengthToExamine -= 4;
- offset -= 4;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 3);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found3;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 2);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found2;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 1);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found;
- }
- while ((byte*)lengthToExamine > (byte*)0)
- {
- lengthToExamine -= 1;
- offset -= 1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found;
- }
- if (Vector.IsHardwareAccelerated && ((byte*)offset > (byte*)0))
- {
- lengthToExamine = (IntPtr)((int)(byte*)offset & ~(Vector<byte>.Count - 1));
- Vector<byte> values0 = new Vector<byte>(value0);
- Vector<byte> values1 = new Vector<byte>(value1);
- while ((byte*)lengthToExamine > (byte*)(Vector<byte>.Count - 1))
- {
- Vector<byte> search = LoadVector(ref searchSpace, offset - Vector<byte>.Count);
- var matches = Vector.BitwiseOr(
- Vector.Equals(search, values0),
- Vector.Equals(search, values1));
- if (Vector<byte>.Zero.Equals(matches))
- {
- offset -= Vector<byte>.Count;
- lengthToExamine -= Vector<byte>.Count;
- continue;
- }
- // Find offset of first match and add to current offset
- return (int)(offset) - Vector<byte>.Count + LocateLastFoundByte(matches);
- }
- if ((byte*)offset > (byte*)0)
- {
- lengthToExamine = offset;
- goto SequentialScan;
- }
- }
- return -1;
- Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
- return (int)(byte*)offset;
- Found1:
- return (int)(byte*)(offset + 1);
- Found2:
- return (int)(byte*)(offset + 2);
- Found3:
- return (int)(byte*)(offset + 3);
- Found4:
- return (int)(byte*)(offset + 4);
- Found5:
- return (int)(byte*)(offset + 5);
- Found6:
- return (int)(byte*)(offset + 6);
- Found7:
- return (int)(byte*)(offset + 7);
- }
- public static unsafe int LastIndexOfAny(ref byte searchSpace, byte value0, byte value1, byte value2, int length)
- {
- Debug.Assert(length >= 0);
- uint uValue0 = value0; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- uint uValue1 = value1;
- uint uValue2 = value2;
- IntPtr offset = (IntPtr)length; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr lengthToExamine = (IntPtr)length;
- if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
- {
- lengthToExamine = UnalignedCountVectorFromEnd(ref searchSpace, length);
- }
- SequentialScan:
- uint lookUp;
- while ((byte*)lengthToExamine >= (byte*)8)
- {
- lengthToExamine -= 8;
- offset -= 8;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 7);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found7;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 6);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found6;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 5);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found5;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 4);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found4;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 3);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found3;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 2);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found2;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 1);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found;
- }
- if ((byte*)lengthToExamine >= (byte*)4)
- {
- lengthToExamine -= 4;
- offset -= 4;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 3);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found3;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 2);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found2;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset + 1);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found;
- }
- while ((byte*)lengthToExamine > (byte*)0)
- {
- lengthToExamine -= 1;
- offset -= 1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
- goto Found;
- }
- if (Vector.IsHardwareAccelerated && ((byte*)offset > (byte*)0))
- {
- lengthToExamine = (IntPtr)((int)(byte*)offset & ~(Vector<byte>.Count - 1));
- Vector<byte> values0 = new Vector<byte>(value0);
- Vector<byte> values1 = new Vector<byte>(value1);
- Vector<byte> values2 = new Vector<byte>(value2);
- while ((byte*)lengthToExamine > (byte*)(Vector<byte>.Count - 1))
- {
- Vector<byte> search = LoadVector(ref searchSpace, offset - Vector<byte>.Count);
- var matches = Vector.BitwiseOr(
- Vector.BitwiseOr(
- Vector.Equals(search, values0),
- Vector.Equals(search, values1)),
- Vector.Equals(search, values2));
- if (Vector<byte>.Zero.Equals(matches))
- {
- offset -= Vector<byte>.Count;
- lengthToExamine -= Vector<byte>.Count;
- continue;
- }
- // Find offset of first match and add to current offset
- return (int)(offset) - Vector<byte>.Count + LocateLastFoundByte(matches);
- }
- if ((byte*)offset > (byte*)0)
- {
- lengthToExamine = offset;
- goto SequentialScan;
- }
- }
- return -1;
- Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
- return (int)(byte*)offset;
- Found1:
- return (int)(byte*)(offset + 1);
- Found2:
- return (int)(byte*)(offset + 2);
- Found3:
- return (int)(byte*)(offset + 3);
- Found4:
- return (int)(byte*)(offset + 4);
- Found5:
- return (int)(byte*)(offset + 5);
- Found6:
- return (int)(byte*)(offset + 6);
- Found7:
- return (int)(byte*)(offset + 7);
- }
- // Optimized byte-based SequenceEquals. The "length" parameter for this one is declared a nuint rather than int as we also use it for types other than byte
- // where the length can exceed 2Gb once scaled by sizeof(T).
- [MethodImpl(MethodImplOptions.AggressiveOptimization)]
- public static unsafe bool SequenceEqual(ref byte first, ref byte second, nuint length)
- {
- if (Unsafe.AreSame(ref first, ref second))
- goto Equal;
- IntPtr offset = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr lengthToExamine = (IntPtr)(void*)length;
- if (Vector.IsHardwareAccelerated && (byte*)lengthToExamine >= (byte*)Vector<byte>.Count)
- {
- lengthToExamine -= Vector<byte>.Count;
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- if (LoadVector(ref first, offset) != LoadVector(ref second, offset))
- {
- goto NotEqual;
- }
- offset += Vector<byte>.Count;
- }
- return LoadVector(ref first, lengthToExamine) == LoadVector(ref second, lengthToExamine);
- }
- if ((byte*)lengthToExamine >= (byte*)sizeof(UIntPtr))
- {
- lengthToExamine -= sizeof(UIntPtr);
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- if (LoadUIntPtr(ref first, offset) != LoadUIntPtr(ref second, offset))
- {
- goto NotEqual;
- }
- offset += sizeof(UIntPtr);
- }
- return LoadUIntPtr(ref first, lengthToExamine) == LoadUIntPtr(ref second, lengthToExamine);
- }
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- if (Unsafe.AddByteOffset(ref first, offset) != Unsafe.AddByteOffset(ref second, offset))
- goto NotEqual;
- offset += 1;
- }
- Equal:
- return true;
- NotEqual: // Workaround for https://github.com/dotnet/coreclr/issues/13549
- return false;
- }
- // Vector sub-search adapted from https://github.com/aspnet/KestrelHttpServer/pull/1138
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static int LocateFirstFoundByte(Vector<byte> match)
- {
- var vector64 = Vector.AsVectorUInt64(match);
- ulong candidate = 0;
- int i = 0;
- // Pattern unrolled by jit https://github.com/dotnet/coreclr/pull/8001
- for (; i < Vector<ulong>.Count; i++)
- {
- candidate = vector64[i];
- if (candidate != 0)
- {
- break;
- }
- }
- // Single LEA instruction with jitted const (using function result)
- return i * 8 + LocateFirstFoundByte(candidate);
- }
- [MethodImpl(MethodImplOptions.AggressiveOptimization)]
- public static unsafe int SequenceCompareTo(ref byte first, int firstLength, ref byte second, int secondLength)
- {
- Debug.Assert(firstLength >= 0);
- Debug.Assert(secondLength >= 0);
- if (Unsafe.AreSame(ref first, ref second))
- goto Equal;
- IntPtr minLength = (IntPtr)((firstLength < secondLength) ? firstLength : secondLength);
- IntPtr offset = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr lengthToExamine = (IntPtr)(void*)minLength;
- if (Avx2.IsSupported)
- {
- if ((byte*)lengthToExamine >= (byte*)Vector256<byte>.Count)
- {
- lengthToExamine -= Vector256<byte>.Count;
- uint matches;
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- matches = (uint)Avx2.MoveMask(Avx2.CompareEqual(LoadVector256(ref first, offset), LoadVector256(ref second, offset)));
- // Note that MoveMask has converted the equal vector elements into a set of bit flags,
- // So the bit position in 'matches' corresponds to the element offset.
- // 32 elements in Vector256<byte> so we compare to uint.MaxValue to check if everything matched
- if (matches == uint.MaxValue)
- {
- // All matched
- offset += Vector256<byte>.Count;
- continue;
- }
- goto Difference;
- }
- // Move to Vector length from end for final compare
- offset = lengthToExamine;
- // Same as method as above
- matches = (uint)Avx2.MoveMask(Avx2.CompareEqual(LoadVector256(ref first, offset), LoadVector256(ref second, offset)));
- if (matches == uint.MaxValue)
- {
- // All matched
- goto Equal;
- }
- Difference:
- // Invert matches to find differences
- uint differences = ~matches;
- // Find bitflag offset of first difference and add to current offset
- offset = (IntPtr)((int)(byte*)offset + BitOperations.TrailingZeroCount((int)differences));
- int result = Unsafe.AddByteOffset(ref first, offset).CompareTo(Unsafe.AddByteOffset(ref second, offset));
- Debug.Assert(result != 0);
- return result;
- }
- if ((byte*)lengthToExamine >= (byte*)Vector128<byte>.Count)
- {
- lengthToExamine -= Vector128<byte>.Count;
- uint matches;
- if ((byte*)lengthToExamine > (byte*)offset)
- {
- matches = (uint)Sse2.MoveMask(Sse2.CompareEqual(LoadVector128(ref first, offset), LoadVector128(ref second, offset)));
- // Note that MoveMask has converted the equal vector elements into a set of bit flags,
- // So the bit position in 'matches' corresponds to the element offset.
- // 16 elements in Vector128<byte> so we compare to ushort.MaxValue to check if everything matched
- if (matches == ushort.MaxValue)
- {
- // All matched
- offset += Vector128<byte>.Count;
- }
- else
- {
- goto Difference;
- }
- }
- // Move to Vector length from end for final compare
- offset = lengthToExamine;
- // Same as method as above
- matches = (uint)Sse2.MoveMask(Sse2.CompareEqual(LoadVector128(ref first, offset), LoadVector128(ref second, offset)));
- if (matches == ushort.MaxValue)
- {
- // All matched
- goto Equal;
- }
- Difference:
- // Invert matches to find differences
- uint differences = ~matches;
- // Find bitflag offset of first difference and add to current offset
- offset = (IntPtr)((int)(byte*)offset + BitOperations.TrailingZeroCount((int)differences));
- int result = Unsafe.AddByteOffset(ref first, offset).CompareTo(Unsafe.AddByteOffset(ref second, offset));
- Debug.Assert(result != 0);
- return result;
- }
- }
- else if (Sse2.IsSupported)
- {
- if ((byte*)lengthToExamine >= (byte*)Vector128<byte>.Count)
- {
- lengthToExamine -= Vector128<byte>.Count;
- uint matches;
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- matches = (uint)Sse2.MoveMask(Sse2.CompareEqual(LoadVector128(ref first, offset), LoadVector128(ref second, offset)));
- // Note that MoveMask has converted the equal vector elements into a set of bit flags,
- // So the bit position in 'matches' corresponds to the element offset.
- // 16 elements in Vector128<byte> so we compare to ushort.MaxValue to check if everything matched
- if (matches == ushort.MaxValue)
- {
- // All matched
- offset += Vector128<byte>.Count;
- continue;
- }
- goto Difference;
- }
- // Move to Vector length from end for final compare
- offset = lengthToExamine;
- // Same as method as above
- matches = (uint)Sse2.MoveMask(Sse2.CompareEqual(LoadVector128(ref first, offset), LoadVector128(ref second, offset)));
- if (matches == ushort.MaxValue)
- {
- // All matched
- goto Equal;
- }
- Difference:
- // Invert matches to find differences
- uint differences = ~matches;
- // Find bitflag offset of first difference and add to current offset
- offset = (IntPtr)((int)(byte*)offset + BitOperations.TrailingZeroCount((int)differences));
- int result = Unsafe.AddByteOffset(ref first, offset).CompareTo(Unsafe.AddByteOffset(ref second, offset));
- Debug.Assert(result != 0);
- return result;
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if ((byte*)lengthToExamine > (byte*)Vector<byte>.Count)
- {
- lengthToExamine -= Vector<byte>.Count;
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- if (LoadVector(ref first, offset) != LoadVector(ref second, offset))
- {
- goto BytewiseCheck;
- }
- offset += Vector<byte>.Count;
- }
- goto BytewiseCheck;
- }
- }
- if ((byte*)lengthToExamine > (byte*)sizeof(UIntPtr))
- {
- lengthToExamine -= sizeof(UIntPtr);
- while ((byte*)lengthToExamine > (byte*)offset)
- {
- if (LoadUIntPtr(ref first, offset) != LoadUIntPtr(ref second, offset))
- {
- goto BytewiseCheck;
- }
- offset += sizeof(UIntPtr);
- }
- }
- BytewiseCheck: // Workaround for https://github.com/dotnet/coreclr/issues/13549
- while ((byte*)minLength > (byte*)offset)
- {
- int result = Unsafe.AddByteOffset(ref first, offset).CompareTo(Unsafe.AddByteOffset(ref second, offset));
- if (result != 0)
- return result;
- offset += 1;
- }
- Equal:
- return firstLength - secondLength;
- }
- // Vector sub-search adapted from https://github.com/aspnet/KestrelHttpServer/pull/1138
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static int LocateLastFoundByte(Vector<byte> match)
- {
- var vector64 = Vector.AsVectorUInt64(match);
- ulong candidate = 0;
- int i = Vector<ulong>.Count - 1;
- // Pattern unrolled by jit https://github.com/dotnet/coreclr/pull/8001
- for (; i >= 0; i--)
- {
- candidate = vector64[i];
- if (candidate != 0)
- {
- break;
- }
- }
- // Single LEA instruction with jitted const (using function result)
- return i * 8 + LocateLastFoundByte(candidate);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static int LocateFirstFoundByte(ulong match)
- {
- if (Bmi1.X64.IsSupported)
- {
- return (int)(Bmi1.X64.TrailingZeroCount(match) >> 3);
- }
- else
- {
- // Flag least significant power of two bit
- var powerOfTwoFlag = match ^ (match - 1);
- // Shift all powers of two into the high byte and extract
- return (int)((powerOfTwoFlag * XorPowerOfTwoToHighByte) >> 57);
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static int LocateLastFoundByte(ulong match)
- {
- return 7 - (BitOperations.LeadingZeroCount(match) >> 3);
- }
- private const ulong XorPowerOfTwoToHighByte = (0x07ul |
- 0x06ul << 8 |
- 0x05ul << 16 |
- 0x04ul << 24 |
- 0x03ul << 32 |
- 0x02ul << 40 |
- 0x01ul << 48) + 1;
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe UIntPtr LoadUIntPtr(ref byte start, IntPtr offset)
- => Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref start, offset));
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe Vector<byte> LoadVector(ref byte start, IntPtr offset)
- => Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref start, offset));
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe Vector128<byte> LoadVector128(ref byte start, IntPtr offset)
- => Unsafe.ReadUnaligned<Vector128<byte>>(ref Unsafe.AddByteOffset(ref start, offset));
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe Vector256<byte> LoadVector256(ref byte start, IntPtr offset)
- => Unsafe.ReadUnaligned<Vector256<byte>>(ref Unsafe.AddByteOffset(ref start, offset));
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe IntPtr GetByteVectorSpanLength(IntPtr offset, int length)
- => (IntPtr)((length - (int)(byte*)offset) & ~(Vector<byte>.Count - 1));
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe IntPtr GetByteVector128SpanLength(IntPtr offset, int length)
- => (IntPtr)((length - (int)(byte*)offset) & ~(Vector128<byte>.Count - 1));
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe IntPtr GetByteVector256SpanLength(IntPtr offset, int length)
- => (IntPtr)((length - (int)(byte*)offset) & ~(Vector256<byte>.Count - 1));
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe IntPtr UnalignedCountVector(ref byte searchSpace)
- {
- int unaligned = (int)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
- return (IntPtr)((Vector<byte>.Count - unaligned) & (Vector<byte>.Count - 1));
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe IntPtr UnalignedCountVector128(ref byte searchSpace)
- {
- int unaligned = (int)Unsafe.AsPointer(ref searchSpace) & (Vector128<byte>.Count - 1);
- return (IntPtr)((Vector128<byte>.Count - unaligned) & (Vector128<byte>.Count - 1));
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe IntPtr UnalignedCountVectorFromEnd(ref byte searchSpace, int length)
- {
- int unaligned = (int)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
- return (IntPtr)(((length & (Vector<byte>.Count - 1)) + unaligned) & (Vector<byte>.Count - 1));
- }
- }
- }
|