Introduce gmxpre.h for truly global definitions
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_avx_128_fma_single / nb_kernel_ElecCSTab_VdwCSTab_GeomP1P1_avx_128_fma_single.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*
36  * Note: this file was generated by the GROMACS avx_128_fma_single kernel generator.
37  */
38 #include "gmxpre.h"
39
40 #include "config.h"
41
42 #include <math.h>
43
44 #include "../nb_kernel.h"
45 #include "gromacs/legacyheaders/types/simple.h"
46 #include "gromacs/math/vec.h"
47 #include "gromacs/legacyheaders/nrnb.h"
48
49 #include "gromacs/simd/math_x86_avx_128_fma_single.h"
50 #include "kernelutil_x86_avx_128_fma_single.h"
51
52 /*
53  * Gromacs nonbonded kernel:   nb_kernel_ElecCSTab_VdwCSTab_GeomP1P1_VF_avx_128_fma_single
54  * Electrostatics interaction: CubicSplineTable
55  * VdW interaction:            CubicSplineTable
56  * Geometry:                   Particle-Particle
57  * Calculate force/pot:        PotentialAndForce
58  */
59 void
60 nb_kernel_ElecCSTab_VdwCSTab_GeomP1P1_VF_avx_128_fma_single
61                     (t_nblist                    * gmx_restrict       nlist,
62                      rvec                        * gmx_restrict          xx,
63                      rvec                        * gmx_restrict          ff,
64                      t_forcerec                  * gmx_restrict          fr,
65                      t_mdatoms                   * gmx_restrict     mdatoms,
66                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
67                      t_nrnb                      * gmx_restrict        nrnb)
68 {
69     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or
70      * just 0 for non-waters.
71      * Suffixes A,B,C,D refer to j loop unrolling done with AVX_128, e.g. for the four different
72      * jnr indices corresponding to data put in the four positions in the SIMD register.
73      */
74     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
75     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
76     int              jnrA,jnrB,jnrC,jnrD;
77     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
78     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
79     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
80     real             rcutoff_scalar;
81     real             *shiftvec,*fshift,*x,*f;
82     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
83     real             scratch[4*DIM];
84     __m128           fscal,rcutoff,rcutoff2,jidxall;
85     int              vdwioffset0;
86     __m128           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
87     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
88     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
89     __m128           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
90     __m128           velec,felec,velecsum,facel,crf,krf,krf2;
91     real             *charge;
92     int              nvdwtype;
93     __m128           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
94     int              *vdwtype;
95     real             *vdwparam;
96     __m128           one_sixth   = _mm_set1_ps(1.0/6.0);
97     __m128           one_twelfth = _mm_set1_ps(1.0/12.0);
98     __m128i          vfitab;
99     __m128i          ifour       = _mm_set1_epi32(4);
100     __m128           rt,vfeps,twovfeps,vftabscale,Y,F,G,H,Fp,VV,FF;
101     real             *vftab;
102     __m128           dummy_mask,cutoff_mask;
103     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
104     __m128           one     = _mm_set1_ps(1.0);
105     __m128           two     = _mm_set1_ps(2.0);
106     x                = xx[0];
107     f                = ff[0];
108
109     nri              = nlist->nri;
110     iinr             = nlist->iinr;
111     jindex           = nlist->jindex;
112     jjnr             = nlist->jjnr;
113     shiftidx         = nlist->shift;
114     gid              = nlist->gid;
115     shiftvec         = fr->shift_vec[0];
116     fshift           = fr->fshift[0];
117     facel            = _mm_set1_ps(fr->epsfac);
118     charge           = mdatoms->chargeA;
119     nvdwtype         = fr->ntype;
120     vdwparam         = fr->nbfp;
121     vdwtype          = mdatoms->typeA;
122
123     vftab            = kernel_data->table_elec_vdw->data;
124     vftabscale       = _mm_set1_ps(kernel_data->table_elec_vdw->scale);
125
126     /* Avoid stupid compiler warnings */
127     jnrA = jnrB = jnrC = jnrD = 0;
128     j_coord_offsetA = 0;
129     j_coord_offsetB = 0;
130     j_coord_offsetC = 0;
131     j_coord_offsetD = 0;
132
133     outeriter        = 0;
134     inneriter        = 0;
135
136     for(iidx=0;iidx<4*DIM;iidx++)
137     {
138         scratch[iidx] = 0.0;
139     }
140
141     /* Start outer loop over neighborlists */
142     for(iidx=0; iidx<nri; iidx++)
143     {
144         /* Load shift vector for this list */
145         i_shift_offset   = DIM*shiftidx[iidx];
146
147         /* Load limits for loop over neighbors */
148         j_index_start    = jindex[iidx];
149         j_index_end      = jindex[iidx+1];
150
151         /* Get outer coordinate index */
152         inr              = iinr[iidx];
153         i_coord_offset   = DIM*inr;
154
155         /* Load i particle coords and add shift vector */
156         gmx_mm_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
157
158         fix0             = _mm_setzero_ps();
159         fiy0             = _mm_setzero_ps();
160         fiz0             = _mm_setzero_ps();
161
162         /* Load parameters for i particles */
163         iq0              = _mm_mul_ps(facel,_mm_load1_ps(charge+inr+0));
164         vdwioffset0      = 2*nvdwtype*vdwtype[inr+0];
165
166         /* Reset potential sums */
167         velecsum         = _mm_setzero_ps();
168         vvdwsum          = _mm_setzero_ps();
169
170         /* Start inner kernel loop */
171         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
172         {
173
174             /* Get j neighbor index, and coordinate index */
175             jnrA             = jjnr[jidx];
176             jnrB             = jjnr[jidx+1];
177             jnrC             = jjnr[jidx+2];
178             jnrD             = jjnr[jidx+3];
179             j_coord_offsetA  = DIM*jnrA;
180             j_coord_offsetB  = DIM*jnrB;
181             j_coord_offsetC  = DIM*jnrC;
182             j_coord_offsetD  = DIM*jnrD;
183
184             /* load j atom coordinates */
185             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
186                                               x+j_coord_offsetC,x+j_coord_offsetD,
187                                               &jx0,&jy0,&jz0);
188
189             /* Calculate displacement vector */
190             dx00             = _mm_sub_ps(ix0,jx0);
191             dy00             = _mm_sub_ps(iy0,jy0);
192             dz00             = _mm_sub_ps(iz0,jz0);
193
194             /* Calculate squared distance and things based on it */
195             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
196
197             rinv00           = gmx_mm_invsqrt_ps(rsq00);
198
199             /* Load parameters for j particles */
200             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
201                                                               charge+jnrC+0,charge+jnrD+0);
202             vdwjidx0A        = 2*vdwtype[jnrA+0];
203             vdwjidx0B        = 2*vdwtype[jnrB+0];
204             vdwjidx0C        = 2*vdwtype[jnrC+0];
205             vdwjidx0D        = 2*vdwtype[jnrD+0];
206
207             /**************************
208              * CALCULATE INTERACTIONS *
209              **************************/
210
211             r00              = _mm_mul_ps(rsq00,rinv00);
212
213             /* Compute parameters for interactions between i and j atoms */
214             qq00             = _mm_mul_ps(iq0,jq0);
215             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
216                                          vdwparam+vdwioffset0+vdwjidx0B,
217                                          vdwparam+vdwioffset0+vdwjidx0C,
218                                          vdwparam+vdwioffset0+vdwjidx0D,
219                                          &c6_00,&c12_00);
220
221             /* Calculate table index by multiplying r with table scale and truncate to integer */
222             rt               = _mm_mul_ps(r00,vftabscale);
223             vfitab           = _mm_cvttps_epi32(rt);
224 #ifdef __XOP__
225             vfeps            = _mm_frcz_ps(rt);
226 #else
227             vfeps            = _mm_sub_ps(rt,_mm_round_ps(rt, _MM_FROUND_FLOOR));
228 #endif
229             twovfeps         = _mm_add_ps(vfeps,vfeps);
230             vfitab           = _mm_slli_epi32(_mm_add_epi32(vfitab,_mm_slli_epi32(vfitab,1)),2);
231
232             /* CUBIC SPLINE TABLE ELECTROSTATICS */
233             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
234             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
235             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
236             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
237             _MM_TRANSPOSE4_PS(Y,F,G,H);
238             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
239             VV               = _mm_macc_ps(vfeps,Fp,Y);
240             velec            = _mm_mul_ps(qq00,VV);
241             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
242             felec            = _mm_xor_ps(signbit,_mm_mul_ps(_mm_mul_ps(qq00,FF),_mm_mul_ps(vftabscale,rinv00)));
243
244             /* CUBIC SPLINE TABLE DISPERSION */
245             vfitab           = _mm_add_epi32(vfitab,ifour);
246             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
247             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
248             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
249             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
250             _MM_TRANSPOSE4_PS(Y,F,G,H);
251             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
252             VV               = _mm_macc_ps(vfeps,Fp,Y);
253             vvdw6            = _mm_mul_ps(c6_00,VV);
254             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
255             fvdw6            = _mm_mul_ps(c6_00,FF);
256
257             /* CUBIC SPLINE TABLE REPULSION */
258             vfitab           = _mm_add_epi32(vfitab,ifour);
259             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
260             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
261             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
262             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
263             _MM_TRANSPOSE4_PS(Y,F,G,H);
264             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
265             VV               = _mm_macc_ps(vfeps,Fp,Y);
266             vvdw12           = _mm_mul_ps(c12_00,VV);
267             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
268             fvdw12           = _mm_mul_ps(c12_00,FF);
269             vvdw             = _mm_add_ps(vvdw12,vvdw6);
270             fvdw             = _mm_xor_ps(signbit,_mm_mul_ps(_mm_add_ps(fvdw6,fvdw12),_mm_mul_ps(vftabscale,rinv00)));
271
272             /* Update potential sum for this i atom from the interaction with this j atom. */
273             velecsum         = _mm_add_ps(velecsum,velec);
274             vvdwsum          = _mm_add_ps(vvdwsum,vvdw);
275
276             fscal            = _mm_add_ps(felec,fvdw);
277
278              /* Update vectorial force */
279             fix0             = _mm_macc_ps(dx00,fscal,fix0);
280             fiy0             = _mm_macc_ps(dy00,fscal,fiy0);
281             fiz0             = _mm_macc_ps(dz00,fscal,fiz0);
282
283             fjptrA             = f+j_coord_offsetA;
284             fjptrB             = f+j_coord_offsetB;
285             fjptrC             = f+j_coord_offsetC;
286             fjptrD             = f+j_coord_offsetD;
287             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,
288                                                    _mm_mul_ps(dx00,fscal),
289                                                    _mm_mul_ps(dy00,fscal),
290                                                    _mm_mul_ps(dz00,fscal));
291
292             /* Inner loop uses 76 flops */
293         }
294
295         if(jidx<j_index_end)
296         {
297
298             /* Get j neighbor index, and coordinate index */
299             jnrlistA         = jjnr[jidx];
300             jnrlistB         = jjnr[jidx+1];
301             jnrlistC         = jjnr[jidx+2];
302             jnrlistD         = jjnr[jidx+3];
303             /* Sign of each element will be negative for non-real atoms.
304              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
305              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
306              */
307             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
308             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
309             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
310             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
311             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
312             j_coord_offsetA  = DIM*jnrA;
313             j_coord_offsetB  = DIM*jnrB;
314             j_coord_offsetC  = DIM*jnrC;
315             j_coord_offsetD  = DIM*jnrD;
316
317             /* load j atom coordinates */
318             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
319                                               x+j_coord_offsetC,x+j_coord_offsetD,
320                                               &jx0,&jy0,&jz0);
321
322             /* Calculate displacement vector */
323             dx00             = _mm_sub_ps(ix0,jx0);
324             dy00             = _mm_sub_ps(iy0,jy0);
325             dz00             = _mm_sub_ps(iz0,jz0);
326
327             /* Calculate squared distance and things based on it */
328             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
329
330             rinv00           = gmx_mm_invsqrt_ps(rsq00);
331
332             /* Load parameters for j particles */
333             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
334                                                               charge+jnrC+0,charge+jnrD+0);
335             vdwjidx0A        = 2*vdwtype[jnrA+0];
336             vdwjidx0B        = 2*vdwtype[jnrB+0];
337             vdwjidx0C        = 2*vdwtype[jnrC+0];
338             vdwjidx0D        = 2*vdwtype[jnrD+0];
339
340             /**************************
341              * CALCULATE INTERACTIONS *
342              **************************/
343
344             r00              = _mm_mul_ps(rsq00,rinv00);
345             r00              = _mm_andnot_ps(dummy_mask,r00);
346
347             /* Compute parameters for interactions between i and j atoms */
348             qq00             = _mm_mul_ps(iq0,jq0);
349             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
350                                          vdwparam+vdwioffset0+vdwjidx0B,
351                                          vdwparam+vdwioffset0+vdwjidx0C,
352                                          vdwparam+vdwioffset0+vdwjidx0D,
353                                          &c6_00,&c12_00);
354
355             /* Calculate table index by multiplying r with table scale and truncate to integer */
356             rt               = _mm_mul_ps(r00,vftabscale);
357             vfitab           = _mm_cvttps_epi32(rt);
358 #ifdef __XOP__
359             vfeps            = _mm_frcz_ps(rt);
360 #else
361             vfeps            = _mm_sub_ps(rt,_mm_round_ps(rt, _MM_FROUND_FLOOR));
362 #endif
363             twovfeps         = _mm_add_ps(vfeps,vfeps);
364             vfitab           = _mm_slli_epi32(_mm_add_epi32(vfitab,_mm_slli_epi32(vfitab,1)),2);
365
366             /* CUBIC SPLINE TABLE ELECTROSTATICS */
367             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
368             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
369             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
370             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
371             _MM_TRANSPOSE4_PS(Y,F,G,H);
372             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
373             VV               = _mm_macc_ps(vfeps,Fp,Y);
374             velec            = _mm_mul_ps(qq00,VV);
375             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
376             felec            = _mm_xor_ps(signbit,_mm_mul_ps(_mm_mul_ps(qq00,FF),_mm_mul_ps(vftabscale,rinv00)));
377
378             /* CUBIC SPLINE TABLE DISPERSION */
379             vfitab           = _mm_add_epi32(vfitab,ifour);
380             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
381             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
382             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
383             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
384             _MM_TRANSPOSE4_PS(Y,F,G,H);
385             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
386             VV               = _mm_macc_ps(vfeps,Fp,Y);
387             vvdw6            = _mm_mul_ps(c6_00,VV);
388             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
389             fvdw6            = _mm_mul_ps(c6_00,FF);
390
391             /* CUBIC SPLINE TABLE REPULSION */
392             vfitab           = _mm_add_epi32(vfitab,ifour);
393             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
394             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
395             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
396             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
397             _MM_TRANSPOSE4_PS(Y,F,G,H);
398             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
399             VV               = _mm_macc_ps(vfeps,Fp,Y);
400             vvdw12           = _mm_mul_ps(c12_00,VV);
401             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
402             fvdw12           = _mm_mul_ps(c12_00,FF);
403             vvdw             = _mm_add_ps(vvdw12,vvdw6);
404             fvdw             = _mm_xor_ps(signbit,_mm_mul_ps(_mm_add_ps(fvdw6,fvdw12),_mm_mul_ps(vftabscale,rinv00)));
405
406             /* Update potential sum for this i atom from the interaction with this j atom. */
407             velec            = _mm_andnot_ps(dummy_mask,velec);
408             velecsum         = _mm_add_ps(velecsum,velec);
409             vvdw             = _mm_andnot_ps(dummy_mask,vvdw);
410             vvdwsum          = _mm_add_ps(vvdwsum,vvdw);
411
412             fscal            = _mm_add_ps(felec,fvdw);
413
414             fscal            = _mm_andnot_ps(dummy_mask,fscal);
415
416              /* Update vectorial force */
417             fix0             = _mm_macc_ps(dx00,fscal,fix0);
418             fiy0             = _mm_macc_ps(dy00,fscal,fiy0);
419             fiz0             = _mm_macc_ps(dz00,fscal,fiz0);
420
421             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
422             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
423             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
424             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
425             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,
426                                                    _mm_mul_ps(dx00,fscal),
427                                                    _mm_mul_ps(dy00,fscal),
428                                                    _mm_mul_ps(dz00,fscal));
429
430             /* Inner loop uses 77 flops */
431         }
432
433         /* End of innermost loop */
434
435         gmx_mm_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
436                                               f+i_coord_offset,fshift+i_shift_offset);
437
438         ggid                        = gid[iidx];
439         /* Update potential energies */
440         gmx_mm_update_1pot_ps(velecsum,kernel_data->energygrp_elec+ggid);
441         gmx_mm_update_1pot_ps(vvdwsum,kernel_data->energygrp_vdw+ggid);
442
443         /* Increment number of inner iterations */
444         inneriter                  += j_index_end - j_index_start;
445
446         /* Outer loop uses 9 flops */
447     }
448
449     /* Increment number of outer iterations */
450     outeriter        += nri;
451
452     /* Update outer/inner flops */
453
454     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_VF,outeriter*9 + inneriter*77);
455 }
456 /*
457  * Gromacs nonbonded kernel:   nb_kernel_ElecCSTab_VdwCSTab_GeomP1P1_F_avx_128_fma_single
458  * Electrostatics interaction: CubicSplineTable
459  * VdW interaction:            CubicSplineTable
460  * Geometry:                   Particle-Particle
461  * Calculate force/pot:        Force
462  */
463 void
464 nb_kernel_ElecCSTab_VdwCSTab_GeomP1P1_F_avx_128_fma_single
465                     (t_nblist                    * gmx_restrict       nlist,
466                      rvec                        * gmx_restrict          xx,
467                      rvec                        * gmx_restrict          ff,
468                      t_forcerec                  * gmx_restrict          fr,
469                      t_mdatoms                   * gmx_restrict     mdatoms,
470                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
471                      t_nrnb                      * gmx_restrict        nrnb)
472 {
473     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or
474      * just 0 for non-waters.
475      * Suffixes A,B,C,D refer to j loop unrolling done with AVX_128, e.g. for the four different
476      * jnr indices corresponding to data put in the four positions in the SIMD register.
477      */
478     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
479     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
480     int              jnrA,jnrB,jnrC,jnrD;
481     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
482     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
483     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
484     real             rcutoff_scalar;
485     real             *shiftvec,*fshift,*x,*f;
486     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
487     real             scratch[4*DIM];
488     __m128           fscal,rcutoff,rcutoff2,jidxall;
489     int              vdwioffset0;
490     __m128           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
491     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
492     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
493     __m128           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
494     __m128           velec,felec,velecsum,facel,crf,krf,krf2;
495     real             *charge;
496     int              nvdwtype;
497     __m128           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
498     int              *vdwtype;
499     real             *vdwparam;
500     __m128           one_sixth   = _mm_set1_ps(1.0/6.0);
501     __m128           one_twelfth = _mm_set1_ps(1.0/12.0);
502     __m128i          vfitab;
503     __m128i          ifour       = _mm_set1_epi32(4);
504     __m128           rt,vfeps,twovfeps,vftabscale,Y,F,G,H,Fp,VV,FF;
505     real             *vftab;
506     __m128           dummy_mask,cutoff_mask;
507     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
508     __m128           one     = _mm_set1_ps(1.0);
509     __m128           two     = _mm_set1_ps(2.0);
510     x                = xx[0];
511     f                = ff[0];
512
513     nri              = nlist->nri;
514     iinr             = nlist->iinr;
515     jindex           = nlist->jindex;
516     jjnr             = nlist->jjnr;
517     shiftidx         = nlist->shift;
518     gid              = nlist->gid;
519     shiftvec         = fr->shift_vec[0];
520     fshift           = fr->fshift[0];
521     facel            = _mm_set1_ps(fr->epsfac);
522     charge           = mdatoms->chargeA;
523     nvdwtype         = fr->ntype;
524     vdwparam         = fr->nbfp;
525     vdwtype          = mdatoms->typeA;
526
527     vftab            = kernel_data->table_elec_vdw->data;
528     vftabscale       = _mm_set1_ps(kernel_data->table_elec_vdw->scale);
529
530     /* Avoid stupid compiler warnings */
531     jnrA = jnrB = jnrC = jnrD = 0;
532     j_coord_offsetA = 0;
533     j_coord_offsetB = 0;
534     j_coord_offsetC = 0;
535     j_coord_offsetD = 0;
536
537     outeriter        = 0;
538     inneriter        = 0;
539
540     for(iidx=0;iidx<4*DIM;iidx++)
541     {
542         scratch[iidx] = 0.0;
543     }
544
545     /* Start outer loop over neighborlists */
546     for(iidx=0; iidx<nri; iidx++)
547     {
548         /* Load shift vector for this list */
549         i_shift_offset   = DIM*shiftidx[iidx];
550
551         /* Load limits for loop over neighbors */
552         j_index_start    = jindex[iidx];
553         j_index_end      = jindex[iidx+1];
554
555         /* Get outer coordinate index */
556         inr              = iinr[iidx];
557         i_coord_offset   = DIM*inr;
558
559         /* Load i particle coords and add shift vector */
560         gmx_mm_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
561
562         fix0             = _mm_setzero_ps();
563         fiy0             = _mm_setzero_ps();
564         fiz0             = _mm_setzero_ps();
565
566         /* Load parameters for i particles */
567         iq0              = _mm_mul_ps(facel,_mm_load1_ps(charge+inr+0));
568         vdwioffset0      = 2*nvdwtype*vdwtype[inr+0];
569
570         /* Start inner kernel loop */
571         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
572         {
573
574             /* Get j neighbor index, and coordinate index */
575             jnrA             = jjnr[jidx];
576             jnrB             = jjnr[jidx+1];
577             jnrC             = jjnr[jidx+2];
578             jnrD             = jjnr[jidx+3];
579             j_coord_offsetA  = DIM*jnrA;
580             j_coord_offsetB  = DIM*jnrB;
581             j_coord_offsetC  = DIM*jnrC;
582             j_coord_offsetD  = DIM*jnrD;
583
584             /* load j atom coordinates */
585             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
586                                               x+j_coord_offsetC,x+j_coord_offsetD,
587                                               &jx0,&jy0,&jz0);
588
589             /* Calculate displacement vector */
590             dx00             = _mm_sub_ps(ix0,jx0);
591             dy00             = _mm_sub_ps(iy0,jy0);
592             dz00             = _mm_sub_ps(iz0,jz0);
593
594             /* Calculate squared distance and things based on it */
595             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
596
597             rinv00           = gmx_mm_invsqrt_ps(rsq00);
598
599             /* Load parameters for j particles */
600             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
601                                                               charge+jnrC+0,charge+jnrD+0);
602             vdwjidx0A        = 2*vdwtype[jnrA+0];
603             vdwjidx0B        = 2*vdwtype[jnrB+0];
604             vdwjidx0C        = 2*vdwtype[jnrC+0];
605             vdwjidx0D        = 2*vdwtype[jnrD+0];
606
607             /**************************
608              * CALCULATE INTERACTIONS *
609              **************************/
610
611             r00              = _mm_mul_ps(rsq00,rinv00);
612
613             /* Compute parameters for interactions between i and j atoms */
614             qq00             = _mm_mul_ps(iq0,jq0);
615             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
616                                          vdwparam+vdwioffset0+vdwjidx0B,
617                                          vdwparam+vdwioffset0+vdwjidx0C,
618                                          vdwparam+vdwioffset0+vdwjidx0D,
619                                          &c6_00,&c12_00);
620
621             /* Calculate table index by multiplying r with table scale and truncate to integer */
622             rt               = _mm_mul_ps(r00,vftabscale);
623             vfitab           = _mm_cvttps_epi32(rt);
624 #ifdef __XOP__
625             vfeps            = _mm_frcz_ps(rt);
626 #else
627             vfeps            = _mm_sub_ps(rt,_mm_round_ps(rt, _MM_FROUND_FLOOR));
628 #endif
629             twovfeps         = _mm_add_ps(vfeps,vfeps);
630             vfitab           = _mm_slli_epi32(_mm_add_epi32(vfitab,_mm_slli_epi32(vfitab,1)),2);
631
632             /* CUBIC SPLINE TABLE ELECTROSTATICS */
633             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
634             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
635             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
636             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
637             _MM_TRANSPOSE4_PS(Y,F,G,H);
638             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
639             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
640             felec            = _mm_xor_ps(signbit,_mm_mul_ps(_mm_mul_ps(qq00,FF),_mm_mul_ps(vftabscale,rinv00)));
641
642             /* CUBIC SPLINE TABLE DISPERSION */
643             vfitab           = _mm_add_epi32(vfitab,ifour);
644             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
645             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
646             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
647             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
648             _MM_TRANSPOSE4_PS(Y,F,G,H);
649             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
650             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
651             fvdw6            = _mm_mul_ps(c6_00,FF);
652
653             /* CUBIC SPLINE TABLE REPULSION */
654             vfitab           = _mm_add_epi32(vfitab,ifour);
655             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
656             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
657             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
658             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
659             _MM_TRANSPOSE4_PS(Y,F,G,H);
660             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
661             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
662             fvdw12           = _mm_mul_ps(c12_00,FF);
663             fvdw             = _mm_xor_ps(signbit,_mm_mul_ps(_mm_add_ps(fvdw6,fvdw12),_mm_mul_ps(vftabscale,rinv00)));
664
665             fscal            = _mm_add_ps(felec,fvdw);
666
667              /* Update vectorial force */
668             fix0             = _mm_macc_ps(dx00,fscal,fix0);
669             fiy0             = _mm_macc_ps(dy00,fscal,fiy0);
670             fiz0             = _mm_macc_ps(dz00,fscal,fiz0);
671
672             fjptrA             = f+j_coord_offsetA;
673             fjptrB             = f+j_coord_offsetB;
674             fjptrC             = f+j_coord_offsetC;
675             fjptrD             = f+j_coord_offsetD;
676             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,
677                                                    _mm_mul_ps(dx00,fscal),
678                                                    _mm_mul_ps(dy00,fscal),
679                                                    _mm_mul_ps(dz00,fscal));
680
681             /* Inner loop uses 64 flops */
682         }
683
684         if(jidx<j_index_end)
685         {
686
687             /* Get j neighbor index, and coordinate index */
688             jnrlistA         = jjnr[jidx];
689             jnrlistB         = jjnr[jidx+1];
690             jnrlistC         = jjnr[jidx+2];
691             jnrlistD         = jjnr[jidx+3];
692             /* Sign of each element will be negative for non-real atoms.
693              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
694              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
695              */
696             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
697             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
698             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
699             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
700             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
701             j_coord_offsetA  = DIM*jnrA;
702             j_coord_offsetB  = DIM*jnrB;
703             j_coord_offsetC  = DIM*jnrC;
704             j_coord_offsetD  = DIM*jnrD;
705
706             /* load j atom coordinates */
707             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
708                                               x+j_coord_offsetC,x+j_coord_offsetD,
709                                               &jx0,&jy0,&jz0);
710
711             /* Calculate displacement vector */
712             dx00             = _mm_sub_ps(ix0,jx0);
713             dy00             = _mm_sub_ps(iy0,jy0);
714             dz00             = _mm_sub_ps(iz0,jz0);
715
716             /* Calculate squared distance and things based on it */
717             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
718
719             rinv00           = gmx_mm_invsqrt_ps(rsq00);
720
721             /* Load parameters for j particles */
722             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
723                                                               charge+jnrC+0,charge+jnrD+0);
724             vdwjidx0A        = 2*vdwtype[jnrA+0];
725             vdwjidx0B        = 2*vdwtype[jnrB+0];
726             vdwjidx0C        = 2*vdwtype[jnrC+0];
727             vdwjidx0D        = 2*vdwtype[jnrD+0];
728
729             /**************************
730              * CALCULATE INTERACTIONS *
731              **************************/
732
733             r00              = _mm_mul_ps(rsq00,rinv00);
734             r00              = _mm_andnot_ps(dummy_mask,r00);
735
736             /* Compute parameters for interactions between i and j atoms */
737             qq00             = _mm_mul_ps(iq0,jq0);
738             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
739                                          vdwparam+vdwioffset0+vdwjidx0B,
740                                          vdwparam+vdwioffset0+vdwjidx0C,
741                                          vdwparam+vdwioffset0+vdwjidx0D,
742                                          &c6_00,&c12_00);
743
744             /* Calculate table index by multiplying r with table scale and truncate to integer */
745             rt               = _mm_mul_ps(r00,vftabscale);
746             vfitab           = _mm_cvttps_epi32(rt);
747 #ifdef __XOP__
748             vfeps            = _mm_frcz_ps(rt);
749 #else
750             vfeps            = _mm_sub_ps(rt,_mm_round_ps(rt, _MM_FROUND_FLOOR));
751 #endif
752             twovfeps         = _mm_add_ps(vfeps,vfeps);
753             vfitab           = _mm_slli_epi32(_mm_add_epi32(vfitab,_mm_slli_epi32(vfitab,1)),2);
754
755             /* CUBIC SPLINE TABLE ELECTROSTATICS */
756             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
757             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
758             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
759             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
760             _MM_TRANSPOSE4_PS(Y,F,G,H);
761             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
762             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
763             felec            = _mm_xor_ps(signbit,_mm_mul_ps(_mm_mul_ps(qq00,FF),_mm_mul_ps(vftabscale,rinv00)));
764
765             /* CUBIC SPLINE TABLE DISPERSION */
766             vfitab           = _mm_add_epi32(vfitab,ifour);
767             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
768             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
769             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
770             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
771             _MM_TRANSPOSE4_PS(Y,F,G,H);
772             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
773             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
774             fvdw6            = _mm_mul_ps(c6_00,FF);
775
776             /* CUBIC SPLINE TABLE REPULSION */
777             vfitab           = _mm_add_epi32(vfitab,ifour);
778             Y                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,0) );
779             F                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,1) );
780             G                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,2) );
781             H                = _mm_load_ps( vftab + _mm_extract_epi32(vfitab,3) );
782             _MM_TRANSPOSE4_PS(Y,F,G,H);
783             Fp               = _mm_macc_ps(vfeps,_mm_macc_ps(H,vfeps,G),F);
784             FF               = _mm_macc_ps(vfeps,_mm_macc_ps(twovfeps,H,G),Fp);
785             fvdw12           = _mm_mul_ps(c12_00,FF);
786             fvdw             = _mm_xor_ps(signbit,_mm_mul_ps(_mm_add_ps(fvdw6,fvdw12),_mm_mul_ps(vftabscale,rinv00)));
787
788             fscal            = _mm_add_ps(felec,fvdw);
789
790             fscal            = _mm_andnot_ps(dummy_mask,fscal);
791
792              /* Update vectorial force */
793             fix0             = _mm_macc_ps(dx00,fscal,fix0);
794             fiy0             = _mm_macc_ps(dy00,fscal,fiy0);
795             fiz0             = _mm_macc_ps(dz00,fscal,fiz0);
796
797             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
798             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
799             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
800             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
801             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,
802                                                    _mm_mul_ps(dx00,fscal),
803                                                    _mm_mul_ps(dy00,fscal),
804                                                    _mm_mul_ps(dz00,fscal));
805
806             /* Inner loop uses 65 flops */
807         }
808
809         /* End of innermost loop */
810
811         gmx_mm_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
812                                               f+i_coord_offset,fshift+i_shift_offset);
813
814         /* Increment number of inner iterations */
815         inneriter                  += j_index_end - j_index_start;
816
817         /* Outer loop uses 7 flops */
818     }
819
820     /* Increment number of outer iterations */
821     outeriter        += nri;
822
823     /* Update outer/inner flops */
824
825     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_F,outeriter*7 + inneriter*65);
826 }