// File: /workspace/github_cpp_makefile_star2_O1/ralgond[P]SortedListIntersection/algo_highlyscalable_avx2.cpp
// Function: highlyscalable_avx2_intersection(unsigned int const*, unsigned long, unsigned int const*, unsigned long, unsigned int*)
size_t highlyscalable_avx2_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 8
  size_t st_a = (s_a / 8) * 8;
  size_t st_b = (s_b / 8) * 8;

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

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

    //[ compute mask of common elements
    const __m256i rotate_arg = _mm256_set_epi32(0,7,6,5,4,3,2,1);
    __m256i cmp_mask = _mm256_cmpeq_epi32(v_a, v_b); // pairwise comparison

    for (int i = 0; i < 7; i++) {
	    v_b = _mm256_permutevar8x32_epi32(v_b, rotate_arg);    // shuffling
	    __m256i cmp_mask1 = _mm256_cmpeq_epi32(v_a, v_b); // again...
	    cmp_mask = _mm256_or_si256(cmp_mask, cmp_mask1);
    }

    // convert the 256-bit mask to the 8-bit mask
    const int mask = _mm256_movemask_ps(_mm256_castsi256_ps(cmp_mask));
    //cout<<"mask: "<<mask<<endl;

    //[ copy out common elements
    const __m256i p = _mm256_permutevar8x32_epi32(v_a, shuffle_mask32[mask]);
    _mm256_storeu_si256((__m256i *)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 0x4131
cmpq %rdx, %r8
je 0x4150
movq %rsi, %r11
andq $-0x8, %r11
sete %al
movq %rcx, %rbx
andq $-0x8, %rbx
sete %r9b
orb %al, %r9b
jne 0x40e8
xorl %r15d, %r15d
leaq 0x511e(%rip), %r14      # 0x9180
movq %r8, %rax
xorl %r12d, %r12d
vmovdqu (%rdi,%r15,4), %ymm0
movl 0x1c(%rdi,%r15,4), %ebp
xorl %r9d, %r9d
xorl %r10d, %r10d
cmpl 0x1c(%rdx,%r12,4), %ebp
setae %r9b
setbe %r10b
shll $0x3, %r10d
shll $0x3, %r9d
vmovdqu (%rdx,%r12,4), %ymm2
vpcmpeqd %ymm2, %ymm0, %ymm1
movl $0x7, %ebp
valignd $0x1, %ymm2, %ymm2, %ymm2 # ymm2 = ymm2[1,2,3,4,5,6,7,0]
vpcmpeqd %ymm2, %ymm0, %ymm3
vpor %ymm3, %ymm1, %ymm1
decl %ebp
jne 0x409d
addq %r15, %r10
addq %r12, %r9
vmovmskps %ymm1, %r15d
popcntl %r15d, %r12d
shll $0x5, %r15d
vmovdqa (%r15,%r14), %ymm1
vpermd %ymm0, %ymm1, %ymm0
vmovdqu %ymm0, (%rax)
leaq (%rax,%r12,4), %rax
cmpq %r11, %r10
jae 0x4119
movq %r10, %r15
movq %r9, %r12
cmpq %rbx, %r9
jb 0x4068
jmp 0x4119
xorl %r9d, %r9d
xorl %r10d, %r10d
movq %r8, %rax
jmp 0x4119
cmpq %rcx, %r9
jae 0x411e
movl (%rdi,%r10,4), %ebx
movl (%rdx,%r9,4), %r11d
cmpl %r11d, %ebx
jae 0x410a
incq %r10
jmp 0x4119
ja 0x4116
movl %r11d, (%rax)
addq $0x4, %rax
incq %r10
incq %r9
cmpq %rsi, %r10
jb 0x40f3
subq %r8, %rax
sarq $0x2, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
leaq 0x2358(%rip), %rdi      # 0x6490
leaq 0x246f(%rip), %rsi      # 0x65ae
leaq 0x2485(%rip), %rcx      # 0x65cb
movl $0x42, %edx
callq 0x20b0
leaq 0x23d3(%rip), %rdi      # 0x652a
leaq 0x2450(%rip), %rsi      # 0x65ae
leaq 0x2466(%rip), %rcx      # 0x65cb
movl $0x43, %edx
callq 0x20b0