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