Repository: dotnet/runtime
File: src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf16Utility.Validation.cs (C#)

Code before:
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;

namespace System.Text.Unicode
{
    internal static unsafe partial class Utf16Utility
    {
        // Returns &inputBuffer[inputLength] if the input buffer is valid.
        /// <summary>
        /// Given an input buffer <paramref name="pInputBuffer"/> of char length <paramref name="inputLength"/>,
        /// returns a pointer to where the first invalid data appears in <paramref name="pInputBuffer"/>.
        /// </summary>
        /// <remarks>
        /// Returns a pointer to the end of <paramref name="pInputBuffer"/> if the buffer is well-formed.
        /// </remarks>
        public static char* GetPointerToFirstInvalidChar(char* pInputBuffer, int inputLength, out long utf8CodeUnitCountAdjustment, out int scalarCountAdjustment)
        {
            Debug.Assert(inputLength >= 0, "Input length must not be negative.");
            Debug.Assert(pInputBuffer != null || inputLength == 0, "Input length must be zero if input buffer pointer is null.");

            // First, we'll handle the common case of all-ASCII. If this is able to
            // consume the entire buffer, we'll skip the remainder of this method's logic.

            int numAsciiCharsConsumedJustNow = (int)Ascii.GetIndexOfFirstNonAsciiChar(pInputBuffer, (uint)inputLength);
            Debug.Assert(0 <= numAsciiCharsConsumedJustNow && numAsciiCharsConsumedJustNow <= inputLength);

            pInputBuffer += (uint)numAsciiCharsConsumedJustNow;
            inputLength -= numAsciiCharsConsumedJustNow;

            if (inputLength == 0)
            {
                utf8CodeUnitCountAdjustment = 0;
                scalarCountAdjustment = 0;
                return pInputBuffer;
            }

            // If we got here, it means we saw some non-ASCII data, so within our
            // vectorized code paths below we'll handle all non-surrogate UTF-16
            // code points branchlessly. We'll only branch if we see surrogates.
            //
            // We still optimistically assume the data is mostly ASCII. This means that the
            // number of UTF-8 code units and the number of scalars almost matches the number
            // of UTF-16 code units. As we go through the input and find non-ASCII
            // characters, we'll keep track of these "adjustment" fixups. To get the
            // total number of UTF-8 code units required to encode the input data, add
            // the UTF-8 code unit count adjustment to the number of UTF-16 code units
            // seen.  To get the total number of scalars present in the input data,
            // add the scalar count adjustment to the number of UTF-16 code units seen.

Review comment (performance):
It seems that we call GetSurrogateMask for LessThan input (hence, we have a guarantee that all elements are either 0 or allbitsset) -- we have a long standing issue to optimize this in JIT instead (to avoid special casing all ExtractMostSignificantBits calls in the managed code), cc @tannergooding 

Code after:
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;

namespace System.Text.Unicode
{
    internal static unsafe partial class Utf16Utility
    {

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static nuint GetSurrogateMask(Vector128<ushort> cmp)
        {
            // Convert the comparison result to a scalar surrogate mask.
            // The elements in 'cmp' should be either all bits set or zero.

            if (AdvSimd.Arm64.IsSupported)
            {
                // Since ExtractMostSignificantBits is very slow on AdvSimd,
                // we use a 64-bit value to encode the mask, where each byte represents one element:
                //   0x01 for all bits set, 0x00 for zero.
                ulong mask = AdvSimd.Arm64.UnzipOdd(cmp.AsByte(), cmp.AsByte()).AsUInt64().ToScalar();
                return (nuint)(mask & 0x0101010101010101u);
            }

            // Otherwise, encode the mask with 8-bits (one byte), where each bit represents one element.
            return cmp.ExtractMostSignificantBits();
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static bool IsSurrogatesMatch(nuint maskHigh, nuint maskLow)
        {
            // Make sure that each high surrogate is followed by a low surrogate character,
            // and each low surrogate follows a high surrogate character.
            // The last character is discarded as it will be checked by 'IsLastCharHighSurrogate'.
            // The first character must not be a low surrogate. This is checked by matching
            // 'maskLow' aganist the zeros inserted after shifting 'maskHigh' to the left.

            if (AdvSimd.Arm64.IsSupported)
            {
                // Each surrogate character is 8 bits apart.
                return (maskHigh << 8) == maskLow;
            }
            // Each surrogate character is 1 bit apart.
            return (byte)(maskHigh << 1) == (byte)maskLow;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static bool IsLastCharHighSurrogate(nuint maskHigh)
        {
            if (AdvSimd.Arm64.IsSupported)
            {