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