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