// File: xocoder[P]m_kcptun/vendor/cm256/gf256.cpp
// Function: gf256_add_mem
void gf256_add_mem(void * GF256_RESTRICT vx,
                              const void * GF256_RESTRICT vy, int bytes)
{
    GF256_M128 * GF256_RESTRICT x16 = reinterpret_cast<GF256_M128 *>(vx);
    const GF256_M128 * GF256_RESTRICT y16 = reinterpret_cast<const GF256_M128 *>(vy);

#if defined(GF256_TARGET_MOBILE)
# if defined(GF256_TRY_NEON)
    // Handle multiples of 64 bytes
    if (CpuHasNeon)
    {
        while (bytes >= 64)
        {
            GF256_M128 x0 = vld1q_u8((uint8_t*) x16);
            GF256_M128 x1 = vld1q_u8((uint8_t*)(x16 + 1) );
            GF256_M128 x2 = vld1q_u8((uint8_t*)(x16 + 2) );
            GF256_M128 x3 = vld1q_u8((uint8_t*)(x16 + 3) );
            GF256_M128 y0 = vld1q_u8((uint8_t*)y16);
            GF256_M128 y1 = vld1q_u8((uint8_t*)(y16 + 1));
            GF256_M128 y2 = vld1q_u8((uint8_t*)(y16 + 2));
            GF256_M128 y3 = vld1q_u8((uint8_t*)(y16 + 3));

            vst1q_u8((uint8_t*)x16,     veorq_u8(x0, y0));
            vst1q_u8((uint8_t*)(x16 + 1), veorq_u8(x1, y1));
            vst1q_u8((uint8_t*)(x16 + 2), veorq_u8(x2, y2));
            vst1q_u8((uint8_t*)(x16 + 3), veorq_u8(x3, y3));

            bytes -= 64, x16 += 4, y16 += 4;
        }

        // Handle multiples of 16 bytes
        while (bytes >= 16)
        {
            GF256_M128 x0 = vld1q_u8((uint8_t*)x16);
            GF256_M128 y0 = vld1q_u8((uint8_t*)y16);

            vst1q_u8((uint8_t*)x16, veorq_u8(x0, y0));

            bytes -= 16, ++x16, ++y16;
        }
    }
    else
# endif // GF256_TRY_NEON
    {
        uint64_t * GF256_RESTRICT x8 = reinterpret_cast<uint64_t *>(x16);
        const uint64_t * GF256_RESTRICT y8 = reinterpret_cast<const uint64_t *>(y16);

        const unsigned count = (unsigned)bytes / 8;
        for (unsigned ii = 0; ii < count; ++ii)
            x8[ii] ^= y8[ii];

        x16 = reinterpret_cast<GF256_M128 *>(x8 + count);
        y16 = reinterpret_cast<const GF256_M128 *>(y8 + count);

        bytes -= (count * 8);
    }
#else // GF256_TARGET_MOBILE
# if defined(GF256_TRY_AVX2)
    if (CpuHasAVX2)
    {
        GF256_M256 * GF256_RESTRICT x32 = reinterpret_cast<GF256_M256 *>(x16);
        const GF256_M256 * GF256_RESTRICT y32 = reinterpret_cast<const GF256_M256 *>(y16);

        while (bytes >= 128)
        {
            GF256_M256 x0 = _mm256_loadu_si256(x32);
            GF256_M256 y0 = _mm256_loadu_si256(y32);
            x0 = _mm256_xor_si256(x0, y0);
            GF256_M256 x1 = _mm256_loadu_si256(x32 + 1);
            GF256_M256 y1 = _mm256_loadu_si256(y32 + 1);
            x1 = _mm256_xor_si256(x1, y1);
            GF256_M256 x2 = _mm256_loadu_si256(x32 + 2);
            GF256_M256 y2 = _mm256_loadu_si256(y32 + 2);
            x2 = _mm256_xor_si256(x2, y2);
            GF256_M256 x3 = _mm256_loadu_si256(x32 + 3);
            GF256_M256 y3 = _mm256_loadu_si256(y32 + 3);
            x3 = _mm256_xor_si256(x3, y3);

            _mm256_storeu_si256(x32, x0);
            _mm256_storeu_si256(x32 + 1, x1);
            _mm256_storeu_si256(x32 + 2, x2);
            _mm256_storeu_si256(x32 + 3, x3);

            bytes -= 128, x32 += 4, y32 += 4;
        }

        // Handle multiples of 32 bytes
        while (bytes >= 32)
        {
            // x[i] = x[i] xor y[i]
            _mm256_storeu_si256(x32,
                _mm256_xor_si256(
                    _mm256_loadu_si256(x32),
                    _mm256_loadu_si256(y32)));

            bytes -= 32, ++x32, ++y32;
        }

        x16 = reinterpret_cast<GF256_M128 *>(x32);
        y16 = reinterpret_cast<const GF256_M128 *>(y32);
    }
    else
# endif // GF256_TRY_AVX2
    {
        while (bytes >= 64)
        {
            GF256_M128 x0 = _mm_loadu_si128(x16);
            GF256_M128 y0 = _mm_loadu_si128(y16);
            x0 = _mm_xor_si128(x0, y0);
            GF256_M128 x1 = _mm_loadu_si128(x16 + 1);
            GF256_M128 y1 = _mm_loadu_si128(y16 + 1);
            x1 = _mm_xor_si128(x1, y1);
            GF256_M128 x2 = _mm_loadu_si128(x16 + 2);
            GF256_M128 y2 = _mm_loadu_si128(y16 + 2);
            x2 = _mm_xor_si128(x2, y2);
            GF256_M128 x3 = _mm_loadu_si128(x16 + 3);
            GF256_M128 y3 = _mm_loadu_si128(y16 + 3);
            x3 = _mm_xor_si128(x3, y3);

            _mm_storeu_si128(x16, x0);
            _mm_storeu_si128(x16 + 1, x1);
            _mm_storeu_si128(x16 + 2, x2);
            _mm_storeu_si128(x16 + 3, x3);

            bytes -= 64, x16 += 4, y16 += 4;
        }
    }
#endif // GF256_TARGET_MOBILE

#if !defined(GF256_TARGET_MOBILE)
    // Handle multiples of 16 bytes
    while (bytes >= 16)
    {
        // x[i] = x[i] xor y[i]
        _mm_storeu_si128(x16,
            _mm_xor_si128(
                _mm_loadu_si128(x16),
                _mm_loadu_si128(y16)));

        bytes -= 16, ++x16, ++y16;
    }
#endif

    uint8_t * GF256_RESTRICT x1 = reinterpret_cast<uint8_t *>(x16);
    const uint8_t * GF256_RESTRICT y1 = reinterpret_cast<const uint8_t *>(y16);

    // Handle a block of 8 bytes
    const int eight = bytes & 8;
    if (eight)
    {
        uint64_t * GF256_RESTRICT x8 = reinterpret_cast<uint64_t *>(x1);
        const uint64_t * GF256_RESTRICT y8 = reinterpret_cast<const uint64_t *>(y1);
        *x8 ^= *y8;
    }

    // Handle a block of 4 bytes
    const int four = bytes & 4;
    if (four)
    {
        uint32_t * GF256_RESTRICT x4 = reinterpret_cast<uint32_t *>(x1 + eight);
        const uint32_t * GF256_RESTRICT y4 = reinterpret_cast<const uint32_t *>(y1 + eight);
        *x4 ^= *y4;
    }

    // Handle final bytes
    const int offset = eight + four;
    switch (bytes & 3)
    {
    case 3: x1[offset + 2] ^= y1[offset + 2];
    case 2: x1[offset + 1] ^= y1[offset + 1];
    case 1: x1[offset] ^= y1[offset];
    default:
        break;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x198(%rbp)
movq %rsi, -0x1a0(%rbp)
movl %edx, -0x1a4(%rbp)
movq -0x198(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0x1a0(%rbp), %rax
movq %rax, -0x1b8(%rbp)
cmpl $0x40, -0x1a4(%rbp)
jl 0x10cc0
movq -0x1b0(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x1d0(%rbp)
movq -0x1b8(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x1e0(%rbp)
movdqa -0x1d0(%rbp), %xmm1
movdqa -0x1e0(%rbp), %xmm0
movdqa %xmm1, -0xb0(%rbp)
movdqa %xmm0, -0xc0(%rbp)
movdqa -0xb0(%rbp), %xmm0
pxor -0xc0(%rbp), %xmm0
movdqa %xmm0, -0x1d0(%rbp)
movq -0x1b0(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x1f0(%rbp)
movq -0x1b8(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x200(%rbp)
movdqa -0x1f0(%rbp), %xmm1
movdqa -0x200(%rbp), %xmm0
movdqa %xmm1, -0xd0(%rbp)
movdqa %xmm0, -0xe0(%rbp)
movdqa -0xd0(%rbp), %xmm0
pxor -0xe0(%rbp), %xmm0
movdqa %xmm0, -0x1f0(%rbp)
movq -0x1b0(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x210(%rbp)
movq -0x1b8(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x220(%rbp)
movdqa -0x210(%rbp), %xmm1
movdqa -0x220(%rbp), %xmm0
movdqa %xmm1, -0xf0(%rbp)
movdqa %xmm0, -0x100(%rbp)
movdqa -0xf0(%rbp), %xmm0
pxor -0x100(%rbp), %xmm0
movdqa %xmm0, -0x210(%rbp)
movq -0x1b0(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x230(%rbp)
movq -0x1b8(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x240(%rbp)
movdqa -0x230(%rbp), %xmm1
movdqa -0x240(%rbp), %xmm0
movdqa %xmm1, -0x110(%rbp)
movdqa %xmm0, -0x120(%rbp)
movdqa -0x110(%rbp), %xmm0
pxor -0x120(%rbp), %xmm0
movdqa %xmm0, -0x230(%rbp)
movq -0x1b0(%rbp), %rax
movdqa -0x1d0(%rbp), %xmm0
movq %rax, -0x8(%rbp)
movdqa %xmm0, -0x20(%rbp)
movdqa -0x20(%rbp), %xmm0
movq -0x8(%rbp), %rax
movdqu %xmm0, (%rax)
movq -0x1b0(%rbp), %rax
addq $0x10, %rax
movdqa -0x1f0(%rbp), %xmm0
movq %rax, -0x28(%rbp)
movdqa %xmm0, -0x40(%rbp)
movdqa -0x40(%rbp), %xmm0
movq -0x28(%rbp), %rax
movdqu %xmm0, (%rax)
movq -0x1b0(%rbp), %rax
addq $0x20, %rax
movdqa -0x210(%rbp), %xmm0
movq %rax, -0x48(%rbp)
movdqa %xmm0, -0x60(%rbp)
movdqa -0x60(%rbp), %xmm0
movq -0x48(%rbp), %rax
movdqu %xmm0, (%rax)
movq -0x1b0(%rbp), %rax
addq $0x30, %rax
movdqa -0x230(%rbp), %xmm0
movq %rax, -0x68(%rbp)
movdqa %xmm0, -0x80(%rbp)
movdqa -0x80(%rbp), %xmm0
movq -0x68(%rbp), %rax
movdqu %xmm0, (%rax)
movl -0x1a4(%rbp), %eax
subl $0x40, %eax
movl %eax, -0x1a4(%rbp)
movq -0x1b0(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x1b0(%rbp)
movq -0x1b8(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x1b8(%rbp)
jmp 0x109db
jmp 0x10cc2
cmpl $0x10, -0x1a4(%rbp)
jl 0x10d82
movq -0x1b0(%rbp), %rax
movq -0x1b0(%rbp), %rcx
movq %rcx, -0x188(%rbp)
movq -0x188(%rbp), %rcx
movdqu (%rcx), %xmm1
movq -0x1b8(%rbp), %rcx
movq %rcx, -0x190(%rbp)
movq -0x190(%rbp), %rcx
movdqu (%rcx), %xmm0
movdqa %xmm1, -0x130(%rbp)
movdqa %xmm0, -0x140(%rbp)
movdqa -0x130(%rbp), %xmm0
pxor -0x140(%rbp), %xmm0
movq %rax, -0x88(%rbp)
movdqa %xmm0, -0xa0(%rbp)
movdqa -0xa0(%rbp), %xmm0
movq -0x88(%rbp), %rax
movdqu %xmm0, (%rax)
movl -0x1a4(%rbp), %eax
subl $0x10, %eax
movl %eax, -0x1a4(%rbp)
movq -0x1b0(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x1b0(%rbp)
movq -0x1b8(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x1b8(%rbp)
jmp 0x10cc2
movq -0x1b0(%rbp), %rax
movq %rax, -0x248(%rbp)
movq -0x1b8(%rbp), %rax
movq %rax, -0x250(%rbp)
movl -0x1a4(%rbp), %eax
andl $0x8, %eax
movl %eax, -0x254(%rbp)
cmpl $0x0, -0x254(%rbp)
je 0x10de9
movq -0x248(%rbp), %rax
movq %rax, -0x260(%rbp)
movq -0x250(%rbp), %rax
movq %rax, -0x268(%rbp)
movq -0x268(%rbp), %rax
movq (%rax), %rcx
movq -0x260(%rbp), %rax
xorq (%rax), %rcx
movq %rcx, (%rax)
movl -0x1a4(%rbp), %eax
andl $0x4, %eax
movl %eax, -0x26c(%rbp)
cmpl $0x0, -0x26c(%rbp)
je 0x10e45
movq -0x248(%rbp), %rax
movslq -0x254(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x278(%rbp)
movq -0x250(%rbp), %rax
movslq -0x254(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x280(%rbp)
movq -0x280(%rbp), %rax
movl (%rax), %ecx
movq -0x278(%rbp), %rax
xorl (%rax), %ecx
movl %ecx, (%rax)
movl -0x254(%rbp), %eax
movl -0x26c(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x284(%rbp)
movl -0x1a4(%rbp), %eax
andl $0x3, %eax
movl %eax, -0x288(%rbp)
subl $0x1, %eax
je 0x10ef7
jmp 0x10e73
movl -0x288(%rbp), %eax
subl $0x2, %eax
je 0x10ec4
jmp 0x10e80
movl -0x288(%rbp), %eax
subl $0x3, %eax
jne 0x10f20
jmp 0x10e91
movq -0x250(%rbp), %rax
movl -0x284(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %esi
movq -0x248(%rbp), %rax
movl -0x284(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movq -0x250(%rbp), %rax
movl -0x284(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %esi
movq -0x248(%rbp), %rax
movl -0x284(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movq -0x250(%rbp), %rax
movslq -0x284(%rbp), %rcx
movzbl (%rax,%rcx), %esi
movq -0x248(%rbp), %rax
movslq -0x284(%rbp), %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
jmp 0x10f22
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
nopl (%rax,%rax)