From 69b8b732e5268bedc75cee99ecc958c903723cdf Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Fri, 6 Apr 2018 06:47:03 -0700 Subject: [PATCH] Add some comments to SpanHelpers.Char IndexOf and LastIndexOf (#17447) --- src/mscorlib/shared/System/SpanHelpers.Char.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/mscorlib/shared/System/SpanHelpers.Char.cs b/src/mscorlib/shared/System/SpanHelpers.Char.cs index 6ca215d..51ace58 100644 --- a/src/mscorlib/shared/System/SpanHelpers.Char.cs +++ b/src/mscorlib/shared/System/SpanHelpers.Char.cs @@ -93,9 +93,13 @@ namespace System #if !netstandard11 if (Vector.IsHardwareAccelerated && length >= Vector.Count * 2) { + // Figure out how many characters to read sequentially until we are vector aligned + // This is equivalent to: + // unaligned = ((int)pCh % Unsafe.SizeOf>()) / elementsPerByte + // length = (Vector.Count - unaligned) % Vector.Count const int elementsPerByte = sizeof(ushort) / sizeof(byte); int unaligned = ((int)pCh & (Unsafe.SizeOf>() - 1)) / elementsPerByte; - length = ((Vector.Count - unaligned) & (Vector.Count - 1)); + length = (Vector.Count - unaligned) & (Vector.Count - 1); } SequentialScan: #endif @@ -129,6 +133,9 @@ namespace System // 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.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.Count * ((int)(pEndCh - pCh) / Vector.Count) length = (int)((pEndCh - pCh) & ~(Vector.Count - 1)); // Get comparison Vector @@ -180,8 +187,10 @@ namespace System #if !netstandard11 if (Vector.IsHardwareAccelerated && length >= Vector.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>()) / elementsPerByte const int elementsPerByte = sizeof(ushort) / sizeof(byte); - length = (((int)pCh & (Unsafe.SizeOf>() - 1)) / elementsPerByte); + length = ((int)pCh & (Unsafe.SizeOf>() - 1)) / elementsPerByte; } SequentialScan: #endif @@ -213,6 +222,9 @@ namespace System // 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.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.Count * ((int)(pCh - pEndCh) / Vector.Count) length = (int)((pCh - pEndCh) & ~(Vector.Count - 1)); // Get comparison Vector -- 2.7.4