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