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