| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655 |
- // 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.Runtime.CompilerServices;
- using System.Numerics;
- 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 0; // A zero-length sequence is always treated as "found" at the start of the search space.
- 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 0; // A zero-length sequence is always treated as "found" at the start of the search space.
- 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 nLength = (IntPtr)length;
- if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
- {
- nLength = UnalignedByteCountVector(ref searchSpace);
- }
- SequentialScan:
- while ((byte*)nLength >= (byte*)8)
- {
- nLength -= 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*)nLength >= (byte*)4)
- {
- nLength -= 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*)nLength > (byte*)0)
- {
- nLength -= 1;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset))
- goto Found;
- offset += 1;
- }
- if (Vector.IsHardwareAccelerated && ((int)(byte*)offset < length))
- {
- nLength = (IntPtr)((length - (int)(byte*)offset) & ~(Vector<byte>.Count - 1));
- Vector<byte> values = new Vector<byte>(value);
- while ((byte*)nLength > (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)
- {
- nLength = (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 nLength = (IntPtr)length;
- if (Avx2.IsSupported || Sse2.IsSupported)
- {
- // Avx2 branch also operates on Sse2 sizes, so check is combined.
- if (length >= Vector128<byte>.Count * 2)
- {
- nLength = UnalignedByteCountVector128(ref searchSpace);
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if (length >= Vector<byte>.Count * 2)
- {
- nLength = UnalignedByteCountVector(ref searchSpace);
- }
- }
- SequentialScan:
- while ((byte*)nLength >= (byte*)8)
- {
- nLength -= 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*)nLength >= (byte*)4)
- {
- nLength -= 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*)nLength > (byte*)0)
- {
- nLength -= 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) + BitOps.TrailingZeroCount(matches);
- }
- }
- nLength = GetByteVector256SpanLength(offset, length);
- if ((byte*)nLength > (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) + BitOps.TrailingZeroCount(matches);
- } while ((byte*)nLength > (byte*)offset);
- }
- nLength = GetByteVector128SpanLength(offset, length);
- if ((byte*)nLength > (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) + BitOps.TrailingZeroCount(matches);
- }
- }
- if ((int)(byte*)offset < length)
- {
- nLength = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Sse2.IsSupported)
- {
- if ((int)(byte*)offset < length)
- {
- nLength = GetByteVector128SpanLength(offset, length);
- Vector128<byte> values = Vector128.Create(value);
- while ((byte*)nLength > (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) + BitOps.TrailingZeroCount(matches);
- }
- if ((int)(byte*)offset < length)
- {
- nLength = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if ((int)(byte*)offset < length)
- {
- nLength = GetByteVectorSpanLength(offset, length);
- Vector<byte> values = new Vector<byte>(value);
- while ((byte*)nLength > (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)
- {
- nLength = (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 nLength = (IntPtr)length;
- if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
- {
- nLength = UnalignedByteCountVectorFromEnd(ref searchSpace, length);
- }
- SequentialScan:
- while ((byte*)nLength >= (byte*)8)
- {
- nLength -= 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*)nLength >= (byte*)4)
- {
- nLength -= 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*)nLength > (byte*)0)
- {
- nLength -= 1;
- offset -= 1;
- if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset))
- goto Found;
- }
- if (Vector.IsHardwareAccelerated && ((byte*)offset > (byte*)0))
- {
- nLength = (IntPtr)((int)(byte*)offset & ~(Vector<byte>.Count - 1));
- Vector<byte> values = new Vector<byte>(value);
- while ((byte*)nLength > (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;
- nLength -= 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)
- {
- nLength = 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 nLength = (IntPtr)length;
- if (Avx2.IsSupported || Sse2.IsSupported)
- {
- // Avx2 branch also operates on Sse2 sizes, so check is combined.
- if (length >= Vector128<byte>.Count * 2)
- {
- nLength = UnalignedByteCountVector128(ref searchSpace);
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if (length >= Vector<byte>.Count * 2)
- {
- nLength = UnalignedByteCountVector(ref searchSpace);
- }
- }
- SequentialScan:
- uint lookUp;
- while ((byte*)nLength >= (byte*)8)
- {
- nLength -= 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*)nLength >= (byte*)4)
- {
- nLength -= 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*)nLength > (byte*)0)
- {
- nLength -= 1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found;
- offset += 1;
- }
- if (Avx2.IsSupported)
- {
- if ((int)(byte*)offset < length)
- {
- nLength = GetByteVector256SpanLength(offset, length);
- if ((byte*)nLength > (byte*)offset)
- {
- Vector256<byte> values0 = Vector256.Create(value0);
- Vector256<byte> values1 = Vector256.Create(value1);
- do
- {
- Vector256<byte> search = LoadVector256(ref searchSpace, 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.
- int matches = Avx2.MoveMask(Avx2.CompareEqual(values0, search));
- // Bitwise Or to combine the flagged matches for the second value to our match flags
- matches |= Avx2.MoveMask(Avx2.CompareEqual(values1, search));
- 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) + BitOps.TrailingZeroCount(matches);
- } while ((byte*)nLength > (byte*)offset);
- }
- nLength = GetByteVector128SpanLength(offset, length);
- if ((byte*)nLength > (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.CompareEqual(values0, search));
- matches |= Sse2.MoveMask(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) + BitOps.TrailingZeroCount(matches);
- }
- }
- if ((int)(byte*)offset < length)
- {
- nLength = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Sse2.IsSupported)
- {
- if ((int)(byte*)offset < length)
- {
- nLength = GetByteVector128SpanLength(offset, length);
- Vector128<byte> values0 = Vector128.Create(value0);
- Vector128<byte> values1 = Vector128.Create(value1);
- while ((byte*)nLength > (byte*)offset)
- {
- Vector128<byte> search = LoadVector128(ref searchSpace, offset);
- // Same method as above
- int matches = Sse2.MoveMask(Sse2.CompareEqual(values0, search));
- matches |= Sse2.MoveMask(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) + BitOps.TrailingZeroCount(matches);
- }
- if ((int)(byte*)offset < length)
- {
- nLength = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if ((int)(byte*)offset < length)
- {
- nLength = GetByteVectorSpanLength(offset, length);
- Vector<byte> values0 = new Vector<byte>(value0);
- Vector<byte> values1 = new Vector<byte>(value1);
- while ((byte*)nLength > (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)
- {
- nLength = (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 nLength = (IntPtr)length;
- if (Avx2.IsSupported || Sse2.IsSupported)
- {
- // Avx2 branch also operates on Sse2 sizes, so check is combined.
- if (length >= Vector128<byte>.Count * 2)
- {
- nLength = UnalignedByteCountVector128(ref searchSpace);
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if (length >= Vector<byte>.Count * 2)
- {
- nLength = UnalignedByteCountVector(ref searchSpace);
- }
- }
- SequentialScan:
- uint lookUp;
- while ((byte*)nLength >= (byte*)8)
- {
- nLength -= 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*)nLength >= (byte*)4)
- {
- nLength -= 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*)nLength > (byte*)0)
- {
- nLength -= 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)
- {
- nLength = GetByteVector256SpanLength(offset, length);
- if ((byte*)nLength > (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);
- // 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.
- int matches = Avx2.MoveMask(Avx2.CompareEqual(values0, search));
- // Bitwise Or to combine the flagged matches for the second value to our match flags
- matches |= Avx2.MoveMask(Avx2.CompareEqual(values1, search));
- // Bitwise Or to combine the flagged matches for the third value to our match flags
- matches |= Avx2.MoveMask(Avx2.CompareEqual(values2, search));
- 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) + BitOps.TrailingZeroCount(matches);
- } while ((byte*)nLength > (byte*)offset);
- }
- nLength = GetByteVector128SpanLength(offset, length);
- if ((byte*)nLength > (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);
- // Same method as above
- int matches = Sse2.MoveMask(Sse2.CompareEqual(values0, search));
- matches |= Sse2.MoveMask(Sse2.CompareEqual(values1, search));
- matches |= Sse2.MoveMask(Sse2.CompareEqual(values2, 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) + BitOps.TrailingZeroCount(matches);
- }
- }
- if ((int)(byte*)offset < length)
- {
- nLength = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Sse2.IsSupported)
- {
- if ((int)(byte*)offset < length)
- {
- nLength = GetByteVector128SpanLength(offset, length);
- Vector128<byte> values0 = Vector128.Create(value0);
- Vector128<byte> values1 = Vector128.Create(value1);
- Vector128<byte> values2 = Vector128.Create(value2);
- while ((byte*)nLength > (byte*)offset)
- {
- Vector128<byte> search = LoadVector128(ref searchSpace, offset);
- // Same method as above
- int matches = Sse2.MoveMask(Sse2.CompareEqual(values0, search));
- matches |= Sse2.MoveMask(Sse2.CompareEqual(values1, search));
- matches |= Sse2.MoveMask(Sse2.CompareEqual(values2, 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) + BitOps.TrailingZeroCount(matches);
- }
- if ((int)(byte*)offset < length)
- {
- nLength = (IntPtr)(length - (int)(byte*)offset);
- goto SequentialScan;
- }
- }
- }
- else if (Vector.IsHardwareAccelerated)
- {
- if ((int)(byte*)offset < length)
- {
- nLength = 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*)nLength > (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)
- {
- nLength = (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 nLength = (IntPtr)length;
- if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
- {
- nLength = UnalignedByteCountVectorFromEnd(ref searchSpace, length);
- }
- SequentialScan:
- uint lookUp;
- while ((byte*)nLength >= (byte*)8)
- {
- nLength -= 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*)nLength >= (byte*)4)
- {
- nLength -= 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*)nLength > (byte*)0)
- {
- nLength -= 1;
- offset -= 1;
- lookUp = Unsafe.AddByteOffset(ref searchSpace, offset);
- if (uValue0 == lookUp || uValue1 == lookUp)
- goto Found;
- }
- if (Vector.IsHardwareAccelerated && ((byte*)offset > (byte*)0))
- {
- nLength = (IntPtr)((int)(byte*)offset & ~(Vector<byte>.Count - 1));
- Vector<byte> values0 = new Vector<byte>(value0);
- Vector<byte> values1 = new Vector<byte>(value1);
- while ((byte*)nLength > (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;
- nLength -= 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)
- {
- nLength = 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 nLength = (IntPtr)length;
- if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
- {
- nLength = UnalignedByteCountVectorFromEnd(ref searchSpace, length);
- }
- SequentialScan:
- uint lookUp;
- while ((byte*)nLength >= (byte*)8)
- {
- nLength -= 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*)nLength >= (byte*)4)
- {
- nLength -= 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*)nLength > (byte*)0)
- {
- nLength -= 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))
- {
- nLength = (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*)nLength > (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;
- nLength -= 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)
- {
- nLength = 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 nLength = (IntPtr)(void*)length;
- if (Vector.IsHardwareAccelerated && (byte*)nLength >= (byte*)Vector<byte>.Count)
- {
- nLength -= Vector<byte>.Count;
- while ((byte*)nLength > (byte*)offset)
- {
- if (LoadVector(ref first, offset) != LoadVector(ref second, offset))
- {
- goto NotEqual;
- }
- offset += Vector<byte>.Count;
- }
- return LoadVector(ref first, nLength) == LoadVector(ref second, nLength);
- }
- if ((byte*)nLength >= (byte*)sizeof(UIntPtr))
- {
- nLength -= sizeof(UIntPtr);
- while ((byte*)nLength > (byte*)offset)
- {
- if (LoadUIntPtr(ref first, offset) != LoadUIntPtr(ref second, offset))
- {
- goto NotEqual;
- }
- offset += sizeof(UIntPtr);
- }
- return LoadUIntPtr(ref first, nLength) == LoadUIntPtr(ref second, nLength);
- }
- while ((byte*)nLength > (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 nLength = (IntPtr)(void*)minLength;
- if (Avx2.IsSupported)
- {
- if ((byte*)nLength >= (byte*)Vector256<byte>.Count)
- {
- nLength -= Vector256<byte>.Count;
- uint matches;
- while ((byte*)nLength > (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 = nLength;
- // 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 + BitOps.TrailingZeroCount((int)differences));
- int result = Unsafe.AddByteOffset(ref first, offset).CompareTo(Unsafe.AddByteOffset(ref second, offset));
- Debug.Assert(result != 0);
- return result;
- }
- if ((byte*)nLength >= (byte*)Vector128<byte>.Count)
- {
- nLength -= Vector128<byte>.Count;
- uint matches;
- if ((byte*)nLength > (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 = nLength;
- // 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 + BitOps.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*)nLength >= (byte*)Vector128<byte>.Count)
- {
- nLength -= Vector128<byte>.Count;
- uint matches;
- while ((byte*)nLength > (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 = nLength;
- // 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 + BitOps.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*)nLength > (byte*)Vector<byte>.Count)
- {
- nLength -= Vector<byte>.Count;
- while ((byte*)nLength > (byte*)offset)
- {
- if (LoadVector(ref first, offset) != LoadVector(ref second, offset))
- {
- goto BytewiseCheck;
- }
- offset += Vector<byte>.Count;
- }
- goto BytewiseCheck;
- }
- }
- if ((byte*)nLength > (byte*)sizeof(UIntPtr))
- {
- nLength -= sizeof(UIntPtr);
- while ((byte*)nLength > (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 - (BitOps.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 UnalignedByteCountVector(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 UnalignedByteCountVector128(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 UnalignedByteCountVectorFromEnd(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));
- }
- }
- }
|