// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpq %rdi, %r8
je 0x4445
cmpq %rdx, %r8
je 0x4464
movq %rsi, %r11
andq $-0x10, %r11
sete %al
movq %rcx, %rbx
andq $-0x10, %rbx
sete %r9b
orb %al, %r9b
movq %r8, (%rsp)
jne 0x43e4
xorl %r15d, %r15d
leaq 0x6e7c(%rip), %r9       # 0xb1c0
movq %r8, %rax
movq %r9, %r8
xorl %r12d, %r12d
vmovdqu64 (%rdi,%r15,4), %zmm0
movl 0x3c(%rdi,%r15,4), %ebp
xorl %r9d, %r9d
xorl %r10d, %r10d
cmpl 0x3c(%rdx,%r12,4), %ebp
setae %r9b
setbe %r10b
shll $0x4, %r10d
shll $0x4, %r9d
vmovdqu64 (%rdx,%r12,4), %zmm1
vpcmpeqd %zmm1, %zmm0, %k0
kmovd %k0, %ebp
movl $0xf, %r13d
valignd $0x1, %zmm1, %zmm1, %zmm1 # zmm1 = zmm1[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0]
vpcmpeqd %zmm1, %zmm0, %k0
kmovd %k0, %r14d
orl %r14d, %ebp
decl %r13d
jne 0x438b
addq %r15, %r10
addq %r12, %r9
movzwl %bp, %r15d
popcntl %r15d, %r12d
shll $0x6, %r15d
vmovdqa64 (%r15,%r8), %zmm1
vpermd %zmm0, %zmm1, %zmm0
vmovdqu64 %zmm0, (%rax)
leaq (%rax,%r12,4), %rax
cmpq %r11, %r10
jae 0x43ed
movq %r10, %r15
movq %r9, %r12
cmpq %rbx, %r9
jb 0x434d
jmp 0x43ed
xorl %r9d, %r9d
xorl %r10d, %r10d
movq %r8, %rax
cmpq %rsi, %r10
jae 0x4428
cmpq %rcx, %r9
movq (%rsp), %rbx
jae 0x442c
movl (%rdi,%r10,4), %r8d
movl (%rdx,%r9,4), %r11d
cmpl %r11d, %r8d
jae 0x440d
incq %r10
jmp 0x441c
ja 0x4419
movl %r11d, (%rax)
addq $0x4, %rax
incq %r10
incq %r9
cmpq %rsi, %r10
jae 0x442c
cmpq %rcx, %r9
jb 0x43fb
jmp 0x442c
movq (%rsp), %rbx
subq %rbx, %rax
sarq $0x2, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
leaq 0x2044(%rip), %rdi      # 0x6490
leaq 0x222d(%rip), %rsi      # 0x6680
leaq 0x2245(%rip), %rcx      # 0x669f
movl $0x57, %edx
callq 0x20b0
leaq 0x20bf(%rip), %rdi      # 0x652a
leaq 0x220e(%rip), %rsi      # 0x6680
leaq 0x2226(%rip), %rcx      # 0x669f
movl $0x58, %edx
callq 0x20b0
nop