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