Repository: microsoft/terminal
File: src/terminal/parser/stateMachine.cpp (C++)

Code before:
        const auto z = _mm_setzero_si128();

        // Dealing with unsigned numbers in SSE2 is annoying because it has poor support for that.
        // We'll use subtractions with saturation ("SubS") to work around that. A check like
        // a < b can be implemented as "max(0, a - b) == 0" and "max(0, a - b)" is what "SubS" is.

        // Check for (wch < 0x20)
        auto a = _mm_subs_epu16(wch, _mm_set1_epi16(0x1f));
        // Check for "(wch >= 0x7f && wch <= 0x9f)" by adding 0x10000-0x7f, which overflows to a
        // negative number if "wch >= 0x7f" and then subtracting 0x9f-0x7f with saturation to an
        // unsigned number (= can't go lower than 0), which results in all numbers up to 0x9f to be 0.
        auto b = _mm_subs_epu16(_mm_add_epi32(wch, _mm_set1_epi16(static_cast<short>(0xff81))), _mm_set1_epi16(0x20));
        a = _mm_cmpeq_epi16(a, z);
        b = _mm_cmpeq_epi16(b, z);

        const auto c = _mm_or_si128(a, b);
        const auto mask = _mm_movemask_epi8(c);

        if (mask)
        {
            unsigned long offset;
            _BitScanForward(&offset, mask);
            it += offset / 2;
            return it - data;
        }
    }

    return findActionableFromGroundPlain(data, data + count, it);

#elif defined(TIL_ARM_NEON_INTRINSICS)

    auto it = data;
    uint64_t mask;

    for (const auto end = data + (count & ~size_t{ 7 }); it < end;)
    {
        const auto wch = vld1q_u16(it);
        const auto a = vcleq_u16(wch, vdupq_n_u16(0x1f));
        const auto b = vcleq_u16(vsubq_u16(wch, vdupq_n_u16(0x7f)), vdupq_n_u16(0x20));
        const auto c = vorrq_u16(a, b);

        mask = vgetq_lane_u64(c, 0);
        if (mask)
        {
            goto exitWithMask;
        }
        it += 4;

        mask = vgetq_lane_u64(c, 1);
        if (mask)
        {

Review comment (suggestion):
prefix:
* `_m_`: MMX
* `_mm_`: MMX / SSE / SSE2
* `_mm256_`: AVX / AVX2

suffix:
* `epi`: signed integer
* `epu`: unsigned integer
* `epi8`: component size = 8 bit
* `epi16`: component size = 16 bit
* `si128`: component size = all 128 bit
  component = in which granularity the operations are executed. byte-wise comparisons? wchar_t-wise comparisons? etc.

name:
* `loadu`: load 128 bits from pointer, unaligned
* `setzero`: 128 bits, all zero
* `subs`: subtraction with saturation (`max(0, a - b)`)
* `cmpeq`: compare equals
* `or`: bitwise or
* `movemask`: take the lowest bit of each component and store it in a single `int`
  a SSE vector like 0xFFFF00FF00FF0000FFFF00FF00FF0000 results in `1101010011010100`
  combined with `cmpeq` and `_BitScanForward` it lets you find the first component where the comparison was successful

Code after:
        const auto wch = _mm_loadu_si128(reinterpret_cast<const __m128i*>(it));
        const auto z = _mm_setzero_si128();

        // Dealing with unsigned numbers in SSE2 is annoying because it has poor support for that.
        // We'll use subtractions with saturation ("SubS") to work around that. A check like
        // a < b can be implemented as "max(0, a - b) == 0" and "max(0, a - b)" is what "SubS" is.

        // Check for (wch < 0x20)
        auto a = _mm_subs_epu16(wch, _mm_set1_epi16(0x1f));
        // Check for "((wch - 0x7f) <= 0x20)" by adding 0x10000-0x7f, which overflows to a
        // negative number if "wch >= 0x7f" and then subtracting 0x9f-0x7f with saturation to an
        // unsigned number (= can't go lower than 0), which results in all numbers up to 0x9f to be 0.
        auto b = _mm_subs_epu16(_mm_add_epi16(wch, _mm_set1_epi16(static_cast<short>(0xff81))), _mm_set1_epi16(0x20));
        a = _mm_cmpeq_epi16(a, z);
        b = _mm_cmpeq_epi16(b, z);

        const auto c = _mm_or_si128(a, b);
        const auto mask = _mm_movemask_epi8(c);

        if (mask)
        {
            unsigned long offset;
            _BitScanForward(&offset, mask);
            it += offset / 2;
            return it - data;
        }
    }

    return findActionableFromGroundPlain(data, data + count, it);

#elif defined(TIL_ARM_NEON_INTRINSICS)

    auto it = data;
    uint64_t mask;

    for (const auto end = data + (count & ~size_t{ 7 }); it < end;)
    {
        const auto wch = vld1q_u16(it);
        const auto a = vcleq_u16(wch, vdupq_n_u16(0x1f));
        const auto b = vcleq_u16(vsubq_u16(wch, vdupq_n_u16(0x7f)), vdupq_n_u16(0x20));
        const auto c = vorrq_u16(a, b);

        mask = vgetq_lane_u64(c, 0);
        if (mask)
        {
            goto exitWithMask;
        }
        it += 4;

        mask = vgetq_lane_u64(c, 1);
        if (mask)