Remove support for implicit solvation
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_avx_256_single / nb_kernel_template_avx_256_single.pre
1 /* #if 0 */
2 /*
3  * This file is part of the GROMACS molecular simulation package.
4  *
5  * Copyright (c) 2012,2013,2014,2015,2017,2018, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 #error This file must be processed with the Gromacs pre-preprocessor
37 /* #endif */
38 /* #if INCLUDE_HEADER */
39 #include "gmxpre.h"
40
41 #include "config.h"
42
43 #include <math.h>
44
45 #include "../nb_kernel.h"
46 #include "gromacs/gmxlib/nrnb.h"
47
48 #include "kernelutil_x86_avx_256_single.h"
49 /* #endif */
50
51 /* ## List of variables set by the generating script:                                    */
52 /* ##                                                                                    */
53 /* ## Setttings that apply to the entire kernel:                                         */
54 /* ## KERNEL_ELEC:           String, choice for electrostatic interactions               */
55 /* ## KERNEL_VDW:            String, choice for van der Waals interactions               */
56 /* ## KERNEL_NAME:           String, name of this kernel                                 */
57 /* ## KERNEL_VF:             String telling if we calculate potential, force, or both    */
58 /* ## GEOMETRY_I/GEOMETRY_J: String, name of each geometry, e.g. 'Water3' or '1Particle' */
59 /* ##                                                                                    */
60 /* ## Setttings that apply to particles in the outer (I) or inner (J) loops:             */
61 /* ## PARTICLES_I[]/         Arrays with lists of i/j particles to use in kernel. It is  */
62 /* ## PARTICLES_J[]:         just [0] for particle geometry, but can be longer for water */
63 /* ## PARTICLES_ELEC_I[]/    Arrays with lists of i/j particle that have electrostatics  */
64 /* ## PARTICLES_ELEC_J[]:    interactions that should be calculated in this kernel.      */
65 /* ## PARTICLES_VDW_I[]/     Arrays with the list of i/j particle that have VdW          */
66 /* ## PARTICLES_VDW_J[]:     interactions that should be calculated in this kernel.      */
67 /* ##                                                                                    */
68 /* ## Setttings for pairs of interactions (e.g. 2nd i particle against 1st j particle)   */
69 /* ## PAIRS_IJ[]:            Array with (i,j) tuples of pairs for which interactions     */
70 /* ##                        should be calculated in this kernel. Zero-charge particles  */
71 /* ##                        do not have interactions with particles without vdw, and    */
72 /* ##                        Vdw-only interactions are not evaluated in a no-vdw-kernel. */
73 /* ## INTERACTION_FLAGS[][]: 2D matrix, dimension e.g. 3*3 for water-water interactions. */
74 /* ##                        For each i-j pair, the element [I][J] is a list of strings  */
75 /* ##                        defining properties/flags of this interaction. Examples     */
76 /* ##                        include 'electrostatics'/'vdw' if that type of interaction  */
77 /* ##                        should be evaluated, 'rsq'/'rinv'/'rinvsq' if those values  */
78 /* ##                        are needed, and 'exactcutoff' or 'shift','switch' to        */
79 /* ##                        decide if the force/potential should be modified. This way  */
80 /* ##                        we only calculate values absolutely needed for each case.   */
81
82 /* ## Calculate the size and offset for (merged/interleaved) table data */
83
84
85
86
87 /*
88  * Gromacs nonbonded kernel:   {KERNEL_NAME}
89  * Electrostatics interaction: {KERNEL_ELEC}
90  * VdW interaction:            {KERNEL_VDW}
91  * Geometry:                   {GEOMETRY_I}-{GEOMETRY_J}
92  * Calculate force/pot:        {KERNEL_VF}
93  */
94 void
95 {KERNEL_NAME}
96                     (t_nblist                    * gmx_restrict       nlist,
97                      rvec                        * gmx_restrict          xx,
98                      rvec                        * gmx_restrict          ff,
99                      struct t_forcerec           * gmx_restrict          fr,
100                      t_mdatoms                   * gmx_restrict     mdatoms,
101                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
102                      t_nrnb                      * gmx_restrict        nrnb)
103 {
104     /* ## Not all variables are used for all kernels, but any optimizing compiler fixes that, */
105     /* ## so there is no point in going to extremes to exclude variables that are not needed. */
106     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
107      * just 0 for non-waters.
108      * Suffixes A,B,C,D,E,F,G,H refer to j loop unrolling done with AVX, e.g. for the eight different
109      * jnr indices corresponding to data put in the four positions in the SIMD register.
110      */
111     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
112     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
113     int              jnrA,jnrB,jnrC,jnrD;
114     int              jnrE,jnrF,jnrG,jnrH;
115     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
116     int              jnrlistE,jnrlistF,jnrlistG,jnrlistH;
117     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
118     int              j_coord_offsetE,j_coord_offsetF,j_coord_offsetG,j_coord_offsetH;
119     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
120     real             rcutoff_scalar;
121     real             *shiftvec,*fshift,*x,*f;
122     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD,*fjptrE,*fjptrF,*fjptrG,*fjptrH;
123     real             scratch[4*DIM];
124     __m256           tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
125     /* #for I in PARTICLES_I */
126     real *           vdwioffsetptr{I};
127     /* #if 'LJEwald' in KERNEL_VDW */
128     real *           vdwgridioffsetptr{I};
129     /* #endif                      */
130     __m256           ix{I},iy{I},iz{I},fix{I},fiy{I},fiz{I},iq{I},isai{I};
131     /* #endfor */
132     /* #for J in PARTICLES_J */
133     int              vdwjidx{J}A,vdwjidx{J}B,vdwjidx{J}C,vdwjidx{J}D,vdwjidx{J}E,vdwjidx{J}F,vdwjidx{J}G,vdwjidx{J}H;
134     __m256           jx{J},jy{J},jz{J},fjx{J},fjy{J},fjz{J},jq{J},isaj{J};
135     /* #endfor */
136     /* #for I,J in PAIRS_IJ */
137     __m256           dx{I}{J},dy{I}{J},dz{I}{J},rsq{I}{J},rinv{I}{J},rinvsq{I}{J},r{I}{J},qq{I}{J},c6_{I}{J},c12_{I}{J};
138     /* #endfor */
139     /* #if KERNEL_ELEC != 'None' */
140     __m256           velec,felec,velecsum,facel,crf,krf,krf2;
141     real             *charge;
142     /* #endif */
143     /* #if KERNEL_VDW != 'None' */
144     int              nvdwtype;
145     __m256           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
146     int              *vdwtype;
147     real             *vdwparam;
148     __m256           one_sixth   = _mm256_set1_ps(1.0/6.0);
149     __m256           one_twelfth = _mm256_set1_ps(1.0/12.0);
150     /* #endif */
151     /* #if 'Table' in KERNEL_ELEC or 'Table' in KERNEL_VDW */
152     __m256i          vfitab;
153     __m128i          vfitab_lo,vfitab_hi;
154     __m128i          ifour       = _mm_set1_epi32(4);
155     __m256           rt,vfeps,vftabscale,Y,F,G,H,Heps,Fp,VV,FF;
156     real             *vftab;
157     /* #endif */
158     /* #if 'LJEwald' in KERNEL_VDW */
159     /* #for I,J in PAIRS_IJ */
160     __m256           c6grid_{I}{J};
161     /* #endfor */
162     real             *vdwgridparam;
163     __m256           ewclj,ewclj2,ewclj6,ewcljrsq,poly,exponent,f6A,f6B,sh_lj_ewald;
164     __m256           one_half  = _mm256_set1_ps(0.5);
165     __m256           minus_one = _mm256_set1_ps(-1.0);
166     /* #endif */
167     /* #if 'Ewald' in KERNEL_ELEC */
168     __m256i          ewitab;
169     __m128i          ewitab_lo,ewitab_hi;
170     __m256           ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
171     __m256           beta,beta2,beta3,zeta2,pmecorrF,pmecorrV,rinv3;
172     real             *ewtab;
173     /* #endif */
174     /* #if 'PotentialSwitch' in [KERNEL_MOD_ELEC,KERNEL_MOD_VDW] */
175     __m256           rswitch,swV3,swV4,swV5,swF2,swF3,swF4,d,d2,sw,dsw;
176     real             rswitch_scalar,d_scalar;
177     /* #endif */
178     __m256           dummy_mask,cutoff_mask;
179     __m256           signbit = _mm256_castsi256_ps( _mm256_set1_epi32(0x80000000) );
180     __m256           one     = _mm256_set1_ps(1.0);
181     __m256           two     = _mm256_set1_ps(2.0);
182     x                = xx[0];
183     f                = ff[0];
184
185     nri              = nlist->nri;
186     iinr             = nlist->iinr;
187     jindex           = nlist->jindex;
188     jjnr             = nlist->jjnr;
189     shiftidx         = nlist->shift;
190     gid              = nlist->gid;
191     shiftvec         = fr->shift_vec[0];
192     fshift           = fr->fshift[0];
193     /* #if KERNEL_ELEC != 'None' */
194     facel            = _mm256_set1_ps(fr->ic->epsfac);
195     charge           = mdatoms->chargeA;
196     /*     #if 'ReactionField' in KERNEL_ELEC */
197     krf              = _mm256_set1_ps(fr->ic->k_rf);
198     krf2             = _mm256_set1_ps(fr->ic->k_rf*2.0);
199     crf              = _mm256_set1_ps(fr->ic->c_rf);
200     /*     #endif */
201     /* #endif */
202     /* #if KERNEL_VDW != 'None' */
203     nvdwtype         = fr->ntype;
204     vdwparam         = fr->nbfp;
205     vdwtype          = mdatoms->typeA;
206     /* #endif */
207     /* #if 'LJEwald' in KERNEL_VDW */
208     vdwgridparam     = fr->ljpme_c6grid;
209     sh_lj_ewald      = _mm256_set1_ps(fr->ic->sh_lj_ewald);
210     ewclj            = _mm256_set1_ps(fr->ic->ewaldcoeff_lj);
211     ewclj2           = _mm256_mul_ps(minus_one,_mm256_mul_ps(ewclj,ewclj));
212     /* #endif */
213
214     /* #if 'Table' in KERNEL_ELEC and 'Table' in KERNEL_VDW */
215     vftab            = kernel_data->table_elec_vdw->data;
216     vftabscale       = _mm256_set1_ps(kernel_data->table_elec_vdw->scale);
217     /* #elif 'Table' in KERNEL_ELEC */
218     vftab            = kernel_data->table_elec->data;
219     vftabscale       = _mm256_set1_ps(kernel_data->table_elec->scale);
220     /* #elif 'Table' in KERNEL_VDW */
221     vftab            = kernel_data->table_vdw->data;
222     vftabscale       = _mm256_set1_ps(kernel_data->table_vdw->scale);
223     /* #endif */
224
225     /* #if 'Ewald' in KERNEL_ELEC */
226     sh_ewald         = _mm256_set1_ps(fr->ic->sh_ewald);
227     beta             = _mm256_set1_ps(fr->ic->ewaldcoeff_q);
228     beta2            = _mm256_mul_ps(beta,beta);
229     beta3            = _mm256_mul_ps(beta,beta2);
230
231     /*     #if KERNEL_VF=='Force' and KERNEL_MOD_ELEC!='PotentialSwitch' */
232     ewtab            = fr->ic->tabq_coul_F;
233     ewtabscale       = _mm256_set1_ps(fr->ic->tabq_scale);
234     ewtabhalfspace   = _mm256_set1_ps(0.5/fr->ic->tabq_scale);
235     /*     #else */
236     ewtab            = fr->ic->tabq_coul_FDV0;
237     ewtabscale       = _mm256_set1_ps(fr->ic->tabq_scale);
238     ewtabhalfspace   = _mm256_set1_ps(0.5/fr->ic->tabq_scale);
239      /*     #endif */
240     /* #endif */
241
242     /* #if 'Water' in GEOMETRY_I */
243     /* Setup water-specific parameters */
244     inr              = nlist->iinr[0];
245     /*     #for I in PARTICLES_ELEC_I */
246     iq{I}              = _mm256_mul_ps(facel,_mm256_set1_ps(charge[inr+{I}]));
247     /*     #endfor */
248     /*     #for I in PARTICLES_VDW_I */
249     vdwioffsetptr{I}   = vdwparam+2*nvdwtype*vdwtype[inr+{I}];
250     /*         #if 'LJEwald' in KERNEL_VDW */
251     vdwgridioffsetptr{I} = vdwgridparam+2*nvdwtype*vdwtype[inr+{I}];
252     /*         #endif */
253     /*     #endfor */
254     /* #endif */
255
256     /* #if 'Water' in GEOMETRY_J */
257     /*     #for J in PARTICLES_ELEC_J */
258     jq{J}              = _mm256_set1_ps(charge[inr+{J}]);
259     /*     #endfor */
260     /*     #for J in PARTICLES_VDW_J */
261     vdwjidx{J}A        = 2*vdwtype[inr+{J}];
262     /*     #endfor */
263     /*     #for I,J in PAIRS_IJ */
264     /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
265     qq{I}{J}             = _mm256_mul_ps(iq{I},jq{J});
266     /*         #endif */
267     /*         #if 'vdw' in INTERACTION_FLAGS[I][J] */
268         /*         #if 'LJEwald' in KERNEL_VDW */
269     c6_{I}{J}            = _mm256_set1_ps(vdwioffsetptr{I}[vdwjidx{J}A]);
270     c12_{I}{J}           = _mm256_set1_ps(vdwioffsetptr{I}[vdwjidx{J}A+1]);
271     c6grid_{I}{J}        = _mm256_set1_ps(vdwgridioffsetptr{I}[vdwjidx{J}A]);
272         /*         #else */
273     c6_{I}{J}            = _mm256_set1_ps(vdwioffsetptr{I}[vdwjidx{J}A]);
274     c12_{I}{J}           = _mm256_set1_ps(vdwioffsetptr{I}[vdwjidx{J}A+1]);
275         /*         #endif */
276     /*         #endif */
277     /*     #endfor */
278     /* #endif */
279
280     /* #if KERNEL_MOD_ELEC!='None' or KERNEL_MOD_VDW!='None' */
281     /*     #if KERNEL_ELEC!='None' */
282     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
283     rcutoff_scalar   = fr->ic->rcoulomb;
284     /*     #else */
285     rcutoff_scalar   = fr->ic->rvdw;
286     /*     #endif */
287     rcutoff          = _mm256_set1_ps(rcutoff_scalar);
288     rcutoff2         = _mm256_mul_ps(rcutoff,rcutoff);
289     /* #endif */
290
291     /* #if KERNEL_MOD_VDW=='PotentialShift' */
292     sh_vdw_invrcut6  = _mm256_set1_ps(fr->ic->sh_invrc6);
293     rvdw             = _mm256_set1_ps(fr->ic->rvdw);
294     /* #endif */
295
296     /* #if 'PotentialSwitch' in [KERNEL_MOD_ELEC,KERNEL_MOD_VDW] */
297     /*     #if KERNEL_MOD_ELEC=='PotentialSwitch'  */
298     rswitch_scalar   = fr->ic->rcoulomb_switch;
299     rswitch          = _mm256_set1_ps(rswitch_scalar);
300     /*     #else */
301     rswitch_scalar   = fr->ic->rvdw_switch;
302     rswitch          = _mm256_set1_ps(rswitch_scalar);
303     /*     #endif */
304     /* Setup switch parameters */
305     d_scalar         = rcutoff_scalar-rswitch_scalar;
306     d                = _mm256_set1_ps(d_scalar);
307     swV3             = _mm256_set1_ps(-10.0/(d_scalar*d_scalar*d_scalar));
308     swV4             = _mm256_set1_ps( 15.0/(d_scalar*d_scalar*d_scalar*d_scalar));
309     swV5             = _mm256_set1_ps( -6.0/(d_scalar*d_scalar*d_scalar*d_scalar*d_scalar));
310     /*     #if 'Force' in KERNEL_VF */
311     swF2             = _mm256_set1_ps(-30.0/(d_scalar*d_scalar*d_scalar));
312     swF3             = _mm256_set1_ps( 60.0/(d_scalar*d_scalar*d_scalar*d_scalar));
313     swF4             = _mm256_set1_ps(-30.0/(d_scalar*d_scalar*d_scalar*d_scalar*d_scalar));
314     /*     #endif */
315     /* #endif */
316
317     /* Avoid stupid compiler warnings */
318     jnrA = jnrB = jnrC = jnrD = jnrE = jnrF = jnrG = jnrH = 0;
319     j_coord_offsetA = 0;
320     j_coord_offsetB = 0;
321     j_coord_offsetC = 0;
322     j_coord_offsetD = 0;
323     j_coord_offsetE = 0;
324     j_coord_offsetF = 0;
325     j_coord_offsetG = 0;
326     j_coord_offsetH = 0;
327
328     /* ## Keep track of the floating point operations we issue for reporting! */
329     /* #define OUTERFLOPS 0 */
330     outeriter        = 0;
331     inneriter        = 0;
332
333     for(iidx=0;iidx<4*DIM;iidx++)
334     {
335         scratch[iidx] = 0.0;
336     }
337
338     /* Start outer loop over neighborlists */
339     for(iidx=0; iidx<nri; iidx++)
340     {
341         /* Load shift vector for this list */
342         i_shift_offset   = DIM*shiftidx[iidx];
343
344         /* Load limits for loop over neighbors */
345         j_index_start    = jindex[iidx];
346         j_index_end      = jindex[iidx+1];
347
348         /* Get outer coordinate index */
349         inr              = iinr[iidx];
350         i_coord_offset   = DIM*inr;
351
352         /* Load i particle coords and add shift vector */
353         /* #if GEOMETRY_I == 'Particle' */
354         gmx_mm256_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
355         /* #elif GEOMETRY_I == 'Water3' */
356         gmx_mm256_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,
357                                                     &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2);
358         /* #elif GEOMETRY_I == 'Water4' */
359         /*     #if 0 in PARTICLES_I                 */
360         gmx_mm256_load_shift_and_4rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,
361                                                     &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2,&ix3,&iy3,&iz3);
362         /*     #else                                */
363         gmx_mm256_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset+DIM,
364                                                     &ix1,&iy1,&iz1,&ix2,&iy2,&iz2,&ix3,&iy3,&iz3);
365         /*     #endif                               */
366         /* #endif                                   */
367
368         /* #if 'Force' in KERNEL_VF */
369         /*     #for I in PARTICLES_I */
370         fix{I}             = _mm256_setzero_ps();
371         fiy{I}             = _mm256_setzero_ps();
372         fiz{I}             = _mm256_setzero_ps();
373         /*     #endfor */
374         /* #endif */
375
376         /* ## For water we already preloaded parameters at the start of the kernel */
377         /* #if not 'Water' in GEOMETRY_I */
378         /* Load parameters for i particles */
379         /*     #for I in PARTICLES_ELEC_I */
380         iq{I}              = _mm256_mul_ps(facel,_mm256_set1_ps(charge[inr+{I}]));
381         /*         #define OUTERFLOPS OUTERFLOPS+1 */
382         /*     #endfor */
383         /*     #for I in PARTICLES_VDW_I */
384         vdwioffsetptr{I}   = vdwparam+2*nvdwtype*vdwtype[inr+{I}];
385         /*         #if 'LJEwald' in KERNEL_VDW */
386         vdwgridioffsetptr{I} = vdwgridparam+2*nvdwtype*vdwtype[inr+{I}];
387         /*         #endif */
388         /*     #endfor */
389         /* #endif */
390
391         /* #if 'Potential' in KERNEL_VF */
392         /* Reset potential sums */
393         /*     #if KERNEL_ELEC != 'None' */
394         velecsum         = _mm256_setzero_ps();
395         /*     #endif */
396         /*     #if KERNEL_VDW != 'None' */
397         vvdwsum          = _mm256_setzero_ps();
398         /*     #endif */
399         /* #endif */
400
401         /* #for ROUND in ['Loop','Epilogue'] */
402
403         /* #if ROUND =='Loop' */
404         /* Start inner kernel loop */
405         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+7]>=0; jidx+=8)
406         {
407         /* ## First round is normal loop (next statement resets indentation) */
408         /*     #if 0 */
409         }
410         /*     #endif */
411         /* #else */
412         if(jidx<j_index_end)
413         {
414         /* ## Second round is epilogue */
415         /* #endif */
416         /* #define INNERFLOPS 0 */
417
418             /* Get j neighbor index, and coordinate index */
419             /* #if ROUND =='Loop' */
420             jnrA             = jjnr[jidx];
421             jnrB             = jjnr[jidx+1];
422             jnrC             = jjnr[jidx+2];
423             jnrD             = jjnr[jidx+3];
424             jnrE             = jjnr[jidx+4];
425             jnrF             = jjnr[jidx+5];
426             jnrG             = jjnr[jidx+6];
427             jnrH             = jjnr[jidx+7];
428             /* #else */
429             jnrlistA         = jjnr[jidx];
430             jnrlistB         = jjnr[jidx+1];
431             jnrlistC         = jjnr[jidx+2];
432             jnrlistD         = jjnr[jidx+3];
433             jnrlistE         = jjnr[jidx+4];
434             jnrlistF         = jjnr[jidx+5];
435             jnrlistG         = jjnr[jidx+6];
436             jnrlistH         = jjnr[jidx+7];
437             /* Sign of each element will be negative for non-real atoms.
438              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
439              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
440              */
441             dummy_mask = gmx_mm256_set_m128(gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx+4)),_mm_setzero_si128())),
442                                             gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128())));
443                                             
444             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
445             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
446             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
447             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
448             jnrE       = (jnrlistE>=0) ? jnrlistE : 0;
449             jnrF       = (jnrlistF>=0) ? jnrlistF : 0;
450             jnrG       = (jnrlistG>=0) ? jnrlistG : 0;
451             jnrH       = (jnrlistH>=0) ? jnrlistH : 0;
452             /* #endif */
453             j_coord_offsetA  = DIM*jnrA;
454             j_coord_offsetB  = DIM*jnrB;
455             j_coord_offsetC  = DIM*jnrC;
456             j_coord_offsetD  = DIM*jnrD;
457             j_coord_offsetE  = DIM*jnrE;
458             j_coord_offsetF  = DIM*jnrF;
459             j_coord_offsetG  = DIM*jnrG;
460             j_coord_offsetH  = DIM*jnrH;
461
462             /* load j atom coordinates */
463             /* #if GEOMETRY_J == 'Particle'             */
464             gmx_mm256_load_1rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
465                                                  x+j_coord_offsetC,x+j_coord_offsetD,
466                                                  x+j_coord_offsetE,x+j_coord_offsetF,
467                                                  x+j_coord_offsetG,x+j_coord_offsetH,
468                                                  &jx0,&jy0,&jz0);
469             /* #elif GEOMETRY_J == 'Water3'             */
470             gmx_mm256_load_3rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
471                                                  x+j_coord_offsetC,x+j_coord_offsetD,
472                                                  x+j_coord_offsetE,x+j_coord_offsetF,
473                                                  x+j_coord_offsetG,x+j_coord_offsetH,
474                                               &jx0,&jy0,&jz0,&jx1,&jy1,&jz1,&jx2,&jy2,&jz2);
475             /* #elif GEOMETRY_J == 'Water4'             */
476             /*     #if 0 in PARTICLES_J                 */
477             gmx_mm256_load_4rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
478                                                  x+j_coord_offsetC,x+j_coord_offsetD,
479                                                  x+j_coord_offsetE,x+j_coord_offsetF,
480                                                  x+j_coord_offsetG,x+j_coord_offsetH,
481                                                  &jx0,&jy0,&jz0,&jx1,&jy1,&jz1,&jx2,
482                                                  &jy2,&jz2,&jx3,&jy3,&jz3);
483             /*     #else                                */
484             gmx_mm256_load_3rvec_8ptr_swizzle_ps(x+j_coord_offsetA+DIM,x+j_coord_offsetB+DIM,
485                                                  x+j_coord_offsetC+DIM,x+j_coord_offsetD+DIM,
486                                                  x+j_coord_offsetE+DIM,x+j_coord_offsetF+DIM,
487                                                  x+j_coord_offsetG+DIM,x+j_coord_offsetH+DIM,
488                                                  &jx1,&jy1,&jz1,&jx2,&jy2,&jz2,&jx3,&jy3,&jz3);
489             /*     #endif                               */
490             /* #endif                                   */
491
492             /* Calculate displacement vector */
493             /* #for I,J in PAIRS_IJ */
494             dx{I}{J}             = _mm256_sub_ps(ix{I},jx{J});
495             dy{I}{J}             = _mm256_sub_ps(iy{I},jy{J});
496             dz{I}{J}             = _mm256_sub_ps(iz{I},jz{J});
497             /*     #define INNERFLOPS INNERFLOPS+3 */
498             /* #endfor */
499
500             /* Calculate squared distance and things based on it */
501             /* #for I,J in PAIRS_IJ */
502             rsq{I}{J}            = gmx_mm256_calc_rsq_ps(dx{I}{J},dy{I}{J},dz{I}{J});
503             /*     #define INNERFLOPS INNERFLOPS+5 */
504             /* #endfor */
505
506             /* #for I,J in PAIRS_IJ */
507             /*     #if 'rinv' in INTERACTION_FLAGS[I][J] */
508             rinv{I}{J}           = avx256_invsqrt_f(rsq{I}{J});
509             /*         #define INNERFLOPS INNERFLOPS+5 */
510             /*     #endif */
511             /* #endfor */
512
513             /* #for I,J in PAIRS_IJ */
514             /*     #if 'rinvsq' in INTERACTION_FLAGS[I][J] */
515             /*         # if 'rinv' not in INTERACTION_FLAGS[I][J] */
516             rinvsq{I}{J}         = avx256_inv_f(rsq{I}{J});
517             /*             #define INNERFLOPS INNERFLOPS+4 */
518             /*         #else */
519             rinvsq{I}{J}         = _mm256_mul_ps(rinv{I}{J},rinv{I}{J});
520             /*             #define INNERFLOPS INNERFLOPS+1 */
521             /*         #endif */
522             /*     #endif */
523             /* #endfor */
524
525             /* #if not 'Water' in GEOMETRY_J */
526             /* Load parameters for j particles */
527             /*     #for J in PARTICLES_ELEC_J */
528             jq{J}              = gmx_mm256_load_8real_swizzle_ps(charge+jnrA+{J},charge+jnrB+{J},
529                                                                  charge+jnrC+{J},charge+jnrD+{J},
530                                                                  charge+jnrE+{J},charge+jnrF+{J},
531                                                                  charge+jnrG+{J},charge+jnrH+{J});
532             /*     #endfor */
533             /*     #for J in PARTICLES_VDW_J */
534             vdwjidx{J}A        = 2*vdwtype[jnrA+{J}];
535             vdwjidx{J}B        = 2*vdwtype[jnrB+{J}];
536             vdwjidx{J}C        = 2*vdwtype[jnrC+{J}];
537             vdwjidx{J}D        = 2*vdwtype[jnrD+{J}];
538             vdwjidx{J}E        = 2*vdwtype[jnrE+{J}];
539             vdwjidx{J}F        = 2*vdwtype[jnrF+{J}];
540             vdwjidx{J}G        = 2*vdwtype[jnrG+{J}];
541             vdwjidx{J}H        = 2*vdwtype[jnrH+{J}];
542             /*     #endfor */
543             /* #endif */
544
545             /* #if 'Force' in KERNEL_VF and not 'Particle' in GEOMETRY_I */
546             /*     #for J in PARTICLES_J */
547             fjx{J}             = _mm256_setzero_ps();
548             fjy{J}             = _mm256_setzero_ps();
549             fjz{J}             = _mm256_setzero_ps();
550             /*     #endfor */
551             /* #endif */
552
553             /* #for I,J in PAIRS_IJ */
554
555             /**************************
556              * CALCULATE INTERACTIONS *
557              **************************/
558
559             /*     ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
560             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
561             /*         ## We always calculate rinv/rinvsq above to enable pipelineing in compilers (performance tested on x86) */
562             if (gmx_mm256_any_lt(rsq{I}{J},rcutoff2))
563             {
564                 /*     #if 0    ## this and the next two lines is a hack to maintain auto-indentation in template file */
565             }
566             /*         #endif */
567             /*         #define INNERFLOPS INNERFLOPS+1 */
568             /*     #endif */
569
570             /*     #if 'r' in INTERACTION_FLAGS[I][J] */
571             r{I}{J}              = _mm256_mul_ps(rsq{I}{J},rinv{I}{J});
572             /*         #if ROUND == 'Epilogue' */
573             r{I}{J}              = _mm256_andnot_ps(dummy_mask,r{I}{J});
574             /*             #define INNERFLOPS INNERFLOPS+1 */
575             /*         #endif */
576             /*         #define INNERFLOPS INNERFLOPS+1 */
577             /*     #endif */
578
579             /*     ## For water geometries we already loaded parameters at the start of the kernel */
580             /*     #if not 'Water' in GEOMETRY_J */
581             /* Compute parameters for interactions between i and j atoms */
582             /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
583             qq{I}{J}             = _mm256_mul_ps(iq{I},jq{J});
584             /*             #define INNERFLOPS INNERFLOPS+1 */
585             /*         #endif */
586             /*         #if 'vdw' in INTERACTION_FLAGS[I][J] */
587             gmx_mm256_load_8pair_swizzle_ps(vdwioffsetptr{I}+vdwjidx{J}A,
588                                             vdwioffsetptr{I}+vdwjidx{J}B,
589                                             vdwioffsetptr{I}+vdwjidx{J}C,
590                                             vdwioffsetptr{I}+vdwjidx{J}D,
591                                             vdwioffsetptr{I}+vdwjidx{J}E,
592                                             vdwioffsetptr{I}+vdwjidx{J}F,
593                                             vdwioffsetptr{I}+vdwjidx{J}G,
594                                             vdwioffsetptr{I}+vdwjidx{J}H,
595                                             &c6_{I}{J},&c12_{I}{J});
596
597             /*         #if 'LJEwald' in KERNEL_VDW */
598             c6grid_{I}{J}       = gmx_mm256_load_8real_swizzle_ps(vdwgridioffsetptr{I}+vdwjidx{J}A,
599                                                                   vdwgridioffsetptr{I}+vdwjidx{J}B,
600                                                                   vdwgridioffsetptr{I}+vdwjidx{J}C,
601                                                                   vdwgridioffsetptr{I}+vdwjidx{J}D,
602                                                                   vdwgridioffsetptr{I}+vdwjidx{J}E,
603                                                                   vdwgridioffsetptr{I}+vdwjidx{J}F,
604                                                                   vdwgridioffsetptr{I}+vdwjidx{J}G,
605                                                                   vdwgridioffsetptr{I}+vdwjidx{J}H);
606             /*          #endif */
607             /*         #endif */
608             /*     #endif */
609
610             /*     #if 'table' in INTERACTION_FLAGS[I][J] */
611             /* Calculate table index by multiplying r with table scale and truncate to integer */
612             rt               = _mm256_mul_ps(r{I}{J},vftabscale);
613             vfitab           = _mm256_cvttps_epi32(rt);
614             vfeps            = _mm256_sub_ps(rt,_mm256_round_ps(rt, _MM_FROUND_FLOOR));
615             /*         #define INNERFLOPS INNERFLOPS+4                          */
616             /*         AVX1 does not support 256-bit integer operations, so now we go to 128-bit mode... */
617             vfitab_lo        = _mm256_extractf128_si256(vfitab,0x0);
618             vfitab_hi        = _mm256_extractf128_si256(vfitab,0x1);
619             /*         #if 'Table' in KERNEL_ELEC and 'Table' in KERNEL_VDW */
620             /*             ## 3 tables, 4 bytes per point: multiply index by 12 */
621             vfitab_lo        = _mm_slli_epi32(_mm_add_epi32(vfitab_lo,_mm_slli_epi32(vfitab_lo,1)),2);
622             vfitab_hi        = _mm_slli_epi32(_mm_add_epi32(vfitab_hi,_mm_slli_epi32(vfitab_hi,1)),2);
623             /*         #elif 'Table' in KERNEL_ELEC                             */
624             /*             ## 1 table, 4 bytes per point: multiply index by 4   */
625             vfitab_lo        = _mm_slli_epi32(vfitab_lo,2);
626             vfitab_hi        = _mm_slli_epi32(vfitab_hi,2);
627             /*         #elif 'Table' in KERNEL_VDW */
628             /*             ## 2 tables, 4 bytes per point: multiply index by 8  */
629             vfitab_lo        = _mm_slli_epi32(vfitab_lo,3);
630             vfitab_hi        = _mm_slli_epi32(vfitab_hi,3);
631             /*         #endif                                                   */
632             /*     #endif */
633
634             /*     ## ELECTROSTATIC INTERACTIONS */
635             /*     #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
636
637             /*         #if KERNEL_ELEC=='Coulomb' */
638
639             /* COULOMB ELECTROSTATICS */
640             velec            = _mm256_mul_ps(qq{I}{J},rinv{I}{J});
641             /*             #define INNERFLOPS INNERFLOPS+1 */
642             /*             #if 'Force' in KERNEL_VF */
643             felec            = _mm256_mul_ps(velec,rinvsq{I}{J});
644             /*                 #define INNERFLOPS INNERFLOPS+1 */
645             /*             #endif */
646
647             /*         #elif KERNEL_ELEC=='ReactionField' */
648
649             /* REACTION-FIELD ELECTROSTATICS */
650             /*             #if 'Potential' in KERNEL_VF */
651             velec            = _mm256_mul_ps(qq{I}{J},_mm256_sub_ps(_mm256_add_ps(rinv{I}{J},_mm256_mul_ps(krf,rsq{I}{J})),crf));
652             /*                 #define INNERFLOPS INNERFLOPS+4 */
653             /*             #endif */
654             /*             #if 'Force' in KERNEL_VF */
655             felec            = _mm256_mul_ps(qq{I}{J},_mm256_sub_ps(_mm256_mul_ps(rinv{I}{J},rinvsq{I}{J}),krf2));
656             /*                 #define INNERFLOPS INNERFLOPS+3 */
657             /*             #endif */
658
659             /*         #elif KERNEL_ELEC=='Ewald' */
660             /* EWALD ELECTROSTATICS */
661             
662             /* Analytical PME correction */
663             zeta2            = _mm256_mul_ps(beta2,rsq{I}{J});
664             /*             #if 'Force' in KERNEL_VF */
665             rinv3            = _mm256_mul_ps(rinvsq{I}{J},rinv{I}{J});
666             pmecorrF         = avx256_pmecorrF_f(zeta2);
667             felec            = _mm256_add_ps( _mm256_mul_ps(pmecorrF,beta3), rinv3);
668             felec            = _mm256_mul_ps(qq{I}{J},felec);
669             /*                 #define INNERFLOPS INNERFLOPS+31 */
670             /*             #endif */
671             /*             #if 'Potential' in KERNEL_VF or KERNEL_MOD_ELEC=='PotentialSwitch' */
672             pmecorrV         = avx256_pmecorrV_f(zeta2);
673             pmecorrV         = _mm256_mul_ps(pmecorrV,beta);
674             /*                 #define INNERFLOPS INNERFLOPS+27 */
675             /*                 #if KERNEL_MOD_ELEC=='PotentialShift' */
676             velec            = _mm256_sub_ps(_mm256_sub_ps(rinv{I}{J},sh_ewald),pmecorrV);
677             /*                     #define INNERFLOPS INNERFLOPS+21 */
678             /*                 #else */
679             velec            = _mm256_sub_ps(rinv{I}{J},pmecorrV);
680             /*                 #endif */
681             velec            = _mm256_mul_ps(qq{I}{J},velec);
682             /*             #endif */
683             
684             /*         #elif KERNEL_ELEC=='CubicSplineTable' */
685
686             /* CUBIC SPLINE TABLE ELECTROSTATICS */
687             Y                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,0)),
688                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,0)));
689             F                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,1)),
690                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,1)));
691             G                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,2)),
692                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,2)));
693             H                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,3)),
694                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,3)));
695             GMX_MM256_HALFTRANSPOSE4_PS(Y,F,G,H);
696             Heps             = _mm256_mul_ps(vfeps,H);
697             Fp               = _mm256_add_ps(F,_mm256_mul_ps(vfeps,_mm256_add_ps(G,Heps)));
698             /*             #define INNERFLOPS INNERFLOPS+4 */
699             /*             #if 'Potential' in KERNEL_VF */
700             VV               = _mm256_add_ps(Y,_mm256_mul_ps(vfeps,Fp));
701             velec            = _mm256_mul_ps(qq{I}{J},VV);
702             /*                 #define INNERFLOPS INNERFLOPS+3 */
703             /*             #endif */
704             /*             #if 'Force' in KERNEL_VF */
705             FF               = _mm256_add_ps(Fp,_mm256_mul_ps(vfeps,_mm256_add_ps(G,_mm256_add_ps(Heps,Heps))));
706             felec            = _mm256_xor_ps(signbit,_mm256_mul_ps(_mm256_mul_ps(qq{I}{J},FF),_mm256_mul_ps(vftabscale,rinv{I}{J})));
707             /*                 #define INNERFLOPS INNERFLOPS+7 */
708             /*             #endif */
709             /*         #endif */
710             /*         ## End of check for electrostatics interaction forms */
711             /*     #endif */
712             /*     ## END OF ELECTROSTATIC INTERACTION CHECK FOR PAIR I-J */
713
714             /*     #if 'vdw' in INTERACTION_FLAGS[I][J] */
715
716             /*         #if KERNEL_VDW=='LennardJones' */
717
718             /* LENNARD-JONES DISPERSION/REPULSION */
719
720             rinvsix          = _mm256_mul_ps(_mm256_mul_ps(rinvsq{I}{J},rinvsq{I}{J}),rinvsq{I}{J});
721             /*             #define INNERFLOPS INNERFLOPS+2 */
722             /*             #if 'Potential' in KERNEL_VF or KERNEL_MOD_VDW=='PotentialSwitch' */
723             vvdw6            = _mm256_mul_ps(c6_{I}{J},rinvsix);
724             vvdw12           = _mm256_mul_ps(c12_{I}{J},_mm256_mul_ps(rinvsix,rinvsix));
725             /*                 #define INNERFLOPS INNERFLOPS+3 */
726             /*                 #if KERNEL_MOD_VDW=='PotentialShift' */
727             vvdw             = _mm256_sub_ps(_mm256_mul_ps( _mm256_sub_ps(vvdw12 , _mm256_mul_ps(c12_{I}{J},_mm256_mul_ps(sh_vdw_invrcut6,sh_vdw_invrcut6))), one_twelfth) ,
728                                           _mm256_mul_ps( _mm256_sub_ps(vvdw6,_mm256_mul_ps(c6_{I}{J},sh_vdw_invrcut6)),one_sixth));
729             /*                     #define INNERFLOPS INNERFLOPS+8 */
730             /*                 #else */
731             vvdw             = _mm256_sub_ps( _mm256_mul_ps(vvdw12,one_twelfth) , _mm256_mul_ps(vvdw6,one_sixth) );
732             /*                     #define INNERFLOPS INNERFLOPS+3 */
733             /*                 #endif */
734             /*                 ## Check for force inside potential check, i.e. this means we already did the potential part */
735             /*                 #if 'Force' in KERNEL_VF */
736             fvdw             = _mm256_mul_ps(_mm256_sub_ps(vvdw12,vvdw6),rinvsq{I}{J});
737             /*                     #define INNERFLOPS INNERFLOPS+2 */
738             /*                 #endif */
739             /*             #elif KERNEL_VF=='Force' */
740             /*                 ## Force-only LennardJones makes it possible to save 1 flop (they do add up...) */
741             fvdw             = _mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(c12_{I}{J},rinvsix),c6_{I}{J}),_mm256_mul_ps(rinvsix,rinvsq{I}{J}));
742             /*                 #define INNERFLOPS INNERFLOPS+4 */
743             /*             #endif */
744
745             /*         #elif KERNEL_VDW=='CubicSplineTable' */
746
747             /* CUBIC SPLINE TABLE DISPERSION */
748             /*             #if 'Table' in KERNEL_ELEC */
749             vfitab_lo        = _mm_add_epi32(vfitab_lo,ifour);
750             vfitab_hi        = _mm_add_epi32(vfitab_hi,ifour);
751             /*             #endif                     */
752             Y                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,0)),
753                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,0)));
754             F                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,1)),
755                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,1)));
756             G                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,2)),
757                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,2)));
758             H                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,3)),
759                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,3)));
760             GMX_MM256_HALFTRANSPOSE4_PS(Y,F,G,H);
761             Heps             = _mm256_mul_ps(vfeps,H);
762             Fp               = _mm256_add_ps(F,_mm256_mul_ps(vfeps,_mm256_add_ps(G,Heps)));
763             /*             #define INNERFLOPS INNERFLOPS+4 */
764             /*             #if 'Potential' in KERNEL_VF */
765             VV               = _mm256_add_ps(Y,_mm256_mul_ps(vfeps,Fp));
766             vvdw6            = _mm256_mul_ps(c6_{I}{J},VV);
767             /*                 #define INNERFLOPS INNERFLOPS+3 */
768             /*             #endif */
769             /*             #if 'Force' in KERNEL_VF */
770             FF               = _mm256_add_ps(Fp,_mm256_mul_ps(vfeps,_mm256_add_ps(G,_mm256_add_ps(Heps,Heps))));
771             fvdw6            = _mm256_mul_ps(c6_{I}{J},FF);
772             /*                 #define INNERFLOPS INNERFLOPS+4 */
773             /*             #endif */
774
775             /* CUBIC SPLINE TABLE REPULSION */
776             vfitab_lo        = _mm_add_epi32(vfitab_lo,ifour);
777             vfitab_hi        = _mm_add_epi32(vfitab_hi,ifour);
778             Y                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,0)),
779                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,0)));
780             F                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,1)),
781                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,1)));
782             G                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,2)),
783                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,2)));
784             H                = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,3)),
785                                                   _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,3)));
786             GMX_MM256_HALFTRANSPOSE4_PS(Y,F,G,H);
787             Heps             = _mm256_mul_ps(vfeps,H);
788             Fp               = _mm256_add_ps(F,_mm256_mul_ps(vfeps,_mm256_add_ps(G,Heps)));
789             /*             #define INNERFLOPS INNERFLOPS+4 */
790             /*             #if 'Potential' in KERNEL_VF */
791             VV               = _mm256_add_ps(Y,_mm256_mul_ps(vfeps,Fp));
792             vvdw12           = _mm256_mul_ps(c12_{I}{J},VV);
793             /*                 #define INNERFLOPS INNERFLOPS+3 */
794             /*             #endif */
795             /*             #if 'Force' in KERNEL_VF */
796             FF               = _mm256_add_ps(Fp,_mm256_mul_ps(vfeps,_mm256_add_ps(G,_mm256_add_ps(Heps,Heps))));
797             fvdw12           = _mm256_mul_ps(c12_{I}{J},FF);
798             /*                 #define INNERFLOPS INNERFLOPS+5 */
799             /*             #endif */
800             /*             #if 'Potential' in KERNEL_VF */
801             vvdw             = _mm256_add_ps(vvdw12,vvdw6);
802             /*                 #define INNERFLOPS INNERFLOPS+1 */
803             /*             #endif */
804             /*             #if 'Force' in KERNEL_VF */
805             fvdw             = _mm256_xor_ps(signbit,_mm256_mul_ps(_mm256_add_ps(fvdw6,fvdw12),_mm256_mul_ps(vftabscale,rinv{I}{J})));
806             /*                 #define INNERFLOPS INNERFLOPS+4 */
807             /*             #endif */
808
809             /*         #elif KERNEL_VDW=='LJEwald' */
810
811             /* Analytical LJ-PME */
812             rinvsix          = _mm256_mul_ps(_mm256_mul_ps(rinvsq{I}{J},rinvsq{I}{J}),rinvsq{I}{J});
813             ewcljrsq         = _mm256_mul_ps(ewclj2,rsq{I}{J});
814             ewclj6           = _mm256_mul_ps(ewclj2,_mm256_mul_ps(ewclj2,ewclj2));
815             exponent         = avx256_exp_f(ewcljrsq);
816             /* poly = exp(-(beta*r)^2) * (1 + (beta*r)^2 + (beta*r)^4 /2) */
817             poly             = _mm256_mul_ps(exponent,_mm256_add_ps(_mm256_sub_ps(one,ewcljrsq),_mm256_mul_ps(_mm256_mul_ps(ewcljrsq,ewcljrsq),one_half)));
818             /*                 #define INNERFLOPS INNERFLOPS+11 */
819             /*             #if 'Potential' in KERNEL_VF or KERNEL_MOD_VDW=='PotentialSwitch' */
820             /* vvdw6 = [C6 - C6grid * (1-poly)]/r6 */
821             vvdw6            = _mm256_mul_ps(_mm256_sub_ps(c6_{I}{J},_mm256_mul_ps(c6grid_{I}{J},_mm256_sub_ps(one,poly))),rinvsix);
822             vvdw12           = _mm256_mul_ps(c12_{I}{J},_mm256_mul_ps(rinvsix,rinvsix));
823             /*                 #define INNERFLOPS INNERFLOPS+6 */
824             /*                 #if KERNEL_MOD_VDW=='PotentialShift' */
825             vvdw             = _mm256_sub_ps(_mm256_mul_ps( _mm256_sub_ps(vvdw12 , _mm256_mul_ps(c12_{I}{J},_mm256_mul_ps(sh_vdw_invrcut6,sh_vdw_invrcut6))), one_twelfth) ,
826                                           _mm256_mul_ps( _mm256_sub_ps(vvdw6,_mm256_add_ps(_mm256_mul_ps(c6_{I}{J},sh_vdw_invrcut6),_mm256_mul_ps(c6grid_{I}{J},sh_lj_ewald))),one_sixth));
827             /*                     #define INNERFLOPS INNERFLOPS+10 */
828             /*                 #else */
829             vvdw             = _mm256_sub_ps(_mm256_mul_ps(vvdw12,one_twelfth),_mm256_mul_ps(vvdw6,one_sixth));
830             /*                 #define INNERFLOPS INNERFLOPS+3 */
831             /*                 #endif */
832             /*                  ## Check for force inside potential check, i.e. this means we already did the potential part */
833             /*                  #if 'Force' in KERNEL_VF */
834             /* fvdw = vvdw12/r - (vvdw6/r + (C6grid * exponent * beta^6)/r) */
835             fvdw             = _mm256_mul_ps(_mm256_sub_ps(vvdw12,_mm256_sub_ps(vvdw6,_mm256_mul_ps(_mm256_mul_ps(c6grid_{I}{J},one_sixth),_mm256_mul_ps(exponent,ewclj6)))),rinvsq{I}{J});
836             /*                 #define INNERFLOPS INNERFLOPS+6 */
837             /*                  #endif */
838             /*              #elif KERNEL_VF=='Force' */
839             /* f6A = 6 * C6grid * (1 - poly) */
840             f6A              = _mm256_mul_ps(c6grid_{I}{J},_mm256_sub_ps(one,poly));
841             /* f6B = C6grid * exponent * beta^6 */
842             f6B              = _mm256_mul_ps(_mm256_mul_ps(c6grid_{I}{J},one_sixth),_mm256_mul_ps(exponent,ewclj6));
843             /* fvdw = 12*C12/r13 - ((6*C6 - f6A)/r6 + f6B)/r */
844             fvdw              = _mm256_mul_ps(_mm256_add_ps(_mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(c12_{I}{J},rinvsix),_mm256_sub_ps(c6_{I}{J},f6A)),rinvsix),f6B),rinvsq{I}{J});
845             /*                 #define INNERFLOPS INNERFLOPS+11 */
846             /*              #endif */
847             /*         #endif */
848             /*         ## End of check for vdw interaction forms */
849             /*     #endif */
850             /*     ## END OF VDW INTERACTION CHECK FOR PAIR I-J */
851
852             /*     #if 'switch' in INTERACTION_FLAGS[I][J] */
853             d                = _mm256_sub_ps(r{I}{J},rswitch);
854             d                = _mm256_max_ps(d,_mm256_setzero_ps());
855             d2               = _mm256_mul_ps(d,d);
856             sw               = _mm256_add_ps(one,_mm256_mul_ps(d2,_mm256_mul_ps(d,_mm256_add_ps(swV3,_mm256_mul_ps(d,_mm256_add_ps(swV4,_mm256_mul_ps(d,swV5)))))));
857             /*         #define INNERFLOPS INNERFLOPS+10 */
858
859             /*         #if 'Force' in KERNEL_VF */
860             dsw              = _mm256_mul_ps(d2,_mm256_add_ps(swF2,_mm256_mul_ps(d,_mm256_add_ps(swF3,_mm256_mul_ps(d,swF4)))));
861             /*             #define INNERFLOPS INNERFLOPS+5 */
862             /*         #endif */
863
864             /* Evaluate switch function */
865             /*         #if 'Force' in KERNEL_VF */
866             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
867             /*             #if 'electrostatics' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_ELEC=='PotentialSwitch' */
868             felec            = _mm256_sub_ps( _mm256_mul_ps(felec,sw) , _mm256_mul_ps(rinv{I}{J},_mm256_mul_ps(velec,dsw)) );
869             /*                 #define INNERFLOPS INNERFLOPS+4 */
870             /*             #endif */
871             /*             #if 'vdw' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_VDW=='PotentialSwitch' */
872             fvdw             = _mm256_sub_ps( _mm256_mul_ps(fvdw,sw) , _mm256_mul_ps(rinv{I}{J},_mm256_mul_ps(vvdw,dsw)) );
873             /*                 #define INNERFLOPS INNERFLOPS+4 */
874             /*             #endif */
875             /*         #endif */
876             /*         #if 'Potential' in KERNEL_VF */
877             /*             #if 'electrostatics' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_ELEC=='PotentialSwitch' */
878             velec            = _mm256_mul_ps(velec,sw);
879             /*                 #define INNERFLOPS INNERFLOPS+1 */
880             /*             #endif */
881             /*             #if 'vdw' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_VDW=='PotentialSwitch' */
882             vvdw             = _mm256_mul_ps(vvdw,sw);
883             /*                 #define INNERFLOPS INNERFLOPS+1 */
884             /*             #endif */
885             /*         #endif */
886             /*     #endif */
887             /*     ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
888             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
889             cutoff_mask      = _mm256_cmp_ps(rsq{I}{J},rcutoff2,_CMP_LT_OQ);
890             /*         #define INNERFLOPS INNERFLOPS+1 */
891             /*     #endif */
892
893             /*     #if 'Potential' in KERNEL_VF */
894             /* Update potential sum for this i atom from the interaction with this j atom. */
895             /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
896             /*             #if 'exactcutoff' in INTERACTION_FLAGS[I][J] */
897             velec            = _mm256_and_ps(velec,cutoff_mask);
898             /*                 #define INNERFLOPS INNERFLOPS+1 */
899             /*             #endif                                       */
900             /*             #if ROUND == 'Epilogue' */
901             velec            = _mm256_andnot_ps(dummy_mask,velec);
902             /*             #endif */
903             velecsum         = _mm256_add_ps(velecsum,velec);
904             /*             #define INNERFLOPS INNERFLOPS+1 */
905             /*         #endif */
906             /*         #if 'vdw' in INTERACTION_FLAGS[I][J] */
907             /*     ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
908             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
909             vvdw             = _mm256_and_ps(vvdw,cutoff_mask);
910             /*                 #define INNERFLOPS INNERFLOPS+1 */
911             /*             #endif                                       */
912             /*             #if ROUND == 'Epilogue' */
913             vvdw             = _mm256_andnot_ps(dummy_mask,vvdw);
914             /*             #endif */
915             vvdwsum          = _mm256_add_ps(vvdwsum,vvdw);
916             /*             #define INNERFLOPS INNERFLOPS+1 */
917             /*         #endif */
918             /*     #endif */
919
920             /*     #if 'Force' in KERNEL_VF */
921
922             /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] and 'vdw' in INTERACTION_FLAGS[I][J] */
923             fscal            = _mm256_add_ps(felec,fvdw);
924             /*             #define INNERFLOPS INNERFLOPS+1 */
925             /*         #elif 'electrostatics' in INTERACTION_FLAGS[I][J] */
926             fscal            = felec;
927             /*         #elif 'vdw' in INTERACTION_FLAGS[I][J] */
928             fscal            = fvdw;
929             /*        #endif */
930
931             /*     ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
932             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
933             fscal            = _mm256_and_ps(fscal,cutoff_mask);
934             /*                 #define INNERFLOPS INNERFLOPS+1 */
935             /*             #endif                                       */
936
937             /*             #if ROUND == 'Epilogue' */
938             fscal            = _mm256_andnot_ps(dummy_mask,fscal);
939             /*             #endif */
940
941             /* Calculate temporary vectorial force */
942             tx               = _mm256_mul_ps(fscal,dx{I}{J});
943             ty               = _mm256_mul_ps(fscal,dy{I}{J});
944             tz               = _mm256_mul_ps(fscal,dz{I}{J});
945
946             /* Update vectorial force */
947             fix{I}             = _mm256_add_ps(fix{I},tx);
948             fiy{I}             = _mm256_add_ps(fiy{I},ty);
949             fiz{I}             = _mm256_add_ps(fiz{I},tz);
950             /*             #define INNERFLOPS INNERFLOPS+6 */
951
952             /* #if GEOMETRY_I == 'Particle'             */
953             /*     #if ROUND == 'Loop' */
954             fjptrA             = f+j_coord_offsetA;
955             fjptrB             = f+j_coord_offsetB;
956             fjptrC             = f+j_coord_offsetC;
957             fjptrD             = f+j_coord_offsetD;
958             fjptrE             = f+j_coord_offsetE;
959             fjptrF             = f+j_coord_offsetF;
960             fjptrG             = f+j_coord_offsetG;
961             fjptrH             = f+j_coord_offsetH;
962             /*     #else */
963             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
964             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
965             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
966             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
967             fjptrE             = (jnrlistE>=0) ? f+j_coord_offsetE : scratch;
968             fjptrF             = (jnrlistF>=0) ? f+j_coord_offsetF : scratch;
969             fjptrG             = (jnrlistG>=0) ? f+j_coord_offsetG : scratch;
970             fjptrH             = (jnrlistH>=0) ? f+j_coord_offsetH : scratch;
971             /*     #endif */
972             gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,tx,ty,tz);
973             /*     #define INNERFLOPS INNERFLOPS+3      */
974             /* #else                                    */
975             fjx{J}             = _mm256_add_ps(fjx{J},tx);
976             fjy{J}             = _mm256_add_ps(fjy{J},ty);
977             fjz{J}             = _mm256_add_ps(fjz{J},tz);
978             /*     #define INNERFLOPS INNERFLOPS+3      */
979             /* #endif                                   */
980
981             /*     #endif */
982
983             /*     ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
984             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
985             /*         #if 0    ## This and next two lines is a hack to maintain indentation in template file */
986             {
987                 /*     #endif */
988             }
989             /*     #endif */
990             /*    ## End of check for the interaction being outside the cutoff */
991
992             /* #endfor */
993             /* ## End of loop over i-j interaction pairs */
994
995             /* #if GEOMETRY_I != 'Particle' */
996             /*     #if ROUND == 'Loop' */
997             fjptrA             = f+j_coord_offsetA;
998             fjptrB             = f+j_coord_offsetB;
999             fjptrC             = f+j_coord_offsetC;
1000             fjptrD             = f+j_coord_offsetD;
1001             fjptrE             = f+j_coord_offsetE;
1002             fjptrF             = f+j_coord_offsetF;
1003             fjptrG             = f+j_coord_offsetG;
1004             fjptrH             = f+j_coord_offsetH;
1005             /*     #else */
1006             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
1007             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
1008             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
1009             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
1010             fjptrE             = (jnrlistE>=0) ? f+j_coord_offsetE : scratch;
1011             fjptrF             = (jnrlistF>=0) ? f+j_coord_offsetF : scratch;
1012             fjptrG             = (jnrlistG>=0) ? f+j_coord_offsetG : scratch;
1013             fjptrH             = (jnrlistH>=0) ? f+j_coord_offsetH : scratch;
1014             /*     #endif */
1015             /* #endif */
1016
1017             /* #if 'Water' in GEOMETRY_I and GEOMETRY_J == 'Particle' */
1018             gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,fjx0,fjy0,fjz0);
1019             /*     #define INNERFLOPS INNERFLOPS+3      */
1020             /* #elif GEOMETRY_J == 'Water3'             */
1021             gmx_mm256_decrement_3rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,
1022                                                       fjx0,fjy0,fjz0,fjx1,fjy1,fjz1,fjx2,fjy2,fjz2);
1023             /*     #define INNERFLOPS INNERFLOPS+9      */
1024             /* #elif GEOMETRY_J == 'Water4'             */
1025             /*     #if 0 in PARTICLES_J                 */
1026             gmx_mm256_decrement_4rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,
1027                                                       fjx0,fjy0,fjz0,fjx1,fjy1,fjz1,
1028                                                       fjx2,fjy2,fjz2,fjx3,fjy3,fjz3);
1029             /*     #define INNERFLOPS INNERFLOPS+12     */
1030             /*     #else                                */
1031             gmx_mm256_decrement_3rvec_8ptr_swizzle_ps(fjptrA+DIM,fjptrB+DIM,fjptrC+DIM,fjptrD+DIM,
1032                                                       fjptrE+DIM,fjptrF+DIM,fjptrG+DIM,fjptrH+DIM,
1033                                                       fjx1,fjy1,fjz1,fjx2,fjy2,fjz2,fjx3,fjy3,fjz3);
1034             /*     #define INNERFLOPS INNERFLOPS+9      */
1035             /*     #endif                               */
1036             /* #endif                                   */
1037
1038             /* Inner loop uses {INNERFLOPS} flops */
1039         }
1040
1041         /* #endfor */
1042
1043         /* End of innermost loop */
1044
1045         /* #if 'Force' in KERNEL_VF */
1046         /*     #if GEOMETRY_I == 'Particle'            */
1047         gmx_mm256_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
1048                                                  f+i_coord_offset,fshift+i_shift_offset);
1049         /*         #define OUTERFLOPS OUTERFLOPS+6     */
1050         /*     #elif GEOMETRY_I == 'Water3'            */
1051         gmx_mm256_update_iforce_3atom_swizzle_ps(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,
1052                                                  f+i_coord_offset,fshift+i_shift_offset);
1053         /*         #define OUTERFLOPS OUTERFLOPS+18    */
1054         /*     #elif GEOMETRY_I == 'Water4'            */
1055         /*         #if 0 in PARTICLES_I                */
1056         gmx_mm256_update_iforce_4atom_swizzle_ps(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,fix3,fiy3,fiz3,
1057                                                  f+i_coord_offset,fshift+i_shift_offset);
1058         /*             #define OUTERFLOPS OUTERFLOPS+24    */
1059         /*         #else                               */
1060         gmx_mm256_update_iforce_3atom_swizzle_ps(fix1,fiy1,fiz1,fix2,fiy2,fiz2,fix3,fiy3,fiz3,
1061                                                  f+i_coord_offset+DIM,fshift+i_shift_offset);
1062         /*             #define OUTERFLOPS OUTERFLOPS+18    */
1063         /*         #endif                              */
1064         /*     #endif                                  */
1065         /* #endif                                      */
1066
1067         /* #if 'Potential' in KERNEL_VF */
1068         ggid                        = gid[iidx];
1069         /* Update potential energies */
1070         /*     #if KERNEL_ELEC != 'None' */
1071         gmx_mm256_update_1pot_ps(velecsum,kernel_data->energygrp_elec+ggid);
1072         /*         #define OUTERFLOPS OUTERFLOPS+1 */
1073         /*     #endif */
1074         /*     #if KERNEL_VDW != 'None' */
1075         gmx_mm256_update_1pot_ps(vvdwsum,kernel_data->energygrp_vdw+ggid);
1076         /*         #define OUTERFLOPS OUTERFLOPS+1 */
1077         /*     #endif */
1078         /* #endif */
1079
1080         /* Increment number of inner iterations */
1081         inneriter                  += j_index_end - j_index_start;
1082
1083         /* Outer loop uses {OUTERFLOPS} flops */
1084     }
1085
1086     /* Increment number of outer iterations */
1087     outeriter        += nri;
1088
1089     /* Update outer/inner flops */
1090     /* ## NB: This is not important, it just affects the flopcount. However, since our preprocessor is */
1091     /* ## primitive and replaces aggressively even in strings inside these directives, we need to      */
1092     /* ## assemble the main part of the name (containing KERNEL/ELEC/VDW) directly in the source.      */
1093     /* #if GEOMETRY_I == 'Water3'            */
1094     /*     #define ISUFFIX '_W3'             */
1095     /* #elif GEOMETRY_I == 'Water4'          */
1096     /*     #define ISUFFIX '_W4'             */
1097     /* #else                                 */
1098     /*     #define ISUFFIX ''                */
1099     /* #endif                                */
1100     /* #if GEOMETRY_J == 'Water3'            */
1101     /*     #define JSUFFIX 'W3'              */
1102     /* #elif GEOMETRY_J == 'Water4'          */
1103     /*     #define JSUFFIX 'W4'              */
1104     /* #else                                 */
1105     /*     #define JSUFFIX ''                */
1106     /* #endif                                */
1107     /* #if 'PotentialAndForce' in KERNEL_VF  */
1108     /*     #define VFSUFFIX  '_VF'           */
1109     /* #elif 'Potential' in KERNEL_VF        */
1110     /*     #define VFSUFFIX '_V'             */
1111     /* #else                                 */
1112     /*     #define VFSUFFIX '_F'             */
1113     /* #endif                                */
1114
1115     /* #if KERNEL_ELEC != 'None' and KERNEL_VDW != 'None' */
1116     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
1117     /* #elif KERNEL_ELEC != 'None' */
1118     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
1119     /* #else */
1120     inc_nrnb(nrnb,eNR_NBKERNEL_VDW{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
1121     /* #endif  */
1122 }