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