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