Compile nonbonded kernels as C++
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_sse2_single / nb_kernel_ElecNone_VdwLJEw_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_ElecNone_VdwLJEw_GeomP1P1_VF_sse2_single
51  * Electrostatics interaction: None
52  * VdW interaction:            LJEwald
53  * Geometry:                   Particle-Particle
54  * Calculate force/pot:        PotentialAndForce
55  */
56 void
57 nb_kernel_ElecNone_VdwLJEw_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     int              nvdwtype;
88     __m128           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
89     int              *vdwtype;
90     real             *vdwparam;
91     __m128           one_sixth   = _mm_set1_ps(1.0/6.0);
92     __m128           one_twelfth = _mm_set1_ps(1.0/12.0);
93     __m128           c6grid_00;
94     __m128           ewclj,ewclj2,ewclj6,ewcljrsq,poly,exponent,f6A,f6B,sh_lj_ewald;
95     real             *vdwgridparam;
96     __m128           one_half = _mm_set1_ps(0.5);
97     __m128           minus_one = _mm_set1_ps(-1.0);
98     __m128           dummy_mask,cutoff_mask;
99     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
100     __m128           one     = _mm_set1_ps(1.0);
101     __m128           two     = _mm_set1_ps(2.0);
102     x                = xx[0];
103     f                = ff[0];
104
105     nri              = nlist->nri;
106     iinr             = nlist->iinr;
107     jindex           = nlist->jindex;
108     jjnr             = nlist->jjnr;
109     shiftidx         = nlist->shift;
110     gid              = nlist->gid;
111     shiftvec         = fr->shift_vec[0];
112     fshift           = fr->fshift[0];
113     nvdwtype         = fr->ntype;
114     vdwparam         = fr->nbfp;
115     vdwtype          = mdatoms->typeA;
116     vdwgridparam     = fr->ljpme_c6grid;
117     sh_lj_ewald      = _mm_set1_ps(fr->ic->sh_lj_ewald);
118     ewclj            = _mm_set1_ps(fr->ic->ewaldcoeff_lj);
119     ewclj2           = _mm_mul_ps(minus_one,_mm_mul_ps(ewclj,ewclj));
120
121     /* Avoid stupid compiler warnings */
122     jnrA = jnrB = jnrC = jnrD = 0;
123     j_coord_offsetA = 0;
124     j_coord_offsetB = 0;
125     j_coord_offsetC = 0;
126     j_coord_offsetD = 0;
127
128     outeriter        = 0;
129     inneriter        = 0;
130
131     for(iidx=0;iidx<4*DIM;iidx++)
132     {
133         scratch[iidx] = 0.0;
134     }  
135
136     /* Start outer loop over neighborlists */
137     for(iidx=0; iidx<nri; iidx++)
138     {
139         /* Load shift vector for this list */
140         i_shift_offset   = DIM*shiftidx[iidx];
141
142         /* Load limits for loop over neighbors */
143         j_index_start    = jindex[iidx];
144         j_index_end      = jindex[iidx+1];
145
146         /* Get outer coordinate index */
147         inr              = iinr[iidx];
148         i_coord_offset   = DIM*inr;
149
150         /* Load i particle coords and add shift vector */
151         gmx_mm_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
152         
153         fix0             = _mm_setzero_ps();
154         fiy0             = _mm_setzero_ps();
155         fiz0             = _mm_setzero_ps();
156
157         /* Load parameters for i particles */
158         vdwioffset0      = 2*nvdwtype*vdwtype[inr+0];
159
160         /* Reset potential sums */
161         vvdwsum          = _mm_setzero_ps();
162
163         /* Start inner kernel loop */
164         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
165         {
166
167             /* Get j neighbor index, and coordinate index */
168             jnrA             = jjnr[jidx];
169             jnrB             = jjnr[jidx+1];
170             jnrC             = jjnr[jidx+2];
171             jnrD             = jjnr[jidx+3];
172             j_coord_offsetA  = DIM*jnrA;
173             j_coord_offsetB  = DIM*jnrB;
174             j_coord_offsetC  = DIM*jnrC;
175             j_coord_offsetD  = DIM*jnrD;
176
177             /* load j atom coordinates */
178             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
179                                               x+j_coord_offsetC,x+j_coord_offsetD,
180                                               &jx0,&jy0,&jz0);
181
182             /* Calculate displacement vector */
183             dx00             = _mm_sub_ps(ix0,jx0);
184             dy00             = _mm_sub_ps(iy0,jy0);
185             dz00             = _mm_sub_ps(iz0,jz0);
186
187             /* Calculate squared distance and things based on it */
188             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
189
190             rinv00           = sse2_invsqrt_f(rsq00);
191
192             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
193
194             /* Load parameters for j particles */
195             vdwjidx0A        = 2*vdwtype[jnrA+0];
196             vdwjidx0B        = 2*vdwtype[jnrB+0];
197             vdwjidx0C        = 2*vdwtype[jnrC+0];
198             vdwjidx0D        = 2*vdwtype[jnrD+0];
199
200             /**************************
201              * CALCULATE INTERACTIONS *
202              **************************/
203
204             r00              = _mm_mul_ps(rsq00,rinv00);
205
206             /* Compute parameters for interactions between i and j atoms */
207             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
208                                          vdwparam+vdwioffset0+vdwjidx0B,
209                                          vdwparam+vdwioffset0+vdwjidx0C,
210                                          vdwparam+vdwioffset0+vdwjidx0D,
211                                          &c6_00,&c12_00);
212             c6grid_00       = gmx_mm_load_4real_swizzle_ps(vdwgridparam+vdwioffset0+vdwjidx0A,
213                                                                vdwgridparam+vdwioffset0+vdwjidx0B,
214                                                                vdwgridparam+vdwioffset0+vdwjidx0C,
215                                                                vdwgridparam+vdwioffset0+vdwjidx0D);
216
217             /* Analytical LJ-PME */
218             rinvsix          = _mm_mul_ps(_mm_mul_ps(rinvsq00,rinvsq00),rinvsq00);
219             ewcljrsq         = _mm_mul_ps(ewclj2,rsq00);
220             ewclj6           = _mm_mul_ps(ewclj2,_mm_mul_ps(ewclj2,ewclj2));
221             exponent         = sse2_exp_f(ewcljrsq);
222             /* poly = exp(-(beta*r)^2) * (1 + (beta*r)^2 + (beta*r)^4 /2) */
223             poly             = _mm_mul_ps(exponent,_mm_add_ps(_mm_sub_ps(one,ewcljrsq),_mm_mul_ps(_mm_mul_ps(ewcljrsq,ewcljrsq),one_half)));
224             /* vvdw6 = [C6 - C6grid * (1-poly)]/r6 */
225             vvdw6            = _mm_mul_ps(_mm_sub_ps(c6_00,_mm_mul_ps(c6grid_00,_mm_sub_ps(one,poly))),rinvsix);
226             vvdw12           = _mm_mul_ps(c12_00,_mm_mul_ps(rinvsix,rinvsix));
227             vvdw             = _mm_sub_ps(_mm_mul_ps(vvdw12,one_twelfth),_mm_mul_ps(vvdw6,one_sixth));
228             /* fvdw = vvdw12/r - (vvdw6/r + (C6grid * exponent * beta^6)/r) */
229             fvdw             = _mm_mul_ps(_mm_sub_ps(vvdw12,_mm_sub_ps(vvdw6,_mm_mul_ps(_mm_mul_ps(c6grid_00,one_sixth),_mm_mul_ps(exponent,ewclj6)))),rinvsq00);
230
231             /* Update potential sum for this i atom from the interaction with this j atom. */
232             vvdwsum          = _mm_add_ps(vvdwsum,vvdw);
233
234             fscal            = fvdw;
235
236             /* Calculate temporary vectorial force */
237             tx               = _mm_mul_ps(fscal,dx00);
238             ty               = _mm_mul_ps(fscal,dy00);
239             tz               = _mm_mul_ps(fscal,dz00);
240
241             /* Update vectorial force */
242             fix0             = _mm_add_ps(fix0,tx);
243             fiy0             = _mm_add_ps(fiy0,ty);
244             fiz0             = _mm_add_ps(fiz0,tz);
245
246             fjptrA             = f+j_coord_offsetA;
247             fjptrB             = f+j_coord_offsetB;
248             fjptrC             = f+j_coord_offsetC;
249             fjptrD             = f+j_coord_offsetD;
250             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
251             
252             /* Inner loop uses 51 flops */
253         }
254
255         if(jidx<j_index_end)
256         {
257
258             /* Get j neighbor index, and coordinate index */
259             jnrlistA         = jjnr[jidx];
260             jnrlistB         = jjnr[jidx+1];
261             jnrlistC         = jjnr[jidx+2];
262             jnrlistD         = jjnr[jidx+3];
263             /* Sign of each element will be negative for non-real atoms.
264              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
265              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
266              */
267             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
268             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
269             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
270             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
271             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
272             j_coord_offsetA  = DIM*jnrA;
273             j_coord_offsetB  = DIM*jnrB;
274             j_coord_offsetC  = DIM*jnrC;
275             j_coord_offsetD  = DIM*jnrD;
276
277             /* load j atom coordinates */
278             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
279                                               x+j_coord_offsetC,x+j_coord_offsetD,
280                                               &jx0,&jy0,&jz0);
281
282             /* Calculate displacement vector */
283             dx00             = _mm_sub_ps(ix0,jx0);
284             dy00             = _mm_sub_ps(iy0,jy0);
285             dz00             = _mm_sub_ps(iz0,jz0);
286
287             /* Calculate squared distance and things based on it */
288             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
289
290             rinv00           = sse2_invsqrt_f(rsq00);
291
292             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
293
294             /* Load parameters for j particles */
295             vdwjidx0A        = 2*vdwtype[jnrA+0];
296             vdwjidx0B        = 2*vdwtype[jnrB+0];
297             vdwjidx0C        = 2*vdwtype[jnrC+0];
298             vdwjidx0D        = 2*vdwtype[jnrD+0];
299
300             /**************************
301              * CALCULATE INTERACTIONS *
302              **************************/
303
304             r00              = _mm_mul_ps(rsq00,rinv00);
305             r00              = _mm_andnot_ps(dummy_mask,r00);
306
307             /* Compute parameters for interactions between i and j atoms */
308             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
309                                          vdwparam+vdwioffset0+vdwjidx0B,
310                                          vdwparam+vdwioffset0+vdwjidx0C,
311                                          vdwparam+vdwioffset0+vdwjidx0D,
312                                          &c6_00,&c12_00);
313             c6grid_00       = gmx_mm_load_4real_swizzle_ps(vdwgridparam+vdwioffset0+vdwjidx0A,
314                                                                vdwgridparam+vdwioffset0+vdwjidx0B,
315                                                                vdwgridparam+vdwioffset0+vdwjidx0C,
316                                                                vdwgridparam+vdwioffset0+vdwjidx0D);
317
318             /* Analytical LJ-PME */
319             rinvsix          = _mm_mul_ps(_mm_mul_ps(rinvsq00,rinvsq00),rinvsq00);
320             ewcljrsq         = _mm_mul_ps(ewclj2,rsq00);
321             ewclj6           = _mm_mul_ps(ewclj2,_mm_mul_ps(ewclj2,ewclj2));
322             exponent         = sse2_exp_f(ewcljrsq);
323             /* poly = exp(-(beta*r)^2) * (1 + (beta*r)^2 + (beta*r)^4 /2) */
324             poly             = _mm_mul_ps(exponent,_mm_add_ps(_mm_sub_ps(one,ewcljrsq),_mm_mul_ps(_mm_mul_ps(ewcljrsq,ewcljrsq),one_half)));
325             /* vvdw6 = [C6 - C6grid * (1-poly)]/r6 */
326             vvdw6            = _mm_mul_ps(_mm_sub_ps(c6_00,_mm_mul_ps(c6grid_00,_mm_sub_ps(one,poly))),rinvsix);
327             vvdw12           = _mm_mul_ps(c12_00,_mm_mul_ps(rinvsix,rinvsix));
328             vvdw             = _mm_sub_ps(_mm_mul_ps(vvdw12,one_twelfth),_mm_mul_ps(vvdw6,one_sixth));
329             /* fvdw = vvdw12/r - (vvdw6/r + (C6grid * exponent * beta^6)/r) */
330             fvdw             = _mm_mul_ps(_mm_sub_ps(vvdw12,_mm_sub_ps(vvdw6,_mm_mul_ps(_mm_mul_ps(c6grid_00,one_sixth),_mm_mul_ps(exponent,ewclj6)))),rinvsq00);
331
332             /* Update potential sum for this i atom from the interaction with this j atom. */
333             vvdw             = _mm_andnot_ps(dummy_mask,vvdw);
334             vvdwsum          = _mm_add_ps(vvdwsum,vvdw);
335
336             fscal            = fvdw;
337
338             fscal            = _mm_andnot_ps(dummy_mask,fscal);
339
340             /* Calculate temporary vectorial force */
341             tx               = _mm_mul_ps(fscal,dx00);
342             ty               = _mm_mul_ps(fscal,dy00);
343             tz               = _mm_mul_ps(fscal,dz00);
344
345             /* Update vectorial force */
346             fix0             = _mm_add_ps(fix0,tx);
347             fiy0             = _mm_add_ps(fiy0,ty);
348             fiz0             = _mm_add_ps(fiz0,tz);
349
350             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
351             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
352             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
353             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
354             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
355             
356             /* Inner loop uses 52 flops */
357         }
358
359         /* End of innermost loop */
360
361         gmx_mm_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
362                                               f+i_coord_offset,fshift+i_shift_offset);
363
364         ggid                        = gid[iidx];
365         /* Update potential energies */
366         gmx_mm_update_1pot_ps(vvdwsum,kernel_data->energygrp_vdw+ggid);
367
368         /* Increment number of inner iterations */
369         inneriter                  += j_index_end - j_index_start;
370
371         /* Outer loop uses 7 flops */
372     }
373
374     /* Increment number of outer iterations */
375     outeriter        += nri;
376
377     /* Update outer/inner flops */
378
379     inc_nrnb(nrnb,eNR_NBKERNEL_VDW_VF,outeriter*7 + inneriter*52);
380 }
381 /*
382  * Gromacs nonbonded kernel:   nb_kernel_ElecNone_VdwLJEw_GeomP1P1_F_sse2_single
383  * Electrostatics interaction: None
384  * VdW interaction:            LJEwald
385  * Geometry:                   Particle-Particle
386  * Calculate force/pot:        Force
387  */
388 void
389 nb_kernel_ElecNone_VdwLJEw_GeomP1P1_F_sse2_single
390                     (t_nblist                    * gmx_restrict       nlist,
391                      rvec                        * gmx_restrict          xx,
392                      rvec                        * gmx_restrict          ff,
393                      struct t_forcerec           * gmx_restrict          fr,
394                      t_mdatoms                   * gmx_restrict     mdatoms,
395                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
396                      t_nrnb                      * gmx_restrict        nrnb)
397 {
398     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
399      * just 0 for non-waters.
400      * Suffixes A,B,C,D refer to j loop unrolling done with SSE, e.g. for the four different
401      * jnr indices corresponding to data put in the four positions in the SIMD register.
402      */
403     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
404     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
405     int              jnrA,jnrB,jnrC,jnrD;
406     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
407     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
408     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
409     real             rcutoff_scalar;
410     real             *shiftvec,*fshift,*x,*f;
411     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
412     real             scratch[4*DIM];
413     __m128           tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
414     int              vdwioffset0;
415     __m128           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
416     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
417     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
418     __m128           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
419     int              nvdwtype;
420     __m128           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
421     int              *vdwtype;
422     real             *vdwparam;
423     __m128           one_sixth   = _mm_set1_ps(1.0/6.0);
424     __m128           one_twelfth = _mm_set1_ps(1.0/12.0);
425     __m128           c6grid_00;
426     __m128           ewclj,ewclj2,ewclj6,ewcljrsq,poly,exponent,f6A,f6B,sh_lj_ewald;
427     real             *vdwgridparam;
428     __m128           one_half = _mm_set1_ps(0.5);
429     __m128           minus_one = _mm_set1_ps(-1.0);
430     __m128           dummy_mask,cutoff_mask;
431     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
432     __m128           one     = _mm_set1_ps(1.0);
433     __m128           two     = _mm_set1_ps(2.0);
434     x                = xx[0];
435     f                = ff[0];
436
437     nri              = nlist->nri;
438     iinr             = nlist->iinr;
439     jindex           = nlist->jindex;
440     jjnr             = nlist->jjnr;
441     shiftidx         = nlist->shift;
442     gid              = nlist->gid;
443     shiftvec         = fr->shift_vec[0];
444     fshift           = fr->fshift[0];
445     nvdwtype         = fr->ntype;
446     vdwparam         = fr->nbfp;
447     vdwtype          = mdatoms->typeA;
448     vdwgridparam     = fr->ljpme_c6grid;
449     sh_lj_ewald      = _mm_set1_ps(fr->ic->sh_lj_ewald);
450     ewclj            = _mm_set1_ps(fr->ic->ewaldcoeff_lj);
451     ewclj2           = _mm_mul_ps(minus_one,_mm_mul_ps(ewclj,ewclj));
452
453     /* Avoid stupid compiler warnings */
454     jnrA = jnrB = jnrC = jnrD = 0;
455     j_coord_offsetA = 0;
456     j_coord_offsetB = 0;
457     j_coord_offsetC = 0;
458     j_coord_offsetD = 0;
459
460     outeriter        = 0;
461     inneriter        = 0;
462
463     for(iidx=0;iidx<4*DIM;iidx++)
464     {
465         scratch[iidx] = 0.0;
466     }  
467
468     /* Start outer loop over neighborlists */
469     for(iidx=0; iidx<nri; iidx++)
470     {
471         /* Load shift vector for this list */
472         i_shift_offset   = DIM*shiftidx[iidx];
473
474         /* Load limits for loop over neighbors */
475         j_index_start    = jindex[iidx];
476         j_index_end      = jindex[iidx+1];
477
478         /* Get outer coordinate index */
479         inr              = iinr[iidx];
480         i_coord_offset   = DIM*inr;
481
482         /* Load i particle coords and add shift vector */
483         gmx_mm_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
484         
485         fix0             = _mm_setzero_ps();
486         fiy0             = _mm_setzero_ps();
487         fiz0             = _mm_setzero_ps();
488
489         /* Load parameters for i particles */
490         vdwioffset0      = 2*nvdwtype*vdwtype[inr+0];
491
492         /* Start inner kernel loop */
493         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
494         {
495
496             /* Get j neighbor index, and coordinate index */
497             jnrA             = jjnr[jidx];
498             jnrB             = jjnr[jidx+1];
499             jnrC             = jjnr[jidx+2];
500             jnrD             = jjnr[jidx+3];
501             j_coord_offsetA  = DIM*jnrA;
502             j_coord_offsetB  = DIM*jnrB;
503             j_coord_offsetC  = DIM*jnrC;
504             j_coord_offsetD  = DIM*jnrD;
505
506             /* load j atom coordinates */
507             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
508                                               x+j_coord_offsetC,x+j_coord_offsetD,
509                                               &jx0,&jy0,&jz0);
510
511             /* Calculate displacement vector */
512             dx00             = _mm_sub_ps(ix0,jx0);
513             dy00             = _mm_sub_ps(iy0,jy0);
514             dz00             = _mm_sub_ps(iz0,jz0);
515
516             /* Calculate squared distance and things based on it */
517             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
518
519             rinv00           = sse2_invsqrt_f(rsq00);
520
521             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
522
523             /* Load parameters for j particles */
524             vdwjidx0A        = 2*vdwtype[jnrA+0];
525             vdwjidx0B        = 2*vdwtype[jnrB+0];
526             vdwjidx0C        = 2*vdwtype[jnrC+0];
527             vdwjidx0D        = 2*vdwtype[jnrD+0];
528
529             /**************************
530              * CALCULATE INTERACTIONS *
531              **************************/
532
533             r00              = _mm_mul_ps(rsq00,rinv00);
534
535             /* Compute parameters for interactions between i and j atoms */
536             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
537                                          vdwparam+vdwioffset0+vdwjidx0B,
538                                          vdwparam+vdwioffset0+vdwjidx0C,
539                                          vdwparam+vdwioffset0+vdwjidx0D,
540                                          &c6_00,&c12_00);
541             c6grid_00       = gmx_mm_load_4real_swizzle_ps(vdwgridparam+vdwioffset0+vdwjidx0A,
542                                                                vdwgridparam+vdwioffset0+vdwjidx0B,
543                                                                vdwgridparam+vdwioffset0+vdwjidx0C,
544                                                                vdwgridparam+vdwioffset0+vdwjidx0D);
545
546             /* Analytical LJ-PME */
547             rinvsix          = _mm_mul_ps(_mm_mul_ps(rinvsq00,rinvsq00),rinvsq00);
548             ewcljrsq         = _mm_mul_ps(ewclj2,rsq00);
549             ewclj6           = _mm_mul_ps(ewclj2,_mm_mul_ps(ewclj2,ewclj2));
550             exponent         = sse2_exp_f(ewcljrsq);
551             /* poly = exp(-(beta*r)^2) * (1 + (beta*r)^2 + (beta*r)^4 /2) */
552             poly             = _mm_mul_ps(exponent,_mm_add_ps(_mm_sub_ps(one,ewcljrsq),_mm_mul_ps(_mm_mul_ps(ewcljrsq,ewcljrsq),one_half)));
553             /* f6A = 6 * C6grid * (1 - poly) */
554             f6A              = _mm_mul_ps(c6grid_00,_mm_sub_ps(one,poly));
555             /* f6B = C6grid * exponent * beta^6 */
556             f6B              = _mm_mul_ps(_mm_mul_ps(c6grid_00,one_sixth),_mm_mul_ps(exponent,ewclj6));
557             /* fvdw = 12*C12/r13 - ((6*C6 - f6A)/r6 + f6B)/r */
558             fvdw              = _mm_mul_ps(_mm_add_ps(_mm_mul_ps(_mm_sub_ps(_mm_mul_ps(c12_00,rinvsix),_mm_sub_ps(c6_00,f6A)),rinvsix),f6B),rinvsq00);
559
560             fscal            = fvdw;
561
562             /* Calculate temporary vectorial force */
563             tx               = _mm_mul_ps(fscal,dx00);
564             ty               = _mm_mul_ps(fscal,dy00);
565             tz               = _mm_mul_ps(fscal,dz00);
566
567             /* Update vectorial force */
568             fix0             = _mm_add_ps(fix0,tx);
569             fiy0             = _mm_add_ps(fiy0,ty);
570             fiz0             = _mm_add_ps(fiz0,tz);
571
572             fjptrA             = f+j_coord_offsetA;
573             fjptrB             = f+j_coord_offsetB;
574             fjptrC             = f+j_coord_offsetC;
575             fjptrD             = f+j_coord_offsetD;
576             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
577             
578             /* Inner loop uses 46 flops */
579         }
580
581         if(jidx<j_index_end)
582         {
583
584             /* Get j neighbor index, and coordinate index */
585             jnrlistA         = jjnr[jidx];
586             jnrlistB         = jjnr[jidx+1];
587             jnrlistC         = jjnr[jidx+2];
588             jnrlistD         = jjnr[jidx+3];
589             /* Sign of each element will be negative for non-real atoms.
590              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
591              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
592              */
593             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
594             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
595             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
596             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
597             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
598             j_coord_offsetA  = DIM*jnrA;
599             j_coord_offsetB  = DIM*jnrB;
600             j_coord_offsetC  = DIM*jnrC;
601             j_coord_offsetD  = DIM*jnrD;
602
603             /* load j atom coordinates */
604             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
605                                               x+j_coord_offsetC,x+j_coord_offsetD,
606                                               &jx0,&jy0,&jz0);
607
608             /* Calculate displacement vector */
609             dx00             = _mm_sub_ps(ix0,jx0);
610             dy00             = _mm_sub_ps(iy0,jy0);
611             dz00             = _mm_sub_ps(iz0,jz0);
612
613             /* Calculate squared distance and things based on it */
614             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
615
616             rinv00           = sse2_invsqrt_f(rsq00);
617
618             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
619
620             /* Load parameters for j particles */
621             vdwjidx0A        = 2*vdwtype[jnrA+0];
622             vdwjidx0B        = 2*vdwtype[jnrB+0];
623             vdwjidx0C        = 2*vdwtype[jnrC+0];
624             vdwjidx0D        = 2*vdwtype[jnrD+0];
625
626             /**************************
627              * CALCULATE INTERACTIONS *
628              **************************/
629
630             r00              = _mm_mul_ps(rsq00,rinv00);
631             r00              = _mm_andnot_ps(dummy_mask,r00);
632
633             /* Compute parameters for interactions between i and j atoms */
634             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
635                                          vdwparam+vdwioffset0+vdwjidx0B,
636                                          vdwparam+vdwioffset0+vdwjidx0C,
637                                          vdwparam+vdwioffset0+vdwjidx0D,
638                                          &c6_00,&c12_00);
639             c6grid_00       = gmx_mm_load_4real_swizzle_ps(vdwgridparam+vdwioffset0+vdwjidx0A,
640                                                                vdwgridparam+vdwioffset0+vdwjidx0B,
641                                                                vdwgridparam+vdwioffset0+vdwjidx0C,
642                                                                vdwgridparam+vdwioffset0+vdwjidx0D);
643
644             /* Analytical LJ-PME */
645             rinvsix          = _mm_mul_ps(_mm_mul_ps(rinvsq00,rinvsq00),rinvsq00);
646             ewcljrsq         = _mm_mul_ps(ewclj2,rsq00);
647             ewclj6           = _mm_mul_ps(ewclj2,_mm_mul_ps(ewclj2,ewclj2));
648             exponent         = sse2_exp_f(ewcljrsq);
649             /* poly = exp(-(beta*r)^2) * (1 + (beta*r)^2 + (beta*r)^4 /2) */
650             poly             = _mm_mul_ps(exponent,_mm_add_ps(_mm_sub_ps(one,ewcljrsq),_mm_mul_ps(_mm_mul_ps(ewcljrsq,ewcljrsq),one_half)));
651             /* f6A = 6 * C6grid * (1 - poly) */
652             f6A              = _mm_mul_ps(c6grid_00,_mm_sub_ps(one,poly));
653             /* f6B = C6grid * exponent * beta^6 */
654             f6B              = _mm_mul_ps(_mm_mul_ps(c6grid_00,one_sixth),_mm_mul_ps(exponent,ewclj6));
655             /* fvdw = 12*C12/r13 - ((6*C6 - f6A)/r6 + f6B)/r */
656             fvdw              = _mm_mul_ps(_mm_add_ps(_mm_mul_ps(_mm_sub_ps(_mm_mul_ps(c12_00,rinvsix),_mm_sub_ps(c6_00,f6A)),rinvsix),f6B),rinvsq00);
657
658             fscal            = fvdw;
659
660             fscal            = _mm_andnot_ps(dummy_mask,fscal);
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             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
673             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
674             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
675             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
676             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
677             
678             /* Inner loop uses 47 flops */
679         }
680
681         /* End of innermost loop */
682
683         gmx_mm_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
684                                               f+i_coord_offset,fshift+i_shift_offset);
685
686         /* Increment number of inner iterations */
687         inneriter                  += j_index_end - j_index_start;
688
689         /* Outer loop uses 6 flops */
690     }
691
692     /* Increment number of outer iterations */
693     outeriter        += nri;
694
695     /* Update outer/inner flops */
696
697     inc_nrnb(nrnb,eNR_NBKERNEL_VDW_F,outeriter*6 + inneriter*47);
698 }