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