// File: /workspace/github_cpp_makefile_star2_O1/WojciechMula[P]utf16utf8/src/sse.cpp
// Function: sse_convert_valid_utf8_to_utf16_lemire(unsigned char const*, unsigned long, unsigned short*)
size_t sse_convert_valid_utf8_to_utf16_lemire(const uint8_t* input, size_t size, uint16_t* output) {
  size_t pos = 0;
  uint16_t *start = output;
  while (pos + 16 <= size) {
    //////////////////////////////
    // Could go faster if we grab large
    // blocks of data, larger than
    // 16 bytes?
    //////////////////////////////
    const __m128i in = _mm_loadu_si128((__m128i *)(input + pos));
    const uint16_t non_ascii_chars = uint16_t(_mm_movemask_epi8(in));
    // ASCII is likely common in many cases, we want a fast path.
    if(non_ascii_chars == 0) {
        // could use _mm256_cvtepu8_epi16/_mm_cvtepu8_epi16 (SSE4.1)
        const __m128i out1 = _mm_unpacklo_epi8(in, _mm_setzero_si128());// order of parameter determines endianness
        _mm_storeu_si128((__m128i*)output, out1);
        output += 8;
        const __m128i out2 = _mm_unpackhi_epi8(in, _mm_setzero_si128());
        _mm_storeu_si128((__m128i*)output, out2);
        output += 8;
        pos += 16;
        continue;
    }
    // 
    // Otherwise, we use an approach where we try to process up to 12 input bytes.
    // Why 12 input bytes and not 16? Because we are concerned with the size of the lookup tables.
    // Also 12 is nicely divisible by two and three.
    //
    //const __m128i maxtwobyte = _mm_set1_epi8(uint8_t(0xdf));
    //uint16_t istwobytes = uint16_t(_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_max_epu8(maxtwobyte, in), maxtwobyte)));
    // The most significant 4 bits (high nibble) can be used to classify the bytes, as
    // to whether they start a new code point.
    const __m128i hn = _mm_set1_epi8(uint8_t(0xF));
    const __m128i in_high_nibbles = _mm_and_si128(hn, _mm_srli_epi16(in, 4));
    // We could remove the UTF8 headers with one instruction:
    //const __m128i masks = _mm_shuffle_epi8(_mm_set_epi8(0xf0, 0xe0, 0xc0, 0xc0, 0x80, 0x80, 0x80, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0), in_high_nibbles);
    const uint16_t start_of_code_point = uint16_t(_mm_movemask_epi8(_mm_shuffle_epi8(_mm_set_epi8(0xff, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), in_high_nibbles)));
    /////////////
    // ASCII characters are the start and the end of a code point 
    // and anything immediately before the start of a code point 
    // must be the end of a start of code point.
    ///////////
    const uint16_t end_of_code_point = uint16_t((start_of_code_point >> 1) | (! non_ascii_chars));
    const uint16_t twelvebits_mask = 0xFFF;
    const uint16_t twelvebits_end_of_code_point = end_of_code_point&twelvebits_mask;
    const uint8_t idx = utf8bigindex[twelvebits_end_of_code_point][0];
    const uint8_t consumed = utf8bigindex[twelvebits_end_of_code_point][1];
    //
    // Let us first try to see if we are in the easy two-byte scenario
    //
    if(idx < 64) {
        // this is a relatively easy scenario
        // we process SIX (6) input code-words. The max length in bytes of six code words
        // spanning between 1 and 2 bytes each is 12 bytes.
        // On processors where pdep/pext is fast, we might be able to use a small lookup table.
        const __m128i sh = _mm_loadu_si128((const __m128i *)shufutf8[idx]);
        const __m128i perm = _mm_shuffle_epi8(in, sh);
        const __m128i ascii = _mm_and_si128(perm,_mm_set1_epi16(0x7f));
        const __m128i highbyte = _mm_and_si128(perm,_mm_set1_epi16(0x1f00));
        const __m128i composed = _mm_or_si128(ascii,_mm_srli_epi16(highbyte,2));
        _mm_storeu_si128((__m128i*)output, composed);
        output += 6;
        pos += consumed;
    } else if (idx < 145) {
        const __m128i sh = _mm_loadu_si128((const __m128i *)shufutf8[idx]);
        const __m128i perm = _mm_shuffle_epi8(in, sh);
        const __m128i ascii = _mm_and_si128(perm,_mm_set1_epi32(0x7f)); // 7 or 6 bits
        const __m128i middlebyte = _mm_and_si128(perm,_mm_set1_epi32(0x3f00)); // 5 or 6 bits
        const __m128i middlebyte_shifted = _mm_srli_epi32(middlebyte,2);
        const __m128i highbyte = _mm_and_si128(perm,_mm_set1_epi32(0x0f0000)); // 4 bits
        const __m128i highbyte_shifted = _mm_srli_epi32(highbyte,4);
        const __m128i composed = _mm_or_si128(_mm_or_si128(ascii,middlebyte_shifted),highbyte_shifted);
        const __m128i composed_repacked = _mm_packus_epi32(composed,composed);
        _mm_storeu_si128((__m128i*)output,  composed_repacked);
        output += 4;
        pos += consumed;
    } else if(idx < 209) {
        const __m128i sh = _mm_loadu_si128((const __m128i *)shufutf8[idx]);
        const __m128i perm = _mm_shuffle_epi8(in, sh);
        const __m128i ascii = _mm_and_si128(perm,_mm_set1_epi32(0x7f)); 
        const __m128i middlebyte = _mm_and_si128(perm,_mm_set1_epi32(0x3f00));
        const __m128i middlebyte_shifted = _mm_srli_epi32(middlebyte,2);
        __m128i middlehighbyte = _mm_and_si128(perm,_mm_set1_epi32(0x3f0000));//_mm_xor_si128(_mm_and_si128(perm,_mm_set1_epi32(0x3f0000)), );
        // correct for spurious high bit
        const __m128i correct = _mm_srli_epi32(_mm_and_si128(perm,_mm_set1_epi32(0x400000)),1);
        middlehighbyte = _mm_xor_si128(correct, middlehighbyte);
        const __m128i middlehighbyte_shifted = _mm_srli_epi32(middlehighbyte,4);
        const __m128i highbyte = _mm_and_si128(perm,_mm_set1_epi32(0x07000000));
        const __m128i highbyte_shifted = _mm_srli_epi32(highbyte,6);
        const __m128i composed = _mm_or_si128(_mm_or_si128(ascii,middlebyte_shifted),_mm_or_si128(highbyte_shifted,middlehighbyte_shifted));
        const __m128i composedminus = _mm_sub_epi32(composed,_mm_set1_epi32(0x10000));
        const __m128i lowtenbits = _mm_and_si128(composedminus,_mm_set1_epi32(0x3ff));
        const __m128i hightenbits = _mm_srli_epi32(composedminus,10);
        const __m128i lowtenbitsadd = _mm_add_epi32(lowtenbits,_mm_set1_epi32(0xDC00));
        const __m128i hightenbitsadd = _mm_add_epi32(hightenbits,_mm_set1_epi32(0xD800));
        const __m128i lowtenbitsaddshifted = _mm_slli_epi32(lowtenbitsadd,16);
        const __m128i surrogates = _mm_or_si128(hightenbitsadd,lowtenbitsaddshifted);
        uint32_t basic_buffer[4];
        _mm_storeu_si128((__m128i*)basic_buffer,  composed);
        uint32_t surrogate_buffer[4];
        _mm_storeu_si128((__m128i*)surrogate_buffer,  surrogates);
        for(size_t i = 0; i < 3; i++) {
            if(basic_buffer[i]<65536) {
                *output = uint16_t(basic_buffer[i]);
                output++;
            } else {
                output[0] = uint16_t(surrogate_buffer[i] &0xFFFF);
                output[1] = uint16_t(surrogate_buffer[i] >> 16);
                output += 2;
            }
        }
        pos += consumed;        
    } else {
        // here we know that there is an error???
    }
  }
  size_t len = strlen_utf8_to_utf16_with_length(input + pos, size - pos);
  utf8_to_utf16_with_length(input + pos, size - pos, output);
  output += len;
  return output - start;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
movq %rdi, %r14
cmpq $0x10, %rsi
movq %rdx, 0x18(%rsp)
jae 0x2c23
xorl %ebp, %ebp
movq %rdx, %r13
jmp 0x2e54
movl $0x10, %edi
xorl %ebp, %ebp
vpxor %xmm0, %xmm0, %xmm0
vpbroadcastb 0x53eb(%rip), %xmm1 # 0x8022
vpmovsxbd 0xa3a8(%rip), %xmm2 # 0xcfe8
leaq 0x75f9(%rip), %rax      # 0xa240
leaq 0x95f2(%rip), %rcx      # 0xc240
vpbroadcastq 0xa369(%rip), %xmm3 # 0xcfc0
vpbroadcastq 0xa368(%rip), %xmm4 # 0xcfc8
vpbroadcastq 0xa317(%rip), %xmm5 # 0xcf80
vpbroadcastq 0xa32e(%rip), %xmm6 # 0xcfa0
vpbroadcastq 0xa33d(%rip), %xmm7 # 0xcfb8
vpbroadcastq 0xa303(%rip), %xmm17 # 0xcf88
vpbroadcastq 0xa302(%rip), %xmm9 # 0xcf90
vpbroadcastq 0xa301(%rip), %xmm10 # 0xcf98
vpbroadcastd 0xa340(%rip), %xmm11 # 0xcfe0
vpbroadcastd 0xa33b(%rip), %xmm12 # 0xcfe4
vpbroadcastq 0xa2f6(%rip), %xmm13 # 0xcfa8
vpbroadcastq 0xa2f5(%rip), %xmm14 # 0xcfb0
movq %rdx, %r13
vmovdqu (%r14,%rbp), %xmm15
vpmovmskb %xmm15, %edx
testl %edx, %edx
je 0x2d23
vpsrlw $0x4, %xmm15, %xmm16
vpandq %xmm1, %xmm16, %xmm16
vpshufb %xmm16, %xmm2, %xmm8
vpmovmskb %xmm8, %edx
andl $0x1ffe, %edx           # imm = 0x1FFE
movzbl (%rdx,%rax), %edi
movzbl 0x1(%rdx,%rax), %edx
cmpq $0x3f, %rdi
ja 0x2d43
shll $0x4, %edi
vpshufb (%rdi,%rcx), %xmm15, %xmm8
vpsrlw $0x2, %xmm8, %xmm15
vpand %xmm3, %xmm15, %xmm15
vpternlogq $0xf8, %xmm4, %xmm8, %xmm15
vmovdqu %xmm15, (%r13)
addq $0xc, %r13
jmp 0x2e3e
vpmovzxbw %xmm15, %xmm16  # xmm16 = xmm15[0],zero,xmm15[1],zero,xmm15[2],zero,xmm15[3],zero,xmm15[4],zero,xmm15[5],zero,xmm15[6],zero,xmm15[7],zero
vmovdqu64 %xmm16, (%r13)
vpunpckhbw %xmm0, %xmm15, %xmm15 # xmm15 = xmm15[8],xmm0[8],xmm15[9],xmm0[9],xmm15[10],xmm0[10],xmm15[11],xmm0[11],xmm15[12],xmm0[12],xmm15[13],xmm0[13],xmm15[14],xmm0[14],xmm15[15],xmm0[15]
vmovdqu %xmm15, 0x10(%r13)
addq $0x20, %r13
jmp 0x2e44
cmpb $-0x70, %dil
ja 0x2d85
shll $0x4, %edi
vpshufb (%rdi,%rcx), %xmm15, %xmm8
vpsrld $0x2, %xmm8, %xmm15
vpand %xmm5, %xmm15, %xmm15
vpsrld $0x4, %xmm8, %xmm16
vpternlogq $0xf8, %xmm6, %xmm8, %xmm15
vpternlogq $0xf8, %xmm7, %xmm16, %xmm15
vpackusdw %xmm15, %xmm15, %xmm8
vmovdqu %xmm8, (%r13)
addq $0x8, %r13
jmp 0x2e3e
cmpb $-0x30, %dil
ja 0x2e41
shll $0x4, %edi
vpshufb (%rdi,%rcx), %xmm15, %xmm8
vpsrld $0x2, %xmm8, %xmm15
vpand %xmm5, %xmm15, %xmm15
vpsrld $0x1, %xmm8, %xmm16
vpandq %xmm17, %xmm16, %xmm16
vpternlogq $0x78, %xmm9, %xmm8, %xmm16
vpsrld $0x4, %xmm16, %xmm16
vpsrld $0x6, %xmm8, %xmm18
vpandq %xmm10, %xmm18, %xmm18
vpternlogq $0xf8, %xmm6, %xmm8, %xmm15
vpternlogq $0xfe, %xmm18, %xmm16, %xmm15
vpaddd %xmm11, %xmm15, %xmm8
vpsrld $0xa, %xmm8, %xmm8
vpaddd %xmm12, %xmm8, %xmm8
vpslld $0x10, %xmm15, %xmm16
vpandq %xmm13, %xmm16, %xmm16
vpternlogq $0xfe, %xmm14, %xmm8, %xmm16
vmovdqa %xmm15, (%rsp)
vmovdqa64 %xmm16, 0x20(%rsp)
xorl %r8d, %r8d
movl (%rsp,%r8,4), %edi
cmpl $0xffff, %edi           # imm = 0xFFFF
ja 0x2e24
movw %di, (%r13)
movl $0x2, %edi
jmp 0x2e32
movl 0x20(%rsp,%r8,4), %edi
movl %edi, (%r13)
movl $0x4, %edi
addq %rdi, %r13
incq %r8
cmpq $0x3, %r8
jne 0x2e0c
addq %rdx, %rbp
movq %rbp, %rdi
movq %rdi, %rbp
addq $0x10, %rdi
cmpq %r15, %rdi
jbe 0x2cbe
addq %rbp, %r14
movq %r15, 0x10(%rsp)
subq %rbp, %r15
movq $0x0, (%rsp)
jne 0x2e6d
xorl %ebx, %ebx
jmp 0x2eaa
xorl %ebx, %ebx
movl $0x1, %r12d
xorl %edi, %edi
addq %r14, %rdi
movq %rsp, %rsi
callq 0x2f7e
xorl %ecx, %ecx
cmpl $0x110000, %eax         # imm = 0x110000
setb %cl
addl %ecx, %ecx
cmpl $0x10000, %eax          # imm = 0x10000
cmovbq %r12, %rcx
addq %rcx, %rbx
movq (%rsp), %rdi
incq %rdi
movq %rdi, (%rsp)
cmpq %r15, %rdi
jb 0x2e77
movq $0x0, (%rsp)
cmpq %rbp, 0x10(%rsp)
je 0x2f62
xorl %ebp, %ebp
movl $0x1, %r12d
xorl %edi, %edi
addq %r14, %rdi
movq %rsp, %rsi
callq 0x2f7e
testq %r13, %r13
je 0x2f37
movl %ebp, %ecx
leaq (%r13,%rcx,2), %rcx
xorl %edx, %edx
cmpl $0x110000, %eax         # imm = 0x110000
setb %dl
addl %edx, %edx
cmpl $0x10000, %eax          # imm = 0x10000
cmovbl %r12d, %edx
cmpl $0x2, %edx
je 0x2f0a
cmpl $0x1, %edx
jne 0x2f32
movw %ax, (%rcx)
testl %eax, %eax
je 0x2f37
addq $0x2, %rcx
jmp 0x2f32
leal 0x3ff0000(%rax), %edx
shrl $0xa, %edx
addl $0xffffd800, %edx       # imm = 0xFFFFD800
movw %dx, (%rcx)
movl %eax, %edx
andl $0x3ff, %edx            # imm = 0x3FF
orl $0xdc00, %edx           # imm = 0xDC00
movw %dx, 0x2(%rcx)
addq $0x4, %rcx
movw $0x0, (%rcx)
xorl %ecx, %ecx
cmpl $0x110000, %eax         # imm = 0x110000
setb %cl
addl %ecx, %ecx
cmpl $0x10000, %eax          # imm = 0x10000
cmovbl %r12d, %ecx
addl %ecx, %ebp
movq (%rsp), %rdi
incq %rdi
movq %rdi, (%rsp)
cmpq %r15, %rdi
jb 0x2ec7
leaq (%r13,%rbx,2), %rax
subq 0x18(%rsp), %rax
sarq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq