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