// File: /workspace/github_cpp_makefile_star2_O3/ralgond[P]SortedListIntersection/algo_highlyscalable_avx512.cpp
// Function: highlyscalable_avx512_intersection(unsigned int const*, unsigned long, unsigned int const*, unsigned long, unsigned int*)
size_t highlyscalable_avx512_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 16
  size_t st_a = (s_a / 16) * 16;
  size_t st_b = (s_b / 16) * 16;

  while (i_a < st_a && i_b < st_b) {
    __m512i v_a = _mm512_loadu_si512(&A[i_a]);
    __m512i v_b = _mm512_loadu_si512(&B[i_b]);
    //]

    //[ move pointers
    const uint32_t a_max = A[i_a + 15];
    const uint32_t b_max = B[i_b + 15];
    i_a += (a_max <= b_max) * 16;
    i_b += (a_max >= b_max) * 16;
    
    //[ compute mask of common elements
    const __m512i rotate_arg = _mm512_set_epi32(0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
    __mmask16 cmp_mask = _mm512_cmpeq_epu32_mask(v_a, v_b); // pairwise comparison

    for (int i = 0; i < 15; i++) {
	    v_b = _mm512_permutexvar_epi32(rotate_arg, v_b);    // shuffling
	    __mmask16 cmp_mask1 = _mm512_cmpeq_epu32_mask(v_a, v_b);
	    cmp_mask = _mm512_kor(cmp_mask, cmp_mask1);
    }

    const int mask = _mm512_mask2int(cmp_mask);
    //cout<<"mask: "<<mask<<endl;

    //[ copy out common elements
    //const __m256i p = _mm256_permutevar8x32_epi32(v_a, shuffle_mask64[mask]);
    //__m256i p; //_mm256_permutevar8x32_epi32(v_a, shuffle_mask64[mask]);
    __m512i p = _mm512_permutexvar_epi32(shuffle_mask64[mask], v_a);
    _mm512_storeu_si512(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 %r14
pushq %rbx
cmpq %rdi, %r8
je 0x4444
cmpq %rdx, %r8
je 0x4463
movq %rsi, %r11
andq $-0x10, %r11
movq %rcx, %rbx
sete %al
andq $-0x10, %rbx
sete %r9b
orb %al, %r9b
jne 0x43ec
vpmovsxbd 0x229e(%rip), %xmm1 # 0x65c8
vpxor %xmm0, %xmm0, %xmm0
leaq 0x6e8b(%rip), %r14      # 0xb1c0
movq %r8, %rax
vmovq %xmm0, %r9
vpextrq $0x1, %xmm0, %r10
vpbroadcastq 0x38(%rdi,%r9,4), %xmm5
vpbroadcastq 0x38(%rdx,%r10,4), %xmm4
vmovdqu64 (%rdx,%r10,4), %zmm3
vmovdqu64 (%rdi,%r9,4), %zmm2
movl $0xf, %r10d
vmovdqa %xmm5, %xmm6
vpermt2d %xmm4, %xmm1, %xmm6
vpermt2d %xmm5, %xmm1, %xmm4
vpcmpeqd %zmm3, %zmm2, %k0
kmovd %k0, %r9d
vpcmpleud %xmm4, %xmm6, %k1
vpbroadcastq 0x2310(%rip), %xmm4 {%k1} {z} # 0x66a0
valignd $0x1, %zmm3, %zmm3, %zmm3 # zmm3 = zmm3[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0]
vpcmpeqd %zmm3, %zmm2, %k0
kmovd %k0, %ebp
orl %ebp, %r9d
decl %r10d
jne 0x4390
movzwl %r9w, %r9d
vpaddq %xmm0, %xmm4, %xmm0
popcntl %r9d, %r10d
shll $0x6, %r9d
vmovdqa64 (%r9,%r14), %zmm3
vpextrq $0x1, %xmm0, %r9
vpermd %zmm2, %zmm3, %zmm2
vmovdqu64 %zmm2, (%rax)
leaq (%rax,%r10,4), %rax
vmovq %xmm0, %r10
cmpq %r11, %r10
jae 0x43f5
cmpq %rbx, %r9
jb 0x4338
jmp 0x43f5
xorl %r9d, %r9d
xorl %r10d, %r10d
movq %r8, %rax
cmpq %rsi, %r10
setb %r11b
cmpq %rcx, %r9
setb %bl
andb %r11b, %bl
cmpb $0x1, %bl
jne 0x4435
movl (%rdi,%r10,4), %ebx
movl (%rdx,%r9,4), %r11d
cmpl %r11d, %ebx
jae 0x441c
incq %r10
jmp 0x442b
ja 0x4428
movl %r11d, (%rax)
addq $0x4, %rax
incq %r10
incq %r9
cmpq %rsi, %r10
jae 0x4435
cmpq %rcx, %r9
jb 0x440a
subq %r8, %rax
sarq $0x2, %rax
popq %rbx
popq %r14
popq %rbp
vzeroupper
retq
leaq 0x2045(%rip), %rdi      # 0x6490
leaq 0x2256(%rip), %rsi      # 0x66a8
leaq 0x226e(%rip), %rcx      # 0x66c7
movl $0x57, %edx
callq 0x20b0
leaq 0x20c0(%rip), %rdi      # 0x652a
leaq 0x2237(%rip), %rsi      # 0x66a8
leaq 0x224f(%rip), %rcx      # 0x66c7
movl $0x58, %edx
callq 0x20b0
nop