// File: /workspace/github_cpp_makefile_star2_O1/kaityo256[P]lj_simd/force_aos.cpp
// Function: force_avx2_swp()
void
force_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 *)(&q[i][X]));
    v4df vpi = _mm256_load_pd((double *)(&p[i][X]));
    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 *)(&q[j_1][X]));
    v4df vqj_2 = _mm256_load_pd((double *)(&q[j_2][X]));
    v4df vqj_3 = _mm256_load_pd((double *)(&q[j_3][X]));
    v4df vqj_4 = _mm256_load_pd((double *)(&q[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 *)(&q[j_1_b][X]));
      vqj_2 = _mm256_load_pd((double *)(&q[j_2_b][X]));
      vqj_3 = _mm256_load_pd((double *)(&q[j_3_b][X]));
      vqj_4 = _mm256_load_pd((double *)(&q[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;

      // --- 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 *)(&p[j_1][X]));
      vpj_2 = _mm256_load_pd((double *)(&p[j_2][X]));
      vpj_3 = _mm256_load_pd((double *)(&p[j_3][X]));
      vpj_4 = _mm256_load_pd((double *)(&p[j_4][X]));

      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_4 * vdf_4;
      vpi += vdq_2 * vdf_2;
      vpi += vdq_3 * vdf_3;

      _mm256_store_pd((double *)(&p[j_1][X]), vpj_1);
      _mm256_store_pd((double *)(&p[j_2][X]), vpj_2);
      _mm256_store_pd((double *)(&p[j_3][X]), vpj_3);
      _mm256_store_pd((double *)(&p[j_4][X]), 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);
      mask = vcl2 - 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 *)(&p[j_1][X]));
    vpj_2 = _mm256_load_pd((double *)(&p[j_2][X]));
    vpj_3 = _mm256_load_pd((double *)(&p[j_3][X]));
    vpj_4 = _mm256_load_pd((double *)(&p[j_4][X]));

    vpi += vdq_1 * vdf_1;
    vpi += vdq_2 * vdf_2;
    vpi += vdq_3 * vdf_3;
    vpi += vdq_4 * vdf_4;
    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 *)(&p[j_1][X]), vpj_1);
    _mm256_store_pd((double *)(&p[j_2][X]), vpj_2);
    _mm256_store_pd((double *)(&p[j_3][X]), vpj_3);
    _mm256_store_pd((double *)(&p[j_4][X]), vpj_4);

    _mm256_store_pd((double *)(&p[i][X]), vpi);

    for (int k = (np / 4) * 4; k < np; k++) {
      const int j = sorted_list[kp + k];
      double dx = q[j][X] - q[i][X];
      double dy = q[j][Y] - q[i][Y];
      double dz = q[j][Z] - q[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;
      p[i][X] += df * dx;
      p[i][Y] += df * dy;
      p[i][Z] += df * dz;
      p[j][X] -= df * dx;
      p[j][Y] -= df * dy;
      p[j][Z] -= df * dz;
    }
  }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movslq 0x325e5b4(%rip), %rax   # 0x3261bc0
movq %rax, -0x20(%rsp)
testq %rax, %rax
jle 0x3af2
xorl %edi, %edi
leaq 0x18a56d(%rip), %rdx    # 0x18db90
leaq 0xdbf566(%rip), %rsi    # 0xdc2b90
leaq 0x90f919f(%rip), %r10   # 0x90fc7d0
vbroadcastsd 0x1a0e(%rip), %ymm0 # 0x5048
vbroadcastsd 0x1a0d(%rip), %ymm1 # 0x5050
vbroadcastsd 0x19f4(%rip), %ymm2 # 0x5040
vxorpd %xmm3, %xmm3, %xmm3
vmovsd 0x19d0(%rip), %xmm4     # 0x5028
vmovsd 0x19d0(%rip), %xmm5     # 0x5030
vmovsd 0x19d0(%rip), %xmm6     # 0x5038
vmovsd 0x19d0(%rip), %xmm7     # 0x5040
vxorpd %xmm8, %xmm8, %xmm8
movq %rdi, %r14
shlq $0x5, %r14
vmovapd (%r14,%rdx), %ymm10
leaq 0x8f72747(%rip), %rax   # 0x8f75dd0
movslq (%rax,%rdi,4), %rbx
movslq (%r10,%rbx,4), %rbp
movslq 0x4(%r10,%rbx,4), %r9
movslq 0x8(%r10,%rbx,4), %rcx
movslq 0xc(%r10,%rbx,4), %r8
movq %rbp, %rax
shlq $0x5, %rax
vmovapd (%rax,%rdx), %ymm9
movq %r9, %rax
shlq $0x5, %rax
vmovapd (%rax,%rdx), %ymm11
movq %rcx, %rax
shlq $0x5, %rax
vmovapd (%rax,%rdx), %ymm13
movq %r8, %rax
shlq $0x5, %rax
vmovapd (%rax,%rdx), %ymm14
leaq 0x325e4f9(%rip), %rax   # 0x3261bd0
movslq (%rax,%rdi,4), %r11
vsubpd %ymm10, %ymm9, %ymm12
vsubpd %ymm10, %ymm11, %ymm11
vsubpd %ymm10, %ymm13, %ymm13
vsubpd %ymm10, %ymm14, %ymm14
vunpcklpd %ymm11, %ymm12, %ymm9 # ymm9 = ymm12[0],ymm11[0],ymm12[2],ymm11[2]
vunpcklpd %ymm14, %ymm13, %ymm15 # ymm15 = ymm13[0],ymm14[0],ymm13[2],ymm14[2]
vinsertf32x4 $0x1, %xmm15, %ymm9, %ymm16
vinsertf32x4 $0x1, %xmm14, %ymm11, %ymm17
vinsertf32x4 $0x1, %xmm13, %ymm12, %ymm18
vunpckhpd %ymm17, %ymm18, %ymm17 # ymm17 = ymm18[1],ymm17[1],ymm18[3],ymm17[3]
vperm2f128 $0x31, %ymm15, %ymm9, %ymm15 # ymm15 = ymm9[2,3],ymm15[2,3]
vmulpd %ymm17, %ymm17, %ymm9
vfmadd231pd %ymm16, %ymm16, %ymm9 # ymm9 = (ymm16 * ymm16) + ymm9
vfmadd231pd %ymm15, %ymm15, %ymm9 # ymm9 = (ymm15 * ymm15) + ymm9
vmulpd %ymm9, %ymm9, %ymm15
vmulpd %ymm15, %ymm9, %ymm17
vmovapd %ymm0, %ymm16
vfmadd213pd %ymm1, %ymm17, %ymm16 # ymm16 = (ymm17 * ymm16) + ymm1
vxorpd %xmm15, %xmm15, %xmm15
cmpq $0x4, %r11
jl 0x3769
vmulpd %ymm17, %ymm17, %ymm15
vmulpd %ymm15, %ymm9, %ymm15
vdivpd %ymm15, %ymm16, %ymm15
vsubpd %ymm9, %ymm2, %ymm9
vblendvpd %ymm9, %ymm3, %ymm15, %ymm15
movq %rdi, -0x8(%rsp)
movq %r14, -0x18(%rsp)
vmovapd (%r14,%rsi), %ymm9
leal 0x3(%r11), %eax
testq %r11, %r11
cmovnsl %r11d, %eax
andl $-0x4, %eax
cmpq $0x8, %r11
movq %rbx, -0x10(%rsp)
movl %eax, -0x24(%rsp)
jl 0x394d
movl %eax, %edi
leaq (%r10,%rbx,4), %rax
movl $0x4, %ebx
movslq (%rax,%rbx,4), %r10
movslq 0x4(%rax,%rbx,4), %r15
movslq 0x8(%rax,%rbx,4), %r12
movslq 0xc(%rax,%rbx,4), %r13
movq %r10, %r14
shlq $0x5, %r14
vmovapd (%r14,%rdx), %ymm16
movq %r15, %r14
shlq $0x5, %r14
vmovapd (%r14,%rdx), %ymm17
movq %r12, %r14
shlq $0x5, %r14
vmovapd (%r14,%rdx), %ymm20
movq %r13, %r14
shlq $0x5, %r14
vmovapd (%r14,%rdx), %ymm21
vsubpd %ymm10, %ymm16, %ymm19
vsubpd %ymm10, %ymm17, %ymm18
vsubpd %ymm10, %ymm20, %ymm17
vsubpd %ymm10, %ymm21, %ymm16
vunpcklpd %ymm18, %ymm19, %ymm20 # ymm20 = ymm19[0],ymm18[0],ymm19[2],ymm18[2]
vunpcklpd %ymm16, %ymm17, %ymm21 # ymm21 = ymm17[0],ymm16[0],ymm17[2],ymm16[2]
vinsertf32x4 $0x1, %xmm21, %ymm20, %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, %ymm20, %ymm20 # ymm20 = ymm20[2,3],ymm21[2,3]
vmulpd %ymm23, %ymm23, %ymm21
vfmadd231pd %ymm22, %ymm22, %ymm21 # ymm21 = (ymm22 * ymm22) + ymm21
vfmadd231pd %ymm20, %ymm20, %ymm21 # ymm21 = (ymm20 * ymm20) + ymm21
vmulpd %ymm21, %ymm21, %ymm20
vmulpd %ymm20, %ymm21, %ymm20
vbroadcastsd %xmm15, %ymm22
vpermpd $0x55, %ymm15, %ymm23   # ymm23 = ymm15[1,1,1,1]
vpermpd $0xaa, %ymm15, %ymm24   # ymm24 = ymm15[2,2,2,2]
vpermpd $0xff, %ymm15, %ymm15   # ymm15 = ymm15[3,3,3,3]
movslq %ebp, %rbp
shlq $0x5, %rbp
movslq %r9d, %r9
shlq $0x5, %r9
movslq %ecx, %rcx
shlq $0x5, %rcx
movslq %r8d, %r8
shlq $0x5, %r8
vmovapd (%rbp,%rsi), %ymm25
vfnmadd231pd %ymm22, %ymm12, %ymm25 # ymm25 = -(ymm12 * ymm22) + ymm25
vmovapd (%r9,%rsi), %ymm26
vfnmadd231pd %ymm23, %ymm11, %ymm26 # ymm26 = -(ymm11 * ymm23) + ymm26
vmovapd (%rcx,%rsi), %ymm27
vfnmadd231pd %ymm24, %ymm13, %ymm27 # ymm27 = -(ymm13 * ymm24) + ymm27
vmovapd (%r8,%rsi), %ymm28
vfnmadd231pd %ymm15, %ymm14, %ymm28 # ymm28 = -(ymm14 * ymm15) + ymm28
vfmadd231pd %ymm22, %ymm12, %ymm9 # ymm9 = (ymm12 * ymm22) + ymm9
vfmadd231pd %ymm15, %ymm14, %ymm9 # ymm9 = (ymm14 * ymm15) + ymm9
vfmadd231pd %ymm23, %ymm11, %ymm9 # ymm9 = (ymm11 * ymm23) + ymm9
vfmadd231pd %ymm24, %ymm13, %ymm9 # ymm9 = (ymm13 * ymm24) + ymm9
vmovapd %ymm25, (%rbp,%rsi)
vmovapd %ymm26, (%r9,%rsi)
vmovapd %ymm27, (%rcx,%rsi)
vmovapd %ymm28, (%r8,%rsi)
vmovapd %ymm0, %ymm11
vfmadd213pd %ymm1, %ymm20, %ymm11 # ymm11 = (ymm20 * ymm11) + ymm1
vmulpd %ymm20, %ymm20, %ymm12
vmulpd %ymm12, %ymm21, %ymm12
vdivpd %ymm12, %ymm11, %ymm11
vsubpd %ymm21, %ymm2, %ymm12
vblendvpd %ymm12, %ymm3, %ymm11, %ymm15
addq $0x4, %rbx
movl %r10d, %ebp
movl %r15d, %r9d
movl %r12d, %ecx
movl %r13d, %r8d
vmovapd %ymm19, %ymm12
vmovapd %ymm18, %ymm11
vmovapd %ymm17, %ymm13
vmovapd %ymm16, %ymm14
cmpq %rdi, %rbx
jb 0x37a5
jmp 0x3971
vmovapd %ymm14, %ymm16
vmovapd %ymm13, %ymm17
vmovapd %ymm11, %ymm18
vmovapd %ymm12, %ymm19
movl %r8d, %r13d
movl %ecx, %r12d
movl %r9d, %r15d
movl %ebp, %r10d
movq -0x18(%rsp), %rbx
leaq (%rsi,%rbx), %rax
vbroadcastsd %xmm15, %ymm10
vpermpd $0x55, %ymm15, %ymm11   # ymm11 = ymm15[1,1,1,1]
vpermpd $0xaa, %ymm15, %ymm12   # ymm12 = ymm15[2,2,2,2]
vpermpd $0xff, %ymm15, %ymm13   # ymm13 = ymm15[3,3,3,3]
movslq %r10d, %rcx
shlq $0x5, %rcx
movslq %r15d, %rdi
shlq $0x5, %rdi
movslq %r12d, %r8
shlq $0x5, %r8
movslq %r13d, %r9
shlq $0x5, %r9
vfmadd231pd %ymm10, %ymm19, %ymm9 # ymm9 = (ymm19 * ymm10) + ymm9
vfmadd231pd %ymm11, %ymm18, %ymm9 # ymm9 = (ymm18 * ymm11) + ymm9
vfmadd231pd %ymm12, %ymm17, %ymm9 # ymm9 = (ymm17 * ymm12) + ymm9
vfmadd231pd %ymm13, %ymm16, %ymm9 # ymm9 = (ymm16 * ymm13) + ymm9
vfnmadd213pd (%rcx,%rsi), %ymm19, %ymm10 # ymm10 = -(ymm19 * ymm10) + mem
vfnmadd213pd (%rdi,%rsi), %ymm18, %ymm11 # ymm11 = -(ymm18 * ymm11) + mem
vfnmadd213pd (%r8,%rsi), %ymm17, %ymm12 # ymm12 = -(ymm17 * ymm12) + mem
vfnmadd213pd (%r9,%rsi), %ymm16, %ymm13 # ymm13 = -(ymm16 * ymm13) + mem
vmovapd %ymm10, (%rcx,%rsi)
vmovapd %ymm11, (%rdi,%rsi)
vmovapd %ymm12, (%r8,%rsi)
vmovapd %ymm13, (%r9,%rsi)
vmovapd %ymm9, (%rax)
movl -0x24(%rsp), %ecx
cmpl %r11d, %ecx
leaq 0x90f8dc7(%rip), %r10   # 0x90fc7d0
movq -0x10(%rsp), %r8
jge 0x3adf
addq %rdx, %rbx
vmovapd (%rbx), %xmm9
vmovsd 0x10(%rbx), %xmm10
movslq %ecx, %rcx
addq %rcx, %r8
shlq $0x2, %r8
subq %rcx, %r11
xorl %ecx, %ecx
leaq (%r8,%rcx,4), %rdi
movslq (%r10,%rdi), %rdi
shlq $0x5, %rdi
vmovsd 0x10(%rdi,%rdx), %xmm11
vmovapd (%rdi,%rdx), %xmm12
vsubpd %xmm9, %xmm12, %xmm12
vmulpd %xmm12, %xmm12, %xmm13
vshufpd $0x1, %xmm13, %xmm13, %xmm13 # xmm13 = xmm13[1,0]
vfmadd231sd %xmm12, %xmm12, %xmm13 # xmm13 = (xmm12 * xmm12) + xmm13
vsubsd %xmm10, %xmm11, %xmm11
vfmadd231sd %xmm11, %xmm11, %xmm13 # xmm13 = (xmm11 * xmm11) + xmm13
vmulsd %xmm13, %xmm13, %xmm14
vmulsd %xmm14, %xmm13, %xmm14
vmovapd %xmm4, %xmm15
vmulsd %xmm14, %xmm14, %xmm16
vfmadd213sd %xmm5, %xmm14, %xmm15 # xmm15 = (xmm14 * xmm15) + xmm5
vmulsd %xmm16, %xmm13, %xmm14
vdivsd %xmm14, %xmm15, %xmm14
vmulsd %xmm6, %xmm14, %xmm14
vcmpgtsd %xmm7, %xmm13, %k1
vmovsd %xmm8, %xmm14, %xmm14 {%k1}
vmovddup %xmm14, %xmm13          # xmm13 = xmm14[0,0]
vmovapd (%rax), %xmm15
vfmadd231pd %xmm12, %xmm13, %xmm15 # xmm15 = (xmm13 * xmm12) + xmm15
vmovapd %xmm15, (%rax)
vmovsd 0x10(%rax), %xmm15
vfmadd231sd %xmm11, %xmm14, %xmm15 # xmm15 = (xmm14 * xmm11) + xmm15
vmovsd %xmm15, 0x10(%rax)
vfnmadd213pd (%rdi,%rsi), %xmm13, %xmm12 # xmm12 = -(xmm13 * xmm12) + mem
vmovapd %xmm12, (%rdi,%rsi)
vfnmadd213sd 0x10(%rdi,%rsi), %xmm11, %xmm14 # xmm14 = -(xmm11 * xmm14) + mem
vmovsd %xmm14, 0x10(%rdi,%rsi)
incq %rcx
cmpq %rcx, %r11
jne 0x3a2f
movq -0x8(%rsp), %rdi
incq %rdi
cmpq -0x20(%rsp), %rdi
jne 0x3675
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq