// File: /workspace/github_cpp_makefile_star2_O3/ralgond[P]SortedListIntersection/algo_highlyscalable_SIMD.cpp
// Function: highlyscalable_SIMD_intersection(unsigned int const*, unsigned long, unsigned int const*, unsigned long, unsigned int*)
size_t 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;

  // trim lengths to be a multiple of 4
  size_t st_a = (s_a / 4) * 4;
  size_t st_b = (s_b / 4) * 4;

  while (i_a < st_a && i_b < st_b) {
    //[ load segments of four 32-bit elements
    __m128i v_a = _mm_loadu_si128((__m128i *)&A[i_a]);
    __m128i v_b = _mm_loadu_si128((__m128i *)&B[i_b]);
    //]

    //[ move pointers
    const uint32_t a_max = A[i_a + 3];
    const uint32_t b_max = B[i_b + 3];
    i_a += (a_max <= b_max) * 4;
    i_b += (a_max >= b_max) * 4;
    //]

    //[ compute mask of common elements
    const uint32_t cyclic_shift = _MM_SHUFFLE(0, 3, 2, 1);
    __m128i cmp_mask1 = _mm_cmpeq_epi32(v_a, v_b); // pairwise comparison
    v_b = _mm_shuffle_epi32(v_b, cyclic_shift);    // shuffling
    __m128i cmp_mask2 = _mm_cmpeq_epi32(v_a, v_b); // again...
    v_b = _mm_shuffle_epi32(v_b, cyclic_shift);
    __m128i cmp_mask3 = _mm_cmpeq_epi32(v_a, v_b); // and again...
    v_b = _mm_shuffle_epi32(v_b, cyclic_shift);
    __m128i cmp_mask4 = _mm_cmpeq_epi32(v_a, v_b); // and again.
    __m128i cmp_mask = _mm_or_si128(
        _mm_or_si128(cmp_mask1, cmp_mask2),
        _mm_or_si128(cmp_mask3, cmp_mask4)); // OR-ing of comparison masks
    // convert the 128-bit mask to the 4-bit mask
    const int mask = _mm_movemask_ps(_mm_castsi128_ps(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
                                 //]
  }

  // 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 0x3dd5
cmpq %rdx, %r8
je 0x3df4
movq %rsi, %r11
andq $-0x4, %r11
movq %rcx, %rbx
sete %al
andq $-0x4, %rbx
sete %r9b
orb %al, %r9b
jne 0x3d8f
leaq 0x2699(%rip), %r14      # 0x6390
xorl %r9d, %r9d
movq %r8, %rax
xorl %r10d, %r10d
vmovdqu (%rdi,%r9,4), %xmm0
vmovdqu (%rdx,%r10,4), %xmm1
xorl %r15d, %r15d
xorl %r12d, %r12d
vpextrd $0x3, %xmm0, %ebp
cmpl 0xc(%rdx,%r10,4), %ebp
vpshufd $0x39, %xmm1, %xmm2     # xmm2 = xmm1[1,2,3,0]
vpcmpeqd %xmm0, %xmm1, %k0
vpcmpeqd %xmm0, %xmm2, %k1
vpshufd $0x4e, %xmm1, %xmm2     # xmm2 = xmm1[2,3,0,1]
vpshufd $0x93, %xmm1, %xmm1     # xmm1 = xmm1[3,0,1,2]
vpcmpeqd %xmm0, %xmm2, %k2
korw %k0, %k1, %k0
vpcmpeqd %xmm0, %xmm1, %k3
korw %k2, %k0, %k0
korw %k3, %k0, %k0
setae %r15b
setbe %r12b
leaq (%r10,%r15,4), %r10
kmovd %k0, %r15d
leaq (%r9,%r12,4), %r9
andl $0xf, %r15d
popcntl %r15d, %r12d
shll $0x4, %r15d
vpshufb (%r15,%r14), %xmm0, %xmm0
vmovdqu %xmm0, (%rax)
leaq (%rax,%r12,4), %rax
cmpq %r11, %r9
jae 0x3dc0
cmpq %rbx, %r10
jb 0x3d00
jmp 0x3dc0
xorl %r10d, %r10d
xorl %r9d, %r9d
movq %r8, %rax
jmp 0x3dc0
cmpq %rcx, %r10
jae 0x3dc5
movl (%rdi,%r9,4), %ebx
movl (%rdx,%r10,4), %r11d
cmpl %r11d, %ebx
jae 0x3db1
incq %r9
jmp 0x3dc0
ja 0x3dbd
movl %r11d, (%rax)
addq $0x4, %rax
incq %r9
incq %r10
cmpq %rsi, %r9
jb 0x3d9a
subq %r8, %rax
sarq $0x2, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x26b4(%rip), %rdi      # 0x6490
leaq 0x26b6(%rip), %rsi      # 0x6499
leaq 0x26cc(%rip), %rcx      # 0x64b6
movl $0x1d, %edx
callq 0x20b0
leaq 0x272f(%rip), %rdi      # 0x652a
leaq 0x2697(%rip), %rsi      # 0x6499
leaq 0x26ad(%rip), %rcx      # 0x64b6
movl $0x1e, %edx
callq 0x20b0