// File: xocoder[P]m_kcptun/vendor/cm256/gf256.cpp
// Function: gf256_addset_mem
void gf256_addset_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)
    {
        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*)z16,     veorq_u8(x0, y0));
            vst1q_u8((uint8_t*)(z16 + 1), veorq_u8(x1, y1));
            vst1q_u8((uint8_t*)(z16 + 2), veorq_u8(x2, y2));
            vst1q_u8((uint8_t*)(z16 + 3), veorq_u8(x3, y3));

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

        // Handle multiples of 16 bytes
        while (bytes >= 16)
        {
            // z[i] = x[i] xor y[i]
            vst1q_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];

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

        bytes -= (count * 8);
    }
#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(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);
    }
    else
# endif // GF256_TRY_AVX2
    {
        // Handle multiples of 64 bytes
        while (bytes >= 64)
        {
            GF256_M128 x0 = _mm_loadu_si128(x16);
            GF256_M128 x1 = _mm_loadu_si128(x16 + 1);
            GF256_M128 x2 = _mm_loadu_si128(x16 + 2);
            GF256_M128 x3 = _mm_loadu_si128(x16 + 3);
            GF256_M128 y0 = _mm_loadu_si128(y16);
            GF256_M128 y1 = _mm_loadu_si128(y16 + 1);
            GF256_M128 y2 = _mm_loadu_si128(y16 + 2);
            GF256_M128 y3 = _mm_loadu_si128(y16 + 3);

            _mm_storeu_si128(z16,     _mm_xor_si128(x0, y0));
            _mm_storeu_si128(z16 + 1, _mm_xor_si128(x1, y1));
            _mm_storeu_si128(z16 + 2, _mm_xor_si128(x2, y2));
            _mm_storeu_si128(z16 + 3, _mm_xor_si128(x3, y3));

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

    // Handle multiples of 16 bytes
    while (bytes >= 16)
    {
        // z[i] = x[i] xor y[i]
        _mm_storeu_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 $0x230, %rsp            # imm = 0x230
movq %rdi, -0x198(%rbp)
movq %rsi, -0x1a0(%rbp)
movq %rdx, -0x1a8(%rbp)
movl %ecx, -0x1ac(%rbp)
movq -0x198(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x1a0(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movq -0x1a8(%rbp), %rax
movq %rax, -0x1c8(%rbp)
cmpl $0x40, -0x1ac(%rbp)
jl 0x11597
movq -0x1c0(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x1e0(%rbp)
movq -0x1c0(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x1f0(%rbp)
movq -0x1c0(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x200(%rbp)
movq -0x1c0(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x210(%rbp)
movq -0x1c8(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x220(%rbp)
movq -0x1c8(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x230(%rbp)
movq -0x1c8(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x240(%rbp)
movq -0x1c8(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, -0x250(%rbp)
movq -0x1b8(%rbp), %rax
movdqa -0x1e0(%rbp), %xmm1
movdqa -0x220(%rbp), %xmm0
movdqa %xmm1, -0xb0(%rbp)
movdqa %xmm0, -0xc0(%rbp)
movdqa -0xb0(%rbp), %xmm0
pxor -0xc0(%rbp), %xmm0
movq %rax, -0x8(%rbp)
movdqa %xmm0, -0x20(%rbp)
movdqa -0x20(%rbp), %xmm0
movq -0x8(%rbp), %rax
movdqu %xmm0, (%rax)
movq -0x1b8(%rbp), %rax
addq $0x10, %rax
movdqa -0x1f0(%rbp), %xmm1
movdqa -0x230(%rbp), %xmm0
movdqa %xmm1, -0xd0(%rbp)
movdqa %xmm0, -0xe0(%rbp)
movdqa -0xd0(%rbp), %xmm0
pxor -0xe0(%rbp), %xmm0
movq %rax, -0x28(%rbp)
movdqa %xmm0, -0x40(%rbp)
movdqa -0x40(%rbp), %xmm0
movq -0x28(%rbp), %rax
movdqu %xmm0, (%rax)
movq -0x1b8(%rbp), %rax
addq $0x20, %rax
movdqa -0x200(%rbp), %xmm1
movdqa -0x240(%rbp), %xmm0
movdqa %xmm1, -0xf0(%rbp)
movdqa %xmm0, -0x100(%rbp)
movdqa -0xf0(%rbp), %xmm0
pxor -0x100(%rbp), %xmm0
movq %rax, -0x48(%rbp)
movdqa %xmm0, -0x60(%rbp)
movdqa -0x60(%rbp), %xmm0
movq -0x48(%rbp), %rax
movdqu %xmm0, (%rax)
movq -0x1b8(%rbp), %rax
addq $0x30, %rax
movdqa -0x210(%rbp), %xmm1
movdqa -0x250(%rbp), %xmm0
movdqa %xmm1, -0x110(%rbp)
movdqa %xmm0, -0x120(%rbp)
movdqa -0x110(%rbp), %xmm0
pxor -0x120(%rbp), %xmm0
movq %rax, -0x68(%rbp)
movdqa %xmm0, -0x80(%rbp)
movdqa -0x80(%rbp), %xmm0
movq -0x68(%rbp), %rax
movdqu %xmm0, (%rax)
movl -0x1ac(%rbp), %eax
subl $0x40, %eax
movl %eax, -0x1ac(%rbp)
movq -0x1c0(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x1c0(%rbp)
movq -0x1c8(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x1c8(%rbp)
movq -0x1b8(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x1b8(%rbp)
jmp 0x112e0
jmp 0x11599
cmpl $0x10, -0x1ac(%rbp)
jl 0x1166b
movq -0x1b8(%rbp), %rax
movq -0x1c0(%rbp), %rcx
movq %rcx, -0x188(%rbp)
movq -0x188(%rbp), %rcx
movdqu (%rcx), %xmm1
movq -0x1c8(%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 -0x1ac(%rbp), %eax
subl $0x10, %eax
movl %eax, -0x1ac(%rbp)
movq -0x1c0(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x1c0(%rbp)
movq -0x1c8(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x1c8(%rbp)
movq -0x1b8(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x1b8(%rbp)
jmp 0x11599
movq -0x1b8(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x1c0(%rbp), %rax
movq %rax, -0x260(%rbp)
movq -0x1c8(%rbp), %rax
movq %rax, -0x268(%rbp)
movl -0x1ac(%rbp), %eax
andl $0x8, %eax
movl %eax, -0x26c(%rbp)
cmpl $0x0, -0x26c(%rbp)
je 0x116f5
movq -0x258(%rbp), %rax
movq %rax, -0x278(%rbp)
movq -0x260(%rbp), %rax
movq %rax, -0x280(%rbp)
movq -0x268(%rbp), %rax
movq %rax, -0x288(%rbp)
movq -0x280(%rbp), %rax
movq (%rax), %rcx
movq -0x288(%rbp), %rax
xorq (%rax), %rcx
movq -0x278(%rbp), %rax
movq %rcx, (%rax)
movl -0x1ac(%rbp), %eax
andl $0x4, %eax
movl %eax, -0x28c(%rbp)
cmpl $0x0, -0x28c(%rbp)
je 0x11770
movq -0x258(%rbp), %rax
movslq -0x26c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x298(%rbp)
movq -0x260(%rbp), %rax
movslq -0x26c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x2a0(%rbp)
movq -0x268(%rbp), %rax
movslq -0x26c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x2a8(%rbp)
movq -0x2a0(%rbp), %rax
movl (%rax), %ecx
movq -0x2a8(%rbp), %rax
xorl (%rax), %ecx
movq -0x298(%rbp), %rax
movl %ecx, (%rax)
movl -0x26c(%rbp), %eax
movl -0x28c(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x2ac(%rbp)
movl -0x1ac(%rbp), %eax
andl $0x3, %eax
movl %eax, -0x2b0(%rbp)
subl $0x1, %eax
je 0x1184c
jmp 0x1179e
movl -0x2b0(%rbp), %eax
subl $0x2, %eax
je 0x11804
jmp 0x117ab
movl -0x2b0(%rbp), %eax
subl $0x3, %eax
jne 0x11885
jmp 0x117bc
movq -0x260(%rbp), %rax
movl -0x2ac(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x268(%rbp), %rcx
movl -0x2ac(%rbp), %edx
addl $0x2, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq -0x258(%rbp), %rax
movl -0x2ac(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x260(%rbp), %rax
movl -0x2ac(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x268(%rbp), %rcx
movl -0x2ac(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq -0x258(%rbp), %rax
movl -0x2ac(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x260(%rbp), %rax
movslq -0x2ac(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0x268(%rbp), %rcx
movslq -0x2ac(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq -0x258(%rbp), %rax
movslq -0x2ac(%rbp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x11887
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq