Introduce gmxpre.h for truly global definitions
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_avx_256_double / nb_kernel_ElecEw_VdwLJ_GeomP1P1_avx_256_double.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*
36  * Note: this file was generated by the GROMACS avx_256_double kernel generator.
37  */
38 #include "gmxpre.h"
39
40 #include "config.h"
41
42 #include <math.h>
43
44 #include "../nb_kernel.h"
45 #include "gromacs/legacyheaders/types/simple.h"
46 #include "gromacs/math/vec.h"
47 #include "gromacs/legacyheaders/nrnb.h"
48
49 #include "gromacs/simd/math_x86_avx_256_double.h"
50 #include "kernelutil_x86_avx_256_double.h"
51
52 /*
53  * Gromacs nonbonded kernel:   nb_kernel_ElecEw_VdwLJ_GeomP1P1_VF_avx_256_double
54  * Electrostatics interaction: Ewald
55  * VdW interaction:            LennardJones
56  * Geometry:                   Particle-Particle
57  * Calculate force/pot:        PotentialAndForce
58  */
59 void
60 nb_kernel_ElecEw_VdwLJ_GeomP1P1_VF_avx_256_double
61                     (t_nblist                    * gmx_restrict       nlist,
62                      rvec                        * gmx_restrict          xx,
63                      rvec                        * gmx_restrict          ff,
64                      t_forcerec                  * gmx_restrict          fr,
65                      t_mdatoms                   * gmx_restrict     mdatoms,
66                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
67                      t_nrnb                      * gmx_restrict        nrnb)
68 {
69     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
70      * just 0 for non-waters.
71      * Suffixes A,B,C,D refer to j loop unrolling done with AVX, e.g. for the four different
72      * jnr indices corresponding to data put in the four positions in the SIMD register.
73      */
74     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
75     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
76     int              jnrA,jnrB,jnrC,jnrD;
77     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
78     int              jnrlistE,jnrlistF,jnrlistG,jnrlistH;
79     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
80     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
81     real             rcutoff_scalar;
82     real             *shiftvec,*fshift,*x,*f;
83     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
84     real             scratch[4*DIM];
85     __m256d          tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
86     real *           vdwioffsetptr0;
87     __m256d          ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
88     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
89     __m256d          jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
90     __m256d          dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
91     __m256d          velec,felec,velecsum,facel,crf,krf,krf2;
92     real             *charge;
93     int              nvdwtype;
94     __m256d          rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
95     int              *vdwtype;
96     real             *vdwparam;
97     __m256d          one_sixth   = _mm256_set1_pd(1.0/6.0);
98     __m256d          one_twelfth = _mm256_set1_pd(1.0/12.0);
99     __m128i          ewitab;
100     __m256d          ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
101     __m256d          beta,beta2,beta3,zeta2,pmecorrF,pmecorrV,rinv3;
102     real             *ewtab;
103     __m256d          dummy_mask,cutoff_mask;
104     __m128           tmpmask0,tmpmask1;
105     __m256d          signbit = _mm256_castsi256_pd( _mm256_set1_epi32(0x80000000) );
106     __m256d          one     = _mm256_set1_pd(1.0);
107     __m256d          two     = _mm256_set1_pd(2.0);
108     x                = xx[0];
109     f                = ff[0];
110
111     nri              = nlist->nri;
112     iinr             = nlist->iinr;
113     jindex           = nlist->jindex;
114     jjnr             = nlist->jjnr;
115     shiftidx         = nlist->shift;
116     gid              = nlist->gid;
117     shiftvec         = fr->shift_vec[0];
118     fshift           = fr->fshift[0];
119     facel            = _mm256_set1_pd(fr->epsfac);
120     charge           = mdatoms->chargeA;
121     nvdwtype         = fr->ntype;
122     vdwparam         = fr->nbfp;
123     vdwtype          = mdatoms->typeA;
124
125     sh_ewald         = _mm256_set1_pd(fr->ic->sh_ewald);
126     beta             = _mm256_set1_pd(fr->ic->ewaldcoeff_q);
127     beta2            = _mm256_mul_pd(beta,beta);
128     beta3            = _mm256_mul_pd(beta,beta2);
129
130     ewtab            = fr->ic->tabq_coul_FDV0;
131     ewtabscale       = _mm256_set1_pd(fr->ic->tabq_scale);
132     ewtabhalfspace   = _mm256_set1_pd(0.5/fr->ic->tabq_scale);
133
134     /* Avoid stupid compiler warnings */
135     jnrA = jnrB = jnrC = jnrD = 0;
136     j_coord_offsetA = 0;
137     j_coord_offsetB = 0;
138     j_coord_offsetC = 0;
139     j_coord_offsetD = 0;
140
141     outeriter        = 0;
142     inneriter        = 0;
143
144     for(iidx=0;iidx<4*DIM;iidx++)
145     {
146         scratch[iidx] = 0.0;
147     }
148
149     /* Start outer loop over neighborlists */
150     for(iidx=0; iidx<nri; iidx++)
151     {
152         /* Load shift vector for this list */
153         i_shift_offset   = DIM*shiftidx[iidx];
154
155         /* Load limits for loop over neighbors */
156         j_index_start    = jindex[iidx];
157         j_index_end      = jindex[iidx+1];
158
159         /* Get outer coordinate index */
160         inr              = iinr[iidx];
161         i_coord_offset   = DIM*inr;
162
163         /* Load i particle coords and add shift vector */
164         gmx_mm256_load_shift_and_1rvec_broadcast_pd(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
165
166         fix0             = _mm256_setzero_pd();
167         fiy0             = _mm256_setzero_pd();
168         fiz0             = _mm256_setzero_pd();
169
170         /* Load parameters for i particles */
171         iq0              = _mm256_mul_pd(facel,_mm256_set1_pd(charge[inr+0]));
172         vdwioffsetptr0   = vdwparam+2*nvdwtype*vdwtype[inr+0];
173
174         /* Reset potential sums */
175         velecsum         = _mm256_setzero_pd();
176         vvdwsum          = _mm256_setzero_pd();
177
178         /* Start inner kernel loop */
179         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
180         {
181
182             /* Get j neighbor index, and coordinate index */
183             jnrA             = jjnr[jidx];
184             jnrB             = jjnr[jidx+1];
185             jnrC             = jjnr[jidx+2];
186             jnrD             = jjnr[jidx+3];
187             j_coord_offsetA  = DIM*jnrA;
188             j_coord_offsetB  = DIM*jnrB;
189             j_coord_offsetC  = DIM*jnrC;
190             j_coord_offsetD  = DIM*jnrD;
191
192             /* load j atom coordinates */
193             gmx_mm256_load_1rvec_4ptr_swizzle_pd(x+j_coord_offsetA,x+j_coord_offsetB,
194                                                  x+j_coord_offsetC,x+j_coord_offsetD,
195                                                  &jx0,&jy0,&jz0);
196
197             /* Calculate displacement vector */
198             dx00             = _mm256_sub_pd(ix0,jx0);
199             dy00             = _mm256_sub_pd(iy0,jy0);
200             dz00             = _mm256_sub_pd(iz0,jz0);
201
202             /* Calculate squared distance and things based on it */
203             rsq00            = gmx_mm256_calc_rsq_pd(dx00,dy00,dz00);
204
205             rinv00           = gmx_mm256_invsqrt_pd(rsq00);
206
207             rinvsq00         = _mm256_mul_pd(rinv00,rinv00);
208
209             /* Load parameters for j particles */
210             jq0              = gmx_mm256_load_4real_swizzle_pd(charge+jnrA+0,charge+jnrB+0,
211                                                                  charge+jnrC+0,charge+jnrD+0);
212             vdwjidx0A        = 2*vdwtype[jnrA+0];
213             vdwjidx0B        = 2*vdwtype[jnrB+0];
214             vdwjidx0C        = 2*vdwtype[jnrC+0];
215             vdwjidx0D        = 2*vdwtype[jnrD+0];
216
217             /**************************
218              * CALCULATE INTERACTIONS *
219              **************************/
220
221             r00              = _mm256_mul_pd(rsq00,rinv00);
222
223             /* Compute parameters for interactions between i and j atoms */
224             qq00             = _mm256_mul_pd(iq0,jq0);
225             gmx_mm256_load_4pair_swizzle_pd(vdwioffsetptr0+vdwjidx0A,
226                                             vdwioffsetptr0+vdwjidx0B,
227                                             vdwioffsetptr0+vdwjidx0C,
228                                             vdwioffsetptr0+vdwjidx0D,
229                                             &c6_00,&c12_00);
230
231             /* EWALD ELECTROSTATICS */
232
233             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
234             ewrt             = _mm256_mul_pd(r00,ewtabscale);
235             ewitab           = _mm256_cvttpd_epi32(ewrt);
236             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
237             ewitab           = _mm_slli_epi32(ewitab,2);
238             ewtabF           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,0) );
239             ewtabD           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,1) );
240             ewtabV           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,2) );
241             ewtabFn          = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,3) );
242             GMX_MM256_FULLTRANSPOSE4_PD(ewtabF,ewtabD,ewtabV,ewtabFn);
243             felec            = _mm256_add_pd(ewtabF,_mm256_mul_pd(eweps,ewtabD));
244             velec            = _mm256_sub_pd(ewtabV,_mm256_mul_pd(_mm256_mul_pd(ewtabhalfspace,eweps),_mm256_add_pd(ewtabF,felec)));
245             velec            = _mm256_mul_pd(qq00,_mm256_sub_pd(rinv00,velec));
246             felec            = _mm256_mul_pd(_mm256_mul_pd(qq00,rinv00),_mm256_sub_pd(rinvsq00,felec));
247
248             /* LENNARD-JONES DISPERSION/REPULSION */
249
250             rinvsix          = _mm256_mul_pd(_mm256_mul_pd(rinvsq00,rinvsq00),rinvsq00);
251             vvdw6            = _mm256_mul_pd(c6_00,rinvsix);
252             vvdw12           = _mm256_mul_pd(c12_00,_mm256_mul_pd(rinvsix,rinvsix));
253             vvdw             = _mm256_sub_pd( _mm256_mul_pd(vvdw12,one_twelfth) , _mm256_mul_pd(vvdw6,one_sixth) );
254             fvdw             = _mm256_mul_pd(_mm256_sub_pd(vvdw12,vvdw6),rinvsq00);
255
256             /* Update potential sum for this i atom from the interaction with this j atom. */
257             velecsum         = _mm256_add_pd(velecsum,velec);
258             vvdwsum          = _mm256_add_pd(vvdwsum,vvdw);
259
260             fscal            = _mm256_add_pd(felec,fvdw);
261
262             /* Calculate temporary vectorial force */
263             tx               = _mm256_mul_pd(fscal,dx00);
264             ty               = _mm256_mul_pd(fscal,dy00);
265             tz               = _mm256_mul_pd(fscal,dz00);
266
267             /* Update vectorial force */
268             fix0             = _mm256_add_pd(fix0,tx);
269             fiy0             = _mm256_add_pd(fiy0,ty);
270             fiz0             = _mm256_add_pd(fiz0,tz);
271
272             fjptrA             = f+j_coord_offsetA;
273             fjptrB             = f+j_coord_offsetB;
274             fjptrC             = f+j_coord_offsetC;
275             fjptrD             = f+j_coord_offsetD;
276             gmx_mm256_decrement_1rvec_4ptr_swizzle_pd(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
277
278             /* Inner loop uses 53 flops */
279         }
280
281         if(jidx<j_index_end)
282         {
283
284             /* Get j neighbor index, and coordinate index */
285             jnrlistA         = jjnr[jidx];
286             jnrlistB         = jjnr[jidx+1];
287             jnrlistC         = jjnr[jidx+2];
288             jnrlistD         = jjnr[jidx+3];
289             /* Sign of each element will be negative for non-real atoms.
290              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
291              * so use it as val = _mm_andnot_pd(mask,val) to clear dummy entries.
292              */
293             tmpmask0 = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
294
295             tmpmask1 = _mm_permute_ps(tmpmask0,_GMX_MM_PERMUTE(3,3,2,2));
296             tmpmask0 = _mm_permute_ps(tmpmask0,_GMX_MM_PERMUTE(1,1,0,0));
297             dummy_mask = _mm256_castps_pd(gmx_mm256_set_m128(tmpmask1,tmpmask0));
298
299             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
300             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
301             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
302             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
303             j_coord_offsetA  = DIM*jnrA;
304             j_coord_offsetB  = DIM*jnrB;
305             j_coord_offsetC  = DIM*jnrC;
306             j_coord_offsetD  = DIM*jnrD;
307
308             /* load j atom coordinates */
309             gmx_mm256_load_1rvec_4ptr_swizzle_pd(x+j_coord_offsetA,x+j_coord_offsetB,
310                                                  x+j_coord_offsetC,x+j_coord_offsetD,
311                                                  &jx0,&jy0,&jz0);
312
313             /* Calculate displacement vector */
314             dx00             = _mm256_sub_pd(ix0,jx0);
315             dy00             = _mm256_sub_pd(iy0,jy0);
316             dz00             = _mm256_sub_pd(iz0,jz0);
317
318             /* Calculate squared distance and things based on it */
319             rsq00            = gmx_mm256_calc_rsq_pd(dx00,dy00,dz00);
320
321             rinv00           = gmx_mm256_invsqrt_pd(rsq00);
322
323             rinvsq00         = _mm256_mul_pd(rinv00,rinv00);
324
325             /* Load parameters for j particles */
326             jq0              = gmx_mm256_load_4real_swizzle_pd(charge+jnrA+0,charge+jnrB+0,
327                                                                  charge+jnrC+0,charge+jnrD+0);
328             vdwjidx0A        = 2*vdwtype[jnrA+0];
329             vdwjidx0B        = 2*vdwtype[jnrB+0];
330             vdwjidx0C        = 2*vdwtype[jnrC+0];
331             vdwjidx0D        = 2*vdwtype[jnrD+0];
332
333             /**************************
334              * CALCULATE INTERACTIONS *
335              **************************/
336
337             r00              = _mm256_mul_pd(rsq00,rinv00);
338             r00              = _mm256_andnot_pd(dummy_mask,r00);
339
340             /* Compute parameters for interactions between i and j atoms */
341             qq00             = _mm256_mul_pd(iq0,jq0);
342             gmx_mm256_load_4pair_swizzle_pd(vdwioffsetptr0+vdwjidx0A,
343                                             vdwioffsetptr0+vdwjidx0B,
344                                             vdwioffsetptr0+vdwjidx0C,
345                                             vdwioffsetptr0+vdwjidx0D,
346                                             &c6_00,&c12_00);
347
348             /* EWALD ELECTROSTATICS */
349
350             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
351             ewrt             = _mm256_mul_pd(r00,ewtabscale);
352             ewitab           = _mm256_cvttpd_epi32(ewrt);
353             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
354             ewitab           = _mm_slli_epi32(ewitab,2);
355             ewtabF           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,0) );
356             ewtabD           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,1) );
357             ewtabV           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,2) );
358             ewtabFn          = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,3) );
359             GMX_MM256_FULLTRANSPOSE4_PD(ewtabF,ewtabD,ewtabV,ewtabFn);
360             felec            = _mm256_add_pd(ewtabF,_mm256_mul_pd(eweps,ewtabD));
361             velec            = _mm256_sub_pd(ewtabV,_mm256_mul_pd(_mm256_mul_pd(ewtabhalfspace,eweps),_mm256_add_pd(ewtabF,felec)));
362             velec            = _mm256_mul_pd(qq00,_mm256_sub_pd(rinv00,velec));
363             felec            = _mm256_mul_pd(_mm256_mul_pd(qq00,rinv00),_mm256_sub_pd(rinvsq00,felec));
364
365             /* LENNARD-JONES DISPERSION/REPULSION */
366
367             rinvsix          = _mm256_mul_pd(_mm256_mul_pd(rinvsq00,rinvsq00),rinvsq00);
368             vvdw6            = _mm256_mul_pd(c6_00,rinvsix);
369             vvdw12           = _mm256_mul_pd(c12_00,_mm256_mul_pd(rinvsix,rinvsix));
370             vvdw             = _mm256_sub_pd( _mm256_mul_pd(vvdw12,one_twelfth) , _mm256_mul_pd(vvdw6,one_sixth) );
371             fvdw             = _mm256_mul_pd(_mm256_sub_pd(vvdw12,vvdw6),rinvsq00);
372
373             /* Update potential sum for this i atom from the interaction with this j atom. */
374             velec            = _mm256_andnot_pd(dummy_mask,velec);
375             velecsum         = _mm256_add_pd(velecsum,velec);
376             vvdw             = _mm256_andnot_pd(dummy_mask,vvdw);
377             vvdwsum          = _mm256_add_pd(vvdwsum,vvdw);
378
379             fscal            = _mm256_add_pd(felec,fvdw);
380
381             fscal            = _mm256_andnot_pd(dummy_mask,fscal);
382
383             /* Calculate temporary vectorial force */
384             tx               = _mm256_mul_pd(fscal,dx00);
385             ty               = _mm256_mul_pd(fscal,dy00);
386             tz               = _mm256_mul_pd(fscal,dz00);
387
388             /* Update vectorial force */
389             fix0             = _mm256_add_pd(fix0,tx);
390             fiy0             = _mm256_add_pd(fiy0,ty);
391             fiz0             = _mm256_add_pd(fiz0,tz);
392
393             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
394             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
395             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
396             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
397             gmx_mm256_decrement_1rvec_4ptr_swizzle_pd(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
398
399             /* Inner loop uses 54 flops */
400         }
401
402         /* End of innermost loop */
403
404         gmx_mm256_update_iforce_1atom_swizzle_pd(fix0,fiy0,fiz0,
405                                                  f+i_coord_offset,fshift+i_shift_offset);
406
407         ggid                        = gid[iidx];
408         /* Update potential energies */
409         gmx_mm256_update_1pot_pd(velecsum,kernel_data->energygrp_elec+ggid);
410         gmx_mm256_update_1pot_pd(vvdwsum,kernel_data->energygrp_vdw+ggid);
411
412         /* Increment number of inner iterations */
413         inneriter                  += j_index_end - j_index_start;
414
415         /* Outer loop uses 9 flops */
416     }
417
418     /* Increment number of outer iterations */
419     outeriter        += nri;
420
421     /* Update outer/inner flops */
422
423     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_VF,outeriter*9 + inneriter*54);
424 }
425 /*
426  * Gromacs nonbonded kernel:   nb_kernel_ElecEw_VdwLJ_GeomP1P1_F_avx_256_double
427  * Electrostatics interaction: Ewald
428  * VdW interaction:            LennardJones
429  * Geometry:                   Particle-Particle
430  * Calculate force/pot:        Force
431  */
432 void
433 nb_kernel_ElecEw_VdwLJ_GeomP1P1_F_avx_256_double
434                     (t_nblist                    * gmx_restrict       nlist,
435                      rvec                        * gmx_restrict          xx,
436                      rvec                        * gmx_restrict          ff,
437                      t_forcerec                  * gmx_restrict          fr,
438                      t_mdatoms                   * gmx_restrict     mdatoms,
439                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
440                      t_nrnb                      * gmx_restrict        nrnb)
441 {
442     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
443      * just 0 for non-waters.
444      * Suffixes A,B,C,D refer to j loop unrolling done with AVX, e.g. for the four different
445      * jnr indices corresponding to data put in the four positions in the SIMD register.
446      */
447     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
448     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
449     int              jnrA,jnrB,jnrC,jnrD;
450     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
451     int              jnrlistE,jnrlistF,jnrlistG,jnrlistH;
452     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
453     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
454     real             rcutoff_scalar;
455     real             *shiftvec,*fshift,*x,*f;
456     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
457     real             scratch[4*DIM];
458     __m256d          tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
459     real *           vdwioffsetptr0;
460     __m256d          ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
461     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
462     __m256d          jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
463     __m256d          dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
464     __m256d          velec,felec,velecsum,facel,crf,krf,krf2;
465     real             *charge;
466     int              nvdwtype;
467     __m256d          rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
468     int              *vdwtype;
469     real             *vdwparam;
470     __m256d          one_sixth   = _mm256_set1_pd(1.0/6.0);
471     __m256d          one_twelfth = _mm256_set1_pd(1.0/12.0);
472     __m128i          ewitab;
473     __m256d          ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
474     __m256d          beta,beta2,beta3,zeta2,pmecorrF,pmecorrV,rinv3;
475     real             *ewtab;
476     __m256d          dummy_mask,cutoff_mask;
477     __m128           tmpmask0,tmpmask1;
478     __m256d          signbit = _mm256_castsi256_pd( _mm256_set1_epi32(0x80000000) );
479     __m256d          one     = _mm256_set1_pd(1.0);
480     __m256d          two     = _mm256_set1_pd(2.0);
481     x                = xx[0];
482     f                = ff[0];
483
484     nri              = nlist->nri;
485     iinr             = nlist->iinr;
486     jindex           = nlist->jindex;
487     jjnr             = nlist->jjnr;
488     shiftidx         = nlist->shift;
489     gid              = nlist->gid;
490     shiftvec         = fr->shift_vec[0];
491     fshift           = fr->fshift[0];
492     facel            = _mm256_set1_pd(fr->epsfac);
493     charge           = mdatoms->chargeA;
494     nvdwtype         = fr->ntype;
495     vdwparam         = fr->nbfp;
496     vdwtype          = mdatoms->typeA;
497
498     sh_ewald         = _mm256_set1_pd(fr->ic->sh_ewald);
499     beta             = _mm256_set1_pd(fr->ic->ewaldcoeff_q);
500     beta2            = _mm256_mul_pd(beta,beta);
501     beta3            = _mm256_mul_pd(beta,beta2);
502
503     ewtab            = fr->ic->tabq_coul_F;
504     ewtabscale       = _mm256_set1_pd(fr->ic->tabq_scale);
505     ewtabhalfspace   = _mm256_set1_pd(0.5/fr->ic->tabq_scale);
506
507     /* Avoid stupid compiler warnings */
508     jnrA = jnrB = jnrC = jnrD = 0;
509     j_coord_offsetA = 0;
510     j_coord_offsetB = 0;
511     j_coord_offsetC = 0;
512     j_coord_offsetD = 0;
513
514     outeriter        = 0;
515     inneriter        = 0;
516
517     for(iidx=0;iidx<4*DIM;iidx++)
518     {
519         scratch[iidx] = 0.0;
520     }
521
522     /* Start outer loop over neighborlists */
523     for(iidx=0; iidx<nri; iidx++)
524     {
525         /* Load shift vector for this list */
526         i_shift_offset   = DIM*shiftidx[iidx];
527
528         /* Load limits for loop over neighbors */
529         j_index_start    = jindex[iidx];
530         j_index_end      = jindex[iidx+1];
531
532         /* Get outer coordinate index */
533         inr              = iinr[iidx];
534         i_coord_offset   = DIM*inr;
535
536         /* Load i particle coords and add shift vector */
537         gmx_mm256_load_shift_and_1rvec_broadcast_pd(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
538
539         fix0             = _mm256_setzero_pd();
540         fiy0             = _mm256_setzero_pd();
541         fiz0             = _mm256_setzero_pd();
542
543         /* Load parameters for i particles */
544         iq0              = _mm256_mul_pd(facel,_mm256_set1_pd(charge[inr+0]));
545         vdwioffsetptr0   = vdwparam+2*nvdwtype*vdwtype[inr+0];
546
547         /* Start inner kernel loop */
548         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
549         {
550
551             /* Get j neighbor index, and coordinate index */
552             jnrA             = jjnr[jidx];
553             jnrB             = jjnr[jidx+1];
554             jnrC             = jjnr[jidx+2];
555             jnrD             = jjnr[jidx+3];
556             j_coord_offsetA  = DIM*jnrA;
557             j_coord_offsetB  = DIM*jnrB;
558             j_coord_offsetC  = DIM*jnrC;
559             j_coord_offsetD  = DIM*jnrD;
560
561             /* load j atom coordinates */
562             gmx_mm256_load_1rvec_4ptr_swizzle_pd(x+j_coord_offsetA,x+j_coord_offsetB,
563                                                  x+j_coord_offsetC,x+j_coord_offsetD,
564                                                  &jx0,&jy0,&jz0);
565
566             /* Calculate displacement vector */
567             dx00             = _mm256_sub_pd(ix0,jx0);
568             dy00             = _mm256_sub_pd(iy0,jy0);
569             dz00             = _mm256_sub_pd(iz0,jz0);
570
571             /* Calculate squared distance and things based on it */
572             rsq00            = gmx_mm256_calc_rsq_pd(dx00,dy00,dz00);
573
574             rinv00           = gmx_mm256_invsqrt_pd(rsq00);
575
576             rinvsq00         = _mm256_mul_pd(rinv00,rinv00);
577
578             /* Load parameters for j particles */
579             jq0              = gmx_mm256_load_4real_swizzle_pd(charge+jnrA+0,charge+jnrB+0,
580                                                                  charge+jnrC+0,charge+jnrD+0);
581             vdwjidx0A        = 2*vdwtype[jnrA+0];
582             vdwjidx0B        = 2*vdwtype[jnrB+0];
583             vdwjidx0C        = 2*vdwtype[jnrC+0];
584             vdwjidx0D        = 2*vdwtype[jnrD+0];
585
586             /**************************
587              * CALCULATE INTERACTIONS *
588              **************************/
589
590             r00              = _mm256_mul_pd(rsq00,rinv00);
591
592             /* Compute parameters for interactions between i and j atoms */
593             qq00             = _mm256_mul_pd(iq0,jq0);
594             gmx_mm256_load_4pair_swizzle_pd(vdwioffsetptr0+vdwjidx0A,
595                                             vdwioffsetptr0+vdwjidx0B,
596                                             vdwioffsetptr0+vdwjidx0C,
597                                             vdwioffsetptr0+vdwjidx0D,
598                                             &c6_00,&c12_00);
599
600             /* EWALD ELECTROSTATICS */
601
602             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
603             ewrt             = _mm256_mul_pd(r00,ewtabscale);
604             ewitab           = _mm256_cvttpd_epi32(ewrt);
605             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
606             gmx_mm256_load_4pair_swizzle_pd(ewtab + _mm_extract_epi32(ewitab,0),ewtab + _mm_extract_epi32(ewitab,1),
607                                             ewtab + _mm_extract_epi32(ewitab,2),ewtab + _mm_extract_epi32(ewitab,3),
608                                             &ewtabF,&ewtabFn);
609             felec            = _mm256_add_pd(_mm256_mul_pd( _mm256_sub_pd(one,eweps),ewtabF),_mm256_mul_pd(eweps,ewtabFn));
610             felec            = _mm256_mul_pd(_mm256_mul_pd(qq00,rinv00),_mm256_sub_pd(rinvsq00,felec));
611
612             /* LENNARD-JONES DISPERSION/REPULSION */
613
614             rinvsix          = _mm256_mul_pd(_mm256_mul_pd(rinvsq00,rinvsq00),rinvsq00);
615             fvdw             = _mm256_mul_pd(_mm256_sub_pd(_mm256_mul_pd(c12_00,rinvsix),c6_00),_mm256_mul_pd(rinvsix,rinvsq00));
616
617             fscal            = _mm256_add_pd(felec,fvdw);
618
619             /* Calculate temporary vectorial force */
620             tx               = _mm256_mul_pd(fscal,dx00);
621             ty               = _mm256_mul_pd(fscal,dy00);
622             tz               = _mm256_mul_pd(fscal,dz00);
623
624             /* Update vectorial force */
625             fix0             = _mm256_add_pd(fix0,tx);
626             fiy0             = _mm256_add_pd(fiy0,ty);
627             fiz0             = _mm256_add_pd(fiz0,tz);
628
629             fjptrA             = f+j_coord_offsetA;
630             fjptrB             = f+j_coord_offsetB;
631             fjptrC             = f+j_coord_offsetC;
632             fjptrD             = f+j_coord_offsetD;
633             gmx_mm256_decrement_1rvec_4ptr_swizzle_pd(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
634
635             /* Inner loop uses 43 flops */
636         }
637
638         if(jidx<j_index_end)
639         {
640
641             /* Get j neighbor index, and coordinate index */
642             jnrlistA         = jjnr[jidx];
643             jnrlistB         = jjnr[jidx+1];
644             jnrlistC         = jjnr[jidx+2];
645             jnrlistD         = jjnr[jidx+3];
646             /* Sign of each element will be negative for non-real atoms.
647              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
648              * so use it as val = _mm_andnot_pd(mask,val) to clear dummy entries.
649              */
650             tmpmask0 = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
651
652             tmpmask1 = _mm_permute_ps(tmpmask0,_GMX_MM_PERMUTE(3,3,2,2));
653             tmpmask0 = _mm_permute_ps(tmpmask0,_GMX_MM_PERMUTE(1,1,0,0));
654             dummy_mask = _mm256_castps_pd(gmx_mm256_set_m128(tmpmask1,tmpmask0));
655
656             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
657             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
658             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
659             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
660             j_coord_offsetA  = DIM*jnrA;
661             j_coord_offsetB  = DIM*jnrB;
662             j_coord_offsetC  = DIM*jnrC;
663             j_coord_offsetD  = DIM*jnrD;
664
665             /* load j atom coordinates */
666             gmx_mm256_load_1rvec_4ptr_swizzle_pd(x+j_coord_offsetA,x+j_coord_offsetB,
667                                                  x+j_coord_offsetC,x+j_coord_offsetD,
668                                                  &jx0,&jy0,&jz0);
669
670             /* Calculate displacement vector */
671             dx00             = _mm256_sub_pd(ix0,jx0);
672             dy00             = _mm256_sub_pd(iy0,jy0);
673             dz00             = _mm256_sub_pd(iz0,jz0);
674
675             /* Calculate squared distance and things based on it */
676             rsq00            = gmx_mm256_calc_rsq_pd(dx00,dy00,dz00);
677
678             rinv00           = gmx_mm256_invsqrt_pd(rsq00);
679
680             rinvsq00         = _mm256_mul_pd(rinv00,rinv00);
681
682             /* Load parameters for j particles */
683             jq0              = gmx_mm256_load_4real_swizzle_pd(charge+jnrA+0,charge+jnrB+0,
684                                                                  charge+jnrC+0,charge+jnrD+0);
685             vdwjidx0A        = 2*vdwtype[jnrA+0];
686             vdwjidx0B        = 2*vdwtype[jnrB+0];
687             vdwjidx0C        = 2*vdwtype[jnrC+0];
688             vdwjidx0D        = 2*vdwtype[jnrD+0];
689
690             /**************************
691              * CALCULATE INTERACTIONS *
692              **************************/
693
694             r00              = _mm256_mul_pd(rsq00,rinv00);
695             r00              = _mm256_andnot_pd(dummy_mask,r00);
696
697             /* Compute parameters for interactions between i and j atoms */
698             qq00             = _mm256_mul_pd(iq0,jq0);
699             gmx_mm256_load_4pair_swizzle_pd(vdwioffsetptr0+vdwjidx0A,
700                                             vdwioffsetptr0+vdwjidx0B,
701                                             vdwioffsetptr0+vdwjidx0C,
702                                             vdwioffsetptr0+vdwjidx0D,
703                                             &c6_00,&c12_00);
704
705             /* EWALD ELECTROSTATICS */
706
707             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
708             ewrt             = _mm256_mul_pd(r00,ewtabscale);
709             ewitab           = _mm256_cvttpd_epi32(ewrt);
710             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
711             gmx_mm256_load_4pair_swizzle_pd(ewtab + _mm_extract_epi32(ewitab,0),ewtab + _mm_extract_epi32(ewitab,1),
712                                             ewtab + _mm_extract_epi32(ewitab,2),ewtab + _mm_extract_epi32(ewitab,3),
713                                             &ewtabF,&ewtabFn);
714             felec            = _mm256_add_pd(_mm256_mul_pd( _mm256_sub_pd(one,eweps),ewtabF),_mm256_mul_pd(eweps,ewtabFn));
715             felec            = _mm256_mul_pd(_mm256_mul_pd(qq00,rinv00),_mm256_sub_pd(rinvsq00,felec));
716
717             /* LENNARD-JONES DISPERSION/REPULSION */
718
719             rinvsix          = _mm256_mul_pd(_mm256_mul_pd(rinvsq00,rinvsq00),rinvsq00);
720             fvdw             = _mm256_mul_pd(_mm256_sub_pd(_mm256_mul_pd(c12_00,rinvsix),c6_00),_mm256_mul_pd(rinvsix,rinvsq00));
721
722             fscal            = _mm256_add_pd(felec,fvdw);
723
724             fscal            = _mm256_andnot_pd(dummy_mask,fscal);
725
726             /* Calculate temporary vectorial force */
727             tx               = _mm256_mul_pd(fscal,dx00);
728             ty               = _mm256_mul_pd(fscal,dy00);
729             tz               = _mm256_mul_pd(fscal,dz00);
730
731             /* Update vectorial force */
732             fix0             = _mm256_add_pd(fix0,tx);
733             fiy0             = _mm256_add_pd(fiy0,ty);
734             fiz0             = _mm256_add_pd(fiz0,tz);
735
736             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
737             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
738             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
739             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
740             gmx_mm256_decrement_1rvec_4ptr_swizzle_pd(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
741
742             /* Inner loop uses 44 flops */
743         }
744
745         /* End of innermost loop */
746
747         gmx_mm256_update_iforce_1atom_swizzle_pd(fix0,fiy0,fiz0,
748                                                  f+i_coord_offset,fshift+i_shift_offset);
749
750         /* Increment number of inner iterations */
751         inneriter                  += j_index_end - j_index_start;
752
753         /* Outer loop uses 7 flops */
754     }
755
756     /* Increment number of outer iterations */
757     outeriter        += nri;
758
759     /* Update outer/inner flops */
760
761     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_F,outeriter*7 + inneriter*44);
762 }