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