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