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