Compile nonbonded kernels as C++
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_sse2_single / nb_kernel_ElecEwSh_VdwNone_GeomP1P1_sse2_single.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2017,2018, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*
36  * Note: this file was generated by the GROMACS sse2_single kernel generator.
37  */
38 #include "gmxpre.h"
39
40 #include "config.h"
41
42 #include <math.h>
43
44 #include "../nb_kernel.h"
45 #include "gromacs/gmxlib/nrnb.h"
46
47 #include "kernelutil_x86_sse2_single.h"
48
49 /*
50  * Gromacs nonbonded kernel:   nb_kernel_ElecEwSh_VdwNone_GeomP1P1_VF_sse2_single
51  * Electrostatics interaction: Ewald
52  * VdW interaction:            None
53  * Geometry:                   Particle-Particle
54  * Calculate force/pot:        PotentialAndForce
55  */
56 void
57 nb_kernel_ElecEwSh_VdwNone_GeomP1P1_VF_sse2_single
58                     (t_nblist                    * gmx_restrict       nlist,
59                      rvec                        * gmx_restrict          xx,
60                      rvec                        * gmx_restrict          ff,
61                      struct t_forcerec           * gmx_restrict          fr,
62                      t_mdatoms                   * gmx_restrict     mdatoms,
63                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
64                      t_nrnb                      * gmx_restrict        nrnb)
65 {
66     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
67      * just 0 for non-waters.
68      * Suffixes A,B,C,D refer to j loop unrolling done with SSE, e.g. for the four different
69      * jnr indices corresponding to data put in the four positions in the SIMD register.
70      */
71     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
72     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
73     int              jnrA,jnrB,jnrC,jnrD;
74     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
75     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
76     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
77     real             rcutoff_scalar;
78     real             *shiftvec,*fshift,*x,*f;
79     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
80     real             scratch[4*DIM];
81     __m128           tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
82     int              vdwioffset0;
83     __m128           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
84     int              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     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
116     rcutoff_scalar   = fr->ic->rcoulomb;
117     rcutoff          = _mm_set1_ps(rcutoff_scalar);
118     rcutoff2         = _mm_mul_ps(rcutoff,rcutoff);
119
120     /* Avoid stupid compiler warnings */
121     jnrA = jnrB = jnrC = jnrD = 0;
122     j_coord_offsetA = 0;
123     j_coord_offsetB = 0;
124     j_coord_offsetC = 0;
125     j_coord_offsetD = 0;
126
127     outeriter        = 0;
128     inneriter        = 0;
129
130     for(iidx=0;iidx<4*DIM;iidx++)
131     {
132         scratch[iidx] = 0.0;
133     }  
134
135     /* Start outer loop over neighborlists */
136     for(iidx=0; iidx<nri; iidx++)
137     {
138         /* Load shift vector for this list */
139         i_shift_offset   = DIM*shiftidx[iidx];
140
141         /* Load limits for loop over neighbors */
142         j_index_start    = jindex[iidx];
143         j_index_end      = jindex[iidx+1];
144
145         /* Get outer coordinate index */
146         inr              = iinr[iidx];
147         i_coord_offset   = DIM*inr;
148
149         /* Load i particle coords and add shift vector */
150         gmx_mm_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
151         
152         fix0             = _mm_setzero_ps();
153         fiy0             = _mm_setzero_ps();
154         fiz0             = _mm_setzero_ps();
155
156         /* Load parameters for i particles */
157         iq0              = _mm_mul_ps(facel,_mm_load1_ps(charge+inr+0));
158
159         /* Reset potential sums */
160         velecsum         = _mm_setzero_ps();
161
162         /* Start inner kernel loop */
163         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
164         {
165
166             /* Get j neighbor index, and coordinate index */
167             jnrA             = jjnr[jidx];
168             jnrB             = jjnr[jidx+1];
169             jnrC             = jjnr[jidx+2];
170             jnrD             = jjnr[jidx+3];
171             j_coord_offsetA  = DIM*jnrA;
172             j_coord_offsetB  = DIM*jnrB;
173             j_coord_offsetC  = DIM*jnrC;
174             j_coord_offsetD  = DIM*jnrD;
175
176             /* load j atom coordinates */
177             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
178                                               x+j_coord_offsetC,x+j_coord_offsetD,
179                                               &jx0,&jy0,&jz0);
180
181             /* Calculate displacement vector */
182             dx00             = _mm_sub_ps(ix0,jx0);
183             dy00             = _mm_sub_ps(iy0,jy0);
184             dz00             = _mm_sub_ps(iz0,jz0);
185
186             /* Calculate squared distance and things based on it */
187             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
188
189             rinv00           = sse2_invsqrt_f(rsq00);
190
191             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
192
193             /* Load parameters for j particles */
194             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
195                                                               charge+jnrC+0,charge+jnrD+0);
196
197             /**************************
198              * CALCULATE INTERACTIONS *
199              **************************/
200
201             if (gmx_mm_any_lt(rsq00,rcutoff2))
202             {
203
204             r00              = _mm_mul_ps(rsq00,rinv00);
205
206             /* Compute parameters for interactions between i and j atoms */
207             qq00             = _mm_mul_ps(iq0,jq0);
208
209             /* EWALD ELECTROSTATICS */
210
211             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
212             ewrt             = _mm_mul_ps(r00,ewtabscale);
213             ewitab           = _mm_cvttps_epi32(ewrt);
214             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
215             ewitab           = _mm_slli_epi32(ewitab,2);
216             ewtabF           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,0) );
217             ewtabD           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,1) );
218             ewtabV           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,2) );
219             ewtabFn          = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,3) );
220             _MM_TRANSPOSE4_PS(ewtabF,ewtabD,ewtabV,ewtabFn);
221             felec            = _mm_add_ps(ewtabF,_mm_mul_ps(eweps,ewtabD));
222             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
223             velec            = _mm_mul_ps(qq00,_mm_sub_ps(_mm_sub_ps(rinv00,sh_ewald),velec));
224             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
225
226             cutoff_mask      = _mm_cmplt_ps(rsq00,rcutoff2);
227
228             /* Update potential sum for this i atom from the interaction with this j atom. */
229             velec            = _mm_and_ps(velec,cutoff_mask);
230             velecsum         = _mm_add_ps(velecsum,velec);
231
232             fscal            = felec;
233
234             fscal            = _mm_and_ps(fscal,cutoff_mask);
235
236             /* Calculate temporary vectorial force */
237             tx               = _mm_mul_ps(fscal,dx00);
238             ty               = _mm_mul_ps(fscal,dy00);
239             tz               = _mm_mul_ps(fscal,dz00);
240
241             /* Update vectorial force */
242             fix0             = _mm_add_ps(fix0,tx);
243             fiy0             = _mm_add_ps(fiy0,ty);
244             fiz0             = _mm_add_ps(fiz0,tz);
245
246             fjptrA             = f+j_coord_offsetA;
247             fjptrB             = f+j_coord_offsetB;
248             fjptrC             = f+j_coord_offsetC;
249             fjptrD             = f+j_coord_offsetD;
250             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
251             
252             }
253
254             /* Inner loop uses 46 flops */
255         }
256
257         if(jidx<j_index_end)
258         {
259
260             /* Get j neighbor index, and coordinate index */
261             jnrlistA         = jjnr[jidx];
262             jnrlistB         = jjnr[jidx+1];
263             jnrlistC         = jjnr[jidx+2];
264             jnrlistD         = jjnr[jidx+3];
265             /* Sign of each element will be negative for non-real atoms.
266              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
267              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
268              */
269             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
270             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
271             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
272             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
273             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
274             j_coord_offsetA  = DIM*jnrA;
275             j_coord_offsetB  = DIM*jnrB;
276             j_coord_offsetC  = DIM*jnrC;
277             j_coord_offsetD  = DIM*jnrD;
278
279             /* load j atom coordinates */
280             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
281                                               x+j_coord_offsetC,x+j_coord_offsetD,
282                                               &jx0,&jy0,&jz0);
283
284             /* Calculate displacement vector */
285             dx00             = _mm_sub_ps(ix0,jx0);
286             dy00             = _mm_sub_ps(iy0,jy0);
287             dz00             = _mm_sub_ps(iz0,jz0);
288
289             /* Calculate squared distance and things based on it */
290             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
291
292             rinv00           = sse2_invsqrt_f(rsq00);
293
294             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
295
296             /* Load parameters for j particles */
297             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
298                                                               charge+jnrC+0,charge+jnrD+0);
299
300             /**************************
301              * CALCULATE INTERACTIONS *
302              **************************/
303
304             if (gmx_mm_any_lt(rsq00,rcutoff2))
305             {
306
307             r00              = _mm_mul_ps(rsq00,rinv00);
308             r00              = _mm_andnot_ps(dummy_mask,r00);
309
310             /* Compute parameters for interactions between i and j atoms */
311             qq00             = _mm_mul_ps(iq0,jq0);
312
313             /* EWALD ELECTROSTATICS */
314
315             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
316             ewrt             = _mm_mul_ps(r00,ewtabscale);
317             ewitab           = _mm_cvttps_epi32(ewrt);
318             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
319             ewitab           = _mm_slli_epi32(ewitab,2);
320             ewtabF           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,0) );
321             ewtabD           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,1) );
322             ewtabV           = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,2) );
323             ewtabFn          = _mm_load_ps( ewtab + gmx_mm_extract_epi32(ewitab,3) );
324             _MM_TRANSPOSE4_PS(ewtabF,ewtabD,ewtabV,ewtabFn);
325             felec            = _mm_add_ps(ewtabF,_mm_mul_ps(eweps,ewtabD));
326             velec            = _mm_sub_ps(ewtabV,_mm_mul_ps(_mm_mul_ps(ewtabhalfspace,eweps),_mm_add_ps(ewtabF,felec)));
327             velec            = _mm_mul_ps(qq00,_mm_sub_ps(_mm_sub_ps(rinv00,sh_ewald),velec));
328             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
329
330             cutoff_mask      = _mm_cmplt_ps(rsq00,rcutoff2);
331
332             /* Update potential sum for this i atom from the interaction with this j atom. */
333             velec            = _mm_and_ps(velec,cutoff_mask);
334             velec            = _mm_andnot_ps(dummy_mask,velec);
335             velecsum         = _mm_add_ps(velecsum,velec);
336
337             fscal            = felec;
338
339             fscal            = _mm_and_ps(fscal,cutoff_mask);
340
341             fscal            = _mm_andnot_ps(dummy_mask,fscal);
342
343             /* Calculate temporary vectorial force */
344             tx               = _mm_mul_ps(fscal,dx00);
345             ty               = _mm_mul_ps(fscal,dy00);
346             tz               = _mm_mul_ps(fscal,dz00);
347
348             /* Update vectorial force */
349             fix0             = _mm_add_ps(fix0,tx);
350             fiy0             = _mm_add_ps(fiy0,ty);
351             fiz0             = _mm_add_ps(fiz0,tz);
352
353             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
354             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
355             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
356             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
357             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
358             
359             }
360
361             /* Inner loop uses 47 flops */
362         }
363
364         /* End of innermost loop */
365
366         gmx_mm_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
367                                               f+i_coord_offset,fshift+i_shift_offset);
368
369         ggid                        = gid[iidx];
370         /* Update potential energies */
371         gmx_mm_update_1pot_ps(velecsum,kernel_data->energygrp_elec+ggid);
372
373         /* Increment number of inner iterations */
374         inneriter                  += j_index_end - j_index_start;
375
376         /* Outer loop uses 8 flops */
377     }
378
379     /* Increment number of outer iterations */
380     outeriter        += nri;
381
382     /* Update outer/inner flops */
383
384     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VF,outeriter*8 + inneriter*47);
385 }
386 /*
387  * Gromacs nonbonded kernel:   nb_kernel_ElecEwSh_VdwNone_GeomP1P1_F_sse2_single
388  * Electrostatics interaction: Ewald
389  * VdW interaction:            None
390  * Geometry:                   Particle-Particle
391  * Calculate force/pot:        Force
392  */
393 void
394 nb_kernel_ElecEwSh_VdwNone_GeomP1P1_F_sse2_single
395                     (t_nblist                    * gmx_restrict       nlist,
396                      rvec                        * gmx_restrict          xx,
397                      rvec                        * gmx_restrict          ff,
398                      struct t_forcerec           * gmx_restrict          fr,
399                      t_mdatoms                   * gmx_restrict     mdatoms,
400                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
401                      t_nrnb                      * gmx_restrict        nrnb)
402 {
403     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
404      * just 0 for non-waters.
405      * Suffixes A,B,C,D refer to j loop unrolling done with SSE, e.g. for the four different
406      * jnr indices corresponding to data put in the four positions in the SIMD register.
407      */
408     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
409     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
410     int              jnrA,jnrB,jnrC,jnrD;
411     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
412     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
413     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
414     real             rcutoff_scalar;
415     real             *shiftvec,*fshift,*x,*f;
416     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
417     real             scratch[4*DIM];
418     __m128           tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
419     int              vdwioffset0;
420     __m128           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
421     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
422     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
423     __m128           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
424     __m128           velec,felec,velecsum,facel,crf,krf,krf2;
425     real             *charge;
426     __m128i          ewitab;
427     __m128           ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
428     real             *ewtab;
429     __m128           dummy_mask,cutoff_mask;
430     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
431     __m128           one     = _mm_set1_ps(1.0);
432     __m128           two     = _mm_set1_ps(2.0);
433     x                = xx[0];
434     f                = ff[0];
435
436     nri              = nlist->nri;
437     iinr             = nlist->iinr;
438     jindex           = nlist->jindex;
439     jjnr             = nlist->jjnr;
440     shiftidx         = nlist->shift;
441     gid              = nlist->gid;
442     shiftvec         = fr->shift_vec[0];
443     fshift           = fr->fshift[0];
444     facel            = _mm_set1_ps(fr->ic->epsfac);
445     charge           = mdatoms->chargeA;
446
447     sh_ewald         = _mm_set1_ps(fr->ic->sh_ewald);
448     ewtab            = fr->ic->tabq_coul_F;
449     ewtabscale       = _mm_set1_ps(fr->ic->tabq_scale);
450     ewtabhalfspace   = _mm_set1_ps(0.5/fr->ic->tabq_scale);
451
452     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
453     rcutoff_scalar   = fr->ic->rcoulomb;
454     rcutoff          = _mm_set1_ps(rcutoff_scalar);
455     rcutoff2         = _mm_mul_ps(rcutoff,rcutoff);
456
457     /* Avoid stupid compiler warnings */
458     jnrA = jnrB = jnrC = jnrD = 0;
459     j_coord_offsetA = 0;
460     j_coord_offsetB = 0;
461     j_coord_offsetC = 0;
462     j_coord_offsetD = 0;
463
464     outeriter        = 0;
465     inneriter        = 0;
466
467     for(iidx=0;iidx<4*DIM;iidx++)
468     {
469         scratch[iidx] = 0.0;
470     }  
471
472     /* Start outer loop over neighborlists */
473     for(iidx=0; iidx<nri; iidx++)
474     {
475         /* Load shift vector for this list */
476         i_shift_offset   = DIM*shiftidx[iidx];
477
478         /* Load limits for loop over neighbors */
479         j_index_start    = jindex[iidx];
480         j_index_end      = jindex[iidx+1];
481
482         /* Get outer coordinate index */
483         inr              = iinr[iidx];
484         i_coord_offset   = DIM*inr;
485
486         /* Load i particle coords and add shift vector */
487         gmx_mm_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
488         
489         fix0             = _mm_setzero_ps();
490         fiy0             = _mm_setzero_ps();
491         fiz0             = _mm_setzero_ps();
492
493         /* Load parameters for i particles */
494         iq0              = _mm_mul_ps(facel,_mm_load1_ps(charge+inr+0));
495
496         /* Start inner kernel loop */
497         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
498         {
499
500             /* Get j neighbor index, and coordinate index */
501             jnrA             = jjnr[jidx];
502             jnrB             = jjnr[jidx+1];
503             jnrC             = jjnr[jidx+2];
504             jnrD             = jjnr[jidx+3];
505             j_coord_offsetA  = DIM*jnrA;
506             j_coord_offsetB  = DIM*jnrB;
507             j_coord_offsetC  = DIM*jnrC;
508             j_coord_offsetD  = DIM*jnrD;
509
510             /* load j atom coordinates */
511             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
512                                               x+j_coord_offsetC,x+j_coord_offsetD,
513                                               &jx0,&jy0,&jz0);
514
515             /* Calculate displacement vector */
516             dx00             = _mm_sub_ps(ix0,jx0);
517             dy00             = _mm_sub_ps(iy0,jy0);
518             dz00             = _mm_sub_ps(iz0,jz0);
519
520             /* Calculate squared distance and things based on it */
521             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
522
523             rinv00           = sse2_invsqrt_f(rsq00);
524
525             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
526
527             /* Load parameters for j particles */
528             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
529                                                               charge+jnrC+0,charge+jnrD+0);
530
531             /**************************
532              * CALCULATE INTERACTIONS *
533              **************************/
534
535             if (gmx_mm_any_lt(rsq00,rcutoff2))
536             {
537
538             r00              = _mm_mul_ps(rsq00,rinv00);
539
540             /* Compute parameters for interactions between i and j atoms */
541             qq00             = _mm_mul_ps(iq0,jq0);
542
543             /* EWALD ELECTROSTATICS */
544
545             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
546             ewrt             = _mm_mul_ps(r00,ewtabscale);
547             ewitab           = _mm_cvttps_epi32(ewrt);
548             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
549             gmx_mm_load_4pair_swizzle_ps(ewtab+gmx_mm_extract_epi32(ewitab,0),ewtab+gmx_mm_extract_epi32(ewitab,1),
550                                          ewtab+gmx_mm_extract_epi32(ewitab,2),ewtab+gmx_mm_extract_epi32(ewitab,3),
551                                          &ewtabF,&ewtabFn);
552             felec            = _mm_add_ps(_mm_mul_ps( _mm_sub_ps(one,eweps),ewtabF),_mm_mul_ps(eweps,ewtabFn));
553             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
554
555             cutoff_mask      = _mm_cmplt_ps(rsq00,rcutoff2);
556
557             fscal            = felec;
558
559             fscal            = _mm_and_ps(fscal,cutoff_mask);
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             fjptrA             = f+j_coord_offsetA;
572             fjptrB             = f+j_coord_offsetB;
573             fjptrC             = f+j_coord_offsetC;
574             fjptrD             = f+j_coord_offsetD;
575             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
576             
577             }
578
579             /* Inner loop uses 39 flops */
580         }
581
582         if(jidx<j_index_end)
583         {
584
585             /* Get j neighbor index, and coordinate index */
586             jnrlistA         = jjnr[jidx];
587             jnrlistB         = jjnr[jidx+1];
588             jnrlistC         = jjnr[jidx+2];
589             jnrlistD         = jjnr[jidx+3];
590             /* Sign of each element will be negative for non-real atoms.
591              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
592              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
593              */
594             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
595             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
596             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
597             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
598             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
599             j_coord_offsetA  = DIM*jnrA;
600             j_coord_offsetB  = DIM*jnrB;
601             j_coord_offsetC  = DIM*jnrC;
602             j_coord_offsetD  = DIM*jnrD;
603
604             /* load j atom coordinates */
605             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
606                                               x+j_coord_offsetC,x+j_coord_offsetD,
607                                               &jx0,&jy0,&jz0);
608
609             /* Calculate displacement vector */
610             dx00             = _mm_sub_ps(ix0,jx0);
611             dy00             = _mm_sub_ps(iy0,jy0);
612             dz00             = _mm_sub_ps(iz0,jz0);
613
614             /* Calculate squared distance and things based on it */
615             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
616
617             rinv00           = sse2_invsqrt_f(rsq00);
618
619             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
620
621             /* Load parameters for j particles */
622             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
623                                                               charge+jnrC+0,charge+jnrD+0);
624
625             /**************************
626              * CALCULATE INTERACTIONS *
627              **************************/
628
629             if (gmx_mm_any_lt(rsq00,rcutoff2))
630             {
631
632             r00              = _mm_mul_ps(rsq00,rinv00);
633             r00              = _mm_andnot_ps(dummy_mask,r00);
634
635             /* Compute parameters for interactions between i and j atoms */
636             qq00             = _mm_mul_ps(iq0,jq0);
637
638             /* EWALD ELECTROSTATICS */
639
640             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
641             ewrt             = _mm_mul_ps(r00,ewtabscale);
642             ewitab           = _mm_cvttps_epi32(ewrt);
643             eweps            = _mm_sub_ps(ewrt,_mm_cvtepi32_ps(ewitab));
644             gmx_mm_load_4pair_swizzle_ps(ewtab+gmx_mm_extract_epi32(ewitab,0),ewtab+gmx_mm_extract_epi32(ewitab,1),
645                                          ewtab+gmx_mm_extract_epi32(ewitab,2),ewtab+gmx_mm_extract_epi32(ewitab,3),
646                                          &ewtabF,&ewtabFn);
647             felec            = _mm_add_ps(_mm_mul_ps( _mm_sub_ps(one,eweps),ewtabF),_mm_mul_ps(eweps,ewtabFn));
648             felec            = _mm_mul_ps(_mm_mul_ps(qq00,rinv00),_mm_sub_ps(rinvsq00,felec));
649
650             cutoff_mask      = _mm_cmplt_ps(rsq00,rcutoff2);
651
652             fscal            = felec;
653
654             fscal            = _mm_and_ps(fscal,cutoff_mask);
655
656             fscal            = _mm_andnot_ps(dummy_mask,fscal);
657
658             /* Calculate temporary vectorial force */
659             tx               = _mm_mul_ps(fscal,dx00);
660             ty               = _mm_mul_ps(fscal,dy00);
661             tz               = _mm_mul_ps(fscal,dz00);
662
663             /* Update vectorial force */
664             fix0             = _mm_add_ps(fix0,tx);
665             fiy0             = _mm_add_ps(fiy0,ty);
666             fiz0             = _mm_add_ps(fiz0,tz);
667
668             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
669             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
670             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
671             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
672             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,tx,ty,tz);
673             
674             }
675
676             /* Inner loop uses 40 flops */
677         }
678
679         /* End of innermost loop */
680
681         gmx_mm_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
682                                               f+i_coord_offset,fshift+i_shift_offset);
683
684         /* Increment number of inner iterations */
685         inneriter                  += j_index_end - j_index_start;
686
687         /* Outer loop uses 7 flops */
688     }
689
690     /* Increment number of outer iterations */
691     outeriter        += nri;
692
693     /* Update outer/inner flops */
694
695     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_F,outeriter*7 + inneriter*40);
696 }