| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645 |
- // 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;
- }
- if (Avx2.IsSupported)
- {
- if ((int)(byte*)offset < length)
- {
- 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)
- {
- // TODO: Arm variants
- 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)
- {
- // TODO: Arm variants
- if (Lzcnt.X64.IsSupported)
- {
- return 7 - (int)(Lzcnt.X64.LeadingZeroCount(match) >> 3);
- }
- else
- {
- // Find the most significant byte that has its highest bit set
- int index = 7;
- while ((long)match > 0)
- {
- match = match << 8;
- index--;
- }
- return index;
- }
- }
- 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));
- }
- }
- }
|