diff options
author | Ahson Khan <ahkha@microsoft.com> | 2018-04-06 16:47:03 +0300 |
---|---|---|
committer | Anirudh Agnihotry <anirudhagnihotry098@gmail.com> | 2018-04-09 23:51:01 +0300 |
commit | 4ef69a4340714f549d195a845e5e58e1d21b78c5 (patch) | |
tree | 3e294c321985d2bbe328704efa10bee98516bf1d | |
parent | 69d9ecc3dfcd2fcc93ff0cb2adb33b0f2a6fd5da (diff) |
Add some comments to SpanHelpers.Char IndexOf and LastIndexOf (#17447)
Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
-rw-r--r-- | src/Common/src/CoreLib/System/SpanHelpers.Char.cs | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/Common/src/CoreLib/System/SpanHelpers.Char.cs b/src/Common/src/CoreLib/System/SpanHelpers.Char.cs index 6ca215d6a3..51ace58a39 100644 --- a/src/Common/src/CoreLib/System/SpanHelpers.Char.cs +++ b/src/Common/src/CoreLib/System/SpanHelpers.Char.cs @@ -93,9 +93,13 @@ namespace System #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 @@ -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<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 @@ -180,8 +187,10 @@ namespace System #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 @@ -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<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 |