// File: /workspace/github_cpp_makefile_star2_O2/ralgond[P]SortedListIntersection/algo_highlyscalable_SIMD.cpp
// Function: lemire_highlyscalable_SIMD_intersection(unsigned int const*, unsigned long, unsigned int const*, unsigned long, unsigned int*)
size_t lemire_highlyscalable_SIMD_intersection(const uint32_t *A, const size_t s_a,
                                            const uint32_t *B, const size_t s_b,
                                            uint32_t *out) {
  assert(out != A);
  assert(out != B);
  const uint32_t *const initout(out);
  size_t i_a = 0, i_b = 0;
  const static uint32_t cyclic_shift1 = _MM_SHUFFLE(0, 3, 2, 1);
  const static uint32_t cyclic_shift2 = _MM_SHUFFLE(1, 0, 3, 2);
  const static uint32_t cyclic_shift3 = _MM_SHUFFLE(2, 1, 0, 3);

  // trim lengths to be a multiple of 4
  size_t st_a = (s_a / 4) * 4;
  size_t st_b = (s_b / 4) * 4;
  if (i_a < st_a && i_b < st_b) {
    __m128i v_a, v_b;
    v_a = MM_LOAD_SI_128((__m128i *)&A[i_a]);
    v_b = MM_LOAD_SI_128((__m128i *)&B[i_b]);
    while (true) {
      const __m128i cmp_mask1 =
          _mm_cmpeq_epi32(v_a, v_b); // pairwise comparison
      const __m128i cmp_mask2 = _mm_cmpeq_epi32(
          v_a, _mm_shuffle_epi32(v_b, cyclic_shift1)); // again...
      __m128i cmp_mask = _mm_or_si128(cmp_mask1, cmp_mask2);
      const __m128i cmp_mask3 = _mm_cmpeq_epi32(
          v_a, _mm_shuffle_epi32(v_b, cyclic_shift2)); // and again...
      cmp_mask = _mm_or_si128(cmp_mask, cmp_mask3);
      const __m128i cmp_mask4 = _mm_cmpeq_epi32(
          v_a, _mm_shuffle_epi32(v_b, cyclic_shift3)); // and again.
      cmp_mask = _mm_or_si128(cmp_mask, cmp_mask4);
      // convert the 128-bit mask to the 4-bit mask
      const int mask = _mm_movemask_ps(*reinterpret_cast<__m128 *>(&cmp_mask));
      // copy out common elements
      const __m128i p = _mm_shuffle_epi8(v_a, shuffle_mask[mask]);

      _mm_storeu_si128((__m128i *)out, p);
      out +=
          _mm_popcnt_u32(mask); // a number of elements is a weight of the mask

      const uint32_t a_max = A[i_a + 3];
      if (a_max <= B[i_b + 3]) {
        i_a += 4;
        if (i_a >= st_a)
          break;
        v_a = MM_LOAD_SI_128((__m128i *)&A[i_a]);
      }
      if (a_max >= B[i_b + 3]) {
        i_b += 4;
        if (i_b >= st_b)
          break;
        v_b = MM_LOAD_SI_128((__m128i *)&B[i_b]);
      }
    }
  }

  // intersect the tail using scalar intersection
  while (i_a < s_a && i_b < s_b) {
    if (A[i_a] < B[i_b]) {
      i_a++;
    } else if (B[i_b] < A[i_a]) {
      i_b++;
    } else {
      *out++ = B[i_b];
      i_a++;
      i_b++;
    }
  }

  return out - initout;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
cmpq %rdi, %r8
je 0x3be5
cmpq %rdx, %r8
je 0x3bfe
movq %rsi, %r11
andq $-0x4, %r11
movq %rcx, %rbx
setne %al
andq $-0x4, %rbx
setne %r9b
testb %r9b, %al
jne 0x3b01
xorl %r9d, %r9d
xorl %r10d, %r10d
movq %r8, %rax
jmp 0x3ba6
vmovdqu (%rdi), %xmm0
leaq 0x28d4(%rip), %r14      # 0x63e0
xorl %r9d, %r9d
xorl %r10d, %r10d
movq %r8, %rax
vmovdqu (%rdx,%r9,4), %xmm1
vpshufd $0x39, %xmm1, %xmm2     # xmm2 = xmm1[1,2,3,0]
vpshufd $0x4e, %xmm1, %xmm3     # xmm3 = xmm1[2,3,0,1]
vpshufd $0x93, %xmm1, %xmm4     # xmm4 = xmm1[3,0,1,2]
vpcmpeqd %xmm0, %xmm1, %k0
vpcmpeqd %xmm0, %xmm2, %k1
korw %k1, %k0, %k0
vpcmpeqd %xmm0, %xmm3, %k1
korw %k1, %k0, %k0
vpcmpeqd %xmm0, %xmm4, %k1
korw %k1, %k0, %k0
kmovd %k0, %r15d
andl $0xf, %r15d
popcntl %r15d, %r12d
shll $0x4, %r15d
vpshufb (%r15,%r14), %xmm0, %xmm5
vmovdqu %xmm5, (%rax)
leaq (%rax,%r12,4), %rax
movl 0xc(%rdi,%r10,4), %ebp
movl 0xc(%rdx,%r9,4), %r15d
cmpl %r15d, %ebp
ja 0x3b8f
leaq 0x4(%r10), %r12
cmpq %r11, %r12
jae 0x3ba3
vmovdqu 0x10(%rdi,%r10,4), %xmm0
movq %r12, %r10
cmpl %r15d, %ebp
jb 0x3b2a
addq $0x4, %r9
cmpq %rbx, %r9
jb 0x3b15
jmp 0x3ba6
movq %r12, %r10
cmpq %rsi, %r10
jae 0x3bd5
cmpq %rcx, %r9
jae 0x3bd5
movl (%rdi,%r10,4), %ebx
movl (%rdx,%r9,4), %r11d
cmpl %r11d, %ebx
jae 0x3bc2
incq %r10
jmp 0x3ba6
jbe 0x3bc9
incq %r9
jmp 0x3ba6
movl %r11d, (%rax)
addq $0x4, %rax
incq %r10
jmp 0x3bc4
subq %r8, %rax
sarq $0x2, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x28f4(%rip), %rdi      # 0x64e0
leaq 0x28f6(%rip), %rsi      # 0x64e9
leaq 0x2989(%rip), %rcx      # 0x6583
pushq $0x67
jmp 0x3c15
leaq 0x2975(%rip), %rdi      # 0x657a
leaq 0x28dd(%rip), %rsi      # 0x64e9
leaq 0x2970(%rip), %rcx      # 0x6583
pushq $0x68
popq %rdx
callq 0x20f0
nop