c9387d377e5560212e6b70f2e4bae7ef16894ff4
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_sse2_single / nb_kernel_ElecEwSh_VdwLJSh_GeomW3P1_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 "types/simple.h"
44 #include "gromacs/math/vec.h"
45 #include "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_GeomW3P1_VF_sse2_single
52  * Electrostatics interaction: Ewald
53  * VdW interaction:            LennardJones
54  * Geometry:                   Water3-Particle
55  * Calculate force/pot:        PotentialAndForce
56  */
57 void
58 nb_kernel_ElecEwSh_VdwLJSh_GeomW3P1_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              vdwioffset1;
86     __m128           ix1,iy1,iz1,fix1,fiy1,fiz1,iq1,isai1;
87     int              vdwioffset2;
88     __m128           ix2,iy2,iz2,fix2,fiy2,fiz2,iq2,isai2;
89     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
90     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
91     __m128           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
92     __m128           dx10,dy10,dz10,rsq10,rinv10,rinvsq10,r10,qq10,c6_10,c12_10;
93     __m128           dx20,dy20,dz20,rsq20,rinv20,rinvsq20,r20,qq20,c6_20,c12_20;
94     __m128           velec,felec,velecsum,facel,crf,krf,krf2;
95     real             *charge;
96     int              nvdwtype;
97     __m128           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
98     int              *vdwtype;
99     real             *vdwparam;
100     __m128           one_sixth   = _mm_set1_ps(1.0/6.0);
101     __m128           one_twelfth = _mm_set1_ps(1.0/12.0);
102     __m128i          ewitab;
103     __m128           ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
104     real             *ewtab;
105     __m128           dummy_mask,cutoff_mask;
106     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
107     __m128           one     = _mm_set1_ps(1.0);
108     __m128           two     = _mm_set1_ps(2.0);
109     x                = xx[0];
110     f                = ff[0];
111
112     nri              = nlist->nri;
113     iinr             = nlist->iinr;
114     jindex           = nlist->jindex;
115     jjnr             = nlist->jjnr;
116     shiftidx         = nlist->shift;
117     gid              = nlist->gid;
118     shiftvec         = fr->shift_vec[0];
119     fshift           = fr->fshift[0];
120     facel            = _mm_set1_ps(fr->epsfac);
121     charge           = mdatoms->chargeA;
122     nvdwtype         = fr->ntype;
123     vdwparam         = fr->nbfp;
124     vdwtype          = mdatoms->typeA;
125
126     sh_ewald         = _mm_set1_ps(fr->ic->sh_ewald);
127     ewtab            = fr->ic->tabq_coul_FDV0;
128     ewtabscale       = _mm_set1_ps(fr->ic->tabq_scale);
129     ewtabhalfspace   = _mm_set1_ps(0.5/fr->ic->tabq_scale);
130
131     /* Setup water-specific parameters */
132     inr              = nlist->iinr[0];
133     iq0              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+0]));
134     iq1              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+1]));
135     iq2              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+2]));
136     vdwioffset0      = 2*nvdwtype*vdwtype[inr+0];
137
138     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
139     rcutoff_scalar   = fr->rcoulomb;
140     rcutoff          = _mm_set1_ps(rcutoff_scalar);
141     rcutoff2         = _mm_mul_ps(rcutoff,rcutoff);
142
143     sh_vdw_invrcut6  = _mm_set1_ps(fr->ic->sh_invrc6);
144     rvdw             = _mm_set1_ps(fr->rvdw);
145
146     /* Avoid stupid compiler warnings */
147     jnrA = jnrB = jnrC = jnrD = 0;
148     j_coord_offsetA = 0;
149     j_coord_offsetB = 0;
150     j_coord_offsetC = 0;
151     j_coord_offsetD = 0;
152
153     outeriter        = 0;
154     inneriter        = 0;
155
156     for(iidx=0;iidx<4*DIM;iidx++)
157     {
158         scratch[iidx] = 0.0;
159     }  
160
161     /* Start outer loop over neighborlists */
162     for(iidx=0; iidx<nri; iidx++)
163     {
164         /* Load shift vector for this list */
165         i_shift_offset   = DIM*shiftidx[iidx];
166
167         /* Load limits for loop over neighbors */
168         j_index_start    = jindex[iidx];
169         j_index_end      = jindex[iidx+1];
170
171         /* Get outer coordinate index */
172         inr              = iinr[iidx];
173         i_coord_offset   = DIM*inr;
174
175         /* Load i particle coords and add shift vector */
176         gmx_mm_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,
177                                                  &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2);
178         
179         fix0             = _mm_setzero_ps();
180         fiy0             = _mm_setzero_ps();
181         fiz0             = _mm_setzero_ps();
182         fix1             = _mm_setzero_ps();
183         fiy1             = _mm_setzero_ps();
184         fiz1             = _mm_setzero_ps();
185         fix2             = _mm_setzero_ps();
186         fiy2             = _mm_setzero_ps();
187         fiz2             = _mm_setzero_ps();
188
189         /* Reset potential sums */
190         velecsum         = _mm_setzero_ps();
191         vvdwsum          = _mm_setzero_ps();
192
193         /* Start inner kernel loop */
194         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
195         {
196
197             /* Get j neighbor index, and coordinate index */
198             jnrA             = jjnr[jidx];
199             jnrB             = jjnr[jidx+1];
200             jnrC             = jjnr[jidx+2];
201             jnrD             = jjnr[jidx+3];
202             j_coord_offsetA  = DIM*jnrA;
203             j_coord_offsetB  = DIM*jnrB;
204             j_coord_offsetC  = DIM*jnrC;
205             j_coord_offsetD  = DIM*jnrD;
206
207             /* load j atom coordinates */
208             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
209                                               x+j_coord_offsetC,x+j_coord_offsetD,
210                                               &jx0,&jy0,&jz0);
211
212             /* Calculate displacement vector */
213             dx00             = _mm_sub_ps(ix0,jx0);
214             dy00             = _mm_sub_ps(iy0,jy0);
215             dz00             = _mm_sub_ps(iz0,jz0);
216             dx10             = _mm_sub_ps(ix1,jx0);
217             dy10             = _mm_sub_ps(iy1,jy0);
218             dz10             = _mm_sub_ps(iz1,jz0);
219             dx20             = _mm_sub_ps(ix2,jx0);
220             dy20             = _mm_sub_ps(iy2,jy0);
221             dz20             = _mm_sub_ps(iz2,jz0);
222
223             /* Calculate squared distance and things based on it */
224             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
225             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
226             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
227
228             rinv00           = gmx_mm_invsqrt_ps(rsq00);
229             rinv10           = gmx_mm_invsqrt_ps(rsq10);
230             rinv20           = gmx_mm_invsqrt_ps(rsq20);
231
232             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
233             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
234             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
235
236             /* Load parameters for j particles */
237             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
238                                                               charge+jnrC+0,charge+jnrD+0);
239             vdwjidx0A        = 2*vdwtype[jnrA+0];
240             vdwjidx0B        = 2*vdwtype[jnrB+0];
241             vdwjidx0C        = 2*vdwtype[jnrC+0];
242             vdwjidx0D        = 2*vdwtype[jnrD+0];
243
244             fjx0             = _mm_setzero_ps();
245             fjy0             = _mm_setzero_ps();
246             fjz0             = _mm_setzero_ps();
247
248             /**************************
249              * CALCULATE INTERACTIONS *
250              **************************/
251
252             if (gmx_mm_any_lt(rsq00,rcutoff2))
253             {
254
255             r00              = _mm_mul_ps(rsq00,rinv00);
256
257             /* Compute parameters for interactions between i and j atoms */
258             qq00             = _mm_mul_ps(iq0,jq0);
259             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
260                                          vdwparam+vdwioffset0+vdwjidx0B,
261                                          vdwparam+vdwioffset0+vdwjidx0C,
262                                          vdwparam+vdwioffset0+vdwjidx0D,
263                                          &c6_00,&c12_00);
264
265             /* EWALD ELECTROSTATICS */
266
267             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
268             ewrt             = _mm_mul_ps(r00,ewtabscale);
269             ewitab           = _mm_cvttps_epi32(ewrt);
270             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
271             ewitab           = _mm_slli_epi32(ewitab,2);
272             ewtabF           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,0) );
273             ewtabD           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,1) );
274             ewtabV           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,2) );
275             ewtabFn          = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,3) );
276             _MM_TRANSPOSE4_PS(ewtabF,ewtabD,ewtabV,ewtabFn);
277             felec            = _mm_add_ps(ewtabF,_mm_mul_ps(eweps,ewtabD));
278             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
279             velec            = _mm_mul_ps(qq00,_mm_sub_ps(_mm_sub_ps(rinv00,sh_ewald),velec));
280             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
281
282             /* LENNARD-JONES DISPERSION/REPULSION */
283
284             rinvsix          = _mm_mul_ps(_mm_mul_ps(rinvsq00,rinvsq00),rinvsq00);
285             vvdw6            = _mm_mul_ps(c6_00,rinvsix);
286             vvdw12           = _mm_mul_ps(c12_00,_mm_mul_ps(rinvsix,rinvsix));
287             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) ,
288                                           _mm_mul_ps( _mm_sub_ps(vvdw6,_mm_mul_ps(c6_00,sh_vdw_invrcut6)),one_sixth));
289             fvdw             = _mm_mul_ps(_mm_sub_ps(vvdw12,vvdw6),rinvsq00);
290
291             cutoff_mask      = _mm_cmplt_ps(rsq00,rcutoff2);
292
293             /* Update potential sum for this i atom from the interaction with this j atom. */
294             velec            = _mm_and_ps(velec,cutoff_mask);
295             velecsum         = _mm_add_ps(velecsum,velec);
296             vvdw             = _mm_and_ps(vvdw,cutoff_mask);
297             vvdwsum          = _mm_add_ps(vvdwsum,vvdw);
298
299             fscal            = _mm_add_ps(felec,fvdw);
300
301             fscal            = _mm_and_ps(fscal,cutoff_mask);
302
303             /* Calculate temporary vectorial force */
304             tx               = _mm_mul_ps(fscal,dx00);
305             ty               = _mm_mul_ps(fscal,dy00);
306             tz               = _mm_mul_ps(fscal,dz00);
307
308             /* Update vectorial force */
309             fix0             = _mm_add_ps(fix0,tx);
310             fiy0             = _mm_add_ps(fiy0,ty);
311             fiz0             = _mm_add_ps(fiz0,tz);
312
313             fjx0             = _mm_add_ps(fjx0,tx);
314             fjy0             = _mm_add_ps(fjy0,ty);
315             fjz0             = _mm_add_ps(fjz0,tz);
316             
317             }
318
319             /**************************
320              * CALCULATE INTERACTIONS *
321              **************************/
322
323             if (gmx_mm_any_lt(rsq10,rcutoff2))
324             {
325
326             r10              = _mm_mul_ps(rsq10,rinv10);
327
328             /* Compute parameters for interactions between i and j atoms */
329             qq10             = _mm_mul_ps(iq1,jq0);
330
331             /* EWALD ELECTROSTATICS */
332
333             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
334             ewrt             = _mm_mul_ps(r10,ewtabscale);
335             ewitab           = _mm_cvttps_epi32(ewrt);
336             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
337             ewitab           = _mm_slli_epi32(ewitab,2);
338             ewtabF           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,0) );
339             ewtabD           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,1) );
340             ewtabV           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,2) );
341             ewtabFn          = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,3) );
342             _MM_TRANSPOSE4_PS(ewtabF,ewtabD,ewtabV,ewtabFn);
343             felec            = _mm_add_ps(ewtabF,_mm_mul_ps(eweps,ewtabD));
344             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
345             velec            = _mm_mul_ps(qq10,_mm_sub_ps(_mm_sub_ps(rinv10,sh_ewald),velec));
346             felec            = _mm_mul_ps(_mm_mul_ps(qq10,rinv10),_mm_sub_ps(rinvsq10,felec));
347
348             cutoff_mask      = _mm_cmplt_ps(rsq10,rcutoff2);
349
350             /* Update potential sum for this i atom from the interaction with this j atom. */
351             velec            = _mm_and_ps(velec,cutoff_mask);
352             velecsum         = _mm_add_ps(velecsum,velec);
353
354             fscal            = felec;
355
356             fscal            = _mm_and_ps(fscal,cutoff_mask);
357
358             /* Calculate temporary vectorial force */
359             tx               = _mm_mul_ps(fscal,dx10);
360             ty               = _mm_mul_ps(fscal,dy10);
361             tz               = _mm_mul_ps(fscal,dz10);
362
363             /* Update vectorial force */
364             fix1             = _mm_add_ps(fix1,tx);
365             fiy1             = _mm_add_ps(fiy1,ty);
366             fiz1             = _mm_add_ps(fiz1,tz);
367
368             fjx0             = _mm_add_ps(fjx0,tx);
369             fjy0             = _mm_add_ps(fjy0,ty);
370             fjz0             = _mm_add_ps(fjz0,tz);
371             
372             }
373
374             /**************************
375              * CALCULATE INTERACTIONS *
376              **************************/
377
378             if (gmx_mm_any_lt(rsq20,rcutoff2))
379             {
380
381             r20              = _mm_mul_ps(rsq20,rinv20);
382
383             /* Compute parameters for interactions between i and j atoms */
384             qq20             = _mm_mul_ps(iq2,jq0);
385
386             /* EWALD ELECTROSTATICS */
387
388             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
389             ewrt             = _mm_mul_ps(r20,ewtabscale);
390             ewitab           = _mm_cvttps_epi32(ewrt);
391             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
392             ewitab           = _mm_slli_epi32(ewitab,2);
393             ewtabF           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,0) );
394             ewtabD           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,1) );
395             ewtabV           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,2) );
396             ewtabFn          = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,3) );
397             _MM_TRANSPOSE4_PS(ewtabF,ewtabD,ewtabV,ewtabFn);
398             felec            = _mm_add_ps(ewtabF,_mm_mul_ps(eweps,ewtabD));
399             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
400             velec            = _mm_mul_ps(qq20,_mm_sub_ps(_mm_sub_ps(rinv20,sh_ewald),velec));
401             felec            = _mm_mul_ps(_mm_mul_ps(qq20,rinv20),_mm_sub_ps(rinvsq20,felec));
402
403             cutoff_mask      = _mm_cmplt_ps(rsq20,rcutoff2);
404
405             /* Update potential sum for this i atom from the interaction with this j atom. */
406             velec            = _mm_and_ps(velec,cutoff_mask);
407             velecsum         = _mm_add_ps(velecsum,velec);
408
409             fscal            = felec;
410
411             fscal            = _mm_and_ps(fscal,cutoff_mask);
412
413             /* Calculate temporary vectorial force */
414             tx               = _mm_mul_ps(fscal,dx20);
415             ty               = _mm_mul_ps(fscal,dy20);
416             tz               = _mm_mul_ps(fscal,dz20);
417
418             /* Update vectorial force */
419             fix2             = _mm_add_ps(fix2,tx);
420             fiy2             = _mm_add_ps(fiy2,ty);
421             fiz2             = _mm_add_ps(fiz2,tz);
422
423             fjx0             = _mm_add_ps(fjx0,tx);
424             fjy0             = _mm_add_ps(fjy0,ty);
425             fjz0             = _mm_add_ps(fjz0,tz);
426             
427             }
428
429             fjptrA             = f+j_coord_offsetA;
430             fjptrB             = f+j_coord_offsetB;
431             fjptrC             = f+j_coord_offsetC;
432             fjptrD             = f+j_coord_offsetD;
433
434             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
435
436             /* Inner loop uses 156 flops */
437         }
438
439         if(jidx<j_index_end)
440         {
441
442             /* Get j neighbor index, and coordinate index */
443             jnrlistA         = jjnr[jidx];
444             jnrlistB         = jjnr[jidx+1];
445             jnrlistC         = jjnr[jidx+2];
446             jnrlistD         = jjnr[jidx+3];
447             /* Sign of each element will be negative for non-real atoms.
448              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
449              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
450              */
451             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
452             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
453             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
454             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
455             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
456             j_coord_offsetA  = DIM*jnrA;
457             j_coord_offsetB  = DIM*jnrB;
458             j_coord_offsetC  = DIM*jnrC;
459             j_coord_offsetD  = DIM*jnrD;
460
461             /* load j atom coordinates */
462             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
463                                               x+j_coord_offsetC,x+j_coord_offsetD,
464                                               &jx0,&jy0,&jz0);
465
466             /* Calculate displacement vector */
467             dx00             = _mm_sub_ps(ix0,jx0);
468             dy00             = _mm_sub_ps(iy0,jy0);
469             dz00             = _mm_sub_ps(iz0,jz0);
470             dx10             = _mm_sub_ps(ix1,jx0);
471             dy10             = _mm_sub_ps(iy1,jy0);
472             dz10             = _mm_sub_ps(iz1,jz0);
473             dx20             = _mm_sub_ps(ix2,jx0);
474             dy20             = _mm_sub_ps(iy2,jy0);
475             dz20             = _mm_sub_ps(iz2,jz0);
476
477             /* Calculate squared distance and things based on it */
478             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
479             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
480             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
481
482             rinv00           = gmx_mm_invsqrt_ps(rsq00);
483             rinv10           = gmx_mm_invsqrt_ps(rsq10);
484             rinv20           = gmx_mm_invsqrt_ps(rsq20);
485
486             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
487             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
488             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
489
490             /* Load parameters for j particles */
491             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
492                                                               charge+jnrC+0,charge+jnrD+0);
493             vdwjidx0A        = 2*vdwtype[jnrA+0];
494             vdwjidx0B        = 2*vdwtype[jnrB+0];
495             vdwjidx0C        = 2*vdwtype[jnrC+0];
496             vdwjidx0D        = 2*vdwtype[jnrD+0];
497
498             fjx0             = _mm_setzero_ps();
499             fjy0             = _mm_setzero_ps();
500             fjz0             = _mm_setzero_ps();
501
502             /**************************
503              * CALCULATE INTERACTIONS *
504              **************************/
505
506             if (gmx_mm_any_lt(rsq00,rcutoff2))
507             {
508
509             r00              = _mm_mul_ps(rsq00,rinv00);
510             r00              = _mm_andnot_ps(dummy_mask,r00);
511
512             /* Compute parameters for interactions between i and j atoms */
513             qq00             = _mm_mul_ps(iq0,jq0);
514             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
515                                          vdwparam+vdwioffset0+vdwjidx0B,
516                                          vdwparam+vdwioffset0+vdwjidx0C,
517                                          vdwparam+vdwioffset0+vdwjidx0D,
518                                          &c6_00,&c12_00);
519
520             /* EWALD ELECTROSTATICS */
521
522             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
523             ewrt             = _mm_mul_ps(r00,ewtabscale);
524             ewitab           = _mm_cvttps_epi32(ewrt);
525             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
526             ewitab           = _mm_slli_epi32(ewitab,2);
527             ewtabF           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,0) );
528             ewtabD           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,1) );
529             ewtabV           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,2) );
530             ewtabFn          = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,3) );
531             _MM_TRANSPOSE4_PS(ewtabF,ewtabD,ewtabV,ewtabFn);
532             felec            = _mm_add_ps(ewtabF,_mm_mul_ps(eweps,ewtabD));
533             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
534             velec            = _mm_mul_ps(qq00,_mm_sub_ps(_mm_sub_ps(rinv00,sh_ewald),velec));
535             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
536
537             /* LENNARD-JONES DISPERSION/REPULSION */
538
539             rinvsix          = _mm_mul_ps(_mm_mul_ps(rinvsq00,rinvsq00),rinvsq00);
540             vvdw6            = _mm_mul_ps(c6_00,rinvsix);
541             vvdw12           = _mm_mul_ps(c12_00,_mm_mul_ps(rinvsix,rinvsix));
542             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) ,
543                                           _mm_mul_ps( _mm_sub_ps(vvdw6,_mm_mul_ps(c6_00,sh_vdw_invrcut6)),one_sixth));
544             fvdw             = _mm_mul_ps(_mm_sub_ps(vvdw12,vvdw6),rinvsq00);
545
546             cutoff_mask      = _mm_cmplt_ps(rsq00,rcutoff2);
547
548             /* Update potential sum for this i atom from the interaction with this j atom. */
549             velec            = _mm_and_ps(velec,cutoff_mask);
550             velec            = _mm_andnot_ps(dummy_mask,velec);
551             velecsum         = _mm_add_ps(velecsum,velec);
552             vvdw             = _mm_and_ps(vvdw,cutoff_mask);
553             vvdw             = _mm_andnot_ps(dummy_mask,vvdw);
554             vvdwsum          = _mm_add_ps(vvdwsum,vvdw);
555
556             fscal            = _mm_add_ps(felec,fvdw);
557
558             fscal            = _mm_and_ps(fscal,cutoff_mask);
559
560             fscal            = _mm_andnot_ps(dummy_mask,fscal);
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             fjx0             = _mm_add_ps(fjx0,tx);
573             fjy0             = _mm_add_ps(fjy0,ty);
574             fjz0             = _mm_add_ps(fjz0,tz);
575             
576             }
577
578             /**************************
579              * CALCULATE INTERACTIONS *
580              **************************/
581
582             if (gmx_mm_any_lt(rsq10,rcutoff2))
583             {
584
585             r10              = _mm_mul_ps(rsq10,rinv10);
586             r10              = _mm_andnot_ps(dummy_mask,r10);
587
588             /* Compute parameters for interactions between i and j atoms */
589             qq10             = _mm_mul_ps(iq1,jq0);
590
591             /* EWALD ELECTROSTATICS */
592
593             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
594             ewrt             = _mm_mul_ps(r10,ewtabscale);
595             ewitab           = _mm_cvttps_epi32(ewrt);
596             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
597             ewitab           = _mm_slli_epi32(ewitab,2);
598             ewtabF           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,0) );
599             ewtabD           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,1) );
600             ewtabV           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,2) );
601             ewtabFn          = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,3) );
602             _MM_TRANSPOSE4_PS(ewtabF,ewtabD,ewtabV,ewtabFn);
603             felec            = _mm_add_ps(ewtabF,_mm_mul_ps(eweps,ewtabD));
604             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
605             velec            = _mm_mul_ps(qq10,_mm_sub_ps(_mm_sub_ps(rinv10,sh_ewald),velec));
606             felec            = _mm_mul_ps(_mm_mul_ps(qq10,rinv10),_mm_sub_ps(rinvsq10,felec));
607
608             cutoff_mask      = _mm_cmplt_ps(rsq10,rcutoff2);
609
610             /* Update potential sum for this i atom from the interaction with this j atom. */
611             velec            = _mm_and_ps(velec,cutoff_mask);
612             velec            = _mm_andnot_ps(dummy_mask,velec);
613             velecsum         = _mm_add_ps(velecsum,velec);
614
615             fscal            = felec;
616
617             fscal            = _mm_and_ps(fscal,cutoff_mask);
618
619             fscal            = _mm_andnot_ps(dummy_mask,fscal);
620
621             /* Calculate temporary vectorial force */
622             tx               = _mm_mul_ps(fscal,dx10);
623             ty               = _mm_mul_ps(fscal,dy10);
624             tz               = _mm_mul_ps(fscal,dz10);
625
626             /* Update vectorial force */
627             fix1             = _mm_add_ps(fix1,tx);
628             fiy1             = _mm_add_ps(fiy1,ty);
629             fiz1             = _mm_add_ps(fiz1,tz);
630
631             fjx0             = _mm_add_ps(fjx0,tx);
632             fjy0             = _mm_add_ps(fjy0,ty);
633             fjz0             = _mm_add_ps(fjz0,tz);
634             
635             }
636
637             /**************************
638              * CALCULATE INTERACTIONS *
639              **************************/
640
641             if (gmx_mm_any_lt(rsq20,rcutoff2))
642             {
643
644             r20              = _mm_mul_ps(rsq20,rinv20);
645             r20              = _mm_andnot_ps(dummy_mask,r20);
646
647             /* Compute parameters for interactions between i and j atoms */
648             qq20             = _mm_mul_ps(iq2,jq0);
649
650             /* EWALD ELECTROSTATICS */
651
652             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
653             ewrt             = _mm_mul_ps(r20,ewtabscale);
654             ewitab           = _mm_cvttps_epi32(ewrt);
655             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
656             ewitab           = _mm_slli_epi32(ewitab,2);
657             ewtabF           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,0) );
658             ewtabD           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,1) );
659             ewtabV           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,2) );
660             ewtabFn          = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,3) );
661             _MM_TRANSPOSE4_PS(ewtabF,ewtabD,ewtabV,ewtabFn);
662             felec            = _mm_add_ps(ewtabF,_mm_mul_ps(eweps,ewtabD));
663             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
664             velec            = _mm_mul_ps(qq20,_mm_sub_ps(_mm_sub_ps(rinv20,sh_ewald),velec));
665             felec            = _mm_mul_ps(_mm_mul_ps(qq20,rinv20),_mm_sub_ps(rinvsq20,felec));
666
667             cutoff_mask      = _mm_cmplt_ps(rsq20,rcutoff2);
668
669             /* Update potential sum for this i atom from the interaction with this j atom. */
670             velec            = _mm_and_ps(velec,cutoff_mask);
671             velec            = _mm_andnot_ps(dummy_mask,velec);
672             velecsum         = _mm_add_ps(velecsum,velec);
673
674             fscal            = felec;
675
676             fscal            = _mm_and_ps(fscal,cutoff_mask);
677
678             fscal            = _mm_andnot_ps(dummy_mask,fscal);
679
680             /* Calculate temporary vectorial force */
681             tx               = _mm_mul_ps(fscal,dx20);
682             ty               = _mm_mul_ps(fscal,dy20);
683             tz               = _mm_mul_ps(fscal,dz20);
684
685             /* Update vectorial force */
686             fix2             = _mm_add_ps(fix2,tx);
687             fiy2             = _mm_add_ps(fiy2,ty);
688             fiz2             = _mm_add_ps(fiz2,tz);
689
690             fjx0             = _mm_add_ps(fjx0,tx);
691             fjy0             = _mm_add_ps(fjy0,ty);
692             fjz0             = _mm_add_ps(fjz0,tz);
693             
694             }
695
696             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
697             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
698             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
699             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
700
701             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
702
703             /* Inner loop uses 159 flops */
704         }
705
706         /* End of innermost loop */
707
708         gmx_mm_update_iforce_3atom_swizzle_ps(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,
709                                               f+i_coord_offset,fshift+i_shift_offset);
710
711         ggid                        = gid[iidx];
712         /* Update potential energies */
713         gmx_mm_update_1pot_ps(velecsum,kernel_data->energygrp_elec+ggid);
714         gmx_mm_update_1pot_ps(vvdwsum,kernel_data->energygrp_vdw+ggid);
715
716         /* Increment number of inner iterations */
717         inneriter                  += j_index_end - j_index_start;
718
719         /* Outer loop uses 20 flops */
720     }
721
722     /* Increment number of outer iterations */
723     outeriter        += nri;
724
725     /* Update outer/inner flops */
726
727     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_W3_VF,outeriter*20 + inneriter*159);
728 }
729 /*
730  * Gromacs nonbonded kernel:   nb_kernel_ElecEwSh_VdwLJSh_GeomW3P1_F_sse2_single
731  * Electrostatics interaction: Ewald
732  * VdW interaction:            LennardJones
733  * Geometry:                   Water3-Particle
734  * Calculate force/pot:        Force
735  */
736 void
737 nb_kernel_ElecEwSh_VdwLJSh_GeomW3P1_F_sse2_single
738                     (t_nblist                    * gmx_restrict       nlist,
739                      rvec                        * gmx_restrict          xx,
740                      rvec                        * gmx_restrict          ff,
741                      t_forcerec                  * gmx_restrict          fr,
742                      t_mdatoms                   * gmx_restrict     mdatoms,
743                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
744                      t_nrnb                      * gmx_restrict        nrnb)
745 {
746     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
747      * just 0 for non-waters.
748      * Suffixes A,B,C,D refer to j loop unrolling done with SSE, e.g. for the four different
749      * jnr indices corresponding to data put in the four positions in the SIMD register.
750      */
751     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
752     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
753     int              jnrA,jnrB,jnrC,jnrD;
754     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
755     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
756     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
757     real             rcutoff_scalar;
758     real             *shiftvec,*fshift,*x,*f;
759     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
760     real             scratch[4*DIM];
761     __m128           tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
762     int              vdwioffset0;
763     __m128           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
764     int              vdwioffset1;
765     __m128           ix1,iy1,iz1,fix1,fiy1,fiz1,iq1,isai1;
766     int              vdwioffset2;
767     __m128           ix2,iy2,iz2,fix2,fiy2,fiz2,iq2,isai2;
768     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
769     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
770     __m128           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
771     __m128           dx10,dy10,dz10,rsq10,rinv10,rinvsq10,r10,qq10,c6_10,c12_10;
772     __m128           dx20,dy20,dz20,rsq20,rinv20,rinvsq20,r20,qq20,c6_20,c12_20;
773     __m128           velec,felec,velecsum,facel,crf,krf,krf2;
774     real             *charge;
775     int              nvdwtype;
776     __m128           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
777     int              *vdwtype;
778     real             *vdwparam;
779     __m128           one_sixth   = _mm_set1_ps(1.0/6.0);
780     __m128           one_twelfth = _mm_set1_ps(1.0/12.0);
781     __m128i          ewitab;
782     __m128           ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
783     real             *ewtab;
784     __m128           dummy_mask,cutoff_mask;
785     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
786     __m128           one     = _mm_set1_ps(1.0);
787     __m128           two     = _mm_set1_ps(2.0);
788     x                = xx[0];
789     f                = ff[0];
790
791     nri              = nlist->nri;
792     iinr             = nlist->iinr;
793     jindex           = nlist->jindex;
794     jjnr             = nlist->jjnr;
795     shiftidx         = nlist->shift;
796     gid              = nlist->gid;
797     shiftvec         = fr->shift_vec[0];
798     fshift           = fr->fshift[0];
799     facel            = _mm_set1_ps(fr->epsfac);
800     charge           = mdatoms->chargeA;
801     nvdwtype         = fr->ntype;
802     vdwparam         = fr->nbfp;
803     vdwtype          = mdatoms->typeA;
804
805     sh_ewald         = _mm_set1_ps(fr->ic->sh_ewald);
806     ewtab            = fr->ic->tabq_coul_F;
807     ewtabscale       = _mm_set1_ps(fr->ic->tabq_scale);
808     ewtabhalfspace   = _mm_set1_ps(0.5/fr->ic->tabq_scale);
809
810     /* Setup water-specific parameters */
811     inr              = nlist->iinr[0];
812     iq0              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+0]));
813     iq1              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+1]));
814     iq2              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+2]));
815     vdwioffset0      = 2*nvdwtype*vdwtype[inr+0];
816
817     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
818     rcutoff_scalar   = fr->rcoulomb;
819     rcutoff          = _mm_set1_ps(rcutoff_scalar);
820     rcutoff2         = _mm_mul_ps(rcutoff,rcutoff);
821
822     sh_vdw_invrcut6  = _mm_set1_ps(fr->ic->sh_invrc6);
823     rvdw             = _mm_set1_ps(fr->rvdw);
824
825     /* Avoid stupid compiler warnings */
826     jnrA = jnrB = jnrC = jnrD = 0;
827     j_coord_offsetA = 0;
828     j_coord_offsetB = 0;
829     j_coord_offsetC = 0;
830     j_coord_offsetD = 0;
831
832     outeriter        = 0;
833     inneriter        = 0;
834
835     for(iidx=0;iidx<4*DIM;iidx++)
836     {
837         scratch[iidx] = 0.0;
838     }  
839
840     /* Start outer loop over neighborlists */
841     for(iidx=0; iidx<nri; iidx++)
842     {
843         /* Load shift vector for this list */
844         i_shift_offset   = DIM*shiftidx[iidx];
845
846         /* Load limits for loop over neighbors */
847         j_index_start    = jindex[iidx];
848         j_index_end      = jindex[iidx+1];
849
850         /* Get outer coordinate index */
851         inr              = iinr[iidx];
852         i_coord_offset   = DIM*inr;
853
854         /* Load i particle coords and add shift vector */
855         gmx_mm_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,
856                                                  &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2);
857         
858         fix0             = _mm_setzero_ps();
859         fiy0             = _mm_setzero_ps();
860         fiz0             = _mm_setzero_ps();
861         fix1             = _mm_setzero_ps();
862         fiy1             = _mm_setzero_ps();
863         fiz1             = _mm_setzero_ps();
864         fix2             = _mm_setzero_ps();
865         fiy2             = _mm_setzero_ps();
866         fiz2             = _mm_setzero_ps();
867
868         /* Start inner kernel loop */
869         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
870         {
871
872             /* Get j neighbor index, and coordinate index */
873             jnrA             = jjnr[jidx];
874             jnrB             = jjnr[jidx+1];
875             jnrC             = jjnr[jidx+2];
876             jnrD             = jjnr[jidx+3];
877             j_coord_offsetA  = DIM*jnrA;
878             j_coord_offsetB  = DIM*jnrB;
879             j_coord_offsetC  = DIM*jnrC;
880             j_coord_offsetD  = DIM*jnrD;
881
882             /* load j atom coordinates */
883             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
884                                               x+j_coord_offsetC,x+j_coord_offsetD,
885                                               &jx0,&jy0,&jz0);
886
887             /* Calculate displacement vector */
888             dx00             = _mm_sub_ps(ix0,jx0);
889             dy00             = _mm_sub_ps(iy0,jy0);
890             dz00             = _mm_sub_ps(iz0,jz0);
891             dx10             = _mm_sub_ps(ix1,jx0);
892             dy10             = _mm_sub_ps(iy1,jy0);
893             dz10             = _mm_sub_ps(iz1,jz0);
894             dx20             = _mm_sub_ps(ix2,jx0);
895             dy20             = _mm_sub_ps(iy2,jy0);
896             dz20             = _mm_sub_ps(iz2,jz0);
897
898             /* Calculate squared distance and things based on it */
899             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
900             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
901             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
902
903             rinv00           = gmx_mm_invsqrt_ps(rsq00);
904             rinv10           = gmx_mm_invsqrt_ps(rsq10);
905             rinv20           = gmx_mm_invsqrt_ps(rsq20);
906
907             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
908             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
909             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
910
911             /* Load parameters for j particles */
912             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
913                                                               charge+jnrC+0,charge+jnrD+0);
914             vdwjidx0A        = 2*vdwtype[jnrA+0];
915             vdwjidx0B        = 2*vdwtype[jnrB+0];
916             vdwjidx0C        = 2*vdwtype[jnrC+0];
917             vdwjidx0D        = 2*vdwtype[jnrD+0];
918
919             fjx0             = _mm_setzero_ps();
920             fjy0             = _mm_setzero_ps();
921             fjz0             = _mm_setzero_ps();
922
923             /**************************
924              * CALCULATE INTERACTIONS *
925              **************************/
926
927             if (gmx_mm_any_lt(rsq00,rcutoff2))
928             {
929
930             r00              = _mm_mul_ps(rsq00,rinv00);
931
932             /* Compute parameters for interactions between i and j atoms */
933             qq00             = _mm_mul_ps(iq0,jq0);
934             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
935                                          vdwparam+vdwioffset0+vdwjidx0B,
936                                          vdwparam+vdwioffset0+vdwjidx0C,
937                                          vdwparam+vdwioffset0+vdwjidx0D,
938                                          &c6_00,&c12_00);
939
940             /* EWALD ELECTROSTATICS */
941
942             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
943             ewrt             = _mm_mul_ps(r00,ewtabscale);
944             ewitab           = _mm_cvttps_epi32(ewrt);
945             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
946             gmx_mm_load_4pair_swizzle_ps(ewtab+gmx_mm_extract_epi32(ewitab,0),ewtab+gmx_mm_extract_epi32(ewitab,1),
947                                          ewtab+gmx_mm_extract_epi32(ewitab,2),ewtab+gmx_mm_extract_epi32(ewitab,3),
948                                          &ewtabF,&ewtabFn);
949             felec            = _mm_add_ps(_mm_mul_ps( _mm_sub_ps(one,eweps),ewtabF),_mm_mul_ps(eweps,ewtabFn));
950             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
951
952             /* LENNARD-JONES DISPERSION/REPULSION */
953
954             rinvsix          = _mm_mul_ps(_mm_mul_ps(rinvsq00,rinvsq00),rinvsq00);
955             fvdw             = _mm_mul_ps(_mm_sub_ps(_mm_mul_ps(c12_00,rinvsix),c6_00),_mm_mul_ps(rinvsix,rinvsq00));
956
957             cutoff_mask      = _mm_cmplt_ps(rsq00,rcutoff2);
958
959             fscal            = _mm_add_ps(felec,fvdw);
960
961             fscal            = _mm_and_ps(fscal,cutoff_mask);
962
963             /* Calculate temporary vectorial force */
964             tx               = _mm_mul_ps(fscal,dx00);
965             ty               = _mm_mul_ps(fscal,dy00);
966             tz               = _mm_mul_ps(fscal,dz00);
967
968             /* Update vectorial force */
969             fix0             = _mm_add_ps(fix0,tx);
970             fiy0             = _mm_add_ps(fiy0,ty);
971             fiz0             = _mm_add_ps(fiz0,tz);
972
973             fjx0             = _mm_add_ps(fjx0,tx);
974             fjy0             = _mm_add_ps(fjy0,ty);
975             fjz0             = _mm_add_ps(fjz0,tz);
976             
977             }
978
979             /**************************
980              * CALCULATE INTERACTIONS *
981              **************************/
982
983             if (gmx_mm_any_lt(rsq10,rcutoff2))
984             {
985
986             r10              = _mm_mul_ps(rsq10,rinv10);
987
988             /* Compute parameters for interactions between i and j atoms */
989             qq10             = _mm_mul_ps(iq1,jq0);
990
991             /* EWALD ELECTROSTATICS */
992
993             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
994             ewrt             = _mm_mul_ps(r10,ewtabscale);
995             ewitab           = _mm_cvttps_epi32(ewrt);
996             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
997             gmx_mm_load_4pair_swizzle_ps(ewtab+gmx_mm_extract_epi32(ewitab,0),ewtab+gmx_mm_extract_epi32(ewitab,1),
998                                          ewtab+gmx_mm_extract_epi32(ewitab,2),ewtab+gmx_mm_extract_epi32(ewitab,3),
999                                          &ewtabF,&ewtabFn);
1000             felec            = _mm_add_ps(_mm_mul_ps( _mm_sub_ps(one,eweps),ewtabF),_mm_mul_ps(eweps,ewtabFn));
1001             felec            = _mm_mul_ps(_mm_mul_ps(qq10,rinv10),_mm_sub_ps(rinvsq10,felec));
1002
1003             cutoff_mask      = _mm_cmplt_ps(rsq10,rcutoff2);
1004
1005             fscal            = felec;
1006
1007             fscal            = _mm_and_ps(fscal,cutoff_mask);
1008
1009             /* Calculate temporary vectorial force */
1010             tx               = _mm_mul_ps(fscal,dx10);
1011             ty               = _mm_mul_ps(fscal,dy10);
1012             tz               = _mm_mul_ps(fscal,dz10);
1013
1014             /* Update vectorial force */
1015             fix1             = _mm_add_ps(fix1,tx);
1016             fiy1             = _mm_add_ps(fiy1,ty);
1017             fiz1             = _mm_add_ps(fiz1,tz);
1018
1019             fjx0             = _mm_add_ps(fjx0,tx);
1020             fjy0             = _mm_add_ps(fjy0,ty);
1021             fjz0             = _mm_add_ps(fjz0,tz);
1022             
1023             }
1024
1025             /**************************
1026              * CALCULATE INTERACTIONS *
1027              **************************/
1028
1029             if (gmx_mm_any_lt(rsq20,rcutoff2))
1030             {
1031
1032             r20              = _mm_mul_ps(rsq20,rinv20);
1033
1034             /* Compute parameters for interactions between i and j atoms */
1035             qq20             = _mm_mul_ps(iq2,jq0);
1036
1037             /* EWALD ELECTROSTATICS */
1038
1039             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
1040             ewrt             = _mm_mul_ps(r20,ewtabscale);
1041             ewitab           = _mm_cvttps_epi32(ewrt);
1042             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
1043             gmx_mm_load_4pair_swizzle_ps(ewtab+gmx_mm_extract_epi32(ewitab,0),ewtab+gmx_mm_extract_epi32(ewitab,1),
1044                                          ewtab+gmx_mm_extract_epi32(ewitab,2),ewtab+gmx_mm_extract_epi32(ewitab,3),
1045                                          &ewtabF,&ewtabFn);
1046             felec            = _mm_add_ps(_mm_mul_ps( _mm_sub_ps(one,eweps),ewtabF),_mm_mul_ps(eweps,ewtabFn));
1047             felec            = _mm_mul_ps(_mm_mul_ps(qq20,rinv20),_mm_sub_ps(rinvsq20,felec));
1048
1049             cutoff_mask      = _mm_cmplt_ps(rsq20,rcutoff2);
1050
1051             fscal            = felec;
1052
1053             fscal            = _mm_and_ps(fscal,cutoff_mask);
1054
1055             /* Calculate temporary vectorial force */
1056             tx               = _mm_mul_ps(fscal,dx20);
1057             ty               = _mm_mul_ps(fscal,dy20);
1058             tz               = _mm_mul_ps(fscal,dz20);
1059
1060             /* Update vectorial force */
1061             fix2             = _mm_add_ps(fix2,tx);
1062             fiy2             = _mm_add_ps(fiy2,ty);
1063             fiz2             = _mm_add_ps(fiz2,tz);
1064
1065             fjx0             = _mm_add_ps(fjx0,tx);
1066             fjy0             = _mm_add_ps(fjy0,ty);
1067             fjz0             = _mm_add_ps(fjz0,tz);
1068             
1069             }
1070
1071             fjptrA             = f+j_coord_offsetA;
1072             fjptrB             = f+j_coord_offsetB;
1073             fjptrC             = f+j_coord_offsetC;
1074             fjptrD             = f+j_coord_offsetD;
1075
1076             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
1077
1078             /* Inner loop uses 124 flops */
1079         }
1080
1081         if(jidx<j_index_end)
1082         {
1083
1084             /* Get j neighbor index, and coordinate index */
1085             jnrlistA         = jjnr[jidx];
1086             jnrlistB         = jjnr[jidx+1];
1087             jnrlistC         = jjnr[jidx+2];
1088             jnrlistD         = jjnr[jidx+3];
1089             /* Sign of each element will be negative for non-real atoms.
1090              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
1091              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
1092              */
1093             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
1094             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
1095             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
1096             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
1097             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
1098             j_coord_offsetA  = DIM*jnrA;
1099             j_coord_offsetB  = DIM*jnrB;
1100             j_coord_offsetC  = DIM*jnrC;
1101             j_coord_offsetD  = DIM*jnrD;
1102
1103             /* load j atom coordinates */
1104             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
1105                                               x+j_coord_offsetC,x+j_coord_offsetD,
1106                                               &jx0,&jy0,&jz0);
1107
1108             /* Calculate displacement vector */
1109             dx00             = _mm_sub_ps(ix0,jx0);
1110             dy00             = _mm_sub_ps(iy0,jy0);
1111             dz00             = _mm_sub_ps(iz0,jz0);
1112             dx10             = _mm_sub_ps(ix1,jx0);
1113             dy10             = _mm_sub_ps(iy1,jy0);
1114             dz10             = _mm_sub_ps(iz1,jz0);
1115             dx20             = _mm_sub_ps(ix2,jx0);
1116             dy20             = _mm_sub_ps(iy2,jy0);
1117             dz20             = _mm_sub_ps(iz2,jz0);
1118
1119             /* Calculate squared distance and things based on it */
1120             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
1121             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
1122             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
1123
1124             rinv00           = gmx_mm_invsqrt_ps(rsq00);
1125             rinv10           = gmx_mm_invsqrt_ps(rsq10);
1126             rinv20           = gmx_mm_invsqrt_ps(rsq20);
1127
1128             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
1129             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
1130             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
1131
1132             /* Load parameters for j particles */
1133             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
1134                                                               charge+jnrC+0,charge+jnrD+0);
1135             vdwjidx0A        = 2*vdwtype[jnrA+0];
1136             vdwjidx0B        = 2*vdwtype[jnrB+0];
1137             vdwjidx0C        = 2*vdwtype[jnrC+0];
1138             vdwjidx0D        = 2*vdwtype[jnrD+0];
1139
1140             fjx0             = _mm_setzero_ps();
1141             fjy0             = _mm_setzero_ps();
1142             fjz0             = _mm_setzero_ps();
1143
1144             /**************************
1145              * CALCULATE INTERACTIONS *
1146              **************************/
1147
1148             if (gmx_mm_any_lt(rsq00,rcutoff2))
1149             {
1150
1151             r00              = _mm_mul_ps(rsq00,rinv00);
1152             r00              = _mm_andnot_ps(dummy_mask,r00);
1153
1154             /* Compute parameters for interactions between i and j atoms */
1155             qq00             = _mm_mul_ps(iq0,jq0);
1156             gmx_mm_load_4pair_swizzle_ps(vdwparam+vdwioffset0+vdwjidx0A,
1157                                          vdwparam+vdwioffset0+vdwjidx0B,
1158                                          vdwparam+vdwioffset0+vdwjidx0C,
1159                                          vdwparam+vdwioffset0+vdwjidx0D,
1160                                          &c6_00,&c12_00);
1161
1162             /* EWALD ELECTROSTATICS */
1163
1164             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
1165             ewrt             = _mm_mul_ps(r00,ewtabscale);
1166             ewitab           = _mm_cvttps_epi32(ewrt);
1167             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
1168             gmx_mm_load_4pair_swizzle_ps(ewtab+gmx_mm_extract_epi32(ewitab,0),ewtab+gmx_mm_extract_epi32(ewitab,1),
1169                                          ewtab+gmx_mm_extract_epi32(ewitab,2),ewtab+gmx_mm_extract_epi32(ewitab,3),
1170                                          &ewtabF,&ewtabFn);
1171             felec            = _mm_add_ps(_mm_mul_ps( _mm_sub_ps(one,eweps),ewtabF),_mm_mul_ps(eweps,ewtabFn));
1172             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
1173
1174             /* LENNARD-JONES DISPERSION/REPULSION */
1175
1176             rinvsix          = _mm_mul_ps(_mm_mul_ps(rinvsq00,rinvsq00),rinvsq00);
1177             fvdw             = _mm_mul_ps(_mm_sub_ps(_mm_mul_ps(c12_00,rinvsix),c6_00),_mm_mul_ps(rinvsix,rinvsq00));
1178
1179             cutoff_mask      = _mm_cmplt_ps(rsq00,rcutoff2);
1180
1181             fscal            = _mm_add_ps(felec,fvdw);
1182
1183             fscal            = _mm_and_ps(fscal,cutoff_mask);
1184
1185             fscal            = _mm_andnot_ps(dummy_mask,fscal);
1186
1187             /* Calculate temporary vectorial force */
1188             tx               = _mm_mul_ps(fscal,dx00);
1189             ty               = _mm_mul_ps(fscal,dy00);
1190             tz               = _mm_mul_ps(fscal,dz00);
1191
1192             /* Update vectorial force */
1193             fix0             = _mm_add_ps(fix0,tx);
1194             fiy0             = _mm_add_ps(fiy0,ty);
1195             fiz0             = _mm_add_ps(fiz0,tz);
1196
1197             fjx0             = _mm_add_ps(fjx0,tx);
1198             fjy0             = _mm_add_ps(fjy0,ty);
1199             fjz0             = _mm_add_ps(fjz0,tz);
1200             
1201             }
1202
1203             /**************************
1204              * CALCULATE INTERACTIONS *
1205              **************************/
1206
1207             if (gmx_mm_any_lt(rsq10,rcutoff2))
1208             {
1209
1210             r10              = _mm_mul_ps(rsq10,rinv10);
1211             r10              = _mm_andnot_ps(dummy_mask,r10);
1212
1213             /* Compute parameters for interactions between i and j atoms */
1214             qq10             = _mm_mul_ps(iq1,jq0);
1215
1216             /* EWALD ELECTROSTATICS */
1217
1218             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
1219             ewrt             = _mm_mul_ps(r10,ewtabscale);
1220             ewitab           = _mm_cvttps_epi32(ewrt);
1221             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
1222             gmx_mm_load_4pair_swizzle_ps(ewtab+gmx_mm_extract_epi32(ewitab,0),ewtab+gmx_mm_extract_epi32(ewitab,1),
1223                                          ewtab+gmx_mm_extract_epi32(ewitab,2),ewtab+gmx_mm_extract_epi32(ewitab,3),
1224                                          &ewtabF,&ewtabFn);
1225             felec            = _mm_add_ps(_mm_mul_ps( _mm_sub_ps(one,eweps),ewtabF),_mm_mul_ps(eweps,ewtabFn));
1226             felec            = _mm_mul_ps(_mm_mul_ps(qq10,rinv10),_mm_sub_ps(rinvsq10,felec));
1227
1228             cutoff_mask      = _mm_cmplt_ps(rsq10,rcutoff2);
1229
1230             fscal            = felec;
1231
1232             fscal            = _mm_and_ps(fscal,cutoff_mask);
1233
1234             fscal            = _mm_andnot_ps(dummy_mask,fscal);
1235
1236             /* Calculate temporary vectorial force */
1237             tx               = _mm_mul_ps(fscal,dx10);
1238             ty               = _mm_mul_ps(fscal,dy10);
1239             tz               = _mm_mul_ps(fscal,dz10);
1240
1241             /* Update vectorial force */
1242             fix1             = _mm_add_ps(fix1,tx);
1243             fiy1             = _mm_add_ps(fiy1,ty);
1244             fiz1             = _mm_add_ps(fiz1,tz);
1245
1246             fjx0             = _mm_add_ps(fjx0,tx);
1247             fjy0             = _mm_add_ps(fjy0,ty);
1248             fjz0             = _mm_add_ps(fjz0,tz);
1249             
1250             }
1251
1252             /**************************
1253              * CALCULATE INTERACTIONS *
1254              **************************/
1255
1256             if (gmx_mm_any_lt(rsq20,rcutoff2))
1257             {
1258
1259             r20              = _mm_mul_ps(rsq20,rinv20);
1260             r20              = _mm_andnot_ps(dummy_mask,r20);
1261
1262             /* Compute parameters for interactions between i and j atoms */
1263             qq20             = _mm_mul_ps(iq2,jq0);
1264
1265             /* EWALD ELECTROSTATICS */
1266
1267             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
1268             ewrt             = _mm_mul_ps(r20,ewtabscale);
1269             ewitab           = _mm_cvttps_epi32(ewrt);
1270             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
1271             gmx_mm_load_4pair_swizzle_ps(ewtab+gmx_mm_extract_epi32(ewitab,0),ewtab+gmx_mm_extract_epi32(ewitab,1),
1272                                          ewtab+gmx_mm_extract_epi32(ewitab,2),ewtab+gmx_mm_extract_epi32(ewitab,3),
1273                                          &ewtabF,&ewtabFn);
1274             felec            = _mm_add_ps(_mm_mul_ps( _mm_sub_ps(one,eweps),ewtabF),_mm_mul_ps(eweps,ewtabFn));
1275             felec            = _mm_mul_ps(_mm_mul_ps(qq20,rinv20),_mm_sub_ps(rinvsq20,felec));
1276
1277             cutoff_mask      = _mm_cmplt_ps(rsq20,rcutoff2);
1278
1279             fscal            = felec;
1280
1281             fscal            = _mm_and_ps(fscal,cutoff_mask);
1282
1283             fscal            = _mm_andnot_ps(dummy_mask,fscal);
1284
1285             /* Calculate temporary vectorial force */
1286             tx               = _mm_mul_ps(fscal,dx20);
1287             ty               = _mm_mul_ps(fscal,dy20);
1288             tz               = _mm_mul_ps(fscal,dz20);
1289
1290             /* Update vectorial force */
1291             fix2             = _mm_add_ps(fix2,tx);
1292             fiy2             = _mm_add_ps(fiy2,ty);
1293             fiz2             = _mm_add_ps(fiz2,tz);
1294
1295             fjx0             = _mm_add_ps(fjx0,tx);
1296             fjy0             = _mm_add_ps(fjy0,ty);
1297             fjz0             = _mm_add_ps(fjz0,tz);
1298             
1299             }
1300
1301             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
1302             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
1303             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
1304             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
1305
1306             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
1307
1308             /* Inner loop uses 127 flops */
1309         }
1310
1311         /* End of innermost loop */
1312
1313         gmx_mm_update_iforce_3atom_swizzle_ps(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,
1314                                               f+i_coord_offset,fshift+i_shift_offset);
1315
1316         /* Increment number of inner iterations */
1317         inneriter                  += j_index_end - j_index_start;
1318
1319         /* Outer loop uses 18 flops */
1320     }
1321
1322     /* Increment number of outer iterations */
1323     outeriter        += nri;
1324
1325     /* Update outer/inner flops */
1326
1327     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_W3_F,outeriter*18 + inneriter*127);
1328 }