Added option to gmx nmeig to print ZPE.
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_sse4_1_single / nb_kernel_ElecEw_VdwNone_GeomP1P1_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_ElecEw_VdwNone_GeomP1P1_VF_sse4_1_single
51  * Electrostatics interaction: Ewald
52  * VdW interaction:            None
53  * Geometry:                   Particle-Particle
54  * Calculate force/pot:        PotentialAndForce
55  */
56 void
57 nb_kernel_ElecEw_VdwNone_GeomP1P1_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              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
85     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
86     __m128           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
87     __m128           velec,felec,velecsum,facel,crf,krf,krf2;
88     real             *charge;
89     __m128i          ewitab;
90     __m128           ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
91     real             *ewtab;
92     __m128           dummy_mask,cutoff_mask;
93     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
94     __m128           one     = _mm_set1_ps(1.0);
95     __m128           two     = _mm_set1_ps(2.0);
96     x                = xx[0];
97     f                = ff[0];
98
99     nri              = nlist->nri;
100     iinr             = nlist->iinr;
101     jindex           = nlist->jindex;
102     jjnr             = nlist->jjnr;
103     shiftidx         = nlist->shift;
104     gid              = nlist->gid;
105     shiftvec         = fr->shift_vec[0];
106     fshift           = fr->fshift[0];
107     facel            = _mm_set1_ps(fr->ic->epsfac);
108     charge           = mdatoms->chargeA;
109
110     sh_ewald         = _mm_set1_ps(fr->ic->sh_ewald);
111     ewtab            = fr->ic->tabq_coul_FDV0;
112     ewtabscale       = _mm_set1_ps(fr->ic->tabq_scale);
113     ewtabhalfspace   = _mm_set1_ps(0.5/fr->ic->tabq_scale);
114
115     /* Avoid stupid compiler warnings */
116     jnrA = jnrB = jnrC = jnrD = 0;
117     j_coord_offsetA = 0;
118     j_coord_offsetB = 0;
119     j_coord_offsetC = 0;
120     j_coord_offsetD = 0;
121
122     outeriter        = 0;
123     inneriter        = 0;
124
125     for(iidx=0;iidx<4*DIM;iidx++)
126     {
127         scratch[iidx] = 0.0;
128     }
129
130     /* Start outer loop over neighborlists */
131     for(iidx=0; iidx<nri; iidx++)
132     {
133         /* Load shift vector for this list */
134         i_shift_offset   = DIM*shiftidx[iidx];
135
136         /* Load limits for loop over neighbors */
137         j_index_start    = jindex[iidx];
138         j_index_end      = jindex[iidx+1];
139
140         /* Get outer coordinate index */
141         inr              = iinr[iidx];
142         i_coord_offset   = DIM*inr;
143
144         /* Load i particle coords and add shift vector */
145         gmx_mm_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
146
147         fix0             = _mm_setzero_ps();
148         fiy0             = _mm_setzero_ps();
149         fiz0             = _mm_setzero_ps();
150
151         /* Load parameters for i particles */
152         iq0              = _mm_mul_ps(facel,_mm_load1_ps(charge+inr+0));
153
154         /* Reset potential sums */
155         velecsum         = _mm_setzero_ps();
156
157         /* Start inner kernel loop */
158         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
159         {
160
161             /* Get j neighbor index, and coordinate index */
162             jnrA             = jjnr[jidx];
163             jnrB             = jjnr[jidx+1];
164             jnrC             = jjnr[jidx+2];
165             jnrD             = jjnr[jidx+3];
166             j_coord_offsetA  = DIM*jnrA;
167             j_coord_offsetB  = DIM*jnrB;
168             j_coord_offsetC  = DIM*jnrC;
169             j_coord_offsetD  = DIM*jnrD;
170
171             /* load j atom coordinates */
172             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
173                                               x+j_coord_offsetC,x+j_coord_offsetD,
174                                               &jx0,&jy0,&jz0);
175
176             /* Calculate displacement vector */
177             dx00             = _mm_sub_ps(ix0,jx0);
178             dy00             = _mm_sub_ps(iy0,jy0);
179             dz00             = _mm_sub_ps(iz0,jz0);
180
181             /* Calculate squared distance and things based on it */
182             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
183
184             rinv00           = sse41_invsqrt_f(rsq00);
185
186             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
187
188             /* Load parameters for j particles */
189             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
190                                                               charge+jnrC+0,charge+jnrD+0);
191
192             /**************************
193              * CALCULATE INTERACTIONS *
194              **************************/
195
196             r00              = _mm_mul_ps(rsq00,rinv00);
197
198             /* Compute parameters for interactions between i and j atoms */
199             qq00             = _mm_mul_ps(iq0,jq0);
200
201             /* EWALD ELECTROSTATICS */
202
203             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
204             ewrt             = _mm_mul_ps(r00,ewtabscale);
205             ewitab           = _mm_cvttps_epi32(ewrt);
206             eweps            = _mm_sub_ps(ewrt,_mm_round_ps(ewrt, _MM_FROUND_FLOOR));
207             ewitab           = _mm_slli_epi32(ewitab,2);
208             ewtabF           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,0) );
209             ewtabD           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,1) );
210             ewtabV           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,2) );
211             ewtabFn          = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,3) );
212             _MM_TRANSPOSE4_PS(ewtabF,ewtabD,ewtabV,ewtabFn);
213             felec            = _mm_add_ps(ewtabF,_mm_mul_ps(eweps,ewtabD));
214             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
215             velec            = _mm_mul_ps(qq00,_mm_sub_ps(rinv00,velec));
216             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
217
218             /* Update potential sum for this i atom from the interaction with this j atom. */
219             velecsum         = _mm_add_ps(velecsum,velec);
220
221             fscal            = felec;
222
223             /* Calculate temporary vectorial force */
224             tx               = _mm_mul_ps(fscal,dx00);
225             ty               = _mm_mul_ps(fscal,dy00);
226             tz               = _mm_mul_ps(fscal,dz00);
227
228             /* Update vectorial force */
229             fix0             = _mm_add_ps(fix0,tx);
230             fiy0             = _mm_add_ps(fiy0,ty);
231             fiz0             = _mm_add_ps(fiz0,tz);
232
233             fjptrA             = f+j_coord_offsetA;
234             fjptrB             = f+j_coord_offsetB;
235             fjptrC             = f+j_coord_offsetC;
236             fjptrD             = f+j_coord_offsetD;
237             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
238
239             /* Inner loop uses 41 flops */
240         }
241
242         if(jidx<j_index_end)
243         {
244
245             /* Get j neighbor index, and coordinate index */
246             jnrlistA         = jjnr[jidx];
247             jnrlistB         = jjnr[jidx+1];
248             jnrlistC         = jjnr[jidx+2];
249             jnrlistD         = jjnr[jidx+3];
250             /* Sign of each element will be negative for non-real atoms.
251              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
252              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
253              */
254             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
255             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
256             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
257             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
258             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
259             j_coord_offsetA  = DIM*jnrA;
260             j_coord_offsetB  = DIM*jnrB;
261             j_coord_offsetC  = DIM*jnrC;
262             j_coord_offsetD  = DIM*jnrD;
263
264             /* load j atom coordinates */
265             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
266                                               x+j_coord_offsetC,x+j_coord_offsetD,
267                                               &jx0,&jy0,&jz0);
268
269             /* Calculate displacement vector */
270             dx00             = _mm_sub_ps(ix0,jx0);
271             dy00             = _mm_sub_ps(iy0,jy0);
272             dz00             = _mm_sub_ps(iz0,jz0);
273
274             /* Calculate squared distance and things based on it */
275             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
276
277             rinv00           = sse41_invsqrt_f(rsq00);
278
279             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
280
281             /* Load parameters for j particles */
282             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
283                                                               charge+jnrC+0,charge+jnrD+0);
284
285             /**************************
286              * CALCULATE INTERACTIONS *
287              **************************/
288
289             r00              = _mm_mul_ps(rsq00,rinv00);
290             r00              = _mm_andnot_ps(dummy_mask,r00);
291
292             /* Compute parameters for interactions between i and j atoms */
293             qq00             = _mm_mul_ps(iq0,jq0);
294
295             /* EWALD ELECTROSTATICS */
296
297             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
298             ewrt             = _mm_mul_ps(r00,ewtabscale);
299             ewitab           = _mm_cvttps_epi32(ewrt);
300             eweps            = _mm_sub_ps(ewrt,_mm_round_ps(ewrt, _MM_FROUND_FLOOR));
301             ewitab           = _mm_slli_epi32(ewitab,2);
302             ewtabF           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,0) );
303             ewtabD           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,1) );
304             ewtabV           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,2) );
305             ewtabFn          = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,3) );
306             _MM_TRANSPOSE4_PS(ewtabF,ewtabD,ewtabV,ewtabFn);
307             felec            = _mm_add_ps(ewtabF,_mm_mul_ps(eweps,ewtabD));
308             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
309             velec            = _mm_mul_ps(qq00,_mm_sub_ps(rinv00,velec));
310             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
311
312             /* Update potential sum for this i atom from the interaction with this j atom. */
313             velec            = _mm_andnot_ps(dummy_mask,velec);
314             velecsum         = _mm_add_ps(velecsum,velec);
315
316             fscal            = felec;
317
318             fscal            = _mm_andnot_ps(dummy_mask,fscal);
319
320             /* Calculate temporary vectorial force */
321             tx               = _mm_mul_ps(fscal,dx00);
322             ty               = _mm_mul_ps(fscal,dy00);
323             tz               = _mm_mul_ps(fscal,dz00);
324
325             /* Update vectorial force */
326             fix0             = _mm_add_ps(fix0,tx);
327             fiy0             = _mm_add_ps(fiy0,ty);
328             fiz0             = _mm_add_ps(fiz0,tz);
329
330             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
331             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
332             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
333             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
334             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
335
336             /* Inner loop uses 42 flops */
337         }
338
339         /* End of innermost loop */
340
341         gmx_mm_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
342                                               f+i_coord_offset,fshift+i_shift_offset);
343
344         ggid                        = gid[iidx];
345         /* Update potential energies */
346         gmx_mm_update_1pot_ps(velecsum,kernel_data->energygrp_elec+ggid);
347
348         /* Increment number of inner iterations */
349         inneriter                  += j_index_end - j_index_start;
350
351         /* Outer loop uses 8 flops */
352     }
353
354     /* Increment number of outer iterations */
355     outeriter        += nri;
356
357     /* Update outer/inner flops */
358
359     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VF,outeriter*8 + inneriter*42);
360 }
361 /*
362  * Gromacs nonbonded kernel:   nb_kernel_ElecEw_VdwNone_GeomP1P1_F_sse4_1_single
363  * Electrostatics interaction: Ewald
364  * VdW interaction:            None
365  * Geometry:                   Particle-Particle
366  * Calculate force/pot:        Force
367  */
368 void
369 nb_kernel_ElecEw_VdwNone_GeomP1P1_F_sse4_1_single
370                     (t_nblist                    * gmx_restrict       nlist,
371                      rvec                        * gmx_restrict          xx,
372                      rvec                        * gmx_restrict          ff,
373                      struct t_forcerec           * gmx_restrict          fr,
374                      t_mdatoms                   * gmx_restrict     mdatoms,
375                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
376                      t_nrnb                      * gmx_restrict        nrnb)
377 {
378     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
379      * just 0 for non-waters.
380      * Suffixes A,B,C,D refer to j loop unrolling done with SSE, e.g. for the four different
381      * jnr indices corresponding to data put in the four positions in the SIMD register.
382      */
383     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
384     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
385     int              jnrA,jnrB,jnrC,jnrD;
386     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
387     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
388     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
389     real             rcutoff_scalar;
390     real             *shiftvec,*fshift,*x,*f;
391     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
392     real             scratch[4*DIM];
393     __m128           tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
394     int              vdwioffset0;
395     __m128           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
396     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
397     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
398     __m128           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
399     __m128           velec,felec,velecsum,facel,crf,krf,krf2;
400     real             *charge;
401     __m128i          ewitab;
402     __m128           ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
403     real             *ewtab;
404     __m128           dummy_mask,cutoff_mask;
405     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
406     __m128           one     = _mm_set1_ps(1.0);
407     __m128           two     = _mm_set1_ps(2.0);
408     x                = xx[0];
409     f                = ff[0];
410
411     nri              = nlist->nri;
412     iinr             = nlist->iinr;
413     jindex           = nlist->jindex;
414     jjnr             = nlist->jjnr;
415     shiftidx         = nlist->shift;
416     gid              = nlist->gid;
417     shiftvec         = fr->shift_vec[0];
418     fshift           = fr->fshift[0];
419     facel            = _mm_set1_ps(fr->ic->epsfac);
420     charge           = mdatoms->chargeA;
421
422     sh_ewald         = _mm_set1_ps(fr->ic->sh_ewald);
423     ewtab            = fr->ic->tabq_coul_F;
424     ewtabscale       = _mm_set1_ps(fr->ic->tabq_scale);
425     ewtabhalfspace   = _mm_set1_ps(0.5/fr->ic->tabq_scale);
426
427     /* Avoid stupid compiler warnings */
428     jnrA = jnrB = jnrC = jnrD = 0;
429     j_coord_offsetA = 0;
430     j_coord_offsetB = 0;
431     j_coord_offsetC = 0;
432     j_coord_offsetD = 0;
433
434     outeriter        = 0;
435     inneriter        = 0;
436
437     for(iidx=0;iidx<4*DIM;iidx++)
438     {
439         scratch[iidx] = 0.0;
440     }
441
442     /* Start outer loop over neighborlists */
443     for(iidx=0; iidx<nri; iidx++)
444     {
445         /* Load shift vector for this list */
446         i_shift_offset   = DIM*shiftidx[iidx];
447
448         /* Load limits for loop over neighbors */
449         j_index_start    = jindex[iidx];
450         j_index_end      = jindex[iidx+1];
451
452         /* Get outer coordinate index */
453         inr              = iinr[iidx];
454         i_coord_offset   = DIM*inr;
455
456         /* Load i particle coords and add shift vector */
457         gmx_mm_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
458
459         fix0             = _mm_setzero_ps();
460         fiy0             = _mm_setzero_ps();
461         fiz0             = _mm_setzero_ps();
462
463         /* Load parameters for i particles */
464         iq0              = _mm_mul_ps(facel,_mm_load1_ps(charge+inr+0));
465
466         /* Start inner kernel loop */
467         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
468         {
469
470             /* Get j neighbor index, and coordinate index */
471             jnrA             = jjnr[jidx];
472             jnrB             = jjnr[jidx+1];
473             jnrC             = jjnr[jidx+2];
474             jnrD             = jjnr[jidx+3];
475             j_coord_offsetA  = DIM*jnrA;
476             j_coord_offsetB  = DIM*jnrB;
477             j_coord_offsetC  = DIM*jnrC;
478             j_coord_offsetD  = DIM*jnrD;
479
480             /* load j atom coordinates */
481             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
482                                               x+j_coord_offsetC,x+j_coord_offsetD,
483                                               &jx0,&jy0,&jz0);
484
485             /* Calculate displacement vector */
486             dx00             = _mm_sub_ps(ix0,jx0);
487             dy00             = _mm_sub_ps(iy0,jy0);
488             dz00             = _mm_sub_ps(iz0,jz0);
489
490             /* Calculate squared distance and things based on it */
491             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
492
493             rinv00           = sse41_invsqrt_f(rsq00);
494
495             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
496
497             /* Load parameters for j particles */
498             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
499                                                               charge+jnrC+0,charge+jnrD+0);
500
501             /**************************
502              * CALCULATE INTERACTIONS *
503              **************************/
504
505             r00              = _mm_mul_ps(rsq00,rinv00);
506
507             /* Compute parameters for interactions between i and j atoms */
508             qq00             = _mm_mul_ps(iq0,jq0);
509
510             /* EWALD ELECTROSTATICS */
511
512             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
513             ewrt             = _mm_mul_ps(r00,ewtabscale);
514             ewitab           = _mm_cvttps_epi32(ewrt);
515             eweps            = _mm_sub_ps(ewrt,_mm_round_ps(ewrt, _MM_FROUND_FLOOR));
516             gmx_mm_load_4pair_swizzle_ps(ewtab + gmx_mm_extract_epi32(ewitab,0),ewtab + gmx_mm_extract_epi32(ewitab,1),
517                                          ewtab + gmx_mm_extract_epi32(ewitab,2),ewtab + gmx_mm_extract_epi32(ewitab,3),
518                                          &ewtabF,&ewtabFn);
519             felec            = _mm_add_ps(_mm_mul_ps( _mm_sub_ps(one,eweps),ewtabF),_mm_mul_ps(eweps,ewtabFn));
520             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
521
522             fscal            = felec;
523
524             /* Calculate temporary vectorial force */
525             tx               = _mm_mul_ps(fscal,dx00);
526             ty               = _mm_mul_ps(fscal,dy00);
527             tz               = _mm_mul_ps(fscal,dz00);
528
529             /* Update vectorial force */
530             fix0             = _mm_add_ps(fix0,tx);
531             fiy0             = _mm_add_ps(fiy0,ty);
532             fiz0             = _mm_add_ps(fiz0,tz);
533
534             fjptrA             = f+j_coord_offsetA;
535             fjptrB             = f+j_coord_offsetB;
536             fjptrC             = f+j_coord_offsetC;
537             fjptrD             = f+j_coord_offsetD;
538             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
539
540             /* Inner loop uses 36 flops */
541         }
542
543         if(jidx<j_index_end)
544         {
545
546             /* Get j neighbor index, and coordinate index */
547             jnrlistA         = jjnr[jidx];
548             jnrlistB         = jjnr[jidx+1];
549             jnrlistC         = jjnr[jidx+2];
550             jnrlistD         = jjnr[jidx+3];
551             /* Sign of each element will be negative for non-real atoms.
552              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
553              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
554              */
555             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
556             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
557             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
558             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
559             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
560             j_coord_offsetA  = DIM*jnrA;
561             j_coord_offsetB  = DIM*jnrB;
562             j_coord_offsetC  = DIM*jnrC;
563             j_coord_offsetD  = DIM*jnrD;
564
565             /* load j atom coordinates */
566             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
567                                               x+j_coord_offsetC,x+j_coord_offsetD,
568                                               &jx0,&jy0,&jz0);
569
570             /* Calculate displacement vector */
571             dx00             = _mm_sub_ps(ix0,jx0);
572             dy00             = _mm_sub_ps(iy0,jy0);
573             dz00             = _mm_sub_ps(iz0,jz0);
574
575             /* Calculate squared distance and things based on it */
576             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
577
578             rinv00           = sse41_invsqrt_f(rsq00);
579
580             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
581
582             /* Load parameters for j particles */
583             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
584                                                               charge+jnrC+0,charge+jnrD+0);
585
586             /**************************
587              * CALCULATE INTERACTIONS *
588              **************************/
589
590             r00              = _mm_mul_ps(rsq00,rinv00);
591             r00              = _mm_andnot_ps(dummy_mask,r00);
592
593             /* Compute parameters for interactions between i and j atoms */
594             qq00             = _mm_mul_ps(iq0,jq0);
595
596             /* EWALD ELECTROSTATICS */
597
598             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
599             ewrt             = _mm_mul_ps(r00,ewtabscale);
600             ewitab           = _mm_cvttps_epi32(ewrt);
601             eweps            = _mm_sub_ps(ewrt,_mm_round_ps(ewrt, _MM_FROUND_FLOOR));
602             gmx_mm_load_4pair_swizzle_ps(ewtab + gmx_mm_extract_epi32(ewitab,0),ewtab + gmx_mm_extract_epi32(ewitab,1),
603                                          ewtab + gmx_mm_extract_epi32(ewitab,2),ewtab + gmx_mm_extract_epi32(ewitab,3),
604                                          &ewtabF,&ewtabFn);
605             felec            = _mm_add_ps(_mm_mul_ps( _mm_sub_ps(one,eweps),ewtabF),_mm_mul_ps(eweps,ewtabFn));
606             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
607
608             fscal            = felec;
609
610             fscal            = _mm_andnot_ps(dummy_mask,fscal);
611
612             /* Calculate temporary vectorial force */
613             tx               = _mm_mul_ps(fscal,dx00);
614             ty               = _mm_mul_ps(fscal,dy00);
615             tz               = _mm_mul_ps(fscal,dz00);
616
617             /* Update vectorial force */
618             fix0             = _mm_add_ps(fix0,tx);
619             fiy0             = _mm_add_ps(fiy0,ty);
620             fiz0             = _mm_add_ps(fiz0,tz);
621
622             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
623             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
624             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
625             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
626             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
627
628             /* Inner loop uses 37 flops */
629         }
630
631         /* End of innermost loop */
632
633         gmx_mm_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
634                                               f+i_coord_offset,fshift+i_shift_offset);
635
636         /* Increment number of inner iterations */
637         inneriter                  += j_index_end - j_index_start;
638
639         /* Outer loop uses 7 flops */
640     }
641
642     /* Increment number of outer iterations */
643     outeriter        += nri;
644
645     /* Update outer/inner flops */
646
647     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_F,outeriter*7 + inneriter*37);
648 }