// File: xocoder[P]m_kcptun/vendor/cm256/gf256.cpp
// Function: gf256_add2_mem
void gf256_add2_mem(void * GF256_RESTRICT vz, const void * GF256_RESTRICT vx,
                               const void * GF256_RESTRICT vy, int bytes)
{
    GF256_M128 * GF256_RESTRICT z16 = reinterpret_cast<GF256_M128*>(vz);
    const GF256_M128 * GF256_RESTRICT x16 = reinterpret_cast<const 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)
    {
        // Handle multiples of 16 bytes
        while (bytes >= 16)
        {
            // z[i] = z[i] xor x[i] xor y[i]
            vst1q_u8((uint8_t*)z16,
                veorq_u8(
                    vld1q_u8((uint8_t*)z16),
                    veorq_u8(
                        vld1q_u8((uint8_t*)x16),
                        vld1q_u8((uint8_t*)y16))));

            bytes -= 16, ++x16, ++y16, ++z16;
        }
    }
    else
# endif // GF256_TRY_NEON
    {
        uint64_t * GF256_RESTRICT z8 = reinterpret_cast<uint64_t *>(z16);
        const uint64_t * GF256_RESTRICT x8 = reinterpret_cast<const 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)
            z8[ii] ^= x8[ii] ^ y8[ii];

        z16 = reinterpret_cast<GF256_M128 *>(z8 + count);
        x16 = reinterpret_cast<const GF256_M128 *>(x8 + count);
        y16 = reinterpret_cast<const GF256_M128 *>(y8 + count);
    }
#else // GF256_TARGET_MOBILE
# if defined(GF256_TRY_AVX2)
    if (CpuHasAVX2)
    {
        GF256_M256 * GF256_RESTRICT z32 = reinterpret_cast<GF256_M256 *>(z16);
        const GF256_M256 * GF256_RESTRICT x32 = reinterpret_cast<const GF256_M256 *>(x16);
        const GF256_M256 * GF256_RESTRICT y32 = reinterpret_cast<const GF256_M256 *>(y16);

        const unsigned count = bytes / 32;
        for (unsigned i = 0; i < count; ++i)
        {
            _mm256_storeu_si256(z32 + i,
                _mm256_xor_si256(
                    _mm256_loadu_si256(z32 + i),
                    _mm256_xor_si256(
                        _mm256_loadu_si256(x32 + i),
                        _mm256_loadu_si256(y32 + i))));
        }

        bytes -= count * 32;
        z16 = reinterpret_cast<GF256_M128 *>(z32 + count);
        x16 = reinterpret_cast<const GF256_M128 *>(x32 + count);
        y16 = reinterpret_cast<const GF256_M128 *>(y32 + count);
    }
# endif // GF256_TRY_AVX2

    // Handle multiples of 16 bytes
    while (bytes >= 16)
    {
        // z[i] = z[i] xor x[i] xor y[i]
        _mm_storeu_si128(z16,
            _mm_xor_si128(
                _mm_loadu_si128(z16),
                _mm_xor_si128(
                    _mm_loadu_si128(x16),
                    _mm_loadu_si128(y16))));

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

    uint8_t * GF256_RESTRICT z1 = reinterpret_cast<uint8_t *>(z16);
    const uint8_t * GF256_RESTRICT x1 = reinterpret_cast<const 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 z8 = reinterpret_cast<uint64_t *>(z1);
        const uint64_t * GF256_RESTRICT x8 = reinterpret_cast<const uint64_t *>(x1);
        const uint64_t * GF256_RESTRICT y8 = reinterpret_cast<const uint64_t *>(y1);
        *z8 ^= *x8 ^ *y8;
    }

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

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x80(%rbp)
movq %rsi, -0x88(%rbp)
movq %rdx, -0x90(%rbp)
movl %ecx, -0x94(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpl $0x10, -0x94(%rbp)
jl 0x1104f
movq -0xa0(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq %rcx, -0x68(%rbp)
movq -0x68(%rbp), %rcx
movdqu (%rcx), %xmm1
movq -0xa8(%rbp), %rcx
movq %rcx, -0x70(%rbp)
movq -0x70(%rbp), %rcx
movdqu (%rcx), %xmm2
movq -0xb0(%rbp), %rcx
movq %rcx, -0x78(%rbp)
movq -0x78(%rbp), %rcx
movdqu (%rcx), %xmm0
movdqa %xmm2, -0x30(%rbp)
movdqa %xmm0, -0x40(%rbp)
movdqa -0x30(%rbp), %xmm0
pxor -0x40(%rbp), %xmm0
movdqa %xmm1, -0x50(%rbp)
movdqa %xmm0, -0x60(%rbp)
movdqa -0x50(%rbp), %xmm0
pxor -0x60(%rbp), %xmm0
movq %rax, -0x8(%rbp)
movdqa %xmm0, -0x20(%rbp)
movdqa -0x20(%rbp), %xmm0
movq -0x8(%rbp), %rax
movdqu %xmm0, (%rax)
movl -0x94(%rbp), %eax
subl $0x10, %eax
movl %eax, -0x94(%rbp)
movq -0xa8(%rbp), %rax
addq $0x10, %rax
movq %rax, -0xa8(%rbp)
movq -0xb0(%rbp), %rax
addq $0x10, %rax
movq %rax, -0xb0(%rbp)
movq -0xa0(%rbp), %rax
addq $0x10, %rax
movq %rax, -0xa0(%rbp)
jmp 0x10f7a
movq -0xa0(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xc8(%rbp)
movl -0x94(%rbp), %eax
andl $0x8, %eax
movl %eax, -0xcc(%rbp)
cmpl $0x0, -0xcc(%rbp)
je 0x110dc
movq -0xb8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe0(%rbp), %rax
movq (%rax), %rcx
movq -0xe8(%rbp), %rax
xorq (%rax), %rcx
movq -0xd8(%rbp), %rax
xorq (%rax), %rcx
movq %rcx, (%rax)
movl -0x94(%rbp), %eax
andl $0x4, %eax
movl %eax, -0xec(%rbp)
cmpl $0x0, -0xec(%rbp)
je 0x11159
movq -0xb8(%rbp), %rax
movslq -0xcc(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xf8(%rbp)
movq -0xc0(%rbp), %rax
movslq -0xcc(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x100(%rbp)
movq -0xc8(%rbp), %rax
movslq -0xcc(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x108(%rbp)
movq -0x100(%rbp), %rax
movl (%rax), %ecx
movq -0x108(%rbp), %rax
xorl (%rax), %ecx
movq -0xf8(%rbp), %rax
xorl (%rax), %ecx
movl %ecx, (%rax)
movl -0xcc(%rbp), %eax
movl -0xec(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x10c(%rbp)
movl -0x94(%rbp), %eax
andl $0x3, %eax
movl %eax, -0x110(%rbp)
subl $0x1, %eax
je 0x1123d
jmp 0x11187
movl -0x110(%rbp), %eax
subl $0x2, %eax
je 0x111f1
jmp 0x11194
movl -0x110(%rbp), %eax
subl $0x3, %eax
jne 0x1127a
jmp 0x111a5
movq -0xc0(%rbp), %rax
movl -0x10c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %esi
movq -0xc8(%rbp), %rax
movl -0x10c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
xorl %eax, %esi
movq -0xb8(%rbp), %rax
movl -0x10c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movq -0xc0(%rbp), %rax
movl -0x10c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %esi
movq -0xc8(%rbp), %rax
movl -0x10c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
xorl %eax, %esi
movq -0xb8(%rbp), %rax
movl -0x10c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movq -0xc0(%rbp), %rax
movslq -0x10c(%rbp), %rcx
movzbl (%rax,%rcx), %esi
movq -0xc8(%rbp), %rax
movslq -0x10c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
xorl %eax, %esi
movq -0xb8(%rbp), %rax
movslq -0x10c(%rbp), %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
jmp 0x1127c
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)