// File: /workspace/github_cpp_makefile_star2_O1/kaityo256[P]lj_simd/force_aos.cpp
// Function: force_sorted_z_avx2_swp()
void
force_sorted_z_avx2_swp(void) {
  const v4df vzero = _mm256_set1_pd(0.0);
  const v4df vcl2 = _mm256_set1_pd(CL2);
  const v4df vc24 = _mm256_set1_pd(24 * dt);
  const v4df vc48 = _mm256_set1_pd(48 * dt);
  const int pn = particle_number;
  for (int i = 0; i < pn; i++) {
    const v4df vqi = _mm256_load_pd((double *)(&z[i][X]));
    v4df vpi = _mm256_load_pd((double *)(&z[i][PX]));
    const int np = number_of_partners[i];
    const int kp = pointer[i];
    int k = 0;
    // --- 8< ---
    int j_1 = sorted_list[kp + k];
    int j_2 = sorted_list[kp + k + 1];
    int j_3 = sorted_list[kp + k + 2];
    int j_4 = sorted_list[kp + k + 3];
    v4df vqj_1 = _mm256_load_pd((double *)(&z[j_1][X]));
    v4df vqj_2 = _mm256_load_pd((double *)(&z[j_2][X]));
    v4df vqj_3 = _mm256_load_pd((double *)(&z[j_3][X]));
    v4df vqj_4 = _mm256_load_pd((double *)(&z[j_4][X]));

    v4df vdq_1 = (vqj_1 - vqi);
    v4df vdq_2 = (vqj_2 - vqi);
    v4df vdq_3 = (vqj_3 - vqi);
    v4df vdq_4 = (vqj_4 - vqi);

    v4df tmp0 = _mm256_unpacklo_pd(vdq_1, vdq_2);
    v4df tmp1 = _mm256_unpackhi_pd(vdq_1, vdq_2);
    v4df tmp2 = _mm256_unpacklo_pd(vdq_3, vdq_4);
    v4df tmp3 = _mm256_unpackhi_pd(vdq_3, vdq_4);

    v4df vdx = _mm256_permute2f128_pd(tmp0, tmp2, 0x20);
    v4df vdy = _mm256_permute2f128_pd(tmp1, tmp3, 0x20);
    v4df vdz = _mm256_permute2f128_pd(tmp0, tmp2, 0x31);

    v4df vr2 = vdx * vdx + vdy * vdy + vdz * vdz;
    v4df vr6 = vr2 * vr2 * vr2;
    v4df vdf = (vc24 * vr6 - vc48) / (vr6 * vr6 * vr2);
    v4df mask = vcl2 - vr2;
    vdf = _mm256_blendv_pd(vdf, vzero, mask);

    if (np < 4) {
      vdf =  _mm256_setzero_pd();
    }

    v4df vdf_1, vdf_2, vdf_3, vdf_4;
    v4df vpj_1, vpj_2, vpj_3, vpj_4;
    for (k = 4; k < (np / 4) * 4; k += 4) {
      // --- 8< ---
      const int j_1_b = sorted_list[kp + k];
      const int j_2_b = sorted_list[kp + k + 1];
      const int j_3_b = sorted_list[kp + k + 2];
      const int j_4_b = sorted_list[kp + k + 3];
      vqj_1 = _mm256_load_pd((double *)(&z[j_1_b][X]));
      vqj_2 = _mm256_load_pd((double *)(&z[j_2_b][X]));
      vqj_3 = _mm256_load_pd((double *)(&z[j_3_b][X]));
      vqj_4 = _mm256_load_pd((double *)(&z[j_4_b][X]));
      v4df vdq_1_b = (vqj_1 - vqi);
      v4df vdq_2_b = (vqj_2 - vqi);
      v4df vdq_3_b = (vqj_3 - vqi);
      v4df vdq_4_b = (vqj_4 - vqi);
      tmp0 = _mm256_unpacklo_pd(vdq_1_b, vdq_2_b);
      tmp1 = _mm256_unpackhi_pd(vdq_1_b, vdq_2_b);
      tmp2 = _mm256_unpacklo_pd(vdq_3_b, vdq_4_b);
      tmp3 = _mm256_unpackhi_pd(vdq_3_b, vdq_4_b);
      vdx = _mm256_permute2f128_pd(tmp0, tmp2, 0x20);
      vdy = _mm256_permute2f128_pd(tmp1, tmp3, 0x20);
      vdz = _mm256_permute2f128_pd(tmp0, tmp2, 0x31);
      v4df vr2 = vdx * vdx + vdy * vdy + vdz * vdz;
      vr6 = vr2 * vr2 * vr2;
      mask = vcl2 - vr2;

      // --- 8< ---
      vdf_1 = _mm256_permute4x64_pd(vdf, 0);
      vdf_2 = _mm256_permute4x64_pd(vdf, 85);
      vdf_3 = _mm256_permute4x64_pd(vdf, 170);
      vdf_4 = _mm256_permute4x64_pd(vdf, 255);

      vpi += vdq_1 * vdf_1;
      vpi += vdq_4 * vdf_4;
      vpi += vdq_2 * vdf_2;
      vpi += vdq_3 * vdf_3;
      vpj_1 = _mm256_load_pd((double *)(&z[j_1][PX]));
      vpj_2 = _mm256_load_pd((double *)(&z[j_2][PX]));
      vpj_3 = _mm256_load_pd((double *)(&z[j_3][PX]));
      vpj_4 = _mm256_load_pd((double *)(&z[j_4][PX]));


      vpj_1 -= vdq_1 * vdf_1;
      vpj_2 -= vdq_2 * vdf_2;
      vpj_3 -= vdq_3 * vdf_3;
      vpj_4 -= vdq_4 * vdf_4;

      _mm256_store_pd((double *)(&z[j_1][PX]), vpj_1);
      _mm256_store_pd((double *)(&z[j_2][PX]), vpj_2);
      _mm256_store_pd((double *)(&z[j_3][PX]), vpj_3);
      _mm256_store_pd((double *)(&z[j_4][PX]), vpj_4);


      // --- 8< ---
      j_1 = j_1_b;
      j_2 = j_2_b;
      j_3 = j_3_b;
      j_4 = j_4_b;
      vdq_1 = vdq_1_b;
      vdq_2 = vdq_2_b;
      vdq_3 = vdq_3_b;
      vdq_4 = vdq_4_b;
      vdf = (vc24 * vr6 - vc48) / (vr6 * vr6 * vr2);
      vdf = _mm256_blendv_pd(vdf, vzero, mask);
    }
    // --- 8< ---
    vdf_1 = _mm256_permute4x64_pd(vdf, 0);
    vdf_2 = _mm256_permute4x64_pd(vdf, 85);
    vdf_3 = _mm256_permute4x64_pd(vdf, 170);
    vdf_4 = _mm256_permute4x64_pd(vdf, 255);

    vpj_1 = _mm256_load_pd((double *)(&z[j_1][PX]));
    vpj_2 = _mm256_load_pd((double *)(&z[j_2][PX]));
    vpj_3 = _mm256_load_pd((double *)(&z[j_3][PX]));
    vpj_4 = _mm256_load_pd((double *)(&z[j_4][PX]));

    vpj_1 -= vdq_1 * vdf_1;
    vpj_2 -= vdq_2 * vdf_2;
    vpj_3 -= vdq_3 * vdf_3;
    vpj_4 -= vdq_4 * vdf_4;

    vpi += vdq_1 * vdf_1;
    vpi += vdq_2 * vdf_2;
    vpi += vdq_3 * vdf_3;
    vpi += vdq_4 * vdf_4;

    _mm256_store_pd((double *)(&z[j_1][PX]), vpj_1);
    _mm256_store_pd((double *)(&z[j_2][PX]), vpj_2);
    _mm256_store_pd((double *)(&z[j_3][PX]), vpj_3);
    _mm256_store_pd((double *)(&z[j_4][PX]), vpj_4);

    _mm256_store_pd((double *)(&z[i][PX]), vpi);

    for (int k = (np / 4) * 4; k < np; k++) {
      const int j = sorted_list[kp + k];
      double dx = z[j][X] - z[i][X];
      double dy = z[j][Y] - z[i][Y];
      double dz = z[j][Z] - z[i][Z];
      double r2 = (dx * dx + dy * dy + dz * dz);
      double r6 = r2 * r2 * r2;
      double df = ((24.0 * r6 - 48.0) / (r6 * r6 * r2)) * dt;
      if (r2 > CL2) df = 0.0;
      z[i][PX] += df * dx;
      z[i][PY] += df * dy;
      z[i][PZ] += df * dz;
      z[j][PX] -= df * dx;
      z[j][PY] -= df * dy;
      z[j][PZ] -= df * dz;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movslq 0x325ddc8(%rip), %rax   # 0x3261bc0
movq %rax, -0x18(%rsp)
testq %rax, %rax
jle 0x42ca
xorl %esi, %esi
leaq 0x19f3db1(%rip), %rdx   # 0x19f7bc0
leaq 0x90f89ba(%rip), %r8    # 0x90fc7d0
vbroadcastsd 0x1229(%rip), %ymm0 # 0x5048
vbroadcastsd 0x1228(%rip), %ymm1 # 0x5050
vbroadcastsd 0x120f(%rip), %ymm2 # 0x5040
vxorpd %xmm3, %xmm3, %xmm3
vmovsd 0x11eb(%rip), %xmm4     # 0x5028
vmovsd 0x11e9(%rip), %xmm20    # 0x5030
vmovsd 0x11e9(%rip), %xmm6     # 0x5038
vmovsd 0x11e9(%rip), %xmm7     # 0x5040
vxorpd %xmm8, %xmm8, %xmm8
movq %rsi, %r9
shlq $0x6, %r9
vmovapd (%r9,%rdx), %ymm10
leaq 0x8f71f60(%rip), %rax   # 0x8f75dd0
movslq (%rax,%rsi,4), %rbx
movslq (%r8,%rbx,4), %r10
movslq 0x4(%r8,%rbx,4), %rcx
movslq 0x8(%r8,%rbx,4), %rdi
movslq 0xc(%r8,%rbx,4), %r13
movq %r10, %rax
shlq $0x6, %rax
vmovapd (%rax,%rdx), %ymm5
movq %rcx, %rax
shlq $0x6, %rax
vmovapd (%rax,%rdx), %ymm9
movq %rdi, %rax
shlq $0x6, %rax
vmovapd (%rax,%rdx), %ymm13
movq %r13, %rax
shlq $0x6, %rax
vmovapd (%rax,%rdx), %ymm14
leaq 0x325dd12(%rip), %rax   # 0x3261bd0
movslq (%rax,%rsi,4), %r11
vsubpd %ymm10, %ymm5, %ymm11
vsubpd %ymm10, %ymm9, %ymm12
vsubpd %ymm10, %ymm13, %ymm13
vsubpd %ymm10, %ymm14, %ymm14
vunpcklpd %ymm12, %ymm11, %ymm5 # ymm5 = ymm11[0],ymm12[0],ymm11[2],ymm12[2]
vunpcklpd %ymm14, %ymm13, %ymm9 # ymm9 = ymm13[0],ymm14[0],ymm13[2],ymm14[2]
vinsertf128 $0x1, %xmm9, %ymm5, %ymm15
vinsertf32x4 $0x1, %xmm14, %ymm12, %ymm16
vinsertf32x4 $0x1, %xmm13, %ymm11, %ymm17
vunpckhpd %ymm16, %ymm17, %ymm16 # ymm16 = ymm17[1],ymm16[1],ymm17[3],ymm16[3]
vperm2f128 $0x31, %ymm9, %ymm5, %ymm9 # ymm9 = ymm5[2,3],ymm9[2,3]
vmulpd %ymm16, %ymm16, %ymm5
vfmadd231pd %ymm15, %ymm15, %ymm5 # ymm5 = (ymm15 * ymm15) + ymm5
vfmadd231pd %ymm9, %ymm9, %ymm5 # ymm5 = (ymm9 * ymm9) + ymm5
vmulpd %ymm5, %ymm5, %ymm9
vmulpd %ymm9, %ymm5, %ymm16
vmovapd %ymm0, %ymm9
vfmadd213pd %ymm1, %ymm16, %ymm9 # ymm9 = (ymm16 * ymm9) + ymm1
vxorpd %xmm15, %xmm15, %xmm15
cmpq $0x4, %r11
jl 0x3f48
vmulpd %ymm16, %ymm16, %ymm15
vmulpd %ymm5, %ymm15, %ymm15
vdivpd %ymm15, %ymm9, %ymm9
vsubpd %ymm5, %ymm2, %ymm5
vblendvpd %ymm5, %ymm3, %ymm9, %ymm15
movq %rsi, -0x8(%rsp)
vmovapd 0x20(%r9,%rdx), %ymm9
leal 0x3(%r11), %eax
testq %r11, %r11
cmovnsl %r11d, %eax
andl $-0x4, %eax
cmpq $0x8, %r11
movq %rbx, -0x10(%rsp)
movl %eax, -0x1c(%rsp)
jl 0x4114
movl %eax, %esi
leaq (%r8,%rbx,4), %rax
movl $0x4, %r8d
movslq (%rax,%r8,4), %rbx
movslq 0x4(%rax,%r8,4), %r15
movslq 0x8(%rax,%r8,4), %r12
movslq 0xc(%rax,%r8,4), %rbp
movq %rbx, %r14
shlq $0x6, %r14
vmovapd (%r14,%rdx), %ymm5
movq %r15, %r14
shlq $0x6, %r14
vmovapd (%r14,%rdx), %ymm16
movq %r12, %r14
shlq $0x6, %r14
vmovapd (%r14,%rdx), %ymm17
movq %rbp, %r14
shlq $0x6, %r14
vmovapd (%r14,%rdx), %ymm21
vsubpd %ymm10, %ymm5, %ymm19
vsubpd %ymm10, %ymm16, %ymm18
vsubpd %ymm10, %ymm17, %ymm17
vsubpd %ymm10, %ymm21, %ymm16
vunpcklpd %ymm18, %ymm19, %ymm5 # ymm5 = ymm19[0],ymm18[0],ymm19[2],ymm18[2]
vunpcklpd %ymm16, %ymm17, %ymm21 # ymm21 = ymm17[0],ymm16[0],ymm17[2],ymm16[2]
vinsertf32x4 $0x1, %xmm21, %ymm5, %ymm22
vinsertf32x4 $0x1, %xmm16, %ymm18, %ymm23
vinsertf32x4 $0x1, %xmm17, %ymm19, %ymm24
vunpckhpd %ymm23, %ymm24, %ymm23 # ymm23 = ymm24[1],ymm23[1],ymm24[3],ymm23[3]
vshuff64x2 $0x3, %ymm21, %ymm5, %ymm5 # ymm5 = ymm5[2,3],ymm21[2,3]
vmulpd %ymm23, %ymm23, %ymm21
vfmadd231pd %ymm22, %ymm22, %ymm21 # ymm21 = (ymm22 * ymm22) + ymm21
vfmadd231pd %ymm5, %ymm5, %ymm21 # ymm21 = (ymm5 * ymm5) + ymm21
vmulpd %ymm21, %ymm21, %ymm5
vmulpd %ymm5, %ymm21, %ymm22
vsubpd %ymm21, %ymm2, %ymm5
vbroadcastsd %xmm15, %ymm23
vpermpd $0x55, %ymm15, %ymm24   # ymm24 = ymm15[1,1,1,1]
vpermpd $0xaa, %ymm15, %ymm25   # ymm25 = ymm15[2,2,2,2]
vpermpd $0xff, %ymm15, %ymm15   # ymm15 = ymm15[3,3,3,3]
vfmadd231pd %ymm23, %ymm11, %ymm9 # ymm9 = (ymm11 * ymm23) + ymm9
vfmadd231pd %ymm15, %ymm14, %ymm9 # ymm9 = (ymm14 * ymm15) + ymm9
vfmadd231pd %ymm24, %ymm12, %ymm9 # ymm9 = (ymm12 * ymm24) + ymm9
vfmadd231pd %ymm25, %ymm13, %ymm9 # ymm9 = (ymm13 * ymm25) + ymm9
movslq %r10d, %r10
shlq $0x6, %r10
movslq %ecx, %rcx
shlq $0x6, %rcx
movslq %edi, %rdi
shlq $0x6, %rdi
movslq %r13d, %r14
shlq $0x6, %r14
vfnmadd213pd 0x20(%r10,%rdx), %ymm11, %ymm23 # ymm23 = -(ymm11 * ymm23) + mem
vfnmadd213pd 0x20(%rcx,%rdx), %ymm12, %ymm24 # ymm24 = -(ymm12 * ymm24) + mem
vfnmadd213pd 0x20(%rdi,%rdx), %ymm13, %ymm25 # ymm25 = -(ymm13 * ymm25) + mem
vfnmadd213pd 0x20(%r14,%rdx), %ymm14, %ymm15 # ymm15 = -(ymm14 * ymm15) + mem
vmovapd %ymm23, 0x20(%r10,%rdx)
vmovapd %ymm24, 0x20(%rcx,%rdx)
vmovapd %ymm25, 0x20(%rdi,%rdx)
vmovapd %ymm15, 0x20(%r14,%rdx)
vmovapd %ymm0, %ymm11
vfmadd213pd %ymm1, %ymm22, %ymm11 # ymm11 = (ymm22 * ymm11) + ymm1
vmulpd %ymm22, %ymm22, %ymm12
vmulpd %ymm12, %ymm21, %ymm12
vdivpd %ymm12, %ymm11, %ymm11
vblendvpd %ymm5, %ymm3, %ymm11, %ymm15
addq $0x4, %r8
movl %ebx, %r10d
movl %r15d, %ecx
movl %r12d, %edi
movl %ebp, %r13d
vmovapd %ymm19, %ymm11
vmovapd %ymm18, %ymm12
vmovapd %ymm17, %ymm13
vmovapd %ymm16, %ymm14
cmpq %rsi, %r8
jb 0x3f81
jmp 0x4138
vmovapd %ymm14, %ymm16
vmovapd %ymm13, %ymm17
vmovapd %ymm12, %ymm18
vmovapd %ymm11, %ymm19
movl %r13d, %ebp
movl %edi, %r12d
movl %ecx, %r15d
movl %r10d, %ebx
addq %rdx, %r9
vbroadcastsd %xmm15, %ymm5
vpermpd $0x55, %ymm15, %ymm10   # ymm10 = ymm15[1,1,1,1]
vpermpd $0xaa, %ymm15, %ymm11   # ymm11 = ymm15[2,2,2,2]
vpermpd $0xff, %ymm15, %ymm12   # ymm12 = ymm15[3,3,3,3]
movslq %ebx, %rax
shlq $0x6, %rax
movslq %r15d, %rcx
shlq $0x6, %rcx
movslq %r12d, %rsi
shlq $0x6, %rsi
movslq %ebp, %rdi
shlq $0x6, %rdi
vmovapd 0x20(%rax,%rdx), %ymm13
vfnmadd231pd %ymm5, %ymm19, %ymm13 # ymm13 = -(ymm19 * ymm5) + ymm13
vmovapd 0x20(%rcx,%rdx), %ymm14
vfnmadd231pd %ymm10, %ymm18, %ymm14 # ymm14 = -(ymm18 * ymm10) + ymm14
vmovapd 0x20(%rsi,%rdx), %ymm15
vfnmadd231pd %ymm11, %ymm17, %ymm15 # ymm15 = -(ymm17 * ymm11) + ymm15
vmovapd 0x20(%rdi,%rdx), %ymm21
vfnmadd231pd %ymm12, %ymm16, %ymm21 # ymm21 = -(ymm16 * ymm12) + ymm21
vfmadd231pd %ymm5, %ymm19, %ymm9 # ymm9 = (ymm19 * ymm5) + ymm9
vfmadd231pd %ymm10, %ymm18, %ymm9 # ymm9 = (ymm18 * ymm10) + ymm9
vfmadd231pd %ymm11, %ymm17, %ymm9 # ymm9 = (ymm17 * ymm11) + ymm9
vfmadd231pd %ymm12, %ymm16, %ymm9 # ymm9 = (ymm16 * ymm12) + ymm9
vmovapd %ymm13, 0x20(%rax,%rdx)
vmovapd %ymm14, 0x20(%rcx,%rdx)
vmovapd %ymm15, 0x20(%rsi,%rdx)
vmovapd %ymm21, 0x20(%rdi,%rdx)
vmovapd %ymm9, 0x20(%r9)
movl -0x1c(%rsp), %eax
cmpl %r11d, %eax
leaq 0x90f85ea(%rip), %r8    # 0x90fc7d0
movq -0x10(%rsp), %rsi
jge 0x42b7
cltq
addq %rax, %rsi
shlq $0x2, %rsi
subq %rax, %r11
xorl %eax, %eax
leaq (%rsi,%rax,4), %rcx
movslq (%r8,%rcx), %rcx
shlq $0x6, %rcx
vmovsd 0x10(%rcx,%rdx), %xmm5
vsubsd 0x10(%r9), %xmm5, %xmm5
vmovapd (%rcx,%rdx), %xmm9
vsubpd (%r9), %xmm9, %xmm9
vmulpd %xmm9, %xmm9, %xmm10
vshufpd $0x1, %xmm10, %xmm10, %xmm10 # xmm10 = xmm10[1,0]
vfmadd231sd %xmm9, %xmm9, %xmm10 # xmm10 = (xmm9 * xmm9) + xmm10
vfmadd231sd %xmm5, %xmm5, %xmm10 # xmm10 = (xmm5 * xmm5) + xmm10
vmulsd %xmm10, %xmm10, %xmm11
vmulsd %xmm11, %xmm10, %xmm11
vmovapd %xmm4, %xmm12
vmulsd %xmm11, %xmm11, %xmm13
vfmadd213sd %xmm20, %xmm11, %xmm12 # xmm12 = (xmm11 * xmm12) + xmm20
vmulsd %xmm13, %xmm10, %xmm11
vdivsd %xmm11, %xmm12, %xmm11
vmulsd %xmm6, %xmm11, %xmm11
vcmpgtsd %xmm7, %xmm10, %k1
vmovsd %xmm8, %xmm11, %xmm11 {%k1}
vmovddup %xmm11, %xmm10          # xmm10 = xmm11[0,0]
vmovapd 0x20(%r9), %xmm12
vfmadd231pd %xmm9, %xmm10, %xmm12 # xmm12 = (xmm10 * xmm9) + xmm12
vmovapd %xmm12, 0x20(%r9)
vmovsd 0x30(%r9), %xmm12
vfmadd231sd %xmm5, %xmm11, %xmm12 # xmm12 = (xmm11 * xmm5) + xmm12
vmovsd %xmm12, 0x30(%r9)
vfnmadd213pd 0x20(%rcx,%rdx), %xmm10, %xmm9 # xmm9 = -(xmm10 * xmm9) + mem
vmovapd %xmm9, 0x20(%rcx,%rdx)
vfnmadd213sd 0x30(%rcx,%rdx), %xmm5, %xmm11 # xmm11 = -(xmm5 * xmm11) + mem
vmovsd %xmm11, 0x30(%rcx,%rdx)
incq %rax
cmpq %rax, %r11
jne 0x41ff
movq -0x8(%rsp), %rsi
incq %rsi
cmpq -0x18(%rsp), %rsi
jne 0x3e5c
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq