// File: /workspace/github_cpp_makefile_star2_O3/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 0x3f47
cmpq %rdx, %r8
je 0x3f66
movq %rsi, %r11
andq $-0x4, %r11
movq %rcx, %rbx
sete %al
andq $-0x4, %rbx
sete %r9b
orb %al, %r9b
je 0x3e55
xorl %r9d, %r9d
xorl %r10d, %r10d
movq %r8, %rax
jmp 0x3efa
vmovdqu (%rdi), %xmm0
leaq 0x2530(%rip), %r14      # 0x6390
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 0x3ee3
leaq 0x4(%r10), %r12
cmpq %r11, %r12
jae 0x3ef7
vmovdqu 0x10(%rdi,%r10,4), %xmm0
movq %r12, %r10
cmpl %r15d, %ebp
jb 0x3e7e
addq $0x4, %r9
cmpq %rbx, %r9
jb 0x3e69
jmp 0x3efa
movq %r12, %r10
cmpq %rsi, %r10
setae %r11b
cmpq %rcx, %r9
setae %bl
orb %r11b, %bl
jne 0x3f37
movl (%rdi,%r10,4), %ebx
movl (%rdx,%r9,4), %r11d
cmpl %r11d, %ebx
jae 0x3f1e
incq %r10
jmp 0x3f2d
ja 0x3f2a
movl %r11d, (%rax)
addq $0x4, %rax
incq %r10
incq %r9
cmpq %rsi, %r10
jae 0x3f37
cmpq %rcx, %r9
jb 0x3f0c
subq %r8, %rax
sarq $0x2, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2542(%rip), %rdi      # 0x6490
leaq 0x2544(%rip), %rsi      # 0x6499
leaq 0x25d7(%rip), %rcx      # 0x6533
movl $0x67, %edx
callq 0x20b0
leaq 0x25bd(%rip), %rdi      # 0x652a
leaq 0x2525(%rip), %rsi      # 0x6499
leaq 0x25b8(%rip), %rcx      # 0x6533
movl $0x68, %edx
callq 0x20b0
nopl (%rax)