Debug.Assert(length >= 0);
uint uValue = value; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- IntPtr index = (IntPtr)0; // Use UIntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr nLength = (IntPtr)(uint)length;
+ IntPtr index = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
+ IntPtr nLength = (IntPtr)length;
#if !netstandard11
if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
{
- unchecked
- {
- int unaligned = (int)(byte*)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
- nLength = (IntPtr)(uint)((Vector<byte>.Count - unaligned) & (Vector<byte>.Count - 1));
- }
+ int unaligned = (int)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
+ nLength = (IntPtr)((Vector<byte>.Count - unaligned) & (Vector<byte>.Count - 1));
}
SequentialScan:
#endif
{
nLength -= 8;
- if (uValue == Unsafe.Add(ref searchSpace, index))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index))
goto Found;
- if (uValue == Unsafe.Add(ref searchSpace, index + 1))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 1))
goto Found1;
- if (uValue == Unsafe.Add(ref searchSpace, index + 2))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 2))
goto Found2;
- if (uValue == Unsafe.Add(ref searchSpace, index + 3))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 3))
goto Found3;
- if (uValue == Unsafe.Add(ref searchSpace, index + 4))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 4))
goto Found4;
- if (uValue == Unsafe.Add(ref searchSpace, index + 5))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 5))
goto Found5;
- if (uValue == Unsafe.Add(ref searchSpace, index + 6))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 6))
goto Found6;
- if (uValue == Unsafe.Add(ref searchSpace, index + 7))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 7))
goto Found7;
index += 8;
{
nLength -= 4;
- if (uValue == Unsafe.Add(ref searchSpace, index))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index))
goto Found;
- if (uValue == Unsafe.Add(ref searchSpace, index + 1))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 1))
goto Found1;
- if (uValue == Unsafe.Add(ref searchSpace, index + 2))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 2))
goto Found2;
- if (uValue == Unsafe.Add(ref searchSpace, index + 3))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 3))
goto Found3;
index += 4;
{
nLength -= 1;
- if (uValue == Unsafe.Add(ref searchSpace, index))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index))
goto Found;
index += 1;
#if !netstandard11
if (Vector.IsHardwareAccelerated && ((int)(byte*)index < length))
{
- nLength = (IntPtr)(uint)((length - (uint)index) & ~(Vector<byte>.Count - 1));
+ nLength = (IntPtr)((length - (int)(byte*)index) & ~(Vector<byte>.Count - 1));
+
// Get comparison Vector
Vector<byte> vComparison = GetVector(value);
+
while ((byte*)nLength > (byte*)index)
{
var vMatches = Vector.Equals(vComparison, Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref searchSpace, index)));
if ((int)(byte*)index < length)
{
- unchecked
- {
- nLength = (IntPtr)(length - (int)(byte*)index);
- }
+ nLength = (IntPtr)(length - (int)(byte*)index);
goto SequentialScan;
}
}
Debug.Assert(length >= 0);
uint uValue = value; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- IntPtr index = (IntPtr)(uint)length; // Use UIntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr nLength = (IntPtr)(uint)length;
+ IntPtr index = (IntPtr)length; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
+ IntPtr nLength = (IntPtr)length;
#if !netstandard11
if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
{
- unchecked
- {
- int unaligned = (int)(byte*)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
- nLength = (IntPtr)(((length & (Vector<byte>.Count - 1)) + unaligned) & (Vector<byte>.Count - 1));
- }
+ int unaligned = (int)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
+ nLength = (IntPtr)(((length & (Vector<byte>.Count - 1)) + unaligned) & (Vector<byte>.Count - 1));
}
SequentialScan:
#endif
nLength -= 8;
index -= 8;
- if (uValue == Unsafe.Add(ref searchSpace, index + 7))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 7))
goto Found7;
- if (uValue == Unsafe.Add(ref searchSpace, index + 6))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 6))
goto Found6;
- if (uValue == Unsafe.Add(ref searchSpace, index + 5))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 5))
goto Found5;
- if (uValue == Unsafe.Add(ref searchSpace, index + 4))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 4))
goto Found4;
- if (uValue == Unsafe.Add(ref searchSpace, index + 3))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 3))
goto Found3;
- if (uValue == Unsafe.Add(ref searchSpace, index + 2))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 2))
goto Found2;
- if (uValue == Unsafe.Add(ref searchSpace, index + 1))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 1))
goto Found1;
- if (uValue == Unsafe.Add(ref searchSpace, index))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index))
goto Found;
}
nLength -= 4;
index -= 4;
- if (uValue == Unsafe.Add(ref searchSpace, index + 3))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 3))
goto Found3;
- if (uValue == Unsafe.Add(ref searchSpace, index + 2))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 2))
goto Found2;
- if (uValue == Unsafe.Add(ref searchSpace, index + 1))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index + 1))
goto Found1;
- if (uValue == Unsafe.Add(ref searchSpace, index))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index))
goto Found;
}
nLength -= 1;
index -= 1;
- if (uValue == Unsafe.Add(ref searchSpace, index))
+ if (uValue == Unsafe.AddByteOffset(ref searchSpace, index))
goto Found;
}
#if !netstandard11
- if (Vector.IsHardwareAccelerated && ((int)(byte*)index > 0))
+ if (Vector.IsHardwareAccelerated && ((byte*)index > (byte*)0))
{
- nLength = (IntPtr)(uint)((uint)index & ~(Vector<byte>.Count - 1));
+ nLength = (IntPtr)((int)(byte*)index & ~(Vector<byte>.Count - 1));
// Get comparison Vector
Vector<byte> vComparison = GetVector(value);
+
while ((byte*)nLength > (byte*)(Vector<byte>.Count - 1))
{
var vMatches = Vector.Equals(vComparison, Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref searchSpace, index - Vector<byte>.Count)));
continue;
}
// Find offset of first match
- return (int)(byte*)(index) - Vector<byte>.Count + LocateLastFoundByte(vMatches);
+ return (int)(index) - Vector<byte>.Count + LocateLastFoundByte(vMatches);
}
- if ((int)(byte*)index > 0)
+ if ((byte*)index > (byte*)0)
{
nLength = index;
goto SequentialScan;
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 index = (IntPtr)0; // Use UIntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr nLength = (IntPtr)(uint)length;
+ IntPtr index = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
+ IntPtr nLength = (IntPtr)length;
#if !netstandard11
if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
{
- unchecked
- {
- int unaligned = (int)(byte*)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
- nLength = (IntPtr)(uint)((Vector<byte>.Count - unaligned) & (Vector<byte>.Count - 1));
- }
+ int unaligned = (int)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
+ nLength = (IntPtr)((Vector<byte>.Count - unaligned) & (Vector<byte>.Count - 1));
}
SequentialScan:
#endif
{
nLength -= 8;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found;
- lookUp = Unsafe.Add(ref searchSpace, index + 1);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 1);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found1;
- lookUp = Unsafe.Add(ref searchSpace, index + 2);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 2);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found2;
- lookUp = Unsafe.Add(ref searchSpace, index + 3);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 3);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found3;
- lookUp = Unsafe.Add(ref searchSpace, index + 4);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 4);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found4;
- lookUp = Unsafe.Add(ref searchSpace, index + 5);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 5);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found5;
- lookUp = Unsafe.Add(ref searchSpace, index + 6);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 6);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found6;
- lookUp = Unsafe.Add(ref searchSpace, index + 7);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 7);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found7;
{
nLength -= 4;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found;
- lookUp = Unsafe.Add(ref searchSpace, index + 1);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 1);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found1;
- lookUp = Unsafe.Add(ref searchSpace, index + 2);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 2);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found2;
- lookUp = Unsafe.Add(ref searchSpace, index + 3);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 3);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found3;
{
nLength -= 1;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found;
#if !netstandard11
if (Vector.IsHardwareAccelerated && ((int)(byte*)index < length))
{
- nLength = (IntPtr)(uint)((length - (uint)index) & ~(Vector<byte>.Count - 1));
+ nLength = (IntPtr)((length - (int)(byte*)index) & ~(Vector<byte>.Count - 1));
+
// Get comparison Vector
Vector<byte> values0 = GetVector(value0);
Vector<byte> values1 = GetVector(value1);
if ((int)(byte*)index < length)
{
- unchecked
- {
- nLength = (IntPtr)(length - (int)(byte*)index);
- }
+ nLength = (IntPtr)(length - (int)(byte*)index);
goto SequentialScan;
}
}
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
uint uValue2 = value2; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- IntPtr index = (IntPtr)0; // Use UIntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr nLength = (IntPtr)(uint)length;
+ IntPtr index = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
+ IntPtr nLength = (IntPtr)length;
#if !netstandard11
if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
{
- unchecked
- {
- int unaligned = (int)(byte*)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
- nLength = (IntPtr)(uint)((Vector<byte>.Count - unaligned) & (Vector<byte>.Count - 1));
- }
+ int unaligned = (int)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
+ nLength = (IntPtr)((Vector<byte>.Count - unaligned) & (Vector<byte>.Count - 1));
}
SequentialScan:
#endif
{
nLength -= 8;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found;
- lookUp = Unsafe.Add(ref searchSpace, index + 1);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 1);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found1;
- lookUp = Unsafe.Add(ref searchSpace, index + 2);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 2);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found2;
- lookUp = Unsafe.Add(ref searchSpace, index + 3);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 3);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found3;
- lookUp = Unsafe.Add(ref searchSpace, index + 4);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 4);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found4;
- lookUp = Unsafe.Add(ref searchSpace, index + 5);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 5);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found5;
- lookUp = Unsafe.Add(ref searchSpace, index + 6);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 6);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found6;
- lookUp = Unsafe.Add(ref searchSpace, index + 7);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 7);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found7;
{
nLength -= 4;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found;
- lookUp = Unsafe.Add(ref searchSpace, index + 1);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 1);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found1;
- lookUp = Unsafe.Add(ref searchSpace, index + 2);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 2);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found2;
- lookUp = Unsafe.Add(ref searchSpace, index + 3);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 3);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found3;
{
nLength -= 1;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found;
#if !netstandard11
if (Vector.IsHardwareAccelerated && ((int)(byte*)index < length))
{
- nLength = (IntPtr)(uint)((length - (uint)index) & ~(Vector<byte>.Count - 1));
+ nLength = (IntPtr)((length - (int)(byte*)index) & ~(Vector<byte>.Count - 1));
+
// Get comparison Vector
Vector<byte> values0 = GetVector(value0);
Vector<byte> values1 = GetVector(value1);
Vector<byte> values2 = GetVector(value2);
+
while ((byte*)nLength > (byte*)index)
{
Vector<byte> vData = Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref searchSpace, index));
if ((int)(byte*)index < length)
{
- unchecked
- {
- nLength = (IntPtr)(length - (int)(byte*)index);
- }
+ nLength = (IntPtr)(length - (int)(byte*)index);
goto SequentialScan;
}
}
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 index = (IntPtr)(uint)length; // Use UIntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr nLength = (IntPtr)(uint)length;
+ IntPtr index = (IntPtr)length; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
+ IntPtr nLength = (IntPtr)length;
#if !netstandard11
if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
{
- unchecked
- {
- int unaligned = (int)(byte*)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
- nLength = (IntPtr)(((length & (Vector<byte>.Count - 1)) + unaligned) & (Vector<byte>.Count - 1));
- }
+ int unaligned = (int)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
+ nLength = (IntPtr)(((length & (Vector<byte>.Count - 1)) + unaligned) & (Vector<byte>.Count - 1));
}
SequentialScan:
#endif
nLength -= 8;
index -= 8;
- lookUp = Unsafe.Add(ref searchSpace, index + 7);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 7);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found7;
- lookUp = Unsafe.Add(ref searchSpace, index + 6);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 6);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found6;
- lookUp = Unsafe.Add(ref searchSpace, index + 5);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 5);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found5;
- lookUp = Unsafe.Add(ref searchSpace, index + 4);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 4);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found4;
- lookUp = Unsafe.Add(ref searchSpace, index + 3);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 3);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found3;
- lookUp = Unsafe.Add(ref searchSpace, index + 2);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 2);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found2;
- lookUp = Unsafe.Add(ref searchSpace, index + 1);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 1);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found1;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found;
}
nLength -= 4;
index -= 4;
- lookUp = Unsafe.Add(ref searchSpace, index + 3);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 3);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found3;
- lookUp = Unsafe.Add(ref searchSpace, index + 2);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 2);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found2;
- lookUp = Unsafe.Add(ref searchSpace, index + 1);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 1);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found1;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found;
}
nLength -= 1;
index -= 1;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp)
goto Found;
}
#if !netstandard11
- if (Vector.IsHardwareAccelerated && ((int)(byte*)index > 0))
+ if (Vector.IsHardwareAccelerated && ((byte*)index > (byte*)0))
{
- nLength = (IntPtr)(uint)((uint)index & ~(Vector<byte>.Count - 1));
+ nLength = (IntPtr)((int)(byte*)index & ~(Vector<byte>.Count - 1));
+
// Get comparison Vector
Vector<byte> values0 = GetVector(value0);
Vector<byte> values1 = GetVector(value1);
continue;
}
// Find offset of first match
- return (int)(byte*)(index) - Vector<byte>.Count + LocateLastFoundByte(vMatches);
+ return (int)(index) - Vector<byte>.Count + LocateLastFoundByte(vMatches);
}
- if ((int)(byte*)index > 0)
+ if ((byte*)index > (byte*)0)
{
nLength = index;
goto SequentialScan;
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
uint uValue2 = value2; // Use uint for comparisons to avoid unnecessary 8->32 extensions
- IntPtr index = (IntPtr)(uint)length; // Use UIntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
- IntPtr nLength = (IntPtr)(uint)length;
+ IntPtr index = (IntPtr)length; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
+ IntPtr nLength = (IntPtr)length;
#if !netstandard11
if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2)
{
- unchecked
- {
- int unaligned = (int)(byte*)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
- nLength = (IntPtr)(((length & (Vector<byte>.Count - 1)) + unaligned) & (Vector<byte>.Count - 1));
- }
+ int unaligned = (int)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
+ nLength = (IntPtr)(((length & (Vector<byte>.Count - 1)) + unaligned) & (Vector<byte>.Count - 1));
}
SequentialScan:
#endif
nLength -= 8;
index -= 8;
- lookUp = Unsafe.Add(ref searchSpace, index + 7);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 7);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found7;
- lookUp = Unsafe.Add(ref searchSpace, index + 6);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 6);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found6;
- lookUp = Unsafe.Add(ref searchSpace, index + 5);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 5);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found5;
- lookUp = Unsafe.Add(ref searchSpace, index + 4);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 4);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found4;
- lookUp = Unsafe.Add(ref searchSpace, index + 3);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 3);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found3;
- lookUp = Unsafe.Add(ref searchSpace, index + 2);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 2);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found2;
- lookUp = Unsafe.Add(ref searchSpace, index + 1);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 1);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found1;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found;
}
nLength -= 4;
index -= 4;
- lookUp = Unsafe.Add(ref searchSpace, index + 3);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 3);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found3;
- lookUp = Unsafe.Add(ref searchSpace, index + 2);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 2);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found2;
- lookUp = Unsafe.Add(ref searchSpace, index + 1);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index + 1);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found1;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found;
}
nLength -= 1;
index -= 1;
- lookUp = Unsafe.Add(ref searchSpace, index);
+ lookUp = Unsafe.AddByteOffset(ref searchSpace, index);
if (uValue0 == lookUp || uValue1 == lookUp || uValue2 == lookUp)
goto Found;
}
#if !netstandard11
- if (Vector.IsHardwareAccelerated && ((int)(byte*)index > 0))
+ if (Vector.IsHardwareAccelerated && ((byte*)index > (byte*)0))
{
- nLength = (IntPtr)(uint)((uint)index & ~(Vector<byte>.Count - 1));
+ nLength = (IntPtr)((int)(byte*)index & ~(Vector<byte>.Count - 1));
+
// Get comparison Vector
Vector<byte> values0 = GetVector(value0);
Vector<byte> values1 = GetVector(value1);
Vector<byte> values2 = GetVector(value2);
+
while ((byte*)nLength > (byte*)(Vector<byte>.Count - 1))
{
Vector<byte> vData = Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref searchSpace, index - Vector<byte>.Count));
continue;
}
// Find offset of first match
- return (int)(byte*)(index) - Vector<byte>.Count + LocateLastFoundByte(vMatches);
+ return (int)(index) - Vector<byte>.Count + LocateLastFoundByte(vMatches);
}
- if ((int)(byte*)index > 0)
+ if ((byte*)index > (byte*)0)
{
nLength = index;
goto SequentialScan;
if (Unsafe.AreSame(ref first, ref second))
goto Equal;
- nuint i = (nuint)0; // Explicit conversion required for netfx (NUInt)
- nuint n = length;
+ IntPtr i = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
+ IntPtr n = (IntPtr)(void*)length;
#if !netstandard11
- if (Vector.IsHardwareAccelerated && n >= (nuint)Vector<byte>.Count)
+ if (Vector.IsHardwareAccelerated && (byte*)n >= (byte*)Vector<byte>.Count)
{
- n -= (nuint)Vector<byte>.Count;
- while (n > i)
+ n -= Vector<byte>.Count;
+ while ((byte*)n > (byte*)i)
{
- if (Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref first, (IntPtr)i)) !=
- Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref second, (IntPtr)i)))
+ if (Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref first, i)) !=
+ Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref second, i)))
{
goto NotEqual;
}
- i += (nuint)Vector<byte>.Count;
+ i += Vector<byte>.Count;
}
- return Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref first, (IntPtr)n)) ==
- Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref second, (IntPtr)n));
+ return Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref first, n)) ==
+ Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref second, n));
}
#endif
- if (n >= (nuint)sizeof(UIntPtr))
+ if ((byte*)n >= (byte*)sizeof(UIntPtr))
{
- n -= (nuint)sizeof(UIntPtr);
- while (n > i)
+ n -= sizeof(UIntPtr);
+ while ((byte*)n > (byte*)i)
{
- if (Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref first, (IntPtr)i)) !=
- Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref second, (IntPtr)i)))
+ if (Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref first, i)) !=
+ Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref second, i)))
{
goto NotEqual;
}
- i += (nuint)sizeof(UIntPtr);
+ i += sizeof(UIntPtr);
}
- return Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref first, (IntPtr)n)) ==
- Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref second, (IntPtr)n));
+ return Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref first, n)) ==
+ Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref second, n));
}
- while (n > i)
+ while ((byte*)n > (byte*)i)
{
- if (Unsafe.AddByteOffset(ref first, (IntPtr)i) != Unsafe.AddByteOffset(ref second, (IntPtr)i))
+ if (Unsafe.AddByteOffset(ref first, i) != Unsafe.AddByteOffset(ref second, i))
goto NotEqual;
i += 1;
}
if (Unsafe.AreSame(ref first, ref second))
goto Equal;
- nuint minLength = (nuint)firstLength;
- if (minLength > (nuint)secondLength) minLength = (nuint)secondLength;
+ IntPtr minLength = (IntPtr)((firstLength < secondLength) ? firstLength : secondLength);
- nuint i = (nuint)0; // Explicit conversion required for netfx (NUInt)
- nuint n = minLength;
+ IntPtr i = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
+ IntPtr n = (IntPtr)(void*)minLength;
#if !netstandard11
- if (Vector.IsHardwareAccelerated && n > (nuint)Vector<byte>.Count)
+ if (Vector.IsHardwareAccelerated && (byte*)n > (byte*)Vector<byte>.Count)
{
- n -= (nuint)Vector<byte>.Count;
- while (n > i)
+ n -= Vector<byte>.Count;
+ while ((byte*)n > (byte*)i)
{
- if (Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref first, (IntPtr)i)) !=
- Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref second, (IntPtr)i)))
+ if (Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref first, i)) !=
+ Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref second, i)))
{
goto NotEqual;
}
- i += (nuint)Vector<byte>.Count;
+ i += Vector<byte>.Count;
}
goto NotEqual;
}
#endif
- if (n > (nuint)sizeof(UIntPtr))
+ if ((byte*)n > (byte*)sizeof(UIntPtr))
{
- n -= (nuint)sizeof(UIntPtr);
- while (n > i)
+ n -= sizeof(UIntPtr);
+ while ((byte*)n > (byte*)i)
{
- if (Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref first, (IntPtr)i)) !=
- Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref second, (IntPtr)i)))
+ if (Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref first, i)) !=
+ Unsafe.ReadUnaligned<UIntPtr>(ref Unsafe.AddByteOffset(ref second, i)))
{
goto NotEqual;
}
- i += (nuint)sizeof(UIntPtr);
+ i += sizeof(UIntPtr);
}
}
NotEqual: // Workaround for https://github.com/dotnet/coreclr/issues/13549
- while (minLength > i)
+ while ((byte*)minLength > (byte*)i)
{
- int result = Unsafe.AddByteOffset(ref first, (IntPtr)i).CompareTo(Unsafe.AddByteOffset(ref second, (IntPtr)i));
+ int result = Unsafe.AddByteOffset(ref first, i).CompareTo(Unsafe.AddByteOffset(ref second, i));
if (result != 0) return result;
i += 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int LocateFirstFoundByte(ulong match)
{
- unchecked
- {
- // 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);
- }
+ // 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);
}
#endif