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