// File: /workspace/github_cpp_makefile_star2_O2/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
vbroadcastsd 0x14d6(%rip), %ymm0 # 0x4048
vbroadcastsd 0x14d5(%rip), %ymm1 # 0x4050
vbroadcastsd 0x14bb(%rip), %ymm25 # 0x4040
vmovsd 0x149b(%rip), %xmm4     # 0x4028
vmovsd 0x1499(%rip), %xmm24    # 0x4030
vmovsd 0x1499(%rip), %xmm6     # 0x4038
vmovsd 0x1499(%rip), %xmm7     # 0x4040
movl 0x325dfd3(%rip), %eax   # 0x3260b80
leaq 0x19f3fcc(%rip), %r8    # 0x19f6b80
leaq 0x90f8bd5(%rip), %rdi   # 0x90fb790
vxorpd %xmm8, %xmm8, %xmm8
vxorpd %xmm2, %xmm2, %xmm2
movl %eax, %ecx
sarl $0x1f, %ecx
andnl %eax, %ecx, %eax
xorl %ecx, %ecx
movq %rax, -0x18(%rsp)
cmpq -0x18(%rsp), %rcx
je 0x2ff2
leaq 0x8f721a9(%rip), %rax   # 0x8f74d90
movq %rcx, %rsi
shlq $0x6, %rsi
movq %rcx, -0x8(%rsp)
movslq (%rax,%rcx,4), %rbx
vmovapd (%rsi,%r8), %ymm9
movslq 0x4(%rdi,%rbx,4), %rbp
movslq (%rdi,%rbx,4), %r15
movslq 0x8(%rdi,%rbx,4), %r9
movslq 0xc(%rdi,%rbx,4), %r10
movq %rbp, %rdx
shlq $0x6, %rdx
movq %r15, %rax
movq %r9, %r11
shlq $0x6, %rax
shlq $0x6, %r11
vmovapd (%rdx,%r8), %ymm10
movq %r10, %rdx
shlq $0x6, %rdx
vmovapd (%rax,%r8), %ymm3
vmovapd (%r11,%r8), %ymm11
leaq 0x325df4b(%rip), %rax   # 0x3260b90
vmovapd (%rdx,%r8), %ymm12
movslq (%rax,%rcx,4), %rcx
vsubpd %ymm9, %ymm11, %ymm13
vsubpd %ymm9, %ymm3, %ymm15
vsubpd %ymm9, %ymm10, %ymm14
vsubpd %ymm9, %ymm12, %ymm11
vinsertf32x4 $0x1, %xmm13, %ymm15, %ymm17
vunpcklpd %ymm14, %ymm15, %ymm3 # ymm3 = ymm15[0],ymm14[0],ymm15[2],ymm14[2]
vinsertf32x4 $0x1, %xmm11, %ymm14, %ymm16
vunpcklpd %ymm11, %ymm13, %ymm10 # ymm10 = ymm13[0],ymm11[0],ymm13[2],ymm11[2]
vinsertf128 $0x1, %xmm10, %ymm3, %ymm12
vperm2f128 $0x31, %ymm10, %ymm3, %ymm3 # ymm3 = ymm3[2,3],ymm10[2,3]
vunpckhpd %ymm16, %ymm17, %ymm16 # ymm16 = ymm17[1],ymm16[1],ymm17[3],ymm16[3]
vmulpd %ymm16, %ymm16, %ymm10
vfmadd231pd %ymm12, %ymm12, %ymm10 # ymm10 = (ymm12 * ymm12) + ymm10
vmovapd %ymm0, %ymm12
vfmadd231pd %ymm3, %ymm3, %ymm10 # ymm10 = (ymm3 * ymm3) + ymm10
vmulpd %ymm10, %ymm10, %ymm3
vmulpd %ymm3, %ymm10, %ymm16
vxorpd %xmm3, %xmm3, %xmm3
vfmadd213pd %ymm1, %ymm16, %ymm12 # ymm12 = (ymm16 * ymm12) + ymm1
cmpq $0x4, %rcx
jl 0x2cda
vmulpd %ymm16, %ymm16, %ymm16
vmulpd %ymm16, %ymm10, %ymm16
vsubpd %ymm10, %ymm25, %ymm10
vdivpd %ymm16, %ymm12, %ymm12
vblendvpd %ymm10, %ymm3, %ymm12, %ymm3
movl %ecx, %eax
cltd
movl $0x4, %r11d
vmovapd 0x20(%rsi,%r8), %ymm12
leaq (%r8,%rsi), %r12
movabsq $0x4, %r14
idivl %r11d
vmovsd 0x10(%rsi,%r8), %xmm10
leaq (%rdi,%rbx,4), %rdx
movq %rbx, -0x10(%rsp)
shll $0x2, %eax
cltq
vpermpd $0x55, %ymm3, %ymm18    # ymm18 = ymm3[1,1,1,1]
vpermpd $0xaa, %ymm3, %ymm17    # ymm17 = ymm3[2,2,2,2]
vpermpd $0xff, %ymm3, %ymm16    # ymm16 = ymm3[3,3,3,3]
movslq %r15d, %rdi
movslq %ebp, %r11
movslq %r9d, %rsi
movslq %r10d, %rbx
cmpq %rax, %r14
jge 0x2e88
movslq (%rdx,%r14,4), %r15
movslq 0x4(%rdx,%r14,4), %rbp
movslq 0x8(%rdx,%r14,4), %r9
movslq 0xc(%rdx,%r14,4), %r10
vbroadcastsd %xmm3, %ymm3
shlq $0x6, %rdi
shlq $0x6, %r11
shlq $0x6, %rsi
shlq $0x6, %rbx
addq $0x4, %r14
vfmadd231pd %ymm3, %ymm15, %ymm12 # ymm12 = (ymm15 * ymm3) + ymm12
vfnmadd213pd 0x20(%rdi,%r8), %ymm15, %ymm3 # ymm3 = -(ymm15 * ymm3) + mem
movq %r15, %r13
shlq $0x6, %r13
vmovapd (%r13,%r8), %ymm19
movq %rbp, %r13
shlq $0x6, %r13
vfmadd231pd %ymm16, %ymm11, %ymm12 # ymm12 = (ymm11 * ymm16) + ymm12
vfnmadd213pd 0x20(%rbx,%r8), %ymm11, %ymm16 # ymm16 = -(ymm11 * ymm16) + mem
vmovapd (%r13,%r8), %ymm20
movq %r9, %r13
shlq $0x6, %r13
vmovapd (%r13,%r8), %ymm21
movq %r10, %r13
shlq $0x6, %r13
vmovapd (%r13,%r8), %ymm22
vfmadd231pd %ymm18, %ymm14, %ymm12 # ymm12 = (ymm14 * ymm18) + ymm12
vfnmadd213pd 0x20(%r11,%r8), %ymm14, %ymm18 # ymm18 = -(ymm14 * ymm18) + mem
vfmadd231pd %ymm17, %ymm13, %ymm12 # ymm12 = (ymm13 * ymm17) + ymm12
vfnmadd213pd 0x20(%rsi,%r8), %ymm13, %ymm17 # ymm17 = -(ymm13 * ymm17) + mem
vsubpd %ymm9, %ymm19, %ymm15
vmovapd %ymm3, 0x20(%rdi,%r8)
vmovapd %ymm0, %ymm3
vsubpd %ymm9, %ymm20, %ymm14
vsubpd %ymm9, %ymm21, %ymm13
vsubpd %ymm9, %ymm22, %ymm11
vunpcklpd %ymm14, %ymm15, %ymm19 # ymm19 = ymm15[0],ymm14[0],ymm15[2],ymm14[2]
vinsertf32x4 $0x1, %xmm13, %ymm15, %ymm23
vinsertf32x4 $0x1, %xmm11, %ymm14, %ymm22
vunpcklpd %ymm11, %ymm13, %ymm20 # ymm20 = ymm13[0],ymm11[0],ymm13[2],ymm11[2]
vinsertf32x4 $0x1, %xmm20, %ymm19, %ymm21
vshuff64x2 $0x3, %ymm20, %ymm19, %ymm19 # ymm19 = ymm19[2,3],ymm20[2,3]
vmovapd %ymm18, 0x20(%r11,%r8)
vunpckhpd %ymm22, %ymm23, %ymm22 # ymm22 = ymm23[1],ymm22[1],ymm23[3],ymm22[3]
vmulpd %ymm22, %ymm22, %ymm20
vmovapd %ymm17, 0x20(%rsi,%r8)
vmovapd %ymm16, 0x20(%rbx,%r8)
vfmadd231pd %ymm21, %ymm21, %ymm20 # ymm20 = (ymm21 * ymm21) + ymm20
vfmadd231pd %ymm19, %ymm19, %ymm20 # ymm20 = (ymm19 * ymm19) + ymm20
vmulpd %ymm20, %ymm20, %ymm19
vsubpd %ymm20, %ymm25, %ymm5
vmulpd %ymm19, %ymm20, %ymm19
vmulpd %ymm19, %ymm19, %ymm16
vfmadd213pd %ymm1, %ymm19, %ymm3 # ymm3 = (ymm19 * ymm3) + ymm1
vmulpd %ymm16, %ymm20, %ymm16
vdivpd %ymm16, %ymm3, %ymm3
vblendvpd %ymm5, %ymm2, %ymm3, %ymm3
jmp 0x2d10
vbroadcastsd %xmm3, %ymm3
shlq $0x6, %rdi
shlq $0x6, %r11
shlq $0x6, %rsi
shlq $0x6, %rbx
movq -0x10(%rsp), %rdx
vmovapd 0x20(%rdi,%r8), %ymm5
vmovapd 0x20(%r11,%r8), %ymm19
vmovapd 0x20(%rsi,%r8), %ymm20
vmovapd 0x20(%rbx,%r8), %ymm21
vfmadd231pd %ymm3, %ymm15, %ymm12 # ymm12 = (ymm15 * ymm3) + ymm12
addq %rax, %rdx
vfmadd231pd %ymm18, %ymm14, %ymm12 # ymm12 = (ymm14 * ymm18) + ymm12
vfnmadd231pd %ymm3, %ymm15, %ymm5 # ymm5 = -(ymm15 * ymm3) + ymm5
vfnmadd231pd %ymm18, %ymm14, %ymm19 # ymm19 = -(ymm14 * ymm18) + ymm19
vfnmadd231pd %ymm17, %ymm13, %ymm20 # ymm20 = -(ymm13 * ymm17) + ymm20
vfnmadd231pd %ymm16, %ymm11, %ymm21 # ymm21 = -(ymm11 * ymm16) + ymm21
vfmadd231pd %ymm17, %ymm13, %ymm12 # ymm12 = (ymm13 * ymm17) + ymm12
vmovapd %ymm5, 0x20(%rdi,%r8)
leaq 0x90f8896(%rip), %rdi   # 0x90fb790
vmovapd %ymm19, 0x20(%r11,%r8)
vmovapd %ymm20, 0x20(%rsi,%r8)
vmovapd %ymm21, 0x20(%rbx,%r8)
vfmadd231pd %ymm16, %ymm11, %ymm12 # ymm12 = (ymm11 * ymm16) + ymm12
leaq (%rdi,%rdx,4), %rdx
vmovapd %ymm12, 0x20(%r12)
cmpq %rcx, %rax
jge 0x2fe5
movslq (%rdx), %rsi
vmovapd %xmm4, %xmm13
incq %rax
addq $0x4, %rdx
shlq $0x6, %rsi
vmovapd (%rsi,%r8), %xmm5
vmovsd 0x10(%rsi,%r8), %xmm3
vsubpd %xmm9, %xmm5, %xmm5
vsubsd %xmm10, %xmm3, %xmm3
vmulpd %xmm5, %xmm5, %xmm11
vshufpd $0x1, %xmm11, %xmm11, %xmm11 # xmm11 = xmm11[1,0]
vfmadd231sd %xmm5, %xmm5, %xmm11 # xmm11 = (xmm5 * xmm5) + xmm11
vfmadd231sd %xmm3, %xmm3, %xmm11 # xmm11 = (xmm3 * xmm3) + xmm11
vmulsd %xmm11, %xmm11, %xmm12
vcmpgtsd %xmm7, %xmm11, %k1
vmulsd %xmm12, %xmm11, %xmm12
vfmadd213sd %xmm24, %xmm12, %xmm13 # xmm13 = (xmm12 * xmm13) + xmm24
vmulsd %xmm12, %xmm12, %xmm12
vmulsd %xmm12, %xmm11, %xmm12
vdivsd %xmm12, %xmm13, %xmm12
vmovapd 0x20(%r12), %xmm13
vmulsd %xmm6, %xmm12, %xmm12
vmovsd %xmm8, %xmm12, %xmm12 {%k1}
vmovddup %xmm12, %xmm11          # xmm11 = xmm12[0,0]
vfmadd231pd %xmm5, %xmm11, %xmm13 # xmm13 = (xmm11 * xmm5) + xmm13
vmovapd %xmm13, 0x20(%r12)
vmovsd 0x30(%r12), %xmm13
vfmadd231sd %xmm3, %xmm12, %xmm13 # xmm13 = (xmm12 * xmm3) + xmm13
vmovsd %xmm13, 0x30(%r12)
vfnmadd213pd 0x20(%rsi,%r8), %xmm11, %xmm5 # xmm5 = -(xmm11 * xmm5) + mem
vmovapd %xmm5, 0x20(%rsi,%r8)
vfnmadd213sd 0x30(%rsi,%r8), %xmm12, %xmm3 # xmm3 = -(xmm12 * xmm3) + mem
vmovsd %xmm3, 0x30(%rsi,%r8)
jmp 0x2f23
movq -0x8(%rsp), %rcx
incq %rcx
jmp 0x2bd5
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq