f66ab5e5da6ef30a02b2f72bb1473477d820a3cb
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_sse4_1_single / nb_kernel_template_sse4_1_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, 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_sse4_1_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  * Gromacs nonbonded kernel:   {KERNEL_NAME}
86  * Electrostatics interaction: {KERNEL_ELEC}
87  * VdW interaction:            {KERNEL_VDW}
88  * Geometry:                   {GEOMETRY_I}-{GEOMETRY_J}
89  * Calculate force/pot:        {KERNEL_VF}
90  */
91 void
92 {KERNEL_NAME}
93                     (t_nblist                    * gmx_restrict       nlist,
94                      rvec                        * gmx_restrict          xx,
95                      rvec                        * gmx_restrict          ff,
96                      struct t_forcerec           * gmx_restrict          fr,
97                      t_mdatoms                   * gmx_restrict     mdatoms,
98                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
99                      t_nrnb                      * gmx_restrict        nrnb)
100 {
101     /* ## Not all variables are used for all kernels, but any optimizing compiler fixes that, */
102     /* ## so there is no point in going to extremes to exclude variables that are not needed. */
103     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
104      * just 0 for non-waters.
105      * Suffixes A,B,C,D refer to j loop unrolling done with SSE, e.g. for the four different
106      * jnr indices corresponding to data put in the four positions in the SIMD register.
107      */
108     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
109     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
110     int              jnrA,jnrB,jnrC,jnrD;
111     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
112     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
113     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
114     real             rcutoff_scalar;
115     real             *shiftvec,*fshift,*x,*f;
116     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
117     real             scratch[4*DIM];
118     __m128           tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
119     /* #for I in PARTICLES_I */
120     int              vdwioffset{I};
121     __m128           ix{I},iy{I},iz{I},fix{I},fiy{I},fiz{I},iq{I},isai{I};
122     /* #endfor */
123     /* #for J in PARTICLES_J */
124     int              vdwjidx{J}A,vdwjidx{J}B,vdwjidx{J}C,vdwjidx{J}D;
125     __m128           jx{J},jy{J},jz{J},fjx{J},fjy{J},fjz{J},jq{J},isaj{J};
126     /* #endfor */
127     /* #for I,J in PAIRS_IJ */
128     __m128           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};
129     /* #endfor */
130     /* #if KERNEL_ELEC != 'None' */
131     __m128           velec,felec,velecsum,facel,crf,krf,krf2;
132     real             *charge;
133     /* #endif */
134     /* #if 'GeneralizedBorn' in KERNEL_ELEC */
135     __m128i          gbitab;
136     __m128           vgb,fgb,vgbsum,dvdasum,gbscale,gbtabscale,isaprod,gbqqfactor,gbinvepsdiff,gbeps,dvdatmp;
137     __m128           minushalf = _mm_set1_ps(-0.5);
138     real             *invsqrta,*dvda,*gbtab;
139     /* #endif */
140     /* #if KERNEL_VDW != 'None' */
141     int              nvdwtype;
142     __m128           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
143     int              *vdwtype;
144     real             *vdwparam;
145     __m128           one_sixth   = _mm_set1_ps(1.0/6.0);
146     __m128           one_twelfth = _mm_set1_ps(1.0/12.0);
147     /* #endif */
148     /* #if 'Table' in KERNEL_ELEC or 'GeneralizedBorn' in KERNEL_ELEC or 'Table' in KERNEL_VDW */
149     __m128i          vfitab;
150     __m128i          ifour       = _mm_set1_epi32(4);
151     __m128           rt,vfeps,vftabscale,Y,F,G,H,Heps,Fp,VV,FF;
152     real             *vftab;
153     /* #endif */
154     /* #if 'LJEwald' in KERNEL_VDW */
155     /* #for I,J in PAIRS_IJ */
156     __m128           c6grid_{I}{J};
157     /* #endfor */
158     __m128           ewclj,ewclj2,ewclj6,ewcljrsq,poly,exponent,f6A,f6B,sh_lj_ewald;
159     real             *vdwgridparam;
160     __m128           one_half  = _mm_set1_ps(0.5);
161     __m128           minus_one = _mm_set1_ps(-1.0);
162     /* #endif */
163     /* #if 'Ewald' in KERNEL_ELEC */
164     __m128i          ewitab;
165     __m128           ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
166     real             *ewtab;
167     /* #endif */
168     /* #if 'PotentialSwitch' in [KERNEL_MOD_ELEC,KERNEL_MOD_VDW] */
169     __m128           rswitch,swV3,swV4,swV5,swF2,swF3,swF4,d,d2,sw,dsw;
170     real             rswitch_scalar,d_scalar;
171     /* #endif */
172     __m128           dummy_mask,cutoff_mask;
173     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
174     __m128           one     = _mm_set1_ps(1.0);
175     __m128           two     = _mm_set1_ps(2.0);
176     x                = xx[0];
177     f                = ff[0];
178
179     nri              = nlist->nri;
180     iinr             = nlist->iinr;
181     jindex           = nlist->jindex;
182     jjnr             = nlist->jjnr;
183     shiftidx         = nlist->shift;
184     gid              = nlist->gid;
185     shiftvec         = fr->shift_vec[0];
186     fshift           = fr->fshift[0];
187     /* #if KERNEL_ELEC != 'None' */
188     facel            = _mm_set1_ps(fr->ic->epsfac);
189     charge           = mdatoms->chargeA;
190     /*     #if 'ReactionField' in KERNEL_ELEC */
191     krf              = _mm_set1_ps(fr->ic->k_rf);
192     krf2             = _mm_set1_ps(fr->ic->k_rf*2.0);
193     crf              = _mm_set1_ps(fr->ic->c_rf);
194     /*     #endif */
195     /* #endif */
196     /* #if KERNEL_VDW != 'None' */
197     nvdwtype         = fr->ntype;
198     vdwparam         = fr->nbfp;
199     vdwtype          = mdatoms->typeA;
200     /* #endif */
201     /* #if 'LJEwald' in KERNEL_VDW */
202     vdwgridparam     = fr->ljpme_c6grid;
203     sh_lj_ewald      = _mm_set1_ps(fr->ic->sh_lj_ewald);
204     ewclj            = _mm_set1_ps(fr->ic->ewaldcoeff_lj);
205     ewclj2           = _mm_mul_ps(minus_one,_mm_mul_ps(ewclj,ewclj));
206     /* #endif */
207
208     /* #if 'Table' in KERNEL_ELEC and 'Table' in KERNEL_VDW */
209     vftab            = kernel_data->table_elec_vdw->data;
210     vftabscale       = _mm_set1_ps(kernel_data->table_elec_vdw->scale);
211     /* #elif 'Table' in KERNEL_ELEC */
212     vftab            = kernel_data->table_elec->data;
213     vftabscale       = _mm_set1_ps(kernel_data->table_elec->scale);
214     /* #elif 'Table' in KERNEL_VDW */
215     vftab            = kernel_data->table_vdw->data;
216     vftabscale       = _mm_set1_ps(kernel_data->table_vdw->scale);
217     /* #endif */
218
219     /* #if 'Ewald' in KERNEL_ELEC */
220     sh_ewald         = _mm_set1_ps(fr->ic->sh_ewald);
221     /*     #if KERNEL_VF=='Force' and KERNEL_MOD_ELEC!='PotentialSwitch' */
222     ewtab            = fr->ic->tabq_coul_F;
223     ewtabscale       = _mm_set1_ps(fr->ic->tabq_scale);
224     ewtabhalfspace   = _mm_set1_ps(0.5/fr->ic->tabq_scale);
225     /*     #else */
226     ewtab            = fr->ic->tabq_coul_FDV0;
227     ewtabscale       = _mm_set1_ps(fr->ic->tabq_scale);
228     ewtabhalfspace   = _mm_set1_ps(0.5/fr->ic->tabq_scale);
229      /*     #endif */
230     /* #endif */
231
232     /* #if KERNEL_ELEC=='GeneralizedBorn' */
233     invsqrta         = fr->invsqrta;
234     dvda             = fr->dvda;
235     gbtabscale       = _mm_set1_ps(fr->gbtab->scale);
236     gbtab            = fr->gbtab->data;
237     gbinvepsdiff     = _mm_set1_ps((1.0/fr->ic->epsilon_r) - (1.0/fr->gb_epsilon_solvent));
238     /* #endif */
239
240     /* #if 'Water' in GEOMETRY_I */
241     /* Setup water-specific parameters */
242     inr              = nlist->iinr[0];
243     /*     #for I in PARTICLES_ELEC_I */
244     iq{I}              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+{I}]));
245     /*     #endfor */
246     /*     #for I in PARTICLES_VDW_I */
247     vdwioffset{I}      = 2*nvdwtype*vdwtype[inr+{I}];
248     /*     #endfor */
249     /* #endif */
250
251     /* #if 'Water' in GEOMETRY_J */
252     /*     #for J in PARTICLES_ELEC_J */
253     jq{J}              = _mm_set1_ps(charge[inr+{J}]);
254     /*     #endfor */
255     /*     #for J in PARTICLES_VDW_J */
256     vdwjidx{J}A        = 2*vdwtype[inr+{J}];
257     /*     #endfor */
258     /*     #for I,J in PAIRS_IJ */
259     /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
260     qq{I}{J}             = _mm_mul_ps(iq{I},jq{J});
261     /*         #endif */
262     /*         #if 'vdw' in INTERACTION_FLAGS[I][J] */
263     /*             #if 'LJEwald' in KERNEL_VDW */
264     c6_{I}{J}            = _mm_set1_ps(vdwparam[vdwioffset{I}+vdwjidx{J}A]);
265     c12_{I}{J}           = _mm_set1_ps(vdwparam[vdwioffset{I}+vdwjidx{J}A+1]);
266     c6grid_{I}{J}        = _mm_set1_ps(vdwgridparam[vdwioffset{I}+vdwjidx{J}A]);
267     /*             #else */
268     c6_{I}{J}            = _mm_set1_ps(vdwparam[vdwioffset{I}+vdwjidx{J}A]);
269     c12_{I}{J}           = _mm_set1_ps(vdwparam[vdwioffset{I}+vdwjidx{J}A+1]);
270     /*             #endif */
271     /*         #endif */
272     /*     #endfor */
273     /* #endif */
274
275     /* #if KERNEL_MOD_ELEC!='None' or KERNEL_MOD_VDW!='None' */
276     /*     #if KERNEL_ELEC!='None' */
277     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
278     rcutoff_scalar   = fr->ic->rcoulomb;
279     /*     #else */
280     rcutoff_scalar   = fr->ic->rvdw;
281     /*     #endif */
282     rcutoff          = _mm_set1_ps(rcutoff_scalar);
283     rcutoff2         = _mm_mul_ps(rcutoff,rcutoff);
284     /* #endif */
285
286     /* #if KERNEL_MOD_VDW=='PotentialShift' */
287     sh_vdw_invrcut6  = _mm_set1_ps(fr->ic->sh_invrc6);
288     rvdw             = _mm_set1_ps(fr->ic->rvdw);
289     /* #endif */
290
291     /* #if 'PotentialSwitch' in [KERNEL_MOD_ELEC,KERNEL_MOD_VDW] */
292     /*     #if KERNEL_MOD_ELEC=='PotentialSwitch'  */
293     rswitch_scalar   = fr->ic->rcoulomb_switch;
294     rswitch          = _mm_set1_ps(rswitch_scalar);
295     /*     #else */
296     rswitch_scalar   = fr->ic->rvdw_switch;
297     rswitch          = _mm_set1_ps(rswitch_scalar);
298     /*     #endif */
299     /* Setup switch parameters */
300     d_scalar         = rcutoff_scalar-rswitch_scalar;
301     d                = _mm_set1_ps(d_scalar);
302     swV3             = _mm_set1_ps(-10.0/(d_scalar*d_scalar*d_scalar));
303     swV4             = _mm_set1_ps( 15.0/(d_scalar*d_scalar*d_scalar*d_scalar));
304     swV5             = _mm_set1_ps( -6.0/(d_scalar*d_scalar*d_scalar*d_scalar*d_scalar));
305     /*     #if 'Force' in KERNEL_VF */
306     swF2             = _mm_set1_ps(-30.0/(d_scalar*d_scalar*d_scalar));
307     swF3             = _mm_set1_ps( 60.0/(d_scalar*d_scalar*d_scalar*d_scalar));
308     swF4             = _mm_set1_ps(-30.0/(d_scalar*d_scalar*d_scalar*d_scalar*d_scalar));
309     /*     #endif */
310     /* #endif */
311
312     /* Avoid stupid compiler warnings */
313     jnrA = jnrB = jnrC = jnrD = 0;
314     j_coord_offsetA = 0;
315     j_coord_offsetB = 0;
316     j_coord_offsetC = 0;
317     j_coord_offsetD = 0;
318
319     /* ## Keep track of the floating point operations we issue for reporting! */
320     /* #define OUTERFLOPS 0 */
321     outeriter        = 0;
322     inneriter        = 0;
323
324     for(iidx=0;iidx<4*DIM;iidx++)
325     {
326         scratch[iidx] = 0.0;
327     }
328
329     /* Start outer loop over neighborlists */
330     for(iidx=0; iidx<nri; iidx++)
331     {
332         /* Load shift vector for this list */
333         i_shift_offset   = DIM*shiftidx[iidx];
334
335         /* Load limits for loop over neighbors */
336         j_index_start    = jindex[iidx];
337         j_index_end      = jindex[iidx+1];
338
339         /* Get outer coordinate index */
340         inr              = iinr[iidx];
341         i_coord_offset   = DIM*inr;
342
343         /* Load i particle coords and add shift vector */
344         /* #if GEOMETRY_I == 'Particle' */
345         gmx_mm_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
346         /* #elif GEOMETRY_I == 'Water3' */
347         gmx_mm_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,
348                                                  &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2);
349         /* #elif GEOMETRY_I == 'Water4' */
350         /*     #if 0 in PARTICLES_I                 */
351         gmx_mm_load_shift_and_4rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,
352                                                  &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2,&ix3,&iy3,&iz3);
353         /*     #else                                */
354         gmx_mm_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset+DIM,
355                                                  &ix1,&iy1,&iz1,&ix2,&iy2,&iz2,&ix3,&iy3,&iz3);
356         /*     #endif                               */
357         /* #endif                                   */
358
359         /* #if 'Force' in KERNEL_VF */
360         /*     #for I in PARTICLES_I */
361         fix{I}             = _mm_setzero_ps();
362         fiy{I}             = _mm_setzero_ps();
363         fiz{I}             = _mm_setzero_ps();
364         /*     #endfor */
365         /* #endif */
366
367         /* ## For water we already preloaded parameters at the start of the kernel */
368         /* #if not 'Water' in GEOMETRY_I */
369         /* Load parameters for i particles */
370         /*     #for I in PARTICLES_ELEC_I */
371         iq{I}              = _mm_mul_ps(facel,_mm_load1_ps(charge+inr+{I}));
372         /*         #define OUTERFLOPS OUTERFLOPS+1 */
373         /*         #if KERNEL_ELEC=='GeneralizedBorn' */
374         isai{I}            = _mm_load1_ps(invsqrta+inr+{I});
375         /*         #endif */
376         /*     #endfor */
377         /*     #for I in PARTICLES_VDW_I */
378         vdwioffset{I}      = 2*nvdwtype*vdwtype[inr+{I}];
379         /*     #endfor */
380         /* #endif */
381
382         /* #if 'Potential' in KERNEL_VF */
383         /* Reset potential sums */
384         /*     #if KERNEL_ELEC != 'None' */
385         velecsum         = _mm_setzero_ps();
386         /*     #endif */
387         /*     #if 'GeneralizedBorn' in KERNEL_ELEC */
388         vgbsum           = _mm_setzero_ps();
389         /*     #endif */
390         /*     #if KERNEL_VDW != 'None' */
391         vvdwsum          = _mm_setzero_ps();
392         /*     #endif */
393         /* #endif */
394         /*     #if 'GeneralizedBorn' in KERNEL_ELEC and 'Force' in KERNEL_VF */
395         dvdasum          = _mm_setzero_ps();
396         /*     #endif */
397
398         /* #for ROUND in ['Loop','Epilogue'] */
399
400         /* #if ROUND =='Loop' */
401         /* Start inner kernel loop */
402         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
403         {
404         /* ## First round is normal loop (next statement resets indentation) */
405         /*     #if 0 */
406         }
407         /*     #endif */
408         /* #else */
409         if(jidx<j_index_end)
410         {
411         /* ## Second round is epilogue */
412         /* #endif */
413         /* #define INNERFLOPS 0 */
414
415             /* Get j neighbor index, and coordinate index */
416             /* #if ROUND =='Loop' */
417             jnrA             = jjnr[jidx];
418             jnrB             = jjnr[jidx+1];
419             jnrC             = jjnr[jidx+2];
420             jnrD             = jjnr[jidx+3];
421             /* #else */
422             jnrlistA         = jjnr[jidx];
423             jnrlistB         = jjnr[jidx+1];
424             jnrlistC         = jjnr[jidx+2];
425             jnrlistD         = jjnr[jidx+3];
426             /* Sign of each element will be negative for non-real atoms.
427              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
428              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
429              */
430             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
431             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
432             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
433             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
434             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
435             /* #endif */
436             j_coord_offsetA  = DIM*jnrA;
437             j_coord_offsetB  = DIM*jnrB;
438             j_coord_offsetC  = DIM*jnrC;
439             j_coord_offsetD  = DIM*jnrD;
440
441             /* load j atom coordinates */
442             /* #if GEOMETRY_J == 'Particle'             */
443             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
444                                               x+j_coord_offsetC,x+j_coord_offsetD,
445                                               &jx0,&jy0,&jz0);
446             /* #elif GEOMETRY_J == 'Water3'             */
447             gmx_mm_load_3rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
448                                               x+j_coord_offsetC,x+j_coord_offsetD,
449                                               &jx0,&jy0,&jz0,&jx1,&jy1,&jz1,&jx2,&jy2,&jz2);
450             /* #elif GEOMETRY_J == 'Water4'             */
451             /*     #if 0 in PARTICLES_J                 */
452             gmx_mm_load_4rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
453                                               x+j_coord_offsetC,x+j_coord_offsetD,
454                                               &jx0,&jy0,&jz0,&jx1,&jy1,&jz1,&jx2,
455                                               &jy2,&jz2,&jx3,&jy3,&jz3);
456             /*     #else                                */
457             gmx_mm_load_3rvec_4ptr_swizzle_ps(x+j_coord_offsetA+DIM,x+j_coord_offsetB+DIM,
458                                               x+j_coord_offsetC+DIM,x+j_coord_offsetD+DIM,
459                                               &jx1,&jy1,&jz1,&jx2,&jy2,&jz2,&jx3,&jy3,&jz3);
460             /*     #endif                               */
461             /* #endif                                   */
462
463             /* Calculate displacement vector */
464             /* #for I,J in PAIRS_IJ */
465             dx{I}{J}             = _mm_sub_ps(ix{I},jx{J});
466             dy{I}{J}             = _mm_sub_ps(iy{I},jy{J});
467             dz{I}{J}             = _mm_sub_ps(iz{I},jz{J});
468             /*     #define INNERFLOPS INNERFLOPS+3 */
469             /* #endfor */
470
471             /* Calculate squared distance and things based on it */
472             /* #for I,J in PAIRS_IJ */
473             rsq{I}{J}            = gmx_mm_calc_rsq_ps(dx{I}{J},dy{I}{J},dz{I}{J});
474             /*     #define INNERFLOPS INNERFLOPS+5 */
475             /* #endfor */
476
477             /* #for I,J in PAIRS_IJ */
478             /*     #if 'rinv' in INTERACTION_FLAGS[I][J] */
479             rinv{I}{J}           = sse41_invsqrt_f(rsq{I}{J});
480             /*         #define INNERFLOPS INNERFLOPS+5 */
481             /*     #endif */
482             /* #endfor */
483
484             /* #for I,J in PAIRS_IJ */
485             /*     #if 'rinvsq' in INTERACTION_FLAGS[I][J] */
486             /*         # if 'rinv' not in INTERACTION_FLAGS[I][J] */
487             rinvsq{I}{J}         = sse41_inv_f(rsq{I}{J});
488             /*             #define INNERFLOPS INNERFLOPS+4 */
489             /*         #else */
490             rinvsq{I}{J}         = _mm_mul_ps(rinv{I}{J},rinv{I}{J});
491             /*             #define INNERFLOPS INNERFLOPS+1 */
492             /*         #endif */
493             /*     #endif */
494             /* #endfor */
495
496             /* #if not 'Water' in GEOMETRY_J */
497             /* Load parameters for j particles */
498             /*     #for J in PARTICLES_ELEC_J */
499             jq{J}              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+{J},charge+jnrB+{J},
500                                                               charge+jnrC+{J},charge+jnrD+{J});
501             /*         #if KERNEL_ELEC=='GeneralizedBorn' */
502             isaj{J}            = gmx_mm_load_4real_swizzle_ps(invsqrta+jnrA+{J},invsqrta+jnrB+{J},
503                                                               invsqrta+jnrC+{J},invsqrta+jnrD+{J});
504             /*         #endif */
505             /*     #endfor */
506             /*     #for J in PARTICLES_VDW_J */
507             vdwjidx{J}A        = 2*vdwtype[jnrA+{J}];
508             vdwjidx{J}B        = 2*vdwtype[jnrB+{J}];
509             vdwjidx{J}C        = 2*vdwtype[jnrC+{J}];
510             vdwjidx{J}D        = 2*vdwtype[jnrD+{J}];
511             /*     #endfor */
512             /* #endif */
513
514             /* #if 'Force' in KERNEL_VF and not 'Particle' in GEOMETRY_I */
515             /*     #for J in PARTICLES_J */
516             fjx{J}             = _mm_setzero_ps();
517             fjy{J}             = _mm_setzero_ps();
518             fjz{J}             = _mm_setzero_ps();
519             /*     #endfor */
520             /* #endif */
521
522             /* #for I,J in PAIRS_IJ */
523
524             /**************************
525              * CALCULATE INTERACTIONS *
526              **************************/
527
528             /*     ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
529             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
530             /*         ## We always calculate rinv/rinvsq above to enable pipelineing in compilers (performance tested on x86) */
531             if (gmx_mm_any_lt(rsq{I}{J},rcutoff2))
532             {
533                 /*     #if 0    ## this and the next two lines is a hack to maintain auto-indentation in template file */
534             }
535             /*         #endif */
536             /*         #define INNERFLOPS INNERFLOPS+1 */
537             /*     #endif */
538
539             /*     #if 'r' in INTERACTION_FLAGS[I][J] */
540             r{I}{J}              = _mm_mul_ps(rsq{I}{J},rinv{I}{J});
541             /*         #if ROUND == 'Epilogue' */
542             r{I}{J}              = _mm_andnot_ps(dummy_mask,r{I}{J});
543             /*             #define INNERFLOPS INNERFLOPS+1 */
544             /*         #endif */
545             /*         #define INNERFLOPS INNERFLOPS+1 */
546             /*     #endif */
547
548             /*     ## For water geometries we already loaded parameters at the start of the kernel */
549             /*     #if not 'Water' in GEOMETRY_J */
550             /* Compute parameters for interactions between i and j atoms */
551             /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
552             qq{I}{J}             = _mm_mul_ps(iq{I},jq{J});
553             /*             #define INNERFLOPS INNERFLOPS+1 */
554             /*         #endif */
555             /*         #if 'vdw' in INTERACTION_FLAGS[I][J] */
556             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset{I}+vdwjidx{J}A,
557                                          vdwparam+vdwioffset{I}+vdwjidx{J}B,
558                                          vdwparam+vdwioffset{I}+vdwjidx{J}C,
559                                          vdwparam+vdwioffset{I}+vdwjidx{J}D,
560                                          &c6_{I}{J},&c12_{I}{J});
561
562             /*         #if 'LJEwald' in KERNEL_VDW */
563             c6grid_{I}{J}       = gmx_mm_load_4real_swizzle_ps(vdwgridparam+vdwioffset{I}+vdwjidx{J}A,
564                                                                vdwgridparam+vdwioffset{I}+vdwjidx{J}B,
565                                                                vdwgridparam+vdwioffset{I}+vdwjidx{J}C,
566                                                                vdwgridparam+vdwioffset{I}+vdwjidx{J}D);
567             /*          #endif */
568             /*         #endif */
569             /*     #endif */
570
571             /*     #if 'table' in INTERACTION_FLAGS[I][J] */
572             /* Calculate table index by multiplying r with table scale and truncate to integer */
573             rt               = _mm_mul_ps(r{I}{J},vftabscale);
574             vfitab           = _mm_cvttps_epi32(rt);
575             vfeps            = _mm_sub_ps(rt,_mm_round_ps(rt, _MM_FROUND_FLOOR));
576             /*         #define INNERFLOPS INNERFLOPS+4                          */
577             /*         #if 'Table' in KERNEL_ELEC and 'Table' in KERNEL_VDW     */
578             /*             ## 3 tables, 4 bytes per point: multiply index by 12 */
579             vfitab           = _mm_slli_epi32(_mm_add_epi32(vfitab,_mm_slli_epi32(vfitab,1)),2);
580             /*         #elif 'Table' in KERNEL_ELEC                             */
581             /*             ## 1 table, 4 bytes per point: multiply index by 4   */
582             vfitab           = _mm_slli_epi32(vfitab,2);
583             /*         #elif 'Table' in KERNEL_VDW                              */
584             /*             ## 2 tables, 4 bytes per point: multiply index by 8  */
585             vfitab           = _mm_slli_epi32(vfitab,3);
586             /*         #endif                                                   */
587             /*     #endif */
588
589             /*     ## ELECTROSTATIC INTERACTIONS */
590             /*     #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
591
592             /*         #if KERNEL_ELEC=='Coulomb' */
593
594             /* COULOMB ELECTROSTATICS */
595             velec            = _mm_mul_ps(qq{I}{J},rinv{I}{J});
596             /*             #define INNERFLOPS INNERFLOPS+1 */
597             /*             #if 'Force' in KERNEL_VF */
598             felec            = _mm_mul_ps(velec,rinvsq{I}{J});
599             /*                 #define INNERFLOPS INNERFLOPS+2 */
600             /*             #endif */
601
602             /*         #elif KERNEL_ELEC=='ReactionField' */
603
604             /* REACTION-FIELD ELECTROSTATICS */
605             /*             #if 'Potential' in KERNEL_VF */
606             velec            = _mm_mul_ps(qq{I}{J},_mm_sub_ps(_mm_add_ps(rinv{I}{J},_mm_mul_ps(krf,rsq{I}{J})),crf));
607             /*                 #define INNERFLOPS INNERFLOPS+4 */
608             /*             #endif */
609             /*             #if 'Force' in KERNEL_VF */
610             felec            = _mm_mul_ps(qq{I}{J},_mm_sub_ps(_mm_mul_ps(rinv{I}{J},rinvsq{I}{J}),krf2));
611             /*                 #define INNERFLOPS INNERFLOPS+3 */
612             /*             #endif */
613
614             /*         #elif KERNEL_ELEC=='GeneralizedBorn' */
615
616             /* GENERALIZED BORN AND COULOMB ELECTROSTATICS */
617             isaprod          = _mm_mul_ps(isai{I},isaj{J});
618             gbqqfactor       = _mm_xor_ps(signbit,_mm_mul_ps(qq{I}{J},_mm_mul_ps(isaprod,gbinvepsdiff)));
619             gbscale          = _mm_mul_ps(isaprod,gbtabscale);
620             /*             #define INNERFLOPS INNERFLOPS+5 */
621
622             /* Calculate generalized born table index - this is a separate table from the normal one,
623              * but we use the same procedure by multiplying r with scale and truncating to integer.
624              */
625             rt               = _mm_mul_ps(r{I}{J},gbscale);
626             gbitab           = _mm_cvttps_epi32(rt);
627             gbeps            = _mm_sub_ps(rt,_mm_round_ps(rt, _MM_FROUND_FLOOR));
628             gbitab           = _mm_slli_epi32(gbitab,2);
629             Y                = _mm_load_ps( gbtab + gmx_mm_extract_epi32(gbitab,0) );
630             F                = _mm_load_ps( gbtab + gmx_mm_extract_epi32(gbitab,1) );
631             G                = _mm_load_ps( gbtab + gmx_mm_extract_epi32(gbitab,2) );
632             H                = _mm_load_ps( gbtab + gmx_mm_extract_epi32(gbitab,3) );
633             _MM_TRANSPOSE4_PS(Y,F,G,H);
634             Heps             = _mm_mul_ps(gbeps,H);
635             Fp               = _mm_add_ps(F,_mm_mul_ps(gbeps,_mm_add_ps(G,Heps)));
636             VV               = _mm_add_ps(Y,_mm_mul_ps(gbeps,Fp));
637             vgb              = _mm_mul_ps(gbqqfactor,VV);
638             /*             #define INNERFLOPS INNERFLOPS+10 */
639
640             /*             #if 'Force' in KERNEL_VF */
641             FF               = _mm_add_ps(Fp,_mm_mul_ps(gbeps,_mm_add_ps(G,_mm_add_ps(Heps,Heps))));
642             fgb              = _mm_mul_ps(gbqqfactor,_mm_mul_ps(FF,gbscale));
643             dvdatmp          = _mm_mul_ps(minushalf,_mm_add_ps(vgb,_mm_mul_ps(fgb,r{I}{J})));
644             /*                 #if ROUND == 'Epilogue' */
645             dvdatmp          = _mm_andnot_ps(dummy_mask,dvdatmp);
646             /*                 #endif */
647             dvdasum          = _mm_add_ps(dvdasum,dvdatmp);
648             /*                 #if ROUND == 'Loop' */
649             fjptrA           = dvda+jnrA;
650             fjptrB           = dvda+jnrB;
651             fjptrC           = dvda+jnrC;
652             fjptrD           = dvda+jnrD;
653             /*                 #else */
654             /* The pointers to scratch make sure that this code with compilers that take gmx_restrict seriously (e.g. icc 13) really can't screw things up. */
655             fjptrA             = (jnrlistA>=0) ? dvda+jnrA : scratch;
656             fjptrB             = (jnrlistB>=0) ? dvda+jnrB : scratch;
657             fjptrC             = (jnrlistC>=0) ? dvda+jnrC : scratch;
658             fjptrD             = (jnrlistD>=0) ? dvda+jnrD : scratch;
659             /*                 #endif */
660             gmx_mm_increment_4real_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,_mm_mul_ps(dvdatmp,_mm_mul_ps(isaj{J},isaj{J})));
661             /*                 #define INNERFLOPS INNERFLOPS+13 */
662             /*             #endif */
663             velec            = _mm_mul_ps(qq{I}{J},rinv{I}{J});
664             /*                 #define INNERFLOPS INNERFLOPS+1 */
665             /*             #if 'Force' in KERNEL_VF */
666             felec            = _mm_mul_ps(_mm_sub_ps(_mm_mul_ps(velec,rinv{I}{J}),fgb),rinv{I}{J});
667             /*                 #define INNERFLOPS INNERFLOPS+3 */
668             /*             #endif */
669
670             /*         #elif KERNEL_ELEC=='Ewald' */
671             /* EWALD ELECTROSTATICS */
672
673             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
674             ewrt             = _mm_mul_ps(r{I}{J},ewtabscale);
675             ewitab           = _mm_cvttps_epi32(ewrt);
676             eweps            = _mm_sub_ps(ewrt,_mm_round_ps(ewrt, _MM_FROUND_FLOOR));
677             /*             #define INNERFLOPS INNERFLOPS+4 */
678             /*             #if 'Potential' in KERNEL_VF or KERNEL_MOD_ELEC=='PotentialSwitch' */
679             ewitab           = _mm_slli_epi32(ewitab,2);
680             ewtabF           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,0) );
681             ewtabD           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,1) );
682             ewtabV           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,2) );
683             ewtabFn          = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,3) );
684             _MM_TRANSPOSE4_PS(ewtabF,ewtabD,ewtabV,ewtabFn);
685             felec            = _mm_add_ps(ewtabF,_mm_mul_ps(eweps,ewtabD));
686             /*                 #define INNERFLOPS INNERFLOPS+2 */
687             /*                 #if KERNEL_MOD_ELEC=='PotentialShift' */
688             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
689             velec            = _mm_mul_ps(qq{I}{J},_mm_sub_ps(_mm_sub_ps(rinv{I}{J},sh_ewald),velec));
690             /*                     #define INNERFLOPS INNERFLOPS+7 */
691             /*                 #else */
692             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
693             velec            = _mm_mul_ps(qq{I}{J},_mm_sub_ps(rinv{I}{J},velec));
694             /*                     #define INNERFLOPS INNERFLOPS+6 */
695             /*                 #endif */
696             /*                 #if 'Force' in KERNEL_VF */
697             felec            = _mm_mul_ps(_mm_mul_ps(qq{I}{J},rinv{I}{J}),_mm_sub_ps(rinvsq{I}{J},felec));
698             /*                      #define INNERFLOPS INNERFLOPS+3 */
699             /*                 #endif */
700             /*             #elif KERNEL_VF=='Force' */
701             gmx_mm_load_4pair_swizzle_ps(ewtab + gmx_mm_extract_epi32(ewitab,0),ewtab + gmx_mm_extract_epi32(ewitab,1),
702                                          ewtab + gmx_mm_extract_epi32(ewitab,2),ewtab + gmx_mm_extract_epi32(ewitab,3),
703                                          &ewtabF,&ewtabFn);
704             felec            = _mm_add_ps(_mm_mul_ps( _mm_sub_ps(one,eweps),ewtabF),_mm_mul_ps(eweps,ewtabFn));
705             felec            = _mm_mul_ps(_mm_mul_ps(qq{I}{J},rinv{I}{J}),_mm_sub_ps(rinvsq{I}{J},felec));
706             /*                 #define INNERFLOPS INNERFLOPS+7 */
707             /*             #endif */
708
709             /*         #elif KERNEL_ELEC=='CubicSplineTable' */
710
711             /* CUBIC SPLINE TABLE ELECTROSTATICS */
712             Y                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,0) );
713             F                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,1) );
714             G                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,2) );
715             H                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,3) );
716             _MM_TRANSPOSE4_PS(Y,F,G,H);
717             Heps             = _mm_mul_ps(vfeps,H);
718             Fp               = _mm_add_ps(F,_mm_mul_ps(vfeps,_mm_add_ps(G,Heps)));
719             /*             #define INNERFLOPS INNERFLOPS+4 */
720             /*             #if 'Potential' in KERNEL_VF */
721             VV               = _mm_add_ps(Y,_mm_mul_ps(vfeps,Fp));
722             velec            = _mm_mul_ps(qq{I}{J},VV);
723             /*                 #define INNERFLOPS INNERFLOPS+3 */
724             /*             #endif */
725             /*             #if 'Force' in KERNEL_VF */
726             FF               = _mm_add_ps(Fp,_mm_mul_ps(vfeps,_mm_add_ps(G,_mm_add_ps(Heps,Heps))));
727             felec            = _mm_xor_ps(signbit,_mm_mul_ps(_mm_mul_ps(qq{I}{J},FF),_mm_mul_ps(vftabscale,rinv{I}{J})));
728             /*                 #define INNERFLOPS INNERFLOPS+7 */
729             /*             #endif */
730             /*         #endif */
731             /*         ## End of check for electrostatics interaction forms */
732             /*     #endif */
733             /*     ## END OF ELECTROSTATIC INTERACTION CHECK FOR PAIR I-J */
734
735             /*     #if 'vdw' in INTERACTION_FLAGS[I][J] */
736
737             /*         #if KERNEL_VDW=='LennardJones' */
738
739             /* LENNARD-JONES DISPERSION/REPULSION */
740
741             rinvsix          = _mm_mul_ps(_mm_mul_ps(rinvsq{I}{J},rinvsq{I}{J}),rinvsq{I}{J});
742             /*             #define INNERFLOPS INNERFLOPS+2 */
743             /*             #if 'Potential' in KERNEL_VF or KERNEL_MOD_VDW=='PotentialSwitch' */
744             vvdw6            = _mm_mul_ps(c6_{I}{J},rinvsix);
745             vvdw12           = _mm_mul_ps(c12_{I}{J},_mm_mul_ps(rinvsix,rinvsix));
746             /*                 #define INNERFLOPS INNERFLOPS+3 */
747             /*                 #if KERNEL_MOD_VDW=='PotentialShift' */
748             vvdw             = _mm_sub_ps(_mm_mul_ps( _mm_sub_ps(vvdw12 , _mm_mul_ps(c12_{I}{J},_mm_mul_ps(sh_vdw_invrcut6,sh_vdw_invrcut6))), one_twelfth) ,
749                                           _mm_mul_ps( _mm_sub_ps(vvdw6,_mm_mul_ps(c6_{I}{J},sh_vdw_invrcut6)),one_sixth));
750             /*                     #define INNERFLOPS INNERFLOPS+8 */
751             /*                 #else */
752             vvdw             = _mm_sub_ps( _mm_mul_ps(vvdw12,one_twelfth) , _mm_mul_ps(vvdw6,one_sixth) );
753             /*                     #define INNERFLOPS INNERFLOPS+3 */
754             /*                 #endif */
755             /*                 ## Check for force inside potential check, i.e. this means we already did the potential part */
756             /*                 #if 'Force' in KERNEL_VF */
757             fvdw             = _mm_mul_ps(_mm_sub_ps(vvdw12,vvdw6),rinvsq{I}{J});
758             /*                     #define INNERFLOPS INNERFLOPS+2 */
759             /*                 #endif */
760             /*             #elif KERNEL_VF=='Force' */
761             /*                 ## Force-only LennardJones makes it possible to save 1 flop (they do add up...) */
762             fvdw             = _mm_mul_ps(_mm_sub_ps(_mm_mul_ps(c12_{I}{J},rinvsix),c6_{I}{J}),_mm_mul_ps(rinvsix,rinvsq{I}{J}));
763             /*                 #define INNERFLOPS INNERFLOPS+4 */
764             /*             #endif */
765
766             /*         #elif KERNEL_VDW=='CubicSplineTable' */
767
768             /* CUBIC SPLINE TABLE DISPERSION */
769             /*             #if 'Table' in KERNEL_ELEC */
770             vfitab           = _mm_add_epi32(vfitab,ifour);
771             /*             #endif                     */
772             Y                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,0) );
773             F                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,1) );
774             G                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,2) );
775             H                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,3) );
776             _MM_TRANSPOSE4_PS(Y,F,G,H);
777             Heps             = _mm_mul_ps(vfeps,H);
778             Fp               = _mm_add_ps(F,_mm_mul_ps(vfeps,_mm_add_ps(G,Heps)));
779             /*             #define INNERFLOPS INNERFLOPS+4 */
780             /*             #if 'Potential' in KERNEL_VF */
781             VV               = _mm_add_ps(Y,_mm_mul_ps(vfeps,Fp));
782             vvdw6            = _mm_mul_ps(c6_{I}{J},VV);
783             /*                 #define INNERFLOPS INNERFLOPS+3 */
784             /*             #endif */
785             /*             #if 'Force' in KERNEL_VF */
786             FF               = _mm_add_ps(Fp,_mm_mul_ps(vfeps,_mm_add_ps(G,_mm_add_ps(Heps,Heps))));
787             fvdw6            = _mm_mul_ps(c6_{I}{J},FF);
788             /*                 #define INNERFLOPS INNERFLOPS+4 */
789             /*             #endif */
790
791             /* CUBIC SPLINE TABLE REPULSION */
792             vfitab           = _mm_add_epi32(vfitab,ifour);
793             Y                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,0) );
794             F                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,1) );
795             G                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,2) );
796             H                = _mm_load_ps( vftab + gmx_mm_extract_epi32(vfitab,3) );
797             _MM_TRANSPOSE4_PS(Y,F,G,H);
798             Heps             = _mm_mul_ps(vfeps,H);
799             Fp               = _mm_add_ps(F,_mm_mul_ps(vfeps,_mm_add_ps(G,Heps)));
800             /*             #define INNERFLOPS INNERFLOPS+4 */
801             /*             #if 'Potential' in KERNEL_VF */
802             VV               = _mm_add_ps(Y,_mm_mul_ps(vfeps,Fp));
803             vvdw12           = _mm_mul_ps(c12_{I}{J},VV);
804             /*                 #define INNERFLOPS INNERFLOPS+3 */
805             /*             #endif */
806             /*             #if 'Force' in KERNEL_VF */
807             FF               = _mm_add_ps(Fp,_mm_mul_ps(vfeps,_mm_add_ps(G,_mm_add_ps(Heps,Heps))));
808             fvdw12           = _mm_mul_ps(c12_{I}{J},FF);
809             /*                 #define INNERFLOPS INNERFLOPS+5 */
810             /*             #endif */
811             /*             #if 'Potential' in KERNEL_VF */
812             vvdw             = _mm_add_ps(vvdw12,vvdw6);
813             /*                 #define INNERFLOPS INNERFLOPS+1 */
814             /*             #endif */
815             /*             #if 'Force' in KERNEL_VF */
816             fvdw             = _mm_xor_ps(signbit,_mm_mul_ps(_mm_add_ps(fvdw6,fvdw12),_mm_mul_ps(vftabscale,rinv{I}{J})));
817             /*                 #define INNERFLOPS INNERFLOPS+4 */
818             /*             #endif */
819
820             /*         #elif KERNEL_VDW=='LJEwald' */
821
822             /* Analytical LJ-PME */
823             rinvsix          = _mm_mul_ps(_mm_mul_ps(rinvsq{I}{J},rinvsq{I}{J}),rinvsq{I}{J});
824             ewcljrsq         = _mm_mul_ps(ewclj2,rsq{I}{J});
825             ewclj6           = _mm_mul_ps(ewclj2,_mm_mul_ps(ewclj2,ewclj2));
826             exponent         = sse41_exp_f(ewcljrsq);
827             /* poly = exp(-(beta*r)^2) * (1 + (beta*r)^2 + (beta*r)^4 /2) */
828             poly             = _mm_mul_ps(exponent,_mm_add_ps(_mm_sub_ps(one,ewcljrsq),_mm_mul_ps(_mm_mul_ps(ewcljrsq,ewcljrsq),one_half)));
829             /*             #define INNERFLOPS INNERFLOPS+11 */
830             /*             #if 'Potential' in KERNEL_VF or KERNEL_MOD_VDW=='PotentialSwitch' */
831             /* vvdw6 = [C6 - C6grid * (1-poly)]/r6 */
832             vvdw6            = _mm_mul_ps(_mm_sub_ps(c6_{I}{J},_mm_mul_ps(c6grid_{I}{J},_mm_sub_ps(one,poly))),rinvsix);
833             vvdw12           = _mm_mul_ps(c12_{I}{J},_mm_mul_ps(rinvsix,rinvsix));
834             /*                 #define INNERFLOPS INNERFLOPS+6 */
835             /*                 #if KERNEL_MOD_VDW=='PotentialShift' */
836             vvdw             = _mm_sub_ps(_mm_mul_ps( _mm_sub_ps(vvdw12 , _mm_mul_ps(c12_{I}{J},_mm_mul_ps(sh_vdw_invrcut6,sh_vdw_invrcut6))),one_twelfth),
837                                _mm_mul_ps( _mm_sub_ps(vvdw6,_mm_add_ps(_mm_mul_ps(c6_{I}{J},sh_vdw_invrcut6),_mm_mul_ps(c6grid_{I}{J},sh_lj_ewald))),one_sixth));
838             /*                 #define INNERFLOPS INNERFLOPS+10 */
839             /*                 #else */
840             vvdw             = _mm_sub_ps(_mm_mul_ps(vvdw12,one_twelfth),_mm_mul_ps(vvdw6,one_sixth));
841             /*                 #define INNERFLOPS INNERFLOPS+3 */
842             /*                 #endif */
843             /*                 ## Check for force inside potential check, i.e. this means we already did the potential part */
844             /*                 #if 'Force' in KERNEL_VF */
845             /* fvdw = vvdw12/r - (vvdw6/r + (C6grid * exponent * beta^6)/r) */
846             fvdw             = _mm_mul_ps(_mm_sub_ps(vvdw12,_mm_sub_ps(vvdw6,_mm_mul_ps(_mm_mul_ps(c6grid_{I}{J},one_sixth),_mm_mul_ps(exponent,ewclj6)))),rinvsq{I}{J});
847             /*                 #define INNERFLOPS INNERFLOPS+6 */
848             /*                 #endif */
849             /*             #elif KERNEL_VF=='Force' */
850             /* f6A = 6 * C6grid * (1 - poly) */
851             f6A              = _mm_mul_ps(c6grid_{I}{J},_mm_sub_ps(one,poly));
852             /* f6B = C6grid * exponent * beta^6 */
853             f6B              = _mm_mul_ps(_mm_mul_ps(c6grid_{I}{J},one_sixth),_mm_mul_ps(exponent,ewclj6));
854             /* fvdw = 12*C12/r13 - ((6*C6 - f6A)/r6 + f6B)/r */
855             fvdw              = _mm_mul_ps(_mm_add_ps(_mm_mul_ps(_mm_sub_ps(_mm_mul_ps(c12_{I}{J},rinvsix),_mm_sub_ps(c6_{I}{J},f6A)),rinvsix),f6B),rinvsq{I}{J});
856             /*                 #define INNERFLOPS INNERFLOPS+11 */
857             /*             #endif */
858             /*         #endif */
859             /*         ## End of check for vdw interaction forms */
860             /*     #endif */
861             /*     ## END OF VDW INTERACTION CHECK FOR PAIR I-J */
862
863             /*     #if 'switch' in INTERACTION_FLAGS[I][J] */
864             d                = _mm_sub_ps(r{I}{J},rswitch);
865             d                = _mm_max_ps(d,_mm_setzero_ps());
866             d2               = _mm_mul_ps(d,d);
867             sw               = _mm_add_ps(one,_mm_mul_ps(d2,_mm_mul_ps(d,_mm_add_ps(swV3,_mm_mul_ps(d,_mm_add_ps(swV4,_mm_mul_ps(d,swV5)))))));
868             /*         #define INNERFLOPS INNERFLOPS+10 */
869
870             /*         #if 'Force' in KERNEL_VF */
871             dsw              = _mm_mul_ps(d2,_mm_add_ps(swF2,_mm_mul_ps(d,_mm_add_ps(swF3,_mm_mul_ps(d,swF4)))));
872             /*             #define INNERFLOPS INNERFLOPS+5 */
873             /*         #endif */
874
875             /* Evaluate switch function */
876             /*         #if 'Force' in KERNEL_VF */
877             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
878             /*             #if 'electrostatics' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_ELEC=='PotentialSwitch' */
879             felec            = _mm_sub_ps( _mm_mul_ps(felec,sw) , _mm_mul_ps(rinv{I}{J},_mm_mul_ps(velec,dsw)) );
880             /*                 #define INNERFLOPS INNERFLOPS+4 */
881             /*             #endif */
882             /*             #if 'vdw' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_VDW=='PotentialSwitch' */
883             fvdw             = _mm_sub_ps( _mm_mul_ps(fvdw,sw) , _mm_mul_ps(rinv{I}{J},_mm_mul_ps(vvdw,dsw)) );
884             /*                 #define INNERFLOPS INNERFLOPS+4 */
885             /*             #endif */
886             /*         #endif */
887             /*         #if 'Potential' in KERNEL_VF */
888             /*             #if 'electrostatics' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_ELEC=='PotentialSwitch' */
889             velec            = _mm_mul_ps(velec,sw);
890             /*                 #define INNERFLOPS INNERFLOPS+1 */
891             /*             #endif */
892             /*             #if 'vdw' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_VDW=='PotentialSwitch' */
893             vvdw             = _mm_mul_ps(vvdw,sw);
894             /*                 #define INNERFLOPS INNERFLOPS+1 */
895             /*             #endif */
896             /*         #endif */
897             /*     #endif */
898             /*     ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
899             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
900             cutoff_mask      = _mm_cmplt_ps(rsq{I}{J},rcutoff2);
901             /*         #define INNERFLOPS INNERFLOPS+1 */
902             /*     #endif */
903
904             /*     #if 'Potential' in KERNEL_VF */
905             /* Update potential sum for this i atom from the interaction with this j atom. */
906             /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
907             /*             #if 'exactcutoff' in INTERACTION_FLAGS[I][J] */
908             velec            = _mm_and_ps(velec,cutoff_mask);
909             /*                 #define INNERFLOPS INNERFLOPS+1 */
910             /*             #endif                                       */
911             /*             #if ROUND == 'Epilogue' */
912             velec            = _mm_andnot_ps(dummy_mask,velec);
913             /*             #endif */
914             velecsum         = _mm_add_ps(velecsum,velec);
915             /*             #define INNERFLOPS INNERFLOPS+1 */
916             /*             #if KERNEL_ELEC=='GeneralizedBorn' */
917             /*             #if 'exactcutoff' in INTERACTION_FLAGS[I][J] */
918             vgb              = _mm_and_ps(vgb,cutoff_mask);
919             /*                 #define INNERFLOPS INNERFLOPS+1 */
920             /*             #endif                                       */
921             /*             #if ROUND == 'Epilogue' */
922             vgb              = _mm_andnot_ps(dummy_mask,vgb);
923             /*             #endif */
924             vgbsum           = _mm_add_ps(vgbsum,vgb);
925             /*                 #define INNERFLOPS INNERFLOPS+1 */
926             /*             #endif */
927             /*         #endif */
928             /*         #if 'vdw' in INTERACTION_FLAGS[I][J] */
929             /*     ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
930             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
931             vvdw             = _mm_and_ps(vvdw,cutoff_mask);
932             /*                 #define INNERFLOPS INNERFLOPS+1 */
933             /*             #endif                                       */
934             /*             #if ROUND == 'Epilogue' */
935             vvdw             = _mm_andnot_ps(dummy_mask,vvdw);
936             /*             #endif */
937             vvdwsum          = _mm_add_ps(vvdwsum,vvdw);
938             /*             #define INNERFLOPS INNERFLOPS+1 */
939             /*         #endif */
940             /*     #endif */
941
942             /*     #if 'Force' in KERNEL_VF */
943
944             /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] and 'vdw' in INTERACTION_FLAGS[I][J] */
945             fscal            = _mm_add_ps(felec,fvdw);
946             /*             #define INNERFLOPS INNERFLOPS+1 */
947             /*         #elif 'electrostatics' in INTERACTION_FLAGS[I][J] */
948             fscal            = felec;
949             /*         #elif 'vdw' in INTERACTION_FLAGS[I][J] */
950             fscal            = fvdw;
951             /*        #endif */
952
953             /*     ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
954             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
955             fscal            = _mm_and_ps(fscal,cutoff_mask);
956             /*                 #define INNERFLOPS INNERFLOPS+1 */
957             /*             #endif                                       */
958
959             /*             #if ROUND == 'Epilogue' */
960             fscal            = _mm_andnot_ps(dummy_mask,fscal);
961             /*             #endif */
962
963             /* Calculate temporary vectorial force */
964             tx               = _mm_mul_ps(fscal,dx{I}{J});
965             ty               = _mm_mul_ps(fscal,dy{I}{J});
966             tz               = _mm_mul_ps(fscal,dz{I}{J});
967
968             /* Update vectorial force */
969             fix{I}             = _mm_add_ps(fix{I},tx);
970             fiy{I}             = _mm_add_ps(fiy{I},ty);
971             fiz{I}             = _mm_add_ps(fiz{I},tz);
972             /*             #define INNERFLOPS INNERFLOPS+6 */
973
974             /* #if GEOMETRY_I == 'Particle'             */
975             /*     #if ROUND == 'Loop' */
976             fjptrA             = f+j_coord_offsetA;
977             fjptrB             = f+j_coord_offsetB;
978             fjptrC             = f+j_coord_offsetC;
979             fjptrD             = f+j_coord_offsetD;
980             /*     #else */
981             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
982             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
983             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
984             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
985             /*     #endif */
986             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
987             /*     #define INNERFLOPS INNERFLOPS+3      */
988             /* #else                                    */
989             fjx{J}             = _mm_add_ps(fjx{J},tx);
990             fjy{J}             = _mm_add_ps(fjy{J},ty);
991             fjz{J}             = _mm_add_ps(fjz{J},tz);
992             /*     #define INNERFLOPS INNERFLOPS+3      */
993             /* #endif                                   */
994
995             /*     #endif */
996
997             /*     ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
998             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
999             /*         #if 0    ## This and next two lines is a hack to maintain indentation in template file */
1000             {
1001                 /*     #endif */
1002             }
1003             /*     #endif */
1004             /*    ## End of check for the interaction being outside the cutoff */
1005
1006             /* #endfor */
1007             /* ## End of loop over i-j interaction pairs */
1008
1009             /* #if GEOMETRY_I != 'Particle' */
1010             /*     #if ROUND == 'Loop' */
1011             fjptrA             = f+j_coord_offsetA;
1012             fjptrB             = f+j_coord_offsetB;
1013             fjptrC             = f+j_coord_offsetC;
1014             fjptrD             = f+j_coord_offsetD;
1015             /*     #else */
1016             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
1017             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
1018             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
1019             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
1020             /*     #endif */
1021             /* #endif */
1022
1023             /* #if 'Water' in GEOMETRY_I and GEOMETRY_J == 'Particle' */
1024             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
1025             /* #elif GEOMETRY_J == 'Water3'               */
1026             gmx_mm_decrement_3rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,
1027                                                    fjx0,fjy0,fjz0,fjx1,fjy1,fjz1,fjx2,fjy2,fjz2);
1028             /*     #define INNERFLOPS INNERFLOPS+9      */
1029             /* #elif GEOMETRY_J == 'Water4'             */
1030             /*     #if 0 in PARTICLES_J                 */
1031             gmx_mm_decrement_4rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,
1032                                                    fjx0,fjy0,fjz0,fjx1,fjy1,fjz1,
1033                                                    fjx2,fjy2,fjz2,fjx3,fjy3,fjz3);
1034             /*     #define INNERFLOPS INNERFLOPS+12     */
1035             /*     #else                                */
1036             gmx_mm_decrement_3rvec_4ptr_swizzle_ps(fjptrA+DIM,fjptrB+DIM,fjptrC+DIM,fjptrD+DIM,
1037                                                    fjx1,fjy1,fjz1,fjx2,fjy2,fjz2,fjx3,fjy3,fjz3);
1038             /*     #define INNERFLOPS INNERFLOPS+9      */
1039             /*     #endif                               */
1040             /* #endif                                   */
1041
1042             /* Inner loop uses {INNERFLOPS} flops */
1043         }
1044
1045         /* #endfor */
1046
1047         /* End of innermost loop */
1048
1049         /* #if 'Force' in KERNEL_VF */
1050         /*     #if GEOMETRY_I == 'Particle'            */
1051         gmx_mm_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
1052                                               f+i_coord_offset,fshift+i_shift_offset);
1053         /*         #define OUTERFLOPS OUTERFLOPS+6     */
1054         /*     #elif GEOMETRY_I == 'Water3'            */
1055         gmx_mm_update_iforce_3atom_swizzle_ps(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,
1056                                               f+i_coord_offset,fshift+i_shift_offset);
1057         /*         #define OUTERFLOPS OUTERFLOPS+18    */
1058         /*     #elif GEOMETRY_I == 'Water4'            */
1059         /*         #if 0 in PARTICLES_I                */
1060         gmx_mm_update_iforce_4atom_swizzle_ps(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,fix3,fiy3,fiz3,
1061                                               f+i_coord_offset,fshift+i_shift_offset);
1062         /*             #define OUTERFLOPS OUTERFLOPS+24    */
1063         /*         #else                               */
1064         gmx_mm_update_iforce_3atom_swizzle_ps(fix1,fiy1,fiz1,fix2,fiy2,fiz2,fix3,fiy3,fiz3,
1065                                               f+i_coord_offset+DIM,fshift+i_shift_offset);
1066         /*             #define OUTERFLOPS OUTERFLOPS+18    */
1067         /*         #endif                              */
1068         /*     #endif                                  */
1069         /* #endif                                      */
1070
1071         /* #if 'Potential' in KERNEL_VF */
1072         ggid                        = gid[iidx];
1073         /* Update potential energies */
1074         /*     #if KERNEL_ELEC != 'None' */
1075         gmx_mm_update_1pot_ps(velecsum,kernel_data->energygrp_elec+ggid);
1076         /*         #define OUTERFLOPS OUTERFLOPS+1 */
1077         /*     #endif */
1078         /*     #if 'GeneralizedBorn' in KERNEL_ELEC */
1079         gmx_mm_update_1pot_ps(vgbsum,kernel_data->energygrp_polarization+ggid);
1080         /*         #define OUTERFLOPS OUTERFLOPS+1 */
1081         /*     #endif */
1082         /*     #if KERNEL_VDW != 'None' */
1083         gmx_mm_update_1pot_ps(vvdwsum,kernel_data->energygrp_vdw+ggid);
1084         /*         #define OUTERFLOPS OUTERFLOPS+1 */
1085         /*     #endif */
1086         /* #endif */
1087         /*     #if 'GeneralizedBorn' in KERNEL_ELEC and 'Force' in KERNEL_VF */
1088         dvdasum = _mm_mul_ps(dvdasum, _mm_mul_ps(isai{I},isai{I}));
1089         gmx_mm_update_1pot_ps(dvdasum,dvda+inr);
1090         /*     #endif */
1091
1092         /* Increment number of inner iterations */
1093         inneriter                  += j_index_end - j_index_start;
1094
1095         /* Outer loop uses {OUTERFLOPS} flops */
1096     }
1097
1098     /* Increment number of outer iterations */
1099     outeriter        += nri;
1100
1101     /* Update outer/inner flops */
1102     /* ## NB: This is not important, it just affects the flopcount. However, since our preprocessor is */
1103     /* ## primitive and replaces aggressively even in strings inside these directives, we need to      */
1104     /* ## assemble the main part of the name (containing KERNEL/ELEC/VDW) directly in the source.      */
1105     /* #if GEOMETRY_I == 'Water3'            */
1106     /*     #define ISUFFIX '_W3'             */
1107     /* #elif GEOMETRY_I == 'Water4'          */
1108     /*     #define ISUFFIX '_W4'             */
1109     /* #else                                 */
1110     /*     #define ISUFFIX ''                */
1111     /* #endif                                */
1112     /* #if GEOMETRY_J == 'Water3'            */
1113     /*     #define JSUFFIX 'W3'              */
1114     /* #elif GEOMETRY_J == 'Water4'          */
1115     /*     #define JSUFFIX 'W4'              */
1116     /* #else                                 */
1117     /*     #define JSUFFIX ''                */
1118     /* #endif                                */
1119     /* #if 'PotentialAndForce' in KERNEL_VF  */
1120     /*     #define VFSUFFIX  '_VF'           */
1121     /* #elif 'Potential' in KERNEL_VF        */
1122     /*     #define VFSUFFIX '_V'             */
1123     /* #else                                 */
1124     /*     #define VFSUFFIX '_F'             */
1125     /* #endif                                */
1126
1127     /* #if KERNEL_ELEC != 'None' and KERNEL_VDW != 'None' */
1128     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
1129     /* #elif KERNEL_ELEC != 'None' */
1130     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
1131     /* #else */
1132     inc_nrnb(nrnb,eNR_NBKERNEL_VDW{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
1133     /* #endif  */
1134 }