Skip to content

Commit

Permalink
Add some comments to SpanHelpers.Char IndexOf and LastIndexOf (dotnet…
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsonkhan authored and stephentoub committed Apr 6, 2018
1 parent 44be29b commit 69b8b73
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/mscorlib/shared/System/SpanHelpers.Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ public static unsafe int IndexOf(ref char searchSpace, char value, int length)
#if !netstandard11
if (Vector.IsHardwareAccelerated && length >= Vector<ushort>.Count * 2)
{
// Figure out how many characters to read sequentially until we are vector aligned
// This is equivalent to:
// unaligned = ((int)pCh % Unsafe.SizeOf<Vector<ushort>>()) / elementsPerByte
// length = (Vector<ushort>.Count - unaligned) % Vector<ushort>.Count
const int elementsPerByte = sizeof(ushort) / sizeof(byte);
int unaligned = ((int)pCh & (Unsafe.SizeOf<Vector<ushort>>() - 1)) / elementsPerByte;
length = ((Vector<ushort>.Count - unaligned) & (Vector<ushort>.Count - 1));
length = (Vector<ushort>.Count - unaligned) & (Vector<ushort>.Count - 1);
}
SequentialScan:
#endif
Expand Down Expand Up @@ -129,6 +133,9 @@ public static unsafe int IndexOf(ref char searchSpace, char value, int length)
// the JIT to see that the code is unreachable and eliminate it when the platform does not have hardware accelerated.
if (Vector.IsHardwareAccelerated && pCh < pEndCh)
{
// Get the highest multiple of Vector<ushort>.Count that is within the search space.
// That will be how many times we iterate in the loop below.
// This is equivalent to: length = Vector<ushort>.Count * ((int)(pEndCh - pCh) / Vector<ushort>.Count)
length = (int)((pEndCh - pCh) & ~(Vector<ushort>.Count - 1));

// Get comparison Vector
Expand Down Expand Up @@ -180,8 +187,10 @@ public static unsafe int LastIndexOf(ref char searchSpace, char value, int lengt
#if !netstandard11
if (Vector.IsHardwareAccelerated && length >= Vector<ushort>.Count * 2)
{
// Figure out how many characters to read sequentially from the end until we are vector aligned
// This is equivalent to: length = ((int)pCh % Unsafe.SizeOf<Vector<ushort>>()) / elementsPerByte
const int elementsPerByte = sizeof(ushort) / sizeof(byte);
length = (((int)pCh & (Unsafe.SizeOf<Vector<ushort>>() - 1)) / elementsPerByte);
length = ((int)pCh & (Unsafe.SizeOf<Vector<ushort>>() - 1)) / elementsPerByte;
}
SequentialScan:
#endif
Expand Down Expand Up @@ -213,6 +222,9 @@ public static unsafe int LastIndexOf(ref char searchSpace, char value, int lengt
// the JIT to see that the code is unreachable and eliminate it when the platform does not have hardware accelerated.
if (Vector.IsHardwareAccelerated && pCh > pEndCh)
{
// Get the highest multiple of Vector<ushort>.Count that is within the search space.
// That will be how many times we iterate in the loop below.
// This is equivalent to: length = Vector<ushort>.Count * ((int)(pCh - pEndCh) / Vector<ushort>.Count)
length = (int)((pCh - pEndCh) & ~(Vector<ushort>.Count - 1));

// Get comparison Vector
Expand Down

0 comments on commit 69b8b73

Please sign in to comment.