// File: /workspace/github_cpp_makefile_star2_O1/kaityo256[P]lj_simd/force_aos.cpp
// Function: force_sorted_z_avx2()
void
force_sorted_z_avx2(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));
    v4df vpi = _mm256_load_pd((double *)(&z[i][PX]));
    const int np = number_of_partners[i];
    const int kp = pointer[i];
    for (int k = 0; k < (np / 4) * 4; k += 4) {
      const int j_1 = sorted_list[kp + k];
      const int j_2 = sorted_list[kp + k + 1];
      const int j_3 = sorted_list[kp + k + 2];
      const 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);

      v4df vdf_1 = _mm256_permute4x64_pd(vdf, 0);
      v4df vdf_2 = _mm256_permute4x64_pd(vdf, 85);
      v4df vdf_3 = _mm256_permute4x64_pd(vdf, 170);
      v4df vdf_4 = _mm256_permute4x64_pd(vdf, 255);

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

      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 *)(&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:
movslq 0x325e0b9(%rip), %rax   # 0x3261bc0
testq %rax, %rax
jle 0x3de3
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
xorl %ecx, %ecx
leaq 0x19f409d(%rip), %rdx   # 0x19f7bc0
leaq 0x90f8ca6(%rip), %r8    # 0x90fc7d0
vbroadcastsd 0x1515(%rip), %ymm0 # 0x5048
vbroadcastsd 0x1513(%rip), %ymm20 # 0x5050
vbroadcastsd 0x14fa(%rip), %ymm2 # 0x5040
vxorpd %xmm3, %xmm3, %xmm3
vmovsd 0x14d6(%rip), %xmm4     # 0x5028
vmovsd 0x14d6(%rip), %xmm5     # 0x5030
vmovsd 0x14d6(%rip), %xmm6     # 0x5038
vmovsd 0x14d6(%rip), %xmm7     # 0x5040
vxorpd %xmm8, %xmm8, %xmm8
movq %rcx, %r9
shlq $0x6, %r9
vmovapd 0x20(%r9,%rdx), %ymm9
leaq 0x325e04c(%rip), %rsi   # 0x3261bd0
movslq (%rsi,%rcx,4), %r10
leal 0x3(%r10), %ebx
testq %r10, %r10
cmovnsl %r10d, %ebx
andl $-0x4, %ebx
leaq 0x8f72233(%rip), %rsi   # 0x8f75dd0
movslq (%rsi,%rcx,4), %r11
cmpq $0x4, %r10
jl 0x3cf5
vmovapd (%r9,%rdx), %ymm10
movl %ebx, %r14d
leaq 0xc(%r8,%r11,4), %r15
xorl %r12d, %r12d
movslq -0xc(%r15,%r12,4), %r13
movslq -0x8(%r15,%r12,4), %rbp
movslq -0x4(%r15,%r12,4), %rsi
movslq (%r15,%r12,4), %rdi
shlq $0x6, %r13
vmovapd (%r13,%rdx), %ymm11
shlq $0x6, %rbp
vmovapd (%rbp,%rdx), %ymm12
shlq $0x6, %rsi
vmovapd (%rsi,%rdx), %ymm13
shlq $0x6, %rdi
vmovapd (%rdi,%rdx), %ymm14
vsubpd %ymm10, %ymm11, %ymm11
vsubpd %ymm10, %ymm12, %ymm12
vsubpd %ymm10, %ymm13, %ymm13
vsubpd %ymm10, %ymm14, %ymm14
vunpcklpd %ymm12, %ymm11, %ymm15 # ymm15 = ymm11[0],ymm12[0],ymm11[2],ymm12[2]
vunpcklpd %ymm14, %ymm13, %ymm16 # ymm16 = ymm13[0],ymm14[0],ymm13[2],ymm14[2]
vinsertf32x4 $0x1, %xmm16, %ymm15, %ymm17
vinsertf32x4 $0x1, %xmm14, %ymm12, %ymm18
vinsertf32x4 $0x1, %xmm13, %ymm11, %ymm19
vunpckhpd %ymm18, %ymm19, %ymm18 # ymm18 = ymm19[1],ymm18[1],ymm19[3],ymm18[3]
vshuff64x2 $0x3, %ymm16, %ymm15, %ymm15 # ymm15 = ymm15[2,3],ymm16[2,3]
vmulpd %ymm18, %ymm18, %ymm16
vfmadd231pd %ymm17, %ymm17, %ymm16 # ymm16 = (ymm17 * ymm17) + ymm16
vfmadd231pd %ymm15, %ymm15, %ymm16 # ymm16 = (ymm15 * ymm15) + ymm16
vmulpd %ymm16, %ymm16, %ymm15
vmulpd %ymm15, %ymm16, %ymm15
vmovapd %ymm0, %ymm17
vfmadd213pd %ymm20, %ymm15, %ymm17 # ymm17 = (ymm15 * ymm17) + ymm20
vmulpd %ymm15, %ymm15, %ymm15
vmulpd %ymm15, %ymm16, %ymm15
vdivpd %ymm15, %ymm17, %ymm15
vsubpd %ymm16, %ymm2, %ymm1
vblendvpd %ymm1, %ymm3, %ymm15, %ymm1
vbroadcastsd %xmm1, %ymm15
vpermpd $0x55, %ymm1, %ymm16    # ymm16 = ymm1[1,1,1,1]
vpermpd $0xaa, %ymm1, %ymm17    # ymm17 = ymm1[2,2,2,2]
vpermpd $0xff, %ymm1, %ymm1     # ymm1 = ymm1[3,3,3,3]
vfmadd231pd %ymm15, %ymm11, %ymm9 # ymm9 = (ymm11 * ymm15) + ymm9
vfmadd231pd %ymm16, %ymm12, %ymm9 # ymm9 = (ymm12 * ymm16) + ymm9
vfmadd231pd %ymm17, %ymm13, %ymm9 # ymm9 = (ymm13 * ymm17) + ymm9
vfmadd231pd %ymm1, %ymm14, %ymm9 # ymm9 = (ymm14 * ymm1) + ymm9
vfnmadd213pd 0x20(%r13,%rdx), %ymm11, %ymm15 # ymm15 = -(ymm11 * ymm15) + mem
vfnmadd213pd 0x20(%rbp,%rdx), %ymm12, %ymm16 # ymm16 = -(ymm12 * ymm16) + mem
vfnmadd213pd 0x20(%rsi,%rdx), %ymm13, %ymm17 # ymm17 = -(ymm13 * ymm17) + mem
vfnmadd213pd 0x20(%rdi,%rdx), %ymm14, %ymm1 # ymm1 = -(ymm14 * ymm1) + mem
vmovapd %ymm15, 0x20(%r13,%rdx)
vmovapd %ymm16, 0x20(%rbp,%rdx)
vmovapd %ymm17, 0x20(%rsi,%rdx)
vmovapd %ymm1, 0x20(%rdi,%rdx)
addq $0x4, %r12
cmpq %r14, %r12
jb 0x3bbc
addq %rdx, %r9
vmovapd %ymm9, 0x20(%r9)
cmpl %r10d, %ebx
jge 0x3dcd
movslq %ebx, %rsi
addq %rsi, %r11
shlq $0x2, %r11
subq %rsi, %r10
xorl %esi, %esi
leaq (%r11,%rsi,4), %rdi
movslq (%r8,%rdi), %rdi
shlq $0x6, %rdi
vmovsd 0x10(%rdi,%rdx), %xmm1
vsubsd 0x10(%r9), %xmm1, %xmm1
vmovapd (%rdi,%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 %xmm1, %xmm1, %xmm10 # xmm10 = (xmm1 * xmm1) + xmm10
vmulsd %xmm10, %xmm10, %xmm11
vmulsd %xmm11, %xmm10, %xmm11
vmovapd %xmm4, %xmm12
vmulsd %xmm11, %xmm11, %xmm13
vfmadd213sd %xmm5, %xmm11, %xmm12 # xmm12 = (xmm11 * xmm12) + xmm5
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 %xmm1, %xmm11, %xmm12 # xmm12 = (xmm11 * xmm1) + xmm12
vmovsd %xmm12, 0x30(%r9)
vfnmadd213pd 0x20(%rdi,%rdx), %xmm10, %xmm9 # xmm9 = -(xmm10 * xmm9) + mem
vmovapd %xmm9, 0x20(%rdi,%rdx)
vfnmadd213sd 0x30(%rdi,%rdx), %xmm1, %xmm11 # xmm11 = -(xmm1 * xmm11) + mem
vmovsd %xmm11, 0x30(%rdi,%rdx)
incq %rsi
cmpq %rsi, %r10
jne 0x3d16
incq %rcx
cmpq %rax, %rcx
jne 0x3b6f
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq