// File: /workspace/github_cpp_makefile_star2_O2/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
vbroadcastsd 0x1c5a(%rip), %ymm0 # 0x4048
vbroadcastsd 0x1c59(%rip), %ymm1 # 0x4050
vbroadcastsd 0x1c40(%rip), %ymm2 # 0x4040
vmovsd 0x1c20(%rip), %xmm4     # 0x4028
vmovsd 0x1c1e(%rip), %xmm24    # 0x4030
vmovsd 0x1c1c(%rip), %xmm25    # 0x4038
vmovsd 0x1c1c(%rip), %xmm7     # 0x4040
movl 0x325e756(%rip), %eax   # 0x3260b80
leaq 0x18a71f(%rip), %r8     # 0x18cb50
leaq 0xdbf718(%rip), %r9     # 0xdc1b50
leaq 0x90f9351(%rip), %rdi   # 0x90fb790
vxorpd %xmm8, %xmm8, %xmm8
vxorpd %xmm6, %xmm6, %xmm6
xorl %edx, %edx
movl %eax, %ecx
sarl $0x1f, %ecx
andnl %eax, %ecx, %eax
movq %rax, -0x20(%rsp)
cmpq -0x20(%rsp), %rdx
je 0x2866
leaq 0x8f72925(%rip), %rax   # 0x8f74d90
movq %rdx, %rsi
shlq $0x5, %rsi
movq %rdx, -0x8(%rsp)
movslq (%rax,%rdx,4), %r14
vmovapd (%rsi,%r8), %ymm9
movslq 0x4(%rdi,%r14,4), %r10
movslq (%rdi,%r14,4), %rcx
movslq 0x8(%rdi,%r14,4), %r11
movslq 0xc(%rdi,%r14,4), %rbp
movq %r10, %rbx
shlq $0x5, %rbx
movq %rcx, %rax
movq %r11, %r15
shlq $0x5, %rax
shlq $0x5, %r15
vmovapd (%rbx,%r8), %ymm10
movq %rbp, %rbx
shlq $0x5, %rbx
vmovapd (%rax,%r8), %ymm3
vmovapd (%r15,%r8), %ymm11
leaq 0x325e6c7(%rip), %rax   # 0x3260b90
vmovapd (%rbx,%r8), %ymm12
movslq (%rax,%rdx,4), %rax
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, %rax
jl 0x255d
vmulpd %ymm16, %ymm16, %ymm16
vmulpd %ymm16, %ymm10, %ymm16
vsubpd %ymm10, %ymm2, %ymm10
vdivpd %ymm16, %ymm12, %ymm12
vblendvpd %ymm10, %ymm3, %ymm12, %ymm3
movq %rax, -0x10(%rsp)
cltd
movl $0x4, %ebx
vmovapd (%rsi,%r9), %ymm12
leaq (%r9,%rsi), %r12
idivl %ebx
vmovsd 0x10(%rsi,%r8), %xmm10
leaq (%rdi,%r14,4), %rdx
movq %r14, -0x18(%rsp)
movabsq $0x4, %r14
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 %ecx, %r15
movslq %r10d, %rdi
movslq %r11d, %rbx
movslq %ebp, %rsi
cmpq %rax, %r14
jge 0x2719
movslq (%rdx,%r14,4), %rcx
shlq $0x5, %r15
movslq 0x4(%rdx,%r14,4), %r10
movslq 0x8(%rdx,%r14,4), %r11
movslq 0xc(%rdx,%r14,4), %rbp
vbroadcastsd %xmm3, %ymm3
shlq $0x5, %rbx
shlq $0x5, %rsi
shlq $0x5, %rdi
addq $0x4, %r14
vmovapd (%r15,%r9), %ymm23
vmovapd (%rbx,%r9), %ymm26
vmovapd (%rsi,%r9), %ymm5
vfmadd231pd %ymm3, %ymm15, %ymm12 # ymm12 = (ymm15 * ymm3) + ymm12
movq %rcx, %r13
shlq $0x5, %r13
vmovapd (%r13,%r8), %ymm19
movq %r10, %r13
shlq $0x5, %r13
vfmadd231pd %ymm16, %ymm11, %ymm12 # ymm12 = (ymm11 * ymm16) + ymm12
vmovapd (%r13,%r8), %ymm20
movq %r11, %r13
shlq $0x5, %r13
vmovapd (%r13,%r8), %ymm21
movq %rbp, %r13
shlq $0x5, %r13
vfnmadd231pd %ymm3, %ymm15, %ymm23 # ymm23 = -(ymm15 * ymm3) + ymm23
vmovapd (%rdi,%r9), %ymm3
vmovapd (%r13,%r8), %ymm22
vfnmadd231pd %ymm16, %ymm11, %ymm5 # ymm5 = -(ymm11 * ymm16) + ymm5
vfnmadd231pd %ymm17, %ymm13, %ymm26 # ymm26 = -(ymm13 * ymm17) + ymm26
vfmadd231pd %ymm18, %ymm14, %ymm12 # ymm12 = (ymm14 * ymm18) + ymm12
vmovapd %ymm23, (%r15,%r9)
vfmadd231pd %ymm17, %ymm13, %ymm12 # ymm12 = (ymm13 * ymm17) + ymm12
vsubpd %ymm9, %ymm19, %ymm15
vsubpd %ymm9, %ymm21, %ymm13
vfnmadd231pd %ymm18, %ymm14, %ymm3 # ymm3 = -(ymm14 * ymm18) + ymm3
vsubpd %ymm9, %ymm22, %ymm11
vsubpd %ymm9, %ymm20, %ymm14
vinsertf32x4 $0x1, %xmm13, %ymm15, %ymm22
vinsertf32x4 $0x1, %xmm11, %ymm14, %ymm21
vunpcklpd %ymm14, %ymm15, %ymm16 # ymm16 = ymm15[0],ymm14[0],ymm15[2],ymm14[2]
vunpcklpd %ymm11, %ymm13, %ymm19 # ymm19 = ymm13[0],ymm11[0],ymm13[2],ymm11[2]
vinsertf32x4 $0x1, %xmm19, %ymm16, %ymm20
vshuff64x2 $0x3, %ymm19, %ymm16, %ymm16 # ymm16 = ymm16[2,3],ymm19[2,3]
vmovapd %ymm3, (%rdi,%r9)
vmovapd %ymm0, %ymm3
vmovapd %ymm26, (%rbx,%r9)
vmovapd %ymm5, (%rsi,%r9)
vunpckhpd %ymm21, %ymm22, %ymm21 # ymm21 = ymm22[1],ymm21[1],ymm22[3],ymm21[3]
vmulpd %ymm21, %ymm21, %ymm19
vfmadd231pd %ymm20, %ymm20, %ymm19 # ymm19 = (ymm20 * ymm20) + ymm19
vfmadd231pd %ymm16, %ymm16, %ymm19 # ymm19 = (ymm16 * ymm16) + ymm19
vmulpd %ymm19, %ymm19, %ymm16
vsubpd %ymm19, %ymm2, %ymm5
vmulpd %ymm16, %ymm19, %ymm16
vfmadd213pd %ymm1, %ymm16, %ymm3 # ymm3 = (ymm16 * ymm3) + ymm1
vmulpd %ymm16, %ymm16, %ymm16
vmulpd %ymm16, %ymm19, %ymm16
vdivpd %ymm16, %ymm3, %ymm3
vblendvpd %ymm5, %ymm6, %ymm3, %ymm3
jmp 0x2593
vbroadcastsd %xmm3, %ymm3
shlq $0x5, %r15
shlq $0x5, %rdi
shlq $0x5, %rbx
shlq $0x5, %rsi
movq -0x18(%rsp), %rcx
vfmadd231pd %ymm3, %ymm15, %ymm12 # ymm12 = (ymm15 * ymm3) + ymm12
vfnmadd213pd (%r15,%r9), %ymm15, %ymm3 # ymm3 = -(ymm15 * ymm3) + mem
addq %rax, %rcx
vfmadd231pd %ymm18, %ymm14, %ymm12 # ymm12 = (ymm14 * ymm18) + ymm12
vfnmadd213pd (%rdi,%r9), %ymm14, %ymm18 # ymm18 = -(ymm14 * ymm18) + mem
vfmadd231pd %ymm17, %ymm13, %ymm12 # ymm12 = (ymm13 * ymm17) + ymm12
vfnmadd213pd (%rbx,%r9), %ymm13, %ymm17 # ymm17 = -(ymm13 * ymm17) + mem
vfmadd231pd %ymm16, %ymm11, %ymm12 # ymm12 = (ymm11 * ymm16) + ymm12
vfnmadd213pd (%rsi,%r9), %ymm11, %ymm16 # ymm16 = -(ymm11 * ymm16) + mem
vmovapd %ymm3, (%r15,%r9)
vmovapd %ymm18, (%rdi,%r9)
leaq 0x90f9014(%rip), %rdi   # 0x90fb790
leaq (%rdi,%rcx,4), %rdx
movq -0x10(%rsp), %rcx
vmovapd %ymm17, (%rbx,%r9)
vmovapd %ymm16, (%rsi,%r9)
vmovapd %ymm12, (%r12)
cmpq %rcx, %rax
jge 0x2859
movslq (%rdx), %rsi
vmovapd %xmm4, %xmm13
incq %rax
addq $0x4, %rdx
shlq $0x5, %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 (%r12), %xmm13
vmulsd %xmm25, %xmm12, %xmm12
vmovsd %xmm8, %xmm12, %xmm12 {%k1}
vmovddup %xmm12, %xmm11          # xmm11 = xmm12[0,0]
vfmadd231pd %xmm5, %xmm11, %xmm13 # xmm13 = (xmm11 * xmm5) + xmm13
vmovapd %xmm13, (%r12)
vmovsd 0x10(%r12), %xmm13
vfmadd231sd %xmm3, %xmm12, %xmm13 # xmm13 = (xmm12 * xmm3) + xmm13
vmovsd %xmm13, 0x10(%r12)
vfnmadd213pd (%rsi,%r9), %xmm11, %xmm5 # xmm5 = -(xmm11 * xmm5) + mem
vmovapd %xmm5, (%rsi,%r9)
vfnmadd213sd 0x10(%rsi,%r9), %xmm12, %xmm3 # xmm3 = -(xmm12 * xmm3) + mem
vmovsd %xmm3, 0x10(%rsi,%r9)
jmp 0x2799
movq -0x8(%rsp), %rdx
incq %rdx
jmp 0x2459
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq