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